For Telliskivi2 2014

Dependents:   Telliskivi2_2014

Fork of HumanInterface by Reiko Randoja

HumanInterface.cpp

Committer:
Reiko
Date:
2013-09-19
Revision:
0:ccf47494061b
Child:
1:9b2455659c7d

File content as of revision 0:ccf47494061b:

#include "HumanInterface.h"

HumanInterface::HumanInterface (Serial *pc, 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) { 
        redBlinking = false;
        blueBlinking = false;
        yellowBlinking = false;

        goalLastState = goalButton.read();
        startLastState = startButton.read();
        ballLastState = ballSense.read();

        ioExt->change(this, &HumanInterface::extChangedCallback);

        setGoal(UNSET);

        ledTicker.attach(this, &HumanInterface::blinkLeds, 0.2);
}

void HumanInterface::setError(bool state) {
    if (state) {
        redBlinking = true;
    } else {
        redBlinking = false;
        redLed.clear();
    }
}


void HumanInterface::setGoal(Goal goal) {
    if (goal == YELLOW) {
        yellowLed.set();
        blueLed.clear();
        blueBlinking = false;
        yellowBlinking = false;
    } else if (goal == BLUE) {
        yellowLed.clear();
        blueLed.set();
        blueBlinking = false;
        yellowBlinking = false;
    } else if (goal == UNSET) {
        yellowLed.clear();
        blueLed.clear();
        blueBlinking = true;
        yellowBlinking = true;
    }
}

void HumanInterface::blinkLeds(void) {
    if (redBlinking)
        redLed.toggle();
    if (blueBlinking)
        blueLed.toggle();
    if (yellowBlinking)
        yellowLed.toggle();
}

bool HumanInterface::getBallState() {
    return ballLastState;
}

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::change(void (*function)(void)) { 
    changeCallback.attach(function);
}

void HumanInterface::goalRise(void (*function)(void)) { 
    goalRiseCallback.attach(function);
}

void HumanInterface::startRise(void (*function)(void)) { 
    startRiseCallback.attach(function);
}

void HumanInterface::ballChange(void (*function)(void)) { 
    ballChangeCallback.attach(function);
}