Hugo Hu / Mbed 2 deprecated BRAVEHEART

Dependencies:   mbed N5110 ShiftReg PinDetect

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 pinD, 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     btnD = new PinDetect(pinD);
00019     
00020     // Default sample frequency
00021     btnA->setSampleFrequency();
00022     btnB->setSampleFrequency();
00023     btnC->setSampleFrequency();
00024     btnD->setSampleFrequency();
00025 }
00026 
00027 InputManager::~InputManager()
00028 {
00029     delete joystick;
00030     delete btnA;
00031     delete btnB;
00032     delete btnC;
00033     delete btnD;
00034 }
00035 
00036 void InputManager::addBtnPressInterrupt(Input::Button button, void (*func)(void))
00037 {
00038     PinDetect *btn = getBtnPtr(button);
00039     if (btn) // if not null pointer
00040         btn->attach_asserted(func);
00041 }
00042 
00043 PinDetect* InputManager::getBtnPtr(Input::Button button)
00044 {
00045     switch(button)
00046     {
00047         case Input::ButtonA:
00048             return btnA;
00049         
00050         case Input::ButtonB:
00051             return btnB;
00052         
00053         case Input::ButtonC:
00054             return btnC;
00055             
00056         case Input::ButtonD:
00057             return btnD;
00058         
00059         default:
00060             return 0; // Return 0 (nullptr) if invalid input
00061     }
00062 }
00063 
00064 int InputManager::read(Input::Button button)
00065 {
00066     PinDetect *btn = getBtnPtr(button);
00067     if (!btn) return 0; // Invalid button
00068     
00069     return *btn; // Value of button
00070 }
00071 
00072 Joystick::Joystick(PinName x, PinName y, PinName button)
00073 {
00074     xPot = new AnalogIn(x);
00075     yPot = new AnalogIn(y);
00076     btn = new DigitalIn(button);
00077 }
00078 
00079 Joystick::~Joystick()
00080 {
00081     delete xPot;
00082     delete yPot;
00083     delete btn;
00084 }
00085 
00086 
00087 // Make sure that the Joystick is centered while calibrating
00088 void Joystick::calibrate()
00089 {
00090     btn->mode(PullDown);
00091     centerX = *xPot;
00092     centerY = *yPot;
00093 }
00094 
00095 
00096 void Joystick::update()
00097 {
00098     // Get position of joystick compared to the center values (delta x and delta y).
00099     dx = *xPot - centerX;
00100     dy = *yPot - centerY;
00101     
00102     if (abs(dx) < DEAD_ZONE && abs(dy) < DEAD_ZONE)
00103         dir = CENTER;
00104     else if (abs(dx) < DEAD_ZONE)                       // Inside horizontal dead zone.
00105         dir = (dy > DEAD_ZONE) ? UP : DOWN;
00106     else if (abs(dy) < DEAD_ZONE)                       // Inside vertical dead zone.
00107         dir = (dx > DEAD_ZONE) ? LEFT : RIGHT;
00108     else if (dx > 0)                                    // To the left, outside both deadzones.
00109         dir = (dy > 0) ? UP_LEFT : DOWN_LEFT; 
00110     else                                                // To the right, outside both deadzones
00111         dir = (dy > 0) ? UP_RIGHT : DOWN_RIGHT;
00112 }