Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef SEQUENCEMODE_H
00019 #define SEQUENCEMODE_H
00020
00021 #include <mode.h>
00022
00023 typedef struct sequence
00024 {
00025 color::rgba color;
00026 double holdtime;
00027 double crosstime;
00028 }seq_t;
00029
00030 enum State{INIT_SEQ,CROSSFADE,HOLD,NEXT,BEGIN};
00031
00032 class SequenceMode : public Mode
00033 {
00034 public:
00035 SequenceMode(std::vector<seq_t> sequences, int priority = 0, double freq = 0.25, int pulses = 0, double timeout = 0)
00036 :Mode(priority, freq, pulses, timeout), _init(true), _int_inc(0.0), _int_count(0.0)
00037 {
00038 _seqences = sequences;
00039 _state = INIT_SEQ;
00040 }
00041
00042 void execute()
00043 {
00044 switch (_state)
00045 {
00046 case INIT_SEQ:
00047
00048 _seqidx = 0;
00049 _int_inc = 0.0;
00050 _int_count = 0.0;
00051
00052 _int_inc = 1.0/(_seqences[_seqidx].crosstime * UPDATE_RATE_HZ);
00053 _state = CROSSFADE;
00054
00055 break;
00056
00057 case CROSSFADE:
00058
00059
00060 if(_int_count <= 1.0001)
00061 {
00062
00063 _color = interpolateColor(_actualColor, _seqences[_seqidx].color, _int_count);
00064 _int_count += _int_inc;
00065 m_sigColorReady(_color);
00066 }
00067 else
00068 {
00069 _state = HOLD;
00070 _int_count = 0.0;
00071 _int_inc = 1.0/(_seqences[_seqidx].holdtime * UPDATE_RATE_HZ);
00072 _actualColor = _color;
00073 m_sigColorReady(_color);
00074 }
00075
00076 break;
00077
00078 case HOLD:
00079 if(_int_count < 1)
00080 _int_count += _int_inc;
00081 else
00082 _state = NEXT;
00083 break;
00084
00085 case NEXT:
00086
00087 _seqidx++;
00088 if(_seqidx == _seqences.size())
00089 _seqidx = 0;
00090 _int_inc = 0.0;
00091 _int_count = 0.0;
00092 _int_inc = 1.0/(_seqences[_seqidx].crosstime * UPDATE_RATE_HZ);
00093 _state = CROSSFADE;
00094
00095 break;
00096 }
00097 }
00098
00099 std::string getName(){ return std::string("SequenceMode"); }
00100
00101 private:
00102 std::vector<seq_t> _seqences;
00103 int _seqidx;
00104 bool _init;
00105 float _int_inc;
00106 float _int_count;
00107 int _state;
00108
00109 color::rgba _color;
00110
00111 color::rgba interpolateColor(color::rgba start, color::rgba goal, float t)
00112 {
00113 color::hsv ca;
00114 color::hsv cb;
00115 color::hsv cr;
00116 color::rgba a, b;
00117 a = start;
00118 b = goal;
00119
00120 a.r *= a.a;
00121 a.g *= a.a;
00122 a.b *= a.a;
00123 b.r *= b.a;
00124 b.g *= b.a;
00125 b.b *= b.a;
00126 color::Color::rgb2hsv(a.r, a.g, a.b, ca.h, ca.s, ca.v);
00127 color::Color::rgb2hsv(b.r, b.g, b.b, cb.h, cb.s, cb.v);
00128
00129 cr.h = linearInterpolate(ca.h, cb.h, t);
00130 cr.s = linearInterpolate(ca.s, cb.s, t);
00131 cr.v = linearInterpolate(ca.v, cb.v, t);
00132
00133 color::rgba result;
00134 color::Color::hsv2rgb(cr.h, cr.s, cr.v, result.r, result.g, result.b);
00135 result.a = 1.0;
00136
00137
00138
00139
00140
00141
00142
00143
00144 return result;
00145 }
00146
00147 float linearInterpolate(float a, float b, float t)
00148 {
00149 return a * (1 - t) + b * t;
00150 }
00151 };
00152
00153 #endif