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
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
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  data->intensity = new_intensity;
101  data_changed = true;
102 }
103 
104 /* =========== message create =========== */
105 Message *
106 LedInterface::create_message(const char *type) const
107 {
108  if ( strncmp("SetIntensityMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
109  return new SetIntensityMessage();
110  } else if ( strncmp("TurnOnMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
111  return new TurnOnMessage();
112  } else if ( strncmp("TurnOffMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
113  return new TurnOffMessage();
114  } else {
115  throw UnknownTypeException("The given type '%s' does not match any known "
116  "message type for this interface type.", type);
117  }
118 }
119 
120 
121 /** Copy values from other interface.
122  * @param other other interface to copy values from
123  */
124 void
125 LedInterface::copy_values(const Interface *other)
126 {
127  const LedInterface *oi = dynamic_cast<const LedInterface *>(other);
128  if (oi == NULL) {
129  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
130  type(), other->type());
131  }
132  memcpy(data, oi->data, sizeof(LedInterface_data_t));
133 }
134 
135 const char *
136 LedInterface::enum_tostring(const char *enumtype, int val) const
137 {
138  throw UnknownTypeException("Unknown enum type %s", enumtype);
139 }
140 
141 /* =========== messages =========== */
142 /** @class LedInterface::SetIntensityMessage <interfaces/LedInterface.h>
143  * SetIntensityMessage Fawkes BlackBoard Interface Message.
144  *
145 
146  */
147 
148 
149 /** Constructor with initial values.
150  * @param ini_time_sec initial value for time_sec
151  * @param ini_intensity initial value for intensity
152  */
153 LedInterface::SetIntensityMessage::SetIntensityMessage(const float ini_time_sec, const float ini_intensity) : Message("SetIntensityMessage")
154 {
155  data_size = sizeof(SetIntensityMessage_data_t);
156  data_ptr = malloc(data_size);
157  memset(data_ptr, 0, data_size);
158  data = (SetIntensityMessage_data_t *)data_ptr;
160  data->time_sec = ini_time_sec;
161  data->intensity = ini_intensity;
162  add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
163  add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
164 }
165 /** Constructor */
167 {
168  data_size = sizeof(SetIntensityMessage_data_t);
169  data_ptr = malloc(data_size);
170  memset(data_ptr, 0, data_size);
171  data = (SetIntensityMessage_data_t *)data_ptr;
173  add_fieldinfo(IFT_FLOAT, "time_sec", 1, &data->time_sec);
174  add_fieldinfo(IFT_FLOAT, "intensity", 1, &data->intensity);
175 }
176 
177 /** Destructor */
179 {
180  free(data_ptr);
181 }
182 
183 /** Copy constructor.
184  * @param m message to copy from
185  */
187 {
188  data_size = m->data_size;
189  data_ptr = malloc(data_size);
190  memcpy(data_ptr, m->data_ptr, data_size);
191  data = (SetIntensityMessage_data_t *)data_ptr;
193 }
194 
195 /* Methods */
196 /** Get time_sec value.
197  *
198  Time in seconds when to reach the intensity.
199 
200  * @return time_sec value
201  */
202 float
204 {
205  return data->time_sec;
206 }
207 
208 /** Get maximum length of time_sec value.
209  * @return length of time_sec value, can be length of the array or number of
210  * maximum number of characters for a string
211  */
212 size_t
214 {
215  return 1;
216 }
217 
218 /** Set time_sec value.
219  *
220  Time in seconds when to reach the intensity.
221 
222  * @param new_time_sec new time_sec value
223  */
224 void
225 LedInterface::SetIntensityMessage::set_time_sec(const float new_time_sec)
226 {
227  data->time_sec = new_time_sec;
228 }
229 
230 /** Get intensity value.
231  * Intensity value.
232  * @return intensity value
233  */
234 float
236 {
237  return data->intensity;
238 }
239 
240 /** Get maximum length of intensity value.
241  * @return length of intensity value, can be length of the array or number of
242  * maximum number of characters for a string
243  */
244 size_t
246 {
247  return 1;
248 }
249 
250 /** Set intensity value.
251  * Intensity value.
252  * @param new_intensity new intensity value
253  */
254 void
255 LedInterface::SetIntensityMessage::set_intensity(const float new_intensity)
256 {
257  data->intensity = new_intensity;
258 }
259 
260 /** Clone this message.
261  * Produces a message of the same type as this message and copies the
262  * data to the new message.
263  * @return clone of this message
264  */
265 Message *
267 {
268  return new LedInterface::SetIntensityMessage(this);
269 }
270 /** @class LedInterface::TurnOnMessage <interfaces/LedInterface.h>
271  * TurnOnMessage Fawkes BlackBoard Interface Message.
272  *
273 
274  */
275 
276 
277 /** Constructor */
279 {
280  data_size = sizeof(TurnOnMessage_data_t);
281  data_ptr = malloc(data_size);
282  memset(data_ptr, 0, data_size);
283  data = (TurnOnMessage_data_t *)data_ptr;
285 }
286 
287 /** Destructor */
289 {
290  free(data_ptr);
291 }
292 
293 /** Copy constructor.
294  * @param m message to copy from
295  */
297 {
298  data_size = m->data_size;
299  data_ptr = malloc(data_size);
300  memcpy(data_ptr, m->data_ptr, data_size);
301  data = (TurnOnMessage_data_t *)data_ptr;
303 }
304 
305 /* Methods */
306 /** Clone this message.
307  * Produces a message of the same type as this message and copies the
308  * data to the new message.
309  * @return clone of this message
310  */
311 Message *
313 {
314  return new LedInterface::TurnOnMessage(this);
315 }
316 /** @class LedInterface::TurnOffMessage <interfaces/LedInterface.h>
317  * TurnOffMessage Fawkes BlackBoard Interface Message.
318  *
319 
320  */
321 
322 
323 /** Constructor */
325 {
326  data_size = sizeof(TurnOffMessage_data_t);
327  data_ptr = malloc(data_size);
328  memset(data_ptr, 0, data_size);
329  data = (TurnOffMessage_data_t *)data_ptr;
331 }
332 
333 /** Destructor */
335 {
336  free(data_ptr);
337 }
338 
339 /** Copy constructor.
340  * @param m message to copy from
341  */
343 {
344  data_size = m->data_size;
345  data_ptr = malloc(data_size);
346  memcpy(data_ptr, m->data_ptr, data_size);
347  data = (TurnOffMessage_data_t *)data_ptr;
349 }
350 
351 /* Methods */
352 /** Clone this message.
353  * Produces a message of the same type as this message and copies the
354  * data to the new message.
355  * @return clone of this message
356  */
357 Message *
359 {
360  return new LedInterface::TurnOffMessage(this);
361 }
362 /** Check if message is valid and can be enqueued.
363  * @param message Message to check
364  * @return true if the message is valid, false otherwise.
365  */
366 bool
367 LedInterface::message_valid(const Message *message) const
368 {
369  const SetIntensityMessage *m0 = dynamic_cast<const SetIntensityMessage *>(message);
370  if ( m0 != NULL ) {
371  return true;
372  }
373  const TurnOnMessage *m1 = dynamic_cast<const TurnOnMessage *>(message);
374  if ( m1 != NULL ) {
375  return true;
376  }
377  const TurnOffMessage *m2 = dynamic_cast<const TurnOffMessage *>(message);
378  if ( m2 != NULL ) {
379  return true;
380  }
381  return false;
382 }
383 
384 /// @cond INTERNALS
385 EXPORT_INTERFACE(LedInterface)
386 /// @endcond
387 
388 
389 } // end namespace fawkes
fawkes::LedInterface::copy_values
virtual void copy_values(const Interface *other)
Copy values from other interface.
Definition: LedInterface.cpp:131
fawkes::Interface::data_ptr
void * data_ptr
Definition: interface.h:224
fawkes::LedInterface::SetIntensityMessage::set_time_sec
void set_time_sec(const float new_time_sec)
Set time_sec value.
Definition: LedInterface.cpp:231
fawkes::LedInterface::OFF
static const float OFF
OFF constant.
Definition: LedInterface.h:53
fawkes::LedInterface::TurnOffMessage::clone
virtual Message * clone() const
Clone this message.
Definition: LedInterface.cpp:364
fawkes::LedInterface::SetIntensityMessage::clone
virtual Message * clone() const
Clone this message.
Definition: LedInterface.cpp:272
fawkes::LedInterface::maxlenof_intensity
size_t maxlenof_intensity() const
Get maximum length of intensity value.
Definition: LedInterface.cpp:94
fawkes::Message
Definition: message.h:41
fawkes::LedInterface::enum_tostring
virtual const char * enum_tostring(const char *enumtype, int val) const
Definition: LedInterface.cpp:142
fawkes::Message::data_ptr
void * data_ptr
Definition: message.h:125
fawkes::IFT_FLOAT
@ IFT_FLOAT
float field
Definition: types.h:60
fawkes::Message::data_ts
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:135
fawkes::LedInterface::TurnOffMessage::TurnOffMessage
TurnOffMessage()
Constructor.
Definition: LedInterface.cpp:330
fawkes::LedInterface::TurnOffMessage::~TurnOffMessage
~TurnOffMessage()
Destructor.
Definition: LedInterface.cpp:340
fawkes::LedInterface::TurnOnMessage::TurnOnMessage
TurnOnMessage()
Constructor.
Definition: LedInterface.cpp:284
fawkes::Interface::type
const char * type() const
Get type of interface.
Definition: interface.cpp:645
fawkes::Interface::add_fieldinfo
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 field info list.
Definition: interface.cpp:341
fawkes::LedInterface::SetIntensityMessage
Definition: LedInterface.h:67
fawkes::Interface::data_ts
interface_data_ts_t * data_ts
Definition: interface.h:228
fawkes::Message::message_data_ts_t
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:130
fawkes::LedInterface::create_message
virtual Message * create_message(const char *type) const
Definition: LedInterface.cpp:112
fawkes::Interface::data_changed
bool data_changed
Definition: interface.h:226
fawkes::LedInterface::SetIntensityMessage::maxlenof_time_sec
size_t maxlenof_time_sec() const
Get maximum length of time_sec value.
Definition: LedInterface.cpp:219
fawkes
fawkes::Interface::set_hash
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:321
fawkes::LedInterface::SetIntensityMessage::time_sec
float time_sec() const
Get time_sec value.
Definition: LedInterface.cpp:209
fawkes::Message::data_size
unsigned int data_size
Definition: message.h:126
fawkes::LedInterface::TurnOffMessage
Definition: LedInterface.h:118
fawkes::LedInterface::SetIntensityMessage::SetIntensityMessage
SetIntensityMessage()
Constructor.
Definition: LedInterface.cpp:172
fawkes::LedInterface::SetIntensityMessage::set_intensity
void set_intensity(const float new_intensity)
Set intensity value.
Definition: LedInterface.cpp:261
fawkes::Message::add_fieldinfo
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:406
fawkes::Interface::data_size
unsigned int data_size
Definition: interface.h:225
fawkes::LedInterface::SetIntensityMessage::~SetIntensityMessage
~SetIntensityMessage()
Destructor.
Definition: LedInterface.cpp:184
fawkes::LedInterface::SetIntensityMessage::intensity
float intensity() const
Get intensity value.
Definition: LedInterface.cpp:241
fawkes::LedInterface::message_valid
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Definition: LedInterface.cpp:373
fawkes::LedInterface::TurnOnMessage::~TurnOnMessage
~TurnOnMessage()
Destructor.
Definition: LedInterface.cpp:294
fawkes::LedInterface::SetIntensityMessage::maxlenof_intensity
size_t maxlenof_intensity() const
Get maximum length of intensity value.
Definition: LedInterface.cpp:251
fawkes::LedInterface::TurnOnMessage::clone
virtual Message * clone() const
Clone this message.
Definition: LedInterface.cpp:318
fawkes::Interface::add_messageinfo
void add_messageinfo(const char *name)
Add an entry to the message info list.
Definition: interface.cpp:380
fawkes::LedInterface::set_intensity
void set_intensity(const float new_intensity)
Set intensity value.
Definition: LedInterface.cpp:104
fawkes::LedInterface::ON
static const float ON
ON constant.
Definition: LedInterface.h:52
fawkes::LedInterface::intensity
float intensity() const
Get intensity value.
Definition: LedInterface.cpp:84
fawkes::LedInterface::TurnOnMessage
Definition: LedInterface.h:98