2014-12-10 5:41 1 second pulse (serial)

Dependencies:   MODSERIALhacked Convert SLCD mbed-src

Files at this revision

API Documentation at this revision

Comitter:
jhaugen
Date:
Thu Dec 18 10:56:52 2014 +0000
Parent:
11:79eabc671800
Commit message:
Added timestamping to sensor readings.

Changed in this revision

double_buffer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 79eabc671800 -r c97d6cd17314 double_buffer.h
--- a/double_buffer.h	Thu Dec 18 06:31:26 2014 +0000
+++ b/double_buffer.h	Thu Dec 18 10:56:52 2014 +0000
@@ -1,19 +1,28 @@
 #ifndef DOUBLE_BUFFER_H
 #define DOUBLE_BUFFER_H
 
+#include "Timer.h"
+
 template <class T, unsigned int S>
 class DoubleBuffer {
     public:
         DoubleBuffer();
-        void write(T val);
+        void write(T val, long long timestamp);
         T* getReadBuffer();
         int getReadBufferSize();
+        long long* getTimestampReadBuffer();
         void swapBuff();
+        void init(long long* off, Timer* tim);
+        long long db_timestamps[2][S];
     private:
         T buffer[2][S];
+        
         int fill_buffer;
         int buffer0_size;
         int buffer1_size;
+        long long* off;
+        Timer* time;
+        bool active;
 };
 
 template <class T, unsigned int S>
@@ -21,11 +30,25 @@
     fill_buffer = 0;
     buffer0_size = 0;
     buffer1_size = 0;
+    active = false;
 }
 
 template <class T, unsigned int S>
-void DoubleBuffer<T, S>::write(T val) {
+void DoubleBuffer<T, S>::init(long long* o, Timer* tim) {
+    time = tim;
+    off = o;
+    active = true;
+}
+
+template <class T, unsigned int S>
+void DoubleBuffer<T, S>::write(T val, long long timestamp) {
+    //long long timestamp = time->read_us() - (*off);
     int buffer_size;
+    
+    if (!active) {
+        return;
+    }
+    
     if (fill_buffer == 0) {
         if(buffer0_size >= S) {
             return;
@@ -41,6 +64,7 @@
         buffer1_size++;
     }
     buffer[fill_buffer][buffer_size] = val;
+    db_timestamps[fill_buffer][buffer_size] = timestamp;
 }
 
 template <class T, unsigned int S>
@@ -54,6 +78,16 @@
 }
 
 template <class T, unsigned int S>
+long long* DoubleBuffer<T, S>::getTimestampReadBuffer() {
+    if (fill_buffer == 0) {
+        return db_timestamps[1];
+    }
+    else {
+        return db_timestamps[0];
+    }
+}
+
+template <class T, unsigned int S>
 int DoubleBuffer<T, S>::getReadBufferSize() {
     if (fill_buffer == 0) {
         return buffer1_size;
diff -r 79eabc671800 -r c97d6cd17314 main.cpp
--- a/main.cpp	Thu Dec 18 06:31:26 2014 +0000
+++ b/main.cpp	Thu Dec 18 10:56:52 2014 +0000
@@ -45,9 +45,9 @@
 Timer timer;
 long long offset = 0;
 
-DoubleBuffer<unsigned short, 20> light_sensor_buffer;
-DoubleBuffer<unsigned short, 250> adc_buffer;
-DoubleBuffer<long long, 100> d_event_buffer;
+DoubleBuffer<unsigned short, 50> light_sensor_buffer;
+DoubleBuffer<unsigned short, 500> adc_buffer;
+DoubleBuffer<long long, 30> d_event_buffer;
 
 //initialize variables
 Ticker ticker;
@@ -185,7 +185,7 @@
         // get data from the (2) light sensor (3) analog pin
         //let_there_be_light = my_light_sensor.read();
         unsigned short light_val = my_light_sensor.read_u16();
-        light_sensor_buffer.write(light_val);
+        light_sensor_buffer.write(light_val, ((long long) timer.read_us()) - offset);
         //my_analog_value = my_analog_pin.read();
             
         // print the analog values to uart
@@ -270,19 +270,19 @@
         case ADC_SAMP_FREQ_1HZ:
             if (tick_count % 10000 == 0) {
                 //read_adc();
-                adc_buffer.write(my_analog_pin.read_u16());
+                adc_buffer.write(my_analog_pin.read_u16(), ((long long) timer.read_us()) - offset);
             }
             break;
         case ADC_SAMP_FREQ_10HZ:
             if (tick_count % 1000 == 0) {
                 //read_adc();
-                adc_buffer.write(my_analog_pin.read_u16());
+                adc_buffer.write(my_analog_pin.read_u16(), ((long long) timer.read_us()) - offset);
             }
             break;
         case ADC_SAMP_FREQ_100HZ:
             if (tick_count % 100 == 0) {
                 //read_adc();
-                adc_buffer.write(my_analog_pin.read_u16());
+                adc_buffer.write(my_analog_pin.read_u16(), ((long long) timer.read_us()) - offset);
             }
             break;
         case ADC_SAMP_FREQ_OFF:
@@ -324,12 +324,14 @@
     light_sensor_buffer.swapBuff();
     unsigned short* sensor_data = light_sensor_buffer.getReadBuffer();
     int sensor_data_size = light_sensor_buffer.getReadBufferSize();
+    long long* ts = light_sensor_buffer.getTimestampReadBuffer();
     
     // send header
     pc.putc('l');
     send_int(sensor_data_size);
 
     for (int i = 0; i < sensor_data_size; i++) {
+        send_ull(ts[i]);
         send_us(sensor_data[i]);
     }
 }
@@ -338,12 +340,14 @@
     adc_buffer.swapBuff();
     unsigned short* adc_data = adc_buffer.getReadBuffer();
     int adc_data_size = adc_buffer.getReadBufferSize();
+    long long* ts = adc_buffer.getTimestampReadBuffer();
     
     // send header
     pc.putc('a');
     send_int(adc_data_size);
     
     for (int i = 0; i < adc_data_size; i++) {
+        send_ull(ts[i]);
         send_us(adc_data[i]);
     }
 }
@@ -374,7 +378,7 @@
 void d_event(){
     // take timestamp, send it to thingspeak later
     long long ts = timer.read_us() - offset;
-    d_event_buffer.write(ts);
+    d_event_buffer.write(ts, ts);
 }
 
 int sync_timestamp = 0;
@@ -393,6 +397,9 @@
 {
     redled = true; 
     timer.start();
+    light_sensor_buffer.init(&offset, &timer);
+    adc_buffer.init(&offset, &timer);
+    d_event_buffer.init(&offset, &timer);
     //initialize hardware
     systick_attach();
     d_event_pin.rise(&d_event);
@@ -490,7 +497,10 @@
         long long time_s = time_us / 1000000;
         long long time_m = time_s / 60;
         long long time_h = time_m / 60;
-        pc.printf("time: %lld:%lld:%lld:%lld\n",  time_h % 24, time_m % 60, time_s % 60, time_us % 1000000);
+        //pc.printf("time: %lld:%lld:%lld:%lld\n",  time_h % 24, time_m % 60, time_s % 60, time_us % 1000000);
+        long long* lsdt1 = light_sensor_buffer.db_timestamps[0];
+        long long* lsdt2 = light_sensor_buffer.db_timestamps[1];
+        pc.printf("lsd[0][0] %lld [0][1] %lld [0][2] %lld [1][0] %lld [1][1] %lld [1][2] %lld\n", lsdt1[0], lsdt1[1], lsdt1[2], lsdt2[0], lsdt2[1], lsdt2[2]);
         
         
         // detach the systick, then reattach at the right time