Skip to content

Commit

Permalink
modified: examples/blink/blink.ino
Browse files Browse the repository at this point in the history
	new file:   examples/multi-blink/multi-blink.ino
	new file:   examples/multi-toggle/multi-toggle.ino
	new file:   examples/toggle/toggle.ino
	modified:   src/tcone.cpp
	modified:   src/tcone.h
  • Loading branch information
MicroBeaut committed Apr 30, 2024
1 parent 76f8a8e commit 97ccdc1
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 12 deletions.
20 changes: 13 additions & 7 deletions examples/blink/blink.ino
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// The LED_BUILTIN exhibits a continuous blinking pattern
// with a frequency of 500ms in the ON state followed by 500ms in the OFF state.

#include "TCone.h"

// Define the task periodic in milliseconds
#define TASK_PERIOD 20
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 1

// TaskMember declaration
// Single:
// taskMember = {duration, manualReset, enable};
// Array:
// taskMembers[] = {
// {duration, manualReset, enable},
// {duration, manualReset, enable}
// taskMembers[N] = {
// {duration_0, manualReset_0, enable_0},
// {duration_1, manualReset_1, enable_1},
// {..., ..., ...}
// {duration_N-1, manualReset_N-1, enable_N-1},
// };
TaskMember taskMember = {500};

Expand All @@ -21,7 +26,7 @@ TaskMember taskMember = {500};
// TaskMember *tasks - Pointer of TaskMember
// uint16_t size - Number of Tasks
// It is mandatory to name the TCone declaration as "Task". No other name should be used.
TCone Task(TASK_PERIOD, &taskMember, TASK_SIZE);
TCone Tasks(TASK_PERIOD, &taskMember, TASK_SIZE);

// OnTaskElapsed Function declaration with id and TaskMember
void OnTaskElapsed(volatile uint8_t id, volatile TaskMember *member);
Expand All @@ -31,9 +36,10 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT);

// Implement a callback function that can be triggered when the task's allocated time has elapsed.
Task.callbackOnTaskElapsed(OnTaskElapsed);
Tasks.callbackOnTaskElapsed(OnTaskElapsed);

// It is imperative to utilize the begin() function to internally configure Timer0 and commence its operation.
Task.begin();
Tasks.begin();
}

void loop() {
Expand Down
67 changes: 67 additions & 0 deletions examples/multi-blink/multi-blink.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// The device is equipped with three LED lights, each with a distinct blinking pattern.
// The RED LED blinks at a frequency of 500ms ON and 500ms OFF,
// the YELLOW LED blinks at a frequency of 1000ms ON and 1000ms OFF,
// and the GREEN LED blinks at a frequency of 2000ms ON and 2000ms OFF.

#include "TCone.h"

// Define the task periodic in milliseconds
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 3

// TaskMember declaration
// Single:
// taskMember = {duration, manualReset, enable};
// Array:
// taskMembers[N] = {
// {duration_0, manualReset_0, enable_0},
// {duration_1, manualReset_1, enable_1},
// {..., ..., ...}
// {duration_N-1, manualReset_N-1, enable_N-1},
// };
TaskMember taskMember[TASK_SIZE] = {
{500},
{1000},
{2000}
};

// Task declaration
// TCone Task (period, *tasks, size);
// Para:
// uint16_t period - in milliseconds
// TaskMember *tasks - Pointer of TaskMember
// uint16_t size - Number of Tasks
// It is mandatory to name the TCone declaration as "Task". No other name should be used.
TCone Tasks(TASK_PERIOD, taskMember, TASK_SIZE);

const uint8_t ledPins[TASK_SIZE] = {4, 3, 2};

// OnTaskElapsed Function declaration with id and TaskMember
void OnTaskElapsed(volatile uint8_t id, volatile TaskMember *member);

void setup() {
// Set the pin mode of "LED_BUILTIN" as an output
for (uint8_t index = 0; index < TASK_SIZE; index++) {
pinMode(ledPins[index], OUTPUT);
}

// Implement a callback function that can be triggered when the task's allocated time has elapsed.
Tasks.callbackOnTaskElapsed(OnTaskElapsed);

// It is imperative to utilize the begin() function to internally configure Timer0 and commence its operation.
Tasks.begin();
}

void loop() {
// Use a loop to write the value to LEDs to represent the state status.
for (uint8_t index = 0; index < TASK_SIZE; index++) {
// Write the state value to LED.
digitalWrite(ledPins[index], taskMember[index].state);
}
}

void OnTaskElapsed(volatile uint8_t id, volatile TaskMember *member) {
// Toggle the state
member->state = !member->state;
}
66 changes: 66 additions & 0 deletions examples/multi-toggle/multi-toggle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// The LED_BUILTIN exhibits a continuous blinking pattern
// with a frequency of 500ms in the ON state followed by 500ms in the OFF state.

#include "TCone.h"

// Define the task periodic in milliseconds
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 3

// TaskMember declaration
// Single:
// taskMember = {duration, manualReset, enable};
// Array:
// taskMembers[N] = {
// {duration_0, manualReset_0, enable_0},
// {duration_1, manualReset_1, enable_1},
// {..., ..., ...}
// {duration_N-1, manualReset_N-1, enable_N-1},
// };
TaskMember taskMember[TASK_SIZE] = {
{20},
{20},
{20}
};

// Tasks declaration
// Para:
// uint16_t period - in milliseconds
// TaskMember *tasks - Pointer of TaskMember
// uint16_t size - Number of Tasks
// It is mandatory to name the TCone declaration as "Tasks". No other name should be used.
TCone Tasks(TASK_PERIOD, taskMember, TASK_SIZE);

const uint8_t buttonPins[TASK_SIZE] = {A3, A2, A1};
const uint8_t ledPins[TASK_SIZE] = {4, 3, 2};
bool toggles[TASK_SIZE];


void setup() {
// Set the pin mode of "LED_BUILTIN" as an output
for (uint8_t index = 0; index < TASK_SIZE; index++) {
pinMode(buttonPins[index], INPUT_PULLUP);
pinMode(ledPins[index], OUTPUT);
}

Tasks.manualReset(true);
// It is imperative to utilize the begin() function to internally configure Timer0 and commence its operation.
Tasks.begin();
}

void loop() {
// Write the state value to LED_BUILTIN.

for (uint8_t index = 0; index < TASK_SIZE; index++) {
if (taskMember[index].done) {
bool prevState = taskMember[index].state;
taskMember[index].state = digitalRead(buttonPins[index]);
if (prevState & !taskMember[index].state) {
toggles[index] = !toggles[index];
digitalWrite(ledPins[index], toggles[index]);
}
Tasks.reset(index);
}
}
}
55 changes: 55 additions & 0 deletions examples/toggle/toggle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// The LED_BUILTIN exhibits a continuous blinking pattern
// with a frequency of 500ms in the ON state followed by 500ms in the OFF state.

#include "TCone.h"

#define BUTTON 12
// Define the task periodic in milliseconds
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 1

// TaskMember declaration
// Single:
// taskMember = {duration, manualReset, enable};
// Array:
// taskMembers[N] = {
// {duration_0, manualReset_0, enable_0},
// {duration_1, manualReset_1, enable_1},
// {..., ..., ...}
// {duration_N-1, manualReset_N-1, enable_N-1},
// };
TaskMember taskMember = {10};

// Tasks declaration
// Para:
// uint16_t period - in milliseconds
// TaskMember *tasks - Pointer of TaskMember
// uint16_t size - Number of Tasks
// It is mandatory to name the TCone declaration as "Tasks". No other name should be used.
TCone Tasks(TASK_PERIOD, &taskMember, TASK_SIZE);

bool toggle;

void setup() {
pinMode(BUTTON, INPUT_PULLUP);
// Set the pin mode of "LED_BUILTIN" as an output
pinMode(LED_BUILTIN, OUTPUT);

Tasks.manualReset(true);
// It is imperative to utilize the begin() function to internally configure Timer0 and commence its operation.
Tasks.begin();
}

void loop() {
// Write the state value to LED_BUILTIN.
if (taskMember.done) {
bool prevState = taskMember.state;
taskMember.state = digitalRead(BUTTON);
if (prevState & !taskMember.state) {
toggle = !toggle;
digitalWrite(LED_BUILTIN, toggle);
}
Tasks.reset();
}
}
2 changes: 1 addition & 1 deletion src/TCone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ void TCone::execute() {
}

ISR (TIMER1_COMPA_vect) {
Task.execute();
Tasks.execute();
}
#endif // ARDUINO_ARCH_AVR - TCON-TIMER
8 changes: 4 additions & 4 deletions src/TCone.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#define TCONE_MAX_CLOCK_SELECT 5 // Maximum Clock Select Mode
#define TCONE_DEFAULT_CLOCK_SELECT 5 // Default Clock Select Mode

#define TCONE_PERIOD_MIN 10 // 10 milliseconds
#define TCONE_PERIOD_MAX 500 // 500 milliseconds
#define TCONE_DEFAULT_PERIOD 100 // 100 milliseconds
#define TCONE_PERIOD_MIN 1 // 1 milliseconds
#define TCONE_PERIOD_MAX 500 // 500 milliseconds
#define TCONE_DEFAULT_PERIOD 100 // 100 milliseconds


/// @brief Prescaler Divider
Expand Down Expand Up @@ -79,6 +79,6 @@ class TCone {
#endif // ARDUINO_ARCH_AVR
};

extern TCone Task;
extern TCone Tasks;

#endif // TCONE_H

0 comments on commit 97ccdc1

Please sign in to comment.