HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
simple_visual_effects.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_ITF_SIMPLE_VISUAL_EFFECTS_H
18 #define HF_ITF_SIMPLE_VISUAL_EFFECTS_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 SimpleVisualEffects
29  {
30  class Server;
31  }
32 
48  uint8_t uid);
49 
53  namespace SimpleVisualEffects
54  {
62  typedef enum _CMD
64  {
65  ON_CMD = 1,
66  OFF_CMD = 2,
67  BLINK_CMD = 3,
68  FADE_CMD = 4,
70  __LAST_CMD__ = BREATHE_CMD
71  } CMD;
72 
73  // =============================================================================
74  // Helper classes
75  // =============================================================================
76 
80  struct OnEffect
81  {
82  uint16_t duration;
83 
89  OnEffect(uint16_t _duration = 0): duration(_duration) {}
90 
92  static constexpr uint16_t min_size = sizeof(uint16_t);
93 
95  uint16_t size() const
96  {
97  return min_size;
98  }
99 
101  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
102  {
103  HF_SERIALIZABLE_CHECK(array, offset, min_size);
104 
105  array.write(offset, duration);
106 
107  return min_size;
108  }
109 
111  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
112  {
113  HF_SERIALIZABLE_CHECK(array, offset, min_size);
114 
115  array.read(offset, duration);
116 
117  return min_size;
118  }
119  };
120 
124  struct BlinkEffect
125  {
126  uint16_t duty_cycle_on;
127  uint16_t duty_cycle_off;
128  uint16_t number_of_cycles;
129 
137  BlinkEffect(uint16_t _duty_cycle_on = 0, uint16_t _duty_cycle_off = 0,
138  uint16_t _number_of_cycles = 1): duty_cycle_on(_duty_cycle_on),
139  duty_cycle_off(_duty_cycle_off), number_of_cycles(_number_of_cycles) {}
140 
142  static constexpr uint16_t min_size = 3 * sizeof(uint16_t);
143 
145  static constexpr uint16_t min_number_of_cycles = 1;
146 
148  uint16_t size() const
149  {
150  return min_size;
151  }
152 
154  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
155  {
156  HF_SERIALIZABLE_CHECK(array, offset, min_size);
157 
159  {
160  return 0;
161  });
162 
163  offset += array.write(offset, duty_cycle_on);
164  offset += array.write(offset, duty_cycle_off);
165  offset += array.write(offset, number_of_cycles);
166 
167  return min_size;
168  }
169 
171  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
172  {
173  HF_SERIALIZABLE_CHECK(array, offset, min_size);
174 
175  offset += array.read(offset, duty_cycle_on);
176  offset += array.read(offset, duty_cycle_off);
177  offset += array.read(offset, number_of_cycles);
178 
180  {
181  return 0;
182  })
183 
184  return min_size;
185  }
186  };
187 
191  struct FadeEffect
192  {
193  uint8_t start;
194  uint8_t end;
195  uint16_t duration;
196 
197  FadeEffect(uint8_t _start = 0, uint8_t _end = 0, uint16_t _duration = 0):
198  start(_start), end(_end), duration(_duration) {}
199 
201  static constexpr uint16_t min_size = 2 * sizeof(uint8_t) + sizeof(uint16_t);
202 
204  uint16_t size() const
205  {
206  return min_size;
207  }
208 
210  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
211  {
212  HF_SERIALIZABLE_CHECK(array, offset, min_size);
213 
214  offset += array.write(offset, start);
215  offset += array.write(offset, end);
216  offset += array.write(offset, duration);
217 
218  return min_size;
219  }
220 
222  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
223  {
224  HF_SERIALIZABLE_CHECK(array, offset, min_size);
225 
226  offset += array.read(offset, start);
227  offset += array.read(offset, end);
228  offset += array.read(offset, duration);
229 
230  return min_size;
231  }
232  };
233 
238  {
239  uint8_t start;
240  uint16_t start_hold;
241  uint16_t ste_duration;
242 
243  uint8_t end;
244  uint16_t end_hold;
245  uint16_t ets_duration;
246 
247  uint16_t number_of_cycles;
248 
249  BreatheEffect(uint8_t _start = 0, uint16_t _start_hold = 0,
250  uint16_t _ste_duration = 0,
251  uint8_t _end = 0, uint16_t _end_hold = 0, uint16_t _ets_duration = 0,
252  uint16_t _number_of_cycles = 1):
253  start(_start), start_hold(_start_hold), ste_duration(_ste_duration),
254  end(_end), end_hold(_end_hold), ets_duration(_ets_duration),
255  number_of_cycles(_number_of_cycles) {}
256 
258  static constexpr uint16_t min_size = 2 * sizeof(uint8_t) + 5 * sizeof(uint16_t);
259 
261  static constexpr uint16_t min_number_of_cycles = 1;
262 
264  uint16_t size() const
265  {
266  return min_size;
267  }
268 
270  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
271  {
272  HF_SERIALIZABLE_CHECK(array, offset, min_size);
273 
275  {
276  return 0;
277  });
278 
279  offset += array.write(offset, start);
280  offset += array.write(offset, start_hold);
281  offset += array.write(offset, ste_duration);
282  offset += array.write(offset, end);
283  offset += array.write(offset, end_hold);
284  offset += array.write(offset, ets_duration);
285  offset += array.write(offset, number_of_cycles);
286 
287  return min_size;
288  }
289 
291  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
292  {
293  HF_SERIALIZABLE_CHECK(array, offset, min_size);
294 
295  offset += array.read(offset, start);
296  offset += array.read(offset, start_hold);
297  offset += array.read(offset, ste_duration);
298  offset += array.read(offset, end);
299  offset += array.read(offset, end_hold);
300  offset += array.read(offset, ets_duration);
301  offset += array.read(offset, number_of_cycles);
302 
304  {
305  return 0;
306  });
307 
308  return min_size;
309  }
310  };
311 
323 
329  struct Base: public Interface<HF::Interface::SIMPLE_VISUAL_EFFECTS>
330  {
331  protected:
332 
335  };
336 
342  class Server: public InterfaceRole<SimpleVisualEffects::Base, HF::Interface::SERVER_ROLE>
343  {
344  public:
345 
347  Server(): InterfaceRole<SimpleVisualEffects::Base, HF::Interface::SERVER_ROLE>() {}
348 
350  virtual ~Server() {}
351 
352  // ======================================================================
353  // Commands
354  // ======================================================================
357 
359  // ======================================================================
360 
361  // ======================================================================
362  // Events
363  // ======================================================================
366 
374  virtual void on(const Protocol::Address &addr, const OnEffect &effect);
375 
382  virtual void off(const Protocol::Address &addr);
383 
384 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_BLINK_CMD
385 
392  virtual void blink(const Protocol::Address &addr, const BlinkEffect &effect);
393 #endif
394 
395 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_FADE_CMD
396 
403  virtual void fade(const Protocol::Address &addr, const FadeEffect &effect);
404 #endif
405 
406 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_BREATHE_CMD
407 
414  virtual void breathe(const Protocol::Address &addr, const BreatheEffect &effect);
415 #endif
416 
418  // ======================================================================
419 
420  protected:
421 
423  uint16_t offset);
424  };
425 
431  struct Client: public InterfaceRole<SimpleVisualEffects::Base, HF::Interface::CLIENT_ROLE>
432  {
434 
435  virtual ~Client() {}
436 
437  // ======================================================================
438  // Commands
439  // ======================================================================
442 
443 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_ON_CMD
444 
451  void on(const Protocol::Address &addr, uint16_t duration);
452 
459  void on(uint16_t duration)
460  {
461  Protocol::Address addr;
462  on(addr, duration);
463  }
464 #endif
465 
466 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_OFF_CMD
467 
473  void off(const Protocol::Address &addr);
474 
479  void off()
480  {
481  Protocol::Address addr;
482  off(addr);
483  }
484 #endif
485 
486 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_BLINK_CMD
487 
494  void blink(const Protocol::Address &addr, const BlinkEffect &effect);
495 
502  void blink(const BlinkEffect &effect)
503  {
504  Protocol::Address addr;
505  blink(addr, effect);
506  }
507 #endif
508 
509 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_FADE_CMD
510 
517  void fade(const Protocol::Address &addr, const FadeEffect &effect);
518 
523  void fade(const FadeEffect &effect)
524  {
525  Protocol::Address addr;
526  fade(addr, effect);
527  }
528 #endif
529 
530 #ifdef HF_ITF_SIMPLE_VISUAL_EFFECTS_BREATHE_CMD
531 
538  void breathe(const Protocol::Address &addr, const BreatheEffect &effect);
539 
546  void breathe(const BreatheEffect &effect)
547  {
548  Protocol::Address addr;
549  breathe(addr, effect);
550  }
551 #endif
552 
554  // ======================================================================
555 
556  // ======================================================================
557  // Events
558  // ======================================================================
562 
564  // =============================================================================
565  };
566 
569  } // namespace SimpleVisualEffects
570 
571  } // namespace Interfaces
572 
573 } // namespace HF
574 
580 // =============================================================================
581 // Stream Helpers
582 // =============================================================================
583 
592 std::ostream &operator<<(std::ostream &stream,
594 
597 #endif /* HF_ITF_SIMPLE_VISUAL_EFFECTS_H */
OnEffect(uint16_t _duration=0)
Constructor.
This structure represents the parameters required for the ON_CMD effect.
uint16_t duty_cycle_off
Number of miliseconds to keep visual indicator OFF.
virtual void off(const Protocol::Address &addr)
Callback that is called when a SimpleVisualEffects::OFF_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.
Simple Visual Effects Interface : Parent.
This structure represents the parameters required for the BLICK_CMD effect.
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
uint16_t ste_duration
Number of miliseconds to go from start to end brightness.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
static constexpr uint16_t min_number_of_cycles
Minimum value for number_of_cycles parameter.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
uint16_t size() const
Number bytes needed to serialize the message.
#define HF_ASSERT(_expr, _block)
Helper macro to check for correct assumptions.
uint16_t size() const
Number bytes needed to serialize the message.
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
uint16_t duty_cycle_on
Number of miliseconds to keep visual indicator ON.
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.
BlinkEffect(uint16_t _duty_cycle_on=0, uint16_t _duty_cycle_off=0, uint16_t _number_of_cycles=1)
Constructor.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
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.
uint8_t start
Start brightness in percentage.
uint16_t duration
How long to keep the visual indicator ON in miliseconds.
Simple Visual Effects Interface : Server side implementation.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
This structure represents the parameters required for the FADE_CMD effect.
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
This file contains the definitions common to all interfaces.
uint16_t number_of_cycles
Number of times to repeat the ON-OFF cycle.
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.
virtual void on(const Protocol::Address &addr, const OnEffect &effect)
Callback that is called when a SimpleVisualEffects::ON_CMD, is received.
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::SimpleVisualEffects::CMD command)
Convert the given command into a string and write it to the given stream.
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.
static constexpr uint16_t min_number_of_cycles
Minimum value for number_of_cycles parameter.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::SimpleVis...
This class represents a byte array.
Simple Visual Effects Interface : Client side implementation.
Simple Visual Effects interface UID.
Definition: interface.h:88
uint8_t end
End brightness in percentage.
Network Address.
Definition: protocol.h:201
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.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
uint16_t end_hold
Number of miliseconds to hold end brightness.
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.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Interface/Service Attribute API.
Definition: attributes.h:44
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
This structure represents the parameters required for the BREATHE_CMD effect.
Helper class template for implementing a given interface role.
Definition: interface.h:394
uint16_t start_hold
Number of miliseconds to hold start brightness.
uint16_t duration
Time in miliseconds to go from start to end brightness.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
uint16_t ets_duration
Number of miliseconds to go from end to start brightness.
uint16_t number_of_cycles
Number of times to repeat the Start-End-Start cycle.
uint8_t start
Start brightness in percentage.
Result
Commands result codes.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22