BatteryInterface.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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 memset(data_ptr, 0, data_size);
00050 add_fieldinfo(IFT_UINT, "current", 1, &data->current);
00051 add_fieldinfo(IFT_UINT, "voltage", 1, &data->voltage);
00052 add_fieldinfo(IFT_UINT, "temperature", 1, &data->temperature);
00053 add_fieldinfo(IFT_FLOAT, "absolute_soc", 1, &data->absolute_soc);
00054 add_fieldinfo(IFT_FLOAT, "relative_soc", 1, &data->relative_soc);
00055 add_messageinfo("PushButtonMessage");
00056 add_messageinfo("SleepMessage");
00057 unsigned char tmp_hash[] = {0xaf, 0x87, 0xbb, 0x32, 0x19, 0x6b, 0x9, 0x3d, 0x7a, 0x6c, 0xf0, 0x4a, 0xb0, 0xd8, 0xa, 0x1d};
00058 set_hash(tmp_hash);
00059 }
00060
00061
00062 BatteryInterface::~BatteryInterface()
00063 {
00064 free(data_ptr);
00065 }
00066
00067
00068
00069
00070
00071 unsigned int
00072 BatteryInterface::current() const
00073 {
00074 return data->current;
00075 }
00076
00077
00078
00079
00080
00081 size_t
00082 BatteryInterface::maxlenof_current() const
00083 {
00084 return 1;
00085 }
00086
00087
00088
00089
00090
00091 void
00092 BatteryInterface::set_current(const unsigned int new_current)
00093 {
00094 data->current = new_current;
00095 }
00096
00097
00098
00099
00100
00101 unsigned int
00102 BatteryInterface::voltage() const
00103 {
00104 return data->voltage;
00105 }
00106
00107
00108
00109
00110
00111 size_t
00112 BatteryInterface::maxlenof_voltage() const
00113 {
00114 return 1;
00115 }
00116
00117
00118
00119
00120
00121 void
00122 BatteryInterface::set_voltage(const unsigned int new_voltage)
00123 {
00124 data->voltage = new_voltage;
00125 }
00126
00127
00128
00129
00130
00131 unsigned int
00132 BatteryInterface::temperature() const
00133 {
00134 return data->temperature;
00135 }
00136
00137
00138
00139
00140
00141 size_t
00142 BatteryInterface::maxlenof_temperature() const
00143 {
00144 return 1;
00145 }
00146
00147
00148
00149
00150
00151 void
00152 BatteryInterface::set_temperature(const unsigned int new_temperature)
00153 {
00154 data->temperature = new_temperature;
00155 }
00156
00157
00158
00159
00160
00161 float
00162 BatteryInterface::absolute_soc() const
00163 {
00164 return data->absolute_soc;
00165 }
00166
00167
00168
00169
00170
00171 size_t
00172 BatteryInterface::maxlenof_absolute_soc() const
00173 {
00174 return 1;
00175 }
00176
00177
00178
00179
00180
00181 void
00182 BatteryInterface::set_absolute_soc(const float new_absolute_soc)
00183 {
00184 data->absolute_soc = new_absolute_soc;
00185 }
00186
00187
00188
00189
00190
00191 float
00192 BatteryInterface::relative_soc() const
00193 {
00194 return data->relative_soc;
00195 }
00196
00197
00198
00199
00200
00201 size_t
00202 BatteryInterface::maxlenof_relative_soc() const
00203 {
00204 return 1;
00205 }
00206
00207
00208
00209
00210
00211 void
00212 BatteryInterface::set_relative_soc(const float new_relative_soc)
00213 {
00214 data->relative_soc = new_relative_soc;
00215 }
00216
00217
00218 Message *
00219 BatteryInterface::create_message(const char *type) const
00220 {
00221 if ( strncmp("PushButtonMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00222 return new PushButtonMessage();
00223 } else if ( strncmp("SleepMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00224 return new SleepMessage();
00225 } else {
00226 throw UnknownTypeException("The given type '%s' does not match any known "
00227 "message type for this interface type.", type);
00228 }
00229 }
00230
00231
00232
00233
00234
00235 void
00236 BatteryInterface::copy_values(const Interface *other)
00237 {
00238 const BatteryInterface *oi = dynamic_cast<const BatteryInterface *>(other);
00239 if (oi == NULL) {
00240 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00241 type(), other->type());
00242 }
00243 memcpy(data, oi->data, sizeof(BatteryInterface_data_t));
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 BatteryInterface::PushButtonMessage::PushButtonMessage() : Message("PushButtonMessage")
00256 {
00257 data_size = 0;
00258 data_ptr = NULL;
00259 }
00260
00261
00262 BatteryInterface::PushButtonMessage::~PushButtonMessage()
00263 {
00264 }
00265
00266
00267
00268
00269 BatteryInterface::PushButtonMessage::PushButtonMessage(const PushButtonMessage *m) : Message("PushButtonMessage")
00270 {
00271 data_size = 0;
00272 data_ptr = NULL;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 Message *
00282 BatteryInterface::PushButtonMessage::clone() const
00283 {
00284 return new BatteryInterface::PushButtonMessage(this);
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 BatteryInterface::SleepMessage::SleepMessage() : Message("SleepMessage")
00295 {
00296 data_size = 0;
00297 data_ptr = NULL;
00298 }
00299
00300
00301 BatteryInterface::SleepMessage::~SleepMessage()
00302 {
00303 }
00304
00305
00306
00307
00308 BatteryInterface::SleepMessage::SleepMessage(const SleepMessage *m) : Message("SleepMessage")
00309 {
00310 data_size = 0;
00311 data_ptr = NULL;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 Message *
00321 BatteryInterface::SleepMessage::clone() const
00322 {
00323 return new BatteryInterface::SleepMessage(this);
00324 }
00325
00326
00327
00328 bool
00329 BatteryInterface::message_valid(const Message *message) const
00330 {
00331 const PushButtonMessage *m0 = dynamic_cast<const PushButtonMessage *>(message);
00332 if ( m0 != NULL ) {
00333 return true;
00334 }
00335 const SleepMessage *m1 = dynamic_cast<const SleepMessage *>(message);
00336 if ( m1 != NULL ) {
00337 return true;
00338 }
00339 return false;
00340 }
00341
00342
00343 EXPORT_INTERFACE(BatteryInterface)
00344
00345
00346
00347 }