PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Fork of PokittoLib by
PokittoButtons.cpp
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 > 1 00057 if (console.conscounter) return; 00058 #endif // POK_USE_CONSOLE 00059 #ifndef POK_SIM 00060 /** HARDWARE CODE **/ 00061 for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) { 00062 if (Pokitto::heldStates[thisButton]) { //if button pressed 00063 states[thisButton]++; //increase button hold time 00064 } else { 00065 if (states[thisButton] == 0)//button idle 00066 continue; 00067 if (states[thisButton] == 0xFF)//if previously released 00068 states[thisButton] = 0; //set to idle 00069 else 00070 states[thisButton] = 0xFF; //button just released 00071 } 00072 } 00073 #else 00074 /** POK_SIM code **/ 00075 simulator.pollButtons(); 00076 for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) { 00077 uint8_t temp=0; 00078 switch (thisButton) { 00079 case 0: 00080 temp = simulator.leftHeld(); break; 00081 case 1: 00082 temp = simulator.upHeld(); break; 00083 case 2: 00084 temp = simulator.rightHeld(); break; 00085 case 3: 00086 temp = simulator.downHeld(); break; 00087 case 4: 00088 temp = simulator.aHeld(); break; 00089 case 5: 00090 temp = simulator.bHeld(); break; 00091 case 6: 00092 temp = simulator.cHeld(); break; 00093 default: 00094 break; 00095 } 00096 00097 if (temp == HIGH) { //if button pressed 00098 states[thisButton]++; //increase button hold time 00099 } else { 00100 if (states[thisButton] == 0)//button idle 00101 continue; 00102 if (states[thisButton] == 0xFF)//if previously released 00103 states[thisButton] = 0; //set to idle 00104 else 00105 states[thisButton] = 0xFF; //button just released 00106 } 00107 } 00108 00109 #endif // POK_SIM 00110 } 00111 00112 /* 00113 * Returns true when 'button' is pressed. 00114 * The button has to be released for it to be triggered again. 00115 */ 00116 bool Buttons::pressed(uint8_t button) { 00117 if (states[button] == 1) 00118 return true; 00119 else 00120 return false; 00121 } 00122 00123 /* 00124 * return true if 'button' is released 00125 */ 00126 bool Buttons::released(uint8_t button) { 00127 if (states[button] == 0xFF) 00128 return true; 00129 else 00130 return false; 00131 } 00132 00133 /** 00134 * returns true ONCE when 'button' is held for 'time' frames 00135 * @param button The button's ID 00136 * @param time How much frames button must be held, between 1 and 254. 00137 * @return true when 'button' is held for 'time' frames 00138 */ 00139 bool Buttons::held(uint8_t button, uint8_t time){ 00140 if(states[button] == (time+1)) 00141 return true; 00142 else 00143 return false; 00144 } 00145 00146 /** 00147 * returns true every 'period' frames when 'button' is held 00148 * @param button The button's ID 00149 * @param period How much frames button must be held, between 1 and 254. 00150 * @return true if the button is held for the given time 00151 */ 00152 bool Buttons::repeat(uint8_t button, uint8_t period) { 00153 if (period <= 1) { 00154 if ((states[button] != 0xFF) && (states[button])) 00155 return true; 00156 } else { 00157 if ((states[button] != 0xFF) && ((states[button] % period) == 1)) 00158 return true; 00159 } 00160 return false; 00161 } 00162 00163 /** 00164 * 00165 * @param button The button's ID 00166 * @return The number of frames during which the button has been held. 00167 */ 00168 uint8_t Buttons::timeHeld(uint8_t button){ 00169 if(states[button] != 0xFF) 00170 return states[button]; 00171 else 00172 return 0; 00173 00174 } 00175 00176 void Buttons::pollButtons() { 00177 #ifdef POK_SIM 00178 simulator.pollButtons(); 00179 #else 00180 uint8_t buttons_state_old = buttons_state; 00181 buttons_state = 0; // clear all 00182 if (upBtn()) buttons_state |= (1<<UPBIT); 00183 if (downBtn()) buttons_state |= (1<<DOWNBIT); 00184 if (leftBtn()) buttons_state |= (1<<LEFTBIT); 00185 if (rightBtn()) buttons_state |= (1<<RIGHTBIT); 00186 if (aBtn()) buttons_state |= (1<<ABIT); 00187 if (bBtn()) buttons_state |= (1<<BBIT); 00188 if (cBtn()) buttons_state |= (1<<CBIT); 00189 buttons_held = buttons_state & buttons_state_old; // only if both 1, end result is 1 00190 buttons_released = ~buttons_state & buttons_state_old; // if now zero, then 1 AND previous 1 = 1 00191 #endif // POK_SIM 00192 } 00193 00194 uint8_t Buttons::aBtn() { 00195 #ifdef POK_SIM 00196 return simulator.aBtn(); 00197 #else 00198 return Pokitto::heldStates[BTN_A]; 00199 #endif // POK_SIM 00200 } 00201 00202 00203 uint8_t Buttons::bBtn() { 00204 #ifdef POK_SIM 00205 return simulator.bBtn(); 00206 #else 00207 return Pokitto::heldStates[BTN_B]; 00208 #endif // POK_SIM 00209 } 00210 00211 uint8_t Buttons::cBtn() { 00212 uint8_t c; 00213 #ifdef POK_SIM 00214 c = simulator.cBtn(); 00215 #else 00216 c = Pokitto::heldStates[BTN_C]; 00217 #endif // POK_SIM 00218 return c; 00219 } 00220 00221 uint8_t Buttons::leftBtn() { 00222 #ifdef POK_SIM 00223 return simulator.leftBtn(); 00224 #else 00225 return Pokitto::heldStates[BTN_LEFT]; 00226 #endif // POK_SIM 00227 } 00228 00229 uint8_t Buttons::rightBtn() { 00230 #ifdef POK_SIM 00231 return simulator.rightBtn(); 00232 #else 00233 return Pokitto::heldStates[BTN_RIGHT]; 00234 #endif // POK_SIM 00235 } 00236 00237 uint8_t Buttons::upBtn() { 00238 #ifdef POK_SIM 00239 return simulator.upBtn(); 00240 #else 00241 return Pokitto::heldStates[BTN_UP]; 00242 #endif // POK_SIM 00243 } 00244 00245 uint8_t Buttons::downBtn() { 00246 #ifdef POK_SIM 00247 return simulator.downBtn(); 00248 #else 00249 return Pokitto::heldStates[BTN_DOWN]; 00250 #endif // POK_SIM 00251 } 00252 00253 00254 00255 00256 00257 00258 00259 00260 //** EOF **// 00261
Generated on Tue Jul 12 2022 18:08:12 by 1.7.2