mthread 0.5

mthread.h

Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 
00005 
00006 // Arduino Multi-Threading Library (mthread)
00007 
00008 // Copyright (C) 2010, 2011 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 General Public License as
00012 // published by the Free Software Foundation, either version 3 of the
00013 // 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 // General Public License for more details.
00019 
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program.  If not, see: http://www.gnu.org/licenses/
00022 
00023 #ifndef MTHREAD_H
00024 #define MTHREAD_H
00025 
00026 #include "../newdel/newdel.h"
00027 
00029 #define DEFAULT_DEBOUNCE 50
00030 
00031 class ThreadList;
00032 void loop();
00033 
00035 class Thread
00036 {
00037 public:
00038 
00040   enum Mode
00041     {
00042       run_mode,                 
00043       pause_mode,               
00044       sleep_mode,               
00045 
00046       sleep_milli_mode,         
00047 
00048       sleep_micro_mode,         
00049 
00050       kill_mode                 
00051 
00052     };
00053 
00055   Thread();
00056 
00058   virtual ~Thread();
00059 
00061 
00063   Mode get_mode() const;
00064 
00066 
00071 
00075 
00077   bool kill(bool force = false);
00078 
00080 
00083 
00085 
00087   bool pause();
00088 
00090 
00092   bool resume();
00093 
00095 
00099 
00101 
00103   bool sleep(unsigned long t);
00104 
00107 
00111 
00114 
00116   bool sleep_micro(unsigned long t);
00117 
00120 
00124 
00127 
00129   bool sleep_milli(unsigned long t);
00130 
00131 protected:
00132 
00134 
00139 
00141 
00145   virtual bool loop();
00146 
00148 
00153   bool kill_flag;
00154 
00155 private:
00156 
00159 
00161 
00164 
00170   bool call();
00171 
00172   unsigned long stop_time,      
00173 
00174     wait_time;                  
00175 
00176   Mode mode;                    
00177 
00178 
00179 
00180   friend class ThreadList;
00181   friend void loop();
00182 };
00183 
00185 
00189 
00196 class ThreadList : public Thread
00197 {
00198 public:
00199 
00201 
00205   ThreadList(bool keep = false);
00206 
00208   ~ThreadList();
00209 
00211 
00213 
00215   bool add_thread(Thread *t);
00216 
00217 protected:
00218 
00220 
00222   bool loop();
00223 
00224 private:
00225 
00226   Thread **thread;              
00227 
00228   unsigned thread_count,        
00229 
00230     thread_index;               
00231 
00232   bool keep_flag;               
00233 
00234 
00235 
00236 
00237 };
00238 
00240 class EventHandler : public Thread
00241 {
00242 public:
00243 
00245   EventHandler();
00246 
00248   virtual ~EventHandler();
00249 
00250 protected:
00251 
00254 
00257 
00259   virtual bool condition();
00260 
00262 
00264   bool loop();
00265 
00267 
00270 
00274   virtual bool on_event();
00275 
00276 private:
00277 
00278   bool trigger;                 
00279 
00280 
00281 
00282 
00283 };
00284 
00286 class SwitchInput : public Thread
00287 {
00288 public:
00289 
00291   enum Type {
00292     pull_up_internal,           
00293 
00294     pull_up,                    
00295 
00296     pull_down                   
00297 
00298   };
00299 
00301 
00304 
00306 
00308   SwitchInput(int pin,
00309               unsigned long debounce = DEFAULT_DEBOUNCE,
00310               Type type = pull_up_internal);
00311 
00313   virtual ~SwitchInput();
00314 
00316 
00318   bool is_closed();
00319 
00321 
00323   bool is_open();
00324 
00326   virtual void on_close();
00327 
00329   virtual void on_open();
00330 
00333 
00336   unsigned long time_closed();
00337 
00340 
00343   unsigned long time_open();
00344 
00345 protected:
00346 
00348 
00350   bool loop();
00351 
00352 private:
00353 
00354   unsigned long debounce,       
00355 
00356     last_change,                
00357 
00358 
00359     last_debounce;              
00360 
00361 
00362   int current_value,            
00363 
00364     last_value,                 
00365 
00366     pin;                        
00367 
00368   Type type;                    
00369 
00370 
00371 };
00372 
00374 
00377 extern ThreadList *main_thread_list;
00378 
00379 #endif  // MTHREAD_H
00380 
00381 // jl
 All Classes Files Functions Variables Enumerations Enumerator Defines