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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HWButtons.cpp Source File

HWButtons.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     HWButtons.cpp
00004     @author   Jonne Valola
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2016, Jonne Valola
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 #include "HWButtons.h "
00038 #include "PokittoCore.h "
00039 #include "PokittoSound.h "
00040 #include "PokittoDisplay.h "
00041 
00042 Pokitto::Sound _s;
00043 Pokitto::Display _bd;
00044 
00045 using namespace mbed;
00046 
00047 #ifndef PROJ_BUTTONS_POLLING_ONLY
00048 InterruptIn ABtn(POK_BTN_A_PIN);
00049 InterruptIn BBtn(POK_BTN_B_PIN);
00050 InterruptIn CBtn(POK_BTN_C_PIN);
00051 InterruptIn UBtn(POK_BTN_UP_PIN);
00052 InterruptIn DBtn(POK_BTN_DOWN_PIN);
00053 InterruptIn LBtn(POK_BTN_LEFT_PIN);
00054 InterruptIn RBtn(POK_BTN_RIGHT_PIN);
00055 #else
00056 DigitalIn ABtn(POK_BTN_A_PIN);
00057 DigitalIn BBtn(POK_BTN_B_PIN);
00058 DigitalIn CBtn(POK_BTN_C_PIN);
00059 DigitalIn UBtn(POK_BTN_UP_PIN);
00060 DigitalIn DBtn(POK_BTN_DOWN_PIN);
00061 DigitalIn LBtn(POK_BTN_LEFT_PIN);
00062 DigitalIn RBtn(POK_BTN_RIGHT_PIN);
00063 #endif
00064 
00065 
00066 #define BS_IDLE 0
00067 #define BS_DOWN 1
00068 #define BS_UP 2
00069 
00070 uint8_t Pokitto::heldStates[NUM_BTN];
00071 bool vol_control_clicked=false;
00072 
00073 void APressed() {
00074     Pokitto::heldStates[BTN_A] = 1;
00075     }
00076 void AReleased() {
00077     Pokitto::heldStates[BTN_A] = 0;
00078     }
00079 void BPressed() { Pokitto::heldStates[BTN_B] = 1; }
00080 void BReleased() { Pokitto::heldStates[BTN_B] = 0; }
00081 void CPressed() {
00082     Pokitto::heldStates[BTN_C] = 1;
00083     }
00084 void CReleased() { Pokitto::heldStates[BTN_C] = 0; }
00085 void UPressed() { Pokitto::heldStates[BTN_UP] = 1; }
00086 void UReleased() { Pokitto::heldStates[BTN_UP] = 0; }
00087 void DPressed() { Pokitto::heldStates[BTN_DOWN] = 1; }
00088 void DReleased() { Pokitto::heldStates[BTN_DOWN] = 0; }
00089 void RPressed() {
00090     /* Hardware volume control */
00091     if (Pokitto::heldStates[BTN_C]) _s.volumeUp();
00092     else Pokitto::heldStates[BTN_RIGHT] = 1;
00093     }
00094 void RReleased() { Pokitto::heldStates[BTN_RIGHT] = 0; }
00095 void LPressed() {
00096     /* Hardware volume control */
00097     if (Pokitto::heldStates[BTN_C]) _s.volumeDown();
00098     else Pokitto::heldStates[BTN_LEFT] = 1;
00099     }
00100 void LReleased() { Pokitto::heldStates[BTN_LEFT] = 0; }
00101 
00102 #ifndef PROJ_BUTTONS_POLLING_ONLY
00103 static inline void ClearPinInt(LPC_PIN_INT_T *pPININT, uint32_t pins)
00104 {
00105     pPININT->IST = pins;
00106 }
00107 
00108 void PIN_INT0_IRQHandler(void)
00109 {
00110     //Pokitto::heldStates[BTN_A] = 1 - Pokitto::heldStates[BTN_A];
00111     //uint32_t  pins = ((LPC_PIN_INT_T*)LPC_PININT)->FALL;
00112     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<0)) Pokitto::heldStates[BTN_A] = 1;
00113     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<0)) Pokitto::heldStates[BTN_A] = 0;
00114     Pokitto::heldStates[BTN_A]=ABtn.read();
00115     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(0));
00116 }
00117 
00118 void PIN_INT1_IRQHandler(void)
00119 {
00120     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<1)) Pokitto::heldStates[BTN_B] = 1;
00121     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<1)) Pokitto::heldStates[BTN_B] = 0;
00122     Pokitto::heldStates[BTN_B]=BBtn.read();
00123     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(1));
00124 }
00125 
00126 void PIN_INT2_IRQHandler(void)
00127 {
00128     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<2)) Pokitto::heldStates[BTN_C] = 1;
00129     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<2)) Pokitto::heldStates[BTN_C] = 0;
00130     Pokitto::heldStates[BTN_C]=CBtn.read();
00131     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(2));
00132 }
00133 
00134 void PIN_INT3_IRQHandler(void)
00135 {
00136     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<3)) Pokitto::heldStates[BTN_UP] = 1;
00137     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<3)) Pokitto::heldStates[BTN_UP] = 0;
00138     Pokitto::heldStates[BTN_UP]=UBtn.read();
00139     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(3));
00140 }
00141 
00142 void PIN_INT4_IRQHandler(void)
00143 {
00144     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 1;
00145     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<4)) Pokitto::heldStates[BTN_DOWN] = 0;
00146     Pokitto::heldStates[BTN_DOWN]=DBtn.read();
00147     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(4));
00148 }
00149 
00150 void PIN_INT5_IRQHandler(void)
00151 {
00152     /* Hardware volume control */
00153     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 1;
00154     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<5)) Pokitto::heldStates[BTN_LEFT] = 0;
00155     Pokitto::heldStates[BTN_C]=CBtn.read();
00156     Pokitto::heldStates[BTN_LEFT]=LBtn.read();
00157     if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_LEFT])
00158     {
00159         if (!vol_control_clicked) _s.volumeDown();
00160         Pokitto::heldStates[BTN_LEFT]=0; //do not do normal button operation
00161         vol_control_clicked=true;
00162     } else vol_control_clicked=false;
00163 
00164     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(5));
00165 }
00166 
00167 void PIN_INT6_IRQHandler(void)
00168 {
00169     /* Hardware volume control */
00170     //if ((((LPC_PIN_INT_T*)LPC_PININT)->RISE)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 1;
00171     //else if ((((LPC_PIN_INT_T*)LPC_PININT)->FALL)&(1<<6)) Pokitto::heldStates[BTN_RIGHT] = 0;
00172     Pokitto::heldStates[BTN_C]=CBtn.read();
00173     Pokitto::heldStates[BTN_RIGHT]=RBtn.read();
00174     if (Pokitto::heldStates[BTN_C] && Pokitto::heldStates[BTN_RIGHT])
00175     {
00176         if (!vol_control_clicked) _s.volumeUp();
00177         Pokitto::heldStates[BTN_RIGHT]=0; //do not do normal button operation
00178         vol_control_clicked=true;
00179     } else vol_control_clicked=false;
00180 
00181     ClearPinInt((LPC_PIN_INT_T *)LPC_PININT, PININTCH(6));
00182 }
00183 #endif
00184 
00185 
00186 
00187 void Pokitto::initButtons() {
00188   #ifndef PROJ_BUTTONS_POLLING_ONLY
00189   ABtn.fall(&AReleased);
00190   ABtn.rise(&APressed);
00191   BBtn.fall(&BReleased);
00192   BBtn.rise(&BPressed);
00193   CBtn.fall(&CReleased);
00194   CBtn.rise(&CPressed);
00195   UBtn.fall(&UReleased);
00196   UBtn.rise(&UPressed);
00197   DBtn.fall(&DReleased);
00198   DBtn.rise(&DPressed);
00199   LBtn.fall(&LReleased);
00200   LBtn.rise(&LPressed);
00201   RBtn.fall(&RReleased);
00202   RBtn.rise(&RPressed);
00203   NVIC_SetVector((IRQn_Type)(PIN_INT0_IRQn), (uint32_t)&PIN_INT0_IRQHandler);
00204   NVIC_SetVector((IRQn_Type)(PIN_INT1_IRQn), (uint32_t)&PIN_INT1_IRQHandler);
00205   NVIC_SetVector((IRQn_Type)(PIN_INT2_IRQn), (uint32_t)&PIN_INT2_IRQHandler);
00206   NVIC_SetVector((IRQn_Type)(PIN_INT3_IRQn), (uint32_t)&PIN_INT3_IRQHandler);
00207   NVIC_SetVector((IRQn_Type)(PIN_INT4_IRQn), (uint32_t)&PIN_INT4_IRQHandler);
00208   NVIC_SetVector((IRQn_Type)(PIN_INT5_IRQn), (uint32_t)&PIN_INT5_IRQHandler);
00209   NVIC_SetVector((IRQn_Type)(PIN_INT6_IRQn), (uint32_t)&PIN_INT6_IRQHandler);
00210   #endif
00211 }
00212 
00213 uint8_t Pokitto::Core::aBtn() {
00214 
00215     #ifndef PROJ_BUTTONS_POLLING_ONLY
00216     return Pokitto::heldStates[BTN_A];
00217     #else
00218     Pokitto::heldStates[BTN_A]=ABtn.read();
00219     return Pokitto::heldStates[BTN_A];
00220     #endif
00221 
00222 }
00223 
00224 uint8_t Pokitto::Core::bBtn() {
00225 
00226     #ifndef PROJ_BUTTONS_POLLING_ONLY
00227     return Pokitto::heldStates[BTN_B];
00228     #else
00229     Pokitto::heldStates[BTN_B]=BBtn.read();
00230     return Pokitto::heldStates[BTN_B];
00231     #endif
00232 }
00233 
00234 uint8_t Pokitto::Core::cBtn() {
00235 
00236     #ifndef PROJ_BUTTONS_POLLING_ONLY
00237     return Pokitto::heldStates[BTN_C];
00238     #else
00239     Pokitto::heldStates[BTN_C]=CBtn.read();
00240     return Pokitto::heldStates[BTN_C];
00241     #endif
00242 }
00243 
00244 uint8_t Pokitto::Core::upBtn() {
00245 
00246     #ifndef PROJ_BUTTONS_POLLING_ONLY
00247     return Pokitto::heldStates[BTN_UP];
00248     #else
00249     Pokitto::heldStates[BTN_UP]=UBtn.read();
00250     return Pokitto::heldStates[BTN_UP];
00251     #endif
00252 }
00253 uint8_t Pokitto::Core::downBtn() {
00254 
00255     #ifndef PROJ_BUTTONS_POLLING_ONLY
00256     return Pokitto::heldStates[BTN_DOWN];
00257     #else
00258     Pokitto::heldStates[BTN_DOWN]=DBtn.read();
00259     return Pokitto::heldStates[BTN_DOWN];
00260     #endif
00261 }
00262 
00263 uint8_t Pokitto::Core::leftBtn() {
00264 
00265     #ifndef PROJ_BUTTONS_POLLING_ONLY
00266     return Pokitto::heldStates[BTN_LEFT];
00267     #else
00268     Pokitto::heldStates[BTN_LEFT]=LBtn.read();
00269     return Pokitto::heldStates[BTN_LEFT];
00270     #endif
00271 }
00272 uint8_t Pokitto::Core::rightBtn() {
00273 
00274     #ifndef PROJ_BUTTONS_POLLING_ONLY
00275     return Pokitto::heldStates[BTN_RIGHT];
00276     #else
00277     Pokitto::heldStates[BTN_RIGHT]=RBtn.read();
00278     return Pokitto::heldStates[BTN_RIGHT];
00279     #endif
00280 }
00281