serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Committer:
andrewadavidson
Date:
Tue Mar 24 23:11:32 2015 +0000
Revision:
13:22ad79b4c26c
Parent:
12:7df0f60cf9e5
update

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