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

Committer:
Pokitto
Date:
Wed Apr 25 14:00:49 2018 +0000
Revision:
41:e667167aa9d8
Parent:
40:3b88ff5aff13
new button handler;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 39:e8bb18900c34 1 /**************************************************************************/
Pokitto 39:e8bb18900c34 2 /*!
Pokitto 39:e8bb18900c34 3 @file HWButtons.cpp
Pokitto 39:e8bb18900c34 4 @author Jonne Valola
Pokitto 39:e8bb18900c34 5
Pokitto 39:e8bb18900c34 6 @section LICENSE
Pokitto 39:e8bb18900c34 7
Pokitto 39:e8bb18900c34 8 Software License Agreement (BSD License)
Pokitto 39:e8bb18900c34 9
Pokitto 39:e8bb18900c34 10 Copyright (c) 2016, Jonne Valola
Pokitto 39:e8bb18900c34 11 All rights reserved.
Pokitto 39:e8bb18900c34 12
Pokitto 39:e8bb18900c34 13 Redistribution and use in source and binary forms, with or without
Pokitto 39:e8bb18900c34 14 modification, are permitted provided that the following conditions are met:
Pokitto 39:e8bb18900c34 15 1. Redistributions of source code must retain the above copyright
Pokitto 39:e8bb18900c34 16 notice, this list of conditions and the following disclaimer.
Pokitto 39:e8bb18900c34 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 39:e8bb18900c34 18 notice, this list of conditions and the following disclaimer in the
Pokitto 39:e8bb18900c34 19 documentation and/or other materials provided with the distribution.
Pokitto 39:e8bb18900c34 20 3. Neither the name of the copyright holders nor the
Pokitto 39:e8bb18900c34 21 names of its contributors may be used to endorse or promote products
Pokitto 39:e8bb18900c34 22 derived from this software without specific prior written permission.
Pokitto 39:e8bb18900c34 23
Pokitto 39:e8bb18900c34 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 39:e8bb18900c34 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 39:e8bb18900c34 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 39:e8bb18900c34 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 39:e8bb18900c34 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 39:e8bb18900c34 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 39:e8bb18900c34 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 39:e8bb18900c34 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 39:e8bb18900c34 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 39:e8bb18900c34 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 39:e8bb18900c34 34 */
Pokitto 39:e8bb18900c34 35 /**************************************************************************/
Pokitto 39:e8bb18900c34 36
Pokitto 39:e8bb18900c34 37 #include "HWButtons.h"
Pokitto 39:e8bb18900c34 38 #include "PokittoCore.h"
Pokitto 39:e8bb18900c34 39 #include "PokittoSound.h"
Pokitto 39:e8bb18900c34 40 #include "PokittoDisplay.h"
Pokitto 39:e8bb18900c34 41
Pokitto 39:e8bb18900c34 42 Pokitto::Sound _s;
Pokitto 39:e8bb18900c34 43 Pokitto::Display _bd;
Pokitto 39:e8bb18900c34 44
Pokitto 39:e8bb18900c34 45 using namespace mbed;
Pokitto 39:e8bb18900c34 46
Pokitto 39:e8bb18900c34 47 InterruptIn ABtn(POK_BTN_A_PIN);
Pokitto 39:e8bb18900c34 48 InterruptIn BBtn(POK_BTN_B_PIN);
Pokitto 39:e8bb18900c34 49 InterruptIn CBtn(POK_BTN_C_PIN);
Pokitto 39:e8bb18900c34 50 InterruptIn UBtn(POK_BTN_UP_PIN);
Pokitto 39:e8bb18900c34 51 InterruptIn DBtn(POK_BTN_DOWN_PIN);
Pokitto 39:e8bb18900c34 52 InterruptIn LBtn(POK_BTN_LEFT_PIN);
Pokitto 39:e8bb18900c34 53 InterruptIn RBtn(POK_BTN_RIGHT_PIN);
Pokitto 39:e8bb18900c34 54
Pokitto 39:e8bb18900c34 55 #define BS_IDLE 0
Pokitto 39:e8bb18900c34 56 #define BS_DOWN 1
Pokitto 39:e8bb18900c34 57 #define BS_UP 2
Pokitto 39:e8bb18900c34 58
Pokitto 39:e8bb18900c34 59 uint8_t Pokitto::heldStates[NUM_BTN];
Pokitto 39:e8bb18900c34 60
Pokitto 39:e8bb18900c34 61 void APressed() {
Pokitto 39:e8bb18900c34 62 Pokitto::heldStates[BTN_A] = 1;
Pokitto 39:e8bb18900c34 63 }
Pokitto 39:e8bb18900c34 64 void AReleased() {
Pokitto 39:e8bb18900c34 65 Pokitto::heldStates[BTN_A] = 0;
Pokitto 39:e8bb18900c34 66 }
Pokitto 39:e8bb18900c34 67 void BPressed() { Pokitto::heldStates[BTN_B] = 1; }
Pokitto 39:e8bb18900c34 68 void BReleased() { Pokitto::heldStates[BTN_B] = 0; }
Pokitto 39:e8bb18900c34 69 void CPressed() {
Pokitto 39:e8bb18900c34 70 Pokitto::heldStates[BTN_C] = 1;
Pokitto 39:e8bb18900c34 71 }
Pokitto 39:e8bb18900c34 72 void CReleased() { Pokitto::heldStates[BTN_C] = 0; }
Pokitto 39:e8bb18900c34 73 void UPressed() { Pokitto::heldStates[BTN_UP] = 1; }
Pokitto 39:e8bb18900c34 74 void UReleased() { Pokitto::heldStates[BTN_UP] = 0; }
Pokitto 39:e8bb18900c34 75 void DPressed() { Pokitto::heldStates[BTN_DOWN] = 1; }
Pokitto 39:e8bb18900c34 76 void DReleased() { Pokitto::heldStates[BTN_DOWN] = 0; }
Pokitto 39:e8bb18900c34 77 void RPressed() {
Pokitto 39:e8bb18900c34 78 /* Hardware volume control */
Pokitto 39:e8bb18900c34 79 if (Pokitto::heldStates[BTN_C]) _s.volumeUp();
Pokitto 39:e8bb18900c34 80 else Pokitto::heldStates[BTN_RIGHT] = 1;
Pokitto 39:e8bb18900c34 81 }
Pokitto 39:e8bb18900c34 82 void RReleased() { Pokitto::heldStates[BTN_RIGHT] = 0; }
Pokitto 39:e8bb18900c34 83 void LPressed() {
Pokitto 39:e8bb18900c34 84 /* Hardware volume control */
Pokitto 39:e8bb18900c34 85 if (Pokitto::heldStates[BTN_C]) _s.volumeDown();
Pokitto 39:e8bb18900c34 86 else Pokitto::heldStates[BTN_LEFT] = 1;
Pokitto 39:e8bb18900c34 87 }
Pokitto 39:e8bb18900c34 88 void LReleased() { Pokitto::heldStates[BTN_LEFT] = 0; }
Pokitto 39:e8bb18900c34 89
Pokitto 39:e8bb18900c34 90 static inline void ClearPinInt(LPC_PIN_INT_T *pPININT, uint32_t pins)
Pokitto 39:e8bb18900c34 91 {
Pokitto 39:e8bb18900c34 92 pPININT->IST = pins;
Pokitto 39:e8bb18900c34 93 }
Pokitto 39:e8bb18900c34 94
Pokitto 39:e8bb18900c34 95 void PIN_INT0_IRQHandler(void)
Pokitto 39:e8bb18900c34 96 {
Pokitto 39:e8bb18900c34 97 //Pokitto::heldStates[BTN_A] = 1 - Pokitto::heldStates[BTN_A];
Pokitto 39:e8bb18900c34 98 //uint32_t pins = ((LPC_PIN_INT_T*)LPC_PININT)->FALL;
Pokitto 41:e667167aa9d8 99 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<0)) Pokitto::heldStates[BTN_A] = 1;
Pokitto 41:e667167aa9d8 100 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<0)) Pokitto::heldStates[BTN_A] = 0;
Pokitto 41:e667167aa9d8 101 Pokitto::heldStates[BTN_A]=ABtn.read();
Pokitto 39:e8bb18900c34 102 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(0));
Pokitto 39:e8bb18900c34 103 }
Pokitto 39:e8bb18900c34 104
Pokitto 39:e8bb18900c34 105 void PIN_INT1_IRQHandler(void)
Pokitto 39:e8bb18900c34 106 {
Pokitto 41:e667167aa9d8 107 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<1)) Pokitto::heldStates[BTN_B] = 1;
Pokitto 41:e667167aa9d8 108 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<1)) Pokitto::heldStates[BTN_B] = 0;
Pokitto 41:e667167aa9d8 109 Pokitto::heldStates[BTN_B]=BBtn.read();
Pokitto 39:e8bb18900c34 110 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(1));
Pokitto 39:e8bb18900c34 111 }
Pokitto 39:e8bb18900c34 112
Pokitto 39:e8bb18900c34 113 void PIN_INT2_IRQHandler(void)
Pokitto 39:e8bb18900c34 114 {
Pokitto 41:e667167aa9d8 115 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<2)) Pokitto::heldStates[BTN_C] = 1;
Pokitto 41:e667167aa9d8 116 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<2)) Pokitto::heldStates[BTN_C] = 0;
Pokitto 41:e667167aa9d8 117 Pokitto::heldStates[BTN_C]=CBtn.read();
Pokitto 39:e8bb18900c34 118 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(2));
Pokitto 39:e8bb18900c34 119 }
Pokitto 39:e8bb18900c34 120
Pokitto 39:e8bb18900c34 121 void PIN_INT3_IRQHandler(void)
Pokitto 39:e8bb18900c34 122 {
Pokitto 41:e667167aa9d8 123 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<3)) Pokitto::heldStates[BTN_UP] = 1;
Pokitto 41:e667167aa9d8 124 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<3)) Pokitto::heldStates[BTN_UP] = 0;
Pokitto 41:e667167aa9d8 125 Pokitto::heldStates[BTN_UP]=UBtn.read();
Pokitto 39:e8bb18900c34 126 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(3));
Pokitto 39:e8bb18900c34 127 }
Pokitto 39:e8bb18900c34 128
Pokitto 39:e8bb18900c34 129 void PIN_INT4_IRQHandler(void)
Pokitto 39:e8bb18900c34 130 {
Pokitto 41:e667167aa9d8 131 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 1;
Pokitto 41:e667167aa9d8 132 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 0;
Pokitto 41:e667167aa9d8 133 Pokitto::heldStates[BTN_DOWN]=DBtn.read();
Pokitto 39:e8bb18900c34 134 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(4));
Pokitto 39:e8bb18900c34 135 }
Pokitto 39:e8bb18900c34 136
Pokitto 39:e8bb18900c34 137 void PIN_INT5_IRQHandler(void)
Pokitto 39:e8bb18900c34 138 {
Pokitto 39:e8bb18900c34 139 /* Hardware volume control */
Pokitto 41:e667167aa9d8 140 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 1;
Pokitto 41:e667167aa9d8 141 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 0;
Pokitto 41:e667167aa9d8 142 Pokitto::heldStates[BTN_C]=CBtn.read();
Pokitto 41:e667167aa9d8 143 Pokitto::heldStates[BTN_LEFT]=LBtn.read();
Pokitto 39:e8bb18900c34 144 if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_LEFT]==0) _s.volumeDown();
Pokitto 39:e8bb18900c34 145 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(5));
Pokitto 39:e8bb18900c34 146 }
Pokitto 39:e8bb18900c34 147
Pokitto 39:e8bb18900c34 148 void PIN_INT6_IRQHandler(void)
Pokitto 39:e8bb18900c34 149 {
Pokitto 39:e8bb18900c34 150 /* Hardware volume control */
Pokitto 41:e667167aa9d8 151 //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 1;
Pokitto 41:e667167aa9d8 152 //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 0;
Pokitto 41:e667167aa9d8 153 Pokitto::heldStates[BTN_C]=CBtn.read();
Pokitto 41:e667167aa9d8 154 Pokitto::heldStates[BTN_RIGHT]=RBtn.read();
Pokitto 39:e8bb18900c34 155 if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_RIGHT]==0) _s.volumeUp();
Pokitto 39:e8bb18900c34 156 ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(6));
Pokitto 39:e8bb18900c34 157 }
Pokitto 39:e8bb18900c34 158
Pokitto 39:e8bb18900c34 159
Pokitto 39:e8bb18900c34 160 void Pokitto::initButtons() {
Pokitto 39:e8bb18900c34 161 ABtn.fall(&AReleased);
Pokitto 39:e8bb18900c34 162 ABtn.rise(&APressed);
Pokitto 39:e8bb18900c34 163 BBtn.fall(&BReleased);
Pokitto 39:e8bb18900c34 164 BBtn.rise(&BPressed);
Pokitto 39:e8bb18900c34 165 CBtn.fall(&CReleased);
Pokitto 39:e8bb18900c34 166 CBtn.rise(&CPressed);
Pokitto 39:e8bb18900c34 167 UBtn.fall(&UReleased);
Pokitto 39:e8bb18900c34 168 UBtn.rise(&UPressed);
Pokitto 39:e8bb18900c34 169 DBtn.fall(&DReleased);
Pokitto 39:e8bb18900c34 170 DBtn.rise(&DPressed);
Pokitto 39:e8bb18900c34 171 LBtn.fall(&LReleased);
Pokitto 39:e8bb18900c34 172 LBtn.rise(&LPressed);
Pokitto 39:e8bb18900c34 173 RBtn.fall(&RReleased);
Pokitto 39:e8bb18900c34 174 RBtn.rise(&RPressed);
Pokitto 39:e8bb18900c34 175 NVIC_SetVector((IRQn_Type)(PIN_INT0_IRQn), (uint32_t)&PIN_INT0_IRQHandler);
Pokitto 39:e8bb18900c34 176 NVIC_SetVector((IRQn_Type)(PIN_INT1_IRQn), (uint32_t)&PIN_INT1_IRQHandler);
Pokitto 39:e8bb18900c34 177 NVIC_SetVector((IRQn_Type)(PIN_INT2_IRQn), (uint32_t)&PIN_INT2_IRQHandler);
Pokitto 39:e8bb18900c34 178 NVIC_SetVector((IRQn_Type)(PIN_INT3_IRQn), (uint32_t)&PIN_INT3_IRQHandler);
Pokitto 39:e8bb18900c34 179 NVIC_SetVector((IRQn_Type)(PIN_INT4_IRQn), (uint32_t)&PIN_INT4_IRQHandler);
Pokitto 39:e8bb18900c34 180 NVIC_SetVector((IRQn_Type)(PIN_INT5_IRQn), (uint32_t)&PIN_INT5_IRQHandler);
Pokitto 39:e8bb18900c34 181 NVIC_SetVector((IRQn_Type)(PIN_INT6_IRQn), (uint32_t)&PIN_INT6_IRQHandler);
Pokitto 39:e8bb18900c34 182 }
Pokitto 39:e8bb18900c34 183
Pokitto 39:e8bb18900c34 184 uint8_t Pokitto::Core::aBtn() {
Pokitto 39:e8bb18900c34 185 return Pokitto::heldStates[BTN_A];
Pokitto 39:e8bb18900c34 186 }
Pokitto 39:e8bb18900c34 187
Pokitto 39:e8bb18900c34 188 uint8_t Pokitto::Core::bBtn() {
Pokitto 39:e8bb18900c34 189 return Pokitto::heldStates[BTN_B];
Pokitto 39:e8bb18900c34 190 }
Pokitto 39:e8bb18900c34 191
Pokitto 39:e8bb18900c34 192 uint8_t Pokitto::Core::cBtn() {
Pokitto 39:e8bb18900c34 193 return Pokitto::heldStates[BTN_C];
Pokitto 39:e8bb18900c34 194 }
Pokitto 39:e8bb18900c34 195
Pokitto 39:e8bb18900c34 196 uint8_t Pokitto::Core::upBtn() {
Pokitto 39:e8bb18900c34 197 return Pokitto::heldStates[BTN_UP];
Pokitto 39:e8bb18900c34 198 }
Pokitto 39:e8bb18900c34 199 uint8_t Pokitto::Core::downBtn() {
Pokitto 39:e8bb18900c34 200 return Pokitto::heldStates[BTN_DOWN];
Pokitto 39:e8bb18900c34 201 }
Pokitto 39:e8bb18900c34 202
Pokitto 39:e8bb18900c34 203 uint8_t Pokitto::Core::leftBtn() {
Pokitto 39:e8bb18900c34 204 return Pokitto::heldStates[BTN_LEFT];
Pokitto 39:e8bb18900c34 205 }
Pokitto 39:e8bb18900c34 206 uint8_t Pokitto::Core::rightBtn() {
Pokitto 39:e8bb18900c34 207 return Pokitto::heldStates[BTN_RIGHT];
Pokitto 39:e8bb18900c34 208 }