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

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
43:6183b12dd99c
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 43:6183b12dd99c 1 /**************************************************************************/
Pokitto 43:6183b12dd99c 2 /*!
Pokitto 43:6183b12dd99c 3 @file PokittoButtons.cpp
Pokitto 43:6183b12dd99c 4 @author Jonne Valola
Pokitto 43:6183b12dd99c 5
Pokitto 43:6183b12dd99c 6 @section LICENSE
Pokitto 43:6183b12dd99c 7
Pokitto 43:6183b12dd99c 8 Software License Agreement (BSD License)
Pokitto 43:6183b12dd99c 9
Pokitto 43:6183b12dd99c 10 Copyright (c) 2016, Jonne Valola
Pokitto 43:6183b12dd99c 11 All rights reserved.
Pokitto 43:6183b12dd99c 12
Pokitto 43:6183b12dd99c 13 Redistribution and use in source and binary forms, with or without
Pokitto 43:6183b12dd99c 14 modification, are permitted provided that the following conditions are met:
Pokitto 43:6183b12dd99c 15 1. Redistributions of source code must retain the above copyright
Pokitto 43:6183b12dd99c 16 notice, this list of conditions and the following disclaimer.
Pokitto 43:6183b12dd99c 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 43:6183b12dd99c 18 notice, this list of conditions and the following disclaimer in the
Pokitto 43:6183b12dd99c 19 documentation and/or other materials provided with the distribution.
Pokitto 43:6183b12dd99c 20 3. Neither the name of the copyright holders nor the
Pokitto 43:6183b12dd99c 21 names of its contributors may be used to endorse or promote products
Pokitto 43:6183b12dd99c 22 derived from this software without specific prior written permission.
Pokitto 43:6183b12dd99c 23
Pokitto 43:6183b12dd99c 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 43:6183b12dd99c 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 43:6183b12dd99c 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 43:6183b12dd99c 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 43:6183b12dd99c 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 43:6183b12dd99c 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 43:6183b12dd99c 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 43:6183b12dd99c 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 43:6183b12dd99c 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 43:6183b12dd99c 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 43:6183b12dd99c 34 */
Pokitto 43:6183b12dd99c 35 /**************************************************************************/
Pokitto 43:6183b12dd99c 36
Pokitto 43:6183b12dd99c 37 #include "PokittoCore.h"
Pokitto 43:6183b12dd99c 38
Pokitto 43:6183b12dd99c 39 using namespace Pokitto;
Pokitto 43:6183b12dd99c 40
Pokitto 43:6183b12dd99c 41 uint8_t Buttons::pins[NUM_BTN];
Pokitto 43:6183b12dd99c 42 uint8_t Buttons::states[NUM_BTN];
Pokitto 43:6183b12dd99c 43 uint8_t Buttons::buttons_state;
Pokitto 43:6183b12dd99c 44 uint8_t Buttons::buttons_held;
Pokitto 43:6183b12dd99c 45 uint8_t Buttons::buttons_released; // from LSB up,down,left,right,a,b,c
Pokitto 43:6183b12dd99c 46 uint16_t Buttons::cHWLongPress = CHWLONGPRESSTIMEOUT;
Pokitto 43:6183b12dd99c 47
Pokitto 43:6183b12dd99c 48
Pokitto 43:6183b12dd99c 49 void Buttons::begin() {
Pokitto 43:6183b12dd99c 50 #ifndef POK_SIM
Pokitto 43:6183b12dd99c 51 Pokitto::initButtons();
Pokitto 43:6183b12dd99c 52 #endif // POK_SIM
Pokitto 43:6183b12dd99c 53 }
Pokitto 43:6183b12dd99c 54
Pokitto 43:6183b12dd99c 55 void Buttons::update() {
Pokitto 43:6183b12dd99c 56 #if POK_USE_CONSOLE
Pokitto 43:6183b12dd99c 57 if (console.conscounter) return;
Pokitto 43:6183b12dd99c 58 #endif // POK_USE_CONSOLE
Pokitto 43:6183b12dd99c 59 #ifndef POK_SIM
Pokitto 43:6183b12dd99c 60 /** HARDWARE CODE **/
Pokitto 43:6183b12dd99c 61 for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
Pokitto 43:6183b12dd99c 62 if (Pokitto::heldStates[thisButton]) { //if button pressed
Pokitto 43:6183b12dd99c 63 states[thisButton]++; //increase button hold time
Pokitto 43:6183b12dd99c 64 if (states[thisButton]==0xFF) states[thisButton]=2; // PREVENT WRAPAROUND!!!!
Pokitto 43:6183b12dd99c 65 } else {
Pokitto 43:6183b12dd99c 66 if (states[thisButton] == 0)//button idle
Pokitto 43:6183b12dd99c 67 continue;
Pokitto 43:6183b12dd99c 68 if (states[thisButton] == 0xFF)//if previously released
Pokitto 43:6183b12dd99c 69 states[thisButton] = 0; //set to idle
Pokitto 43:6183b12dd99c 70 else
Pokitto 43:6183b12dd99c 71 states[thisButton] = 0xFF; //button just released
Pokitto 43:6183b12dd99c 72 }
Pokitto 43:6183b12dd99c 73 }
Pokitto 43:6183b12dd99c 74 #else
Pokitto 43:6183b12dd99c 75 /** POK_SIM code **/
Pokitto 43:6183b12dd99c 76 simulator.pollButtons();
Pokitto 43:6183b12dd99c 77 for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
Pokitto 43:6183b12dd99c 78 uint8_t temp=0;
Pokitto 43:6183b12dd99c 79 switch (thisButton) {
Pokitto 43:6183b12dd99c 80 case 0:
Pokitto 43:6183b12dd99c 81 temp = simulator.leftHeld(); break;
Pokitto 43:6183b12dd99c 82 case 1:
Pokitto 43:6183b12dd99c 83 temp = simulator.upHeld(); break;
Pokitto 43:6183b12dd99c 84 case 2:
Pokitto 43:6183b12dd99c 85 temp = simulator.rightHeld(); break;
Pokitto 43:6183b12dd99c 86 case 3:
Pokitto 43:6183b12dd99c 87 temp = simulator.downHeld(); break;
Pokitto 43:6183b12dd99c 88 case 4:
Pokitto 43:6183b12dd99c 89 temp = simulator.aHeld(); break;
Pokitto 43:6183b12dd99c 90 case 5:
Pokitto 43:6183b12dd99c 91 temp = simulator.bHeld(); break;
Pokitto 43:6183b12dd99c 92 case 6:
Pokitto 43:6183b12dd99c 93 temp = simulator.cHeld(); break;
Pokitto 43:6183b12dd99c 94 default:
Pokitto 43:6183b12dd99c 95 break;
Pokitto 43:6183b12dd99c 96 }
Pokitto 43:6183b12dd99c 97
Pokitto 43:6183b12dd99c 98 if (temp == HIGH) { //if button pressed
Pokitto 43:6183b12dd99c 99 states[thisButton]++; //increase button hold time
Pokitto 43:6183b12dd99c 100 if (states[thisButton]==0xFF) states[thisButton] = 2; //prevent wraparound and retrigger
Pokitto 43:6183b12dd99c 101 } else {
Pokitto 43:6183b12dd99c 102 if (states[thisButton] == 0)//button idle
Pokitto 43:6183b12dd99c 103 continue;
Pokitto 43:6183b12dd99c 104 if (states[thisButton] == 0xFF)//if previously released
Pokitto 43:6183b12dd99c 105 states[thisButton] = 0; //set to idle
Pokitto 43:6183b12dd99c 106 else
Pokitto 43:6183b12dd99c 107 states[thisButton] = 0xFF; //button just released
Pokitto 43:6183b12dd99c 108 }
Pokitto 43:6183b12dd99c 109 }
Pokitto 43:6183b12dd99c 110
Pokitto 43:6183b12dd99c 111 #endif // POK_SIM
Pokitto 43:6183b12dd99c 112 }
Pokitto 43:6183b12dd99c 113
Pokitto 43:6183b12dd99c 114 /*
Pokitto 43:6183b12dd99c 115 * Returns true when 'button' is pressed.
Pokitto 43:6183b12dd99c 116 * The button has to be released for it to be triggered again.
Pokitto 43:6183b12dd99c 117 */
Pokitto 43:6183b12dd99c 118 bool Buttons::pressed(uint8_t button) {
Pokitto 43:6183b12dd99c 119 if (states[button] == 1)
Pokitto 43:6183b12dd99c 120 return true;
Pokitto 43:6183b12dd99c 121 else
Pokitto 43:6183b12dd99c 122 return false;
Pokitto 43:6183b12dd99c 123 }
Pokitto 43:6183b12dd99c 124
Pokitto 43:6183b12dd99c 125 /*
Pokitto 43:6183b12dd99c 126 * return true if 'button' is released
Pokitto 43:6183b12dd99c 127 */
Pokitto 43:6183b12dd99c 128 bool Buttons::released(uint8_t button) {
Pokitto 43:6183b12dd99c 129 if (states[button] == 0xFF)
Pokitto 43:6183b12dd99c 130 return true;
Pokitto 43:6183b12dd99c 131 else
Pokitto 43:6183b12dd99c 132 return false;
Pokitto 43:6183b12dd99c 133 }
Pokitto 43:6183b12dd99c 134
Pokitto 43:6183b12dd99c 135 /**
Pokitto 43:6183b12dd99c 136 * returns true ONCE when 'button' is held for 'time' frames
Pokitto 43:6183b12dd99c 137 * @param button The button's ID
Pokitto 43:6183b12dd99c 138 * @param time How much frames button must be held, between 1 and 254.
Pokitto 43:6183b12dd99c 139 * @return true when 'button' is held for 'time' frames
Pokitto 43:6183b12dd99c 140 */
Pokitto 43:6183b12dd99c 141 bool Buttons::held(uint8_t button, uint8_t time){
Pokitto 43:6183b12dd99c 142 if(states[button] == (time+1))
Pokitto 43:6183b12dd99c 143 return true;
Pokitto 43:6183b12dd99c 144 else
Pokitto 43:6183b12dd99c 145 return false;
Pokitto 43:6183b12dd99c 146 }
Pokitto 43:6183b12dd99c 147
Pokitto 43:6183b12dd99c 148 /**
Pokitto 43:6183b12dd99c 149 * returns true every 'period' frames when 'button' is held
Pokitto 43:6183b12dd99c 150 * @param button The button's ID
Pokitto 43:6183b12dd99c 151 * @param period How much frames button must be held, between 1 and 254.
Pokitto 43:6183b12dd99c 152 * @return true if the button is held for the given time
Pokitto 43:6183b12dd99c 153 */
Pokitto 43:6183b12dd99c 154 bool Buttons::repeat(uint8_t button, uint8_t period) {
Pokitto 43:6183b12dd99c 155 if (period <= 1) {
Pokitto 43:6183b12dd99c 156 if ((states[button] != 0xFF) && (states[button]))
Pokitto 43:6183b12dd99c 157 return true;
Pokitto 43:6183b12dd99c 158 } else {
Pokitto 43:6183b12dd99c 159 if ((states[button] != 0xFF) && ((states[button] % period) == 1))
Pokitto 43:6183b12dd99c 160 return true;
Pokitto 43:6183b12dd99c 161 }
Pokitto 43:6183b12dd99c 162 return false;
Pokitto 43:6183b12dd99c 163 }
Pokitto 43:6183b12dd99c 164
Pokitto 43:6183b12dd99c 165 /**
Pokitto 43:6183b12dd99c 166 *
Pokitto 43:6183b12dd99c 167 * @param button The button's ID
Pokitto 43:6183b12dd99c 168 * @return The number of frames during which the button has been held.
Pokitto 43:6183b12dd99c 169 */
Pokitto 43:6183b12dd99c 170 uint8_t Buttons::timeHeld(uint8_t button){
Pokitto 43:6183b12dd99c 171 if(states[button] != 0xFF)
Pokitto 43:6183b12dd99c 172 return states[button];
Pokitto 43:6183b12dd99c 173 else
Pokitto 43:6183b12dd99c 174 return 0;
Pokitto 43:6183b12dd99c 175
Pokitto 43:6183b12dd99c 176 }
Pokitto 43:6183b12dd99c 177
Pokitto 43:6183b12dd99c 178 void Buttons::pollButtons() {
Pokitto 43:6183b12dd99c 179 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 180 simulator.pollButtons();
Pokitto 43:6183b12dd99c 181 #else
Pokitto 43:6183b12dd99c 182 uint8_t buttons_state_old = buttons_state;
Pokitto 43:6183b12dd99c 183 buttons_state = 0; // clear all
Pokitto 43:6183b12dd99c 184 if (upBtn()) buttons_state |= (1<<UPBIT);
Pokitto 43:6183b12dd99c 185 if (downBtn()) buttons_state |= (1<<DOWNBIT);
Pokitto 43:6183b12dd99c 186 if (leftBtn()) buttons_state |= (1<<LEFTBIT);
Pokitto 43:6183b12dd99c 187 if (rightBtn()) buttons_state |= (1<<RIGHTBIT);
Pokitto 43:6183b12dd99c 188 if (aBtn()) buttons_state |= (1<<ABIT);
Pokitto 43:6183b12dd99c 189 if (bBtn()) buttons_state |= (1<<BBIT);
Pokitto 43:6183b12dd99c 190 if (cBtn()) buttons_state |= (1<<CBIT);
Pokitto 43:6183b12dd99c 191 buttons_held = buttons_state & buttons_state_old; // only if both 1, end result is 1
Pokitto 43:6183b12dd99c 192 buttons_released = ~buttons_state & buttons_state_old; // if now zero, then 1 AND previous 1 = 1
Pokitto 43:6183b12dd99c 193 #endif // POK_SIM
Pokitto 43:6183b12dd99c 194 }
Pokitto 43:6183b12dd99c 195
Pokitto 43:6183b12dd99c 196 uint8_t Buttons::aBtn() {
Pokitto 43:6183b12dd99c 197 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 198 return simulator.aBtn();
Pokitto 43:6183b12dd99c 199 #else
Pokitto 43:6183b12dd99c 200 return Pokitto::heldStates[BTN_A];
Pokitto 43:6183b12dd99c 201 #endif // POK_SIM
Pokitto 43:6183b12dd99c 202 }
Pokitto 43:6183b12dd99c 203
Pokitto 43:6183b12dd99c 204
Pokitto 43:6183b12dd99c 205 uint8_t Buttons::bBtn() {
Pokitto 43:6183b12dd99c 206 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 207 return simulator.bBtn();
Pokitto 43:6183b12dd99c 208 #else
Pokitto 43:6183b12dd99c 209 return Pokitto::heldStates[BTN_B];
Pokitto 43:6183b12dd99c 210 #endif // POK_SIM
Pokitto 43:6183b12dd99c 211 }
Pokitto 43:6183b12dd99c 212
Pokitto 43:6183b12dd99c 213 uint8_t Buttons::cBtn() {
Pokitto 43:6183b12dd99c 214 uint8_t c;
Pokitto 43:6183b12dd99c 215 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 216 c = simulator.cBtn();
Pokitto 43:6183b12dd99c 217 #else
Pokitto 43:6183b12dd99c 218 c = Pokitto::heldStates[BTN_C];
Pokitto 43:6183b12dd99c 219 #endif // POK_SIM
Pokitto 43:6183b12dd99c 220 return c;
Pokitto 43:6183b12dd99c 221 }
Pokitto 43:6183b12dd99c 222
Pokitto 43:6183b12dd99c 223 uint8_t Buttons::leftBtn() {
Pokitto 43:6183b12dd99c 224 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 225 return simulator.leftBtn();
Pokitto 43:6183b12dd99c 226 #else
Pokitto 43:6183b12dd99c 227 return Pokitto::heldStates[BTN_LEFT];
Pokitto 43:6183b12dd99c 228 #endif // POK_SIM
Pokitto 43:6183b12dd99c 229 }
Pokitto 43:6183b12dd99c 230
Pokitto 43:6183b12dd99c 231 uint8_t Buttons::rightBtn() {
Pokitto 43:6183b12dd99c 232 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 233 return simulator.rightBtn();
Pokitto 43:6183b12dd99c 234 #else
Pokitto 43:6183b12dd99c 235 return Pokitto::heldStates[BTN_RIGHT];
Pokitto 43:6183b12dd99c 236 #endif // POK_SIM
Pokitto 43:6183b12dd99c 237 }
Pokitto 43:6183b12dd99c 238
Pokitto 43:6183b12dd99c 239 uint8_t Buttons::upBtn() {
Pokitto 43:6183b12dd99c 240 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 241 return simulator.upBtn();
Pokitto 43:6183b12dd99c 242 #else
Pokitto 43:6183b12dd99c 243 return Pokitto::heldStates[BTN_UP];
Pokitto 43:6183b12dd99c 244 #endif // POK_SIM
Pokitto 43:6183b12dd99c 245 }
Pokitto 43:6183b12dd99c 246
Pokitto 43:6183b12dd99c 247 uint8_t Buttons::downBtn() {
Pokitto 43:6183b12dd99c 248 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 249 return simulator.downBtn();
Pokitto 43:6183b12dd99c 250 #else
Pokitto 43:6183b12dd99c 251 return Pokitto::heldStates[BTN_DOWN];
Pokitto 43:6183b12dd99c 252 #endif // POK_SIM
Pokitto 43:6183b12dd99c 253 }
Pokitto 43:6183b12dd99c 254
Pokitto 43:6183b12dd99c 255
Pokitto 43:6183b12dd99c 256
Pokitto 43:6183b12dd99c 257
Pokitto 43:6183b12dd99c 258
Pokitto 43:6183b12dd99c 259
Pokitto 43:6183b12dd99c 260
Pokitto 43:6183b12dd99c 261
Pokitto 43:6183b12dd99c 262 //** EOF **//
Pokitto 43:6183b12dd99c 263