From 54e1dd06984f2f2b215a09ed4506b0a0292e970c Mon Sep 17 00:00:00 2001 From: Thomas Lextrait Date: Tue, 18 Nov 2014 08:57:04 -0500 Subject: [PATCH 1/5] Fixed spelling --- Thread.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Thread.h b/Thread.h index 1bd457a..e7cecd8 100644 --- a/Thread.h +++ b/Thread.h @@ -1,7 +1,7 @@ /* - Thread.h - An runnable object + Thread.h - A runnable object - Thread is responsable for holding the "action" for something, + Thread is responsible for holding the "action" for something, also, it responds if it "should" or "should not" run, based on the current time; @@ -20,7 +20,7 @@ /* Uncomment this line to enable ThreadName Strings. - It might be usefull if you are loging thread with Serial, + It might be useful if you are logging thread with Serial, or displaying a list of threads... */ // #define USE_THREAD_NAMES 1 @@ -30,7 +30,7 @@ class Thread{ // Desired interval between runs long interval; - // Last runned time in Ms + // Last run time in Ms long last_run; // Scheduled run in Ms (MUST BE CACHED) From 21d19f40921efd0bd0e221abbf7194029b223bd4 Mon Sep 17 00:00:00 2001 From: Thomas Lextrait Date: Tue, 18 Nov 2014 08:57:59 -0500 Subject: [PATCH 2/5] Renamed header files to hpp --- Thread.h => Thread.hpp | 0 ThreadController.h => ThreadController.hpp | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Thread.h => Thread.hpp (100%) rename ThreadController.h => ThreadController.hpp (100%) diff --git a/Thread.h b/Thread.hpp similarity index 100% rename from Thread.h rename to Thread.hpp diff --git a/ThreadController.h b/ThreadController.hpp similarity index 100% rename from ThreadController.h rename to ThreadController.hpp From 7f6624f74c42bae282e4d3ad42a9fbbf97400042 Mon Sep 17 00:00:00 2001 From: Thomas Lextrait Date: Tue, 18 Nov 2014 09:00:35 -0500 Subject: [PATCH 3/5] Fixed comments --- ThreadController.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ThreadController.hpp b/ThreadController.hpp index 9914f3a..5bb91c3 100644 --- a/ThreadController.hpp +++ b/ThreadController.hpp @@ -1,7 +1,7 @@ /* - ThreadController.h - Controlls a list of Threads with different timings + ThreadController.h - Controls a list of Threads with different timings - Basicaly, what it does is to keep track of current Threads and run when + Basically, what it does is to keep track of current Threads and run when necessary. ThreadController is an extended class of Thread, because of that, @@ -28,21 +28,21 @@ class ThreadController: public Thread{ public: ThreadController(long _interval = 0); - // run() Method is overrided + // run() Method is overridden 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) + // Removes the given thread (given the Thread* or ThreadID) void remove(int _id); void remove(Thread* _thread); // Removes all threads void clear(); - // Return the quantity of Threads + // Returns the number of Threads in the controller int size(bool cached = true); // Return the I Thread on the array From 0c5c42a71331abcac501a662b341283f54537171 Mon Sep 17 00:00:00 2001 From: Thomas Lextrait Date: Tue, 18 Nov 2014 09:01:20 -0500 Subject: [PATCH 4/5] Added new line at end of cpp --- Thread.cpp | 2 +- ThreadController.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Thread.cpp b/Thread.cpp index f02d73a..2a17304 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -54,4 +54,4 @@ void Thread::run(){ // Update last_run and _cached_next_run runned(); -} \ No newline at end of file +} diff --git a/ThreadController.cpp b/ThreadController.cpp index e4973cd..1ace14a 100644 --- a/ThreadController.cpp +++ b/ThreadController.cpp @@ -15,7 +15,7 @@ ThreadController::ThreadController(long _interval): Thread(){ } /* - ThreadController run() (cool stuf) + ThreadController run() */ void ThreadController::run(){ // Run this thread before @@ -112,4 +112,4 @@ Thread* ThreadController::get(int index){ } return NULL; -} \ No newline at end of file +} From 88e761ea563bedef2e982d1e8ecd83b7a49e1277 Mon Sep 17 00:00:00 2001 From: Thomas Lextrait Date: Tue, 18 Nov 2014 12:50:26 -0500 Subject: [PATCH 5/5] Updated with templating to support passing a variable type argument to the thread --- Thread.cpp | 59 ++++++++++++++++++++++++++------- Thread.hpp | 78 -------------------------------------------- ThreadController.cpp | 44 ++++++++++++++++--------- ThreadController.hpp | 53 ------------------------------ 4 files changed, 77 insertions(+), 157 deletions(-) delete mode 100644 Thread.hpp delete mode 100644 ThreadController.hpp diff --git a/Thread.cpp b/Thread.cpp index 2a17304..0c502fe 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -1,7 +1,10 @@ + #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; @@ -13,9 +16,28 @@ Thread::Thread(void (*callback)(void), long _interval){ #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; -void Thread::runned(long time){ + ThreadID = (int)this; + #ifdef USE_THREAD_NAMES + ThreadName = "Thread "; + ThreadName = ThreadName + ThreadID; + #endif + + setInterval(_interval); +} + +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(); + hasRun(); } diff --git a/Thread.hpp b/Thread.hpp deleted file mode 100644 index e7cecd8..0000000 --- a/Thread.hpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - Thread.h - A runnable object - - Thread is responsible 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 useful if you are logging thread with Serial, - or displaying a list of threads... -*/ -// #define USE_THREAD_NAMES 1 - -class Thread{ -protected: - // Desired interval between runs - long interval; - - // Last run 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 1ace14a..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 @@ -17,10 +18,18 @@ ThreadController::ThreadController(long _interval): Thread(){ /* 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){ diff --git a/ThreadController.hpp b/ThreadController.hpp deleted file mode 100644 index 5bb91c3..0000000 --- a/ThreadController.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - ThreadController.h - Controls a list of Threads with different timings - - Basically, 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 overridden - 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); - - // Removes the given thread (given the Thread* or ThreadID) - void remove(int _id); - void remove(Thread* _thread); - - // Removes all threads - void clear(); - - // Returns the number of Threads in the controller - int size(bool cached = true); - - // Return the I Thread on the array - // Returns NULL if none found - Thread* get(int index); -}; - -#endif