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.
Utility.cpp
00001 #include "Utility.h" 00002 #include <stdlib.h> 00003 #include <stdio.h> 00004 #include <string.h> 00005 #include <stdarg.h> 00006 #include <string> 00007 #include <cctype> 00008 #include <algorithm> 00009 00010 extern I2C i2c; 00011 00012 static void DisplayBuffer(Stream * chp, char * buff) 00013 { 00014 int i = 0; 00015 while(buff[i] != 0) 00016 { 00017 char c = buff[i]; 00018 if (c == '\n') { 00019 chp->printf("\r%c", c); 00020 break; 00021 } 00022 else 00023 chp->printf("%c", c); 00024 00025 i++; 00026 } 00027 } 00028 00029 00030 static int DisplayHeap(void* pBuffer, char const* pFormatString, ...) 00031 { 00032 char* pStringEnd = (char*)pBuffer + strlen((char*)pBuffer); 00033 va_list valist; 00034 00035 va_start(valist, pFormatString); 00036 00037 return vsprintf(pStringEnd, pFormatString, valist); 00038 } 00039 00040 00041 void heapinfo(Stream * chp) { 00042 char OutputBuffer[256]; 00043 chp->printf("Build Date/Time: %s %s\r\n", __DATE__, __TIME__); 00044 chp->printf("Current Stack : 0x%08X\r\n", __current_sp()); 00045 chp->printf("Current Heap : 0x%08X\r\n", __current_pc()); 00046 00047 OutputBuffer[0] = '\0'; 00048 typedef int (*__heapprt)(void *, char const *, ...); 00049 __heapstats( (__heapprt)DisplayHeap, OutputBuffer ) ; 00050 chp->printf("Available Memory: "); 00051 DisplayBuffer(chp, OutputBuffer); 00052 } 00053 00054 char *_strtok(char *str, const char *delim, char **saveptr) 00055 { 00056 char *token; 00057 if (str) 00058 *saveptr = str; 00059 token = *saveptr; 00060 00061 if (!token) 00062 return NULL; 00063 00064 token += strspn(token, delim); 00065 *saveptr = strpbrk(token, delim); 00066 if (*saveptr) 00067 *(*saveptr)++ = '\0'; 00068 00069 return *token ? token : NULL; 00070 } 00071 00072 void split( vector<string> & theStringVector, /* Altered/returned value */ 00073 const string & theString, 00074 const string & theDelimiter) 00075 { 00076 size_t start = 0, end = 0; 00077 while ( end != string::npos) 00078 { 00079 end = theString.find( theDelimiter, start); 00080 00081 // If at end, use length=maxLength. Else use length=end-start. 00082 theStringVector.push_back( theString.substr( start, 00083 (end == string::npos) ? string::npos : end - start)); 00084 00085 // If at end, use start=maxSize. Else use start=end+delimiter. 00086 start = ( ( end > (string::npos - theDelimiter.size()) ) 00087 ? string::npos : end + theDelimiter.size()); 00088 } 00089 } 00090 00091 std::string trim(const std::string& str, 00092 const std::string& whitespace) 00093 { 00094 size_t strBegin = str.find_first_not_of(whitespace); 00095 if (strBegin == std::string::npos) 00096 return ""; // no content 00097 00098 size_t strEnd = str.find_last_not_of(whitespace); 00099 size_t strRange = strEnd - strBegin + 1; 00100 00101 return str.substr(strBegin, strRange); 00102 } 00103 00104 static void transform_if_first_of_word( char& c ) 00105 { 00106 // if the previous character was a space, transform it toupper 00107 if( (*(&c - sizeof(char))) == ' ') 00108 c = toupper( c ); 00109 } 00110 00111 void capitalize_word_start(std::string & str) 00112 { 00113 str[ 0 ] = toupper( str[ 0 ]); 00114 00115 std::for_each( str.begin()+1, str.end(), transform_if_first_of_word ); 00116 } 00117 00118 void set_ctrl_panel(int paneladdr, uint8_t value) 00119 { 00120 char cmd[2]; 00121 uint8_t addr; 00122 00123 switch(paneladdr) 00124 { 00125 case 0: 00126 addr = I2C_CTR_PANEL_ADDR; 00127 cmd[0] = 0x12; 00128 break; 00129 case 1: 00130 addr = I2C_CTR_PANEL_ADDR; 00131 cmd[0] = 0x13; 00132 break; 00133 default: 00134 // don't set anything for non-existing address 00135 return; 00136 } 00137 00138 cmd[1] = value; 00139 00140 i2c.write(addr, cmd, 2); 00141 } 00142 00143 00144
Generated on Tue Jul 12 2022 18:55:02 by
1.7.2