PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
POKITTO_HW/HWButtons.cpp@40:3b88ff5aff13, 2018-04-23 (annotated)
- Committer:
- Pokitto
- Date:
- Mon Apr 23 00:21:50 2018 +0000
- Revision:
- 40:3b88ff5aff13
- Parent:
- 39:e8bb18900c34
- Child:
- 41:e667167aa9d8
Buttons handling
Who changed what in which revision?
User | Revision | Line number | New 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 | 39:e8bb18900c34 | 99 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<0)) Pokitto::heldStates[BTN_A] = 1; |
Pokitto | 40:3b88ff5aff13 | 100 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<0)) Pokitto::heldStates[BTN_A] = 0; |
Pokitto | 39:e8bb18900c34 | 101 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(0)); |
Pokitto | 39:e8bb18900c34 | 102 | } |
Pokitto | 39:e8bb18900c34 | 103 | |
Pokitto | 39:e8bb18900c34 | 104 | void PIN_INT1_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 105 | { |
Pokitto | 39:e8bb18900c34 | 106 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<1)) Pokitto::heldStates[BTN_B] = 1; |
Pokitto | 40:3b88ff5aff13 | 107 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<1)) Pokitto::heldStates[BTN_B] = 0; |
Pokitto | 39:e8bb18900c34 | 108 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(1)); |
Pokitto | 39:e8bb18900c34 | 109 | } |
Pokitto | 39:e8bb18900c34 | 110 | |
Pokitto | 39:e8bb18900c34 | 111 | void PIN_INT2_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 112 | { |
Pokitto | 39:e8bb18900c34 | 113 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<2)) Pokitto::heldStates[BTN_C] = 1; |
Pokitto | 40:3b88ff5aff13 | 114 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<2)) Pokitto::heldStates[BTN_C] = 0; |
Pokitto | 39:e8bb18900c34 | 115 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(2)); |
Pokitto | 39:e8bb18900c34 | 116 | } |
Pokitto | 39:e8bb18900c34 | 117 | |
Pokitto | 39:e8bb18900c34 | 118 | void PIN_INT3_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 119 | { |
Pokitto | 39:e8bb18900c34 | 120 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<3)) Pokitto::heldStates[BTN_UP] = 1; |
Pokitto | 40:3b88ff5aff13 | 121 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<3)) Pokitto::heldStates[BTN_UP] = 0; |
Pokitto | 39:e8bb18900c34 | 122 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(3)); |
Pokitto | 39:e8bb18900c34 | 123 | } |
Pokitto | 39:e8bb18900c34 | 124 | |
Pokitto | 39:e8bb18900c34 | 125 | void PIN_INT4_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 126 | { |
Pokitto | 39:e8bb18900c34 | 127 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 1; |
Pokitto | 40:3b88ff5aff13 | 128 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 0; |
Pokitto | 39:e8bb18900c34 | 129 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(4)); |
Pokitto | 39:e8bb18900c34 | 130 | } |
Pokitto | 39:e8bb18900c34 | 131 | |
Pokitto | 39:e8bb18900c34 | 132 | void PIN_INT5_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 133 | { |
Pokitto | 39:e8bb18900c34 | 134 | /* Hardware volume control */ |
Pokitto | 39:e8bb18900c34 | 135 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 1; |
Pokitto | 40:3b88ff5aff13 | 136 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 0; |
Pokitto | 39:e8bb18900c34 | 137 | if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_LEFT]==0) _s.volumeDown(); |
Pokitto | 39:e8bb18900c34 | 138 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(5)); |
Pokitto | 39:e8bb18900c34 | 139 | } |
Pokitto | 39:e8bb18900c34 | 140 | |
Pokitto | 39:e8bb18900c34 | 141 | void PIN_INT6_IRQHandler(void) |
Pokitto | 39:e8bb18900c34 | 142 | { |
Pokitto | 39:e8bb18900c34 | 143 | /* Hardware volume control */ |
Pokitto | 39:e8bb18900c34 | 144 | if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 1; |
Pokitto | 40:3b88ff5aff13 | 145 | else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 0; |
Pokitto | 39:e8bb18900c34 | 146 | if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_RIGHT]==0) _s.volumeUp(); |
Pokitto | 39:e8bb18900c34 | 147 | ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(6)); |
Pokitto | 39:e8bb18900c34 | 148 | } |
Pokitto | 39:e8bb18900c34 | 149 | |
Pokitto | 39:e8bb18900c34 | 150 | |
Pokitto | 39:e8bb18900c34 | 151 | void Pokitto::initButtons() { |
Pokitto | 39:e8bb18900c34 | 152 | ABtn.fall(&AReleased); |
Pokitto | 39:e8bb18900c34 | 153 | ABtn.rise(&APressed); |
Pokitto | 39:e8bb18900c34 | 154 | BBtn.fall(&BReleased); |
Pokitto | 39:e8bb18900c34 | 155 | BBtn.rise(&BPressed); |
Pokitto | 39:e8bb18900c34 | 156 | CBtn.fall(&CReleased); |
Pokitto | 39:e8bb18900c34 | 157 | CBtn.rise(&CPressed); |
Pokitto | 39:e8bb18900c34 | 158 | UBtn.fall(&UReleased); |
Pokitto | 39:e8bb18900c34 | 159 | UBtn.rise(&UPressed); |
Pokitto | 39:e8bb18900c34 | 160 | DBtn.fall(&DReleased); |
Pokitto | 39:e8bb18900c34 | 161 | DBtn.rise(&DPressed); |
Pokitto | 39:e8bb18900c34 | 162 | LBtn.fall(&LReleased); |
Pokitto | 39:e8bb18900c34 | 163 | LBtn.rise(&LPressed); |
Pokitto | 39:e8bb18900c34 | 164 | RBtn.fall(&RReleased); |
Pokitto | 39:e8bb18900c34 | 165 | RBtn.rise(&RPressed); |
Pokitto | 39:e8bb18900c34 | 166 | NVIC_SetVector((IRQn_Type)(PIN_INT0_IRQn), (uint32_t)&PIN_INT0_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 167 | NVIC_SetVector((IRQn_Type)(PIN_INT1_IRQn), (uint32_t)&PIN_INT1_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 168 | NVIC_SetVector((IRQn_Type)(PIN_INT2_IRQn), (uint32_t)&PIN_INT2_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 169 | NVIC_SetVector((IRQn_Type)(PIN_INT3_IRQn), (uint32_t)&PIN_INT3_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 170 | NVIC_SetVector((IRQn_Type)(PIN_INT4_IRQn), (uint32_t)&PIN_INT4_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 171 | NVIC_SetVector((IRQn_Type)(PIN_INT5_IRQn), (uint32_t)&PIN_INT5_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 172 | NVIC_SetVector((IRQn_Type)(PIN_INT6_IRQn), (uint32_t)&PIN_INT6_IRQHandler); |
Pokitto | 39:e8bb18900c34 | 173 | } |
Pokitto | 39:e8bb18900c34 | 174 | |
Pokitto | 39:e8bb18900c34 | 175 | uint8_t Pokitto::Core::aBtn() { |
Pokitto | 39:e8bb18900c34 | 176 | return Pokitto::heldStates[BTN_A]; |
Pokitto | 39:e8bb18900c34 | 177 | } |
Pokitto | 39:e8bb18900c34 | 178 | |
Pokitto | 39:e8bb18900c34 | 179 | uint8_t Pokitto::Core::bBtn() { |
Pokitto | 39:e8bb18900c34 | 180 | return Pokitto::heldStates[BTN_B]; |
Pokitto | 39:e8bb18900c34 | 181 | } |
Pokitto | 39:e8bb18900c34 | 182 | |
Pokitto | 39:e8bb18900c34 | 183 | uint8_t Pokitto::Core::cBtn() { |
Pokitto | 39:e8bb18900c34 | 184 | return Pokitto::heldStates[BTN_C]; |
Pokitto | 39:e8bb18900c34 | 185 | } |
Pokitto | 39:e8bb18900c34 | 186 | |
Pokitto | 39:e8bb18900c34 | 187 | uint8_t Pokitto::Core::upBtn() { |
Pokitto | 39:e8bb18900c34 | 188 | return Pokitto::heldStates[BTN_UP]; |
Pokitto | 39:e8bb18900c34 | 189 | } |
Pokitto | 39:e8bb18900c34 | 190 | uint8_t Pokitto::Core::downBtn() { |
Pokitto | 39:e8bb18900c34 | 191 | return Pokitto::heldStates[BTN_DOWN]; |
Pokitto | 39:e8bb18900c34 | 192 | } |
Pokitto | 39:e8bb18900c34 | 193 | |
Pokitto | 39:e8bb18900c34 | 194 | uint8_t Pokitto::Core::leftBtn() { |
Pokitto | 39:e8bb18900c34 | 195 | return Pokitto::heldStates[BTN_LEFT]; |
Pokitto | 39:e8bb18900c34 | 196 | } |
Pokitto | 39:e8bb18900c34 | 197 | uint8_t Pokitto::Core::rightBtn() { |
Pokitto | 39:e8bb18900c34 | 198 | return Pokitto::heldStates[BTN_RIGHT]; |
Pokitto | 39:e8bb18900c34 | 199 | } |