HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
simple_button.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_ITF_SIMPLE_BUTTON_H
18 #define HF_ITF_SIMPLE_BUTTON_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/interface.h"
22 
23 // =============================================================================
24 // API
25 // =============================================================================
26 
27 namespace HF
28 {
29  namespace Interfaces
30  {
31  // Forward declaration.
32  namespace SimpleButton
33  {
34  class Server;
35  }
36 
52 
56  namespace SimpleButton
57  {
65  typedef enum _CMD
67  {
72  __LAST_CMD__ = DOUBLE_CLICK_PRESS_CMD
73  } CMD;
74 
76  typedef enum _Attributes
77  {
81  __LAST_ATTR__ = DOUBLE_CLICK_GAP_DURATION_ATTR
82  } Attributes;
83 
84  // =============================================================================
85  // Attribute Helper classes
86  // =============================================================================
87 
92  {
93  static constexpr uint8_t ID = SHORT_PRESS_MAX_DURATION_ATTR;
94  static constexpr bool WRITABLE = true;
95 
96  ShortPressMaxDuration(uint16_t value = 0, HF::Interface *owner = nullptr):
97  Attribute<uint16_t>(HF::Interface::SIMPLE_BUTTON, ID, owner, value, WRITABLE)
98  {}
99  };
100 
105  {
106  static constexpr uint8_t ID = EXTRA_LONG_PRESS_MIN_DURATION_ATTR;
107  static constexpr bool WRITABLE = true;
108 
109  ExtraLongPressMinDuration(uint16_t value = 0, HF::Interface *owner = nullptr):
110  Attribute<uint16_t>(HF::Interface::SIMPLE_BUTTON, ID, owner, value, WRITABLE)
111  {}
112  };
113 
118  {
119  static constexpr uint8_t ID = DOUBLE_CLICK_GAP_DURATION_ATTR;
120  static constexpr bool WRITABLE = true;
121 
122  DoubleClickGapDuration(uint16_t value = 0, HF::Interface *owner = nullptr):
123  Attribute<uint16_t>(HF::Interface::SIMPLE_BUTTON, ID, owner, value, WRITABLE)
124  {}
125  };
126 
138 
144  struct Base: public Interface<HF::Interface::SIMPLE_BUTTON>
145  {
146  protected:
147 
150  };
151 
157  class Server: public InterfaceRole<SimpleButton::Base, HF::Interface::SERVER_ROLE>
158  {
159  protected:
160 
163 
164 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
165  uint16_t _double_click_gap_duration;
167 #endif
168 
169  uint16_t _timestamp;
170 
171 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
172  bool _short_click_cmd;
173 #endif
174 
175  public:
176 
178  static constexpr uint16_t DOUBLE_CLICK_GAP_DURATION_MIN_VALUE = 0x0064;
179 
183 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
184  _double_click_gap_duration(DOUBLE_CLICK_GAP_DURATION_MIN_VALUE),
185 #endif
186  _timestamp(0),
187 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
188  _short_click_cmd(false)
189 #endif
190  {}
191 
193  virtual ~Server() {}
194 
195  // =============================================================================
196  // API
197  // =============================================================================
198 
206  void pressed(const Protocol::Address &addr, uint16_t timestamp);
207 
213  void pressed(uint16_t timestamp)
214  {
215  Protocol::Address addr;
216  pressed(addr, timestamp);
217  }
218 
228  void released(const Protocol::Address &addr, uint16_t timestamp);
229 
239  void released(uint16_t timestamp)
240  {
241  Protocol::Address addr;
242  released(addr, timestamp);
243  }
244 
245  protected:
246 
247  // ======================================================================
248  // Commands
249  // ======================================================================
252 
259  void short_press(const Protocol::Address &addr);
260 
265  void short_press()
266  {
267  Protocol::Address addr;
268  short_press(addr);
269  }
270 
277  void long_press(const Protocol::Address &addr);
278 
283  void long_press()
284  {
285  Protocol::Address addr;
286  long_press(addr);
287  }
288 
289 #ifdef HF_ITF_SIMPLE_BUTTON_EXTRA_LONG_PRESS_CMD
290 
296  void extra_long_press(const Protocol::Address &addr);
297 
302  void extra_long_press()
303  {
304  Protocol::Address addr;
305  extra_long_press(addr);
306  }
307 #endif
308 
309 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
310 
316  void double_click_press(const Protocol::Address &addr);
317 
322  void double_click_press()
323  {
324  Protocol::Address addr;
325  double_click_press(addr);
326  }
327 #endif
328 
330  // ======================================================================
331 
332  public:
333 
334  // =============================================================================
335  // Get/Set API.
336  // =============================================================================
337 
343  uint16_t short_press_max_duration() const;
344 
350  virtual void short_press_max_duration(uint16_t __value);
351 
357  uint16_t extra_long_press_min_duration() const;
358 
364  virtual void extra_long_press_min_duration(uint16_t __value);
365 
366 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
367 
372  uint16_t double_click_gap_duration() const;
373 
379  virtual void double_click_gap_duration(uint16_t __value);
380 #endif
381 
382  // =============================================================================
383  // Attribute API.
384  // =============================================================================
385 
387 
388  HF::Attributes::UIDS attributes(uint8_t pack_id =
390 
391  };
392 
398  struct Client: public InterfaceRole<SimpleButton::Base, HF::Interface::CLIENT_ROLE>
399  {
401 
402  virtual ~Client() {}
403 
404  // ======================================================================
405  // Commands
406  // ======================================================================
409 
411  // ======================================================================
412 
413  // ======================================================================
414  // Events
415  // ======================================================================
419 
420 #ifdef HF_ITF_SIMPLE_BUTTON_SHORT_PRESS_CMD
421 
427  virtual void short_press(const Protocol::Address &addr);
428 #endif
429 
430 #ifdef HF_ITF_SIMPLE_BUTTON_LONG_PRESS_CMD
431 
437  virtual void long_press(const Protocol::Address &addr);
438 #endif
439 
440 #ifdef HF_ITF_SIMPLE_BUTTON_EXTRA_LONG_PRESS_CMD
441 
447  virtual void extra_long_press(const Protocol::Address &addr);
448 #endif
449 
450 #ifdef HF_ITF_SIMPLE_BUTTON_DOUBLE_CLICK_PRESS_CMD
451 
457  virtual void double_click_press(const Protocol::Address &addr);
458 #endif
459 
461  // =============================================================================
462  protected:
463 
465  uint16_t offset);
466  };
467 
470  } // namespace SimpleButton
471 
472  } // namespace Interfaces
473 
474 } // namespace HF
475 
481 // =============================================================================
482 // Stream Helpers
483 // =============================================================================
484 
493 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::SimpleButton::CMD command);
494 
503 std::ostream &operator<<(std::ostream &stream,
507 #endif /* HF_ITF_SIMPLE_BUTTON_H */
uint16_t extra_long_press_min_duration() const
Get the Extra Long Press Min Duration for the Simple Button server.
Simple Button Interface : Parent.
Double Click Gap Duration attribute UID.
Definition: simple_button.h:80
Helper class to handle the Double Click Gap Duration attribute for the Simple Button interface...
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::SimpleBut...
uint16_t _short_press_max_duration
Short Press Max Duration.
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
static constexpr uint8_t ID
Attribute UID.
Simple Button Interface : Client side implementation.
List of attributes UIDs.
Definition: attributes.h:176
Return all mandatory attributes for the interface.
Definition: attributes.h:842
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
This file contains the definitions for the HAN-FUN protocol messages.
Extra Long Press Min Duration attribute UID.
Definition: simple_button.h:79
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
This file contains the definitions common to all interfaces.
static constexpr uint8_t ID
Attribute UID.
Simple Button interface UID.
Definition: interface.h:87
Helper class to handle the Short Press Max Duration attribute for the Simple Button interface...
Definition: simple_button.h:91
HF::Interface const * owner() const
Definition: attributes.h:414
This class represents a byte array.
void released(uint16_t timestamp)
This method is used to indicate that the button was released and generate the appropriate message...
uint16_t _timestamp
Timestamp of last call.
uint16_t short_press_max_duration() const
Get the Short Press Max Duration for the Simple Button server.
Short Press Max Duration attribute UID.
Definition: simple_button.h:78
Network Address.
Definition: protocol.h:201
uint16_t _extra_long_press_min_duration
Extra Long Press Minimum Duration.
static constexpr bool WRITABLE
Attribute Read/Write.
Definition: simple_button.h:94
uint16_t uid() const
This method returns the interface UID.
Definition: interface.h:374
static constexpr uint16_t DOUBLE_CLICK_GAP_DURATION_MIN_VALUE
This is the minimum value that the Double Click Gap Duration can have.
void short_press()
Send a HAN-FUN message containing a SimpleButton::SHORT_PRESS_CMD, to the broadcast network address...
HAN-FUN Protocol Packet.
Definition: protocol.h:298
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
static constexpr bool WRITABLE
Attribute Read/Write.
void released(const Protocol::Address &addr, uint16_t timestamp)
This method is used to indicate that the button was released and generate the appropriate message...
Helper class template for implementing a given interface role.
Definition: interface.h:394
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.
static constexpr bool WRITABLE
Attribute Read/Write.
void pressed(const Protocol::Address &addr, uint16_t timestamp)
This method is used to indicate that the button was pressed and generate the appropriate message...
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
Attribute(const uint16_t interface, const uint8_t uid, const HF::Interface *__owner, uint16_t data, bool writable=false)
Attribute template constructor.
Definition: attributes.h:360
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::SimpleButton::CMD command)
Convert the given command into a string and write it to the given stream.
Simple Button Interface : Server side implementation.
Common interface for all Interfaces.
Definition: interface.h:43
void pressed(uint16_t timestamp)
This method is used to indicate that the button was pressed.
Result
Commands result codes.
static constexpr uint8_t ID
Attribute UID.
Definition: simple_button.h:93
void long_press()
Send a HAN-FUN message containing a SimpleButton::LONG_PRESS_CMD, to the broadcast network address...
Helper class to handle the Extra Long Press Min Duration attribute for the Simple Button interface...
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22