serqet2
Dependencies: BLE_API circular_buffer mbed nRF51822
Fork of serqet by
Diff: main.cpp
- Revision:
- 10:a9db6fc55f1a
- Parent:
- 9:b527643b8c93
- Child:
- 11:d54f92efbe70
--- a/main.cpp Tue Mar 24 21:43:55 2015 +0000 +++ b/main.cpp Tue Mar 24 22:33:50 2015 +0000 @@ -15,7 +15,7 @@ #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 +#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 @@ -65,6 +65,8 @@ 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}; @@ -78,6 +80,7 @@ //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); @@ -107,7 +110,7 @@ //for(index=0; index<bytesRead; index++) //pc.putc(buf[index]); - + coupling_type = (bool) buf[0]; offset_value = buf[1]; trigger_dir = (bool) buf[2]; @@ -133,11 +136,13 @@ 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); - } +// 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) @@ -150,70 +155,82 @@ } -#define VOLTAGE_BUFFER_SIZE 1024 +#define VOLTAGE_BUFFER_SIZE 512 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE); -uint8_t out_buf[VOLTAGE_BUFFER_SIZE]; -void m_status_check_handle(void) +uint8_t out_buf[TXRX_BUF_LEN]; + +void send_buffer_handle(void) +{ + for (int i = 0; i < TXRX_BUF_LEN || voltage_buffer.get_size() == 0; i++) { + out_buf[i] = voltage_buffer.front(); + voltage_buffer.pop_front(); + } + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, TXRX_BUF_LEN); + + if (voltage_buffer.get_size() == 0) { + 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 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 ((is_shifting) || (voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir))) { - if (is_rising_edge(sample, prev_sample) || is_falling_edge(sample, prev_sample)) { - if (time_shift < time_shift_value) { - is_shifting = true; - time_shift++; - } else { - is_shifting = false; - 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); - } - } - prev_sample = sample; + + 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, 1000); + + // } } + prev_sample = sample; } int main(void) { - Ticker ticker; -// ticker.attach_us(m_status_check_handle, 1000); - ticker.attach_us(m_test_handle, 100000); + ticker.attach_us(read_analog_handle, 10000); + // ticker.attach_us(m_test_handle, 100000); ble.init(); ble.onDisconnection(disconnectionCallback); - ble.onDataWritten(WrittenHandler); - + ble.onDataWritten(WrittenHandler); + pc.baud(9600); pc.printf("serqet Init \r\n"); //pc.attach( uartCB , pc.RxIrq); - - // setup advertising + + // 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); + (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. + (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(); + + ble.startAdvertising(); + + pc.printf("Advertising Start \r\n"); + + while(1) { + ble.waitForEvent(); } }