robot arm demo team / Mbed 2 deprecated RobotArmDemo Featured

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timestamp.h Source File

Timestamp.h

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #ifndef __TIMESTAMP_H__
00005 #define __TIMESTAMP_H__
00006 
00007 // use to timestamp alerts and measurements
00008 // with seconds and milliseconds
00009 // MS values are not real, but are intended to space out times properly
00010 class Timestamp
00011 {
00012 public:
00013     Timestamp()
00014     {
00015         _timestamp.start();
00016         _stampSecs = time(NULL);
00017         int now = _timestamp.read_ms();
00018         _stampMs = ((unsigned int)now) % 1000;
00019         _zeroMs = _stampMs;
00020     };
00021     
00022     // Take a timestamp and save secs and ms
00023     // Note: We try to set ms to 0 for first measure, and then set ms 
00024     // with the difference for subsequent values with same secs
00025     void GetTimestamp()
00026     {
00027         time_t nowsecs = time(NULL);
00028         int now = _timestamp.read_ms();
00029 
00030         double diffsecs = difftime(nowsecs, _stampSecs);
00031         
00032         if (diffsecs > 0)
00033         {
00034             {
00035                 _zeroMs = now;
00036             }
00037         }
00038         else
00039         {
00040             if (now - _zeroMs > 999)
00041             {
00042                 _zeroMs = now - 999;
00043             }
00044             if (now - _zeroMs < _stampMs)
00045             {
00046                 _zeroMs = now - _stampMs;
00047             }
00048         }
00049         // keep the ms difference for timestamp
00050         _stampMs = ((unsigned int)(now - _zeroMs)) % 1000;
00051         _stampSecs = nowsecs;
00052     };
00053 
00054     time_t GetSecs()
00055     {
00056         return _stampSecs;
00057     };
00058     
00059     int GetMs()
00060     {
00061         return _stampMs;
00062     };
00063     
00064 private:
00065 
00066     Timer _timestamp;
00067 
00068     time_t _stampSecs;
00069     unsigned int _stampMs;
00070     int _zeroMs;
00071 };
00072 
00073 #endif