Fawkes API  Fawkes Development Version
SwitchInterface.cpp
1 
2 /***************************************************************************
3  * SwitchInterface.cpp - Fawkes BlackBoard Interface - SwitchInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008 Tim Niemueller
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/SwitchInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <cstring>
29 #include <cstdlib>
30 
31 namespace fawkes {
32 
33 /** @class SwitchInterface <interfaces/SwitchInterface.h>
34  * SwitchInterface Fawkes BlackBoard Interface.
35  *
36  This interface provides access to LEDs. The interface controls
37  an intensity value between 0.0 (off) and 1.0 (on, max
38  intensity). LEDs that do not support intensity setting can only
39  be set to on and off.
40 
41  * @ingroup FawkesInterfaces
42  */
43 
44 
45 
46 /** Constructor */
47 SwitchInterface::SwitchInterface() : Interface()
48 {
49  data_size = sizeof(SwitchInterface_data_t);
50  data_ptr = malloc(data_size);
51  data = (SwitchInterface_data_t *)data_ptr;
52  data_ts = (interface_data_ts_t *)data_ptr;
53  memset(data_ptr, 0, data_size);
54  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
55  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value, "");
56  add_fieldinfo(IFT_FLOAT, "history", 1, &data->history, "");
57  add_fieldinfo(IFT_UINT32, "short_activations", 1, &data->short_activations, "");
58  add_fieldinfo(IFT_UINT32, "long_activations", 1, &data->long_activations, "");
59  add_fieldinfo(IFT_UINT32, "activation_count", 1, &data->activation_count, "");
60  add_messageinfo("SetMessage");
61  add_messageinfo("EnableSwitchMessage");
62  add_messageinfo("DisableSwitchMessage");
63  add_messageinfo("EnableDurationMessage");
64  unsigned char tmp_hash[] = {0xa7, 0xa4, 0xc, 0x19, 0x66, 0xa4, 0x87, 0x6b, 0xa9, 0x32, 0x95, 0x40, 0xc7, 0x82, 0x75, 0x6d};
65  set_hash(tmp_hash);
66 }
67 
68 /** Destructor */
69 SwitchInterface::~SwitchInterface()
70 {
71  free(data_ptr);
72 }
73 /* Methods */
74 /** Get enabled value.
75  *
76  True if the switch is currently enabled.
77 
78  * @return enabled value
79  */
80 bool
82 {
83  return data->enabled;
84 }
85 
86 /** Get maximum length of enabled value.
87  * @return length of enabled value, can be length of the array or number of
88  * maximum number of characters for a string
89  */
90 size_t
92 {
93  return 1;
94 }
95 
96 /** Set enabled value.
97  *
98  True if the switch is currently enabled.
99 
100  * @param new_enabled new enabled value
101  */
102 void
103 SwitchInterface::set_enabled(const bool new_enabled)
104 {
105  data->enabled = new_enabled;
106  data_changed = true;
107 }
108 
109 /** Get value value.
110  *
111  If switches support multiple states these can be indicated with
112  this value. For example for a switch that notes the intensity it
113  could be a value in the valid range.
114 
115  * @return value value
116  */
117 float
119 {
120  return data->value;
121 }
122 
123 /** Get maximum length of value value.
124  * @return length of value value, can be length of the array or number of
125  * maximum number of characters for a string
126  */
127 size_t
129 {
130  return 1;
131 }
132 
133 /** Set value value.
134  *
135  If switches support multiple states these can be indicated with
136  this value. For example for a switch that notes the intensity it
137  could be a value in the valid range.
138 
139  * @param new_value new value value
140  */
141 void
142 SwitchInterface::set_value(const float new_value)
143 {
144  data->value = new_value;
145  data_changed = true;
146 }
147 
148 /** Get history value.
149  *
150  This value records the number of seconds a switch has been
151  enabled continuously -- or not. The time is recorded in
152  seconds. A positive value indicates time the switch was turned
153  on, a negative value indicates the time (when converted to the
154  absolute value) the button has not been pressed. Zero means
155  "just initialized".
156 
157  * @return history value
158  */
159 float
161 {
162  return data->history;
163 }
164 
165 /** Get maximum length of history value.
166  * @return length of history value, can be length of the array or number of
167  * maximum number of characters for a string
168  */
169 size_t
171 {
172  return 1;
173 }
174 
175 /** Set history value.
176  *
177  This value records the number of seconds a switch has been
178  enabled continuously -- or not. The time is recorded in
179  seconds. A positive value indicates time the switch was turned
180  on, a negative value indicates the time (when converted to the
181  absolute value) the button has not been pressed. Zero means
182  "just initialized".
183 
184  * @param new_history new history value
185  */
186 void
187 SwitchInterface::set_history(const float new_history)
188 {
189  data->history = new_history;
190  data_changed = true;
191 }
192 
193 /** Get short_activations value.
194  *
195  Number of consecutive short clicks (turned on). Can be used to recognize
196  patterns of clicks. This is an optional field.
197 
198  * @return short_activations value
199  */
200 uint32_t
202 {
203  return data->short_activations;
204 }
205 
206 /** Get maximum length of short_activations value.
207  * @return length of short_activations value, can be length of the array or number of
208  * maximum number of characters for a string
209  */
210 size_t
212 {
213  return 1;
214 }
215 
216 /** Set short_activations value.
217  *
218  Number of consecutive short clicks (turned on). Can be used to recognize
219  patterns of clicks. This is an optional field.
220 
221  * @param new_short_activations new short_activations value
222  */
223 void
224 SwitchInterface::set_short_activations(const uint32_t new_short_activations)
225 {
226  data->short_activations = new_short_activations;
227  data_changed = true;
228 }
229 
230 /** Get long_activations value.
231  *
232  Number of consecutive short clicks (turned on). Can be used to recognize
233  patterns of clicks. This is an optional field.
234 
235  * @return long_activations value
236  */
237 uint32_t
239 {
240  return data->long_activations;
241 }
242 
243 /** Get maximum length of long_activations value.
244  * @return length of long_activations value, can be length of the array or number of
245  * maximum number of characters for a string
246  */
247 size_t
249 {
250  return 1;
251 }
252 
253 /** Set long_activations value.
254  *
255  Number of consecutive short clicks (turned on). Can be used to recognize
256  patterns of clicks. This is an optional field.
257 
258  * @param new_long_activations new long_activations value
259  */
260 void
261 SwitchInterface::set_long_activations(const uint32_t new_long_activations)
262 {
263  data->long_activations = new_long_activations;
264  data_changed = true;
265 }
266 
267 /** Get activation_count value.
268  *
269  Number that is to be incremented whenever a short or long activation
270  happened. Can be used to decide if a change in status happened.
271 
272  * @return activation_count value
273  */
274 uint32_t
276 {
277  return data->activation_count;
278 }
279 
280 /** Get maximum length of activation_count value.
281  * @return length of activation_count value, can be length of the array or number of
282  * maximum number of characters for a string
283  */
284 size_t
286 {
287  return 1;
288 }
289 
290 /** Set activation_count value.
291  *
292  Number that is to be incremented whenever a short or long activation
293  happened. Can be used to decide if a change in status happened.
294 
295  * @param new_activation_count new activation_count value
296  */
297 void
298 SwitchInterface::set_activation_count(const uint32_t new_activation_count)
299 {
300  data->activation_count = new_activation_count;
301  data_changed = true;
302 }
303 
304 /* =========== message create =========== */
305 Message *
307 {
308  if ( strncmp("SetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
309  return new SetMessage();
310  } else if ( strncmp("EnableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
311  return new EnableSwitchMessage();
312  } else if ( strncmp("DisableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
313  return new DisableSwitchMessage();
314  } else if ( strncmp("EnableDurationMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
315  return new EnableDurationMessage();
316  } else {
317  throw UnknownTypeException("The given type '%s' does not match any known "
318  "message type for this interface type.", type);
319  }
320 }
321 
322 
323 /** Copy values from other interface.
324  * @param other other interface to copy values from
325  */
326 void
328 {
329  const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other);
330  if (oi == NULL) {
331  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
332  type(), other->type());
333  }
334  memcpy(data, oi->data, sizeof(SwitchInterface_data_t));
335 }
336 
337 const char *
338 SwitchInterface::enum_tostring(const char *enumtype, int val) const
339 {
340  throw UnknownTypeException("Unknown enum type %s", enumtype);
341 }
342 
343 /* =========== messages =========== */
344 /** @class SwitchInterface::SetMessage <interfaces/SwitchInterface.h>
345  * SetMessage Fawkes BlackBoard Interface Message.
346  *
347 
348  */
349 
350 
351 /** Constructor with initial values.
352  * @param ini_enabled initial value for enabled
353  * @param ini_value initial value for value
354  */
355 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage")
356 {
357  data_size = sizeof(SetMessage_data_t);
358  data_ptr = malloc(data_size);
359  memset(data_ptr, 0, data_size);
360  data = (SetMessage_data_t *)data_ptr;
362  data->enabled = ini_enabled;
363  data->value = ini_value;
364  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
365  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value, "");
366 }
367 /** Constructor */
369 {
370  data_size = sizeof(SetMessage_data_t);
371  data_ptr = malloc(data_size);
372  memset(data_ptr, 0, data_size);
373  data = (SetMessage_data_t *)data_ptr;
375  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled, "");
376  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value, "");
377 }
378 
379 /** Destructor */
381 {
382  free(data_ptr);
383 }
384 
385 /** Copy constructor.
386  * @param m message to copy from
387  */
389 {
390  data_size = m->data_size;
391  data_ptr = malloc(data_size);
392  memcpy(data_ptr, m->data_ptr, data_size);
393  data = (SetMessage_data_t *)data_ptr;
395 }
396 
397 /* Methods */
398 /** Get enabled value.
399  *
400  True if the switch is currently enabled.
401 
402  * @return enabled value
403  */
404 bool
406 {
407  return data->enabled;
408 }
409 
410 /** Get maximum length of enabled value.
411  * @return length of enabled value, can be length of the array or number of
412  * maximum number of characters for a string
413  */
414 size_t
416 {
417  return 1;
418 }
419 
420 /** Set enabled value.
421  *
422  True if the switch is currently enabled.
423 
424  * @param new_enabled new enabled value
425  */
426 void
428 {
429  data->enabled = new_enabled;
430 }
431 
432 /** Get value value.
433  *
434  If switches support multiple states these can be indicated with
435  this value. For example for a switch that notes the intensity it
436  could be a value in the valid range.
437 
438  * @return value value
439  */
440 float
442 {
443  return data->value;
444 }
445 
446 /** Get maximum length of value value.
447  * @return length of value value, can be length of the array or number of
448  * maximum number of characters for a string
449  */
450 size_t
452 {
453  return 1;
454 }
455 
456 /** Set value value.
457  *
458  If switches support multiple states these can be indicated with
459  this value. For example for a switch that notes the intensity it
460  could be a value in the valid range.
461 
462  * @param new_value new value value
463  */
464 void
466 {
467  data->value = new_value;
468 }
469 
470 /** Clone this message.
471  * Produces a message of the same type as this message and copies the
472  * data to the new message.
473  * @return clone of this message
474  */
475 Message *
477 {
478  return new SwitchInterface::SetMessage(this);
479 }
480 /** @class SwitchInterface::EnableSwitchMessage <interfaces/SwitchInterface.h>
481  * EnableSwitchMessage Fawkes BlackBoard Interface Message.
482  *
483 
484  */
485 
486 
487 /** Constructor */
489 {
490  data_size = sizeof(EnableSwitchMessage_data_t);
491  data_ptr = malloc(data_size);
492  memset(data_ptr, 0, data_size);
493  data = (EnableSwitchMessage_data_t *)data_ptr;
495 }
496 
497 /** Destructor */
499 {
500  free(data_ptr);
501 }
502 
503 /** Copy constructor.
504  * @param m message to copy from
505  */
507 {
508  data_size = m->data_size;
509  data_ptr = malloc(data_size);
510  memcpy(data_ptr, m->data_ptr, data_size);
511  data = (EnableSwitchMessage_data_t *)data_ptr;
513 }
514 
515 /* Methods */
516 /** Clone this message.
517  * Produces a message of the same type as this message and copies the
518  * data to the new message.
519  * @return clone of this message
520  */
521 Message *
523 {
524  return new SwitchInterface::EnableSwitchMessage(this);
525 }
526 /** @class SwitchInterface::DisableSwitchMessage <interfaces/SwitchInterface.h>
527  * DisableSwitchMessage Fawkes BlackBoard Interface Message.
528  *
529 
530  */
531 
532 
533 /** Constructor */
535 {
536  data_size = sizeof(DisableSwitchMessage_data_t);
537  data_ptr = malloc(data_size);
538  memset(data_ptr, 0, data_size);
539  data = (DisableSwitchMessage_data_t *)data_ptr;
541 }
542 
543 /** Destructor */
545 {
546  free(data_ptr);
547 }
548 
549 /** Copy constructor.
550  * @param m message to copy from
551  */
553 {
554  data_size = m->data_size;
555  data_ptr = malloc(data_size);
556  memcpy(data_ptr, m->data_ptr, data_size);
557  data = (DisableSwitchMessage_data_t *)data_ptr;
559 }
560 
561 /* Methods */
562 /** Clone this message.
563  * Produces a message of the same type as this message and copies the
564  * data to the new message.
565  * @return clone of this message
566  */
567 Message *
569 {
570  return new SwitchInterface::DisableSwitchMessage(this);
571 }
572 /** @class SwitchInterface::EnableDurationMessage <interfaces/SwitchInterface.h>
573  * EnableDurationMessage Fawkes BlackBoard Interface Message.
574  *
575 
576  */
577 
578 
579 /** Constructor with initial values.
580  * @param ini_duration initial value for duration
581  * @param ini_value initial value for value
582  */
583 SwitchInterface::EnableDurationMessage::EnableDurationMessage(const float ini_duration, const float ini_value) : Message("EnableDurationMessage")
584 {
585  data_size = sizeof(EnableDurationMessage_data_t);
586  data_ptr = malloc(data_size);
587  memset(data_ptr, 0, data_size);
588  data = (EnableDurationMessage_data_t *)data_ptr;
590  data->duration = ini_duration;
591  data->value = ini_value;
592  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration, "");
593  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value, "");
594 }
595 /** Constructor */
597 {
598  data_size = sizeof(EnableDurationMessage_data_t);
599  data_ptr = malloc(data_size);
600  memset(data_ptr, 0, data_size);
601  data = (EnableDurationMessage_data_t *)data_ptr;
603  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration, "");
604  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value, "");
605 }
606 
607 /** Destructor */
609 {
610  free(data_ptr);
611 }
612 
613 /** Copy constructor.
614  * @param m message to copy from
615  */
617 {
618  data_size = m->data_size;
619  data_ptr = malloc(data_size);
620  memcpy(data_ptr, m->data_ptr, data_size);
621  data = (EnableDurationMessage_data_t *)data_ptr;
623 }
624 
625 /* Methods */
626 /** Get duration value.
627  * Duration in seconds for which
628  the switch should be enabled.
629  * @return duration value
630  */
631 float
633 {
634  return data->duration;
635 }
636 
637 /** Get maximum length of duration value.
638  * @return length of duration value, can be length of the array or number of
639  * maximum number of characters for a string
640  */
641 size_t
643 {
644  return 1;
645 }
646 
647 /** Set duration value.
648  * Duration in seconds for which
649  the switch should be enabled.
650  * @param new_duration new duration value
651  */
652 void
654 {
655  data->duration = new_duration;
656 }
657 
658 /** Get value value.
659  *
660  If switches support multiple states these can be indicated with
661  this value. For example for a switch that notes the intensity it
662  could be a value in the valid range.
663 
664  * @return value value
665  */
666 float
668 {
669  return data->value;
670 }
671 
672 /** Get maximum length of value value.
673  * @return length of value value, can be length of the array or number of
674  * maximum number of characters for a string
675  */
676 size_t
678 {
679  return 1;
680 }
681 
682 /** Set value value.
683  *
684  If switches support multiple states these can be indicated with
685  this value. For example for a switch that notes the intensity it
686  could be a value in the valid range.
687 
688  * @param new_value new value value
689  */
690 void
692 {
693  data->value = new_value;
694 }
695 
696 /** Clone this message.
697  * Produces a message of the same type as this message and copies the
698  * data to the new message.
699  * @return clone of this message
700  */
701 Message *
703 {
705 }
706 /** Check if message is valid and can be enqueued.
707  * @param message Message to check
708  * @return true if the message is valid, false otherwise.
709  */
710 bool
712 {
713  const SetMessage *m0 = dynamic_cast<const SetMessage *>(message);
714  if ( m0 != NULL ) {
715  return true;
716  }
717  const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message);
718  if ( m1 != NULL ) {
719  return true;
720  }
721  const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message);
722  if ( m2 != NULL ) {
723  return true;
724  }
725  const EnableDurationMessage *m3 = dynamic_cast<const EnableDurationMessage *>(message);
726  if ( m3 != NULL ) {
727  return true;
728  }
729  return false;
730 }
731 
732 /// @cond INTERNALS
733 EXPORT_INTERFACE(SwitchInterface)
734 /// @endcond
735 
736 
737 } // end namespace fawkes
size_t maxlenof_enabled() const
Get maximum length of enabled value.
bool is_enabled() const
Get enabled value.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:114
size_t maxlenof_value() const
Get maximum length of value value.
virtual Message * clone() const
Clone this message.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:43
SetMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_duration() const
Get maximum length of duration value.
void set_value(const float new_value)
Set value value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:312
const char * type() const
Get message type.
Definition: message.cpp:378
size_t maxlenof_value() const
Get maximum length of value value.
void set_enabled(const bool new_enabled)
Set enabled value.
Fawkes library namespace.
float value() const
Get value value.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:119
virtual Message * clone() const
Clone this message.
uint32_t long_activations() const
Get long_activations value.
size_t maxlenof_value() const
Get maximum length of value value.
void set_value(const float new_value)
Set value value.
void set_history(const float new_history)
Set history value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
void set_short_activations(const uint32_t new_short_activations)
Set short_activations value.
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:123
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:115
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:368
size_t maxlenof_enabled() const
Get maximum length of enabled value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:208
SwitchInterface Fawkes BlackBoard Interface.
float history() const
Get history value.
uint32_t short_activations() const
Get short_activations value.
void set_activation_count(const uint32_t new_activation_count)
Set activation_count value.
void set_enabled(const bool new_enabled)
Set enabled value.
bool is_enabled() const
Get enabled value.
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
uint32_t activation_count() const
Get activation_count value.
virtual Message * clone() const
Clone this message.
void set_long_activations(const uint32_t new_long_activations)
Set long_activations value.
float field
Definition: types.h:43
size_t maxlenof_activation_count() const
Get maximum length of activation_count value.
size_t maxlenof_long_activations() const
Get maximum length of long_activations value.
float duration() const
Get duration value.
virtual Message * clone() const
Clone this message.
virtual void copy_values(const Interface *other)
Copy values from other interface.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
size_t maxlenof_history() const
Get maximum length of history value.
void set_value(const float new_value)
Set value value.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0)
Add an entry to the info list.
Definition: message.cpp:435
float value() const
Get value value.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
size_t maxlenof_short_activations() const
Get maximum length of short_activations value.
EnableDurationMessage Fawkes BlackBoard Interface Message.
virtual Message * create_message(const char *type) const
Create message based on type name.
boolean field
Definition: types.h:34
void set_duration(const float new_duration)
Set duration value.
const char * type() const
Get type of interface.
Definition: interface.cpp:635
32 bit unsigned integer field
Definition: types.h:40