micro code for pen oscilloscope
Dependencies: BLE_API circular_buffer mbed nRF51822
Fork of BLENano_SimpleControls by
Diff: main.cpp
- Revision:
- 4:635dcca9e11a
- Parent:
- 3:6c83be282aac
- Child:
- 5:4112d6243485
diff -r 6c83be282aac -r 635dcca9e11a main.cpp --- a/main.cpp Thu Mar 19 03:32:00 2015 +0000 +++ b/main.cpp Fri Mar 20 01:31:03 2015 +0000 @@ -20,6 +20,7 @@ #include "mbed.h" #include "BLEDevice.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. */ @@ -41,15 +42,43 @@ #define STATUS_LED_PIN P0_11 #define RESET_PIN P0_09 +#define COUPLING_SETTING 0x1 +#define OFFSET_SETTING 0x2 +#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 NEG_EDGE 0 +#define POS_EDGE 1 + +// Global Settings +bool coupling_type; // 0 is AC, 1 is DC +uint8_t offset_value; +bool trigger_dir; // 0 is negative, 1 is positive +uint8_t trigger_value; +uint8_t voltage_range; +uint8_t averaging_amount; +uint8_t time_shift_value; +uint8_t time_range_value; BLEDevice ble; -DigitalOut LED_SET(DIGITAL_OUT_PIN); -DigitalIn BUTTON(DIGITAL_IN_PIN); -PwmOut PWM(PWM_PIN); -AnalogIn ANALOG(ANALOG_IN_PIN); -Servo MYSERVO(SERVO_PIN); +// Pins declaration +DigitalOut AC_EN(AC_EN_PIN); +DigitalOut DC_EN(DC_EN_PIN); +DigitalOut PAIRING_LED(PAIRING_LED_PIN); +AnalogIn BYPASS_ADC(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); +DigitalOut RESET(RESET_PIN); + //Serial pc(USBTX, USBRX); @@ -74,12 +103,11 @@ GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); -GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; +//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"); @@ -101,36 +129,43 @@ //for(index=0; index<bytesRead; index++) //pc.putc(buf[index]); - if(buf[0] == 0x01) - { - if(buf[1] == 0x01) - LED_SET = 1; - else - LED_SET = 0; + if(buf[0] == COUPLING_SETTING) { + coupling_type = buf[1]; + if (coupling_type = AC_COUPLING) { + AC_EN = 1; + DC_EN = 0; + } else // DC coupling + AC_EN = 0; + DC_EN = 1; + } } - else if(buf[0] == 0xA0) - { - if(buf[1] == 0x01) - analog_enabled = 1; - else - analog_enabled = 0; + else if(buf[0] == TRIGGER_DIR_SETTING) { + trigger_dir = buf[1]; + } + else if(buf[0] == AVERAGING_SETTING) { + averaging_amount = buf[0]; } - else if(buf[0] == 0x02) - { - float value = (float)buf[1]/255; - PWM = value; - } - else if(buf[0] == 0x03) + else if(buf[0] == VOLTAGE_RANGE_SETTING) { - MYSERVO.write(buf[1]); + voltage_range = buf[1]; + // i2c write to DAC here + } + else if(buf[0] == TIME_SHIFT_SETTING) { + time_shift_value = buf[1]; + } + else if(buf[0] == TIME_RANGE_SETTING) { + time_range_value = buf[1]; } - else if(buf[0] == 0x04) - { - analog_enabled = 0; - PWM = 0; - MYSERVO.write(0); - LED_SET = 0; - old_state = 0; + else if(buf[0] == OFFSET_SETTING) { + offset_value = buf[1]; + if (offset_value >= 0) { // postitive offset + NEG_OFFSET_EN = 0; + POS_OFFSET_EN = 1; + } else { // negative offset + NEG_OFFSET_EN = 1; + POS_OFFSET_EN = 0; + } + // i2c write to DAC here } } @@ -154,6 +189,11 @@ } } */ + + +uint16_t index; +uint8_t voltage_buffer; + void m_status_check_handle(void) { uint8_t buf[3]; @@ -167,34 +207,12 @@ buf[2] = (value); ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); } - - // If digital in changes, report the state - if (BUTTON != old_state) - { - old_state = BUTTON; - - if (BUTTON == 1) - { - buf[0] = (0x0A); - buf[1] = (0x01); - buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - else - { - buf[0] = (0x0A); - buf[1] = (0x00); - buf[2] = (0x00); - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); - } - } } - int main(void) { Ticker ticker; - ticker.attach_us(m_status_check_handle, 200000); + ticker.attach_us(m_status_check_handle, 1000); ble.init(); ble.onDisconnection(disconnectionCallback);