Telliskivi 2+ peripherals library
HumanInterface.cpp@1:9b2455659c7d, 2013-11-03 (annotated)
- Committer:
- Reiko
- Date:
- Sun Nov 03 11:40:06 2013 +0000
- Revision:
- 1:9b2455659c7d
- Parent:
- 0:ccf47494061b
- Child:
- 2:1ef07b660873
Changed to use buttons and ball detector connected to mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reiko | 0:ccf47494061b | 1 | #include "HumanInterface.h" |
Reiko | 0:ccf47494061b | 2 | |
Reiko | 1:9b2455659c7d | 3 | HumanInterface::HumanInterface (PCA9555 *ioExt, |
Reiko | 0:ccf47494061b | 4 | unsigned int yellowLedPin, unsigned int blueLedPin, unsigned int redLedPin, |
Reiko | 1:9b2455659c7d | 5 | unsigned int greenLedPin, unsigned int orangeLedPin, |
Reiko | 1:9b2455659c7d | 6 | PinName goalButtonPin, PinName startButtonPin, PinName ballSensePin): |
Reiko | 1:9b2455659c7d | 7 | extIO(ioExt), yellowLed(ioExt, yellowLedPin), blueLed(ioExt, blueLedPin), redLed(ioExt, redLedPin), |
Reiko | 1:9b2455659c7d | 8 | greenLed(ioExt, greenLedPin), orangeLed(ioExt, orangeLedPin), |
Reiko | 1:9b2455659c7d | 9 | interruptGoal(goalButtonPin), interruptStart(startButtonPin), interruptBall(ballSensePin) { |
Reiko | 0:ccf47494061b | 10 | redBlinking = false; |
Reiko | 0:ccf47494061b | 11 | blueBlinking = false; |
Reiko | 0:ccf47494061b | 12 | yellowBlinking = false; |
Reiko | 1:9b2455659c7d | 13 | greenBlinking = false; |
Reiko | 1:9b2455659c7d | 14 | orangeBlinking = false; |
Reiko | 0:ccf47494061b | 15 | |
Reiko | 1:9b2455659c7d | 16 | goalLastState = false; |
Reiko | 1:9b2455659c7d | 17 | startLastState = false; |
Reiko | 1:9b2455659c7d | 18 | ballLastState = false; |
Reiko | 0:ccf47494061b | 19 | |
Reiko | 0:ccf47494061b | 20 | setGoal(UNSET); |
Reiko | 0:ccf47494061b | 21 | |
Reiko | 0:ccf47494061b | 22 | ledTicker.attach(this, &HumanInterface::blinkLeds, 0.2); |
Reiko | 1:9b2455659c7d | 23 | |
Reiko | 1:9b2455659c7d | 24 | interruptGoal.mode(PullUp); |
Reiko | 1:9b2455659c7d | 25 | interruptStart.mode(PullUp); |
Reiko | 1:9b2455659c7d | 26 | |
Reiko | 1:9b2455659c7d | 27 | interruptGoal.rise(this, &HumanInterface::goalRised); |
Reiko | 1:9b2455659c7d | 28 | interruptStart.rise(this, &HumanInterface::startRised); |
Reiko | 1:9b2455659c7d | 29 | interruptBall.fall(this, &HumanInterface::ballFalled); |
Reiko | 1:9b2455659c7d | 30 | interruptBall.rise(this, &HumanInterface::ballRised); |
Reiko | 0:ccf47494061b | 31 | } |
Reiko | 0:ccf47494061b | 32 | |
Reiko | 0:ccf47494061b | 33 | void HumanInterface::setError(bool state) { |
Reiko | 0:ccf47494061b | 34 | if (state) { |
Reiko | 0:ccf47494061b | 35 | redBlinking = true; |
Reiko | 0:ccf47494061b | 36 | } else { |
Reiko | 0:ccf47494061b | 37 | redBlinking = false; |
Reiko | 0:ccf47494061b | 38 | redLed.clear(); |
Reiko | 0:ccf47494061b | 39 | } |
Reiko | 0:ccf47494061b | 40 | } |
Reiko | 0:ccf47494061b | 41 | |
Reiko | 1:9b2455659c7d | 42 | void HumanInterface::setGo(bool state) { |
Reiko | 1:9b2455659c7d | 43 | if (state) { |
Reiko | 1:9b2455659c7d | 44 | greenLed.set(); |
Reiko | 1:9b2455659c7d | 45 | } else { |
Reiko | 1:9b2455659c7d | 46 | greenLed.clear(); |
Reiko | 1:9b2455659c7d | 47 | } |
Reiko | 1:9b2455659c7d | 48 | } |
Reiko | 0:ccf47494061b | 49 | |
Reiko | 0:ccf47494061b | 50 | void HumanInterface::setGoal(Goal goal) { |
Reiko | 0:ccf47494061b | 51 | if (goal == YELLOW) { |
Reiko | 0:ccf47494061b | 52 | yellowLed.set(); |
Reiko | 0:ccf47494061b | 53 | blueLed.clear(); |
Reiko | 0:ccf47494061b | 54 | blueBlinking = false; |
Reiko | 0:ccf47494061b | 55 | yellowBlinking = false; |
Reiko | 0:ccf47494061b | 56 | } else if (goal == BLUE) { |
Reiko | 0:ccf47494061b | 57 | yellowLed.clear(); |
Reiko | 0:ccf47494061b | 58 | blueLed.set(); |
Reiko | 0:ccf47494061b | 59 | blueBlinking = false; |
Reiko | 0:ccf47494061b | 60 | yellowBlinking = false; |
Reiko | 0:ccf47494061b | 61 | } else if (goal == UNSET) { |
Reiko | 0:ccf47494061b | 62 | yellowLed.clear(); |
Reiko | 0:ccf47494061b | 63 | blueLed.clear(); |
Reiko | 0:ccf47494061b | 64 | blueBlinking = true; |
Reiko | 0:ccf47494061b | 65 | yellowBlinking = true; |
Reiko | 0:ccf47494061b | 66 | } |
Reiko | 0:ccf47494061b | 67 | } |
Reiko | 0:ccf47494061b | 68 | |
Reiko | 0:ccf47494061b | 69 | void HumanInterface::blinkLeds(void) { |
Reiko | 0:ccf47494061b | 70 | if (redBlinking) |
Reiko | 0:ccf47494061b | 71 | redLed.toggle(); |
Reiko | 0:ccf47494061b | 72 | if (blueBlinking) |
Reiko | 0:ccf47494061b | 73 | blueLed.toggle(); |
Reiko | 0:ccf47494061b | 74 | if (yellowBlinking) |
Reiko | 0:ccf47494061b | 75 | yellowLed.toggle(); |
Reiko | 1:9b2455659c7d | 76 | if (greenBlinking) |
Reiko | 1:9b2455659c7d | 77 | greenLed.toggle(); |
Reiko | 1:9b2455659c7d | 78 | if (orangeBlinking) |
Reiko | 1:9b2455659c7d | 79 | orangeLed.toggle(); |
Reiko | 0:ccf47494061b | 80 | } |
Reiko | 0:ccf47494061b | 81 | |
Reiko | 0:ccf47494061b | 82 | bool HumanInterface::getBallState() { |
Reiko | 1:9b2455659c7d | 83 | return ballCurrentState; |
Reiko | 1:9b2455659c7d | 84 | } |
Reiko | 1:9b2455659c7d | 85 | |
Reiko | 1:9b2455659c7d | 86 | void HumanInterface::goalRised() { |
Reiko | 1:9b2455659c7d | 87 | goalRiseCallback.call(); |
Reiko | 0:ccf47494061b | 88 | } |
Reiko | 0:ccf47494061b | 89 | |
Reiko | 1:9b2455659c7d | 90 | void HumanInterface::startRised() { |
Reiko | 1:9b2455659c7d | 91 | startRiseCallback.call(); |
Reiko | 0:ccf47494061b | 92 | } |
Reiko | 0:ccf47494061b | 93 | |
Reiko | 1:9b2455659c7d | 94 | void HumanInterface::ballRised() { |
Reiko | 1:9b2455659c7d | 95 | ballRiseCallback.call(); |
Reiko | 1:9b2455659c7d | 96 | } |
Reiko | 1:9b2455659c7d | 97 | |
Reiko | 1:9b2455659c7d | 98 | void HumanInterface::ballFalled() { |
Reiko | 1:9b2455659c7d | 99 | ballFallCallback.call(); |
Reiko | 0:ccf47494061b | 100 | } |
Reiko | 0:ccf47494061b | 101 | |
Reiko | 0:ccf47494061b | 102 | void HumanInterface::goalRise(void (*function)(void)) { |
Reiko | 0:ccf47494061b | 103 | goalRiseCallback.attach(function); |
Reiko | 0:ccf47494061b | 104 | } |
Reiko | 0:ccf47494061b | 105 | |
Reiko | 0:ccf47494061b | 106 | void HumanInterface::startRise(void (*function)(void)) { |
Reiko | 0:ccf47494061b | 107 | startRiseCallback.attach(function); |
Reiko | 0:ccf47494061b | 108 | } |
Reiko | 0:ccf47494061b | 109 | |
Reiko | 1:9b2455659c7d | 110 | void HumanInterface::ballRise(void (*function)(void)) { |
Reiko | 1:9b2455659c7d | 111 | ballRiseCallback.attach(function); |
Reiko | 1:9b2455659c7d | 112 | } |
Reiko | 1:9b2455659c7d | 113 | |
Reiko | 1:9b2455659c7d | 114 | void HumanInterface::ballFall(void (*function)(void)) { |
Reiko | 1:9b2455659c7d | 115 | ballFallCallback.attach(function); |
Reiko | 0:ccf47494061b | 116 | } |