PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PokittoButtons.cpp Source File

PokittoButtons.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     PokittoButtons.cpp
00004     @author   Jonne Valola
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2016, Jonne Valola
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 #include "PokittoCore.h "
00038 
00039 using namespace Pokitto;
00040 
00041 uint8_t Buttons::pins[NUM_BTN];
00042 uint8_t Buttons::states[NUM_BTN];
00043 uint8_t Buttons::buttons_state;
00044 uint8_t Buttons::buttons_held;
00045 uint8_t Buttons::buttons_released; // from LSB up,down,left,right,a,b,c
00046 uint16_t Buttons::cHWLongPress = CHWLONGPRESSTIMEOUT;
00047 
00048 
00049 void Buttons::begin() {
00050     #ifndef POK_SIM
00051     Pokitto::initButtons();
00052     #endif // POK_SIM
00053 }
00054 
00055 void Buttons::update() {
00056     #if POK_USE_CONSOLE
00057     if (console.conscounter) return;
00058     #endif // POK_USE_CONSOLE
00059     #ifdef PROJ_BUTTONS_POLLING_ONLY
00060     pollButtons();
00061     #endif
00062     #ifndef POK_SIM
00063         /** HARDWARE CODE **/
00064     for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
00065         if (Pokitto::heldStates[thisButton]) { //if button pressed
00066             states[thisButton]++; //increase button hold time
00067             if (states[thisButton]==0xFF) states[thisButton]=2; // PREVENT WRAPAROUND!!!!
00068         } else {
00069             if (states[thisButton] == 0)//button idle
00070                 continue;
00071             if (states[thisButton] == 0xFF)//if previously released
00072                 states[thisButton] = 0; //set to idle
00073             else
00074                 states[thisButton] = 0xFF; //button just released
00075         }
00076         }
00077     #else
00078         /** POK_SIM code **/
00079         simulator.pollButtons();
00080         for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
00081         uint8_t temp=0;
00082         switch (thisButton) {
00083         case 0:
00084             temp = simulator.leftHeld(); break;
00085         case 1:
00086             temp = simulator.upHeld(); break;
00087         case 2:
00088             temp = simulator.rightHeld(); break;
00089         case 3:
00090             temp = simulator.downHeld(); break;
00091         case 4:
00092             temp = simulator.aHeld(); break;
00093         case 5:
00094             temp = simulator.bHeld(); break;
00095         case 6:
00096             temp = simulator.cHeld(); break;
00097         default:
00098             break;
00099         }
00100 
00101         if (temp == HIGH) { //if button pressed
00102             states[thisButton]++; //increase button hold time
00103             if (states[thisButton]==0xFF) states[thisButton] = 2; //prevent wraparound and retrigger
00104         } else {
00105             if (states[thisButton] == 0)//button idle
00106                 continue;
00107             if (states[thisButton] == 0xFF)//if previously released
00108                 states[thisButton] = 0; //set to idle
00109             else
00110                 states[thisButton] = 0xFF; //button just released
00111         }
00112         }
00113 
00114         #endif // POK_SIM
00115 }
00116 
00117 /*
00118  * Returns true when 'button' is pressed.
00119  * The button has to be released for it to be triggered again.
00120  */
00121 bool Buttons::pressed(uint8_t button) {
00122     if (states[button] == 1)
00123         return true;
00124     else
00125         return false;
00126 }
00127 
00128 /*
00129  * return true if 'button' is released
00130  */
00131 bool Buttons::released(uint8_t button) {
00132     if (states[button] == 0xFF) {
00133         states[button] = 0; // prevent continuous released state!
00134         return true;
00135     }
00136     else
00137         return false;
00138 }
00139 
00140 /**
00141  * returns true ONCE when 'button' is held for 'time' frames
00142  * @param button The button's ID
00143  * @param time How much frames button must be held, between 1 and 254.
00144  * @return true when 'button' is held for 'time' frames
00145  */
00146 bool Buttons::held(uint8_t button, uint8_t time){
00147     if(states[button] == (time+1))
00148         return true;
00149     else
00150         return false;
00151 }
00152 
00153 /**
00154  * returns true every 'period' frames when 'button' is held
00155  * @param button The button's ID
00156  * @param period How much frames button must be held, between 1 and 254.
00157  * @return true if the button is held for the given time
00158  */
00159 bool Buttons::repeat(uint8_t button, uint8_t period) {
00160     if (period <= 1) {
00161         if ((states[button] != 0xFF) && (states[button]))
00162             return true;
00163     } else {
00164         if ((states[button] != 0xFF) && ((states[button] % period) == 1))
00165             return true;
00166     }
00167     return false;
00168 }
00169 
00170 /**
00171  *
00172  * @param button The button's ID
00173  * @return The number of frames during which the button has been held.
00174  */
00175 uint8_t Buttons::timeHeld(uint8_t button){
00176     if(states[button] != 0xFF)
00177         return states[button];
00178     else
00179         return 0;
00180 
00181 }
00182 
00183 void Buttons::pollButtons() {
00184     #ifdef POK_SIM
00185     simulator.pollButtons();
00186     #else
00187     uint8_t buttons_state_old = buttons_state;
00188     buttons_state = 0; // clear all
00189     if (upBtn()) buttons_state |= (1<<UPBIT);
00190     if (downBtn()) buttons_state |= (1<<DOWNBIT);
00191     if (leftBtn()) buttons_state |= (1<<LEFTBIT);
00192     if (rightBtn()) buttons_state |= (1<<RIGHTBIT);
00193     if (aBtn()) buttons_state |= (1<<ABIT);
00194     if (bBtn()) buttons_state |= (1<<BBIT);
00195     if (cBtn()) buttons_state |= (1<<CBIT);
00196     buttons_held = buttons_state & buttons_state_old; // only if both 1, end result is 1
00197     buttons_released = ~buttons_state & buttons_state_old; // if now zero, then 1 AND previous 1 = 1
00198     #endif // POK_SIM
00199 }
00200 
00201 uint8_t Buttons::aBtn() {
00202     #ifdef POK_SIM
00203     return simulator.aBtn();
00204     #else
00205     #ifndef PROJ_BUTTONS_POLLING_ONLY
00206     return Pokitto::heldStates[BTN_A];
00207     #else
00208     return Pokitto::Core::aBtn();
00209     #endif
00210     #endif // POK_SIM
00211 }
00212 
00213 
00214 uint8_t Buttons::bBtn() {
00215     #ifdef POK_SIM
00216     return simulator.bBtn();
00217     #else
00218     #ifndef PROJ_BUTTONS_POLLING_ONLY
00219     return Pokitto::heldStates[BTN_B];
00220     #else
00221     return Pokitto::Core::bBtn();
00222     #endif
00223     #endif // POK_SIM
00224 }
00225 
00226 uint8_t Buttons::cBtn() {
00227     uint8_t c;
00228     #ifdef POK_SIM
00229     c = simulator.cBtn();
00230     #else
00231     #ifndef PROJ_BUTTONS_POLLING_ONLY
00232     c = Pokitto::heldStates[BTN_C];
00233     #else
00234     return Pokitto::Core::cBtn();
00235     #endif
00236     #endif // POK_SIM
00237     return c;
00238 }
00239 
00240 uint8_t Buttons::leftBtn() {
00241     #ifdef POK_SIM
00242     return simulator.leftBtn();
00243     #else
00244     #ifndef PROJ_BUTTONS_POLLING_ONLY
00245     return Pokitto::heldStates[BTN_LEFT];
00246     #else
00247     return Pokitto::Core::leftBtn();
00248     #endif
00249     #endif // POK_SIM
00250 }
00251 
00252 uint8_t Buttons::rightBtn() {
00253     #ifdef POK_SIM
00254     return simulator.rightBtn();
00255     #else
00256     #ifndef PROJ_BUTTONS_POLLING_ONLY
00257     return Pokitto::heldStates[BTN_RIGHT];
00258     #else
00259     return Pokitto::Core::rightBtn();
00260     #endif
00261     #endif // POK_SIM
00262 }
00263 
00264 uint8_t Buttons::upBtn() {
00265     #ifdef POK_SIM
00266     return simulator.upBtn();
00267     #else
00268     #ifndef PROJ_BUTTONS_POLLING_ONLY
00269     return Pokitto::heldStates[BTN_UP];
00270     #else
00271     return Pokitto::Core::upBtn();
00272     #endif
00273     #endif // POK_SIM
00274 }
00275 
00276 uint8_t Buttons::downBtn() {
00277     #ifdef POK_SIM
00278     return simulator.downBtn();
00279     #else
00280     #ifndef PROJ_BUTTONS_POLLING_ONLY
00281     return Pokitto::heldStates[BTN_DOWN];
00282     #else
00283     return Pokitto::Core::downBtn();
00284     #endif
00285     #endif // POK_SIM
00286 }
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 //** EOF **//