serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Revision:
6:e19c2a022a95
Parent:
5:4112d6243485
Child:
8:7a3b7d1c8393
--- a/main.cpp	Fri Mar 20 03:24:25 2015 +0000
+++ b/main.cpp	Mon Mar 23 21:36:32 2015 +0000
@@ -13,7 +13,7 @@
 //#define PWM_PIN                          P0_11      //RXD
 //#define ANALOG_IN_PIN                    P0_4       //P04
 
-#define AC_EN_PIN   P0_4
+#define AC_EN_PIN   P0_19 //FIXFIXFIXTHIS back // P0_4
 #define DC_EN_PIN   P0_5
 #define PAIRING_LED_PIN P0_7
 #define BYPASS_ADC_PIN P0_6
@@ -41,15 +41,15 @@
 #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
-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;
-uint8_t time_range_value;
+bool coupling_type = 1;  // 0 is AC, 1 is DC
+uint8_t offset_value = 0;
+bool trigger_dir = 1; // 0 is negative, 1 is positive
+bool offset_dir = 1; // 0 is negative, 1 is positive
+uint8_t trigger_level = 127;
+uint8_t voltage_range = 1;
+uint8_t averaging_amount = 1;
+uint8_t time_shift_value = 0;
+uint8_t time_range_value = 0;
 
 BLEDevice  ble;
 
@@ -62,7 +62,7 @@
 DigitalOut      NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
 DigitalOut      POS_OFFSET_EN(POS_OFFSET_EN_PIN);
 DigitalOut      STATUS_LED(STATUS_LED_PIN);
-DigitalOut      RESET(RESET_PIN);
+PWMOut          OFFSET_DAC(RESET_PIN);
 
 
 //Serial pc(USBTX, USBRX);
@@ -85,7 +85,7 @@
 
 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
 
-//GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
+GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
 
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
@@ -140,6 +140,7 @@
             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];
     }
@@ -164,11 +165,10 @@
 }
 */
 
-#define VOLTAGE_BUFFER_SIZE 20
+#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 uint8_t time_shift = 0;
     static uint8_t prev_sample = 0;
     // Read and send out
@@ -176,21 +176,24 @@
     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();
+    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();
+            }
+            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)
 {
+    OFFSET_DAC.period_us(100);
     Ticker ticker;
     ticker.attach_us(m_status_check_handle, 1000);
 
@@ -204,7 +207,7 @@
     //pc.attach( uartCB , pc.RxIrq);
 
     // setup advertising
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    //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);