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:
Sat Mar 24 13:17:22 2018 +0000
Revision:
34:427237571c6a
Parent:
31:f4b9b85c7b62
Bug fix (missing definition of uint8_t output)

Who changed what in which revision?

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