serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Revision:
5:4112d6243485
Parent:
4:635dcca9e11a
Child:
6:e19c2a022a95
--- a/main.cpp	Fri Mar 20 01:31:03 2015 +0000
+++ b/main.cpp	Fri Mar 20 03:24:25 2015 +0000
@@ -1,25 +1,6 @@
-/*
-
-Copyright (c) 2012-2014 RedBearLab
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
-and associated documentation files (the "Software"), to deal in the Software without restriction, 
-including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
-and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
-subject to the following conditions:
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
-
 #include "mbed.h"
 #include "BLEDevice.h"
-
+#include "circular_buffer.h"
 // Constants
 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
@@ -40,10 +21,11 @@
 #define POS_OFFSET_EN_PIN P0_29
 #define NEG_OFFSET_EN_PIN P0_28
 #define STATUS_LED_PIN P0_11
-#define RESET_PIN P0_09
+#define RESET_PIN P0_9
 
 #define COUPLING_SETTING 0x1
 #define OFFSET_SETTING 0x2
+#define OFFSET_DIR_SETTING 0x9
 #define TRIGGER_DIR_SETTING 0x3
 #define TRIGGER_VALUE_SETTING 0x4
 #define AVERAGING_SETTING 0x5
@@ -55,12 +37,15 @@
 #define DC_COUPLING 1
 #define NEG_EDGE 0
 #define POS_EDGE 1
+#define NEG_OFFSET 0
+#define POS_OFFSET 1
 
 // Global Settings
 bool coupling_type;  // 0 is AC, 1 is DC
 uint8_t offset_value;
 bool trigger_dir; // 0 is negative, 1 is positive
-uint8_t trigger_value;
+bool offset_dir; // 0 is negative, 1 is positive
+uint8_t trigger_level;
 uint8_t voltage_range;
 uint8_t averaging_amount;
 uint8_t time_shift_value;
@@ -72,7 +57,7 @@
 DigitalOut      AC_EN(AC_EN_PIN);
 DigitalOut      DC_EN(DC_EN_PIN);
 DigitalOut      PAIRING_LED(PAIRING_LED_PIN);
-AnalogIn        BYPASS_ADC(BYPASS_ADC_PIN);
+AnalogIn        ANALOG(BYPASS_ADC_PIN);
 DigitalIn       PARING_BUTTON(PAIRING_BUTTON_PIN);
 DigitalOut      NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
 DigitalOut      POS_OFFSET_EN(POS_OFFSET_EN_PIN);
@@ -82,9 +67,6 @@
 
 //Serial pc(USBTX, USBRX);
 
-static uint8_t analog_enabled = 0;
-static uint8_t old_state = 0;
-
 // The Nordic UART Service
 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
@@ -100,9 +82,9 @@
 
 
 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-                                      
+
 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-                                      
+
 //GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
 
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
@@ -116,69 +98,61 @@
 }
 
 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
-{   
+{
     uint8_t buf[TXRX_BUF_LEN];
     uint16_t bytesRead;
-    
-    if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) 
-    {
+
+    if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) {
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
         memset(txPayload, 0, TXRX_BUF_LEN);
-        memcpy(txPayload, buf, TXRX_BUF_LEN);       
-        
+        memcpy(txPayload, buf, TXRX_BUF_LEN);
+
         //for(index=0; index<bytesRead; index++)
-            //pc.putc(buf[index]);
-            
-        if(buf[0] == COUPLING_SETTING) {  
+        //pc.putc(buf[index]);
+
+        if(buf[0] == COUPLING_SETTING) {
             coupling_type = buf[1];
-            if (coupling_type = AC_COUPLING) {
+            if (coupling_type == AC_COUPLING) {
                 AC_EN = 1;
                 DC_EN = 0;
             } else // DC coupling
                 AC_EN = 0;
-                DC_EN = 1;
-            }
-        }
-        else if(buf[0] == TRIGGER_DIR_SETTING) {
-            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
+            DC_EN = 1;
         }
-        else if(buf[0] == TIME_SHIFT_SETTING) {
-            time_shift_value = buf[1];
-        }
-        else if(buf[0] == TIME_RANGE_SETTING) {
-            time_range_value = buf[1];
+    } else if(buf[0] == TRIGGER_DIR_SETTING) {
+        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) {
+        time_range_value = buf[1];
+    } else if(buf[0] == OFFSET_SETTING) {
+        offset_value = buf[1];
+        if (offset_dir == POS_OFFSET) { // postitive offset
+            NEG_OFFSET_EN = 0;
+            POS_OFFSET_EN = 1;
+        } else { // negative offset
+            NEG_OFFSET_EN = 1;
+            POS_OFFSET_EN = 0;
         }
-        else if(buf[0] == OFFSET_SETTING) {
-            offset_value = buf[1];
-            if (offset_value >= 0) { // postitive offset
-                NEG_OFFSET_EN = 0;
-                POS_OFFSET_EN = 1;
-            } else { // negative offset
-                NEG_OFFSET_EN = 1;
-                POS_OFFSET_EN = 0;
-            }
-            // i2c write to DAC here 
-        }
-
+        // i2c write to DAC here
+    } else if(buf[0] == TRIGGER_VALUE_SETTING) {
+        trigger_level = buf[1];
     }
 }
 /*
 void uartCB(void)
-{   
-    while(pc.readable())    
+{
+    while(pc.readable())
     {
-        rx_buf[rx_len++] = pc.getc();    
+        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); 
+            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
             pc.printf("RecHandler \r\n");
             pc.printf("Length: ");
             pc.putc(rx_len);
@@ -190,58 +164,63 @@
 }
 */
 
-
-uint16_t index;
-uint8_t voltage_buffer;
-
+#define VOLTAGE_BUFFER_SIZE 20
+circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE);
+uint8_t out_buf[VOLTAGE_BUFFER_SIZE];
 void m_status_check_handle(void)
-{   
-    uint8_t buf[3];
-    if (analog_enabled)  // if analog reading enabled
-    {
-        // Read and send out
-        float s = ANALOG;
-        uint16_t value = s*1024; 
-        buf[0] = (0x0B);
-        buf[1] = (value >> 8);
-        buf[2] = (value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
+{
+    static uint8_t time_shift = 0;
+    static uint8_t prev_sample = 0;
+    // Read and send out
+    float s = ANALOG;
+    uint8_t sample = s*255;
+    voltage_buffer.push_back(sample);
+
+    if (sample > trigger_level && prev_sample < trigger_level) {
+        //if (time_shift < time_shift_value) {
+        //}
+        for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
+            out_buf[VOLTAGE_BUFFER_SIZE] = voltage_buffer.front();
+            voltage_buffer.pop_front();
+        }
+        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);  
-    
+    ble.onDataWritten(WrittenHandler);
+
     //pc.baud(9600);
     //pc.printf("SimpleChat Init \r\n");
 
     //pc.attach( uartCB , pc.RxIrq);
-    
-    // setup advertising 
+
+    // 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);
+                                     (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. 
+                                     (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(); 
-    
+
+    ble.startAdvertising();
+
     //pc.printf("Advertising Start \r\n");
-    
-    while(1)
-    {
-        ble.waitForEvent(); 
+
+    while(1) {
+        ble.waitForEvent();
     }
 }