From 792a011185cb454922745dd468032063ca4897e7 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 9 Apr 2017 22:07:17 +0700 Subject: [PATCH] add waitForEvent() for examples --- .../examples/Peripheral/bleuart/bleuart.ino | 42 +++++++++++-------- .../Peripheral/hid_keyboard/hid_keyboard.ino | 4 ++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino index 0a035e18..d9436fd1 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bleuart/bleuart.ino @@ -18,21 +18,11 @@ BLEDis bledis; BLEUart bleuart; BLEBas blebas; -#define STATUS_LED (17) -#define BLINKY_MS (2000) - -uint32_t blinkyms; - void setup() { Serial.begin(115200); - Serial.println("Bluefruit52 BLEUART Example"); - // Setup LED pins and reset blinky counter - pinMode(STATUS_LED, OUTPUT); - blinkyms = millis(); - // Setup the BLE LED to be enabled on CONNECT // Note: This is actually the default behaviour, but provided // here in case you want to control this manually via PIN 19 @@ -80,12 +70,6 @@ void setupAdv(void) void loop() { - // Blinky! - if (blinkyms+BLINKY_MS < millis()) { - blinkyms = millis(); - digitalToggle(STATUS_LED); - } - // forward from Serial to BLEUART while (Serial.available()) { @@ -105,8 +89,8 @@ void loop() Serial.write(ch); } - // Equavilent to __WFE (Wait For Event) instruction. - // It is a hint to allow CPU to enter low-power state until an event occured + // Instruct CPU to enter low-power state until an event/interrupt occured + // Note: This will turn off hardware PWM and maybe other peripherlas !! waitForEvent(); } @@ -123,3 +107,25 @@ void disconnect_callback(uint8_t reason) Serial.println("Disconnected"); Serial.println("Bluefruit will auto start advertising (default)"); } + +/** + * RTOS Idle callback is automatically invoked by FreeRTOS + * when there is no active threads. This function must not + * call any blocking FreeRTOS API such as delay(), xSemaphoreTake() etc .. + * + * Notes: Should be used for background work. Great place to call + * waitForEvent() (see comment below) + */ +void rtos_idle_callback(void) +{ + // Background task here + + // Instruct CPU to enter low-power state until an event/interrupt occured + // Note: This will turn off hardware PWM and maybe other peripherlas !! + // Should only call there is no active PWM devices + if ( !(PWM0.begun() || PWM1.begun() || PWM2.begun() ) ) + { + waitForEvent(); + } +} + diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino index 29be53e4..458fc40c 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino @@ -103,5 +103,9 @@ void loop() // Delay a bit after a report delay(5); } + + // Instruct CPU to enter low-power state until an event/interrupt occured + // Note: This will turn off hardware PWM and maybe other peripherlas !! + waitForEvent(); }