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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

Who changed what in which revision?

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