Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect libmDot mbed-rtos mbed
utilities.cpp
00001 /* ======================================== 00002 * Filename: utilities.c 00003 * 00004 * Description: functions needed by other files 00005 * 00006 * Copyright J-Factor Embedded Technologies 2015 00007 * Copyright TJM Embedded Software 2015 00008 * All Rights Reserved 00009 * UNPUBLISHED, LICENSED SOFTWARE. 00010 * 00011 * CONFIDENTIAL AND PROPRIETARY INFORMATION 00012 * WHICH IS THE PROPERTY OF J-Factor Embedded Technologies. 00013 * 00014 * ======================================== 00015 */ 00016 /** \file 00017 * \brief functions used by other files. 00018 */ 00019 #include <time.h> 00020 #include "stdio.h" 00021 #include "common.h" 00022 00023 /** 00024 * \fn void date(void) 00025 * 00026 * \brief gets current time and date. 00027 * 00028 * gets date current time and date since last set date command. there is no real time clock in the system so date is relative. 00029 * \return None<p> 00030 * 00031 */ 00032 void date(void) 00033 { 00034 struct tm *epoch_tm = gmtime(&epoch); 00035 if(epoch_tm != NULL) 00036 c_time_string = asctime(gmtime(&epoch)); 00037 else 00038 c_time_string = "00:00:00"; 00039 } 00040 00041 00042 /** 00043 * \fn void setDate(char *str) 00044 * 00045 * \brief gets current time and date. 00046 * 00047 * gets date current time and date since last set date command. there is no real time clock in the system so date is relative. 00048 * \return None<p> 00049 * 00050 */ 00051 void setDate(char *str) 00052 { 00053 struct tm t; 00054 int day,month,year,hour,minute,second; 00055 int num; 00056 time_t tempEpoch; 00057 00058 num = sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second); /* get human readable current date */ 00059 if(num != 6) 00060 { 00061 pc.printf("Illegal or missing parameters. expected 6 integers\r\nformat: Month Day Year Hour Minute Second\r\n"); 00062 return; 00063 } 00064 t.tm_year = year-1900; 00065 t.tm_mon = month - 1; // Month, 0 - jan 00066 t.tm_mday = day; // Day of the month 00067 t.tm_hour = hour; 00068 t.tm_min = minute; 00069 t.tm_sec = second; 00070 t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown 00071 tempEpoch = mktime(&t); 00072 if ( (month > 12) || (day > 31) || (hour > 23) || (minute > 59) || (second > 59) ) 00073 { 00074 pc.printf("Illegal or missing parameters. expected 6 integers\r\nformat: Month/Day/Year Hour:Minute:Second\r\n"); 00075 return; 00076 } 00077 epoch = tempEpoch; 00078 } 00079 00080 void printVersion() 00081 { 00082 // pc.printf("%s Built on: %s %s\r\n\r\n", dot->getId().c_str(),__DATE__,__TIME__); 00083 pc.printf("\r\nJLC MT Demo: V%d.%02d %s Built on: %s %s\r\n",VERSION_MAJOR,VERSION_MINOR,VERSION_DATE, __DATE__,__TIME__); 00084 } 00085 00086 uint8 tolower(uint8 c) 00087 { 00088 if( (c >= 'A') && (c <= 'Z') ) 00089 return (c | 0x20); 00090 else 00091 return c; 00092 } 00093 00094 /** 00095 * \fn float strtofloat (char *str) 00096 * 00097 * \brief converts an ASCII string to float. 00098 * 00099 * This routine converts a floating point number in the form 00100 * xx.yy to a floating point number. 00101 * 00102 * \param[in] str - pointer to a character string containing the floating point number 00103 * \return float - converted number<p> 00104 * 00105 */ 00106 float strtofloat (char *str) 00107 { 00108 float result = 0.0; 00109 uint8 intPart[3]; 00110 uint16 intValue = 0; 00111 uint8 floatPart[3]; 00112 float floatValue = 0.0; 00113 uint8 i; 00114 char *ptrDecimalPoint = NULL; 00115 char *ptrStr = NULL; 00116 char ch; 00117 uint8 fractionValid = 0; 00118 uint8 integerValid = 0; 00119 00120 for(i=0;i<sizeof(intPart);i++) /* clear integer digits */ 00121 intPart[i] = 0; 00122 for(i=0;i<sizeof(floatPart);i++) /* floating digits */ 00123 floatPart[i] = 0; 00124 00125 ptrDecimalPoint = strchr(str, '.'); /* find the decimal point if any */ 00126 if (ptrDecimalPoint != NULL) 00127 { 00128 fractionValid = 1; 00129 if(ptrDecimalPoint != str) /* see if any integer digits */ 00130 integerValid = 1; 00131 } 00132 else 00133 { 00134 integerValid = 1; 00135 ptrDecimalPoint = str + strlen(str); 00136 } 00137 00138 if(integerValid) /* get and save all integer digits */ 00139 { 00140 for(i=0, ptrStr = ptrDecimalPoint;ptrStr != str; i++) 00141 { 00142 ptrStr--; 00143 ch = *ptrStr; 00144 if( (ch >= '0') && (ch <= '9') ) 00145 { 00146 intPart[i] = ch - '0'; 00147 } 00148 else 00149 { 00150 fractionValid = 0; 00151 break; 00152 } 00153 } 00154 for(i=0;i<sizeof(intPart);i++) 00155 intValue += (intPart[i] * power(10,i)); 00156 } 00157 00158 if(fractionValid) /* get and save all fraction digits */ 00159 { 00160 for(i=0, ptrStr = ptrDecimalPoint;ptrStr < str + strlen(str); i++) 00161 { 00162 ptrStr++; 00163 ch = *ptrStr; 00164 if( (ch >= '0') && (ch <= '9') ) 00165 { 00166 floatPart[i] = ch - '0'; 00167 } 00168 else 00169 { 00170 break; 00171 } 00172 } 00173 for(i=0;i<sizeof(floatPart);i++) 00174 floatValue += (floatPart[i] * (1.0/power(10,i+1)) ); 00175 } 00176 result = intValue + floatValue; 00177 return result; 00178 } 00179 00180 /** 00181 * \fn uint16 power(uint8 num, uint8 pow) 00182 * 00183 * \brief takes number(num) to power(pow) 00184 * 00185 * This routine takes an 8 bit integer num and 00186 * raises it ot the power pow. 00187 * 00188 * \param[in] num - 8 bit integer number 00189 * \param[in] pow - 8 bit power to raise num 00190 * \return uint16 - 16 bit num to the power pow<p> 00191 * 00192 */ 00193 uint16 power(uint8 num, uint8 pow) 00194 { 00195 uint8 i; 00196 uint16 result = 1; 00197 00198 for(i=0;i<pow;i++) 00199 result = result * num; 00200 return result; 00201 }
Generated on Sat Jul 16 2022 13:04:30 by
1.7.2