add SoftwareTimer support

This commit is contained in:
hathach committed 2017-04-10 17:27:30 +07:00
1 parent e5d98a877f
commit 6c87cf4df2
4 files changed
+69 -27

No files matched your search

+1
View File
@@ -8,6 +8,7 @@
- Add rtos_idle_callback() as optional callback for user to handle background task
- Add mutex to prevent uart conflict
- Change FreeRTOS tick source from systick to RTC for low-power mode
- Add SoftwareTimer class as wrapper for FreeRTOS software timer
#### New Example
+4 -3
View File
@@ -43,6 +43,9 @@ uint32_t setLoopStacksize(void);
} // extern "C"
#endif
// RTOS
#include "rtos.h"
// The following headers are for C++ only compilation
#ifdef __cplusplus
#include "WCharacter.h"
@@ -52,6 +55,7 @@ uint32_t setLoopStacksize(void);
#include "HardwareSerial.h"
#include "pulse.h"
#include "HardwarePWM.h"
#include "utility/SoftwareTimer.h"
#endif
#include "delay.h"
@@ -72,9 +76,6 @@ uint32_t setLoopStacksize(void);
#include "wiring_shift.h"
#include "WInterrupts.h"
// RTOS
#include "rtos.h"
// undefine stdlib's abs if encountered
#ifdef abs
#undef abs
@@ -18,11 +18,18 @@ BLEDis bledis;
BLEUart bleuart;
BLEBas blebas;
// Software Timer for blinking RED LED
SoftwareTimer blinkTimer;
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 BLEUART Example");
// Initialize Blink Timer for 1000 ms and start it
blinkTimer.begin(1000, blink_timer_callback);
blinkTimer.start();
// 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
@@ -108,6 +115,20 @@ void disconnect_callback(uint8_t reason)
Serial.println("Bluefruit will auto start advertising (default)");
}
/**
* Software Timer callback is invoked in built-in thread of FreeRTOS with
* minimal stack size. Therefore it should be as simple as possible. If
* a periodically heavy-task is needed, please use Scheduler.startLoop() to
* create an own task for it.
*
* More information http://www.freertos.org/RTOS-software-timer.html
*/
void blink_timer_callback(TimerHandle_t xTimerID)
{
(void) xTimerID;
digitalToggle(LED_RED);
}
/**
* RTOS Idle callback is automatically invoked by FreeRTOS
* when there is no active threads. E.g loop() call delay() and
@@ -13,9 +13,6 @@
*********************************************************************/
#include <bluefruit.h>
#define STATUS_LED (LED_RED)
#define BLINKY_MS (2000)
/* HRM Service Definitions
* Heart Rate Monitor Service: 0x180D
* Heart Rate Measurement Char: 0x2A37
@@ -28,7 +25,6 @@ BLECharacteristic bslc = BLECharacteristic(UUID16_CHR_BODY_SENSOR_LOCATION);
BLEDis bledis; // DIS (Device Information Service) helper class instance
BLEBas blebas; // BAS (Battery Service) helper class instance
uint32_t blinkyms;
uint8_t bps = 0;
// Advanced function prototypes
@@ -38,16 +34,14 @@ void connect_callback(void);
void disconnect_callback(uint8_t reason);
void cccd_callback(BLECharacteristic& chr, ble_gatts_evt_write_t* request);
void blink_timer_callback(TimerHandle_t xTimerID);
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 HRM Example");
Serial.println("-----------------------");
// Setup LED pins and reset blinky counter
pinMode(STATUS_LED, OUTPUT);
blinkyms = millis();
// Initialise the Bluefruit module
Serial.println("Initialise the Bluefruit nRF52 module");
Bluefruit.begin();
@@ -194,22 +188,47 @@ void cccd_callback(BLECharacteristic& chr, uint16_t cccd_value)
void loop()
{
// Blinky!
if (blinkyms+BLINKY_MS < millis()) {
blinkyms = millis();
digitalToggle(STATUS_LED);
if (Bluefruit.connected()) {
uint8_t hrmdata[2] = { 0b00000110, bps++ }; // Sensor connected, increment BPS value
// Note: We use .notify instead of .write!
// If it is connected but CCCD is not enabled
// The characteristic's value is still updated although notification is not sent
if ( hrmc.notify(hrmdata, sizeof(hrmdata)) ){
Serial.print("Heart Rate Measurement updated to: "); Serial.println(bps);
}else{
Serial.println("ERROR: Notify not set in the CCCD or not connected!");
}
digitalToggle(LED_RED);
if ( Bluefruit.connected() ) {
uint8_t hrmdata[2] = { 0b00000110, bps++ }; // Sensor connected, increment BPS value
// Note: We use .notify instead of .write!
// If it is connected but CCCD is not enabled
// The characteristic's value is still updated although notification is not sent
if ( hrmc.notify(hrmdata, sizeof(hrmdata)) ){
Serial.print("Heart Rate Measurement updated to: "); Serial.println(bps);
}else{
Serial.println("ERROR: Notify not set in the CCCD or not connected!");
}
}
// Only send update once per second
delay(1000);
// No need to call waitForEvent() here, it is already invoked
// by above delay() via Idle Task internally. See the note below
}
/**
* RTOS Idle callback is automatically invoked by FreeRTOS
* when there is no active threads. E.g loop() call delay() and
* there is no bluetooth or hw event. This is a greate place to handle
* background data.
*
* NOTE: After this callback returns, MCU will enter low-power mode
* by waitForEvent() internally. THerefore user SHOULD NOT call
* waitForEvent() in this callback.
*
* WARNING: This function MUST NOT call any blocking FreeRTOS API
* such as delay(), xSemaphoreTake() etc ... for more information
* http://www.freertos.org/a00016.html
*/
void rtos_idle_callback(void)
{
// Background task here
// waitForEvent() will be invoked by internal code when this callback return.
// Don't call waitForEvent() in this function nor any other blocking API()
}