Dissertation project, looking at BLE communication in cars. This project is BLE peripheral acting as car indicator stalk
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_test1 by
Diff: main.cpp
- Revision:
- 11:35b63ab4c76c
- Parent:
- 10:2c5c202c69a5
- Child:
- 12:f5b12e8b6043
--- a/main.cpp Sun Mar 22 17:47:11 2015 +0000 +++ b/main.cpp Thu Mar 26 15:06:21 2015 +0000 @@ -1,29 +1,46 @@ -/* -* TODO: -* - Make it work! -*/ - #include "mbed.h" #include "BLEDevice.h" #include "BroadcasterService.h" #include "ErrorService.h" +#include "PinDetect.h" #include <string> using namespace std; BLEDevice ble; DigitalOut led1(LED1); + +PinDetect pIn(p5); + Serial pc(USBTX, USBRX); /*Variable Declarations*/ const static char DEVICE_NAME[] = "BLE_Broadcaster"; -static volatile bool go = false; +static volatile bool indicatorOn = false; +static volatile bool indicatorSent = false; +//TODO: I want to be able to send something like: + +//with first 4 bits being type code, and last 4 being command, or something +uint8_t cmdOn[8] = { 0x4d,0x32,0x81,0xc0,0x4d,0x4d,0x4d,0x4d }; +uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x00,0x00,0x00,0x00 }; + void blink(void) { - led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ - go = true; + led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ +} +/** Callback routine is interrupt activated by a debounced pb hit*/ +void keyPressed(void) { + indicatorOn = true; + indicatorSent = false; + pc.printf("Switch changed. Is now %d \r\n", indicatorOn); +} + +void keyReleased(void) { + indicatorOn = false; + indicatorSent = false; + pc.printf("Switch changed. Is now %d \r\n", indicatorOn); } void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) @@ -40,25 +57,31 @@ int main(void) { - //TODO: I want to be able to send something like: - uint8_t cmdOn[8] = { 0x4d,0x32,0x81,0xc0,0x4d,0x4d,0x4d,0x4d }; - uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x00,0x00,0x00,0x00 }; - //with first 4 bits being type code, and last 4 being command, or something + /*Setup switch input*/ + pIn.mode(PullUp); + // Delay for initial pullup to take effect + wait(.01); + + //set asserted = 0 volts and deasserted = 5 (or 3.3, actually) + pIn.setAssertValue(0); + // Setup Interrupt callback function for a pb hit + pIn.attach_asserted( &keyPressed ); + pIn.attach_deasserted( &keyReleased ); + + // Start sampling pb input using interrupts + pIn.setSampleFrequency(); + + /*Setup Blinky*/ + //led1 = 1; + //Ticker t; + //t.attach(blink, 1f); - //uint8_t cmdOn = 0x01; - //uint8_t cmdOff = 0x02; - - //blinky - led1 = 1; - Ticker t; - t.attach(blink, 5.0f); - - //Create BLE stuff + /*Setup BLE*/ ble.init(); ble.onDisconnection(disconnectionCallback); ble.onConnection(connectionCallback); - //CarCommsService commsService(ble, cmd); + //create services BroadcasterService broadcasterService(ble); ErrorService errorService(ble); @@ -71,30 +94,33 @@ // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - - //Advertise + ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600 ble.startAdvertising(); pc.printf("Advertising node %s\n\r", DEVICE_NAME); - bool x = true; + //loop forever while(true) { - ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) - if(go) { - if(x) { - pc.printf("Command = %u\r\n", cmdOn); + ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup) + + if(indicatorOn) { //if button is pressed + if(!indicatorSent) { //should only fire the first time! + pc.printf("Command on = %u\r\n", cmdOn); broadcasterService.sendCommand(cmdOn); - - errorService.registerError(ErrorService::ERROR_GENERIC); - x = false; - } else { - pc.printf("Command = %u\r\n", cmdOff); + led1 = true; + //errorService.registerError(ErrorService::ERROR_GENERIC); + indicatorSent = true; //set true to stop multiple firing + } + } else { + if(!indicatorSent) { + pc.printf("Command off = %u\r\n", cmdOff); broadcasterService.sendCommand(cmdOff); - x = true; + led1 = false; + //errorService.registerError(ErrorService::ERROR_DISCONNECTION); + indicatorSent = true; //set true to stop multiple firing } - go = false; } } } \ No newline at end of file