Permalink
Browse files

working on seperating to library... premature

  • Loading branch information...
1 parent 877632b commit dcdf856101ded5053e4530ca7bc91cc5e37ea1c6 @balarsen committed Mar 20, 2012
Showing with 9 additions and 311 deletions.
  1. +4 −87 TaskManager.cpp
  2. +3 −123 TaskManager.h
  3. +2 −101 example1/example1.ino
View
@@ -18,33 +18,16 @@
#ifndef TASKMANAGER_cpp
#define TASKMANAGER_cpp
-#include <TimerOne.h>
+#include <stdint.h>
-// the digital pins that connect to the LEDs
-#define redLEDpin 2
-#define greenLEDpin 3
+#include "TaskManager.h"
+#include <TimerOne.h>
#define SERIAL_DEBUG
-/* useful constants
- F_CPU - CPU frequency
- */
-
////////////////////////////////////////////
// TASK definition, to header file
////////////////////////////////////////////
-class Task {
- public:
- // uint8_t taskNum; // should a task know its own number?
- Task( void (*fn)(), uint32_t ticks);
- Task operator++(); //Prefix decrement operator (++x)
- Task operator++(int); //Postfix decrement operator (x++)
- private:
- void (*function)();
- uint32_t counter;
- uint32_t max_cnt;
-};
-
Task::Task(void (*fn)(), uint32_t ticks ) {
#ifdef SERIAL_DEBUG
Serial.println("in constructor");
@@ -77,26 +60,12 @@ Task Task::operator++() {
if (counter >= max_cnt) {
counter = 0;
function();
- }
-
+ }
}
////////////////////////////////////////////
// TaskManager definition, to header file
////////////////////////////////////////////
-class TaskManager {
- public:
- TaskManager(uint8_t nTasks, uint32_t useconds); // nTasks is the number of task you will have
- void attach( Task *task, uint8_t taskNum );
- void start();
- void stop();
-// void overflow();
- Task* tasks;
- uint8_t nTasks;
- private:
- uint32_t useconds;
-};
-
TaskManager::TaskManager(uint8_t nTasks, uint32_t useconds) {
nTasks = nTasks; // set to class variable
useconds = useconds;
@@ -118,56 +87,4 @@ void TaskManager::stop() {
}
-
-////////////////////////////////////////////
-// Testing demo functions
-////////////////////////////////////////////
-void error(void) {
- #ifdef SERIAL_DEBUG
- Serial.println("ERROR STATE");
- #endif
- digitalWrite(greenLEDpin, HIGH);
- digitalWrite(redLEDpin, HIGH);
-}
-
-void isr(void)
-{
- digitalWrite(greenLEDpin, digitalRead(greenLEDpin) ^ 1);
- #ifdef SERIAL_DEBUG
- Serial.println("In the ISR");
- #endif
-}
-
-
-
-TaskManager TM = TaskManager(1, 10000); // 10000 useconds
-
-Task tsk = Task(isr, 100);
-
-void overflow() {
- uint8_t i;
- for (i=0; i<TM.nTasks; i++){
- TM.tasks[i]++;
- }
-}
-
-
-
-void setup()
-{
- TM.attach(&tsk, 0); // start at zero
- Timer1.attachInterrupt( overflow ); // don't want to have to manually do this
-
- pinMode(greenLEDpin, OUTPUT);
- #ifdef SERIAL_DEBUG
- Serial.begin(19200);
- #endif
-}
-
-void loop()
-{
-}
-
-
-
#endif
View
@@ -15,24 +15,15 @@
* See GitHub project https://github.com/balarsen/TaskManager for latest
* version, please feel free to fork and/or post patches
*/
-#ifndef TASKMANAGER_cpp
-#define TASKMANAGER_cpp
+#ifndef TASKMANAGER_h
+#define TASKMANAGER_h
-#include <TimerOne.h>
-
-// the digital pins that connect to the LEDs
-#define redLEDpin 2
-#define greenLEDpin 3
-
-#define SERIAL_DEBUG
+#include <stdint.h>
/* useful constants
F_CPU - CPU frequency
*/
-////////////////////////////////////////////
-// TASK definition, to header file
-////////////////////////////////////////////
class Task {
public:
// uint8_t taskNum; // should a task know its own number?
@@ -45,45 +36,6 @@ class Task {
uint32_t max_cnt;
};
-Task::Task(void (*fn)(), uint32_t ticks ) {
- #ifdef SERIAL_DEBUG
- Serial.println("in constructor");
- #endif
- function = fn;
- counter = 0;
- max_cnt = ticks;
-}
-
-Task Task::operator++(int val) {
- #ifdef SERIAL_DEBUG
- Serial.print("in Task++: ");
- #endif
- Task::operator++();
-}
-
-Task Task::operator++() {
- #ifdef SERIAL_DEBUG
- Serial.print("in ++Task: ");
- Serial.print("counter: ");
- Serial.print(counter, DEC);
- #endif
- ++counter;
- #ifdef SERIAL_DEBUG
- Serial.print(" counter: ");
- Serial.print(counter, DEC);
- Serial.print(" max_cnt: ");
- Serial.println(max_cnt, DEC);
- #endif
- if (counter >= max_cnt) {
- counter = 0;
- function();
- }
-
-}
-
-////////////////////////////////////////////
-// TaskManager definition, to header file
-////////////////////////////////////////////
class TaskManager {
public:
TaskManager(uint8_t nTasks, uint32_t useconds); // nTasks is the number of task you will have
@@ -97,77 +49,5 @@ class TaskManager {
uint32_t useconds;
};
-TaskManager::TaskManager(uint8_t nTasks, uint32_t useconds) {
- nTasks = nTasks; // set to class variable
- useconds = useconds;
- tasks = (Task *) calloc(nTasks, sizeof(Task));
-}
-
-void TaskManager::attach( Task *task, uint8_t taskNum ) {
- tasks[taskNum] = *task;
-}
-
-void TaskManager::start() {
- Timer1.initialize(useconds);
- Timer1.start();
- // Timer1.attachInterrupt( overflow ); // need to do something like this here...
-}
-
-void TaskManager::stop() {
- Timer1.stop();
-}
-
-
-
-////////////////////////////////////////////
-// Testing demo functions
-////////////////////////////////////////////
-void error(void) {
- #ifdef SERIAL_DEBUG
- Serial.println("ERROR STATE");
- #endif
- digitalWrite(greenLEDpin, HIGH);
- digitalWrite(redLEDpin, HIGH);
-}
-
-void isr(void)
-{
- digitalWrite(greenLEDpin, digitalRead(greenLEDpin) ^ 1);
- #ifdef SERIAL_DEBUG
- Serial.println("In the ISR");
- #endif
-}
-
-
-
-TaskManager TM = TaskManager(1, 10000); // 10000 useconds
-
-Task tsk = Task(isr, 100);
-
-void overflow() {
- uint8_t i;
- for (i=0; i<TM.nTasks; i++){
- TM.tasks[i]++;
- }
-}
-
-
-
-void setup()
-{
- TM.attach(&tsk, 0); // start at zero
- Timer1.attachInterrupt( overflow ); // don't want to have to manually do this
-
- pinMode(greenLEDpin, OUTPUT);
- #ifdef SERIAL_DEBUG
- Serial.begin(19200);
- #endif
-}
-
-void loop()
-{
-}
-
-
#endif
Oops, something went wrong.

0 comments on commit dcdf856

Please sign in to comment.