diff --git a/Thread.cpp b/Thread.cpp index f02d73a..0c502fe 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -1,7 +1,28 @@ + #include "Thread.h" -Thread::Thread(void (*callback)(void), long _interval){ +template +Thread::Thread(void (*callback)(), long _interval){ + enabled = true; + _callbackArgumentSet = false; + onRun(callback); + _cached_next_run = 0; + last_run = 0; + + ThreadID = (int)this; + #ifdef USE_THREAD_NAMES + ThreadName = "Thread "; + ThreadName = ThreadName + ThreadID; + #endif + + setInterval(_interval); +} + +template +Thread::Thread(void (*callback)(Arg), Arg arg, long _interval){ enabled = true; + _callbackArgument = arg; + _callbackArgumentSet = true; onRun(callback); _cached_next_run = 0; last_run = 0; @@ -13,9 +34,10 @@ Thread::Thread(void (*callback)(void), long _interval){ #endif setInterval(_interval); -}; +} -void Thread::runned(long time){ +template +void Thread::hasRun(long time){ // If less than 0, than get current ticks if(time < 0) time = millis(); @@ -27,7 +49,8 @@ void Thread::runned(long time){ _cached_next_run = last_run + interval; } -void Thread::setInterval(long _interval){ +template +void Thread::setInterval(long _interval){ // Filter intervals less than 0 interval = (_interval < 0? 0: _interval); @@ -35,8 +58,9 @@ void Thread::setInterval(long _interval){ _cached_next_run = last_run + interval; } -bool Thread::shouldRun(long time){ - // If less than 0, than get current ticks +template +bool Thread::shouldRun(long time){ + // If less than 0, then get current ticks if(time < 0) time = millis(); @@ -44,14 +68,27 @@ bool Thread::shouldRun(long time){ return ((time >= _cached_next_run) && enabled); } -void Thread::onRun(void (*callback)(void)){ +template +void Thread::onRun(void (*callback)(void)){ _onRun = callback; } -void Thread::run(){ - if(_onRun != NULL) - _onRun(); +template +void Thread::onRun(void (*callback)(Arg)){ + _onRunArg = callback; +} +template +void Thread::run(){ + + if(_callbackArgumentSet){ + if(_onRunArg != NULL) + _onRunArg(this->_callbackArgument); + }else{ + if(_onRun != NULL) + _onRun(); + } + // Update last_run and _cached_next_run - runned(); -} \ No newline at end of file + hasRun(); +} diff --git a/Thread.h b/Thread.h deleted file mode 100644 index 1bd457a..0000000 --- a/Thread.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - Thread.h - An runnable object - - Thread is responsable for holding the "action" for something, - also, it responds if it "should" or "should not" run, based on - the current time; - - For instructions, go to https://github.com/ivanseidel/ArduinoThread - - Created by Ivan Seidel Gomes, March, 2013. - Released into the public domain. -*/ - -#ifndef Thread_h -#define Thread_h - -#include -#include - -/* - Uncomment this line to enable ThreadName Strings. - - It might be usefull if you are loging thread with Serial, - or displaying a list of threads... -*/ -// #define USE_THREAD_NAMES 1 - -class Thread{ -protected: - // Desired interval between runs - long interval; - - // Last runned time in Ms - long last_run; - - // Scheduled run in Ms (MUST BE CACHED) - long _cached_next_run; - - /* - IMPORTANT! Run after all calls to run() - Updates last_run and cache next run. - NOTE: This MUST be called if extending - this class and implementing run() method - */ - void runned(long time=-1); - - // Callback for run() if not implemented - void (*_onRun)(void); - -public: - - // If the current Thread is enabled or not - bool enabled; - - // ID of the Thread (initialized from memory adr.) - int ThreadID; - - #ifdef USE_THREAD_NAMES - // Thread Name (used for better UI). - String ThreadName; - #endif - - Thread(void (*callback)(void) = NULL, long _interval = 0); - - // Set the desired interval for calls, and update _cached_next_run - virtual void setInterval(long _interval); - - // Return if the Thread should be runned or not - virtual bool shouldRun(long time = -1); - - // Callback set - void onRun(void (*callback)(void)); - - // Runs Thread - virtual void run(); -}; - -#endif diff --git a/ThreadController.cpp b/ThreadController.cpp index e4973cd..01f4ac8 100644 --- a/ThreadController.cpp +++ b/ThreadController.cpp @@ -1,11 +1,12 @@ #include "Thread.h" #include "ThreadController.h" -ThreadController::ThreadController(long _interval): Thread(){ +template +ThreadController::ThreadController(long _interval): Thread(){ cached_size = 0; clear(); - setInterval(_interval); + this->setInterval(_interval); #ifdef USE_THREAD_NAMES // Overrides name @@ -15,12 +16,20 @@ ThreadController::ThreadController(long _interval): Thread(){ } /* - ThreadController run() (cool stuf) + ThreadController run() */ -void ThreadController::run(){ - // Run this thread before - if(_onRun != NULL) - _onRun(); +template +void ThreadController::run(){ + + if(this->_callbackArgumentSet){ + // Run this thread before + if(this->_onRunArg != NULL) + this->_onRunArg(this->_callbackArgument); + }else{ + // Run this thread before + if(this->_onRun != NULL) + this->_onRun(); + } long time = millis(); int checks = 0; @@ -34,15 +43,15 @@ void ThreadController::run(){ } } - // ThreadController extends Thread, so we should flag as runned thread - runned(); + // ThreadController extends Thread, so we should flag as hasRun thread + this->hasRun(); } - /* List controller (boring part) */ -bool ThreadController::add(Thread* _thread){ +template +bool ThreadController::add(Thread* _thread){ // Check if the Thread already exists on the array for(int i = 0; i < MAX_THREADS; i++){ if(thread[i] != NULL && thread[i]->ThreadID == _thread->ThreadID) @@ -63,7 +72,8 @@ bool ThreadController::add(Thread* _thread){ return false; } -void ThreadController::remove(int id){ +template +void ThreadController::remove(int id){ // Find Threads with the id, and removes bool found = false; for(int i = 0; i < MAX_THREADS; i++){ @@ -75,18 +85,21 @@ void ThreadController::remove(int id){ } } -void ThreadController::remove(Thread* _thread){ +template +void ThreadController::remove(Thread* _thread){ remove(_thread->ThreadID); } -void ThreadController::clear(){ +template +void ThreadController::clear(){ for(int i = 0; i < MAX_THREADS; i++){ thread[i] = NULL; } cached_size = 0; } -int ThreadController::size(bool cached){ +template +int ThreadController::size(bool cached){ if(cached) return cached_size; @@ -100,7 +113,8 @@ int ThreadController::size(bool cached){ return cached_size; } -Thread* ThreadController::get(int index){ +template +Thread* ThreadController::get(int index){ int pos = -1; for(int i = 0; i < MAX_THREADS; i++){ if(thread[i] != NULL){ @@ -112,4 +126,4 @@ Thread* ThreadController::get(int index){ } return NULL; -} \ No newline at end of file +} diff --git a/ThreadController.h b/ThreadController.h deleted file mode 100644 index 9914f3a..0000000 --- a/ThreadController.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - ThreadController.h - Controlls a list of Threads with different timings - - Basicaly, what it does is to keep track of current Threads and run when - necessary. - - ThreadController is an extended class of Thread, because of that, - it allows you to add a ThreadController inside another ThreadController... - - For instructions, go to https://github.com/ivanseidel/ArduinoThread - - Created by Ivan Seidel Gomes, March, 2013. - Released into the public domain. -*/ - -#ifndef ThreadController_h -#define ThreadController_h - -#include "Thread.h" -#include "inttypes.h" - -#define MAX_THREADS 15 - -class ThreadController: public Thread{ -protected: - Thread* thread[MAX_THREADS]; - int cached_size; -public: - ThreadController(long _interval = 0); - - // run() Method is overrided - void run(); - - // Adds a thread in the first available slot (remove first) - // Returns if the Thread could be added or not - bool add(Thread* _thread); - - // remove the thread (given the Thread* or ThreadID) - void remove(int _id); - void remove(Thread* _thread); - - // Removes all threads - void clear(); - - // Return the quantity of Threads - int size(bool cached = true); - - // Return the I Thread on the array - // Returns NULL if none found - Thread* get(int index); -}; - -#endif