serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Revision:
8:7a3b7d1c8393
Parent:
6:e19c2a022a95
Child:
9:b527643b8c93
--- a/main.cpp	Mon Mar 23 21:36:32 2015 +0000
+++ b/main.cpp	Tue Mar 24 02:41:05 2015 +0000
@@ -10,7 +10,6 @@
 
 //#define DIGITAL_OUT_PIN                  P0_9       //TXD
 //#define DIGITAL_IN_PIN                   P0_10      //CTS
-//#define PWM_PIN                          P0_11      //RXD
 //#define ANALOG_IN_PIN                    P0_4       //P04
 
 #define AC_EN_PIN   P0_19 //FIXFIXFIXTHIS back // P0_4
@@ -35,8 +34,8 @@
 
 #define AC_COUPLING 0
 #define DC_COUPLING 1
-#define NEG_EDGE 0
-#define POS_EDGE 1
+#define FALLING_EDGE 0
+#define RISING_EDGE 1
 #define NEG_OFFSET 0
 #define POS_OFFSET 1
 
@@ -62,8 +61,6 @@
 DigitalOut      NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
 DigitalOut      POS_OFFSET_EN(POS_OFFSET_EN_PIN);
 DigitalOut      STATUS_LED(STATUS_LED_PIN);
-PWMOut          OFFSET_DAC(RESET_PIN);
-
 
 //Serial pc(USBTX, USBRX);
 
@@ -123,9 +120,6 @@
         trigger_dir = buf[1];
     } else if(buf[0] == AVERAGING_SETTING) {
         averaging_amount = buf[0];
-    } else if(buf[0] == VOLTAGE_RANGE_SETTING) {
-        voltage_range = buf[1];
-        // i2c write to DAC here
     } else if(buf[0] == TIME_SHIFT_SETTING) {
         time_shift_value = buf[1];
     } else if(buf[0] == TIME_RANGE_SETTING) {
@@ -139,36 +133,35 @@
             NEG_OFFSET_EN = 1;
             POS_OFFSET_EN = 0;
         }
-        // i2c write to DAC here
-        OFFSET_DAC = offset_value/255.0;
     } else if(buf[0] == TRIGGER_VALUE_SETTING) {
         trigger_level = buf[1];
     }
 }
-/*
-void uartCB(void)
+
+bool is_rising_edge (uint8_t current_sample, uint8_t previous_sample)
+{
+    return (current_sample > trigger_level && previous_sample < trigger_level);
+}
+
+bool is_falling_edge (uint8_t current_sample, uint8_t previous_sample)
 {
-    while(pc.readable())
-    {
-        rx_buf[rx_len++] = pc.getc();
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
-        {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
-        }
+    return (current_sample > trigger_level && previous_sample < trigger_level);
+}
+
+bool is_triggered (uint8_t current_sample, uint8_t previous_sample, bool dir) {
+    if (dir == RISING_EDGE) {
+        return is_rising_edge (current_sample, previous_sample);
+    } else {
+        return is_falling_edge (current_sample, previous_sample);
     }
 }
-*/
 
 #define VOLTAGE_BUFFER_SIZE 1024
 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE);
 uint8_t out_buf[VOLTAGE_BUFFER_SIZE];
-void m_status_check_handle(void) {
+void m_status_check_handle(void)
+{
+    static bool is_shifting = false;
     static uint8_t time_shift = 0;
     static uint8_t prev_sample = 0;
     // Read and send out
@@ -176,56 +169,58 @@
     uint8_t sample = s*255;
     voltage_buffer.push_back(sample);
 
-    if (sample > trigger_level && prev_sample < trigger_level && voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE/*only do if buffer fills up*/)  {
-        if (time_shift < time_shift_value) {
-            time_shift++;
-        } else {
-            time_shift = 0;
-            for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
-                out_buf[i] = voltage_buffer.front();
-                voltage_buffer.pop_front();
+    if ((is_shifting) || (voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir))) {
+        if (is_rising_edge(sample, prev_sample) || is_falling_edge(sample, prev_sample))  {
+            if (time_shift < time_shift_value) {
+                is_shifting = true;
+                time_shift++;
+            } else {
+                is_shifting = false;
+                time_shift = 0;
+                for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
+                    out_buf[i] = voltage_buffer.front();
+                    voltage_buffer.pop_front();
+                }
+                ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, VOLTAGE_BUFFER_SIZE);
             }
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, VOLTAGE_BUFFER_SIZE);
+        }
+        prev_sample = sample;
+    }
+    }
+
+    int main(void) {
+        Ticker ticker;
+        ticker.attach_us(m_status_check_handle, 1000);
+
+        ble.init();
+        ble.onDisconnection(disconnectionCallback);
+        ble.onDataWritten(WrittenHandler);
+
+        //pc.baud(9600);
+        //pc.printf("SimpleChat Init \r\n");
+
+        //pc.attach( uartCB , pc.RxIrq);
+
+        // setup advertising
+        //ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+        ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                         (const uint8_t *)"Serqet", sizeof("Serqet") - 1);
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                         (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+        // 100ms; in multiples of 0.625ms.
+        ble.setAdvertisingInterval(160);
+
+        ble.addService(uartService);
+
+        ble.startAdvertising();
+
+        //pc.printf("Advertising Start \r\n");
+
+        while(1) {
+            ble.waitForEvent();
         }
     }
-    prev_sample = sample;
-}
-
-int main(void)
-{
-    OFFSET_DAC.period_us(100);
-    Ticker ticker;
-    ticker.attach_us(m_status_check_handle, 1000);
-
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(WrittenHandler);
-
-    //pc.baud(9600);
-    //pc.printf("SimpleChat Init \r\n");
-
-    //pc.attach( uartCB , pc.RxIrq);
-
-    // setup advertising
-    //ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"Serqet", sizeof("Serqet") - 1);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
-    // 100ms; in multiples of 0.625ms.
-    ble.setAdvertisingInterval(160);
-
-    ble.addService(uartService);
-
-    ble.startAdvertising();
-
-    //pc.printf("Advertising Start \r\n");
-
-    while(1) {
-        ble.waitForEvent();
-    }
-}