初始化提交
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#include "Calculator.h"
|
||||
#include "SuperSensor.h"
|
||||
|
||||
#if defined (ARDUINO_ARCH_AVR)
|
||||
#include <MemoryFree.h>
|
||||
#endif
|
||||
|
||||
#if defined(__arm__)
|
||||
extern "C" char* sbrk(int incr);
|
||||
static int freeMemory() {
|
||||
char top = 't';
|
||||
return &top - reinterpret_cast<char*>(sbrk(0));
|
||||
}
|
||||
|
||||
#endif
|
||||
Calculator::Calculator( Scheduler* aS, Scheduler* aSensors) : Task(aS) {
|
||||
iS = aSensors;
|
||||
setTimeout(1000 * TASK_MILLISECOND);
|
||||
}
|
||||
|
||||
bool Calculator::Callback() {
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.println("CalcCallback: calculating");
|
||||
if ( getStatusRequest()->getStatus() >= 0) { // only calculate if statusrequest ended successfully
|
||||
Serial.print("CalcCallback: Max distance="); Serial.println(distance);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
extern int pins[];
|
||||
|
||||
bool Calculator::OnEnable() {
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.println("CalcEnable: OnEnable");
|
||||
Serial.println("Activating sensors");
|
||||
|
||||
StatusRequest* sr = getStatusRequest();
|
||||
iNS = sr->getCount();
|
||||
|
||||
distance = 0;
|
||||
for (int i = 0; i < iNS; i++) {
|
||||
SuperSensor *s = new SuperSensor( iS, pins[i], this, sr);
|
||||
s->setId(i + 1);
|
||||
s->begin();
|
||||
s->restartDelayed();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Calculator::OnDisable() {
|
||||
if ( timedOut() ) {
|
||||
getStatusRequest()->signalComplete(-1); // signal error
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.println("MeasureCallback: ***** Timeout *****");
|
||||
}
|
||||
iS->disableAll(false); // only disable tasks in the ts scheduler
|
||||
#if defined (ARDUINO_ARCH_AVR) || defined(__arm__)
|
||||
Serial.print("Free mem = "); Serial.println(freeMemory()); Serial.println();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Calculator::reportDistance(long aD) {
|
||||
if (distance < aD) distance = aD;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef _CALCULATOR_H
|
||||
#define _CALCULATOR_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
#define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
|
||||
#define _TASK_PRIORITY // Support for layered scheduling priority
|
||||
#define _TASK_TIMEOUT // Support for overall task timeout
|
||||
#define _TASK_OO_CALLBACKS // Support for dynamic callback method binding
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
|
||||
class Calculator : public Task {
|
||||
public:
|
||||
Calculator( Scheduler* aS, Scheduler* aSensors);
|
||||
|
||||
void reportDistance(long aD);
|
||||
|
||||
bool Callback();
|
||||
bool OnEnable();
|
||||
void OnDisable();
|
||||
|
||||
private:
|
||||
Scheduler* iS;
|
||||
|
||||
long distance;
|
||||
int iNS;
|
||||
|
||||
};
|
||||
|
||||
#endif // _CALCULATOR_H
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
This is example 5 rewritten with dynamic binding of callback methods
|
||||
- 1 second timeout is set for the main calculation task
|
||||
- LTS is used to address task-specific sensor class object
|
||||
- WDT is used to set the Task ID and use that for identifying the tasks (debug)
|
||||
|
||||
Original description:
|
||||
====================
|
||||
This test emulates querying 1 to 10 sensors once every 10 seconds, each could respond with a different delay
|
||||
(ultrasonic sensors for instance) and printing a max value of them when all have reported their values.
|
||||
The overall timeout of 1 second is setup as well.
|
||||
An error message needs to be printed if a timeout occurred instead of a distance value.
|
||||
|
||||
Task and SuperSensor objects are dynamically created and destroyed as needed every 10 seconds
|
||||
|
||||
This sketch uses a FreeMemory library: https://github.com/McNeight/MemoryFree
|
||||
FreeMemory for ARM32 boards is based on: http://www.stm32duino.com/viewtopic.php?f=18&t=2065
|
||||
*/
|
||||
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
#define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
|
||||
#define _TASK_PRIORITY // Support for layered scheduling priority
|
||||
#define _TASK_TIMEOUT // Support for overall task timeout
|
||||
#define _TASK_OO_CALLBACKS
|
||||
|
||||
#include <TaskScheduler.h>
|
||||
|
||||
#include "SuperSensor.h"
|
||||
#include "Calculator.h"
|
||||
#include "Ticker.h"
|
||||
|
||||
StatusRequest measure;
|
||||
|
||||
Scheduler ts, hts;
|
||||
|
||||
// Tasks
|
||||
|
||||
Calculator* tCalculate;
|
||||
Ticker* tCycle;
|
||||
|
||||
int pins[] = { 1, 9, 3, 7, 5, 6, 4, 8, 2, 10 };
|
||||
|
||||
#ifdef ARDUINO_ARCH_STM32F1
|
||||
#define A0 3
|
||||
#endif
|
||||
|
||||
/** Main Arduino code
|
||||
Not much is left here - everything is taken care of by the framework
|
||||
*/
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
while (!Serial) {}
|
||||
Serial.println("TaskScheduler StatusRequest Sensor Emulation Test. Complex Test.");
|
||||
|
||||
#ifdef ARDUINO_ARCH_STM32F1
|
||||
pinMode(A0, INPUT_ANALOG);
|
||||
#endif
|
||||
|
||||
randomSeed(analogRead(A0) + millis());
|
||||
|
||||
ts.setHighPriorityScheduler(&hts);
|
||||
|
||||
tCalculate = new Calculator (&hts, &ts);
|
||||
tCycle = new Ticker (&hts, (Task*) tCalculate, &measure);
|
||||
|
||||
tCalculate->setTimeout(1 * TASK_SECOND);
|
||||
tCycle->enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ts.execute();
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "SuperSensor.h"
|
||||
|
||||
SuperSensor::SuperSensor(Scheduler* aScheduler, int aPin, Calculator* aC, StatusRequest* aS) : Task(TASK_MILLISECOND, TASK_FOREVER, aScheduler, false) {
|
||||
iPin = aPin;
|
||||
iC = aC;
|
||||
iS = aS;
|
||||
}
|
||||
|
||||
SuperSensor::~SuperSensor() {
|
||||
iValue = -1;
|
||||
}
|
||||
|
||||
void SuperSensor::begin() {
|
||||
iDelay = random(300, 1500);
|
||||
iValue = -1;
|
||||
}
|
||||
|
||||
void SuperSensor::stop() {
|
||||
//nothing to do
|
||||
}
|
||||
|
||||
long SuperSensor::trigger() {
|
||||
iStart = millis();
|
||||
return iDelay;
|
||||
}
|
||||
|
||||
bool SuperSensor::measurementReady() {
|
||||
if ( millis() - iStart > iDelay ) {
|
||||
iValue = random(501);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
long SuperSensor::value() {
|
||||
return iValue;
|
||||
}
|
||||
|
||||
bool SuperSensor::OnEnable() {
|
||||
int i = getId();
|
||||
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.print("SEnable: TaskID=");
|
||||
Serial.println(i);
|
||||
Serial.print("Triggering sensor. Delay=");
|
||||
|
||||
long dly = trigger();
|
||||
|
||||
Serial.println( dly );
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SuperSensor::Callback() {
|
||||
|
||||
if ( measurementReady() ) {
|
||||
int i = getId();
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.print("SCallback: TaskID=");
|
||||
Serial.println(i);
|
||||
Serial.print("Emulating measurement. d=");
|
||||
|
||||
long d = value();
|
||||
iC->reportDistance(d);
|
||||
|
||||
Serial.println(d);
|
||||
|
||||
iS->signal();
|
||||
disable();
|
||||
delete this;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SuperSensor::OnDisable() {
|
||||
int i = getId();
|
||||
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.print("SDisable: TaskID=");
|
||||
Serial.println(i);
|
||||
|
||||
stop();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef _SUPER_SENSOR_H
|
||||
#define _SUPER_SENSOR_H
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
#define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
|
||||
#define _TASK_PRIORITY // Support for layered scheduling priority
|
||||
#define _TASK_TIMEOUT // Support for overall task timeout
|
||||
#define _TASK_OO_CALLBACKS
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include "Calculator.h"
|
||||
|
||||
//class Calculator;
|
||||
|
||||
class SuperSensor : public Task {
|
||||
|
||||
public:
|
||||
SuperSensor(Scheduler* aScheduler, int aPin, Calculator* aC, StatusRequest* aS);
|
||||
~SuperSensor();
|
||||
|
||||
void begin();
|
||||
void stop();
|
||||
long trigger();
|
||||
bool measurementReady();
|
||||
long value();
|
||||
|
||||
bool Callback();
|
||||
bool OnEnable();
|
||||
void OnDisable();
|
||||
|
||||
private:
|
||||
long iDelay;
|
||||
long iValue;
|
||||
int iPin;
|
||||
unsigned long iStart;
|
||||
Calculator* iC;
|
||||
StatusRequest* iS;
|
||||
};
|
||||
|
||||
#endif // _SUPER_SENSOR_H
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "Ticker.h"
|
||||
|
||||
Ticker::Ticker(Scheduler* aS, Task* aCalc, StatusRequest* aM) : Task(10000, TASK_FOREVER, aS, false) {
|
||||
iCalc = aCalc;
|
||||
iMeasure = aM;
|
||||
}
|
||||
|
||||
bool Ticker::Callback() {
|
||||
Serial.println(); Serial.println(); Serial.println();
|
||||
Serial.print(millis()); Serial.print(":\t");
|
||||
Serial.println("CycleCallback: Initiating measurement cycle every 10 seconds");
|
||||
|
||||
int numberSensors = random(1, 11); // 1 to 10 sensors, randomly
|
||||
Serial.print("Number of sensors=");
|
||||
Serial.println(numberSensors);
|
||||
|
||||
iMeasure->setWaiting(numberSensors); // Set the StatusRequest to wait for 1 to 10 signals.
|
||||
iCalc->waitFor(iMeasure);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef _TICKER_H
|
||||
#define _TICKER_H
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
|
||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||
#define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
|
||||
#define _TASK_PRIORITY // Support for layered scheduling priority
|
||||
#define _TASK_TIMEOUT // Support for overall task timeout
|
||||
#define _TASK_OO_CALLBACKS
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
|
||||
class Ticker : public Task {
|
||||
public:
|
||||
Ticker(Scheduler* aS, Task* aCalc, StatusRequest* aM);
|
||||
~Ticker() {};
|
||||
|
||||
bool Callback();
|
||||
|
||||
private:
|
||||
Task *iCalc;
|
||||
StatusRequest* iMeasure;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user