mthread
0.5.1
|
00001 // -*- mode: c++ -*- 00002 00005 00006 // Arduino Multi-Threading Library (mthread) 00007 00008 // Copyright (C) 2010-2012 Jonathan Lamothe <jonathan@jlamothe.net> 00009 00010 // This program is free software: you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public License 00012 // as published by the Free Software Foundation, either version 3 of 00013 // the License, or (at your option) any later version. 00014 00015 // This program is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this program. If not, see: 00022 // http://www.gnu.org/licenses/ 00023 00024 #ifndef MTHREAD_H 00025 #define MTHREAD_H 00026 00027 // 00028 // INCLUDES 00029 // 00030 00031 #include "../newdel/newdel.h" 00032 00034 #define DEFAULT_DEBOUNCE 50 00035 00036 class ThreadList; 00037 void loop(void); 00038 00039 // 00040 // CLASSES 00041 // 00042 00044 class Thread 00045 { 00046 public: 00047 00049 enum Mode 00050 { 00051 00053 run_mode, 00054 00056 pause_mode, 00057 00059 sleep_mode, 00060 00062 sleep_milli_mode, 00063 00065 sleep_micro_mode, 00066 00068 kill_mode 00069 00070 }; 00071 00073 Thread(); 00074 00076 virtual ~Thread(); 00077 00080 Mode get_mode() const; 00081 00091 bool kill(bool force = false); 00092 00097 bool pause(); 00098 00101 bool resume(); 00102 00110 bool sleep(unsigned long t); 00111 00119 bool sleep_micro(unsigned long t); 00120 00129 bool sleep_milli(unsigned long t); 00130 00131 protected: 00132 00143 virtual bool loop(); 00144 00150 bool kill_flag; 00151 00152 private: 00153 00162 bool call(); 00163 00165 unsigned long stop_time; 00166 00168 unsigned long wait_time; 00169 00172 Mode mode; 00173 00174 friend class ThreadList; 00175 friend void loop(void); 00176 }; 00177 00188 class ThreadList : public Thread 00189 { 00190 public: 00191 00193 00197 ThreadList(bool keep = false); 00198 00200 ~ThreadList(); 00201 00205 bool add_thread(Thread *t); 00206 00207 protected: 00208 00211 bool loop(); 00212 00213 private: 00214 00216 Thread **thread; 00217 00219 unsigned thread_count; 00220 00222 unsigned thread_index; 00223 00226 bool keep_flag; 00227 00228 }; 00229 00231 class EventHandler : public Thread 00232 { 00233 public: 00234 00236 EventHandler(); 00237 00239 virtual ~EventHandler(); 00240 00241 protected: 00242 00247 virtual bool condition(); 00248 00251 bool loop(); 00252 00259 virtual bool on_event(); 00260 00261 private: 00262 00265 bool trigger; 00266 00267 }; 00268 00270 class SwitchInput : public Thread 00271 { 00272 public: 00273 00275 enum Type { 00276 00278 pull_up_internal, 00279 00281 pull_up, 00282 00284 pull_down 00285 00286 }; 00287 00292 SwitchInput(int pin, 00293 unsigned long debounce = DEFAULT_DEBOUNCE, 00294 Type type = pull_up_internal); 00295 00297 virtual ~SwitchInput(); 00298 00301 bool is_closed() const; 00302 00305 bool is_open() const; 00306 00308 virtual void on_close(); 00309 00311 virtual void on_open(); 00312 00316 unsigned long time_closed() const; 00317 00321 unsigned long time_open() const; 00322 00323 protected: 00324 00327 bool loop(); 00328 00329 private: 00330 00332 unsigned long debounce; 00333 00336 unsigned long last_change; 00337 00340 unsigned long last_debounce; 00341 00343 int current_value; 00344 00347 int last_value; 00348 00350 int pin; 00351 00353 Type type; 00354 00355 }; 00356 00360 extern ThreadList *main_thread_list; 00361 00362 #endif // MTHREAD_H 00363 00364 // jl