serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Committer:
andrewadavidson
Date:
Tue Mar 24 22:33:50 2015 +0000
Revision:
10:a9db6fc55f1a
Parent:
9:b527643b8c93
Child:
11:d54f92efbe70
almost working;

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
andrewadavidson 10:a9db6fc55f1a 18 #define BYPASS_ADC_PIN P0_4
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;
kmhiemst 9:b527643b8c93 52 bool trigger_sign = 1; // 0 is negative, 1 is positive
aadavids 2:ddf1f6325a37 53
RedBearLab 0:be2e4095513a 54 BLEDevice ble;
RedBearLab 0:be2e4095513a 55
aadavids 4:635dcca9e11a 56 // Pins declaration
aadavids 4:635dcca9e11a 57 DigitalOut AC_EN(AC_EN_PIN);
aadavids 4:635dcca9e11a 58 DigitalOut DC_EN(DC_EN_PIN);
aadavids 4:635dcca9e11a 59 DigitalOut PAIRING_LED(PAIRING_LED_PIN);
aadavids 5:4112d6243485 60 AnalogIn ANALOG(BYPASS_ADC_PIN);
aadavids 4:635dcca9e11a 61 DigitalIn PARING_BUTTON(PAIRING_BUTTON_PIN);
aadavids 4:635dcca9e11a 62 DigitalOut NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
aadavids 4:635dcca9e11a 63 DigitalOut POS_OFFSET_EN(POS_OFFSET_EN_PIN);
aadavids 4:635dcca9e11a 64 DigitalOut STATUS_LED(STATUS_LED_PIN);
RedBearLab 0:be2e4095513a 65
kmhiemst 9:b527643b8c93 66 Serial pc(USBTX, USBRX);
RedBearLab 0:be2e4095513a 67
andrewadavidson 10:a9db6fc55f1a 68 void read_analog_handle(void);
andrewadavidson 10:a9db6fc55f1a 69
RedBearLab 0:be2e4095513a 70 // The Nordic UART Service
RedBearLab 0:be2e4095513a 71 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 72 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 73 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 74 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 75
RedBearLab 0:be2e4095513a 76
RedBearLab 0:be2e4095513a 77 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 78 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:be2e4095513a 79
RedBearLab 0:be2e4095513a 80 //static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 81 //static uint8_t rx_len=0;
RedBearLab 0:be2e4095513a 82
andrewadavidson 10:a9db6fc55f1a 83 Ticker ticker;
RedBearLab 0:be2e4095513a 84
RedBearLab 0:be2e4095513a 85 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 86
RedBearLab 0:be2e4095513a 87 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
aadavids 5:4112d6243485 88
aadavids 6:e19c2a022a95 89 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:be2e4095513a 90
RedBearLab 0:be2e4095513a 91 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:be2e4095513a 92
RedBearLab 0:be2e4095513a 93
RedBearLab 0:be2e4095513a 94 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:be2e4095513a 95 {
kmhiemst 9:b527643b8c93 96 pc.printf("Disconnected \r\n");
kmhiemst 9:b527643b8c93 97 pc.printf("Restart advertising \r\n");
RedBearLab 0:be2e4095513a 98 ble.startAdvertising();
RedBearLab 0:be2e4095513a 99 }
RedBearLab 0:be2e4095513a 100
RedBearLab 0:be2e4095513a 101 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
aadavids 5:4112d6243485 102 {
RedBearLab 0:be2e4095513a 103 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:be2e4095513a 104 uint16_t bytesRead;
aadavids 5:4112d6243485 105
aadavids 5:4112d6243485 106 if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) {
RedBearLab 0:be2e4095513a 107 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:be2e4095513a 108 memset(txPayload, 0, TXRX_BUF_LEN);
aadavids 5:4112d6243485 109 memcpy(txPayload, buf, TXRX_BUF_LEN);
aadavids 5:4112d6243485 110
RedBearLab 0:be2e4095513a 111 //for(index=0; index<bytesRead; index++)
aadavids 5:4112d6243485 112 //pc.putc(buf[index]);
andrewadavidson 10:a9db6fc55f1a 113
kmhiemst 9:b527643b8c93 114 coupling_type = (bool) buf[0];
aadavids 5:4112d6243485 115 offset_value = buf[1];
kmhiemst 9:b527643b8c93 116 trigger_dir = (bool) buf[2];
kmhiemst 9:b527643b8c93 117 trigger_level = buf[3];
kmhiemst 9:b527643b8c93 118 averaging_amount = buf[4];
kmhiemst 9:b527643b8c93 119 voltage_range = buf[5];
kmhiemst 9:b527643b8c93 120 time_shift_value = buf[6];
kmhiemst 9:b527643b8c93 121 time_range_value = buf[7];
kmhiemst 9:b527643b8c93 122 offset_dir = (bool) buf[8];
kmhiemst 9:b527643b8c93 123 trigger_sign = (bool) buf[9];
RedBearLab 0:be2e4095513a 124 }
RedBearLab 0:be2e4095513a 125 }
aadavids 8:7a3b7d1c8393 126
aadavids 8:7a3b7d1c8393 127 bool is_rising_edge (uint8_t current_sample, uint8_t previous_sample)
aadavids 8:7a3b7d1c8393 128 {
aadavids 8:7a3b7d1c8393 129 return (current_sample > trigger_level && previous_sample < trigger_level);
aadavids 8:7a3b7d1c8393 130 }
aadavids 8:7a3b7d1c8393 131
aadavids 8:7a3b7d1c8393 132 bool is_falling_edge (uint8_t current_sample, uint8_t previous_sample)
aadavids 5:4112d6243485 133 {
aadavids 8:7a3b7d1c8393 134 return (current_sample > trigger_level && previous_sample < trigger_level);
aadavids 8:7a3b7d1c8393 135 }
aadavids 8:7a3b7d1c8393 136
kmhiemst 9:b527643b8c93 137 bool is_triggered (uint8_t current_sample, uint8_t previous_sample, bool dir)
kmhiemst 9:b527643b8c93 138 {
andrewadavidson 10:a9db6fc55f1a 139 // if (dir == RISING_EDGE) {
andrewadavidson 10:a9db6fc55f1a 140 // return is_rising_edge (current_sample, previous_sample);
andrewadavidson 10:a9db6fc55f1a 141 // } else {
andrewadavidson 10:a9db6fc55f1a 142 // return is_falling_edge (current_sample, previous_sample);
andrewadavidson 10:a9db6fc55f1a 143 // }
andrewadavidson 10:a9db6fc55f1a 144 // pc.printf("trigger test: current: %d, prev: %d, level: %d, result: %s\n\r", current_sample, previous_sample, trigger_level, (current_sample > trigger_level && previous_sample < trigger_level)?"true":"false");
andrewadavidson 10:a9db6fc55f1a 145 return (current_sample > trigger_level && previous_sample < trigger_level);
RedBearLab 0:be2e4095513a 146 }
aadavids 4:635dcca9e11a 147
kmhiemst 9:b527643b8c93 148 void m_test_handle(void)
kmhiemst 9:b527643b8c93 149 {
kmhiemst 9:b527643b8c93 150 uint8_t buf[20];
kmhiemst 9:b527643b8c93 151 for (int i = 0; i < 20; i++) {
kmhiemst 9:b527643b8c93 152 buf[i] = (uint8_t)(sin(3.14*i/20)*255);
kmhiemst 9:b527643b8c93 153 }
kmhiemst 9:b527643b8c93 154 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 20);
kmhiemst 9:b527643b8c93 155
kmhiemst 9:b527643b8c93 156 }
kmhiemst 9:b527643b8c93 157
andrewadavidson 10:a9db6fc55f1a 158 #define VOLTAGE_BUFFER_SIZE 512
aadavids 5:4112d6243485 159 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE);
andrewadavidson 10:a9db6fc55f1a 160 uint8_t out_buf[TXRX_BUF_LEN];
andrewadavidson 10:a9db6fc55f1a 161
andrewadavidson 10:a9db6fc55f1a 162 void send_buffer_handle(void)
andrewadavidson 10:a9db6fc55f1a 163 {
andrewadavidson 10:a9db6fc55f1a 164 for (int i = 0; i < TXRX_BUF_LEN || voltage_buffer.get_size() == 0; i++) {
andrewadavidson 10:a9db6fc55f1a 165 out_buf[i] = voltage_buffer.front();
andrewadavidson 10:a9db6fc55f1a 166 voltage_buffer.pop_front();
andrewadavidson 10:a9db6fc55f1a 167 }
andrewadavidson 10:a9db6fc55f1a 168 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, TXRX_BUF_LEN);
andrewadavidson 10:a9db6fc55f1a 169
andrewadavidson 10:a9db6fc55f1a 170 if (voltage_buffer.get_size() == 0) {
andrewadavidson 10:a9db6fc55f1a 171 ticker.detach();
andrewadavidson 10:a9db6fc55f1a 172 ticker.attach_us(read_analog_handle, 10000);
andrewadavidson 10:a9db6fc55f1a 173 }
andrewadavidson 10:a9db6fc55f1a 174 }
andrewadavidson 10:a9db6fc55f1a 175
andrewadavidson 10:a9db6fc55f1a 176 void read_analog_handle(void)
aadavids 8:7a3b7d1c8393 177 {
andrewadavidson 10:a9db6fc55f1a 178 // static bool is_shifting = false;
andrewadavidson 10:a9db6fc55f1a 179 // static uint8_t time_shift = 0;
aadavids 5:4112d6243485 180 static uint8_t prev_sample = 0;
aadavids 5:4112d6243485 181 // Read and send out
aadavids 5:4112d6243485 182 float s = ANALOG;
aadavids 5:4112d6243485 183 uint8_t sample = s*255;
aadavids 5:4112d6243485 184 voltage_buffer.push_back(sample);
andrewadavidson 10:a9db6fc55f1a 185 // pc.printf("sampled, %d. %d samples in buffer\r\n", sample, voltage_buffer.get_size());
aadavids 5:4112d6243485 186
andrewadavidson 10:a9db6fc55f1a 187
andrewadavidson 10:a9db6fc55f1a 188 if (((voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir)))) {
andrewadavidson 10:a9db6fc55f1a 189 // pc.printf("triggered, %d\n", sample);
andrewadavidson 10:a9db6fc55f1a 190 //
andrewadavidson 10:a9db6fc55f1a 191 //if (time_shift < time_shift_value) {
andrewadavidson 10:a9db6fc55f1a 192 // is_shifting = true;
andrewadavidson 10:a9db6fc55f1a 193 // time_shift++;
andrewadavidson 10:a9db6fc55f1a 194 //} else {
andrewadavidson 10:a9db6fc55f1a 195
andrewadavidson 10:a9db6fc55f1a 196 ticker.detach();
andrewadavidson 10:a9db6fc55f1a 197 ticker.attach_us(send_buffer_handle, 1000);
andrewadavidson 10:a9db6fc55f1a 198
andrewadavidson 10:a9db6fc55f1a 199 // }
aadavids 8:7a3b7d1c8393 200 }
andrewadavidson 10:a9db6fc55f1a 201 prev_sample = sample;
kmhiemst 9:b527643b8c93 202 }
aadavids 8:7a3b7d1c8393 203
kmhiemst 9:b527643b8c93 204 int main(void)
kmhiemst 9:b527643b8c93 205 {
andrewadavidson 10:a9db6fc55f1a 206 ticker.attach_us(read_analog_handle, 10000);
andrewadavidson 10:a9db6fc55f1a 207 // ticker.attach_us(m_test_handle, 100000);
kmhiemst 9:b527643b8c93 208 ble.init();
kmhiemst 9:b527643b8c93 209 ble.onDisconnection(disconnectionCallback);
andrewadavidson 10:a9db6fc55f1a 210 ble.onDataWritten(WrittenHandler);
andrewadavidson 10:a9db6fc55f1a 211
kmhiemst 9:b527643b8c93 212 pc.baud(9600);
kmhiemst 9:b527643b8c93 213 pc.printf("serqet Init \r\n");
aadavids 8:7a3b7d1c8393 214
kmhiemst 9:b527643b8c93 215 //pc.attach( uartCB , pc.RxIrq);
andrewadavidson 10:a9db6fc55f1a 216
andrewadavidson 10:a9db6fc55f1a 217 // setup advertising
kmhiemst 9:b527643b8c93 218 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
kmhiemst 9:b527643b8c93 219 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
kmhiemst 9:b527643b8c93 220 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
andrewadavidson 10:a9db6fc55f1a 221 (const uint8_t *)"Serqet", sizeof("Serqet") - 1);
kmhiemst 9:b527643b8c93 222 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
andrewadavidson 10:a9db6fc55f1a 223 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
andrewadavidson 10:a9db6fc55f1a 224 // 100ms; in multiples of 0.625ms.
kmhiemst 9:b527643b8c93 225 ble.setAdvertisingInterval(160);
RedBearLab 0:be2e4095513a 226
kmhiemst 9:b527643b8c93 227 ble.addService(uartService);
andrewadavidson 10:a9db6fc55f1a 228
andrewadavidson 10:a9db6fc55f1a 229 ble.startAdvertising();
andrewadavidson 10:a9db6fc55f1a 230
andrewadavidson 10:a9db6fc55f1a 231 pc.printf("Advertising Start \r\n");
andrewadavidson 10:a9db6fc55f1a 232
andrewadavidson 10:a9db6fc55f1a 233 while(1) {
andrewadavidson 10:a9db6fc55f1a 234 ble.waitForEvent();
kmhiemst 9:b527643b8c93 235 }
kmhiemst 9:b527643b8c93 236 }