Ball Sensor

Dependents:  

Files at this revision

API Documentation at this revision

Comitter:
Reiko
Date:
Mon Sep 09 17:07:33 2013 +0000
Commit message:
Initial version

Changed in this revision

ballsens.cpp Show annotated file Show diff for this revision Revisions of this file
ballsens.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 962fa32d6675 ballsens.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ballsens.cpp	Mon Sep 09 17:07:33 2013 +0000
@@ -0,0 +1,36 @@
+#include "ballsens.h"
+
+BallSens::BallSens(PCA9555 *ioExt, unsigned int ballPin)
+    : extIO(ioExt), ballPinNumber(ballPin) {
+    
+    ballState = false;
+ 
+    extIO->change(this, &BallSens::callChange);
+}
+
+bool BallSens::read() {
+    //return ballState;
+    return (extIO->read() & 0x0400) > 0;
+}
+
+void BallSens::setState(bool newState) {
+    bool oldState = ballState;
+    ballState = newState;
+    /*if (oldState != newState) {
+        _callbackChange.call();
+    }*/
+}
+
+void BallSens::change(void (*function)(void)) { 
+    _callbackChange.attach(function);
+}
+/*
+template<typename T>
+void BallSens::change(T *object, void (T::*member)(void)) { 
+    _callbackChange.attach(object, member); 
+}
+*/
+void BallSens::callChange(void) {
+    setState((extIO->read() & 0x0400) > 0);
+    _callbackChange.call();
+}
\ No newline at end of file
diff -r 000000000000 -r 962fa32d6675 ballsens.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ballsens.h	Mon Sep 09 17:07:33 2013 +0000
@@ -0,0 +1,31 @@
+#ifndef BALLSENS_H
+#define BALLSENS_H
+
+#include "mbed.h"
+#include "PCA9555.h"
+
+class BallSens {
+protected:
+    FunctionPointer _callbackChange;
+public:
+    BallSens(PCA9555 *ioExt, unsigned int ballPin);
+    bool read();
+    
+    void change(void (*function)(void));
+    
+    template<typename T>
+    void change(T *object, void (T::*member)(void)) { 
+        _callbackChange.attach(object, member); 
+    }   
+    
+private:
+    bool ballState;
+    PCA9555 *extIO;
+    unsigned int ballPinNumber;
+    
+    void callChange(void);        
+    
+    void setState(bool newState);
+};
+
+#endif
\ No newline at end of file