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.
Timer.cpp
00001 /************************************************************************************** 00002 * Copyright (c) 2016, Tomoaki Yamaguchi 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation 00015 **************************************************************************************/ 00016 00017 #include <stdio.h> 00018 #include <time.h> 00019 #include <sys/time.h> 00020 #include <unistd.h> 00021 #include <fcntl.h> 00022 #include <string.h> 00023 #include "Timer.h" 00024 #include "MQTTSNGWDefines.h" 00025 00026 using namespace std; 00027 using namespace MQTTSNGW; 00028 00029 /*===================================== 00030 Print Current Date & Time 00031 =====================================*/ 00032 char theCurrentTime[32]; 00033 00034 const char* currentDateTime() 00035 { 00036 struct timeval now; 00037 struct tm tstruct; 00038 gettimeofday(&now, 0); 00039 tstruct = *localtime(&now.tv_sec); 00040 strftime(theCurrentTime, sizeof(theCurrentTime), "%Y%m%d %H%M%S", &tstruct); 00041 sprintf(theCurrentTime + 15, ".%03d", (int)now.tv_usec / 1000 ); 00042 return theCurrentTime; 00043 } 00044 00045 /*============================================ 00046 Timer 00047 ============================================*/ 00048 Timer::Timer(void) 00049 { 00050 stop(); 00051 } 00052 00053 Timer::~Timer(void) 00054 { 00055 00056 } 00057 00058 void Timer::start(uint32_t msecs) 00059 { 00060 gettimeofday(&_startTime, 0); 00061 _millis = msecs; 00062 } 00063 00064 bool Timer::isTimeup(void) 00065 { 00066 return isTimeup(_millis); 00067 } 00068 00069 bool Timer::isTimeup(uint32_t msecs) 00070 { 00071 struct timeval curTime; 00072 long secs, usecs; 00073 if (_startTime.tv_sec == 0) 00074 { 00075 return false; 00076 } 00077 else 00078 { 00079 gettimeofday(&curTime, 0); 00080 secs = (curTime.tv_sec - _startTime.tv_sec) * 1000; 00081 usecs = (curTime.tv_usec - _startTime.tv_usec) / 1000.0; 00082 return ((secs + usecs) > (long) msecs); 00083 } 00084 } 00085 00086 void Timer::stop() 00087 { 00088 _startTime.tv_sec = 0; 00089 _millis = 0; 00090 } 00091 00092 /*===================================== 00093 Class LightIndicator 00094 =====================================*/ 00095 00096 LightIndicator::LightIndicator() 00097 { 00098 _greenStatus = false; 00099 for ( int i = 0; i <= MAX_GPIO; i++) 00100 { 00101 _gpio[i] = 0; 00102 } 00103 init(); 00104 } 00105 00106 LightIndicator::~LightIndicator() 00107 { 00108 for ( int i = 0; i <= MAX_GPIO; i++) 00109 { 00110 if ( _gpio[i] ) 00111 { 00112 close( _gpio[i]); 00113 } 00114 } 00115 } 00116 00117 void LightIndicator::greenLight(bool on) 00118 { 00119 if (on) 00120 { 00121 if (!_greenStatus) 00122 { 00123 _greenStatus = true; 00124 //Turn Green on & turn Red off 00125 lit(LIGHT_INDICATOR_GREEN, "1"); 00126 lit(LIGHT_INDICATOR_RED, "0"); 00127 } 00128 } 00129 else 00130 { 00131 if (_greenStatus) 00132 { 00133 _greenStatus = false; 00134 //Turn Green off & turn Red on 00135 lit(LIGHT_INDICATOR_GREEN, "0"); 00136 lit(LIGHT_INDICATOR_RED, "1"); 00137 } 00138 } 00139 } 00140 void LightIndicator::blueLight(bool on) 00141 { 00142 if (on) 00143 { 00144 lit(LIGHT_INDICATOR_BLUE, "1"); 00145 if ( !_greenStatus ) 00146 { 00147 greenLight(true); 00148 } 00149 } 00150 else 00151 { 00152 lit(LIGHT_INDICATOR_BLUE, "0"); 00153 } 00154 } 00155 00156 void LightIndicator::redLight(bool on) 00157 { 00158 if (on) 00159 { 00160 lit(LIGHT_INDICATOR_RED, "1"); 00161 } 00162 else 00163 { 00164 lit(LIGHT_INDICATOR_RED, "0"); 00165 } 00166 } 00167 00168 void LightIndicator::allLightOff(void) 00169 { 00170 lit(LIGHT_INDICATOR_RED, "0"); 00171 lit(LIGHT_INDICATOR_BLUE, "0"); 00172 lit(LIGHT_INDICATOR_GREEN, "0"); 00173 _greenStatus = false; 00174 } 00175 00176 void LightIndicator::init() 00177 { 00178 pinMode(LIGHT_INDICATOR_GREEN); 00179 pinMode(LIGHT_INDICATOR_RED); 00180 pinMode(LIGHT_INDICATOR_BLUE); 00181 } 00182 00183 int LightIndicator::lit(int gpioNo, const char* onoff) 00184 { 00185 int rc = 0; 00186 if( _gpio[gpioNo] ) 00187 { 00188 rc = write(_gpio[gpioNo], onoff, 1); 00189 } 00190 return rc; 00191 } 00192 00193 void LightIndicator::pinMode(int gpioNo) 00194 { 00195 int rc = 0; 00196 int fd = rc; // eliminate unused warnning of compiler 00197 00198 fd = open("/sys/class/gpio/export", O_WRONLY); 00199 if ( fd < 0 ) 00200 { 00201 return; 00202 } 00203 char no[4]; 00204 00205 sprintf(no,"%d", gpioNo); 00206 rc = write(fd, no, strlen(no)); 00207 close(fd); 00208 00209 char fileName[64]; 00210 sprintf( fileName, "/sys/class/gpio/gpio%d/direction", gpioNo); 00211 00212 fd = open(fileName, O_WRONLY); 00213 if ( fd < 0 ) 00214 { 00215 return; 00216 } 00217 rc = write(fd,"out", 3); 00218 close(fd); 00219 sprintf( fileName, "/sys/class/gpio/gpio%d/value", gpioNo); 00220 fd = open(fileName, O_WRONLY); 00221 if ( fd > 0 ) 00222 { 00223 _gpio[gpioNo] = fd; 00224 } 00225 } 00226 00227
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2