serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Committer:
aadavids
Date:
Tue Mar 24 02:41:05 2015 +0000
Revision:
8:7a3b7d1c8393
Parent:
6:e19c2a022a95
Child:
9:b527643b8c93
fixed trigering logic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:be2e4095513a 1 #include "mbed.h"
RedBearLab 0:be2e4095513a 2 #include "BLEDevice.h"
aadavids 5:4112d6243485 3 #include "circular_buffer.h"
aadavids 4:635dcca9e11a 4 // Constants
RedBearLab 0:be2e4095513a 5 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:be2e4095513a 6 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:be2e4095513a 7 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:be2e4095513a 8
RedBearLab 0:be2e4095513a 9 #define TXRX_BUF_LEN 20
RedBearLab 0:be2e4095513a 10
aadavids 3:6c83be282aac 11 //#define DIGITAL_OUT_PIN P0_9 //TXD
aadavids 3:6c83be282aac 12 //#define DIGITAL_IN_PIN P0_10 //CTS
aadavids 3:6c83be282aac 13 //#define ANALOG_IN_PIN P0_4 //P04
aadavids 3:6c83be282aac 14
aadavids 6:e19c2a022a95 15 #define AC_EN_PIN P0_19 //FIXFIXFIXTHIS back // P0_4
aadavids 3:6c83be282aac 16 #define DC_EN_PIN P0_5
aadavids 3:6c83be282aac 17 #define PAIRING_LED_PIN P0_7
aadavids 3:6c83be282aac 18 #define BYPASS_ADC_PIN P0_6
aadavids 3:6c83be282aac 19 #define PAIRING_BUTTON_PIN P0_15
aadavids 3:6c83be282aac 20 #define POS_OFFSET_EN_PIN P0_29
aadavids 3:6c83be282aac 21 #define NEG_OFFSET_EN_PIN P0_28
aadavids 3:6c83be282aac 22 #define STATUS_LED_PIN P0_11
aadavids 5:4112d6243485 23 #define RESET_PIN P0_9
RedBearLab 0:be2e4095513a 24
aadavids 4:635dcca9e11a 25 #define COUPLING_SETTING 0x1
aadavids 4:635dcca9e11a 26 #define OFFSET_SETTING 0x2
aadavids 5:4112d6243485 27 #define OFFSET_DIR_SETTING 0x9
aadavids 4:635dcca9e11a 28 #define TRIGGER_DIR_SETTING 0x3
aadavids 4:635dcca9e11a 29 #define TRIGGER_VALUE_SETTING 0x4
aadavids 4:635dcca9e11a 30 #define AVERAGING_SETTING 0x5
aadavids 4:635dcca9e11a 31 #define VOLTAGE_RANGE_SETTING 0x6
aadavids 4:635dcca9e11a 32 #define TIME_SHIFT_SETTING 0x7
aadavids 4:635dcca9e11a 33 #define TIME_RANGE_SETTING 0x8
aadavids 2:ddf1f6325a37 34
aadavids 4:635dcca9e11a 35 #define AC_COUPLING 0
aadavids 4:635dcca9e11a 36 #define DC_COUPLING 1
aadavids 8:7a3b7d1c8393 37 #define FALLING_EDGE 0
aadavids 8:7a3b7d1c8393 38 #define RISING_EDGE 1
aadavids 5:4112d6243485 39 #define NEG_OFFSET 0
aadavids 5:4112d6243485 40 #define POS_OFFSET 1
aadavids 4:635dcca9e11a 41
aadavids 4:635dcca9e11a 42 // Global Settings
aadavids 6:e19c2a022a95 43 bool coupling_type = 1; // 0 is AC, 1 is DC
aadavids 6:e19c2a022a95 44 uint8_t offset_value = 0;
aadavids 6:e19c2a022a95 45 bool trigger_dir = 1; // 0 is negative, 1 is positive
aadavids 6:e19c2a022a95 46 bool offset_dir = 1; // 0 is negative, 1 is positive
aadavids 6:e19c2a022a95 47 uint8_t trigger_level = 127;
aadavids 6:e19c2a022a95 48 uint8_t voltage_range = 1;
aadavids 6:e19c2a022a95 49 uint8_t averaging_amount = 1;
aadavids 6:e19c2a022a95 50 uint8_t time_shift_value = 0;
aadavids 6:e19c2a022a95 51 uint8_t time_range_value = 0;
aadavids 2:ddf1f6325a37 52
RedBearLab 0:be2e4095513a 53 BLEDevice ble;
RedBearLab 0:be2e4095513a 54
aadavids 4:635dcca9e11a 55 // Pins declaration
aadavids 4:635dcca9e11a 56 DigitalOut AC_EN(AC_EN_PIN);
aadavids 4:635dcca9e11a 57 DigitalOut DC_EN(DC_EN_PIN);
aadavids 4:635dcca9e11a 58 DigitalOut PAIRING_LED(PAIRING_LED_PIN);
aadavids 5:4112d6243485 59 AnalogIn ANALOG(BYPASS_ADC_PIN);
aadavids 4:635dcca9e11a 60 DigitalIn PARING_BUTTON(PAIRING_BUTTON_PIN);
aadavids 4:635dcca9e11a 61 DigitalOut NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
aadavids 4:635dcca9e11a 62 DigitalOut POS_OFFSET_EN(POS_OFFSET_EN_PIN);
aadavids 4:635dcca9e11a 63 DigitalOut STATUS_LED(STATUS_LED_PIN);
RedBearLab 0:be2e4095513a 64
RedBearLab 0:be2e4095513a 65 //Serial pc(USBTX, USBRX);
RedBearLab 0:be2e4095513a 66
RedBearLab 0:be2e4095513a 67 // The Nordic UART Service
RedBearLab 0:be2e4095513a 68 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 69 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 70 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:be2e4095513a 71 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:be2e4095513a 72
RedBearLab 0:be2e4095513a 73
RedBearLab 0:be2e4095513a 74 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 75 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 76
RedBearLab 0:be2e4095513a 77 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 78 //static uint8_t rx_len=0;
RedBearLab 0:be2e4095513a 79
RedBearLab 0:be2e4095513a 80
RedBearLab 0:be2e4095513a 81 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
aadavids 5:4112d6243485 82
RedBearLab 0:be2e4095513a 83 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
aadavids 5:4112d6243485 84
aadavids 6:e19c2a022a95 85 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 86
RedBearLab 0:be2e4095513a 87 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 88
RedBearLab 0:be2e4095513a 89
RedBearLab 0:be2e4095513a 90 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:be2e4095513a 91 {
RedBearLab 0:be2e4095513a 92 //pc.printf("Disconnected \r\n");
RedBearLab 0:be2e4095513a 93 //pc.printf("Restart advertising \r\n");
RedBearLab 0:be2e4095513a 94 ble.startAdvertising();
RedBearLab 0:be2e4095513a 95 }
RedBearLab 0:be2e4095513a 96
RedBearLab 0:be2e4095513a 97 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
aadavids 5:4112d6243485 98 {
RedBearLab 0:be2e4095513a 99 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 100 uint16_t bytesRead;
aadavids 5:4112d6243485 101
aadavids 5:4112d6243485 102 if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) {
RedBearLab 0:be2e4095513a 103 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 104 memset(txPayload, 0, TXRX_BUF_LEN);
aadavids 5:4112d6243485 105 memcpy(txPayload, buf, TXRX_BUF_LEN);
aadavids 5:4112d6243485 106
RedBearLab 0:be2e4095513a 107 //for(index=0; index<bytesRead; index++)
aadavids 5:4112d6243485 108 //pc.putc(buf[index]);
aadavids 5:4112d6243485 109
aadavids 5:4112d6243485 110 if(buf[0] == COUPLING_SETTING) {
aadavids 4:635dcca9e11a 111 coupling_type = buf[1];
aadavids 5:4112d6243485 112 if (coupling_type == AC_COUPLING) {
aadavids 4:635dcca9e11a 113 AC_EN = 1;
aadavids 4:635dcca9e11a 114 DC_EN = 0;
aadavids 4:635dcca9e11a 115 } else // DC coupling
aadavids 4:635dcca9e11a 116 AC_EN = 0;
aadavids 5:4112d6243485 117 DC_EN = 1;
aadavids 4:635dcca9e11a 118 }
aadavids 5:4112d6243485 119 } else if(buf[0] == TRIGGER_DIR_SETTING) {
aadavids 5:4112d6243485 120 trigger_dir = buf[1];
aadavids 5:4112d6243485 121 } else if(buf[0] == AVERAGING_SETTING) {
aadavids 5:4112d6243485 122 averaging_amount = buf[0];
aadavids 5:4112d6243485 123 } else if(buf[0] == TIME_SHIFT_SETTING) {
aadavids 5:4112d6243485 124 time_shift_value = buf[1];
aadavids 5:4112d6243485 125 } else if(buf[0] == TIME_RANGE_SETTING) {
aadavids 5:4112d6243485 126 time_range_value = buf[1];
aadavids 5:4112d6243485 127 } else if(buf[0] == OFFSET_SETTING) {
aadavids 5:4112d6243485 128 offset_value = buf[1];
aadavids 5:4112d6243485 129 if (offset_dir == POS_OFFSET) { // postitive offset
aadavids 5:4112d6243485 130 NEG_OFFSET_EN = 0;
aadavids 5:4112d6243485 131 POS_OFFSET_EN = 1;
aadavids 5:4112d6243485 132 } else { // negative offset
aadavids 5:4112d6243485 133 NEG_OFFSET_EN = 1;
aadavids 5:4112d6243485 134 POS_OFFSET_EN = 0;
RedBearLab 0:be2e4095513a 135 }
aadavids 5:4112d6243485 136 } else if(buf[0] == TRIGGER_VALUE_SETTING) {
aadavids 5:4112d6243485 137 trigger_level = buf[1];
RedBearLab 0:be2e4095513a 138 }
RedBearLab 0:be2e4095513a 139 }
aadavids 8:7a3b7d1c8393 140
aadavids 8:7a3b7d1c8393 141 bool is_rising_edge (uint8_t current_sample, uint8_t previous_sample)
aadavids 8:7a3b7d1c8393 142 {
aadavids 8:7a3b7d1c8393 143 return (current_sample > trigger_level && previous_sample < trigger_level);
aadavids 8:7a3b7d1c8393 144 }
aadavids 8:7a3b7d1c8393 145
aadavids 8:7a3b7d1c8393 146 bool is_falling_edge (uint8_t current_sample, uint8_t previous_sample)
aadavids 5:4112d6243485 147 {
aadavids 8:7a3b7d1c8393 148 return (current_sample > trigger_level && previous_sample < trigger_level);
aadavids 8:7a3b7d1c8393 149 }
aadavids 8:7a3b7d1c8393 150
aadavids 8:7a3b7d1c8393 151 bool is_triggered (uint8_t current_sample, uint8_t previous_sample, bool dir) {
aadavids 8:7a3b7d1c8393 152 if (dir == RISING_EDGE) {
aadavids 8:7a3b7d1c8393 153 return is_rising_edge (current_sample, previous_sample);
aadavids 8:7a3b7d1c8393 154 } else {
aadavids 8:7a3b7d1c8393 155 return is_falling_edge (current_sample, previous_sample);
RedBearLab 0:be2e4095513a 156 }
RedBearLab 0:be2e4095513a 157 }
aadavids 4:635dcca9e11a 158
aadavids 6:e19c2a022a95 159 #define VOLTAGE_BUFFER_SIZE 1024
aadavids 5:4112d6243485 160 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE);
aadavids 5:4112d6243485 161 uint8_t out_buf[VOLTAGE_BUFFER_SIZE];
aadavids 8:7a3b7d1c8393 162 void m_status_check_handle(void)
aadavids 8:7a3b7d1c8393 163 {
aadavids 8:7a3b7d1c8393 164 static bool is_shifting = false;
aadavids 5:4112d6243485 165 static uint8_t time_shift = 0;
aadavids 5:4112d6243485 166 static uint8_t prev_sample = 0;
aadavids 5:4112d6243485 167 // Read and send out
aadavids 5:4112d6243485 168 float s = ANALOG;
aadavids 5:4112d6243485 169 uint8_t sample = s*255;
aadavids 5:4112d6243485 170 voltage_buffer.push_back(sample);
aadavids 5:4112d6243485 171
aadavids 8:7a3b7d1c8393 172 if ((is_shifting) || (voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir))) {
aadavids 8:7a3b7d1c8393 173 if (is_rising_edge(sample, prev_sample) || is_falling_edge(sample, prev_sample)) {
aadavids 8:7a3b7d1c8393 174 if (time_shift < time_shift_value) {
aadavids 8:7a3b7d1c8393 175 is_shifting = true;
aadavids 8:7a3b7d1c8393 176 time_shift++;
aadavids 8:7a3b7d1c8393 177 } else {
aadavids 8:7a3b7d1c8393 178 is_shifting = false;
aadavids 8:7a3b7d1c8393 179 time_shift = 0;
aadavids 8:7a3b7d1c8393 180 for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
aadavids 8:7a3b7d1c8393 181 out_buf[i] = voltage_buffer.front();
aadavids 8:7a3b7d1c8393 182 voltage_buffer.pop_front();
aadavids 8:7a3b7d1c8393 183 }
aadavids 8:7a3b7d1c8393 184 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, VOLTAGE_BUFFER_SIZE);
aadavids 6:e19c2a022a95 185 }
aadavids 8:7a3b7d1c8393 186 }
aadavids 8:7a3b7d1c8393 187 prev_sample = sample;
aadavids 8:7a3b7d1c8393 188 }
aadavids 8:7a3b7d1c8393 189 }
aadavids 8:7a3b7d1c8393 190
aadavids 8:7a3b7d1c8393 191 int main(void) {
aadavids 8:7a3b7d1c8393 192 Ticker ticker;
aadavids 8:7a3b7d1c8393 193 ticker.attach_us(m_status_check_handle, 1000);
aadavids 8:7a3b7d1c8393 194
aadavids 8:7a3b7d1c8393 195 ble.init();
aadavids 8:7a3b7d1c8393 196 ble.onDisconnection(disconnectionCallback);
aadavids 8:7a3b7d1c8393 197 ble.onDataWritten(WrittenHandler);
aadavids 8:7a3b7d1c8393 198
aadavids 8:7a3b7d1c8393 199 //pc.baud(9600);
aadavids 8:7a3b7d1c8393 200 //pc.printf("SimpleChat Init \r\n");
aadavids 8:7a3b7d1c8393 201
aadavids 8:7a3b7d1c8393 202 //pc.attach( uartCB , pc.RxIrq);
aadavids 8:7a3b7d1c8393 203
aadavids 8:7a3b7d1c8393 204 // setup advertising
aadavids 8:7a3b7d1c8393 205 //ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
aadavids 8:7a3b7d1c8393 206 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
aadavids 8:7a3b7d1c8393 207 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
aadavids 8:7a3b7d1c8393 208 (const uint8_t *)"Serqet", sizeof("Serqet") - 1);
aadavids 8:7a3b7d1c8393 209 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
aadavids 8:7a3b7d1c8393 210 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
aadavids 8:7a3b7d1c8393 211 // 100ms; in multiples of 0.625ms.
aadavids 8:7a3b7d1c8393 212 ble.setAdvertisingInterval(160);
aadavids 8:7a3b7d1c8393 213
aadavids 8:7a3b7d1c8393 214 ble.addService(uartService);
aadavids 8:7a3b7d1c8393 215
aadavids 8:7a3b7d1c8393 216 ble.startAdvertising();
aadavids 8:7a3b7d1c8393 217
aadavids 8:7a3b7d1c8393 218 //pc.printf("Advertising Start \r\n");
aadavids 8:7a3b7d1c8393 219
aadavids 8:7a3b7d1c8393 220 while(1) {
aadavids 8:7a3b7d1c8393 221 ble.waitForEvent();
aadavids 5:4112d6243485 222 }
RedBearLab 0:be2e4095513a 223 }
RedBearLab 0:be2e4095513a 224
RedBearLab 0:be2e4095513a 225
RedBearLab 0:be2e4095513a 226
RedBearLab 0:be2e4095513a 227
RedBearLab 0:be2e4095513a 228
RedBearLab 0:be2e4095513a 229
RedBearLab 0:be2e4095513a 230
RedBearLab 0:be2e4095513a 231
RedBearLab 0:be2e4095513a 232
RedBearLab 0:be2e4095513a 233
RedBearLab 0:be2e4095513a 234
RedBearLab 0:be2e4095513a 235
RedBearLab 0:be2e4095513a 236
RedBearLab 0:be2e4095513a 237
RedBearLab 0:be2e4095513a 238
RedBearLab 0:be2e4095513a 239
RedBearLab 0:be2e4095513a 240
RedBearLab 0:be2e4095513a 241
RedBearLab 0:be2e4095513a 242
RedBearLab 0:be2e4095513a 243
RedBearLab 0:be2e4095513a 244
RedBearLab 0:be2e4095513a 245
RedBearLab 0:be2e4095513a 246
RedBearLab 0:be2e4095513a 247
RedBearLab 0:be2e4095513a 248
RedBearLab 0:be2e4095513a 249
RedBearLab 0:be2e4095513a 250
RedBearLab 0:be2e4095513a 251
RedBearLab 0:be2e4095513a 252
RedBearLab 0:be2e4095513a 253
RedBearLab 0:be2e4095513a 254