Andreas Garmannslund / Mbed 2 deprecated SimplePlatformGame

Dependencies:   N5110 PinDetect PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InputManager.cpp Source File

InputManager.cpp

Go to the documentation of this file.
00001 #include "InputManager.h "
00002 
00003 /** @file InputManager.cpp
00004  *  @author Andreas Garmannslund
00005  *  @date April 2015
00006 */
00007 
00008 InputManager::InputManager(PinName pinA, PinName pinB, PinName pinC, PinName joyH, PinName joyV, PinName joyBtn)
00009 {
00010     // Init joystick
00011     joystick = new Joystick(joyH, joyV, joyBtn);
00012     joystick->calibrate();
00013     
00014     // Init buttons
00015     btnA = new PinDetect(pinA);
00016     btnB = new PinDetect(pinB);
00017     btnC = new PinDetect(pinC);
00018     
00019     // Default sample frequency
00020     btnA->setSampleFrequency();
00021     btnB->setSampleFrequency();
00022     btnC->setSampleFrequency();
00023 }
00024 
00025 InputManager::~InputManager()
00026 {
00027     delete joystick;
00028     delete btnA;
00029     delete btnB;
00030     delete btnC;
00031 }
00032 
00033 void InputManager::addBtnPressInterrupt(Input::Button button, void (*func)(void))
00034 {
00035     PinDetect *btn = getBtnPtr(button);
00036     if (btn) // if not null pointer
00037         btn->attach_asserted(func);
00038 }
00039 
00040 PinDetect* InputManager::getBtnPtr(Input::Button button)
00041 {
00042     switch(button)
00043     {
00044         case Input::ButtonA:
00045             return btnA;
00046         
00047         case Input::ButtonB:
00048             return btnB;
00049         
00050         case Input::ButtonC:
00051             return btnC;
00052         
00053         default:
00054             return 0; // Return 0 (nullptr) if invalid input
00055     }
00056 }
00057 
00058 int InputManager::read(Input::Button button)
00059 {
00060     PinDetect *btn = getBtnPtr(button);
00061     if (!btn) return 0; // Invalid button
00062     
00063     return *btn; // Value of button
00064 }