00001 /*! \file WaspUtils.h 00002 \brief Library containing useful general functions 00003 00004 Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. 00005 http://www.libelium.com 00006 00007 This program is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation, either version 2.1 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 00020 Version: 0.12 00021 00022 Design: David Gascón 00023 00024 Implementation: Alberto Bielsa, David Cuartielles 00025 00026 */ 00027 00028 00029 /*! \def Wasputils_h 00030 \brief The library flag 00031 00032 */ 00033 #ifndef Wasputils_h 00034 #define Wasputils_h 00035 00036 /****************************************************************************** 00037 * Includes 00038 ******************************************************************************/ 00039 00040 #include <inttypes.h> 00041 #include <avr/eeprom.h> 00042 00043 /****************************************************************************** 00044 * Definitions & Declarations 00045 ******************************************************************************/ 00046 00047 /*! \def GPS_BUFFER_SIZE 00048 \brief internal 'inBuffer' size, needs to be at least 148 bytes for the ephemeris 00049 */ 00050 #define GPS_BUFFER_SIZE 160 00051 00052 /*! \def MAX_ARGS 00053 \brief max amount of arguments in Wasp 00054 */ 00055 #define MAX_ARGS 20 00056 00057 /*! \def MAX_ARG_LENGTH 00058 \brief max length for Wasp arguments 00059 */ 00060 #define MAX_ARG_LENGTH 16 00061 00062 /*! \def LED_ON 00063 \brief sets LED ON 00064 */ 00065 #define LED_ON 1 00066 00067 /*! \def LED_OFF 00068 \brief sets LED OFF 00069 */ 00070 #define LED_OFF 0 00071 00072 /*! \def MUX_TO_HIGH 00073 \brief sets mux high 00074 */ 00075 #define MUX_TO_HIGH 1 00076 00077 /*! \def MUX_TO_LOW 00078 \brief sets mux low 00079 */ 00080 #define MUX_TO_LOW 0 00081 00082 00083 /****************************************************************************** 00084 * Class 00085 ******************************************************************************/ 00086 00087 //! WaspUtils Class 00088 /*! 00089 WaspUtils Class defines all the variables and functions used to set LEDs, multiplexor and useful general functions 00090 */ 00091 class WaspUtils 00092 { 00093 private: 00094 00095 public: 00096 //! Variable : Free available memory 00097 /*! 00098 It stores the last free available memory size returned by 'getFreeMemory' function 00099 */ 00100 uint16_t freeMemory; 00101 00102 //! Variable : buffer to store incoming data 00103 /*! 00104 */ 00105 char inBuffer[GPS_BUFFER_SIZE]; 00106 00107 //! Variable : bidimensional array of arguments in Waspmote, mainly used with the GPS 00108 /*! 00109 */ 00110 char arguments[MAX_ARGS][MAX_ARG_LENGTH]; 00111 00112 //! class constructor 00113 /*! 00114 It does nothing 00115 \param void 00116 \return void 00117 */ 00118 WaspUtils(void); 00119 00120 //! It gets the free available memory 00121 /*! 00122 \param void 00123 \return free available memory expressed in Bytes 00124 */ 00125 uint8_t getFreeMemory(void); 00126 00127 //! It sets the specified LED to the specified state(ON or OFF) 00128 /*! 00129 \param uint8_t led : the LED to set ON/OFF 00130 \param uint8_t state : the state to set the LED 00131 \return void 00132 \sa getLED(uint8_t led), blinkLEDs(uint16_t time) 00133 */ 00134 void setLED(uint8_t led, uint8_t state); 00135 00136 //! It gets the state of the specified LED 00137 /*! 00138 \param uint8_t led : the LED to get the state 00139 \return the state of the LED 00140 \sa setLED(uint8_t led, uint8_t state), blinkLEDs(uint16_t time) 00141 */ 00142 uint8_t getLED(uint8_t led); 00143 00144 //! It blinks LEDs, with the specified time for blinking 00145 /*! 00146 \param uint16_t time : time for blinking 00147 \return void 00148 \sa setLED(uint8_t led, uint8_t state), getLED(uint8_t led) 00149 */ 00150 void blinkLEDs(uint16_t time); 00151 00152 //! It maps 'x' from the read range to the specified range 00153 /*! 00154 \param long x : value to map 00155 \param long in_min : minimum input value for 'x' 00156 \param long in_max : maximum input value for 'x' 00157 \param long out_min : minimum output value for 'x' 00158 \param long out_max : maximum output value for 'x' 00159 \return the value 'x' mapped to the [out_min,out_max] range 00160 */ 00161 long map(long x, long in_min, long in_max, long out_min, long out_max); 00162 00163 //! It sets MUX to the desired combination 00164 /*! 00165 It sets MUX to the desired combination. Possible combinations are: 00166 00167 MUX_LOW = 0 & MUX_HIGH = 1 ---> GPS MODULE 00168 MUX_LOW = 1 & MUX_HIGH = 1 ---> GPRS MODULE 00169 MUX_LOW = 1 & MUX_HIGH = 0 ---> AUX1 MODULE 00170 MUX_LOW = 0 & MUX_HIGH = 0 ---> AUX2 MODULE 00171 00172 \param uint8_t MUX_LOW : low combination part 00173 \param uint8_t MUX_HIGH : high combination part 00174 \return void 00175 \sa setMuxGPS(), setMuxGPRS(), setMuxAux1(), setMuxAux2() 00176 */ 00177 void setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH); 00178 00179 //! It sets MUX to the desired combination (0,1) to enable GPS module 00180 /*! 00181 Possible combinatios are: 00182 00183 MUX_LOW = 0 & MUX_HIGH = 1 ---> GPS MODULE 00184 MUX_LOW = 1 & MUX_HIGH = 1 ---> GPRS MODULE 00185 MUX_LOW = 1 & MUX_HIGH = 0 ---> AUX1 MODULE 00186 MUX_LOW = 0 & MUX_HIGH = 0 ---> AUX2 MODULE 00187 00188 \return void 00189 \sa setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH), setMuxGPRS(), setMuxAux1(), setMuxAux2() 00190 */ 00191 void setMuxGPS(); 00192 00193 //! It sets MUX to the desired combination (1,1) to enable GPRS module 00194 /*! 00195 Possible combinatios are: 00196 00197 MUX_LOW = 0 & MUX_HIGH = 1 ---> GPS MODULE 00198 MUX_LOW = 1 & MUX_HIGH = 1 ---> GPRS MODULE 00199 MUX_LOW = 1 & MUX_HIGH = 0 ---> AUX1 MODULE 00200 MUX_LOW = 0 & MUX_HIGH = 0 ---> AUX2 MODULE 00201 00202 \return void 00203 \sa setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH), setMuxGPS(), setMuxAux1(), setMuxAux2() 00204 */ 00205 void setMuxGPRS(); 00206 00207 //! It sets MUX to the desired combination (1,0) to enable AUX1 module 00208 /*! 00209 Possible combinatios are: 00210 00211 MUX_LOW = 0 & MUX_HIGH = 1 ---> GPS MODULE 00212 MUX_LOW = 1 & MUX_HIGH = 1 ---> GPRS MODULE 00213 MUX_LOW = 1 & MUX_HIGH = 0 ---> AUX1 MODULE 00214 MUX_LOW = 0 & MUX_HIGH = 0 ---> AUX2 MODULE 00215 00216 \return void 00217 \sa setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH), setMuxGPS(), setMuxGPRS(), setMuxAux2() 00218 */ 00219 void setMuxAux1(); 00220 00221 //! It sets MUX to the desired combination (0,0) to enable AUX2 module 00222 /*! 00223 Possible combinatios are: 00224 00225 MUX_LOW = 0 & MUX_HIGH = 1 ---> GPS MODULE 00226 MUX_LOW = 1 & MUX_HIGH = 1 ---> GPRS MODULE 00227 MUX_LOW = 1 & MUX_HIGH = 0 ---> AUX1 MODULE 00228 MUX_LOW = 0 & MUX_HIGH = 0 ---> AUX2 MODULE 00229 00230 \return void 00231 \sa setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH), setMuxGPS(), setMuxGPRS(), setMuxAux1() 00232 */ 00233 void setMuxAux2(); 00234 00235 //! It reads a value from the specified EEPROM address 00236 /*! 00237 \param int address : EEPROM address to read from 00238 \return the value read from EEPROM address 00239 \sa writeEEPROM(int address, uint8_t value) 00240 */ 00241 uint8_t readEEPROM(int address); 00242 00243 //! It writes the specified value to the specified EEPROM address 00244 /*! 00245 \param int address : EEPROM address to write to 00246 \param uint8_t value: value to write to the EEPROM 00247 \return void 00248 \sa readEEPROM(int address) 00249 */ 00250 void writeEEPROM(int address, uint8_t value); 00251 00252 //! It gets a number out of a string. It gets till the 2nd decimal of the number. 00253 /*! 00254 \param char* str: string containing the number to extract 00255 \return the number extracted from the string 00256 \sa parse_degrees(char* str), gpsatol(char* str), gpsisdigit(char c), parse_latitude(char* str) 00257 */ 00258 long parse_decimal(char *str); 00259 00260 //! It gets the degree number out of a string 00261 /*! 00262 \param char* str: string containing the number to extract 00263 \return the number extracted from the string 00264 \sa parse_decimal(char* str), gpsatol(char* str), gpsisdigit(char c), parse_latitude(char* str) 00265 */ 00266 double parse_degrees(char *str); 00267 00268 //! It gets the integer part of a number out of a string 00269 /*! 00270 \param char* str: string containing the number to extract 00271 \return the number extracted from the string 00272 \sa parse_decimal(char* str), parse_degrees(char* str), gpsisdigit(char c), parse_latitude(char* str) 00273 */ 00274 long gpsatol(char *str); 00275 00276 //! It checks if the char is a digit or not 00277 /*! 00278 \param char c: character to determine if it is a digit or not 00279 \return TRUE if is a digit, FALSE if not 00280 \sa parse_decimal(char* str), parse_degrees(char* str), gpsatol(char* str), parse_latitude(char* str) 00281 */ 00282 bool gpsisdigit(char c) { return c >= '0' && c <= '9'; }; 00283 00284 //! It parses latitude or longitude, getting the number out of a string 00285 /*! 00286 \param char* str: string containing the latitude or longitude to parse 00287 \return the latitude or longitude extracted from the string 00288 \sa parse_decimal(char* str), parse_degrees(char* str), gpsatol(char* str), gpsisdigit(char c) 00289 */ 00290 double parse_latitude(char *str); 00291 00292 //! It converts a decimal number into an hexadecimal number 00293 /*! 00294 \param uint8_t number: number to convert 00295 \return the number converted to hexadecimal 00296 \sa array2long(char* num), long2array(long num, char* numb), str2hex(char* str), str2hex(uint8_t* str) 00297 */ 00298 uint8_t dec2hex(uint8_t number); 00299 00300 //! It converts a number stored in an array into a decimal number 00301 /*! 00302 \param char* num : string that contains the number to extract 00303 \return the number extracted 00304 \sa dec2hex(uint8_t num), long2array(long num, char* numb), str2hex(char* str), str2hex(uint8_t* str) 00305 */ 00306 long array2long(char* num); 00307 00308 //! It converts a decimal number into a string 00309 /*! 00310 \param long num : number to convert 00311 \param char* numb : string where store the converted number 00312 \return the number of digits of the number 00313 \sa array2long(char* num), dec2hex(uint8_t num), str2hex(char* str), str2hex(uint8_t* str) 00314 */ 00315 uint8_t long2array(long num, char* numb); 00316 00317 //! It converts a number stored in a string into a hexadecimal number 00318 /*! 00319 \param char* str : string where the number is stored 00320 \return the converted number 00321 \sa array2long(char* num), dec2hex(uint8_t num), long2array(long num, char* numb), str2hex(uint8_t* str) 00322 */ 00323 uint8_t str2hex(char* str); 00324 00325 //! It converts a number stored in a string into a hexadecimal number 00326 /*! 00327 \param char* str : string where thember is stored 00328 \return the converted number 00329 \sa array2long(char* num), dec2hex(uint8_t num), long2array(long num, char* numb), str2hex(char* str) 00330 */ 00331 uint8_t str2hex(uint8_t* str); 00332 00333 //! It converts a hexadecimal number stored in an array to a string (8 Byte numbers) 00334 /*! 00335 \param uint8_t* number : hexadecimal array to conver to a string 00336 \param const char* macDest : char array where the converted number is stored 00337 \return void 00338 \sa array2long(char* num), dec2hex(uint8_t num), long2array(long num, char* numb), str2hex(char* str), str2hex(uint8_t* str) 00339 */ 00340 void hex2str(uint8_t* number, char* macDest); 00341 00342 //! It converts a hexadecimal number stored in an array to a string (8 Byte numbers) 00343 /*! 00344 \param uint8_t* number : hexadecimal array to conver to a string 00345 \param const char* macDest : char array where the converted number is stored 00346 \param uint8_t length : length to copy 00347 \return void 00348 \sa array2long(char* num), dec2hex(uint8_t num), long2array(long num, char* numb), str2hex(char* str), str2hex(uint8_t* str) 00349 */ 00350 void hex2str(uint8_t* number, char* macDest, uint8_t length); 00351 00352 //! It converts a number stored in an array into a decimal number 00353 /*! 00354 \param const char* str : string that contains the number to extract 00355 \return the number extracted 00356 \sa sizeOf(const char* str), strCmp(const char* str1, const char* str2, size), strCp(char* str1, char* str2) 00357 */ 00358 uint32_t strtolong(const char* str); 00359 00360 //! It gets the size of a string or array 00361 /*! 00362 \param const char* str : string to get the size from 00363 \return the array or string size 00364 \sa strtolong(const char* str), strCmp(const char* str1, const char* str2, size), strCp(char* str1, char* str2) 00365 */ 00366 int sizeOf(const char* str); 00367 00368 //! It compares two strings 00369 /*! 00370 \param const char* str1 : string to compare 00371 \param const char* str2 : string to compare 00372 \param uint8_t size : string size 00373 \return '0' if strings are equal, '1' if don't 00374 \sa strtolong(const char* str), sizeOf(const char* str), strCp(char* str1, char* str2) 00375 */ 00376 uint8_t strCmp(const char* str1, const char* str2, uint8_t size); 00377 00378 //! It copies one string into another 00379 /*! 00380 - str1: origin 00381 - str2: target 00382 \param char* str1 : origin string 00383 \param char* str2 : target string 00384 \return void 00385 \sa strtolong(const char* str), sizeOf(const char* str), strCmp(const char* str1, const char* str2, uint8_t size) 00386 */ 00387 void strCp(char* str1, char* str2); 00388 00389 //! It clears the arguments[][] data matrix 00390 /*! 00391 \param void 00392 \return void 00393 \sa clearBuffer(),strExplode(const char* str, char separator) 00394 */ 00395 void clearArguments(void); 00396 00397 //! It clears the inBuffer array 00398 /*! 00399 \param void 00400 \return void 00401 \sa clearArguments(),strExplode(const char* str, char separator) 00402 */ 00403 void clearBuffer(void) {for (int i = 0; i < GPS_BUFFER_SIZE; i++) inBuffer[i] = '\0';}; 00404 00405 //! It breaks a string into its arguments separated by "separators". The pieces are stored in 'arguments' array 00406 /*! 00407 \param const char* str : string to separate 00408 \param char separator : the separator used to separate the string in pieces 00409 \return void 00410 \sa clearArguments(),clearBuffer() 00411 */ 00412 void strExplode(const char* str, char separator); 00413 00414 //! It generates a decimal number from two ASCII characters which were numbers 00415 /*! 00416 \param uint8_t conv1 : the ASCII number first digit to convert 00417 \param uint8_t conv2 : the ASCII number second digit to convert 00418 \return the converted number 00419 */ 00420 uint8_t converter(uint8_t conv1, uint8_t conv2); 00421 00422 //! It converts a float into a string 00423 /*! 00424 \param float fl : the float to convert 00425 \param char str[] : the string where store the float converted 00426 \param int N : the number of decimals 00427 \return void 00428 */ 00429 void float2String (float fl, char str[], int N); 00430 00431 }; 00432 00433 extern WaspUtils Utils; 00434 00435 #endif
1.5.6