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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Sat Mar 23 20:03:34 2019 +0000
Revision:
66:6281a40d73e6
Parent:
43:6183b12dd99c
Child:
68:61a4ccb0a4b6
Updated pokittolib to current embitz dev branch

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 66:6281a40d73e6 129 if (states[button] == 0xFF) {
Pokitto 66:6281a40d73e6 130 states[button] = 0; // prevent continuous released state!
Pokitto 43:6183b12dd99c 131 return true;
Pokitto 66:6281a40d73e6 132 }
Pokitto 43:6183b12dd99c 133 else
Pokitto 43:6183b12dd99c 134 return false;
Pokitto 43:6183b12dd99c 135 }
Pokitto 43:6183b12dd99c 136
Pokitto 43:6183b12dd99c 137 /**
Pokitto 43:6183b12dd99c 138 * returns true ONCE when 'button' is held for 'time' frames
Pokitto 43:6183b12dd99c 139 * @param button The button's ID
Pokitto 43:6183b12dd99c 140 * @param time How much frames button must be held, between 1 and 254.
Pokitto 43:6183b12dd99c 141 * @return true when 'button' is held for 'time' frames
Pokitto 43:6183b12dd99c 142 */
Pokitto 43:6183b12dd99c 143 bool Buttons::held(uint8_t button, uint8_t time){
Pokitto 43:6183b12dd99c 144 if(states[button] == (time+1))
Pokitto 43:6183b12dd99c 145 return true;
Pokitto 43:6183b12dd99c 146 else
Pokitto 43:6183b12dd99c 147 return false;
Pokitto 43:6183b12dd99c 148 }
Pokitto 43:6183b12dd99c 149
Pokitto 43:6183b12dd99c 150 /**
Pokitto 43:6183b12dd99c 151 * returns true every 'period' frames when 'button' is held
Pokitto 43:6183b12dd99c 152 * @param button The button's ID
Pokitto 43:6183b12dd99c 153 * @param period How much frames button must be held, between 1 and 254.
Pokitto 43:6183b12dd99c 154 * @return true if the button is held for the given time
Pokitto 43:6183b12dd99c 155 */
Pokitto 43:6183b12dd99c 156 bool Buttons::repeat(uint8_t button, uint8_t period) {
Pokitto 43:6183b12dd99c 157 if (period <= 1) {
Pokitto 43:6183b12dd99c 158 if ((states[button] != 0xFF) && (states[button]))
Pokitto 43:6183b12dd99c 159 return true;
Pokitto 43:6183b12dd99c 160 } else {
Pokitto 43:6183b12dd99c 161 if ((states[button] != 0xFF) && ((states[button] % period) == 1))
Pokitto 43:6183b12dd99c 162 return true;
Pokitto 43:6183b12dd99c 163 }
Pokitto 43:6183b12dd99c 164 return false;
Pokitto 43:6183b12dd99c 165 }
Pokitto 43:6183b12dd99c 166
Pokitto 43:6183b12dd99c 167 /**
Pokitto 43:6183b12dd99c 168 *
Pokitto 43:6183b12dd99c 169 * @param button The button's ID
Pokitto 43:6183b12dd99c 170 * @return The number of frames during which the button has been held.
Pokitto 43:6183b12dd99c 171 */
Pokitto 43:6183b12dd99c 172 uint8_t Buttons::timeHeld(uint8_t button){
Pokitto 43:6183b12dd99c 173 if(states[button] != 0xFF)
Pokitto 43:6183b12dd99c 174 return states[button];
Pokitto 43:6183b12dd99c 175 else
Pokitto 43:6183b12dd99c 176 return 0;
Pokitto 43:6183b12dd99c 177
Pokitto 43:6183b12dd99c 178 }
Pokitto 43:6183b12dd99c 179
Pokitto 43:6183b12dd99c 180 void Buttons::pollButtons() {
Pokitto 43:6183b12dd99c 181 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 182 simulator.pollButtons();
Pokitto 43:6183b12dd99c 183 #else
Pokitto 43:6183b12dd99c 184 uint8_t buttons_state_old = buttons_state;
Pokitto 43:6183b12dd99c 185 buttons_state = 0; // clear all
Pokitto 43:6183b12dd99c 186 if (upBtn()) buttons_state |= (1<<UPBIT);
Pokitto 43:6183b12dd99c 187 if (downBtn()) buttons_state |= (1<<DOWNBIT);
Pokitto 43:6183b12dd99c 188 if (leftBtn()) buttons_state |= (1<<LEFTBIT);
Pokitto 43:6183b12dd99c 189 if (rightBtn()) buttons_state |= (1<<RIGHTBIT);
Pokitto 43:6183b12dd99c 190 if (aBtn()) buttons_state |= (1<<ABIT);
Pokitto 43:6183b12dd99c 191 if (bBtn()) buttons_state |= (1<<BBIT);
Pokitto 43:6183b12dd99c 192 if (cBtn()) buttons_state |= (1<<CBIT);
Pokitto 43:6183b12dd99c 193 buttons_held = buttons_state & buttons_state_old; // only if both 1, end result is 1
Pokitto 43:6183b12dd99c 194 buttons_released = ~buttons_state & buttons_state_old; // if now zero, then 1 AND previous 1 = 1
Pokitto 43:6183b12dd99c 195 #endif // POK_SIM
Pokitto 43:6183b12dd99c 196 }
Pokitto 43:6183b12dd99c 197
Pokitto 43:6183b12dd99c 198 uint8_t Buttons::aBtn() {
Pokitto 43:6183b12dd99c 199 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 200 return simulator.aBtn();
Pokitto 43:6183b12dd99c 201 #else
Pokitto 43:6183b12dd99c 202 return Pokitto::heldStates[BTN_A];
Pokitto 43:6183b12dd99c 203 #endif // POK_SIM
Pokitto 43:6183b12dd99c 204 }
Pokitto 43:6183b12dd99c 205
Pokitto 43:6183b12dd99c 206
Pokitto 43:6183b12dd99c 207 uint8_t Buttons::bBtn() {
Pokitto 43:6183b12dd99c 208 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 209 return simulator.bBtn();
Pokitto 43:6183b12dd99c 210 #else
Pokitto 43:6183b12dd99c 211 return Pokitto::heldStates[BTN_B];
Pokitto 43:6183b12dd99c 212 #endif // POK_SIM
Pokitto 43:6183b12dd99c 213 }
Pokitto 43:6183b12dd99c 214
Pokitto 43:6183b12dd99c 215 uint8_t Buttons::cBtn() {
Pokitto 43:6183b12dd99c 216 uint8_t c;
Pokitto 43:6183b12dd99c 217 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 218 c = simulator.cBtn();
Pokitto 43:6183b12dd99c 219 #else
Pokitto 43:6183b12dd99c 220 c = Pokitto::heldStates[BTN_C];
Pokitto 43:6183b12dd99c 221 #endif // POK_SIM
Pokitto 43:6183b12dd99c 222 return c;
Pokitto 43:6183b12dd99c 223 }
Pokitto 43:6183b12dd99c 224
Pokitto 43:6183b12dd99c 225 uint8_t Buttons::leftBtn() {
Pokitto 43:6183b12dd99c 226 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 227 return simulator.leftBtn();
Pokitto 43:6183b12dd99c 228 #else
Pokitto 43:6183b12dd99c 229 return Pokitto::heldStates[BTN_LEFT];
Pokitto 43:6183b12dd99c 230 #endif // POK_SIM
Pokitto 43:6183b12dd99c 231 }
Pokitto 43:6183b12dd99c 232
Pokitto 43:6183b12dd99c 233 uint8_t Buttons::rightBtn() {
Pokitto 43:6183b12dd99c 234 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 235 return simulator.rightBtn();
Pokitto 43:6183b12dd99c 236 #else
Pokitto 43:6183b12dd99c 237 return Pokitto::heldStates[BTN_RIGHT];
Pokitto 43:6183b12dd99c 238 #endif // POK_SIM
Pokitto 43:6183b12dd99c 239 }
Pokitto 43:6183b12dd99c 240
Pokitto 43:6183b12dd99c 241 uint8_t Buttons::upBtn() {
Pokitto 43:6183b12dd99c 242 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 243 return simulator.upBtn();
Pokitto 43:6183b12dd99c 244 #else
Pokitto 43:6183b12dd99c 245 return Pokitto::heldStates[BTN_UP];
Pokitto 43:6183b12dd99c 246 #endif // POK_SIM
Pokitto 43:6183b12dd99c 247 }
Pokitto 43:6183b12dd99c 248
Pokitto 43:6183b12dd99c 249 uint8_t Buttons::downBtn() {
Pokitto 43:6183b12dd99c 250 #ifdef POK_SIM
Pokitto 43:6183b12dd99c 251 return simulator.downBtn();
Pokitto 43:6183b12dd99c 252 #else
Pokitto 43:6183b12dd99c 253 return Pokitto::heldStates[BTN_DOWN];
Pokitto 43:6183b12dd99c 254 #endif // POK_SIM
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
Pokitto 43:6183b12dd99c 263
Pokitto 43:6183b12dd99c 264 //** EOF **//
Pokitto 43:6183b12dd99c 265