mbed for tic tac toe

Dependents:   TicTacToe

Fork of mbed by -deleted-

Files at this revision

API Documentation at this revision

Comitter:
simon.ford@mbed.co.uk
Date:
Fri Nov 14 15:25:20 2008 +0000
Parent:
2:969fc1867111
Child:
4:5d1359a283bc
Commit message:
Added Ticker and Timeout abstractions

Changed in this revision

DigitalOut.h Show annotated file Show diff for this revision Revisions of this file
FunctionPointer.h Show annotated file Show diff for this revision Revisions of this file
Ticker.h Show annotated file Show diff for this revision Revisions of this file
Timeout.h Show annotated file Show diff for this revision Revisions of this file
TimerEvent.h Show annotated file Show diff for this revision Revisions of this file
mbed.ar Show annotated file Show diff for this revision Revisions of this file
mbed.h Show annotated file Show diff for this revision Revisions of this file
--- a/DigitalOut.h	Thu Sep 18 14:02:33 2008 +0000
+++ b/DigitalOut.h	Fri Nov 14 15:25:20 2008 +0000
@@ -17,6 +17,8 @@
 
 public:
 
+	/* Group: Configuration Methods */
+	
 	/* Constructor: DigitalOut
 	 *  Create a DigitalOut connected to the specified pin
 	 *
@@ -25,6 +27,8 @@
 	 */
 	DigitalOut(int pin);
 
+	/* Group: Access Methods */
+		
 	/* Function: write
 	 *  Set the output, specified as 0 or 1 (int)
 	 *
@@ -43,6 +47,8 @@
 	 */
     int read();
    
+   	/* Group: Access Method Shorthand */
+   	 
 	/* Function: operator=
 	 *  A shorthand for <write>
 	 */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunctionPointer.h	Fri Nov 14 15:25:20 2008 +0000
@@ -0,0 +1,86 @@
+/* mbed Microcontroller Library - FunctionPointer
+ * Copyright (c) 2007-2008, sford
+ */
+
+#ifndef MBED_FUNCTIONPOINTER_H
+#define MBED_FUNCTIONPOINTER_H
+
+#include "string.h"
+
+namespace mbed { 
+
+/* Class FunctionPointer
+ *  A class for storing and calling a pointer to a static or member void function
+ */
+class FunctionPointer {
+
+public:
+
+	/* Constructor FunctionPointer
+	 *  Create a FunctionPointer, attaching a static function
+	 * 
+	 * Variables
+	 *  function - The void static function to attach (default is none)
+	 */
+	FunctionPointer(void (*function)(void) = 0);
+
+	/* Constructor FunctionPointer
+	 *  Create a FunctionPointer, attaching a member function
+	 * 
+	 * Variables
+	 *  object - The object pointer to invoke the member function on (i.e. the this pointer)
+	 *  function - The address of the void member function to attach 
+	 */
+	template<typename T>	
+	FunctionPointer(T *object, void (T::*member)(void)) {
+		attach(object, member);
+	}
+
+	/* Function attach
+	 *  Attach a static function
+	 * 
+	 * Variables
+	 *  function - The void static function to attach (default is none)
+	 */
+	void attach(void (*function)(void) = 0);
+	
+	/* Function attach
+	 *  Attach a member function
+	 * 
+	 * Variables
+	 *  object - The object pointer to invoke the member function on (i.e. the this pointer)
+	 *  function - The address of the void member function to attach 
+	 */
+	template<typename T>
+	void attach(T *object, void (T::*member)(void)) {
+		_object = static_cast<void*>(object);
+        memcpy(_member, (char*)&member, sizeof(member));
+		_membercaller = &FunctionPointer::membercaller<T>;
+		_function = 0;
+	}
+
+	/* Function call
+	 *  Call the attached static or member function
+	 */		
+	void call();
+		
+private:
+
+	template<typename T>
+	static void membercaller(void *object, char *member) {	
+		T* o = static_cast<T*>(object);
+		void (T::*m)(void);
+        memcpy((char*)&m, member, sizeof(m));
+		(o->*m)();
+	}
+	
+	void (*_function)(void);				// static function pointer - 0 if none attached
+	void *_object;							// object this pointer - 0 if none attached
+	char _member[16];						// raw member function pointer storage - converted back by registered _membercaller
+	void (*_membercaller)(void*, char*);	// registered membercaller function to convert back and call _member on _object
+	
+};
+
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ticker.h	Fri Nov 14 15:25:20 2008 +0000
@@ -0,0 +1,89 @@
+/* mbed Microcontroller Library - Ticker
+ * Copyright (c) 2007-2008, sford
+ */
+
+#ifndef MBED_TICKER_H
+#define MBED_TICKER_H
+
+#include "TimerEvent.h"
+#include "FunctionPointer.h"	
+
+namespace mbed {
+
+/* Class: Ticker
+ *  A Ticker is used to call a function at a recurring interval
+ *
+ * You can use as many seperate Ticker objects as you require. 
+ */
+class Ticker : public TimerEvent {
+
+public:
+
+    /* Function: attach
+     *  Attach a function to be called by the Ticker, specifiying the interval in seconds
+     *     
+     * Variables:
+     *  fptr - pointer to the function to be called
+     *  t - the time between calls in seconds
+     */
+    void attach(void (*fptr)(void), float t) {
+        attach_us(fptr, t * 1000000.0f);
+    }
+    
+    /* Function: attach
+     *  Attach a member function to be called by the Ticker, specifiying the interval in seconds
+     *     
+     * Variables:
+     *  tptr - pointer to the object to call the member function on
+     *  mptr - pointer to the member function to be called
+     *  t - the time between calls in seconds
+     */
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void), float t) {
+        attach_us(tptr, mptr, t * 1000000.0f);
+    }
+    
+    /* Function: attach_us
+     *  Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
+     *     
+     * Variables:
+     *  fptr - pointer to the function to be called
+     *  t - the time between calls in micro-seconds
+     */
+    void attach_us(void (*fptr)(void), unsigned int t) {
+        _function.attach(fptr);
+        setup(t);
+    }
+
+    /* Function: attach_us
+     *  Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
+     *     
+     * Variables:
+     *  tptr - pointer to the object to call the member function on
+     *  mptr - pointer to the member function to be called
+     *  t - the time between calls in micro-seconds
+     */    
+    template<typename T>
+    void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
+        _function.attach(tptr, mptr);
+        setup(t);
+    }
+    
+    /* Function: detach
+     *  Detach the function
+     */        
+    void detach();
+	
+protected:
+
+    void setup(unsigned int t);
+    virtual void handler();
+	
+    unsigned int _delay;	
+    FunctionPointer _function;	
+
+};	
+
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Timeout.h	Fri Nov 14 15:25:20 2008 +0000
@@ -0,0 +1,86 @@
+/* mbed Microcontroller Library - Timeout
+ * Copyright (c) 2007-2008, sford
+ */
+
+#ifndef MBED_TIMEOUT_H
+#define MBED_TIMEOUT_H
+
+#include "Ticker.h"
+
+namespace mbed {
+
+/* Class: Timeout
+ *  A Timeout is used to call a function at a point in the future
+ *
+ * You can use as many seperate Timeout objects as you require. 
+ */
+class Timeout : public Ticker {
+
+#if 0 // For documentation
+
+    /* Function: attach
+     *  Attach a function to be called by the Timeout, specifiying the delay in seconds
+     *     
+     * Variables:
+     *  fptr - pointer to the function to be called
+     *  t - the time before the call in seconds
+     */
+    void attach(void (*fptr)(void), float t) {
+        attach_us(fptr, t * 1000000.0f);
+    }
+    
+    /* Function: attach
+     *  Attach a member function to be called by the Timeout, specifiying the delay in seconds
+     *     
+     * Variables:
+     *  tptr - pointer to the object to call the member function on
+     *  mptr - pointer to the member function to be called
+     *  t - the time before the calls in seconds
+     */
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void), float t) {
+        attach_us(tptr, mptr, t * 1000000.0f);
+    }
+    
+    /* Function: attach_us
+     *  Attach a function to be called by the Timeout, specifiying the delay in micro-seconds
+     *     
+     * Variables:
+     *  fptr - pointer to the function to be called
+     *  t - the time before the call in micro-seconds
+     */
+    void attach_us(void (*fptr)(void), unsigned int t) {
+        _function.attach(fptr);
+        setup(t);
+    }
+
+    /* Function: attach_us
+     *  Attach a member function to be called by the Timeout, specifiying the delay in micro-seconds
+     *     
+     * Variables:
+     *  tptr - pointer to the object to call the member function on
+     *  mptr - pointer to the member function to be called
+     *  t - the time before the call in micro-seconds
+     */    
+    template<typename T>
+    void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
+        _function.attach(tptr, mptr);
+        setup(t);
+    }
+    
+    /* Function: detach
+     *  Detach the function
+     */        
+    void detach();
+
+#endif
+    
+protected:
+
+	virtual void handler();
+	
+};	
+
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TimerEvent.h	Fri Nov 14 15:25:20 2008 +0000
@@ -0,0 +1,43 @@
+/* mbed Microcontroller Library - TimerEvent
+ * Copyright (c) 2007-2008, sford
+ */
+
+#ifndef MBED_TIMEREVENT_H
+#define MBED_TIMEREVENT_H
+
+namespace mbed {
+
+// Base abstraction for timer interrupts
+class TimerEvent {
+
+public:
+
+	// The handler registered with the underlying timer interrupt
+	static void irq();
+
+	// Destruction removes it...	
+	virtual ~TimerEvent();
+
+protected:
+
+	// The handler called to service the timer event of the derived class
+	virtual void handler() = 0;
+	
+	// insert in to linked list
+	void insert(unsigned int timestamp);
+	
+	// remove from linked list, if in it
+	void remove();
+	
+	// Get the current usec timestamp
+	static unsigned int timestamp();
+
+	static TimerEvent *_head;   // The head of the list of the events, NULL if none
+	TimerEvent *_next;          // Pointer to the next in the list, NULL if last
+	unsigned int _timestamp;    // The timestamp at which the even should be triggered
+
+};
+
+}
+
+#endif
Binary file mbed.ar has changed
--- a/mbed.h	Thu Sep 18 14:02:33 2008 +0000
+++ b/mbed.h	Fri Nov 14 15:25:20 2008 +0000
@@ -32,6 +32,8 @@
 // mbed Internal components
 #include "Timer.h"
 #include "wait.h"
+#include "Ticker.h"
+#include "Timeout.h"
 
 using namespace mbed;