Platform library for RETRO
Embed:
(wiki syntax)
Show/hide line numbers
Retro.cpp
00001 /* 00002 * (C) Copyright 2015 Valentin Ivanov. All rights reserved. 00003 * 00004 * This file is part of the RetroPlatform Library 00005 * 00006 * The RetroPlatform Library is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/> 00018 * 00019 * This library is inspired by Gamebuino Library (http://gamebuino.com) 00020 * from Aurélien Rodot. 00021 */ 00022 #include "Retro.h" 00023 #include "us_ticker_api.h" 00024 00025 const uint16_t OKSequence[] = {0x0005,0x138,0x168,0x0000}; 00026 const uint16_t CancelSequence[] = {0x0005,0x168,0x138,0x0000}; 00027 const uint16_t TickSequence[] = {0x0045,0x168,0x0000}; 00028 00029 DigitalIn Retro::pin[NUM_BTN] = { 00030 DigitalIn(P0_14, PullUp), //left 00031 DigitalIn(P0_11, PullUp), //right 00032 DigitalIn(P0_12, PullUp), //down 00033 DigitalIn(P0_13, PullUp), //up 00034 DigitalIn(P0_16, PullUp), //robot 00035 DigitalIn(P0_1, PullUp) //ship 00036 }; 00037 00038 Retro::Retro(): display( 00039 P0_19, 00040 P0_20, 00041 P0_7, 00042 P0_21, 00043 P0_22, 00044 P1_15, 00045 P0_2, 00046 LCD_ST7735::RGB), leftEye(P0_9,false), rightEye(P0_8,false) 00047 { 00048 initialize(); 00049 } 00050 00051 00052 void Retro::initialize() 00053 { 00054 timePerFrame = 50000; 00055 frameStartUs = us_ticker_read(); 00056 00057 display.setOrientation(LCD_ST7735::Rotate270, false); 00058 display.setForegroundColor(Color565::White); 00059 display.setBackgroundColor(Color565::Black); 00060 00061 sound.initialize(); 00062 sound.setVolume(1); 00063 00064 setFrameRate(20); 00065 } 00066 00067 void Retro::setFrameRate(uint8_t fps) 00068 { 00069 timePerFrame = 1000000 / fps; 00070 sound.prescaler = fps / 20; 00071 sound.prescaler = max(1, sound.prescaler); 00072 } 00073 00074 bool once = false; 00075 00076 bool Retro::update() 00077 { 00078 uint32_t current_time = us_ticker_read(); 00079 frameDurationUs = current_time - frameStartUs; 00080 00081 if( frameDurationUs > timePerFrame ) { 00082 frameStartUs = current_time; 00083 00084 00085 once = false; 00086 00087 readButtons(); 00088 return true; 00089 } else { 00090 if( !once ) { 00091 once = true; 00092 sound.update(); 00093 00094 } 00095 return false; 00096 } 00097 } 00098 00099 void Retro::playOK() 00100 { 00101 sound.playSequence(OKSequence,0); 00102 } 00103 00104 void Retro::playCancel() 00105 { 00106 sound.playSequence(CancelSequence,0); 00107 } 00108 00109 void Retro::playTick() 00110 { 00111 sound.playSequence(TickSequence,0); 00112 } 00113 00114 bool Retro::collideCheck( int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) 00115 { 00116 return !( x2 >= x1+w1 || x1 >= x2+w2 || y2 >= y1+h1 || y1 >= y2 + h2 ); 00117 } 00118 00119 //Buttons 00120 void Retro::readButtons() 00121 { 00122 for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) { 00123 if ( pin[thisButton].read() == 0 ) { 00124 _state[thisButton]++; 00125 } else { 00126 if (_state[thisButton] == 0) 00127 continue; 00128 if (_state[thisButton] == 0xFF) 00129 _state[thisButton] = 0; 00130 else 00131 _state[thisButton] = 0xFF; 00132 } 00133 } 00134 } 00135 00136 bool Retro::pressed(uint8_t button) 00137 { 00138 if (_state[button] == 1) 00139 return true; 00140 else 00141 return false; 00142 } 00143 00144 bool Retro::released(uint8_t button) 00145 { 00146 if (_state[button] == 0xFF) 00147 return true; 00148 else 00149 return false; 00150 } 00151 00152 bool Retro::held(uint8_t button, uint8_t time) 00153 { 00154 if(_state[button] == (time+1)) 00155 return true; 00156 else 00157 return false; 00158 } 00159 00160 bool Retro::repeat(uint8_t button, uint8_t period) 00161 { 00162 if (period <= 1) { 00163 if ((_state[button] != 0xFF) && (_state[button])) 00164 return true; 00165 } else { 00166 if ((_state[button] != 0xFF) && ((_state[button] % period) == 1)) 00167 return true; 00168 } 00169 return false; 00170 } 00171 00172 uint8_t Retro::timeHeld(uint8_t button) 00173 { 00174 if(_state[button] != 0xFF) 00175 return _state[button]; 00176 else 00177 return 0; 00178 00179 }
Generated on Tue Jul 12 2022 23:21:39 by 1.7.2