Serqet2 / Mbed 2 deprecated serqet2

Dependencies:   BLE_API circular_buffer mbed nRF51822

Fork of serqet by Serqet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLEDevice.h"
00003 #include "circular_buffer.h"
00004 // Constants
00005 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
00006 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
00007 #define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
00008 
00009 #define TXRX_BUF_LEN                     20
00010 
00011 //#define DIGITAL_OUT_PIN                  P0_9       //TXD
00012 //#define DIGITAL_IN_PIN                   P0_10      //CTS
00013 //#define ANALOG_IN_PIN                    P0_4       //P04
00014 
00015 #define AC_EN_PIN   P0_4
00016 #define testLED P0_19
00017 #define DC_EN_PIN   P0_5
00018 #define PAIRING_LED_PIN P0_7
00019 #define BYPASS_ADC_PIN P0_4
00020 #define PAIRING_BUTTON_PIN P0_15
00021 #define POS_OFFSET_EN_PIN P0_29
00022 #define NEG_OFFSET_EN_PIN P0_28
00023 #define STATUS_LED_PIN P0_19//P0_11
00024 #define RESET_PIN P0_9
00025 
00026 #define COUPLING_SETTING 0x1
00027 #define OFFSET_SETTING 0x2
00028 #define OFFSET_DIR_SETTING 0x9
00029 #define TRIGGER_DIR_SETTING 0x3
00030 #define TRIGGER_VALUE_SETTING 0x4
00031 #define AVERAGING_SETTING 0x5
00032 #define VOLTAGE_RANGE_SETTING 0x6
00033 #define TIME_SHIFT_SETTING 0x7
00034 #define TIME_RANGE_SETTING 0x8
00035 
00036 #define AC_COUPLING 0
00037 #define DC_COUPLING 1
00038 #define FALLING_EDGE 0
00039 #define RISING_EDGE 1
00040 #define NEG_OFFSET 0
00041 #define POS_OFFSET 1
00042 
00043 // Global Settings
00044 bool coupling_type = 1;  // 0 is AC, 1 is DC
00045 uint8_t offset_value = 0;
00046 bool trigger_dir = 1; // 0 is negative, 1 is positive
00047 bool offset_dir = 1; // 0 is negative, 1 is positive
00048 uint8_t trigger_level = 127;
00049 uint8_t voltage_range = 1;
00050 uint8_t averaging_amount = 1;
00051 uint8_t time_shift_value = 0;
00052 uint8_t time_range_value = 0;
00053 bool trigger_sign = 1; // 0 is negative, 1 is positive
00054 
00055 BLEDevice  ble;
00056 
00057 // Pins declaration
00058 DigitalOut      AC_EN(AC_EN_PIN);
00059 DigitalOut      DC_EN(DC_EN_PIN);
00060 DigitalOut      PAIRING_LED(PAIRING_LED_PIN);
00061 AnalogIn        ANALOG(BYPASS_ADC_PIN);
00062 DigitalIn       PARING_BUTTON(PAIRING_BUTTON_PIN);
00063 DigitalOut      NEG_OFFSET_EN(NEG_OFFSET_EN_PIN);
00064 DigitalOut      POS_OFFSET_EN(POS_OFFSET_EN_PIN);
00065 DigitalOut      STATUS_LED(STATUS_LED_PIN);
00066 
00067 Serial pc(USBTX, USBRX);
00068 
00069 void read_analog_handle(void);
00070 
00071 // The Nordic UART Service
00072 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00073 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00074 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00075 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
00076 
00077 
00078 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00079 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
00080 
00081 //static uint8_t rx_buf[TXRX_BUF_LEN];
00082 //static uint8_t rx_len=0;
00083 
00084 Ticker ticker;
00085 
00086 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00087 
00088 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00089 
00090 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00091 
00092 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00093 
00094 
00095 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00096 {
00097     pc.printf("Disconnected \r\n");
00098     pc.printf("Restart advertising \r\n");
00099     ble.startAdvertising();
00100 }
00101 
00102 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
00103 {
00104     uint8_t buf[TXRX_BUF_LEN];
00105     uint16_t bytesRead;
00106 
00107     if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) {
00108         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00109         memset(txPayload, 0, TXRX_BUF_LEN);
00110         memcpy(txPayload, buf, TXRX_BUF_LEN);
00111 
00112         //for(index=0; index<bytesRead; index++)
00113         //pc.putc(buf[index]);
00114 
00115         coupling_type = (bool) buf[0];
00116         offset_value = buf[1];
00117         trigger_dir = (bool) buf[2];
00118         trigger_level = buf[3];
00119         averaging_amount = buf[4];
00120         voltage_range = buf[5];
00121         time_shift_value = buf[6];
00122         time_range_value = buf[7];
00123         offset_dir = (bool) buf[8];
00124         trigger_sign = (bool) buf[9];
00125     }
00126 }
00127 
00128 bool is_rising_edge (uint8_t current_sample, uint8_t previous_sample)
00129 {
00130     return (current_sample > trigger_level && previous_sample < trigger_level);
00131 }
00132 
00133 bool is_falling_edge (uint8_t current_sample, uint8_t previous_sample)
00134 {
00135     return (current_sample > trigger_level && previous_sample < trigger_level);
00136 }
00137 
00138 bool is_triggered (uint8_t current_sample, uint8_t previous_sample, bool dir)
00139 {
00140 //    if (dir == RISING_EDGE) {
00141 //        return is_rising_edge (current_sample, previous_sample);
00142 //    } else {
00143 //        return is_falling_edge (current_sample, previous_sample);
00144 //    }
00145     // 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");
00146     return (current_sample > trigger_level && previous_sample < trigger_level);
00147 }
00148 
00149 void m_test_handle(void)
00150 {
00151     static bool sw = false;
00152     sw = !sw;
00153     uint8_t buf[20];
00154     for (int i = 0; i < 20; i++) {
00155         buf[i] = (uint8_t)(sin(3.14*i/20)*255);
00156     }
00157     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 20);
00158     STATUS_LED = sw;
00159 }
00160 
00161 #define VOLTAGE_BUFFER_SIZE 512
00162 circular_buffer<uint8_t> voltage_buffer(VOLTAGE_BUFFER_SIZE);
00163 uint8_t out_buf[TXRX_BUF_LEN];
00164 
00165 void send_buffer_handle(void)
00166 {   
00167     for (int i = 0; i < TXRX_BUF_LEN; i++) {
00168         if (voltage_buffer.get_size() == 0) {
00169             break;
00170         }
00171         out_buf[i] = voltage_buffer.front();
00172         voltage_buffer.pop_front();
00173     }
00174     ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), out_buf, TXRX_BUF_LEN);
00175  //   pc.printf("sent 20, %d left\n\r", voltage_buffer.get_size());
00176 
00177     if (voltage_buffer.get_size() == 0) {
00178        // pc.printf("done sending\n\r");
00179         ticker.detach();
00180         ticker.attach_us(read_analog_handle, 10000);
00181     }
00182 }
00183 
00184 void read_analog_handle(void)
00185 {
00186 //    static bool is_shifting = false;
00187 //    static uint8_t time_shift = 0;
00188     static uint8_t prev_sample = 0;
00189     // Read and send out
00190     float s = ANALOG;
00191     uint8_t sample = s*255;
00192     voltage_buffer.push_back(sample);
00193   //  pc.printf("sampled, %d. %d samples in buffer\r\n", sample, voltage_buffer.get_size());
00194 
00195 
00196     if (((voltage_buffer.get_size() == VOLTAGE_BUFFER_SIZE) && (is_triggered(sample, prev_sample, trigger_dir)))) {
00197       //  pc.printf("triggered, %d\n", sample);
00198         //
00199         //if (time_shift < time_shift_value) {
00200         //    is_shifting = true;
00201         //    time_shift++;
00202         //} else {
00203         
00204         ticker.detach();
00205         ticker.attach_us(send_buffer_handle, 10000);
00206 
00207         //  }
00208     }
00209     prev_sample = sample;
00210 }
00211 
00212 int main(void)
00213 {
00214     ticker.attach_us(read_analog_handle, 10000);
00215    //  ticker.attach_us(m_test_handle, 10000);
00216     ble.init();
00217     ble.onDisconnection(disconnectionCallback);
00218     ble.onDataWritten(WrittenHandler);
00219 
00220     pc.baud(9600);
00221     pc.printf("serqet Init \r\n");
00222 
00223     //pc.attach( uartCB , pc.RxIrq);
00224 
00225     // setup advertising
00226     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00227     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00228     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00229                                      (const uint8_t *)"Serqet", sizeof("Serqet") - 1);
00230     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00231                                      (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00232     // 100ms; in multiples of 0.625ms.
00233     ble.setAdvertisingInterval(160);
00234 
00235     ble.addService(uartService);
00236 
00237     ble.startAdvertising();
00238 
00239     pc.printf("Advertising Start \r\n");
00240 
00241     while(1) {
00242         ble.waitForEvent();
00243     }
00244 }