sequenceMode.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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       //std::cout<<"INIT_SEQ"<<std::endl;
00048       _seqidx = 0;
00049       _int_inc = 0.0;
00050       _int_count = 0.0;
00051       //_color_old = getColor();
00052       _int_inc = 1.0/(_seqences[_seqidx].crosstime * UPDATE_RATE_HZ);
00053       _state = CROSSFADE;
00054       //std::cout<<"Setting color: "<<_seqences[_seqidx].color.r<<" "<<_seqences[_seqidx].color.g<<" "<<_seqences[_seqidx].color.b<<" "<<_seqences[_seqidx].color.a<<std::endl;
00055       break;
00056 
00057     case CROSSFADE:
00058       //std::cout<<"CROSSFADE"<<std::endl;
00059       //first execute cross fade between old and new color for current seq
00060       if(_int_count <= 1.0001)
00061       {
00062         //std::cout<<"_int_count "<<_int_count<<std::endl;
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       //calculate
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       //std::cout<<"NEXT"<<std::endl;
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       //std::cout<<"Setting color: "<<_seqences[_seqidx].color.r<<" "<<_seqences[_seqidx].color.g<<" "<<_seqences[_seqidx].color.b<<" "<<_seqences[_seqidx].color.a<<std::endl;
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 //    std::cout<<"Original h:"<<ca.h<<" s:"<<ca.s<<" v:"<<ca.v<<std::endl;
00138 //    std::cout<<"Original r:"<<a.r<<" g:"<<a.g<<" b:"<<a.b<<std::endl;
00139 //    std::cout<<"Goal     h:"<<cb.h<<" s:"<<cb.s<<" v:"<<cb.v<<std::endl;
00140 //    std::cout<<"Goal     r:"<<b.r<<" g:"<<b.g<<" b:"<<b.b<<std::endl;
00141 //    std::cout<<"New      h:"<<cr.h<<" s:"<<cr.s<<" v:"<<cr.v<<std::endl;
00142 //    std::cout<<"New      r:"<<result.r<<" g:"<<result.g<<" b:"<<result.b<<std::endl;
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


cob_light
Author(s): Benjamin Maidel
autogenerated on Sat Jun 8 2019 21:02:07