demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Committer:
henryrawas
Date:
Thu Feb 04 20:10:33 2016 +0000
Revision:
33:8b9dcbf6d8ec
Parent:
20:891b5270845a
update libs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
henryrawas 20:891b5270845a 1 // Copyright (c) Microsoft. All rights reserved.
henryrawas 20:891b5270845a 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
henryrawas 20:891b5270845a 3
henryrawas 20:891b5270845a 4 #ifndef __TIMESTAMP_H__
henryrawas 20:891b5270845a 5 #define __TIMESTAMP_H__
henryrawas 20:891b5270845a 6
henryrawas 20:891b5270845a 7 // use to timestamp alerts and measurements
henryrawas 20:891b5270845a 8 // with seconds and milliseconds
henryrawas 20:891b5270845a 9 // MS values are not real, but are intended to space out times properly
henryrawas 20:891b5270845a 10 class Timestamp
henryrawas 20:891b5270845a 11 {
henryrawas 20:891b5270845a 12 public:
henryrawas 20:891b5270845a 13 Timestamp()
henryrawas 20:891b5270845a 14 {
henryrawas 20:891b5270845a 15 _timestamp.start();
henryrawas 20:891b5270845a 16 _stampSecs = time(NULL);
henryrawas 20:891b5270845a 17 int now = _timestamp.read_ms();
henryrawas 20:891b5270845a 18 _stampMs = ((unsigned int)now) % 1000;
henryrawas 20:891b5270845a 19 _zeroMs = _stampMs;
henryrawas 20:891b5270845a 20 };
henryrawas 20:891b5270845a 21
henryrawas 20:891b5270845a 22 // Take a timestamp and save secs and ms
henryrawas 20:891b5270845a 23 // Note: We try to set ms to 0 for first measure, and then set ms
henryrawas 20:891b5270845a 24 // with the difference for subsequent values with same secs
henryrawas 20:891b5270845a 25 void GetTimestamp()
henryrawas 20:891b5270845a 26 {
henryrawas 20:891b5270845a 27 time_t nowsecs = time(NULL);
henryrawas 20:891b5270845a 28 int now = _timestamp.read_ms();
henryrawas 20:891b5270845a 29
henryrawas 20:891b5270845a 30 double diffsecs = difftime(nowsecs, _stampSecs);
henryrawas 20:891b5270845a 31
henryrawas 20:891b5270845a 32 if (diffsecs > 0)
henryrawas 20:891b5270845a 33 {
henryrawas 20:891b5270845a 34 {
henryrawas 20:891b5270845a 35 _zeroMs = now;
henryrawas 20:891b5270845a 36 }
henryrawas 20:891b5270845a 37 }
henryrawas 20:891b5270845a 38 else
henryrawas 20:891b5270845a 39 {
henryrawas 20:891b5270845a 40 if (now - _zeroMs > 999)
henryrawas 20:891b5270845a 41 {
henryrawas 20:891b5270845a 42 _zeroMs = now - 999;
henryrawas 20:891b5270845a 43 }
henryrawas 20:891b5270845a 44 if (now - _zeroMs < _stampMs)
henryrawas 20:891b5270845a 45 {
henryrawas 20:891b5270845a 46 _zeroMs = now - _stampMs;
henryrawas 20:891b5270845a 47 }
henryrawas 20:891b5270845a 48 }
henryrawas 20:891b5270845a 49 // keep the ms difference for timestamp
henryrawas 20:891b5270845a 50 _stampMs = ((unsigned int)(now - _zeroMs)) % 1000;
henryrawas 20:891b5270845a 51 _stampSecs = nowsecs;
henryrawas 20:891b5270845a 52 };
henryrawas 20:891b5270845a 53
henryrawas 20:891b5270845a 54 time_t GetSecs()
henryrawas 20:891b5270845a 55 {
henryrawas 20:891b5270845a 56 return _stampSecs;
henryrawas 20:891b5270845a 57 };
henryrawas 20:891b5270845a 58
henryrawas 20:891b5270845a 59 int GetMs()
henryrawas 20:891b5270845a 60 {
henryrawas 20:891b5270845a 61 return _stampMs;
henryrawas 20:891b5270845a 62 };
henryrawas 20:891b5270845a 63
henryrawas 20:891b5270845a 64 private:
henryrawas 20:891b5270845a 65
henryrawas 20:891b5270845a 66 Timer _timestamp;
henryrawas 20:891b5270845a 67
henryrawas 20:891b5270845a 68 time_t _stampSecs;
henryrawas 20:891b5270845a 69 unsigned int _stampMs;
henryrawas 20:891b5270845a 70 int _zeroMs;
henryrawas 20:891b5270845a 71 };
henryrawas 20:891b5270845a 72
henryrawas 20:891b5270845a 73 #endif