sequenceMode.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef SEQUENCEMODE_H
19 #define SEQUENCEMODE_H
20 
21 #include <mode.h>
22 
23 typedef struct sequence
24 {
26  double holdtime;
27  double crosstime;
28 }seq_t;
29 
31 
32 class SequenceMode : public Mode
33 {
34 public:
35  SequenceMode(std::vector<seq_t> sequences, int priority = 0, double freq = 0.25, int pulses = 0, double timeout = 0)
36  :Mode(priority, freq, pulses, timeout), _init(true), _int_inc(0.0), _int_count(0.0)
37  {
38  _seqences = sequences;
39  _state = INIT_SEQ;
40  }
41 
42  void execute()
43  {
44  switch (_state)
45  {
46  case INIT_SEQ:
47  //std::cout<<"INIT_SEQ"<<std::endl;
48  _seqidx = 0;
49  _int_inc = 0.0;
50  _int_count = 0.0;
51  //_color_old = getColor();
52  _int_inc = 1.0/(_seqences[_seqidx].crosstime * UPDATE_RATE_HZ);
53  _state = CROSSFADE;
54  //std::cout<<"Setting color: "<<_seqences[_seqidx].color.r<<" "<<_seqences[_seqidx].color.g<<" "<<_seqences[_seqidx].color.b<<" "<<_seqences[_seqidx].color.a<<std::endl;
55  break;
56 
57  case CROSSFADE:
58  //std::cout<<"CROSSFADE"<<std::endl;
59  //first execute cross fade between old and new color for current seq
60  if(_int_count <= 1.0001)
61  {
62  //std::cout<<"_int_count "<<_int_count<<std::endl;
63  _color = interpolateColor(_actualColor, _seqences[_seqidx].color, _int_count);
64  _int_count += _int_inc;
65  m_sigColorReady(_color);
66  }
67  else
68  {
69  _state = HOLD;
70  _int_count = 0.0;
71  _int_inc = 1.0/(_seqences[_seqidx].holdtime * UPDATE_RATE_HZ);
72  _actualColor = _color;
73  m_sigColorReady(_color);
74  }
75  //calculate
76  break;
77 
78  case HOLD:
79  if(_int_count < 1)
80  _int_count += _int_inc;
81  else
82  _state = NEXT;
83  break;
84 
85  case NEXT:
86  //std::cout<<"NEXT"<<std::endl;
87  _seqidx++;
88  if(_seqidx == _seqences.size())
89  _seqidx = 0;
90  _int_inc = 0.0;
91  _int_count = 0.0;
92  _int_inc = 1.0/(_seqences[_seqidx].crosstime * UPDATE_RATE_HZ);
93  _state = CROSSFADE;
94  //std::cout<<"Setting color: "<<_seqences[_seqidx].color.r<<" "<<_seqences[_seqidx].color.g<<" "<<_seqences[_seqidx].color.b<<" "<<_seqences[_seqidx].color.a<<std::endl;
95  break;
96  }
97  }
98 
99  std::string getName(){ return std::string("SequenceMode"); }
100 
101 private:
102  std::vector<seq_t> _seqences;
103  int _seqidx;
104  bool _init;
105  float _int_inc;
106  float _int_count;
107  int _state;
108 
110 
112  {
113  color::hsv ca;
114  color::hsv cb;
115  color::hsv cr;
116  color::rgba a, b;
117  a = start;
118  b = goal;
119 
120  a.r *= a.a;
121  a.g *= a.a;
122  a.b *= a.a;
123  b.r *= b.a;
124  b.g *= b.a;
125  b.b *= b.a;
126  color::Color::rgb2hsv(a.r, a.g, a.b, ca.h, ca.s, ca.v);
127  color::Color::rgb2hsv(b.r, b.g, b.b, cb.h, cb.s, cb.v);
128 
129  cr.h = linearInterpolate(ca.h, cb.h, t);
130  cr.s = linearInterpolate(ca.s, cb.s, t);
131  cr.v = linearInterpolate(ca.v, cb.v, t);
132 
133  color::rgba result;
134  color::Color::hsv2rgb(cr.h, cr.s, cr.v, result.r, result.g, result.b);
135  result.a = 1.0;
136 
137 // std::cout<<"Original h:"<<ca.h<<" s:"<<ca.s<<" v:"<<ca.v<<std::endl;
138 // std::cout<<"Original r:"<<a.r<<" g:"<<a.g<<" b:"<<a.b<<std::endl;
139 // std::cout<<"Goal h:"<<cb.h<<" s:"<<cb.s<<" v:"<<cb.v<<std::endl;
140 // std::cout<<"Goal r:"<<b.r<<" g:"<<b.g<<" b:"<<b.b<<std::endl;
141 // std::cout<<"New h:"<<cr.h<<" s:"<<cr.s<<" v:"<<cr.v<<std::endl;
142 // std::cout<<"New r:"<<result.r<<" g:"<<result.g<<" b:"<<result.b<<std::endl;
143 
144  return result;
145  }
146 
147  float linearInterpolate(float a, float b, float t)
148  {
149  return a * (1 - t) + b * t;
150  }
151 };
152 
153 #endif
float linearInterpolate(float a, float b, float t)
Definition: sequenceMode.h:147
float _int_count
Definition: sequenceMode.h:106
static void rgb2hsv(float r, float g, float b, float &h, float &s, float &v)
Definition: colorUtils.h:54
std::vector< seq_t > _seqences
Definition: sequenceMode.h:102
Definition: mode.h:26
color::rgba color
Definition: sequenceMode.h:25
State
Definition: sequenceMode.h:30
SequenceMode(std::vector< seq_t > sequences, int priority=0, double freq=0.25, int pulses=0, double timeout=0)
Definition: sequenceMode.h:35
static void hsv2rgb(float h, float s, float v, float &r, float &g, float &b)
Definition: colorUtils.h:81
color::rgba _color
Definition: sequenceMode.h:109
double crosstime
Definition: sequenceMode.h:27
std::string getName()
Definition: sequenceMode.h:99
void execute()
Definition: sequenceMode.h:42
struct sequence seq_t
float v
Definition: colorUtils.h:48
float s
Definition: colorUtils.h:47
float h
Definition: colorUtils.h:46
double holdtime
Definition: sequenceMode.h:26
color::rgba interpolateColor(color::rgba start, color::rgba goal, float t)
Definition: sequenceMode.h:111


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39