Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LTimer.cpp Source File

LTimer.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 <stdlib.h>
00018 #include <string.h>
00019 #include <sys/time.h>
00020 
00021 #include "LMqttsnClientApp.h"
00022 #include "LTimer.h"
00023 
00024 //using namespace std;
00025 using namespace linuxAsyncClient;
00026 
00027 /*=====================================
00028         Class Timer
00029  =====================================*/
00030 
00031 LTimer::LTimer(){
00032     _startTime.tv_sec = 0;
00033     _millis = 0;
00034 }
00035 
00036 LTimer::~LTimer(){
00037 
00038 }
00039 
00040 void LTimer::start(uint32_t msec){
00041   gettimeofday(&_startTime, 0);
00042   _millis = msec;
00043 }
00044 
00045 bool LTimer::isTimeUp(void){
00046   return isTimeUp(_millis);
00047 }
00048 
00049 bool LTimer::isTimeUp(uint32_t msec){
00050     struct timeval curTime;
00051     uint32_t secs, usecs;
00052     if (_startTime.tv_sec == 0){
00053         return false;
00054     }else{
00055         gettimeofday(&curTime, 0);
00056         secs  = (curTime.tv_sec  - _startTime.tv_sec) * 1000;
00057         usecs = (curTime.tv_usec - _startTime.tv_usec) / 1000.0;
00058         return ((secs + usecs) > (uint32_t)msec);
00059     }
00060 }
00061 
00062 void LTimer::stop(){
00063   _startTime.tv_sec = 0;
00064   _millis = 0;
00065 }
00066 
00067 uint32_t LTimer::getRemain(void)
00068 {
00069     struct timeval curTime;
00070     uint32_t secs, usecs;
00071     if (_millis <= 0){
00072         return 0;
00073     }else{
00074         gettimeofday(&curTime, 0);
00075         secs  = (curTime.tv_sec  - _startTime.tv_sec) * 1000;
00076         usecs = (curTime.tv_usec - _startTime.tv_usec) / 1000.0;
00077         secs = _millis - (secs + usecs);
00078         return secs;
00079     }
00080 }