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:
Mon Apr 02 22:37:22 2018 +0000
Revision:
36:771321e70814
Synced with Github repo

Who changed what in which revision?

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