Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Controller.h Source File

Controller.h

00001 #ifndef __CONTROLLER_H__
00002 #define __CONTROLLER_H__
00003 
00004 #include <string>
00005 
00006 #include "rtos.h"
00007 
00008 /** The base class for everything which interfaces with sensors or actuators.
00009  * Extending classes must implement \a std::string \a getName() and \a void \a update()
00010  */
00011 class Controller {
00012     public:
00013     
00014         /**
00015          * Initializes the Controller.
00016          * @param threaded whether or not the controller should be threaded.
00017          * @param interval_ms delay between each iteration, only if threaded is set to \a true.
00018         */
00019         Controller(bool threaded, int interval_ms);
00020         
00021         /**
00022          * Returns the name of this Controller. Must be implemented by extending classes.
00023          * @return The name of this Controller.
00024         */        
00025         virtual std::string getName() = 0;
00026         
00027         /**
00028          * Returns whether or not this Controller is being threaded.
00029          * @return \a true if this Controller is being threaded, \a false otherwise.
00030         */   
00031         bool isThreaded();
00032 
00033         /**
00034          * If this Controller is not threaded, \a update() is called. Otherwise, the first time a thread is spawned which repeatedly calls \a update().
00035          * When the thread has already been spawned no action is taken.
00036         */           
00037         void run();
00038 
00039         /**
00040          * Returns the interval between each iteration if threaded.
00041          * @return \a interval_ms as specified in the constructor.
00042         */                 
00043         int getIntervalMs();
00044 
00045         /**
00046          * Sets the underlying RTOS thread priority.
00047          * @see cmis_os.h
00048         */           
00049         void setPriority(osPriority priority);
00050 
00051         /**
00052          * Returns the thread priority.
00053          * @return the thread priority.
00054         */           
00055         osPriority getPriority();
00056         
00057     protected:
00058         /**
00059          * Updates the Controller. This is called once every \a interval_ms if \a threaded is set to \a true.
00060         */    
00061         virtual void update() = 0;
00062         
00063         /**
00064          * The number of times \a run() is called.
00065         */
00066         unsigned long num_iters;
00067                 
00068     private:
00069         /**
00070          * Whether or not this Controller is threaded.
00071         */    
00072         bool threaded;
00073         
00074         /**
00075          * Whether or not a thread has been spawned for this Controller.
00076         */        
00077         bool has_spawned;
00078         
00079         /**
00080          * Pointer to the spawned thread if \a has_spawned is \a true, undefined otherwise.
00081         */
00082         Thread *ctrl_thread;
00083         
00084         /**
00085          * Interval in ms between each iteration, only if \a threaded is set to \a true.
00086         */
00087         int interval_ms;
00088         
00089         /**
00090          * Priority of the underlying thread.
00091         */        
00092         osPriority prio;
00093         
00094         /**
00095          * This function is called when the thread is created, it repeatedly calls \a run() and waits for \a interval_ms milliseconds.
00096         */        
00097         static void threadStub(void const *args);
00098 };
00099 
00100 #endif