Jonne Valola / PokittoLib Featured

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 Jan 29 12:29:45 2018 +0000
Revision:
29:9467921dec10
Parent:
24:9561281d0378
Fixed fillScreen bug on HiRes mode

Who changed what in which revision?

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