For Telliskivi2 2014

Dependents:   Telliskivi2_2014

Fork of HumanInterface by Reiko Randoja

Revision:
1:9b2455659c7d
Parent:
0:ccf47494061b
Child:
2:1ef07b660873
--- a/HumanInterface.cpp	Thu Sep 19 13:13:10 2013 +0000
+++ b/HumanInterface.cpp	Sun Nov 03 11:40:06 2013 +0000
@@ -1,23 +1,33 @@
 #include "HumanInterface.h"
 
-HumanInterface::HumanInterface (Serial *pc, PCA9555 *ioExt,
+HumanInterface::HumanInterface (PCA9555 *ioExt,
     unsigned int yellowLedPin, unsigned int blueLedPin, unsigned int redLedPin,
-    unsigned int goalButtonPin, unsigned int startButtonPin, unsigned int ballSensePin):
-    extIO(ioExt), yellowLed(ioExt, yellowLedPin), blueLed(ioExt, blueLedPin), redLed(ioExt, redLedPin),// blue,yellow,red 12,11,13
-    goalButton(ioExt, goalButtonPin), startButton(ioExt, startButtonPin), ballSense(ioExt, ballSensePin) { 
+    unsigned int greenLedPin, unsigned int orangeLedPin,
+    PinName goalButtonPin, PinName startButtonPin, PinName ballSensePin):
+    extIO(ioExt), yellowLed(ioExt, yellowLedPin), blueLed(ioExt, blueLedPin), redLed(ioExt, redLedPin),
+    greenLed(ioExt, greenLedPin), orangeLed(ioExt, orangeLedPin),
+    interruptGoal(goalButtonPin), interruptStart(startButtonPin), interruptBall(ballSensePin) { 
         redBlinking = false;
         blueBlinking = false;
         yellowBlinking = false;
+        greenBlinking = false;
+        orangeBlinking = false;
 
-        goalLastState = goalButton.read();
-        startLastState = startButton.read();
-        ballLastState = ballSense.read();
-
-        ioExt->change(this, &HumanInterface::extChangedCallback);
+        goalLastState = false;
+        startLastState = false;
+        ballLastState = false;
 
         setGoal(UNSET);
 
         ledTicker.attach(this, &HumanInterface::blinkLeds, 0.2);
+        
+        interruptGoal.mode(PullUp);
+        interruptStart.mode(PullUp);
+        
+        interruptGoal.rise(this, &HumanInterface::goalRised);
+        interruptStart.rise(this, &HumanInterface::startRised);
+        interruptBall.fall(this, &HumanInterface::ballFalled);
+        interruptBall.rise(this, &HumanInterface::ballRised);
 }
 
 void HumanInterface::setError(bool state) {
@@ -29,6 +39,13 @@
     }
 }
 
+void HumanInterface::setGo(bool state) {
+    if (state) {
+        greenLed.set();
+    } else {
+        greenLed.clear();
+    }
+}
 
 void HumanInterface::setGoal(Goal goal) {
     if (goal == YELLOW) {
@@ -56,46 +73,30 @@
         blueLed.toggle();
     if (yellowBlinking)
         yellowLed.toggle();
+    if (greenBlinking)
+        greenLed.toggle();
+    if (orangeBlinking)
+        orangeLed.toggle();
 }
 
 bool HumanInterface::getBallState() {
-    return ballLastState;
+    return ballCurrentState;
+}
+
+void HumanInterface::goalRised() {
+    goalRiseCallback.call();
 }
 
-void HumanInterface::extChangedCallback(void) {
-    //pc->printf("Callback!");
-    
-    bool goalState = goalButton.read();
-    bool startState = startButton.read();
-    bool ballState = ballSense.read();
-
-    if (goalLastState != goalState) {
-        //pc->printf("goal button change!");
-        if (goalState) {
-            goalRiseCallback.call();
-        }
-    }
-    if (startLastState != startState) {
-        //pc->printf("start button change!");
-        if (startState) {
-            startRiseCallback.call();
-        }
-    }
-    if (ballLastState != ballState) {
-        //pc->printf("ball change!");
-        //Todo: if new state is "true" -> button was released
-        ballChangeCallback.call();
-    }
-    
-    goalLastState = goalState;
-    startLastState = startState;
-    ballLastState = ballState;
-    
-    changeCallback.call();
+void HumanInterface::startRised() {
+    startRiseCallback.call();
 }
 
-void HumanInterface::change(void (*function)(void)) { 
-    changeCallback.attach(function);
+void HumanInterface::ballRised() {
+    ballRiseCallback.call();
+}
+
+void HumanInterface::ballFalled() {
+    ballFallCallback.call();
 }
 
 void HumanInterface::goalRise(void (*function)(void)) { 
@@ -106,6 +107,10 @@
     startRiseCallback.attach(function);
 }
 
-void HumanInterface::ballChange(void (*function)(void)) { 
-    ballChangeCallback.attach(function);
+void HumanInterface::ballRise(void (*function)(void)) { 
+    ballRiseCallback.attach(function);
+}
+
+void HumanInterface::ballFall(void (*function)(void)) { 
+    ballFallCallback.attach(function);
 }
\ No newline at end of file