Sergei G / Mbed OS Borsch

Dependencies:   DataStore JobScheduler NetworkServices W5500Interface nanopb protocol

Revision:
9:d6a9210bfd41
Parent:
8:51b6c5ed33f8
--- a/source/jobFakeADC.cpp	Fri Jul 14 22:36:37 2017 +0000
+++ b/source/jobFakeADC.cpp	Mon Jul 17 18:33:51 2017 +0000
@@ -2,6 +2,7 @@
 
 #include "mbed-trace/mbed_trace.h"
 #define TRACE_GROUP  "jtp"
+#include "us_ticker_api.h"
 
 __packed struct AdcCh {
     uint32_t Raw;   // 4 bytes
@@ -14,27 +15,50 @@
     struct AdcCh Chs[8];    // 64 = 8 * (4+4)
 };  // 8+1+64=73
 
-uint32_t swapByteOrder(uint32_t num)
+uint32_t swap_uint32(uint32_t num)
 {
+    /*
+    source: https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func
+    */
     return ((num>>24)&0xff) | // move byte 3 to byte 0
            ((num<<8)&0xff0000) | // move byte 1 to byte 2
            ((num>>8)&0xff00) | // move byte 2 to byte 1
            ((num<<24)&0xff000000);
 };
 
+uint64_t swap_uint64( uint64_t uvalue )
+{
+    /* 
+    source: https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func
+    https://social.msdn.microsoft.com/Forums/vstudio/en-US/144b3047-bcbd-48d6-a28a-6fbda82e151e/convert-big-endian-to-little-endian?forum=csharpgeneral
+    */
+
+    return ( (0x00000000000000FF) & (uvalue >> 56)
+        | (0x000000000000FF00) & (uvalue >> 40)
+        | (0x0000000000FF0000) & (uvalue >> 24)
+        | (0x00000000FF000000) & (uvalue >> 8)
+        | (0x000000FF00000000) & (uvalue << 8)
+        | (0x0000FF0000000000) & (uvalue << 24)
+        | (0x00FF000000000000) & (uvalue << 40)
+        | (0xFF00000000000000) & (uvalue << 56));
+}
+
 void JobFakeADC::Run() {
     tr_info("Job Fake ADC");
-    // 1256729737 is HEX: 4a e8 2c 89; // Wed, 28 Oct 2009 11:35:37
-    // let's flip byte order: 89 2c e8 4a
-    time_t now = 0x892ce84a;
+    time_t now = time(NULL);
+    //tr_debug("ADC created time is: %s\n", ctime(&now));
     struct AdcMsg msg;
-    msg.Created = now;
+    // convert seconds to nanoseconds
+    msg.Created = now * 1e9;
+    tr_debug("Current time: %x sec, %llx nanosecs", now, msg.Created);
+    // swapt for byte order
+    msg.Created = swap_uint64(msg.Created); // swap_uint64(msg.Created);
     msg.Mask = 0x80; // 0x80 bits: 1000 0000
     for (int i = 0; i < 8; i++) {
         msg.Chs[i].Raw = 0;
         msg.Chs[i].V = 0;
     }
-    msg.Chs[0].Raw = swapByteOrder(_next++);
+    msg.Chs[0].Raw = swap_uint32(_next++);
     msg.Chs[0].V = msg.Chs[0].Raw;
     // /test can be used to send to test endpoint that responds with
     // a short hardcoded string