BatteryInterface.cpp

00001 
00002 /***************************************************************************
00003  *  BatteryInterface.cpp - Fawkes BlackBoard Interface - BatteryInterface
00004  *
00005  *  Templated created:   Thu Oct 12 10:49:19 2006
00006  *  Copyright  2008  Daniel Beck
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <interfaces/BatteryInterface.h>
00025 
00026 #include <core/exceptions/software.h>
00027 
00028 #include <cstring>
00029 #include <cstdlib>
00030 
00031 namespace fawkes {
00032 
00033 /** @class BatteryInterface <interfaces/BatteryInterface.h>
00034  * BatteryInterface Fawkes BlackBoard Interface.
00035  * This interface contains status information about the
00036     battery. In addition to this it allows to send messages which
00037     turn the battery on/off
00038  * @ingroup FawkesInterfaces
00039  */
00040 
00041 
00042 
00043 /** Constructor */
00044 BatteryInterface::BatteryInterface() : Interface()
00045 {
00046   data_size = sizeof(BatteryInterface_data_t);
00047   data_ptr  = malloc(data_size);
00048   data      = (BatteryInterface_data_t *)data_ptr;
00049   data_ts   = (interface_data_ts_t *)data_ptr;
00050   memset(data_ptr, 0, data_size);
00051   add_fieldinfo(IFT_UINT32, "current", 1, &data->current);
00052   add_fieldinfo(IFT_UINT32, "voltage", 1, &data->voltage);
00053   add_fieldinfo(IFT_UINT32, "temperature", 1, &data->temperature);
00054   add_fieldinfo(IFT_FLOAT, "absolute_soc", 1, &data->absolute_soc);
00055   add_fieldinfo(IFT_FLOAT, "relative_soc", 1, &data->relative_soc);
00056   add_messageinfo("PushButtonMessage");
00057   add_messageinfo("SleepMessage");
00058   unsigned char tmp_hash[] = {0x28, 0xb6, 0xbe, 0xe7, 0xf1, 0x47, 0x2, 0x12, 0x1d, 0xe3, 0x7c, 0x14, 0xe9, 0x1f, 0x24, 0x4d};
00059   set_hash(tmp_hash);
00060 }
00061 
00062 /** Destructor */
00063 BatteryInterface::~BatteryInterface()
00064 {
00065   free(data_ptr);
00066 }
00067 /* Methods */
00068 /** Get current value.
00069  * Battery Current [mA]
00070  * @return current value
00071  */
00072 uint32_t
00073 BatteryInterface::current() const
00074 {
00075   return data->current;
00076 }
00077 
00078 /** Get maximum length of current value.
00079  * @return length of current value, can be length of the array or number of 
00080  * maximum number of characters for a string
00081  */
00082 size_t
00083 BatteryInterface::maxlenof_current() const
00084 {
00085   return 1;
00086 }
00087 
00088 /** Set current value.
00089  * Battery Current [mA]
00090  * @param new_current new current value
00091  */
00092 void
00093 BatteryInterface::set_current(const uint32_t new_current)
00094 {
00095   data->current = new_current;
00096   data_changed = true;
00097 }
00098 
00099 /** Get voltage value.
00100  * Battery Voltage [mV]
00101  * @return voltage value
00102  */
00103 uint32_t
00104 BatteryInterface::voltage() const
00105 {
00106   return data->voltage;
00107 }
00108 
00109 /** Get maximum length of voltage value.
00110  * @return length of voltage value, can be length of the array or number of 
00111  * maximum number of characters for a string
00112  */
00113 size_t
00114 BatteryInterface::maxlenof_voltage() const
00115 {
00116   return 1;
00117 }
00118 
00119 /** Set voltage value.
00120  * Battery Voltage [mV]
00121  * @param new_voltage new voltage value
00122  */
00123 void
00124 BatteryInterface::set_voltage(const uint32_t new_voltage)
00125 {
00126   data->voltage = new_voltage;
00127   data_changed = true;
00128 }
00129 
00130 /** Get temperature value.
00131  * Battery Temperature [°C]
00132  * @return temperature value
00133  */
00134 uint32_t
00135 BatteryInterface::temperature() const
00136 {
00137   return data->temperature;
00138 }
00139 
00140 /** Get maximum length of temperature value.
00141  * @return length of temperature value, can be length of the array or number of 
00142  * maximum number of characters for a string
00143  */
00144 size_t
00145 BatteryInterface::maxlenof_temperature() const
00146 {
00147   return 1;
00148 }
00149 
00150 /** Set temperature value.
00151  * Battery Temperature [°C]
00152  * @param new_temperature new temperature value
00153  */
00154 void
00155 BatteryInterface::set_temperature(const uint32_t new_temperature)
00156 {
00157   data->temperature = new_temperature;
00158   data_changed = true;
00159 }
00160 
00161 /** Get absolute_soc value.
00162  * Absolute state of charge [%]
00163  * @return absolute_soc value
00164  */
00165 float
00166 BatteryInterface::absolute_soc() const
00167 {
00168   return data->absolute_soc;
00169 }
00170 
00171 /** Get maximum length of absolute_soc value.
00172  * @return length of absolute_soc value, can be length of the array or number of 
00173  * maximum number of characters for a string
00174  */
00175 size_t
00176 BatteryInterface::maxlenof_absolute_soc() const
00177 {
00178   return 1;
00179 }
00180 
00181 /** Set absolute_soc value.
00182  * Absolute state of charge [%]
00183  * @param new_absolute_soc new absolute_soc value
00184  */
00185 void
00186 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
00187 {
00188   data->absolute_soc = new_absolute_soc;
00189   data_changed = true;
00190 }
00191 
00192 /** Get relative_soc value.
00193  * Relative state of charge [%]
00194  * @return relative_soc value
00195  */
00196 float
00197 BatteryInterface::relative_soc() const
00198 {
00199   return data->relative_soc;
00200 }
00201 
00202 /** Get maximum length of relative_soc value.
00203  * @return length of relative_soc value, can be length of the array or number of 
00204  * maximum number of characters for a string
00205  */
00206 size_t
00207 BatteryInterface::maxlenof_relative_soc() const
00208 {
00209   return 1;
00210 }
00211 
00212 /** Set relative_soc value.
00213  * Relative state of charge [%]
00214  * @param new_relative_soc new relative_soc value
00215  */
00216 void
00217 BatteryInterface::set_relative_soc(const float new_relative_soc)
00218 {
00219   data->relative_soc = new_relative_soc;
00220   data_changed = true;
00221 }
00222 
00223 /* =========== message create =========== */
00224 Message *
00225 BatteryInterface::create_message(const char *type) const
00226 {
00227   if ( strncmp("PushButtonMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00228     return new PushButtonMessage();
00229   } else if ( strncmp("SleepMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00230     return new SleepMessage();
00231   } else {
00232     throw UnknownTypeException("The given type '%s' does not match any known "
00233                                "message type for this interface type.", type);
00234   }
00235 }
00236 
00237 
00238 /** Copy values from other interface.
00239  * @param other other interface to copy values from
00240  */
00241 void
00242 BatteryInterface::copy_values(const Interface *other)
00243 {
00244   const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
00245   if (oi == NULL) {
00246     throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00247                                 type(), other->type());
00248   }
00249   memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
00250 }
00251 
00252 const char *
00253 BatteryInterface::enum_tostring(const char *enumtype, int val) const
00254 {
00255   throw UnknownTypeException("Unknown enum type %s", enumtype);
00256 }
00257 
00258 /* =========== messages =========== */
00259 /** @class BatteryInterface::PushButtonMessage <interfaces/BatteryInterface.h>
00260  * PushButtonMessage Fawkes BlackBoard Interface Message.
00261  * 
00262     
00263  */
00264 
00265 
00266 /** Constructor */
00267 BatteryInterface::PushButtonMessage::PushButtonMessage() : Message("PushButtonMessage")
00268 {
00269   data_size = sizeof(PushButtonMessage_data_t);
00270   data_ptr  = malloc(data_size);
00271   memset(data_ptr, 0, data_size);
00272   data      = (PushButtonMessage_data_t *)data_ptr;
00273   data_ts   = (message_data_ts_t *)data_ptr;
00274 }
00275 
00276 /** Destructor */
00277 BatteryInterface::PushButtonMessage::~PushButtonMessage()
00278 {
00279   free(data_ptr);
00280 }
00281 
00282 /** Copy constructor.
00283  * @param m message to copy from
00284  */
00285 BatteryInterface::PushButtonMessage::PushButtonMessage(const PushButtonMessage *m) : Message("PushButtonMessage")
00286 {
00287   data_size = m->data_size;
00288   data_ptr  = malloc(data_size);
00289   memcpy(data_ptr, m->data_ptr, data_size);
00290   data      = (PushButtonMessage_data_t *)data_ptr;
00291   data_ts   = (message_data_ts_t *)data_ptr;
00292 }
00293 
00294 /* Methods */
00295 /** Clone this message.
00296  * Produces a message of the same type as this message and copies the
00297  * data to the new message.
00298  * @return clone of this message
00299  */
00300 Message *
00301 BatteryInterface::PushButtonMessage::clone() const
00302 {
00303   return new BatteryInterface::PushButtonMessage(this);
00304 }
00305 /** @class BatteryInterface::SleepMessage <interfaces/BatteryInterface.h>
00306  * SleepMessage Fawkes BlackBoard Interface Message.
00307  * 
00308     
00309  */
00310 
00311 
00312 /** Constructor */
00313 BatteryInterface::SleepMessage::SleepMessage() : Message("SleepMessage")
00314 {
00315   data_size = sizeof(SleepMessage_data_t);
00316   data_ptr  = malloc(data_size);
00317   memset(data_ptr, 0, data_size);
00318   data      = (SleepMessage_data_t *)data_ptr;
00319   data_ts   = (message_data_ts_t *)data_ptr;
00320 }
00321 
00322 /** Destructor */
00323 BatteryInterface::SleepMessage::~SleepMessage()
00324 {
00325   free(data_ptr);
00326 }
00327 
00328 /** Copy constructor.
00329  * @param m message to copy from
00330  */
00331 BatteryInterface::SleepMessage::SleepMessage(const SleepMessage *m) : Message("SleepMessage")
00332 {
00333   data_size = m->data_size;
00334   data_ptr  = malloc(data_size);
00335   memcpy(data_ptr, m->data_ptr, data_size);
00336   data      = (SleepMessage_data_t *)data_ptr;
00337   data_ts   = (message_data_ts_t *)data_ptr;
00338 }
00339 
00340 /* Methods */
00341 /** Clone this message.
00342  * Produces a message of the same type as this message and copies the
00343  * data to the new message.
00344  * @return clone of this message
00345  */
00346 Message *
00347 BatteryInterface::SleepMessage::clone() const
00348 {
00349   return new BatteryInterface::SleepMessage(this);
00350 }
00351 /** Check if message is valid and can be enqueued.
00352  * @param message Message to check
00353  * @return true if the message is valid, false otherwise.
00354  */
00355 bool
00356 BatteryInterface::message_valid(const Message *message) const
00357 {
00358   const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
00359   if ( m0 != NULL ) {
00360     return true;
00361   }
00362   const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
00363   if ( m1 != NULL ) {
00364     return true;
00365   }
00366   return false;
00367 }
00368 
00369 /// @cond INTERNALS
00370 EXPORT_INTERFACE(BatteryInterface)
00371 /// @endcond
00372 
00373 
00374 } // end namespace fawkes