JLC test

Dependencies:   PinDetect libmDot mbed-rtos mbed

Committer:
tmulrooney
Date:
Sat Jan 30 17:02:23 2016 +0000
Revision:
2:376af6a70e8a
Parent:
1:96c429800568
using UART 2 - not working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tmulrooney 1:96c429800568 1 /* ========================================
tmulrooney 1:96c429800568 2 * Filename: utilities.c
tmulrooney 1:96c429800568 3 *
tmulrooney 1:96c429800568 4 * Description: functions needed by other files
tmulrooney 1:96c429800568 5 *
tmulrooney 1:96c429800568 6 * Copyright J-Factor Embedded Technologies 2015
tmulrooney 1:96c429800568 7 * Copyright TJM Embedded Software 2015
tmulrooney 1:96c429800568 8 * All Rights Reserved
tmulrooney 1:96c429800568 9 * UNPUBLISHED, LICENSED SOFTWARE.
tmulrooney 1:96c429800568 10 *
tmulrooney 1:96c429800568 11 * CONFIDENTIAL AND PROPRIETARY INFORMATION
tmulrooney 1:96c429800568 12 * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies.
tmulrooney 1:96c429800568 13 *
tmulrooney 1:96c429800568 14 * ========================================
tmulrooney 1:96c429800568 15 */
tmulrooney 1:96c429800568 16 /** \file
tmulrooney 1:96c429800568 17 * \brief functions used by other files.
tmulrooney 1:96c429800568 18 */
tmulrooney 1:96c429800568 19 #include <time.h>
tmulrooney 1:96c429800568 20 #include "stdio.h"
tmulrooney 1:96c429800568 21 #include "common.h"
tmulrooney 1:96c429800568 22
tmulrooney 1:96c429800568 23 /**
tmulrooney 1:96c429800568 24 * \fn void date(void)
tmulrooney 1:96c429800568 25 *
tmulrooney 1:96c429800568 26 * \brief gets current time and date.
tmulrooney 1:96c429800568 27 *
tmulrooney 1:96c429800568 28 * gets date current time and date since last set date command. there is no real time clock in the system so date is relative.
tmulrooney 1:96c429800568 29 * \return None<p>
tmulrooney 1:96c429800568 30 *
tmulrooney 1:96c429800568 31 */
tmulrooney 1:96c429800568 32 void date(void)
tmulrooney 1:96c429800568 33 {
tmulrooney 1:96c429800568 34 struct tm *epoch_tm = gmtime(&epoch);
tmulrooney 1:96c429800568 35 if(epoch_tm != NULL)
tmulrooney 1:96c429800568 36 c_time_string = asctime(gmtime(&epoch));
tmulrooney 1:96c429800568 37 else
tmulrooney 1:96c429800568 38 c_time_string = "00:00:00";
tmulrooney 1:96c429800568 39 }
tmulrooney 1:96c429800568 40
tmulrooney 1:96c429800568 41
tmulrooney 1:96c429800568 42 /**
tmulrooney 1:96c429800568 43 * \fn void setDate(char *str)
tmulrooney 1:96c429800568 44 *
tmulrooney 1:96c429800568 45 * \brief gets current time and date.
tmulrooney 1:96c429800568 46 *
tmulrooney 1:96c429800568 47 * gets date current time and date since last set date command. there is no real time clock in the system so date is relative.
tmulrooney 1:96c429800568 48 * \return None<p>
tmulrooney 1:96c429800568 49 *
tmulrooney 1:96c429800568 50 */
tmulrooney 1:96c429800568 51 void setDate(char *str)
tmulrooney 1:96c429800568 52 {
tmulrooney 1:96c429800568 53 struct tm t;
tmulrooney 1:96c429800568 54 int day,month,year,hour,minute,second;
tmulrooney 1:96c429800568 55 int num;
tmulrooney 1:96c429800568 56 time_t tempEpoch;
tmulrooney 1:96c429800568 57
tmulrooney 1:96c429800568 58 num = sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second); /* get human readable current date */
tmulrooney 1:96c429800568 59 if(num != 6)
tmulrooney 1:96c429800568 60 {
tmulrooney 1:96c429800568 61 pc.printf("Illegal or missing parameters. expected 6 integers\r\nformat: Month Day Year Hour Minute Second\r\n");
tmulrooney 1:96c429800568 62 return;
tmulrooney 1:96c429800568 63 }
tmulrooney 1:96c429800568 64 t.tm_year = year-1900;
tmulrooney 1:96c429800568 65 t.tm_mon = month - 1; // Month, 0 - jan
tmulrooney 1:96c429800568 66 t.tm_mday = day; // Day of the month
tmulrooney 1:96c429800568 67 t.tm_hour = hour;
tmulrooney 1:96c429800568 68 t.tm_min = minute;
tmulrooney 1:96c429800568 69 t.tm_sec = second;
tmulrooney 1:96c429800568 70 t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
tmulrooney 1:96c429800568 71 tempEpoch = mktime(&t);
tmulrooney 1:96c429800568 72 if ( (month > 12) || (day > 31) || (hour > 23) || (minute > 59) || (second > 59) )
tmulrooney 1:96c429800568 73 {
tmulrooney 1:96c429800568 74 pc.printf("Illegal or missing parameters. expected 6 integers\r\nformat: Month/Day/Year Hour:Minute:Second\r\n");
tmulrooney 1:96c429800568 75 return;
tmulrooney 1:96c429800568 76 }
tmulrooney 1:96c429800568 77 epoch = tempEpoch;
tmulrooney 1:96c429800568 78 }
tmulrooney 1:96c429800568 79
tmulrooney 1:96c429800568 80 void printVersion()
tmulrooney 1:96c429800568 81 {
tmulrooney 1:96c429800568 82 // pc.printf("%s Built on: %s %s\r\n\r\n", dot->getId().c_str(),__DATE__,__TIME__);
tmulrooney 1:96c429800568 83 pc.printf("\r\nJLC MT Demo: V%d.%02d %s Built on: %s %s\r\n",VERSION_MAJOR,VERSION_MINOR,VERSION_DATE, __DATE__,__TIME__);
tmulrooney 1:96c429800568 84 }
tmulrooney 1:96c429800568 85
tmulrooney 1:96c429800568 86 uint8 tolower(uint8 c)
tmulrooney 1:96c429800568 87 {
tmulrooney 1:96c429800568 88 if( (c >= 'A') && (c <= 'Z') )
tmulrooney 1:96c429800568 89 return (c | 0x20);
tmulrooney 1:96c429800568 90 else
tmulrooney 1:96c429800568 91 return c;
tmulrooney 1:96c429800568 92 }
tmulrooney 1:96c429800568 93
tmulrooney 1:96c429800568 94 /**
tmulrooney 1:96c429800568 95 * \fn float strtofloat (char *str)
tmulrooney 1:96c429800568 96 *
tmulrooney 1:96c429800568 97 * \brief converts an ASCII string to float.
tmulrooney 1:96c429800568 98 *
tmulrooney 1:96c429800568 99 * This routine converts a floating point number in the form
tmulrooney 1:96c429800568 100 * xx.yy to a floating point number.
tmulrooney 1:96c429800568 101 *
tmulrooney 1:96c429800568 102 * \param[in] str - pointer to a character string containing the floating point number
tmulrooney 1:96c429800568 103 * \return float - converted number<p>
tmulrooney 1:96c429800568 104 *
tmulrooney 1:96c429800568 105 */
tmulrooney 1:96c429800568 106 float strtofloat (char *str)
tmulrooney 1:96c429800568 107 {
tmulrooney 1:96c429800568 108 float result = 0.0;
tmulrooney 1:96c429800568 109 uint8 intPart[3];
tmulrooney 1:96c429800568 110 uint16 intValue = 0;
tmulrooney 1:96c429800568 111 uint8 floatPart[3];
tmulrooney 1:96c429800568 112 float floatValue = 0.0;
tmulrooney 1:96c429800568 113 uint8 i;
tmulrooney 1:96c429800568 114 char *ptrDecimalPoint = NULL;
tmulrooney 1:96c429800568 115 char *ptrStr = NULL;
tmulrooney 1:96c429800568 116 char ch;
tmulrooney 1:96c429800568 117 uint8 fractionValid = 0;
tmulrooney 1:96c429800568 118 uint8 integerValid = 0;
tmulrooney 1:96c429800568 119
tmulrooney 1:96c429800568 120 for(i=0;i<sizeof(intPart);i++) /* clear integer digits */
tmulrooney 1:96c429800568 121 intPart[i] = 0;
tmulrooney 1:96c429800568 122 for(i=0;i<sizeof(floatPart);i++) /* floating digits */
tmulrooney 1:96c429800568 123 floatPart[i] = 0;
tmulrooney 1:96c429800568 124
tmulrooney 1:96c429800568 125 ptrDecimalPoint = strchr(str, '.'); /* find the decimal point if any */
tmulrooney 1:96c429800568 126 if (ptrDecimalPoint != NULL)
tmulrooney 1:96c429800568 127 {
tmulrooney 1:96c429800568 128 fractionValid = 1;
tmulrooney 1:96c429800568 129 if(ptrDecimalPoint != str) /* see if any integer digits */
tmulrooney 1:96c429800568 130 integerValid = 1;
tmulrooney 1:96c429800568 131 }
tmulrooney 1:96c429800568 132 else
tmulrooney 1:96c429800568 133 {
tmulrooney 1:96c429800568 134 integerValid = 1;
tmulrooney 1:96c429800568 135 ptrDecimalPoint = str + strlen(str);
tmulrooney 1:96c429800568 136 }
tmulrooney 1:96c429800568 137
tmulrooney 1:96c429800568 138 if(integerValid) /* get and save all integer digits */
tmulrooney 1:96c429800568 139 {
tmulrooney 1:96c429800568 140 for(i=0, ptrStr = ptrDecimalPoint;ptrStr != str; i++)
tmulrooney 1:96c429800568 141 {
tmulrooney 1:96c429800568 142 ptrStr--;
tmulrooney 1:96c429800568 143 ch = *ptrStr;
tmulrooney 1:96c429800568 144 if( (ch >= '0') && (ch <= '9') )
tmulrooney 1:96c429800568 145 {
tmulrooney 1:96c429800568 146 intPart[i] = ch - '0';
tmulrooney 1:96c429800568 147 }
tmulrooney 1:96c429800568 148 else
tmulrooney 1:96c429800568 149 {
tmulrooney 1:96c429800568 150 fractionValid = 0;
tmulrooney 1:96c429800568 151 break;
tmulrooney 1:96c429800568 152 }
tmulrooney 1:96c429800568 153 }
tmulrooney 1:96c429800568 154 for(i=0;i<sizeof(intPart);i++)
tmulrooney 1:96c429800568 155 intValue += (intPart[i] * power(10,i));
tmulrooney 1:96c429800568 156 }
tmulrooney 1:96c429800568 157
tmulrooney 1:96c429800568 158 if(fractionValid) /* get and save all fraction digits */
tmulrooney 1:96c429800568 159 {
tmulrooney 1:96c429800568 160 for(i=0, ptrStr = ptrDecimalPoint;ptrStr < str + strlen(str); i++)
tmulrooney 1:96c429800568 161 {
tmulrooney 1:96c429800568 162 ptrStr++;
tmulrooney 1:96c429800568 163 ch = *ptrStr;
tmulrooney 1:96c429800568 164 if( (ch >= '0') && (ch <= '9') )
tmulrooney 1:96c429800568 165 {
tmulrooney 1:96c429800568 166 floatPart[i] = ch - '0';
tmulrooney 1:96c429800568 167 }
tmulrooney 1:96c429800568 168 else
tmulrooney 1:96c429800568 169 {
tmulrooney 1:96c429800568 170 break;
tmulrooney 1:96c429800568 171 }
tmulrooney 1:96c429800568 172 }
tmulrooney 1:96c429800568 173 for(i=0;i<sizeof(floatPart);i++)
tmulrooney 1:96c429800568 174 floatValue += (floatPart[i] * (1.0/power(10,i+1)) );
tmulrooney 1:96c429800568 175 }
tmulrooney 1:96c429800568 176 result = intValue + floatValue;
tmulrooney 1:96c429800568 177 return result;
tmulrooney 1:96c429800568 178 }
tmulrooney 1:96c429800568 179
tmulrooney 1:96c429800568 180 /**
tmulrooney 1:96c429800568 181 * \fn uint16 power(uint8 num, uint8 pow)
tmulrooney 1:96c429800568 182 *
tmulrooney 1:96c429800568 183 * \brief takes number(num) to power(pow)
tmulrooney 1:96c429800568 184 *
tmulrooney 1:96c429800568 185 * This routine takes an 8 bit integer num and
tmulrooney 1:96c429800568 186 * raises it ot the power pow.
tmulrooney 1:96c429800568 187 *
tmulrooney 1:96c429800568 188 * \param[in] num - 8 bit integer number
tmulrooney 1:96c429800568 189 * \param[in] pow - 8 bit power to raise num
tmulrooney 1:96c429800568 190 * \return uint16 - 16 bit num to the power pow<p>
tmulrooney 1:96c429800568 191 *
tmulrooney 1:96c429800568 192 */
tmulrooney 1:96c429800568 193 uint16 power(uint8 num, uint8 pow)
tmulrooney 1:96c429800568 194 {
tmulrooney 1:96c429800568 195 uint8 i;
tmulrooney 1:96c429800568 196 uint16 result = 1;
tmulrooney 1:96c429800568 197
tmulrooney 1:96c429800568 198 for(i=0;i<pow;i++)
tmulrooney 1:96c429800568 199 result = result * num;
tmulrooney 1:96c429800568 200 return result;
tmulrooney 1:96c429800568 201 }