Telliskivi 2+ peripherals library

Committer:
Reiko
Date:
Thu Sep 19 13:13:10 2013 +0000
Revision:
0:ccf47494061b
Child:
1:9b2455659c7d
Created library for leds and buttons

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Reiko 0:ccf47494061b 1 #include "HumanInterface.h"
Reiko 0:ccf47494061b 2
Reiko 0:ccf47494061b 3 HumanInterface::HumanInterface (Serial *pc, PCA9555 *ioExt,
Reiko 0:ccf47494061b 4 unsigned int yellowLedPin, unsigned int blueLedPin, unsigned int redLedPin,
Reiko 0:ccf47494061b 5 unsigned int goalButtonPin, unsigned int startButtonPin, unsigned int ballSensePin):
Reiko 0:ccf47494061b 6 extIO(ioExt), yellowLed(ioExt, yellowLedPin), blueLed(ioExt, blueLedPin), redLed(ioExt, redLedPin),// blue,yellow,red 12,11,13
Reiko 0:ccf47494061b 7 goalButton(ioExt, goalButtonPin), startButton(ioExt, startButtonPin), ballSense(ioExt, ballSensePin) {
Reiko 0:ccf47494061b 8 redBlinking = false;
Reiko 0:ccf47494061b 9 blueBlinking = false;
Reiko 0:ccf47494061b 10 yellowBlinking = false;
Reiko 0:ccf47494061b 11
Reiko 0:ccf47494061b 12 goalLastState = goalButton.read();
Reiko 0:ccf47494061b 13 startLastState = startButton.read();
Reiko 0:ccf47494061b 14 ballLastState = ballSense.read();
Reiko 0:ccf47494061b 15
Reiko 0:ccf47494061b 16 ioExt->change(this, &HumanInterface::extChangedCallback);
Reiko 0:ccf47494061b 17
Reiko 0:ccf47494061b 18 setGoal(UNSET);
Reiko 0:ccf47494061b 19
Reiko 0:ccf47494061b 20 ledTicker.attach(this, &HumanInterface::blinkLeds, 0.2);
Reiko 0:ccf47494061b 21 }
Reiko 0:ccf47494061b 22
Reiko 0:ccf47494061b 23 void HumanInterface::setError(bool state) {
Reiko 0:ccf47494061b 24 if (state) {
Reiko 0:ccf47494061b 25 redBlinking = true;
Reiko 0:ccf47494061b 26 } else {
Reiko 0:ccf47494061b 27 redBlinking = false;
Reiko 0:ccf47494061b 28 redLed.clear();
Reiko 0:ccf47494061b 29 }
Reiko 0:ccf47494061b 30 }
Reiko 0:ccf47494061b 31
Reiko 0:ccf47494061b 32
Reiko 0:ccf47494061b 33 void HumanInterface::setGoal(Goal goal) {
Reiko 0:ccf47494061b 34 if (goal == YELLOW) {
Reiko 0:ccf47494061b 35 yellowLed.set();
Reiko 0:ccf47494061b 36 blueLed.clear();
Reiko 0:ccf47494061b 37 blueBlinking = false;
Reiko 0:ccf47494061b 38 yellowBlinking = false;
Reiko 0:ccf47494061b 39 } else if (goal == BLUE) {
Reiko 0:ccf47494061b 40 yellowLed.clear();
Reiko 0:ccf47494061b 41 blueLed.set();
Reiko 0:ccf47494061b 42 blueBlinking = false;
Reiko 0:ccf47494061b 43 yellowBlinking = false;
Reiko 0:ccf47494061b 44 } else if (goal == UNSET) {
Reiko 0:ccf47494061b 45 yellowLed.clear();
Reiko 0:ccf47494061b 46 blueLed.clear();
Reiko 0:ccf47494061b 47 blueBlinking = true;
Reiko 0:ccf47494061b 48 yellowBlinking = true;
Reiko 0:ccf47494061b 49 }
Reiko 0:ccf47494061b 50 }
Reiko 0:ccf47494061b 51
Reiko 0:ccf47494061b 52 void HumanInterface::blinkLeds(void) {
Reiko 0:ccf47494061b 53 if (redBlinking)
Reiko 0:ccf47494061b 54 redLed.toggle();
Reiko 0:ccf47494061b 55 if (blueBlinking)
Reiko 0:ccf47494061b 56 blueLed.toggle();
Reiko 0:ccf47494061b 57 if (yellowBlinking)
Reiko 0:ccf47494061b 58 yellowLed.toggle();
Reiko 0:ccf47494061b 59 }
Reiko 0:ccf47494061b 60
Reiko 0:ccf47494061b 61 bool HumanInterface::getBallState() {
Reiko 0:ccf47494061b 62 return ballLastState;
Reiko 0:ccf47494061b 63 }
Reiko 0:ccf47494061b 64
Reiko 0:ccf47494061b 65 void HumanInterface::extChangedCallback(void) {
Reiko 0:ccf47494061b 66 //pc->printf("Callback!");
Reiko 0:ccf47494061b 67
Reiko 0:ccf47494061b 68 bool goalState = goalButton.read();
Reiko 0:ccf47494061b 69 bool startState = startButton.read();
Reiko 0:ccf47494061b 70 bool ballState = ballSense.read();
Reiko 0:ccf47494061b 71
Reiko 0:ccf47494061b 72 if (goalLastState != goalState) {
Reiko 0:ccf47494061b 73 //pc->printf("goal button change!");
Reiko 0:ccf47494061b 74 if (goalState) {
Reiko 0:ccf47494061b 75 goalRiseCallback.call();
Reiko 0:ccf47494061b 76 }
Reiko 0:ccf47494061b 77 }
Reiko 0:ccf47494061b 78 if (startLastState != startState) {
Reiko 0:ccf47494061b 79 //pc->printf("start button change!");
Reiko 0:ccf47494061b 80 if (startState) {
Reiko 0:ccf47494061b 81 startRiseCallback.call();
Reiko 0:ccf47494061b 82 }
Reiko 0:ccf47494061b 83 }
Reiko 0:ccf47494061b 84 if (ballLastState != ballState) {
Reiko 0:ccf47494061b 85 //pc->printf("ball change!");
Reiko 0:ccf47494061b 86 //Todo: if new state is "true" -> button was released
Reiko 0:ccf47494061b 87 ballChangeCallback.call();
Reiko 0:ccf47494061b 88 }
Reiko 0:ccf47494061b 89
Reiko 0:ccf47494061b 90 goalLastState = goalState;
Reiko 0:ccf47494061b 91 startLastState = startState;
Reiko 0:ccf47494061b 92 ballLastState = ballState;
Reiko 0:ccf47494061b 93
Reiko 0:ccf47494061b 94 changeCallback.call();
Reiko 0:ccf47494061b 95 }
Reiko 0:ccf47494061b 96
Reiko 0:ccf47494061b 97 void HumanInterface::change(void (*function)(void)) {
Reiko 0:ccf47494061b 98 changeCallback.attach(function);
Reiko 0:ccf47494061b 99 }
Reiko 0:ccf47494061b 100
Reiko 0:ccf47494061b 101 void HumanInterface::goalRise(void (*function)(void)) {
Reiko 0:ccf47494061b 102 goalRiseCallback.attach(function);
Reiko 0:ccf47494061b 103 }
Reiko 0:ccf47494061b 104
Reiko 0:ccf47494061b 105 void HumanInterface::startRise(void (*function)(void)) {
Reiko 0:ccf47494061b 106 startRiseCallback.attach(function);
Reiko 0:ccf47494061b 107 }
Reiko 0:ccf47494061b 108
Reiko 0:ccf47494061b 109 void HumanInterface::ballChange(void (*function)(void)) {
Reiko 0:ccf47494061b 110 ballChangeCallback.attach(function);
Reiko 0:ccf47494061b 111 }