Lucrarea nr 12
L68s gcc Arduino22 2560 Simple Task Scheduler
L68s gcc Arduino22 2560 Simple Task Scheduler
//Lab40 Arduino 2560 RTOS A105a
#include <Task.h>
#include <TaskScheduler.h>
const int NUMBER_OF_FIELDS = 3; // how many comma seperated fields we expect
int values[NUMBER_OF_FIELDS]; // array holding values for all the fields
// Timed task to blink a LED.
class Blinker : public TimedTask{
public: // Create a new blinker for the specified pin and rate.
Blinker(uint8_t _pin, uint32_t _rate);
virtual void run(uint32_t now);
private:
uint8_t pin; // LED pin.
uint32_t rate; // Blink rate.
bool on; // Current state of the LED.};
Blinker::Blinker(uint8_t _pin, uint32_t _rate)
: TimedTask(millis()),
pin(_pin),
rate(_rate),
on(false){
pinMode(pin, OUTPUT); // Set pin for output.}
void Blinker::run(uint32_t now){
// If the LED is on, turn it off and remember the state.
if (on) { digitalWrite(pin, LOW);
on = false;
// If the LED is off, turn it on and remember the state. }
else { digitalWrite(pin, HIGH);
on = true; }
// Run again in the required number of milliseconds.
incRunTime(rate);}
// Task to echo serial input.
class Echoer : public Task
{public:
Echoer();
virtual void run(uint32_t now);
virtual bool canRun(uint32_t now); };
Echoer::Echoer()
: Task(){
[Link](4800);}
bool Echoer::canRun(uint32_t now)
{ return [Link]() > 0;}
void Echoer::run(uint32_t now){
while ([Link]() > 0) {
int byte = [Link]();
[Link](byte, BYTE);
if (byte == '\r') {
[Link]('\n', BYTE); }
}
}
// Timed task 1
class Blinker1 : public TimedTask
{public:
// Create a new blinker for the specified pin and rate.
Blinker1( uint32_t _rate);
virtual void run(uint32_t now);
private:
uint32_t rate; // Blink rate. };
Blinker1::Blinker1(uint32_t _rate)
: TimedTask(millis()),
rate(_rate) {
[Link](4800);
clearLCD(); }
void Blinker1::run(uint32_t now){
selectLineOne();
[Link]("Blinker1 ");
[Link](millis());
incRunTime(rate); }
// Timed task 2
class Blinker2 : public TimedTask
{public:
// Create a new blinker for the specified pin and rate.
Blinker2( uint32_t _rate);
virtual void run(uint32_t now);
private:
uint32_t rate; // Blink rate. };
Blinker2::Blinker2( uint32_t _rate)
: TimedTask(millis()),
rate(_rate)
{ [Link](4800); }
void Blinker2::run(uint32_t now)
{selectLineTwo();
[Link]("Blinker2 ");
[Link](millis());
incRunTime(rate);}
void setup()
{ [Link](4800);}
// Main program.
void loop(){
[Link]("Se afiseaza ecoul transmiterii unui caracter");
// Create the tasks.
Blinker blinker(13, 25);
Echoer echoer;
Blinker1 blinker1(10000);
Blinker2 blinker2(10000);
// Initialise the task list and scheduler.
Task *tasks[] = { &blinker1, &blinker, &blinker2, &echoer};
TaskScheduler sched(tasks, NUM_TASKS(tasks));
// Run the scheduler - never returns.
[Link]();}
//SerialLCD Functions
void selectLineOne(){ //puts the cursor at line 0 char 0.
[Link](0xFE, BYTE); //command flag
[Link](128, BYTE); //position}
void selectLineTwo(){ //puts the cursor at line 2 char 0.
[Link](0xFE, BYTE); //command flag
[Link](192, BYTE); //position}
void selectLineThree(){ //puts the cursor at line 3 char 0.
[Link](0xFE, BYTE); //command flag
[Link](148, BYTE); //position}
void selectLineFour(){ //puts the cursor at line 4 char 0.
[Link](0xFE, BYTE); //command flag
[Link](212, BYTE); //position}
void goTo(int position) { //position = line 1: 0-19, line 2: 20-39, etc, 79+ defaults back to 0
if (position<20){
[Link](0xFE, BYTE); //command flag
[Link]((position+128), BYTE); //position }
else if (position<40){
[Link](0xFE, BYTE); //command flag
[Link]((position+128+64-20), BYTE); //position }
else if (position<60){
[Link](0xFE, BYTE); //command flag
[Link]((position+128+20-40), BYTE); //position }
else if (position<80){
[Link](0xFE, BYTE); //command flag
[Link]((position+128+84-60), BYTE); //position }
else { goTo(0); }
}
void clearLCD(){
[Link](0xFE, BYTE); //command flag
[Link](0x01, BYTE); //clear command.}
void backlightOn(){ //turns on the backlight
[Link](0x7C, BYTE); //command flag for backlight stuff
[Link](157, BYTE); //light level.}
void backlightOff(){ //turns off the backlight
[Link](0x7C, BYTE); //command flag for backlight stuff
[Link](128, BYTE); //light level for off.}
void backlight50(){ //sets the backlight at 50% brightness
[Link](0x7C, BYTE); //command flag for backlight stuff
[Link](143, BYTE); //light level for off.}
void serCommand(){ //a general function to call the command flag for issuing all other commands
[Link](0xFE, BYTE);}
#include <Task.h>
#include <TaskScheduler.h>
// Timed task to blink a LED.
class Blinker : public TimedTask
{public:
// Create a new blinker for the specified pin and rate.
Blinker(uint8_t _pin, uint32_t _rate);
virtual void run(uint32_t now);
private: uint8_t pin; // LED pin.
uint32_t rate; // Blink rate.
bool on; // Current state of the LED.};
Blinker::Blinker(uint8_t _pin, uint32_t _rate)
: TimedTask(millis()),
pin(_pin),
rate(_rate),
on(false){
pinMode(pin, OUTPUT); // Set pin for output.}
void Blinker::run(uint32_t now){
// If the LED is on, turn it off and remember the state.
if (on) { digitalWrite(pin, LOW);
on = false;
// If the LED is off, turn it on and remember the state.
} else { digitalWrite(pin, HIGH);
on = true; }
// Run again in the required number of milliseconds.
incRunTime(rate);}
// Task to echo serial input.
class Echoer : public Task{
public:
Echoer();
virtual void run(uint32_t now);
virtual bool canRun(uint32_t now);};
Echoer::Echoer()
: Task(){
[Link](9600);}
bool Echoer::canRun(uint32_t now)
{ return [Link]() > 0;}
void Echoer::run(uint32_t now){
while ([Link]() > 0) {
int byte = [Link]();
[Link](byte, BYTE);
if (byte == '\r') {
[Link]('\n', BYTE); }
} }
void setup(){}
// Main program.
void loop()
{ // Create the tasks.
Blinker blinker(13, 25);
Echoer echoer;
// Initialise the task list and scheduler.
Task *tasks[] = { &blinker, &echoer };
TaskScheduler sched(tasks, NUM_TASKS(tasks));
// Run the scheduler - never returns.
[Link]();}