AirsoftTimer software based on mbed

Dependencies:   mbed TextLCD keypad

games/ShowBuzzer.cpp

Committer:
sillevl
Date:
2015-05-23
Revision:
12:22e9ef610ea2
Parent:
11:50572814f73e

File content as of revision 12:22e9ef610ea2:


#include "ShowBuzzer.h"


static const char* NAME = "ShowBuzzer";

ShowBuzzer::ShowBuzzer(Board* board) : Game(board){
    keyboard->attach(this,&ShowBuzzer::keyPressed);
    keyboard->start();
    leds->off(Leds::ALL);
}

void ShowBuzzer::setup(){
    //no setup required for this game    
}

void ShowBuzzer::run(){
    lcd->printf("ShowBuzzer");   
    newRound();
    while(true){
        
        //wait for button press
        Team team = waitForButtonPress();
        //show leds
        setLeds(team);
        //show display
        setDisplay(team);
        //play sound
        playSound();
        //wait for 3 seconds
        wait_ms(5000);
        //start new round
        newRound();
        
        // don't return from this function yet (only if the game ends);   
    }
}

void ShowBuzzer::newRound(){
        buzzer->playNote(200,50);
        leds->off(Leds::ALL);
        lcd->cls();
        lcd->printf("*** WAITING ***");  
}

ShowBuzzer::Team ShowBuzzer::waitForButtonPress(){
    Team team;
    while(true){
       if(button->read() == 0){
            team = TEAM_B;
            break;
        }
        if(key->read() == 0){
            team = TEAM_A;
            break;    
        }
    }
    return team;
}

void ShowBuzzer::setLeds(Team team){
    switch(team){
        case TEAM_A:
            leds->on(Leds::LEFT);
            break;
        case TEAM_B:
            leds->on(Leds::RIGHT);
            break;
        default:
            break;
    }
}

void ShowBuzzer::setDisplay(Team team){
    lcd->locate(0,0);
    lcd->cls();
    switch(team){
        case TEAM_A:
            lcd->printf("Team A");
            break;
        case TEAM_B:
            lcd->printf("Team B");
            break;
        default:
            break;
    }
}

void ShowBuzzer::playSound(){
    buzzer->playNote(698, 50);
    buzzer->playNote(783, 50);
    buzzer->playNote(880, 50);
    buzzer->playNote(987, 50);
    buzzer->playNote(1108, 50);
    buzzer->playNote(1244, 50);
    buzzer->playNote(1396, 50);
    buzzer->playNote(1567, 50);
    buzzer->playNote(1760, 50);
    buzzer->playNote(1975, 50);
}

void ShowBuzzer::playCorrect(){
    buzzer->playNote(2093, 100);
    buzzer->playNote(2637, 100);
    buzzer->playNote(3165, 100);
    buzzer->playNote(3951, 100);
}

void ShowBuzzer::playWrong(){
    buzzer->playNote(440, 1000);    
}    

uint32_t ShowBuzzer::keyPressed(uint32_t key){
    if(key == 11){ playCorrect();}
    if(key == 9){ playWrong();}
    return 0;
}