We are making a bluetooth application for a vehicle.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
Diff: main.cpp
- Revision:
- 29:e7d4922a4620
- Parent:
- 28:a56710056f4d
- Child:
- 30:243e095a69d9
--- a/main.cpp Mon Jul 17 17:16:11 2017 +0000 +++ b/main.cpp Wed Jul 19 05:03:35 2017 +0000 @@ -39,15 +39,23 @@ #define buttonMask 0x003F PortIn sixButtons(PortC, buttonMask); - + +//InterruptIn event(sixButtons.read()); +//InterruptIn interruptEventButton_0(PC_2); +//InterruptIn interruptEventButton_1(PC_3); +//InterruptIn interruptEventButton_2(PC_4); +//InterruptIn interruptEventButton_3(PC_5); +//InterruptIn interruptEventSensor_0(PC_0); // Vibration +//InterruptIn interruptEventSensor_1(PC_1); // Tilt + unsigned char byteOut = 0; unsigned char prevByteOut = 0; unsigned char byteIn = 0x08; - uint8_t inputs = 0x00; - uint8_t outputs = 0x00; - uint8_t old_outputs = 0x00; - uint32_t timestamp = 0x00; +uint8_t inputs = 0x00; +uint8_t outputs = 0x00; +uint8_t old_outputs = 0x00; +uint32_t timestamp = 0x00; static volatile bool triggerSensorPolling = false; @@ -57,9 +65,13 @@ BLE::Instance().gap().startAdvertising(); // restart advertising } +void toggleLED(void) +{ + led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ +} + void periodicCallback(void) { - led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ /* Note that the periodicCallback() executes in interrupt context, so it is safer to do * heavy-weight sensor polling from the main thread. */ triggerSensorPolling = true; @@ -79,7 +91,44 @@ //} } +void alarm() { + if (alarm_activated && alarm_on) { //Flag set to sound the alarm? + + if (alarm_counter++ < 500) { //Beep it on and off + buzzer = 1; + led_0 = 1; + led_1 = 1; + rumblr = 1; + } + else { + if (alarm_counter > 1000){alarm_counter = 0;} + buzzer = 0; + led_0 = 0; + led_1 = 0; + rumblr = 0; + } + } +} +//void interruptButton_0() { + +//} +//void interruptButton_1() { + +//} +//void interruptButton_2() { + +//} +//void interruptButton_3() { + +//} +//void interruptSensor_0() { // Vibration +// alarm_on = 1; +//} +//void interruptSensor_1() { // Tilt +// +//} + void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; @@ -114,36 +163,42 @@ while (true) { // check for trigger from periodicCallback() if (triggerSensorPolling && ble.getGapState().connected) { + //if (ble.getGapState().connected) { if (outputs & 0x01){ led_0 = 1; - } + } if ((outputs & 0x01) == 0){ led_0 = 0; - } + } + if (outputs & 0x02){ led_1 = 1; - } + } if ((outputs & 0x02) == 0){ led_1 = 0; - } + } + if (outputs & 0x04){ buzzer = 1; } if ((outputs & 0x04) == 0){ buzzer = 0; - } + } + if (outputs & 0x08){ rumblr = 1; - } + } if ((outputs & 0x08) == 0){ rumblr = 0; - } + } + if (outputs & 0x10){ alarm_activated = 1; } if ((outputs & 0x10) == 0){ alarm_activated = 0; } + if (outputs & 0x20){ alarm_on = 1; } @@ -152,45 +207,54 @@ } triggerSensorPolling = false; - byteOut = sixButtons & buttonMask; - if ((byteOut & 0x01) && alarm_activated==1) { - alarm_on = 1; - } + // update the ble with the inputs + bikeService.updateInputs(byteOut); + bikeService.updateTimestamp(); + //if ((byteOut & 0x01) && alarm_activated==1) { + // alarm_on = 1; + // } if (byteOut != prevByteOut){ // update the ble with the inputs bikeService.updateInputs(byteOut); bikeService.updateTimestamp(); } prevByteOut = byteOut; + } else { ble.waitForEvent(); // low power wait for event } - if (alarm_activated && alarm_on) { //Flag set to sound the alarm? - - if (alarm_counter++ < 500) { //Beep it on and off - buzzer = 1; - led_0 = 1; - led_1 = 1; - rumblr = 1; - } - else { - if (alarm_counter > 1000){alarm_counter = 0;} - buzzer = 0; - led_0 = 0; - led_1 = 0; - rumblr = 0; - } - } + alarm(); } } int main(void) { + + //interruptEventButton_0.fall(&interruptButton_0); + //interruptEventButton_1.fall(&interruptButton_1); + //interruptEventButton_2.fall(&interruptButton_2); + //interruptEventButton_3.fall(&interruptButton_3); + //interruptEventSensor_0.fall(&interruptSensor_0); // Vibration + //interruptEventSensor_1.fall(&interruptSensor_1); // Tilt + Ticker ticker; - ticker.attach(periodicCallback, 1); // blink LED every second - + ticker.attach(toggleLED, 1); + + Ticker ticker_CallBack; + //ticker.attach(periodicCallback, 1); // blink LED every second + ticker_CallBack.attach(periodicCallback, 0.01); + BLE::Instance().init(bleInitComplete); } +// Reference +// http://files.amperka.ru/datasheets/nucleo-usermanual.pdf +// http://www.st.com/content/ccc/resource/technical/document/user_manual/65/e0/44/72/9e/34/41/8d/DM00026748.pdf/files/DM00026748.pdf/jcr:content/translations/en.DM00026748.pdf +// https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/default/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/PinNames.h +// https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/default/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_NUCLEO_L476RG/PeripheralPins.c +// http://www.st.com/content/ccc/resource/technical/document/datasheet/c5/ed/2f/60/aa/79/42/0b/DM00108832.pdf/files/DM00108832.pdf/jcr:content/translations/en.DM00108832.pdf +// http://www.st.com/en/microcontrollers/stm32l476rg.html +// http://jeelabs.org/book/1547a/index.html +// http://www.st.com/content/ccc/resource/technical/document/datasheet/33/d4/6f/1d/df/0b/4c/6d/CD00161566.pdf/files/CD00161566.pdf/jcr:content/translations/en.CD00161566.pdf