HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
colour_control.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_ITF_COLOUR_CONTROL_H
18 #define HF_ITF_COLOUR_CONTROL_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/interface.h"
22 
23 namespace HF
24 {
25  namespace Interfaces
26  {
27  // Forward declaration.
28  namespace ColourControl
29  {
30  class IServer;
31  }
32 
48 
52  namespace ColourControl
53  {
63  typedef enum _CMD
65  {
66  MOVE_TO_HUE_CMD = 0x01,
67  MOVE_HUE_CMD = 0x02,
68  STEP_HUE_CMD = 0x03,
73  MOVE_TO_XY_CMD = 0x08,
74  MOVE_XY_CMD = 0x09,
75  STEP_XY_CMD = 0x0a,
77  STOP_CMD = 0x0c,
78  __LAST_CMD__ = STOP_CMD
79  } CMD;
80 
82  typedef enum _Attributes
83  {
84  SUPPORTED_ATTR = 0x01,
85  MODE_ATTR = 0x02,
87  XY_ATTR = 0x04,
89  __LAST_ATTR__ = COLOUR_TEMPERATURE_ATTR
90  } Attributes;
91 
93  typedef enum _Mask : uint8_t
94  {
95  HS_MODE = 0x01,
96  XY_MODE = 0x02,
98  __LAST_MODE__ = TEMPERATURE_MODE
99  } Mask;
100 
102  typedef enum _Direction : uint8_t
103  {
104  UP = 0x01,
105  DOWN = 0x02,
106  SHORTEST = 0x03,
107  LONGEST = 0x04,
108  __LAST_DIRECTION_ = LONGEST
109  } Direction;
110 
117  struct HS_Colour
118  {
119  uint16_t hue;
120  uint8_t saturation;
121 
122  HS_Colour() = default;
123 
130  HS_Colour(uint16_t _hue, uint8_t _saturation):
131  saturation(_saturation)
132  {
133  hue = _hue < HUE ? _hue : HUE - 1;
134  }
135 
136  static constexpr int HUE = 360;
137  static constexpr int SATURATION = 0x100;
138 
154  static int32_t invert_angle(int32_t angle, uint16_t max_value);
155 
170  static int32_t get_travel_distance(const Direction dir, uint16_t initial,
171  uint16_t final, uint16_t max);
172 
174  static constexpr uint16_t min_size = sizeof(hue) // hue
175  + sizeof(saturation); // saturation
176 
178  uint16_t size() const
179  {
180  return min_size;
181  }
182 
184  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
185 
187  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
188 
190  int compare(const HS_Colour &other) const
191  {
192  if (this->hue != other.hue)
193  {
194  return (this->hue - other.hue);
195  }
196  else
197  {
198  if (this->hue != other.hue)
199  {
200  return (this->hue - other.hue);
201  }
202  else
203  {
204  return 0;
205  }
206  }
207  }
208 
210  float changed(const HS_Colour &other) const
211  {
212  if (this->hue != other.hue)
213  {
214  return (100 * (this->hue - other.hue) / static_cast<float>(this->hue));
215  }
216  else
217  {
218  if (this->hue != other.hue)
219  {
220  return (100 * (this->hue - other.hue) / static_cast<float>(this->hue));
221  }
222  else
223  {
224  return 0.0f;
225  }
226  }
227  }
228 
230  bool operator==(const HS_Colour &other) const
231  {
232  return (hue == other.hue && saturation == other.saturation);
233  }
234 
236  bool operator!=(const HS_Colour &other) const
237  {
238  return !operator==(other);
239  }
240  };
241 
248  struct XY_Colour
249  {
250  uint16_t X;
251  uint16_t Y;
252 
253  XY_Colour() = default;
254 
261  XY_Colour(uint16_t X, uint16_t Y): X(X), Y(Y)
262  {}
263 
265  static constexpr uint16_t min_size = sizeof(X) // X
266  + sizeof(Y); // Y
267 
269  uint16_t size() const
270  {
271  return min_size;
272  }
273 
275  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
276 
278  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
279 
281  int compare(const XY_Colour &other) const
282  {
283  if (this->X != other.X)
284  {
285  return (this->X - other.X);
286  }
287  else
288  {
289  if (this->Y != other.Y)
290  {
291  return (this->Y - other.Y);
292  }
293  else
294  {
295  return 0;
296  }
297  }
298  }
299 
301  float changed(const XY_Colour &other) const
302  {
303  if (this->X != other.X)
304  {
305  return (100 * (this->X - other.X) / static_cast<float>(this->X));
306  }
307  else
308  {
309  if (this->Y != other.Y)
310  {
311  return (100 * (this->Y - other.Y) / static_cast<float>(this->Y));
312  }
313  else
314  {
315  return 0.0f;
316  }
317  }
318  }
319 
321  bool operator==(const XY_Colour &other) const
322  {
323  return (X == other.X && Y == other.Y);
324  }
325 
327  bool operator!=(const XY_Colour &other) const
328  {
329  return !operator==(other);
330  }
331  };
332 
333  // =============================================================================
334  // Attribute Helper classes
335  // =============================================================================
336 
340  struct Supported: public HF::Attributes::Attribute<uint8_t>
341  {
342  static constexpr uint8_t ID = SUPPORTED_ATTR;
343  static constexpr bool WRITABLE = false;
344 
345  static constexpr uint8_t get_suport(void)
346  {
347  return
348 #ifdef HF_ITF_COLOUR_CONTROL_HUE_AND_SATURATION_ATTR
349  Mask::HS_MODE +
350 #endif
351 #ifdef HF_ITF_COLOUR_CONTROL_XY_ATTR
352  Mask::XY_MODE +
353 #endif
354 #ifdef HF_ITF_COLOUR_CONTROL_HUE_AND_SATURATION_ATTR
356 #endif
357  ;
358  }
359 
360  Supported(uint8_t value = get_suport(), HF::Interface *owner = nullptr):
361  Attribute<uint8_t>(HF::Interface::COLOUR_CONTROL, ID, owner, value, WRITABLE)
362  {}
363  };
364 
368  struct Mode: public HF::Attributes::Attribute<uint8_t>
369  {
370  static constexpr uint8_t ID = MODE_ATTR;
371  static constexpr bool WRITABLE = false;
372 
373  Mode(uint8_t value = 0, HF::Interface *owner = nullptr):
374  Attribute<uint8_t>(HF::Interface::COLOUR_CONTROL, ID, owner, value, WRITABLE)
375  {}
376  };
377 
381  struct HueAndSaturation: public HF::Attributes::Attribute<HS_Colour>
382  {
383  static constexpr uint8_t ID = HUE_AND_SATURATION_ATTR;
384  static constexpr bool WRITABLE = false;
385 
386  HueAndSaturation(HS_Colour value = HS_Colour(0, 0), HF::Interface *owner = nullptr):
387  Attribute<HS_Colour>(HF::Interface::COLOUR_CONTROL, ID, owner, value, WRITABLE)
388  {}
389  };
390 
394  struct Xy: public HF::Attributes::Attribute<XY_Colour>
395  {
396  static constexpr uint8_t ID = XY_ATTR;
397  static constexpr bool WRITABLE = false;
398 
399  Xy(XY_Colour value = XY_Colour(0, 0), HF::Interface *owner = nullptr):
400  Attribute<XY_Colour>(HF::Interface::COLOUR_CONTROL, ID, owner, value, WRITABLE)
401  {}
402  };
403 
408  {
409  static constexpr uint8_t ID = COLOUR_TEMPERATURE_ATTR;
410  static constexpr bool WRITABLE = false;
411 
412  ColourTemperature(uint16_t value = 0, HF::Interface *owner = nullptr):
413  Attribute<uint16_t>(HF::Interface::COLOUR_CONTROL, ID, owner, value, WRITABLE)
414  {}
415  };
416 
428 
429  // =============================================================================
430  // Commands Messages classes
431  // =============================================================================
432 
439  {
440  uint16_t hue;
442  uint16_t time;
443 
451  MoveToHueMessage(uint16_t hue = 0, Direction direction = Direction::SHORTEST,
452  uint16_t time = 0): direction(direction), time(time)
453  {
454  this->hue = hue < HS_Colour::HUE ? hue : HS_Colour::HUE - 1;
455  }
456 
458  static constexpr uint16_t min_size = sizeof(hue) // Hue
459  + sizeof(uint8_t) // Direction
460  + sizeof(time); // time
461 
463  uint16_t size() const
464  {
465  return min_size;
466  }
467 
469  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
470 
472  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
473  };
474 
481  {
483  uint16_t rate;
484 
491  MoveHueMessage(Direction dir = Direction::UP, uint16_t rate = 0):
492  direction(dir), rate(rate)
493  {
494  this->rate = (rate < HS_Colour::HUE ? rate : HS_Colour::HUE - 1);
495  }
496 
498  static constexpr uint16_t min_size = sizeof(uint8_t) // Direction
499  + sizeof(rate); // Rate
500 
502  uint16_t size() const
503  {
504  return min_size;
505  }
506 
508  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
509 
511  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
512  };
513 
520  {
521  uint8_t step_size;
523  uint8_t time;
533  StepHueMessage(uint8_t step_size = 0, Direction dir = Direction::UP, uint8_t time = 0):
535  {}
536 
538  static constexpr uint16_t min_size = sizeof(step_size) // Step size
539  + sizeof(uint8_t) // Direction
540  + sizeof(time); // time
541 
543  uint16_t size() const
544  {
545  return min_size;
546  }
547 
549  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
550 
552  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
553  };
554 
561  {
562  uint8_t saturation;
564  uint16_t time;
565 
574  Direction dir = Direction::UP,
575  uint16_t time = 0):
577  {}
578 
580  static constexpr uint16_t min_size = sizeof(saturation) // Step size
581  + sizeof(uint8_t) // Direction
582  + sizeof(time); // time
583 
585  uint16_t size() const
586  {
587  return min_size;
588  }
589 
591  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
592 
594  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
595  };
596 
603  {
605  uint8_t rate;
606 
613  MoveSaturationMessage(Direction dir = Direction::UP, uint8_t rate = 0):
614  direction(dir), rate(rate)
615  {}
616 
618  static constexpr uint16_t min_size = sizeof(uint8_t) // Direction
619  + sizeof(rate); // Rate
620 
622  uint16_t size() const
623  {
624  return min_size;
625  }
626 
628  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
629 
631  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
632  };
633 
640  {
641  uint8_t step_size;
643  uint8_t time;
644 
653  Direction dir = Direction::UP,
654  uint8_t time = 0):
656  {}
657 
659  static constexpr uint16_t min_size = sizeof(step_size) // Step size
660  + sizeof(uint8_t) // Direction
661  + sizeof(time); // time
662 
664  uint16_t size() const
665  {
666  return min_size;
667  }
668 
670  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
671 
673  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
674  };
675 
682  {
685  uint16_t time;
686 
695  Direction dir = Direction::UP,
696  uint16_t time = 0):
697  colour(colour), direction(dir), time(time)
698  {}
699 
701  static constexpr uint16_t min_size = HS_Colour::min_size // HS colour
702  + sizeof(uint8_t) // Direction
703  + sizeof(time); // time
704 
706  uint16_t size() const
707  {
708  return min_size;
709  }
710 
712  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
713 
715  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
716  };
717 
724  {
725  XY_Colour colour;
726  uint16_t time;
727 
734  MoveToXYMessage(XY_Colour colour = XY_Colour(0, 0), uint16_t time = 0):
735  colour(colour), time(time)
736  {}
737 
739  static constexpr uint16_t min_size = sizeof(XY_Colour::X) // X
740  + sizeof(XY_Colour::Y) // Y
741  + sizeof(time); // time
742 
744  uint16_t size() const
745  {
746  return min_size;
747  }
748 
750  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
751 
753  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
754  };
755 
762  {
763  int16_t X_rate;
764  int16_t Y_rate;
765 
772  MoveXYMessage(int16_t X_rate = 0, int16_t Y_rate = 0):
774  {}
775 
777  static constexpr uint16_t min_size = sizeof(X_rate) // X_rate
778  + sizeof(Y_rate); // Y_rate
779 
781  uint16_t size() const
782  {
783  return min_size;
784  }
785 
787  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
788 
790  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
791  };
792 
799  {
800  int16_t X_step;
801  int16_t Y_step;
802  uint8_t time;
803 
811  StepXYMessage(int16_t X_step = 0, int16_t Y_step = 0, uint8_t time = 0):
813  {}
814 
816  static constexpr uint16_t min_size = sizeof(X_step) // X_step
817  + sizeof(Y_step) // Y_step
818  + sizeof(time); // Time
819 
821  uint16_t size() const
822  {
823  return min_size;
824  }
825 
827  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
828 
830  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
831  };
832 
839  {
840  uint16_t colour;
841  uint16_t time;
842 
849  MoveToTemperatureMessage(uint16_t colour = 0, uint16_t time = 0):
850  colour(colour), time(time)
851  {}
852 
854  static constexpr uint16_t min_size = sizeof(colour) // temperature
855  + sizeof(time); // time
856 
858  uint16_t size() const
859  {
860  return min_size;
861  }
862 
864  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
865 
867  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
868  };
869 
875  struct Base: public Interface<HF::Interface::COLOUR_CONTROL>
876  {
877  protected:
878 
881  };
882 
889  struct ITransition
890  {
892 
893  uint16_t period;
894  uint16_t remaining_time;
895 
902  ITransition(IServer &_server, uint16_t period):
904  {}
905 
906  virtual ~ITransition()
907  {}
908 
919  virtual bool run(uint16_t time)
920  {
921  remaining_time -= time;
922 
923  if (remaining_time == 0)
924  {
925  remaining_time = this->period;
926  return true;
927  }
928 
929  return false;
930  }
931 
938  virtual bool next() = 0;
939  };
940 
941 
944  {
945  int32_t step;
946  uint16_t n_steps;
947  uint16_t end;
948 
958  Hue_Transition(IServer &_server, uint16_t period, int32_t step = 0,
959  uint16_t n_steps = 0, uint16_t end = 0):
960  ITransition(_server, period), step(step), n_steps(n_steps), end(end)
961  {}
962 
964  Hue_Transition() = default;
965 
968  {}
969 
970  bool run(uint16_t time);
971 
972  bool next()
973  {
974  return (period != 0 ? true : false);
975  }
976  };
977 
978  struct Hue_Transition_Continuous: public ITransition
979  {
980  int32_t step;
981 
989  Hue_Transition_Continuous(IServer &_server, uint16_t period, int32_t step = 0):
990  ITransition(_server, period), step(step)
991  {}
992 
994  Hue_Transition_Continuous() = default;
995 
997  ~Hue_Transition_Continuous()
998  {}
999 
1000  // @copydoc ColourControl::Hue_Transition::run()
1001  bool run(uint16_t time);
1002 
1003  // @copydoc ITransition::next()
1004  bool next()
1005  {
1006  return (period != 0 ? true : false);
1007  }
1008  };
1009 
1012  {
1013  int32_t step;
1014  uint16_t n_steps;
1015  uint16_t end;
1016 
1026  Saturation_Transition(IServer &_server, uint16_t period, int32_t step = 0,
1027  uint16_t n_steps = 0,
1028  uint16_t end = 0):
1029  ITransition(_server, period), step(step), n_steps(n_steps), end(end)
1030  {}
1031 
1033  Saturation_Transition() = default;
1034 
1037  {}
1038 
1039  bool run(uint16_t time);
1040 
1041  bool next()
1042  {
1043  return (period != 0 ? true : false);
1044  }
1045  };
1046 
1047  struct Saturation_Transition_Continuous: public ITransition
1048  {
1049  int32_t step;
1050 
1058  Saturation_Transition_Continuous(IServer &_server, uint16_t period, int32_t step = 0):
1059  ITransition(_server, period), step(step)
1060  {}
1061 
1063  Saturation_Transition_Continuous() = default;
1064 
1066  ~Saturation_Transition_Continuous()
1067  {}
1068 
1069  bool run(uint16_t time);
1070 
1071  bool next()
1072  {
1073  return (period != 0 ? true : false);
1074  }
1075  };
1076 
1079  {
1080  int32_t hue_step;
1081  int32_t sat_step;
1082  uint16_t n_steps;
1084 
1095  HS_Transition(IServer &_server, uint16_t period,
1096  int32_t hue_step = 0, int32_t sat_step = 0,
1097  uint16_t n_steps = 0, HS_Colour end = HS_Colour(0, 0)):
1098  ITransition(_server, period), hue_step(hue_step),
1100  {}
1101 
1103  HS_Transition() = default;
1104 
1107  {}
1108 
1109  bool run(uint16_t time);
1110 
1111  bool next()
1112  {
1113  return (period != 0 ? true : false);
1114  }
1115  };
1116 
1119  {
1120  int32_t X_step;
1121  int32_t Y_step;
1122  uint16_t n_steps;
1124 
1135  XY_Transition(IServer &_server, uint16_t period,
1136  int32_t X_step = 0, int32_t Y_step = 0,
1137  uint16_t n_steps = 0,
1138  XY_Colour end = XY_Colour(0, 0)):
1139  ITransition(_server, period), X_step(X_step), Y_step(Y_step),
1140  n_steps(n_steps), end(end)
1141  {}
1142 
1144  XY_Transition() = default;
1145 
1148  {}
1149 
1150  bool run(uint16_t time);
1151 
1152  bool next()
1153  {
1154  return (period != 0 ? true : false);
1155  }
1156  };
1157 
1160  {
1161  int16_t X_step;
1162  int16_t Y_step;
1163 
1173  int16_t X_step = 0, int16_t Y_step = 0):
1175  {}
1176 
1178  XY_Transition_Continuous() = default;
1179 
1182  {}
1183 
1184  bool run(uint16_t time);
1185 
1186  bool next()
1187  {
1188  return (period != 0 ? true : false);
1189  }
1190  };
1191 
1194  {
1195  int32_t step;
1196  uint16_t n_steps;
1197  uint16_t end;
1198 
1209  int32_t step = 0,
1210  uint16_t n_steps = 0,
1211  uint16_t end = 0):
1212  ITransition(_server, period), step(step),
1213  n_steps(n_steps), end(end)
1214  {}
1215 
1217  Temperature_Transition() = default;
1218 
1221  {}
1222 
1223  bool run(uint16_t time);
1224 
1225  bool next()
1226  {
1227  return (period != 0 ? true : false);
1228  }
1229  };
1230 
1236  class IServer: public InterfaceRole<ColourControl::Base, HF::Interface::SERVER_ROLE>
1237  {
1238  protected:
1239 
1240  uint8_t _supported;
1241  uint8_t _mode;
1242 
1243 #ifdef HF_ITF_COLOUR_CONTROL_HUE_AND_SATURATION_ATTR
1244 
1245  HS_Colour _hue_and_saturation;
1246 
1247 #endif
1248 
1249 #ifdef HF_ITF_COLOUR_CONTROL_XY_ATTR
1250 
1251  XY_Colour _xy;
1252 
1253 #endif
1254 
1255 #ifdef HF_ITF_COLOUR_CONTROL_COLOUR_TEMPERATURE_ATTR
1256 
1257  uint16_t _colour_temperature;
1258 
1259 #endif
1260 
1261  public:
1262 
1264  IServer(): InterfaceRole<ColourControl::Base, HF::Interface::SERVER_ROLE>() {}
1265 
1267  virtual ~IServer() {}
1268 
1269  // ======================================================================
1270  // Events
1271  // ======================================================================
1274 
1285  virtual Common::Result move_to_hue(const Protocol::Address &addr,
1286  const MoveToHueMessage &message);
1287 
1298  virtual Common::Result move_hue(const Protocol::Address &addr,
1299  const MoveHueMessage &message);
1300 
1311  virtual Common::Result step_hue(const Protocol::Address &addr,
1312  const StepHueMessage &message);
1313 
1325  const MoveToSaturationMessage &message);
1326 
1337  virtual Common::Result move_saturation(const Protocol::Address &addr,
1338  const MoveSaturationMessage &message);
1339 
1350  virtual Common::Result step_saturation(const Protocol::Address &addr,
1351  const StepSaturationMessage &message);
1352 
1364  const MoveToHueSaturationMessage &message);
1365 
1376  virtual Common::Result move_to_xy(const Protocol::Address &addr,
1377  const MoveToXYMessage &message);
1378 
1389  virtual Common::Result move_xy(const Protocol::Address &addr,
1390  const MoveXYMessage &message);
1391 
1402  virtual Common::Result step_xy(const Protocol::Address &addr,
1403  const StepXYMessage &message);
1404 
1416  const MoveToTemperatureMessage &message);
1417 
1418 #ifdef HF_ITF_COLOUR_CONTROL_STOP_CMD
1419 
1429  virtual Common::Result stop(const Protocol::Address &addr);
1430 
1431 #endif
1432 
1434  // ======================================================================
1435 
1436  // =============================================================================
1437  // Get/Set API.
1438  // =============================================================================
1439 
1445  uint8_t supported() const;
1446 
1452  void supported(uint8_t __value);
1453 
1459  uint8_t mode() const;
1460 
1466  void mode(uint8_t __value);
1467 
1468 #ifdef HF_ITF_COLOUR_CONTROL_HUE_AND_SATURATION_ATTR
1469 
1475  HS_Colour hue_and_saturation() const;
1476 
1482  void hue_and_saturation(HS_Colour __value);
1483 
1484 #endif
1485 
1486 #ifdef HF_ITF_COLOUR_CONTROL_XY_ATTR
1487 
1493  XY_Colour xy() const;
1494 
1500  void xy(XY_Colour __value);
1501 
1502 #endif
1503 
1504 #ifdef HF_ITF_COLOUR_CONTROL_COLOUR_TEMPERATURE_ATTR
1505 
1511  uint16_t colour_temperature() const;
1512 
1518  void colour_temperature(uint16_t __value);
1519 
1520 #endif
1521 
1522  // =============================================================================
1523  // Attribute API.
1524  // =============================================================================
1525 
1527 
1528  HF::Attributes::UIDS attributes(uint8_t pack_id =
1530 
1531  protected:
1532 
1534  uint16_t offset);
1535  };
1536 
1542  struct Server: public IServer
1543  {
1545  typedef typename std::vector<ITransition *> Container;
1546 
1547  public:
1548 
1551  {}
1552 
1554  virtual ~Server()
1555  {}
1556 
1562  static uint16_t has_transitions()
1563  {
1564  return transitions.empty() ? 0 : transitions.front()->remaining_time;
1565  }
1566 
1578  static uint16_t transition(void);
1579 
1580  // ======================================================================
1581  // Events
1582  // ======================================================================
1585 
1587  virtual void changed(void) {}
1588 
1590  const MoveToHueMessage &message);
1591 
1593  const MoveHueMessage &message);
1594 
1596  const StepHueMessage &message);
1597 
1599  const MoveToSaturationMessage &message);
1600 
1602  const MoveSaturationMessage &message);
1603 
1605  const StepSaturationMessage &message);
1606 
1608  const MoveToHueSaturationMessage &message);
1609 
1611  const MoveToXYMessage &message);
1612 
1614  const MoveXYMessage &message);
1615 
1617  const StepXYMessage &message);
1618 
1620  const MoveToTemperatureMessage &message);
1621 
1622 #ifdef HF_ITF_COLOUR_CONTROL_STOP_CMD
1623  Common::Result stop(const Protocol::Address &addr);
1624 #endif
1625 
1627  protected:
1628 
1629  static Container transitions;
1630 
1637  {
1638  transitions.push_back(t);
1639  changed();
1640  }
1641 
1647  void remove(IServer const &itf)
1648  {
1649  auto compare = [&itf](ITransition *entry)
1650  {
1651  if (&(entry->server) == &(itf))
1652  {
1653  delete entry;
1654  return true;
1655  }
1656 
1657  return false;
1658  };
1659 
1660  transitions.erase(std::remove_if(transitions.begin(), transitions.end(),
1661  compare), transitions.end());
1662  }
1663  };
1664 
1670  struct Client: public InterfaceRole<ColourControl::Base, HF::Interface::CLIENT_ROLE>
1671  {
1673 
1674  virtual ~Client() {}
1675 
1676  // ======================================================================
1677  // Commands
1678  // ======================================================================
1681 
1691  void move_to_hue(const Protocol::Address &addr, uint16_t hue,
1692  Direction direction, uint16_t transition);
1693 
1702  void move_hue(const Protocol::Address &addr, Direction direction, uint16_t rate);
1703 
1713  void step_hue(const Protocol::Address &addr, uint8_t step,
1714  Direction direction, uint8_t time);
1715 
1725  void move_to_saturation(const Protocol::Address &addr, uint8_t saturation,
1726  Direction direction, uint16_t transition);
1727 
1736  void move_saturation(const Protocol::Address &addr, Direction direction, uint8_t rate);
1737 
1747  void step_saturation(const Protocol::Address &addr, uint8_t step,
1748  Direction direction, uint8_t time);
1749 
1759  void move_to_hue_and_saturation(const Protocol::Address &addr, HS_Colour colour,
1760  Direction direction, uint16_t time);
1761 
1770  void move_to_xy(const Protocol::Address &addr, XY_Colour colour, uint16_t time);
1771 
1780  void move_xy(const Protocol::Address &addr, int16_t X_rate, int16_t Y_rate);
1781 
1791  void step_xy(const Protocol::Address &addr, int16_t X_step,
1792  int16_t Y_step, uint8_t time);
1793 
1803  uint16_t colour, uint16_t time);
1804 
1811  void stop(const Protocol::Address &addr);
1812 
1814  // ======================================================================
1815  };
1816 
1819  } // namespace ColourControl
1820 
1821  } // namespace Interfaces
1822 
1823 } // namespace HF
1824 
1830 // =============================================================================
1831 // Stream Helpers
1832 // =============================================================================
1833 
1842 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::ColourControl::CMD command);
1843 
1852 std::ostream &operator<<(std::ostream &stream,
1856 #endif /* HF_ITF_COLOUR_CONTROL_H */
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
int16_t X_rate
The rate of change of X in units per seconds.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t period
Time period for the transition (in 100 msec units).
virtual Common::Result move_xy(const Protocol::Address &addr, const MoveXYMessage &message)
Callback that is called when a ColourControl::MOVE_XY_CMD, is received.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
void move_saturation(const Protocol::Address &addr, Direction direction, uint8_t rate)
Send a HAN-FUN message containing a ColourControl::MOVE_SATURATION_CMD, to the given network address...
Common::Result move_hue(const Protocol::Address &addr, const MoveHueMessage &message)
Callback that is called when a ColourControl::MOVE_HUE_CMD, is received.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Common::Result step_xy(const Protocol::Address &addr, const StepXYMessage &message)
Callback that is called when a ColourControl::STEP_XY_CMD, is received.
MoveToXYMessage(XY_Colour colour=XY_Colour(0, 0), uint16_t time=0)
Constructor.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Direction direction
Direction of movement.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t end
End value to stop the iteration.
MoveToSaturationMessage(uint8_t saturation=0, Direction dir=Direction::UP, uint16_t time=0)
Constructor.
static constexpr uint8_t ID
Attribute UID.
Common::Result move_to_hue_and_saturation(const Protocol::Address &addr, const MoveToHueSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_TO_HUE_AND_SATURATION_CMD, is received.
static constexpr bool WRITABLE
Attribute Read/Write.
uint16_t time
Time of a single step transition in units of 100msec.
Colour Control Interface : Server side implementation.
ITransition(IServer &_server, uint16_t period)
Constructor.
XY_Transition()=default
Default constructor.
uint16_t size() const
Number bytes needed to serialize the message.
virtual Common::Result move_saturation(const Protocol::Address &addr, const MoveSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_SATURATION_CMD, is received.
Temperature_Transition()=default
Default constructor.
virtual bool run(uint16_t time)
Run the transition.
virtual Common::Result move_hue(const Protocol::Address &addr, const MoveHueMessage &message)
Callback that is called when a ColourControl::MOVE_HUE_CMD, is received.
Colour Control Interface : Server side implementation.
MoveXYMessage(int16_t X_rate=0, int16_t Y_rate=0)
Constructor.
uint16_t size() const
Number bytes needed to serialize the message.
Direction direction
Direction of movement.
static constexpr bool WRITABLE
Attribute Read/Write.
XY_Transition(IServer &_server, uint16_t period, int32_t X_step=0, int32_t Y_step=0, uint16_t n_steps=0, XY_Colour end=XY_Colour(0, 0))
Constructor.
Saturation_Transition(IServer &_server, uint16_t period, int32_t step=0, uint16_t n_steps=0, uint16_t end=0)
Constructor.
static uint16_t has_transitions()
Check if there are transitions to run.
uint8_t supported() const
Get the supported modes bitmask for the Colour Control server.
XY_Transition_Continuous()=default
Default constructor.
void stop(const Protocol::Address &addr)
Send a HAN-FUN message containing a ColourControl::STOP_CMD, to the given network address...
uint16_t size() const
Number bytes needed to serialize the message.
List of attributes UIDs.
Definition: attributes.h:176
void move_to_hue(const Protocol::Address &addr, uint16_t hue, Direction direction, uint16_t transition)
Send a HAN-FUN message containing a ColourControl::MOVE_TO_HUE_CMD, to the given network address...
XY_Colour(uint16_t X, uint16_t Y)
Constructor.
Helper class to handle the Colour Temperature attribute for the Colour Control interface.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
int compare(const XY_Colour &other) const
Compare this attribute with the given attribute in other.
uint16_t n_steps
Counter for the steps needed.
Colour Control interface UID.
Definition: interface.h:81
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Return all mandatory attributes for the interface.
Definition: attributes.h:842
int32_t step
Hue or Saturation step.
Direction direction
Direction of movement.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t size() const
Number bytes needed to serialize the message.
bool run(uint16_t time)
Run the transition.
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
Mask
Mask elements for Colour Modes.
HS_Transition(IServer &_server, uint16_t period, int32_t hue_step=0, int32_t sat_step=0, uint16_t n_steps=0, HS_Colour end=HS_Colour(0, 0))
Constructor.
uint16_t end
End value to stop the iteration.
Helper class to add optional interfaces to other classes.
Definition: interface.h:445
Common::Result step_saturation(const Protocol::Address &addr, const StepSaturationMessage &message)
Callback that is called when a ColourControl::STEP_SATURATION_CMD, is received.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
bool operator!=(const XY_Colour &other) const
Operator not equal.
virtual Common::Result move_to_saturation(const Protocol::Address &addr, const MoveToSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_TO_SATURATION_CMD, is received.
virtual Common::Result step_xy(const Protocol::Address &addr, const StepXYMessage &message)
Callback that is called when a ColourControl::STEP_XY_CMD, is received.
uint16_t size() const
Number bytes needed to serialize the message.
This file contains the definitions for the HAN-FUN protocol messages.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Interface API for the Transitions.
Temperature_Transition(IServer &_server, uint16_t period, int32_t step=0, uint16_t n_steps=0, uint16_t end=0)
Constructor.
uint16_t remaining_time
Remaining time until the transition is ran.
uint16_t time
Time of transition in units of 100msec.
int compare(const HS_Colour &other) const
Compare this attribute with the given attribute in other.
uint16_t n_steps
Counter for the steps needed.
uint16_t size() const
Number bytes needed to serialize the message.
float changed(const XY_Colour &other) const
This method is used to get the percentage of change that the attribute has in relation to the value p...
Common::Result move_to_saturation(const Protocol::Address &addr, const MoveToSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_TO_SATURATION_CMD, is received.
std::vector< ITransition * > Container
Container for the transitions.
uint8_t time
Time of a single step transition (units of 100msec).
bool run(uint16_t time)
Run the transition.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
bool operator==(const HS_Colour &other) const
Operator equal.
MoveToHueSaturationMessage(HS_Colour colour=HS_Colour(0, 0), Direction dir=Direction::UP, uint16_t time=0)
Constructor.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
void move_to_saturation(const Protocol::Address &addr, uint8_t saturation, Direction direction, uint16_t transition)
Send a HAN-FUN message containing a ColourControl::MOVE_TO_SATURATION_CMD, to the given network addre...
float changed(const HS_Colour &other) const
This method is used to get the percentage of change that the attribute has in relation to the value p...
bool run(uint16_t time)
Run the transition.
static constexpr bool WRITABLE
Attribute Read/Write.
bool next()
Check if the transition should continue.
HS_Colour colour
New Hue and Saturation Colour.
Helper class to handle the XY attribute for the Colour Control interface.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
This file contains the definitions common to all interfaces.
virtual Common::Result step_hue(const Protocol::Address &addr, const StepHueMessage &message)
Callback that is called when a ColourControl::STEP_HUE_CMD, is received.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Helper class to handle the Mode attribute for the Colour Control interface.
HF::Attributes::UIDS attributes(uint8_t pack_id=HF::Attributes::Pack::MANDATORY) const
Return a vector containing the attribute UIDs, for the given pack ID.
uint8_t _mode
Current Active Mode.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t size() const
Number bytes needed to serialize the message.
Helper class that supports the XY colour mode.
static constexpr uint8_t ID
Attribute UID.
Saturation_Transition()=default
Default constructor.
void move_hue(const Protocol::Address &addr, Direction direction, uint16_t rate)
Send a HAN-FUN message containing a ColourControl::MOVE_HUE_CMD, to the given network address...
uint8_t rate
Rate of change in degrees per second.
bool operator!=(const HS_Colour &other) const
Operator not equal.
HF::Interface const * owner() const
Definition: attributes.h:414
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
uint16_t rate
Rate of change in degrees per second.
Common::Result move_xy(const Protocol::Address &addr, const MoveXYMessage &message)
Callback that is called when a ColourControl::MOVE_XY_CMD, is received.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Helper class to handle the Supported attribute for the Colour Control interface.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t time
time of a single step transition (units of 100msec).
Hue_Transition(IServer &_server, uint16_t period, int32_t step=0, uint16_t n_steps=0, uint16_t end=0)
Constructor.
XY_Colour end
End value to stop the iteration.
bool next()
Check if the transition should continue.
static constexpr uint8_t ID
Attribute UID.
This class represents a byte array.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HS_Colour(uint16_t _hue, uint8_t _saturation)
Constructor.
Direction
Direction of movement.
virtual bool next()=0
Check if the transition should continue.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
uint8_t mode() const
Get the current active mode for the Colour Control server.
bool operator==(const XY_Colour &other) const
Operator equal.
Common::Result move_to_colour_temperature(const Protocol::Address &addr, const MoveToTemperatureMessage &message)
Callback that is called when a ColourControl::MOVE_TO_COLOUR_TEMPERATURE_CMD, is received.
MoveToTemperatureMessage(uint16_t colour=0, uint16_t time=0)
Constructor.
Network Address.
Definition: protocol.h:201
uint16_t uid() const
This method returns the interface UID.
Definition: interface.h:374
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t time
Time of a single step transition (units of 100msec).
Move to Hue and Saturation Message class.
MoveHueMessage(Direction dir=Direction::UP, uint16_t rate=0)
Constructor.
void move_xy(const Protocol::Address &addr, int16_t X_rate, int16_t Y_rate)
Send a HAN-FUN message containing a ColourControl::MOVE_XY_CMD, to the given network address...
static int32_t get_travel_distance(const Direction dir, uint16_t initial, uint16_t final, uint16_t max)
Helper method to get the angle between two hue/saturation values.
uint16_t hue
The value of the new Hue.
virtual Common::Result step_saturation(const Protocol::Address &addr, const StepSaturationMessage &message)
Callback that is called when a ColourControl::STEP_SATURATION_CMD, is received.
bool next()
Check if the transition should continue.
Common::Result move_saturation(const Protocol::Address &addr, const MoveSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_SATURATION_CMD, is received.
static constexpr uint8_t ID
Attribute UID.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
static uint16_t transition(void)
Method responsible for updating the all the transitions.
void step_saturation(const Protocol::Address &addr, uint8_t step, Direction direction, uint8_t time)
Send a HAN-FUN message containing a ColourControl::STEP_SATURATION_CMD, to the given network address...
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Helper class that supports the Hue and Saturation colour mode.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
HS_Transition()=default
Default constructor.
Common::Result step_hue(const Protocol::Address &addr, const StepHueMessage &message)
Callback that is called when a ColourControl::STEP_HUE_CMD, is received.
void move_to_hue_and_saturation(const Protocol::Address &addr, HS_Colour colour, Direction direction, uint16_t time)
Send a HAN-FUN message containing a ColourControl::MOVE_TO_HUE_AND_SATURATION_CMD, to the given network address.
Colour Control Interface : Parent.
static int32_t invert_angle(int32_t angle, uint16_t max_value)
Helper method to invert a traveling angle.
HF::Attributes::IAttribute * create_attribute(HF::Interfaces::Alert::Server *server, uint8_t uid)
Create an attribute object that can hold the attribute with the given uid.
Interface/Service Attribute API.
Definition: attributes.h:44
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t n_steps
Counter for the steps needed.
bool run(uint16_t time)
Run the transition.
IServer & server
The server instance.
StepHueMessage(uint8_t step_size=0, Direction dir=Direction::UP, uint8_t time=0)
Constructor.
void move_to_colour_temperature(const Protocol::Address &addr, uint16_t colour, uint16_t time)
Send a HAN-FUN message containing a ColourControl::MOVE_TO_COLOUR_TEMPERATURE_CMD, to the given network address.
void add_transition(ITransition *t)
Add a transition to the list.
uint16_t n_steps
Counter for the steps needed.
uint8_t saturation
The value of new saturation.
uint16_t time
Time of a single step transition in units of 100msec.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
XY_Transition_Continuous(IServer &_server, uint16_t period, int16_t X_step=0, int16_t Y_step=0)
Constructor.
bool run(uint16_t time)
Run the transition.
StepXYMessage(int16_t X_step=0, int16_t Y_step=0, uint8_t time=0)
Constructor.
virtual Common::Result move_to_colour_temperature(const Protocol::Address &addr, const MoveToTemperatureMessage &message)
Callback that is called when a ColourControl::MOVE_TO_COLOUR_TEMPERATURE_CMD, is received.
int16_t Y_rate
The rate of change of Y in units per seconds.
void step_xy(const Protocol::Address &addr, int16_t X_step, int16_t Y_step, uint8_t time)
Send a HAN-FUN message containing a ColourControl::STEP_XY_CMD, to the given network address...
uint8_t time
Time of a single step transition in units of 100msec.
uint16_t size() const
Number bytes needed to serialize the message.
virtual Common::Result move_to_hue_and_saturation(const Protocol::Address &addr, const MoveToHueSaturationMessage &message)
Callback that is called when a ColourControl::MOVE_TO_HUE_AND_SATURATION_CMD, is received.
bool next()
Check if the transition should continue.
Helper class template for implementing a given interface role.
Definition: interface.h:394
Colour Control Interface : Client side implementation.
bool run(uint16_t time)
Run the transition.
static constexpr bool WRITABLE
Attribute Read/Write.
virtual Common::Result move_to_xy(const Protocol::Address &addr, const MoveToXYMessage &message)
Callback that is called when a ColourControl::MOVE_TO_XY_CMD, is received.
Common::Result move_to_hue(const Protocol::Address &addr, const MoveToHueMessage &message)
Callback that is called when a ColourControl::MOVE_TO_HUE_CMD, is received.
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
StepSaturationMessage(uint8_t step_size=0, Direction dir=Direction::UP, uint8_t time=0)
Constructor.
Attribute(const uint16_t interface, const uint8_t uid, const HF::Interface *__owner, uint8_t data, bool writable=false)
Attribute template constructor.
Definition: attributes.h:360
Helper class to handle the Hue And Saturation attribute for the Colour Control interface.
uint8_t time
Time of a single step transition in units of 100msec.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
HS_Colour end
End value to stop the iteration.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::ColourControl::CMD command)
Convert the given command into a string and write it to the given stream.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::ColourCon...
MoveSaturationMessage(Direction dir=Direction::UP, uint8_t rate=0)
Constructor.
uint8_t step_size
Step size in degrees.
Common interface for all Interfaces.
Definition: interface.h:43
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
virtual Common::Result move_to_hue(const Protocol::Address &addr, const MoveToHueMessage &message)
Callback that is called when a ColourControl::MOVE_TO_HUE_CMD, is received.
MoveToHueMessage(uint16_t hue=0, Direction direction=Direction::SHORTEST, uint16_t time=0)
Constructor.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Move to Colour Temperature Message class.
bool next()
Check if the transition should continue.
bool next()
Check if the transition should continue.
void step_hue(const Protocol::Address &addr, uint8_t step, Direction direction, uint8_t time)
Send a HAN-FUN message containing a ColourControl::STEP_HUE_CMD, to the given network address...
static constexpr bool WRITABLE
Attribute Read/Write.
virtual void changed(void)
Inform the APP that a new transition was added.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Common::Result move_to_xy(const Protocol::Address &addr, const MoveToXYMessage &message)
Callback that is called when a ColourControl::MOVE_TO_XY_CMD, is received.
Result
Commands result codes.
uint16_t n_steps
Counter for the steps needed.
Hue_Transition()=default
Default constructor.
uint16_t end
End value to stop the iteration.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
static constexpr uint8_t ID
Attribute UID.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22
void move_to_xy(const Protocol::Address &addr, XY_Colour colour, uint16_t time)
Send a HAN-FUN message containing a ColourControl::MOVE_TO_XY_CMD, to the given network address...