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 <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class SwitchInterface <interfaces/SwitchInterface.h>
36  * SwitchInterface Fawkes BlackBoard Interface.
37  *
38  This interface provides access to LEDs. The interface controls
39  an intensity value between 0.0 (off) and 1.0 (on, max
40  intensity). LEDs that do not support intensity setting can only
41  be set to on and off.
42 
43  * @ingroup FawkesInterfaces
44  */
45 
46 
47 
48 /** Constructor */
49 SwitchInterface::SwitchInterface() : Interface()
50 {
51  data_size = sizeof(SwitchInterface_data_t);
52  data_ptr = malloc(data_size);
53  data = (SwitchInterface_data_t *)data_ptr;
54  data_ts = (interface_data_ts_t *)data_ptr;
55  memset(data_ptr, 0, data_size);
56  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
57  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
58  add_fieldinfo(IFT_FLOAT, "history", 1, &data->history);
59  add_fieldinfo(IFT_UINT32, "short_activations", 1, &data->short_activations);
60  add_fieldinfo(IFT_UINT32, "long_activations", 1, &data->long_activations);
61  add_fieldinfo(IFT_UINT32, "activation_count", 1, &data->activation_count);
62  add_messageinfo("SetMessage");
63  add_messageinfo("EnableSwitchMessage");
64  add_messageinfo("DisableSwitchMessage");
65  add_messageinfo("EnableDurationMessage");
66  unsigned char tmp_hash[] = {0xa7, 0xa4, 0xc, 0x19, 0x66, 0xa4, 0x87, 0x6b, 0xa9, 0x32, 0x95, 0x40, 0xc7, 0x82, 0x75, 0x6d};
67  set_hash(tmp_hash);
68 }
69 
70 /** Destructor */
71 SwitchInterface::~SwitchInterface()
72 {
73  free(data_ptr);
74 }
75 /* Methods */
76 /** Get enabled value.
77  *
78  True if the switch is currently enabled.
79 
80  * @return enabled value
81  */
82 bool
83 SwitchInterface::is_enabled() const
84 {
85  return data->enabled;
86 }
87 
88 /** Get maximum length of enabled value.
89  * @return length of enabled value, can be length of the array or number of
90  * maximum number of characters for a string
91  */
92 size_t
93 SwitchInterface::maxlenof_enabled() const
94 {
95  return 1;
96 }
97 
98 /** Set enabled value.
99  *
100  True if the switch is currently enabled.
101 
102  * @param new_enabled new enabled value
103  */
104 void
105 SwitchInterface::set_enabled(const bool new_enabled)
106 {
107  set_field(data->enabled, new_enabled);
108 }
109 
110 /** Get value value.
111  *
112  If switches support multiple states these can be indicated with
113  this value. For example for a switch that notes the intensity it
114  could be a value in the valid range.
115 
116  * @return value value
117  */
118 float
119 SwitchInterface::value() const
120 {
121  return data->value;
122 }
123 
124 /** Get maximum length of value value.
125  * @return length of value value, can be length of the array or number of
126  * maximum number of characters for a string
127  */
128 size_t
129 SwitchInterface::maxlenof_value() const
130 {
131  return 1;
132 }
133 
134 /** Set value value.
135  *
136  If switches support multiple states these can be indicated with
137  this value. For example for a switch that notes the intensity it
138  could be a value in the valid range.
139 
140  * @param new_value new value value
141  */
142 void
143 SwitchInterface::set_value(const float new_value)
144 {
145  set_field(data->value, new_value);
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
160 SwitchInterface::history() const
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
170 SwitchInterface::maxlenof_history() const
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  set_field(data->history, new_history);
190 }
191 
192 /** Get short_activations value.
193  *
194  Number of consecutive short clicks (turned on). Can be used to recognize
195  patterns of clicks. This is an optional field.
196 
197  * @return short_activations value
198  */
199 uint32_t
200 SwitchInterface::short_activations() const
201 {
202  return data->short_activations;
203 }
204 
205 /** Get maximum length of short_activations value.
206  * @return length of short_activations value, can be length of the array or number of
207  * maximum number of characters for a string
208  */
209 size_t
210 SwitchInterface::maxlenof_short_activations() const
211 {
212  return 1;
213 }
214 
215 /** Set short_activations value.
216  *
217  Number of consecutive short clicks (turned on). Can be used to recognize
218  patterns of clicks. This is an optional field.
219 
220  * @param new_short_activations new short_activations value
221  */
222 void
223 SwitchInterface::set_short_activations(const uint32_t new_short_activations)
224 {
225  set_field(data->short_activations, new_short_activations);
226 }
227 
228 /** Get long_activations value.
229  *
230  Number of consecutive short clicks (turned on). Can be used to recognize
231  patterns of clicks. This is an optional field.
232 
233  * @return long_activations value
234  */
235 uint32_t
236 SwitchInterface::long_activations() const
237 {
238  return data->long_activations;
239 }
240 
241 /** Get maximum length of long_activations value.
242  * @return length of long_activations value, can be length of the array or number of
243  * maximum number of characters for a string
244  */
245 size_t
246 SwitchInterface::maxlenof_long_activations() const
247 {
248  return 1;
249 }
250 
251 /** Set long_activations value.
252  *
253  Number of consecutive short clicks (turned on). Can be used to recognize
254  patterns of clicks. This is an optional field.
255 
256  * @param new_long_activations new long_activations value
257  */
258 void
259 SwitchInterface::set_long_activations(const uint32_t new_long_activations)
260 {
261  set_field(data->long_activations, new_long_activations);
262 }
263 
264 /** Get activation_count value.
265  *
266  Number that is to be incremented whenever a short or long activation
267  happened. Can be used to decide if a change in status happened.
268 
269  * @return activation_count value
270  */
271 uint32_t
272 SwitchInterface::activation_count() const
273 {
274  return data->activation_count;
275 }
276 
277 /** Get maximum length of activation_count value.
278  * @return length of activation_count value, can be length of the array or number of
279  * maximum number of characters for a string
280  */
281 size_t
282 SwitchInterface::maxlenof_activation_count() const
283 {
284  return 1;
285 }
286 
287 /** Set activation_count value.
288  *
289  Number that is to be incremented whenever a short or long activation
290  happened. Can be used to decide if a change in status happened.
291 
292  * @param new_activation_count new activation_count value
293  */
294 void
295 SwitchInterface::set_activation_count(const uint32_t new_activation_count)
296 {
297  set_field(data->activation_count, new_activation_count);
298 }
299 
300 /* =========== message create =========== */
301 Message *
302 SwitchInterface::create_message(const char *type) const
303 {
304  if ( strncmp("SetMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
305  return new SetMessage();
306  } else if ( strncmp("EnableSwitchMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
307  return new EnableSwitchMessage();
308  } else if ( strncmp("DisableSwitchMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
309  return new DisableSwitchMessage();
310  } else if ( strncmp("EnableDurationMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
311  return new EnableDurationMessage();
312  } else {
313  throw UnknownTypeException("The given type '%s' does not match any known "
314  "message type for this interface type.", type);
315  }
316 }
317 
318 
319 /** Copy values from other interface.
320  * @param other other interface to copy values from
321  */
322 void
323 SwitchInterface::copy_values(const Interface *other)
324 {
325  const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other);
326  if (oi == NULL) {
327  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
328  type(), other->type());
329  }
330  memcpy(data, oi->data, sizeof(SwitchInterface_data_t));
331 }
332 
333 const char *
334 SwitchInterface::enum_tostring(const char *enumtype, int val) const
335 {
336  throw UnknownTypeException("Unknown enum type %s", enumtype);
337 }
338 
339 /* =========== messages =========== */
340 /** @class SwitchInterface::SetMessage <interfaces/SwitchInterface.h>
341  * SetMessage Fawkes BlackBoard Interface Message.
342  *
343 
344  */
345 
346 
347 /** Constructor with initial values.
348  * @param ini_enabled initial value for enabled
349  * @param ini_value initial value for value
350  */
351 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage")
352 {
353  data_size = sizeof(SetMessage_data_t);
354  data_ptr = malloc(data_size);
355  memset(data_ptr, 0, data_size);
356  data = (SetMessage_data_t *)data_ptr;
358  data->enabled = ini_enabled;
359  data->value = ini_value;
360  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
361  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
362 }
363 /** Constructor */
365 {
366  data_size = sizeof(SetMessage_data_t);
367  data_ptr = malloc(data_size);
368  memset(data_ptr, 0, data_size);
369  data = (SetMessage_data_t *)data_ptr;
371  add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
372  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
373 }
374 
375 /** Destructor */
377 {
378  free(data_ptr);
379 }
380 
381 /** Copy constructor.
382  * @param m message to copy from
383  */
385 {
386  data_size = m->data_size;
387  data_ptr = malloc(data_size);
388  memcpy(data_ptr, m->data_ptr, data_size);
389  data = (SetMessage_data_t *)data_ptr;
391 }
392 
393 /* Methods */
394 /** Get enabled value.
395  *
396  True if the switch is currently enabled.
397 
398  * @return enabled value
399  */
400 bool
402 {
403  return data->enabled;
404 }
405 
406 /** Get maximum length of enabled value.
407  * @return length of enabled value, can be length of the array or number of
408  * maximum number of characters for a string
409  */
410 size_t
412 {
413  return 1;
414 }
415 
416 /** Set enabled value.
417  *
418  True if the switch is currently enabled.
419 
420  * @param new_enabled new enabled value
421  */
422 void
424 {
425  set_field(data->enabled, new_enabled);
426 }
427 
428 /** Get value value.
429  *
430  If switches support multiple states these can be indicated with
431  this value. For example for a switch that notes the intensity it
432  could be a value in the valid range.
433 
434  * @return value value
435  */
436 float
438 {
439  return data->value;
440 }
441 
442 /** Get maximum length of value value.
443  * @return length of value value, can be length of the array or number of
444  * maximum number of characters for a string
445  */
446 size_t
448 {
449  return 1;
450 }
451 
452 /** Set value value.
453  *
454  If switches support multiple states these can be indicated with
455  this value. For example for a switch that notes the intensity it
456  could be a value in the valid range.
457 
458  * @param new_value new value value
459  */
460 void
462 {
463  set_field(data->value, new_value);
464 }
465 
466 /** Clone this message.
467  * Produces a message of the same type as this message and copies the
468  * data to the new message.
469  * @return clone of this message
470  */
471 Message *
473 {
474  return new SwitchInterface::SetMessage(this);
475 }
476 /** @class SwitchInterface::EnableSwitchMessage <interfaces/SwitchInterface.h>
477  * EnableSwitchMessage Fawkes BlackBoard Interface Message.
478  *
479 
480  */
481 
482 
483 /** Constructor */
485 {
486  data_size = sizeof(EnableSwitchMessage_data_t);
487  data_ptr = malloc(data_size);
488  memset(data_ptr, 0, data_size);
489  data = (EnableSwitchMessage_data_t *)data_ptr;
491 }
492 
493 /** Destructor */
495 {
496  free(data_ptr);
497 }
498 
499 /** Copy constructor.
500  * @param m message to copy from
501  */
503 {
504  data_size = m->data_size;
505  data_ptr = malloc(data_size);
506  memcpy(data_ptr, m->data_ptr, data_size);
507  data = (EnableSwitchMessage_data_t *)data_ptr;
509 }
510 
511 /* Methods */
512 /** Clone this message.
513  * Produces a message of the same type as this message and copies the
514  * data to the new message.
515  * @return clone of this message
516  */
517 Message *
519 {
520  return new SwitchInterface::EnableSwitchMessage(this);
521 }
522 /** @class SwitchInterface::DisableSwitchMessage <interfaces/SwitchInterface.h>
523  * DisableSwitchMessage Fawkes BlackBoard Interface Message.
524  *
525 
526  */
527 
528 
529 /** Constructor */
531 {
532  data_size = sizeof(DisableSwitchMessage_data_t);
533  data_ptr = malloc(data_size);
534  memset(data_ptr, 0, data_size);
535  data = (DisableSwitchMessage_data_t *)data_ptr;
537 }
538 
539 /** Destructor */
541 {
542  free(data_ptr);
543 }
544 
545 /** Copy constructor.
546  * @param m message to copy from
547  */
549 {
550  data_size = m->data_size;
551  data_ptr = malloc(data_size);
552  memcpy(data_ptr, m->data_ptr, data_size);
553  data = (DisableSwitchMessage_data_t *)data_ptr;
555 }
556 
557 /* Methods */
558 /** Clone this message.
559  * Produces a message of the same type as this message and copies the
560  * data to the new message.
561  * @return clone of this message
562  */
563 Message *
565 {
566  return new SwitchInterface::DisableSwitchMessage(this);
567 }
568 /** @class SwitchInterface::EnableDurationMessage <interfaces/SwitchInterface.h>
569  * EnableDurationMessage Fawkes BlackBoard Interface Message.
570  *
571 
572  */
573 
574 
575 /** Constructor with initial values.
576  * @param ini_duration initial value for duration
577  * @param ini_value initial value for value
578  */
579 SwitchInterface::EnableDurationMessage::EnableDurationMessage(const float ini_duration, const float ini_value) : Message("EnableDurationMessage")
580 {
581  data_size = sizeof(EnableDurationMessage_data_t);
582  data_ptr = malloc(data_size);
583  memset(data_ptr, 0, data_size);
584  data = (EnableDurationMessage_data_t *)data_ptr;
586  data->duration = ini_duration;
587  data->value = ini_value;
588  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
589  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
590 }
591 /** Constructor */
593 {
594  data_size = sizeof(EnableDurationMessage_data_t);
595  data_ptr = malloc(data_size);
596  memset(data_ptr, 0, data_size);
597  data = (EnableDurationMessage_data_t *)data_ptr;
599  add_fieldinfo(IFT_FLOAT, "duration", 1, &data->duration);
600  add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
601 }
602 
603 /** Destructor */
605 {
606  free(data_ptr);
607 }
608 
609 /** Copy constructor.
610  * @param m message to copy from
611  */
613 {
614  data_size = m->data_size;
615  data_ptr = malloc(data_size);
616  memcpy(data_ptr, m->data_ptr, data_size);
617  data = (EnableDurationMessage_data_t *)data_ptr;
619 }
620 
621 /* Methods */
622 /** Get duration value.
623  * Duration in seconds for which
624  the switch should be enabled.
625  * @return duration value
626  */
627 float
629 {
630  return data->duration;
631 }
632 
633 /** Get maximum length of duration value.
634  * @return length of duration value, can be length of the array or number of
635  * maximum number of characters for a string
636  */
637 size_t
639 {
640  return 1;
641 }
642 
643 /** Set duration value.
644  * Duration in seconds for which
645  the switch should be enabled.
646  * @param new_duration new duration value
647  */
648 void
650 {
651  set_field(data->duration, new_duration);
652 }
653 
654 /** Get value value.
655  *
656  If switches support multiple states these can be indicated with
657  this value. For example for a switch that notes the intensity it
658  could be a value in the valid range.
659 
660  * @return value value
661  */
662 float
664 {
665  return data->value;
666 }
667 
668 /** Get maximum length of value value.
669  * @return length of value value, can be length of the array or number of
670  * maximum number of characters for a string
671  */
672 size_t
674 {
675  return 1;
676 }
677 
678 /** Set value value.
679  *
680  If switches support multiple states these can be indicated with
681  this value. For example for a switch that notes the intensity it
682  could be a value in the valid range.
683 
684  * @param new_value new value value
685  */
686 void
688 {
689  set_field(data->value, new_value);
690 }
691 
692 /** Clone this message.
693  * Produces a message of the same type as this message and copies the
694  * data to the new message.
695  * @return clone of this message
696  */
697 Message *
699 {
701 }
702 /** Check if message is valid and can be enqueued.
703  * @param message Message to check
704  * @return true if the message is valid, false otherwise.
705  */
706 bool
708 {
709  const SetMessage *m0 = dynamic_cast<const SetMessage *>(message);
710  if ( m0 != NULL ) {
711  return true;
712  }
713  const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message);
714  if ( m1 != NULL ) {
715  return true;
716  }
717  const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message);
718  if ( m2 != NULL ) {
719  return true;
720  }
721  const EnableDurationMessage *m3 = dynamic_cast<const EnableDurationMessage *>(message);
722  if ( m3 != NULL ) {
723  return true;
724  }
725  return false;
726 }
727 
728 /// @cond INTERNALS
729 EXPORT_INTERFACE(SwitchInterface)
730 /// @endcond
731 
732 
733 } // end namespace fawkes
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * type() const
Get type of interface.
Definition: interface.cpp:652
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:244
void set_field(FieldT &field, DataT &data)
Set a field, set data_changed to true and update data_changed accordingly.
Definition: interface.h:304
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:435
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:146
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:156
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:147
DisableSwitchMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
EnableDurationMessage Fawkes BlackBoard Interface Message.
void set_duration(const float new_duration)
Set duration value.
void set_value(const float new_value)
Set value value.
size_t maxlenof_duration() const
Get maximum length of duration value.
virtual Message * clone() const
Clone this message.
float duration() const
Get duration value.
size_t maxlenof_value() const
Get maximum length of value value.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
SetMessage Fawkes BlackBoard Interface Message.
void set_enabled(const bool new_enabled)
Set enabled value.
float value() const
Get value value.
virtual Message * clone() const
Clone this message.
bool is_enabled() const
Get enabled value.
void set_value(const float new_value)
Set value value.
size_t maxlenof_enabled() const
Get maximum length of enabled value.
size_t maxlenof_value() const
Get maximum length of value value.
SwitchInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Fawkes library namespace.
@ IFT_FLOAT
float field
Definition: types.h:46
@ IFT_BOOL
boolean field
Definition: types.h:37
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152