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