serqet2
Dependencies: BLE_API circular_buffer mbed nRF51822
Fork of serqet by
main.cpp
- Committer:
- andrewadavidson
- Date:
- 2015-03-24
- Revision:
- 11:d54f92efbe70
- Parent:
- 10:a9db6fc55f1a
- Child:
- 12:7df0f60cf9e5
File content as of revision 11:d54f92efbe70:
#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. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ #define TXRX_BUF_LEN 20 //#define DIGITAL_OUT_PIN P0_9 //TXD //#define DIGITAL_IN_PIN P0_10 //CTS //#define ANALOG_IN_PIN P0_4 //P04 #define AC_EN_PIN P0_4 #define testLED P0_19 #define DC_EN_PIN P0_5 #define PAIRING_LED_PIN P0_7 #define BYPASS_ADC_PIN P0_4 #define PAIRING_BUTTON_PIN P0_15 #define POS_OFFSET_EN_PIN P0_29 #define NEG_OFFSET_EN_PIN P0_28 #define STATUS_LED_PIN P0_19//P0_11 #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 #define VOLTAGE_RANGE_SETTING 0x6 #define TIME_SHIFT_SETTING 0x7 #define TIME_RANGE_SETTING 0x8 #define AC_COUPLING 0 #define DC_COUPLING 1 #define FALLING_EDGE 0 #define RISING_EDGE 1 #define NEG_OFFSET 0 #define POS_OFFSET 1 // Global Settings 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; bool trigger_sign = 1; // 0 is negative, 1 is positive BLEDevice ble; // Pins declaration DigitalOut AC_EN(AC_EN_PIN); DigitalOut DC_EN(DC_EN_PIN); DigitalOut PAIRING_LED(PAIRING_LED_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); DigitalOut STATUS_LED(STATUS_LED_PIN); Serial pc(USBTX, USBRX); void read_analog_handle(void); // 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}; static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; //static uint8_t rx_buf[TXRX_BUF_LEN]; //static uint8_t rx_len=0; Ticker ticker; 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 *)); void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { pc.printf("Disconnected \r\n"); pc.printf("Restart advertising \r\n"); ble.startAdvertising(); } void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) { uint8_t buf[TXRX_BUF_LEN]; uint16_t bytesRead; 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); //for(index=0; index<bytesRead; index++) //pc.putc(buf[index]); coupling_type = (bool) buf[0]; offset_value = buf[1]; trigger_dir = (bool) buf[2]; trigger_level = buf[3]; averaging_amount = buf[4]; voltage_range = buf[5]; time_shift_value = buf[6]; time_range_value = buf[7]; offset_dir = (bool) buf[8]; trigger_sign = (bool) buf[9]; } } bool is_rising_edge (uint8_t current_sample, uint8_t previous_sample) { return (current_sample > trigger_level && previous_sample < trigger_level); } bool is_falling_edge (uint8_t current_sample, uint8_t previous_sample) { return (current_sample > trigger_level && previous_sample < trigger_level); } bool is_triggered (uint8_t current_sample, uint8_t previous_sample, bool dir) { // if (dir == RISING_EDGE) { // return is_rising_edge (current_sample, previous_sample); // } else { // return is_falling_edge (current_sample, previous_sample); // } // 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"); return (current_sample > trigger_level && previous_sample < trigger_level); } void m_test_handle(void) { static bool sw = false; sw = !sw; uint8_t buf[20]; for (int i = 0; i < 20; i++) { buf[i] = (uint8_t)(sin(3.14*i/20)*255); } ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 20); STATUS_LED = sw; } #define VOLTAGE_BUFFER_SIZE 512 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE); uint8_t out_buf[TXRX_BUF_LEN]; void send_buffer_handle(void) { for (int i = 0; i < TXRX_BUF_LEN; i++) { if (voltage_buffer.get_size() == 0) { break; } out_buf[i] = voltage_buffer.front(); voltage_buffer.pop_front(); } ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, TXRX_BUF_LEN); // pc.printf("sent 20, %d left\n\r", voltage_buffer.get_size()); if (voltage_buffer.get_size() == 0) { pc.printf("done sending\n\r"); ticker.detach(); ticker.attach_us(read_analog_handle, 10000); } } void read_analog_handle(void) { // static bool is_shifting = false; // 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); // pc.printf("sampled, %d. %d samples in buffer\r\n", sample, voltage_buffer.get_size()); if (((voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir)))) { pc.printf("triggered, %d\n", sample); // //if (time_shift < time_shift_value) { // is_shifting = true; // time_shift++; //} else { ticker.detach(); ticker.attach_us(send_buffer_handle, 10000); // } } prev_sample = sample; } int main(void) { // ticker.attach_us(read_analog_handle, 10000); ticker.attach_us(m_test_handle, 1000000); ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(WrittenHandler); pc.baud(9600); pc.printf("serqet Init \r\n"); //pc.attach( uartCB , pc.RxIrq); // 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); 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. ble.setAdvertisingInterval(160); ble.addService(uartService); ble.startAdvertising(); pc.printf("Advertising Start \r\n"); while(1) { ble.waitForEvent(); } }