Fawkes API  Fawkes Development Version
LedInterface.cpp
1 
2 /***************************************************************************
3  * LedInterface.cpp - Fawkes BlackBoard Interface - LedInterface
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/LedInterface.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 LedInterface <interfaces/LedInterface.h>
36  * LedInterface Fawkes BlackBoard Interface.
37  *
38  This interface provides access to LEDs. The interface controls an
39  intensity value between 0.0 (off) and 1.0 (on, max intensity). LEDs
40  that do not support intensity setting can only be set to on and off.
41 
42  * @ingroup FawkesInterfaces
43  */
44 
45 
46 /** ON constant */
47 const float LedInterface::ON = 1.0;
48 /** OFF constant */
49 const float LedInterface::OFF = 0.0;
50 
51 /** Constructor */
52 LedInterface::LedInterface() : Interface()
53 {
54  data_size = sizeof(LedInterface_data_t);
55  data_ptr = malloc(data_size);
56  data = (LedInterface_data_t *)data_ptr;
57  data_ts = (interface_data_ts_t *)data_ptr;
58  memset(data_ptr, 0, data_size);
59  add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
60  add_messageinfo("SetIntensityMessage");
61  add_messageinfo("TurnOnMessage");
62  add_messageinfo("TurnOffMessage");
63  unsigned char tmp_hash[] = {0xd, 0x86, 0x60, 0xcd, 0xae, 0x41, 0xa5, 0xa1, 0xbc, 0xb7, 0xf, 0x9, 0x90, 00, 0x4d, 0x40};
64  set_hash(tmp_hash);
65 }
66 
67 /** Destructor */
68 LedInterface::~LedInterface()
69 {
70  free(data_ptr);
71 }
72 /* Methods */
73 /** Get intensity value.
74  * Intensity value.
75  * @return intensity value
76  */
77 float
78 LedInterface::intensity() const
79 {
80  return data->intensity;
81 }
82 
83 /** Get maximum length of intensity value.
84  * @return length of intensity value, can be length of the array or number of
85  * maximum number of characters for a string
86  */
87 size_t
88 LedInterface::maxlenof_intensity() const
89 {
90  return 1;
91 }
92 
93 /** Set intensity value.
94  * Intensity value.
95  * @param new_intensity new intensity value
96  */
97 void
98 LedInterface::set_intensity(const float new_intensity)
99 {
100  set_field(data->intensity, new_intensity);
101 }
102 
103 /* =========== message create =========== */
104 Message *
105 LedInterface::create_message(const char *type) const
106 {
107  if ( strncmp("SetIntensityMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
108  return new SetIntensityMessage();
109  } else if ( strncmp("TurnOnMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
110  return new TurnOnMessage();
111  } else if ( strncmp("TurnOffMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
112  return new TurnOffMessage();
113  } else {
114  throw UnknownTypeException("The given type '%s' does not match any known "
115  "message type for this interface type.", type);
116  }
117 }
118 
119 
120 /** Copy values from other interface.
121  * @param other other interface to copy values from
122  */
123 void
124 LedInterface::copy_values(const Interface *other)
125 {
126  const LedInterface *oi = dynamic_cast<const LedInterface *>(other);
127  if (oi == NULL) {
128  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
129  type(), other->type());
130  }
131  memcpy(data, oi->data, sizeof(LedInterface_data_t));
132 }
133 
134 const char *
135 LedInterface::enum_tostring(const char *enumtype, int val) const
136 {
137  throw UnknownTypeException("Unknown enum type %s", enumtype);
138 }
139 
140 /* =========== messages =========== */
141 /** @class LedInterface::SetIntensityMessage <interfaces/LedInterface.h>
142  * SetIntensityMessage Fawkes BlackBoard Interface Message.
143  *
144 
145  */
146 
147 
148 /** Constructor with initial values.
149  * @param ini_time_sec initial value for time_sec
150  * @param ini_intensity initial value for intensity
151  */
152 LedInterface::SetIntensityMessage::SetIntensityMessage(const float ini_time_sec, const float ini_intensity) : Message("SetIntensityMessage")
153 {
154  data_size = sizeof(SetIntensityMessage_data_t);
155  data_ptr = malloc(data_size);
156  memset(data_ptr, 0, data_size);
157  data = (SetIntensityMessage_data_t *)data_ptr;
159  data->time_sec = ini_time_sec;
160  data->intensity = ini_intensity;
161  add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
162  add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
163 }
164 /** Constructor */
166 {
167  data_size = sizeof(SetIntensityMessage_data_t);
168  data_ptr = malloc(data_size);
169  memset(data_ptr, 0, data_size);
170  data = (SetIntensityMessage_data_t *)data_ptr;
172  add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
173  add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
174 }
175 
176 /** Destructor */
178 {
179  free(data_ptr);
180 }
181 
182 /** Copy constructor.
183  * @param m message to copy from
184  */
186 {
187  data_size = m->data_size;
188  data_ptr = malloc(data_size);
189  memcpy(data_ptr, m->data_ptr, data_size);
190  data = (SetIntensityMessage_data_t *)data_ptr;
192 }
193 
194 /* Methods */
195 /** Get time_sec value.
196  *
197  Time in seconds when to reach the intensity.
198 
199  * @return time_sec value
200  */
201 float
203 {
204  return data->time_sec;
205 }
206 
207 /** Get maximum length of time_sec value.
208  * @return length of time_sec value, can be length of the array or number of
209  * maximum number of characters for a string
210  */
211 size_t
213 {
214  return 1;
215 }
216 
217 /** Set time_sec value.
218  *
219  Time in seconds when to reach the intensity.
220 
221  * @param new_time_sec new time_sec value
222  */
223 void
225 {
226  set_field(data->time_sec, new_time_sec);
227 }
228 
229 /** Get intensity value.
230  * Intensity value.
231  * @return intensity value
232  */
233 float
235 {
236  return data->intensity;
237 }
238 
239 /** Get maximum length of intensity value.
240  * @return length of intensity value, can be length of the array or number of
241  * maximum number of characters for a string
242  */
243 size_t
245 {
246  return 1;
247 }
248 
249 /** Set intensity value.
250  * Intensity value.
251  * @param new_intensity new intensity value
252  */
253 void
255 {
256  set_field(data->intensity, new_intensity);
257 }
258 
259 /** Clone this message.
260  * Produces a message of the same type as this message and copies the
261  * data to the new message.
262  * @return clone of this message
263  */
264 Message *
266 {
267  return new LedInterface::SetIntensityMessage(this);
268 }
269 /** @class LedInterface::TurnOnMessage <interfaces/LedInterface.h>
270  * TurnOnMessage Fawkes BlackBoard Interface Message.
271  *
272 
273  */
274 
275 
276 /** Constructor */
278 {
279  data_size = sizeof(TurnOnMessage_data_t);
280  data_ptr = malloc(data_size);
281  memset(data_ptr, 0, data_size);
282  data = (TurnOnMessage_data_t *)data_ptr;
284 }
285 
286 /** Destructor */
288 {
289  free(data_ptr);
290 }
291 
292 /** Copy constructor.
293  * @param m message to copy from
294  */
296 {
297  data_size = m->data_size;
298  data_ptr = malloc(data_size);
299  memcpy(data_ptr, m->data_ptr, data_size);
300  data = (TurnOnMessage_data_t *)data_ptr;
302 }
303 
304 /* Methods */
305 /** Clone this message.
306  * Produces a message of the same type as this message and copies the
307  * data to the new message.
308  * @return clone of this message
309  */
310 Message *
312 {
313  return new LedInterface::TurnOnMessage(this);
314 }
315 /** @class LedInterface::TurnOffMessage <interfaces/LedInterface.h>
316  * TurnOffMessage Fawkes BlackBoard Interface Message.
317  *
318 
319  */
320 
321 
322 /** Constructor */
324 {
325  data_size = sizeof(TurnOffMessage_data_t);
326  data_ptr = malloc(data_size);
327  memset(data_ptr, 0, data_size);
328  data = (TurnOffMessage_data_t *)data_ptr;
330 }
331 
332 /** Destructor */
334 {
335  free(data_ptr);
336 }
337 
338 /** Copy constructor.
339  * @param m message to copy from
340  */
342 {
343  data_size = m->data_size;
344  data_ptr = malloc(data_size);
345  memcpy(data_ptr, m->data_ptr, data_size);
346  data = (TurnOffMessage_data_t *)data_ptr;
348 }
349 
350 /* Methods */
351 /** Clone this message.
352  * Produces a message of the same type as this message and copies the
353  * data to the new message.
354  * @return clone of this message
355  */
356 Message *
358 {
359  return new LedInterface::TurnOffMessage(this);
360 }
361 /** Check if message is valid and can be enqueued.
362  * @param message Message to check
363  * @return true if the message is valid, false otherwise.
364  */
365 bool
367 {
368  const SetIntensityMessage *m0 = dynamic_cast<const SetIntensityMessage *>(message);
369  if ( m0 != NULL ) {
370  return true;
371  }
372  const TurnOnMessage *m1 = dynamic_cast<const TurnOnMessage *>(message);
373  if ( m1 != NULL ) {
374  return true;
375  }
376  const TurnOffMessage *m2 = dynamic_cast<const TurnOffMessage *>(message);
377  if ( m2 != NULL ) {
378  return true;
379  }
380  return false;
381 }
382 
383 /// @cond INTERNALS
384 EXPORT_INTERFACE(LedInterface)
385 /// @endcond
386 
387 
388 } // 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
SetIntensityMessage Fawkes BlackBoard Interface Message.
Definition: LedInterface.h:56
size_t maxlenof_time_sec() const
Get maximum length of time_sec value.
float intensity() const
Get intensity value.
size_t maxlenof_intensity() const
Get maximum length of intensity value.
float time_sec() const
Get time_sec value.
void set_time_sec(const float new_time_sec)
Set time_sec value.
virtual Message * clone() const
Clone this message.
void set_intensity(const float new_intensity)
Set intensity value.
TurnOffMessage Fawkes BlackBoard Interface Message.
Definition: LedInterface.h:107
virtual Message * clone() const
Clone this message.
TurnOnMessage Fawkes BlackBoard Interface Message.
Definition: LedInterface.h:87
virtual Message * clone() const
Clone this message.
LedInterface Fawkes BlackBoard Interface.
Definition: LedInterface.h:34
static const float OFF
OFF constant.
Definition: LedInterface.h:41
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
static const float ON
ON constant.
Definition: LedInterface.h:40
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
Fawkes library namespace.
@ IFT_FLOAT
float field
Definition: types.h:46
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:152