Junwei Zang / Mbed 2 deprecated Embedded_Systems_Project

Dependencies:   mbed

Committer:
el14jz
Date:
Thu May 07 11:26:58 2015 +0000
Revision:
0:411f355688a5
Child:
1:ff57945c704c
Embedded System Project(Arcade Game)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14jz 0:411f355688a5 1 #include "mbed.h"
el14jz 0:411f355688a5 2 #include "N5110.h"
el14jz 0:411f355688a5 3 #include "beep.h"
el14jz 0:411f355688a5 4
el14jz 0:411f355688a5 5 using namespace mbed;
el14jz 0:411f355688a5 6 // constructor
el14jz 0:411f355688a5 7 /** Create a Beep object connected to the specified PwmOut pin
el14jz 0:411f355688a5 8 *
el14jz 0:411f355688a5 9 * @param pin PwmOut pin to connect to
el14jz 0:411f355688a5 10 */
el14jz 0:411f355688a5 11
el14jz 0:411f355688a5 12 BusOut leds(LED1,LED2,LED3,LED4);
el14jz 0:411f355688a5 13 BusOut myled(p24);
el14jz 0:411f355688a5 14
el14jz 0:411f355688a5 15 InterruptIn buttonA(p29);
el14jz 0:411f355688a5 16 InterruptIn buttonB(p28);
el14jz 0:411f355688a5 17
el14jz 0:411f355688a5 18 Beep::Beep(PinName pin) : _pwm(pin) {
el14jz 0:411f355688a5 19 _pwm.write(0.0); // after creating it have to be off
el14jz 0:411f355688a5 20 }
el14jz 0:411f355688a5 21
el14jz 0:411f355688a5 22 /** stop the beep instantaneous
el14jz 0:411f355688a5 23 * usually not used
el14jz 0:411f355688a5 24 */
el14jz 0:411f355688a5 25 void Beep::nobeep() {
el14jz 0:411f355688a5 26 _pwm.write(0.0);
el14jz 0:411f355688a5 27 }
el14jz 0:411f355688a5 28
el14jz 0:411f355688a5 29 /** Beep with given frequency and duration.
el14jz 0:411f355688a5 30 *
el14jz 0:411f355688a5 31 * @param frequency - the frequency of the tone in Hz
el14jz 0:411f355688a5 32 * @param time - the duration of the tone in seconds
el14jz 0:411f355688a5 33 */
el14jz 0:411f355688a5 34
el14jz 0:411f355688a5 35 void Beep::beep(float freq, float time) {
el14jz 0:411f355688a5 36
el14jz 0:411f355688a5 37 _pwm.period(1.0/freq);
el14jz 0:411f355688a5 38 _pwm.write(0.5); // 50% duty cycle - beep on
el14jz 0:411f355688a5 39 toff.attach(this,&Beep::nobeep, time); // time to off
el14jz 0:411f355688a5 40 }
el14jz 0:411f355688a5 41
el14jz 0:411f355688a5 42 Beep buzzer(p21);
el14jz 0:411f355688a5 43
el14jz 0:411f355688a5 44 // change this to alter tolerance of joystick direction
el14jz 0:411f355688a5 45 #define DIRECTION_TOLERANCE 0.05
el14jz 0:411f355688a5 46
el14jz 0:411f355688a5 47 // connections for joystick
el14jz 0:411f355688a5 48 DigitalIn button(p17);
el14jz 0:411f355688a5 49 AnalogIn xPot(p15);
el14jz 0:411f355688a5 50 AnalogIn yPot(p16);
el14jz 0:411f355688a5 51
el14jz 0:411f355688a5 52 // timer to regularly read the joystick
el14jz 0:411f355688a5 53 Ticker pollJoystick;
el14jz 0:411f355688a5 54
el14jz 0:411f355688a5 55 // create enumerated type (0,1,2,3 etc. for direction)
el14jz 0:411f355688a5 56 // could be extended for diagonals etc.
el14jz 0:411f355688a5 57 enum DirectionName {
el14jz 0:411f355688a5 58 UP,
el14jz 0:411f355688a5 59 DOWN,
el14jz 0:411f355688a5 60 LEFT,
el14jz 0:411f355688a5 61 RIGHT,
el14jz 0:411f355688a5 62 CENTRE,
el14jz 0:411f355688a5 63 UNKNOWN
el14jz 0:411f355688a5 64 };
el14jz 0:411f355688a5 65
el14jz 0:411f355688a5 66 // struct for Joystick
el14jz 0:411f355688a5 67 typedef struct JoyStick Joystick;
el14jz 0:411f355688a5 68 struct JoyStick {
el14jz 0:411f355688a5 69 float x; // current x value
el14jz 0:411f355688a5 70 float x0; // 'centred' x value
el14jz 0:411f355688a5 71 float y; // current y value
el14jz 0:411f355688a5 72 float y0; // 'centred' y value
el14jz 0:411f355688a5 73 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el14jz 0:411f355688a5 74 DirectionName direction; // current direction
el14jz 0:411f355688a5 75 };
el14jz 0:411f355688a5 76 // create struct variable
el14jz 0:411f355688a5 77 Joystick joystick;
el14jz 0:411f355688a5 78
el14jz 0:411f355688a5 79 // function prototypes
el14jz 0:411f355688a5 80 void calibrateJoystick();
el14jz 0:411f355688a5 81 void updateJoystick();
el14jz 0:411f355688a5 82
el14jz 0:411f355688a5 83 // read default positions of the joystick to calibrate later readings
el14jz 0:411f355688a5 84 void calibrateJoystick()
el14jz 0:411f355688a5 85 {
el14jz 0:411f355688a5 86 button.mode(PullDown);
el14jz 0:411f355688a5 87 // must not move during calibration
el14jz 0:411f355688a5 88 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14jz 0:411f355688a5 89 joystick.y0 = yPot;
el14jz 0:411f355688a5 90 }
el14jz 0:411f355688a5 91 void updateJoystick()
el14jz 0:411f355688a5 92 {
el14jz 0:411f355688a5 93 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14jz 0:411f355688a5 94 joystick.x = xPot - joystick.x0;
el14jz 0:411f355688a5 95 joystick.y = yPot - joystick.y0;
el14jz 0:411f355688a5 96 // read button state
el14jz 0:411f355688a5 97 joystick.button = button;
el14jz 0:411f355688a5 98
el14jz 0:411f355688a5 99 // calculate direction depending on x,y values
el14jz 0:411f355688a5 100 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14jz 0:411f355688a5 101 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jz 0:411f355688a5 102 joystick.direction = CENTRE;
el14jz 0:411f355688a5 103 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jz 0:411f355688a5 104 joystick.direction = DOWN;
el14jz 0:411f355688a5 105 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jz 0:411f355688a5 106 joystick.direction = UP;
el14jz 0:411f355688a5 107 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14jz 0:411f355688a5 108 joystick.direction = RIGHT;
el14jz 0:411f355688a5 109 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14jz 0:411f355688a5 110 joystick.direction = LEFT;
el14jz 0:411f355688a5 111 } else {
el14jz 0:411f355688a5 112 joystick.direction = UNKNOWN;
el14jz 0:411f355688a5 113 }
el14jz 0:411f355688a5 114 }
el14jz 0:411f355688a5 115
el14jz 0:411f355688a5 116 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin)
el14jz 0:411f355688a5 117 {
el14jz 0:411f355688a5 118
el14jz 0:411f355688a5 119 spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise
el14jz 0:411f355688a5 120 initSPI();
el14jz 0:411f355688a5 121
el14jz 0:411f355688a5 122 // set up pins as required
el14jz 0:411f355688a5 123 led = new PwmOut(ledPin);
el14jz 0:411f355688a5 124 pwr = new DigitalOut(pwrPin);
el14jz 0:411f355688a5 125 sce = new DigitalOut(scePin);
el14jz 0:411f355688a5 126 rst = new DigitalOut(rstPin);
el14jz 0:411f355688a5 127 dc = new DigitalOut(dcPin);
el14jz 0:411f355688a5 128
el14jz 0:411f355688a5 129 }
el14jz 0:411f355688a5 130
el14jz 0:411f355688a5 131 // initialise function - powers up and sends the initialisation commands
el14jz 0:411f355688a5 132 void N5110::init()
el14jz 0:411f355688a5 133 {
el14jz 0:411f355688a5 134 turnOn(); // power up
el14jz 0:411f355688a5 135 wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset
el14jz 0:411f355688a5 136 reset(); // reset LCD - must be done within 100 ms
el14jz 0:411f355688a5 137
el14jz 0:411f355688a5 138 // function set - extended
el14jz 0:411f355688a5 139 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
el14jz 0:411f355688a5 140 // Don't completely understand these parameters - they seem to work as they are
el14jz 0:411f355688a5 141 // Consult the datasheet if you need to change them
el14jz 0:411f355688a5 142 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library
el14jz 0:411f355688a5 143 sendCommand(CMD_TC_TEMP_2); // temperature control
el14jz 0:411f355688a5 144 sendCommand(CMD_BI_MUX_48); // bias
el14jz 0:411f355688a5 145
el14jz 0:411f355688a5 146 // function set - basic
el14jz 0:411f355688a5 147 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
el14jz 0:411f355688a5 148 normalMode(); // normal video mode by default
el14jz 0:411f355688a5 149 sendCommand(CMD_DC_NORMAL_MODE); // black on white
el14jz 0:411f355688a5 150
el14jz 0:411f355688a5 151 // RAM is undefined at power-up so clear
el14jz 0:411f355688a5 152 clearRAM();
el14jz 0:411f355688a5 153
el14jz 0:411f355688a5 154 }
el14jz 0:411f355688a5 155
el14jz 0:411f355688a5 156 // sets normal video mode (black on white)
el14jz 0:411f355688a5 157 void N5110::normalMode()
el14jz 0:411f355688a5 158 {
el14jz 0:411f355688a5 159 sendCommand(CMD_DC_NORMAL_MODE);
el14jz 0:411f355688a5 160
el14jz 0:411f355688a5 161 }
el14jz 0:411f355688a5 162
el14jz 0:411f355688a5 163 // sets normal video mode (white on black)
el14jz 0:411f355688a5 164 void N5110::inverseMode()
el14jz 0:411f355688a5 165 {
el14jz 0:411f355688a5 166 sendCommand(CMD_DC_INVERT_VIDEO);
el14jz 0:411f355688a5 167 }
el14jz 0:411f355688a5 168
el14jz 0:411f355688a5 169 // function to power up the LCD and backlight
el14jz 0:411f355688a5 170 void N5110::turnOn()
el14jz 0:411f355688a5 171 {
el14jz 0:411f355688a5 172 // set brightness of LED - 0.0 to 1.0 - default is 50%
el14jz 0:411f355688a5 173 setBrightness(0.5);
el14jz 0:411f355688a5 174 pwr->write(1); // apply power
el14jz 0:411f355688a5 175 }
el14jz 0:411f355688a5 176
el14jz 0:411f355688a5 177 // function to power down LCD
el14jz 0:411f355688a5 178 void N5110::turnOff()
el14jz 0:411f355688a5 179 {
el14jz 0:411f355688a5 180 setBrightness(0.0); // turn backlight off
el14jz 0:411f355688a5 181 clearRAM(); // clear RAM to ensure specified current consumption
el14jz 0:411f355688a5 182 // send command to ensure we are in basic mode
el14jz 0:411f355688a5 183 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
el14jz 0:411f355688a5 184 // clear the display
el14jz 0:411f355688a5 185 sendCommand(CMD_DC_CLEAR_DISPLAY);
el14jz 0:411f355688a5 186 // enter the extended mode and power down
el14jz 0:411f355688a5 187 sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
el14jz 0:411f355688a5 188 // small delay and then turn off the power pin
el14jz 0:411f355688a5 189 wait_ms(10);
el14jz 0:411f355688a5 190 pwr->write(0);
el14jz 0:411f355688a5 191
el14jz 0:411f355688a5 192 }
el14jz 0:411f355688a5 193
el14jz 0:411f355688a5 194 // function to change LED backlight brightness
el14jz 0:411f355688a5 195 void N5110::setBrightness(float brightness)
el14jz 0:411f355688a5 196 {
el14jz 0:411f355688a5 197 // check whether brightness is within range
el14jz 0:411f355688a5 198 if (brightness < 0.0)
el14jz 0:411f355688a5 199 brightness = 0.0;
el14jz 0:411f355688a5 200 if (brightness > 1.0)
el14jz 0:411f355688a5 201 brightness = 1.0;
el14jz 0:411f355688a5 202 // set PWM duty cycle
el14jz 0:411f355688a5 203 led->write(brightness);
el14jz 0:411f355688a5 204 }
el14jz 0:411f355688a5 205
el14jz 0:411f355688a5 206
el14jz 0:411f355688a5 207 // pulse the active low reset line
el14jz 0:411f355688a5 208 void N5110::reset()
el14jz 0:411f355688a5 209 {
el14jz 0:411f355688a5 210 rst->write(0); // reset the LCD
el14jz 0:411f355688a5 211 rst->write(1);
el14jz 0:411f355688a5 212 }
el14jz 0:411f355688a5 213
el14jz 0:411f355688a5 214 // function to initialise SPI peripheral
el14jz 0:411f355688a5 215 void N5110::initSPI()
el14jz 0:411f355688a5 216 {
el14jz 0:411f355688a5 217 spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge
el14jz 0:411f355688a5 218 spi->frequency(4000000); // maximum of screen is 4 MHz
el14jz 0:411f355688a5 219 }
el14jz 0:411f355688a5 220
el14jz 0:411f355688a5 221 // send a command to the display
el14jz 0:411f355688a5 222 void N5110::sendCommand(unsigned char command)
el14jz 0:411f355688a5 223 {
el14jz 0:411f355688a5 224 dc->write(0); // set DC low for command
el14jz 0:411f355688a5 225 sce->write(0); // set CE low to begin frame
el14jz 0:411f355688a5 226 spi->write(command); // send command
el14jz 0:411f355688a5 227 dc->write(1); // turn back to data by default
el14jz 0:411f355688a5 228 sce->write(1); // set CE high to end frame (expected for transmission of single byte)
el14jz 0:411f355688a5 229
el14jz 0:411f355688a5 230 }
el14jz 0:411f355688a5 231
el14jz 0:411f355688a5 232 // send data to the display at the current XY address
el14jz 0:411f355688a5 233 // dc is set to 1 (i.e. data) after sending a command and so should
el14jz 0:411f355688a5 234 // be the default mode.
el14jz 0:411f355688a5 235 void N5110::sendData(unsigned char data)
el14jz 0:411f355688a5 236 {
el14jz 0:411f355688a5 237 sce->write(0); // set CE low to begin frame
el14jz 0:411f355688a5 238 spi->write(data);
el14jz 0:411f355688a5 239 sce->write(1); // set CE high to end frame (expected for transmission of single byte)
el14jz 0:411f355688a5 240 }
el14jz 0:411f355688a5 241
el14jz 0:411f355688a5 242 // this function writes 0 to the 504 bytes to clear the RAM
el14jz 0:411f355688a5 243 void N5110::clearRAM()
el14jz 0:411f355688a5 244 {
el14jz 0:411f355688a5 245 int i;
el14jz 0:411f355688a5 246 sce->write(0); //set CE low to begin frame
el14jz 0:411f355688a5 247 for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
el14jz 0:411f355688a5 248 spi->write(0x00); // send 0's
el14jz 0:411f355688a5 249 }
el14jz 0:411f355688a5 250 sce->write(1); // set CE high to end frame
el14jz 0:411f355688a5 251
el14jz 0:411f355688a5 252 }
el14jz 0:411f355688a5 253
el14jz 0:411f355688a5 254 // function to set the XY address in RAM for subsequenct data write
el14jz 0:411f355688a5 255 void N5110::setXYAddress(int x, int y)
el14jz 0:411f355688a5 256 {
el14jz 0:411f355688a5 257 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
el14jz 0:411f355688a5 258 sendCommand(0x80 | x); // send addresses to display with relevant mask
el14jz 0:411f355688a5 259 sendCommand(0x40 | y);
el14jz 0:411f355688a5 260 }
el14jz 0:411f355688a5 261 }
el14jz 0:411f355688a5 262
el14jz 0:411f355688a5 263 // These functions are used to set, clear and get the value of pixels in the display
el14jz 0:411f355688a5 264 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh()
el14jz 0:411f355688a5 265 // function must be called after set and clear in order to update the display
el14jz 0:411f355688a5 266 void N5110::setPixel(int x, int y)
el14jz 0:411f355688a5 267 {
el14jz 0:411f355688a5 268 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
el14jz 0:411f355688a5 269 // calculate bank and shift 1 to required position in the data byte
el14jz 0:411f355688a5 270 buffer[x][y/8] |= (1 << y%8);
el14jz 0:411f355688a5 271 }
el14jz 0:411f355688a5 272 }
el14jz 0:411f355688a5 273
el14jz 0:411f355688a5 274 void N5110::clearPixel(int x, int y)
el14jz 0:411f355688a5 275 {
el14jz 0:411f355688a5 276 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
el14jz 0:411f355688a5 277 // calculate bank and shift 1 to required position (using bit clear)
el14jz 0:411f355688a5 278 buffer[x][y/8] &= ~(1 << y%8);
el14jz 0:411f355688a5 279 }
el14jz 0:411f355688a5 280 }
el14jz 0:411f355688a5 281
el14jz 0:411f355688a5 282 int N5110::getPixel(int x, int y)
el14jz 0:411f355688a5 283 {
el14jz 0:411f355688a5 284 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
el14jz 0:411f355688a5 285 // return relevant bank and mask required bit
el14jz 0:411f355688a5 286 return (int) buffer[x][y/8] & (1 << y%8);
el14jz 0:411f355688a5 287 // note this does not necessarily return 1 - a non-zero number represents a pixel
el14jz 0:411f355688a5 288 } else {
el14jz 0:411f355688a5 289 return 0;
el14jz 0:411f355688a5 290 }
el14jz 0:411f355688a5 291 }
el14jz 0:411f355688a5 292
el14jz 0:411f355688a5 293 // function to refresh the display
el14jz 0:411f355688a5 294 void N5110::refresh()
el14jz 0:411f355688a5 295 {
el14jz 0:411f355688a5 296 int i,j;
el14jz 0:411f355688a5 297
el14jz 0:411f355688a5 298 setXYAddress(0,0); // important to set address back to 0,0 before refreshing display
el14jz 0:411f355688a5 299 // address auto increments after printing string, so buffer[0][0] will not coincide
el14jz 0:411f355688a5 300 // with top-left pixel after priting string
el14jz 0:411f355688a5 301
el14jz 0:411f355688a5 302 sce->write(0); //set CE low to begin frame
el14jz 0:411f355688a5 303
el14jz 0:411f355688a5 304 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
el14jz 0:411f355688a5 305 for(i = 0; i < WIDTH; i++) {
el14jz 0:411f355688a5 306 spi->write(buffer[i][j]); // send buffer
el14jz 0:411f355688a5 307 }
el14jz 0:411f355688a5 308 }
el14jz 0:411f355688a5 309 sce->write(1); // set CE high to end frame
el14jz 0:411f355688a5 310
el14jz 0:411f355688a5 311 }
el14jz 0:411f355688a5 312
el14jz 0:411f355688a5 313 // fills the buffer with random bytes. Can be used to test the display.
el14jz 0:411f355688a5 314 // The rand() function isn't seeded so it probably creates the same pattern everytime
el14jz 0:411f355688a5 315 void N5110::randomiseBuffer()
el14jz 0:411f355688a5 316 {
el14jz 0:411f355688a5 317 int i,j;
el14jz 0:411f355688a5 318 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
el14jz 0:411f355688a5 319 for(i = 0; i < WIDTH; i++) {
el14jz 0:411f355688a5 320 buffer[i][j] = rand()%256; // generate random byte
el14jz 0:411f355688a5 321 }
el14jz 0:411f355688a5 322 }
el14jz 0:411f355688a5 323
el14jz 0:411f355688a5 324 }
el14jz 0:411f355688a5 325
el14jz 0:411f355688a5 326 // function to print 5x7 font
el14jz 0:411f355688a5 327 void N5110::printChar(char c,int x,int y)
el14jz 0:411f355688a5 328 {
el14jz 0:411f355688a5 329 if (y>=0 && y<BANKS) { // check if printing in range of y banks
el14jz 0:411f355688a5 330
el14jz 0:411f355688a5 331 for (int i = 0; i < 5 ; i++ ) {
el14jz 0:411f355688a5 332 int pixel_x = x+i;
el14jz 0:411f355688a5 333 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
el14jz 0:411f355688a5 334 break;
el14jz 0:411f355688a5 335 buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
el14jz 0:411f355688a5 336 // array is offset by 32 relative to ASCII, each character is 5 pixels wide
el14jz 0:411f355688a5 337 }
el14jz 0:411f355688a5 338
el14jz 0:411f355688a5 339 refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0
el14jz 0:411f355688a5 340 }
el14jz 0:411f355688a5 341 }
el14jz 0:411f355688a5 342
el14jz 0:411f355688a5 343 // function to print string at specified position
el14jz 0:411f355688a5 344 void N5110::printString(const char * str,int x,int y)
el14jz 0:411f355688a5 345 {
el14jz 0:411f355688a5 346 if (y>=0 && y<BANKS) { // check if printing in range of y banks
el14jz 0:411f355688a5 347
el14jz 0:411f355688a5 348 int n = 0 ; // counter for number of characters in string
el14jz 0:411f355688a5 349 // loop through string and print character
el14jz 0:411f355688a5 350 while(*str) {
el14jz 0:411f355688a5 351
el14jz 0:411f355688a5 352 // writes the character bitmap data to the buffer, so that
el14jz 0:411f355688a5 353 // text and pixels can be displayed at the same time
el14jz 0:411f355688a5 354 for (int i = 0; i < 5 ; i++ ) {
el14jz 0:411f355688a5 355 int pixel_x = x+i+n*6;
el14jz 0:411f355688a5 356 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
el14jz 0:411f355688a5 357 break;
el14jz 0:411f355688a5 358 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
el14jz 0:411f355688a5 359 }
el14jz 0:411f355688a5 360
el14jz 0:411f355688a5 361 str++; // go to next character in string
el14jz 0:411f355688a5 362
el14jz 0:411f355688a5 363 n++; // increment index
el14jz 0:411f355688a5 364
el14jz 0:411f355688a5 365 }
el14jz 0:411f355688a5 366
el14jz 0:411f355688a5 367 refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0
el14jz 0:411f355688a5 368 }
el14jz 0:411f355688a5 369 }
el14jz 0:411f355688a5 370
el14jz 0:411f355688a5 371 // function to clear the screen
el14jz 0:411f355688a5 372 void N5110::clear()
el14jz 0:411f355688a5 373 {
el14jz 0:411f355688a5 374 clearBuffer(); // clear the buffer then call the refresh function
el14jz 0:411f355688a5 375 refresh();
el14jz 0:411f355688a5 376 }
el14jz 0:411f355688a5 377
el14jz 0:411f355688a5 378 // function to clear the buffer
el14jz 0:411f355688a5 379 void N5110::clearBuffer()
el14jz 0:411f355688a5 380 {
el14jz 0:411f355688a5 381 int i,j;
el14jz 0:411f355688a5 382 for (i=0; i<WIDTH; i++) { // loop through the banks and set the buffer to 0
el14jz 0:411f355688a5 383 for (j=0; j<BANKS; j++) {
el14jz 0:411f355688a5 384 buffer[i][j]=0;
el14jz 0:411f355688a5 385 }
el14jz 0:411f355688a5 386 }
el14jz 0:411f355688a5 387 }
el14jz 0:411f355688a5 388
el14jz 0:411f355688a5 389 // function to plot array on display
el14jz 0:411f355688a5 390 void N5110::plotArray(float array[])
el14jz 0:411f355688a5 391 {
el14jz 0:411f355688a5 392
el14jz 0:411f355688a5 393 int i;
el14jz 0:411f355688a5 394
el14jz 0:411f355688a5 395 for (i=0; i<WIDTH; i++) { // loop through array
el14jz 0:411f355688a5 396 // elements are normalised from 0.0 to 1.0, so multiply
el14jz 0:411f355688a5 397 // by 47 to convert to pixel range, and subtract from 47
el14jz 0:411f355688a5 398 // since top-left is 0,0 in the display geometry
el14jz 0:411f355688a5 399 setPixel(i,47 - int(array[i]*47.0));
el14jz 0:411f355688a5 400 }
el14jz 0:411f355688a5 401
el14jz 0:411f355688a5 402 refresh();
el14jz 0:411f355688a5 403
el14jz 0:411f355688a5 404 }
el14jz 0:411f355688a5 405
el14jz 0:411f355688a5 406 // function to draw circle
el14jz 0:411f355688a5 407 void N5110:: drawCircle(int x0,int y0,int radius,int fill)
el14jz 0:411f355688a5 408 {
el14jz 0:411f355688a5 409 // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
el14jz 0:411f355688a5 410 int x = radius;
el14jz 0:411f355688a5 411 int y = 0;
el14jz 0:411f355688a5 412 int radiusError = 1-x;
el14jz 0:411f355688a5 413
el14jz 0:411f355688a5 414 while(x >= y) {
el14jz 0:411f355688a5 415
el14jz 0:411f355688a5 416 // if transparent, just draw outline
el14jz 0:411f355688a5 417 if (fill == 0) {
el14jz 0:411f355688a5 418 setPixel( x + x0, y + y0);
el14jz 0:411f355688a5 419 setPixel(-x + x0, y + y0);
el14jz 0:411f355688a5 420 setPixel( y + x0, x + y0);
el14jz 0:411f355688a5 421 setPixel(-y + x0, x + y0);
el14jz 0:411f355688a5 422 setPixel(-y + x0, -x + y0);
el14jz 0:411f355688a5 423 setPixel( y + x0, -x + y0);
el14jz 0:411f355688a5 424 setPixel( x + x0, -y + y0);
el14jz 0:411f355688a5 425 setPixel(-x + x0, -y + y0);
el14jz 0:411f355688a5 426 } else { // drawing filled circle, so draw lines between points at same y value
el14jz 0:411f355688a5 427
el14jz 0:411f355688a5 428 int type = (fill==1) ? 1:0; // black or white fill
el14jz 0:411f355688a5 429
el14jz 0:411f355688a5 430 drawLine(x+x0,y+y0,-x+x0,y+y0,type);
el14jz 0:411f355688a5 431 drawLine(y+x0,x+y0,-y+x0,x+y0,type);
el14jz 0:411f355688a5 432 drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
el14jz 0:411f355688a5 433 drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
el14jz 0:411f355688a5 434 }
el14jz 0:411f355688a5 435
el14jz 0:411f355688a5 436
el14jz 0:411f355688a5 437 y++;
el14jz 0:411f355688a5 438 if (radiusError<0) {
el14jz 0:411f355688a5 439 radiusError += 2 * y + 1;
el14jz 0:411f355688a5 440 } else {
el14jz 0:411f355688a5 441 x--;
el14jz 0:411f355688a5 442 radiusError += 2 * (y - x) + 1;
el14jz 0:411f355688a5 443 }
el14jz 0:411f355688a5 444 }
el14jz 0:411f355688a5 445
el14jz 0:411f355688a5 446 }
el14jz 0:411f355688a5 447
el14jz 0:411f355688a5 448 void N5110::drawLine(int x0,int y0,int x1,int y1,int type)
el14jz 0:411f355688a5 449 {
el14jz 0:411f355688a5 450 int y_range = y1-y0; // calc range of y and x
el14jz 0:411f355688a5 451 int x_range = x1-x0;
el14jz 0:411f355688a5 452 int start,stop,step;
el14jz 0:411f355688a5 453
el14jz 0:411f355688a5 454 // if dotted line, set step to 2, else step is 1
el14jz 0:411f355688a5 455 step = (type==2) ? 2:1;
el14jz 0:411f355688a5 456
el14jz 0:411f355688a5 457 // make sure we loop over the largest range to get the most pixels on the display
el14jz 0:411f355688a5 458 // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
el14jz 0:411f355688a5 459 // or else we'll only end up with 1 pixel in the x column
el14jz 0:411f355688a5 460 if ( abs(x_range) > abs(y_range) ) {
el14jz 0:411f355688a5 461
el14jz 0:411f355688a5 462 // ensure we loop from smallest to largest or else for-loop won't run as expected
el14jz 0:411f355688a5 463 start = x1>x0 ? x0:x1;
el14jz 0:411f355688a5 464 stop = x1>x0 ? x1:x0;
el14jz 0:411f355688a5 465
el14jz 0:411f355688a5 466 // loop between x pixels
el14jz 0:411f355688a5 467 for (int x = start; x<= stop ; x+=step) {
el14jz 0:411f355688a5 468 // do linear interpolation
el14jz 0:411f355688a5 469 int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
el14jz 0:411f355688a5 470
el14jz 0:411f355688a5 471 if (type == 0) // if 'white' line, turn off pixel
el14jz 0:411f355688a5 472 clearPixel(x,y);
el14jz 0:411f355688a5 473 else
el14jz 0:411f355688a5 474 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el14jz 0:411f355688a5 475 }
el14jz 0:411f355688a5 476 } else {
el14jz 0:411f355688a5 477
el14jz 0:411f355688a5 478 // ensure we loop from smallest to largest or else for-loop won't run as expected
el14jz 0:411f355688a5 479 start = y1>y0 ? y0:y1;
el14jz 0:411f355688a5 480 stop = y1>y0 ? y1:y0;
el14jz 0:411f355688a5 481
el14jz 0:411f355688a5 482 for (int y = start; y<= stop ; y+=step) {
el14jz 0:411f355688a5 483 // do linear interpolation
el14jz 0:411f355688a5 484 int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
el14jz 0:411f355688a5 485
el14jz 0:411f355688a5 486 if (type == 0) // if 'white' line, turn off pixel
el14jz 0:411f355688a5 487 clearPixel(x,y);
el14jz 0:411f355688a5 488 else
el14jz 0:411f355688a5 489 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el14jz 0:411f355688a5 490
el14jz 0:411f355688a5 491 }
el14jz 0:411f355688a5 492 }
el14jz 0:411f355688a5 493
el14jz 0:411f355688a5 494 }
el14jz 0:411f355688a5 495
el14jz 0:411f355688a5 496 void N5110::drawRect(int x0,int y0,int width,int height,int fill)
el14jz 0:411f355688a5 497 {
el14jz 0:411f355688a5 498
el14jz 0:411f355688a5 499 if (fill == 0) { // transparent, just outline
el14jz 0:411f355688a5 500 drawLine(x0,y0,x0+width,y0,1); // top
el14jz 0:411f355688a5 501 drawLine(x0,y0+height,x0+width,y0+height,1); // bottom
el14jz 0:411f355688a5 502 drawLine(x0,y0,x0,y0+height,1); // left
el14jz 0:411f355688a5 503 drawLine(x0+width,y0,x0+width,y0+height,1); // right
el14jz 0:411f355688a5 504 } else { // filled rectangle
el14jz 0:411f355688a5 505 int type = (fill==1) ? 1:0; // black or white fill
el14jz 0:411f355688a5 506 for (int y = y0; y<= y0+height; y++) { // loop through rows of rectangle
el14jz 0:411f355688a5 507 drawLine(x0,y,x0+width,y,type); // draw line across screen
el14jz 0:411f355688a5 508 }
el14jz 0:411f355688a5 509 }
el14jz 0:411f355688a5 510
el14jz 0:411f355688a5 511 }
el14jz 0:411f355688a5 512
el14jz 0:411f355688a5 513 N5110 lcd(p7,p8,p9,p10,p11,p13,p26); // Initialization of Nokia5110 LCD screen
el14jz 0:411f355688a5 514
el14jz 0:411f355688a5 515 void flip() // flip 4 LEDs on mbed and 1 peripheral LED
el14jz 0:411f355688a5 516 {
el14jz 0:411f355688a5 517 leds=15;
el14jz 0:411f355688a5 518 myled=1;
el14jz 0:411f355688a5 519 wait(0.5);
el14jz 0:411f355688a5 520 leds=0;
el14jz 0:411f355688a5 521 myled=0;
el14jz 0:411f355688a5 522 }
el14jz 0:411f355688a5 523 void buttonApressed() // when A button is pressed, turn off the backlight of the screen
el14jz 0:411f355688a5 524 {
el14jz 0:411f355688a5 525 lcd.setBrightness(0.0);
el14jz 0:411f355688a5 526 }
el14jz 0:411f355688a5 527 void buttonBpressed() // when B button is pressed, turn on the backlight of the screen
el14jz 0:411f355688a5 528 {
el14jz 0:411f355688a5 529 lcd.setBrightness(1.0);
el14jz 0:411f355688a5 530 }
el14jz 0:411f355688a5 531 void changeBrightness() // a function to include two buttons to make main function simpler and this can also increase the efficiency
el14jz 0:411f355688a5 532 {
el14jz 0:411f355688a5 533 buttonA.rise(&buttonApressed);
el14jz 0:411f355688a5 534 buttonB.rise(&buttonBpressed);
el14jz 0:411f355688a5 535 }
el14jz 0:411f355688a5 536 void gameOver() // when the car hits the roadblock, clear the screen and display game over and beep/flash to tell the user obviously
el14jz 0:411f355688a5 537 {
el14jz 0:411f355688a5 538 lcd.clear();
el14jz 0:411f355688a5 539 lcd.printString("Game Over!",0,0);
el14jz 0:411f355688a5 540 buzzer.beep(5000,0.1);
el14jz 0:411f355688a5 541 wait(0.1);
el14jz 0:411f355688a5 542 buzzer.beep(4000,0.1);
el14jz 0:411f355688a5 543 wait(0.1);
el14jz 0:411f355688a5 544 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 545 wait(0.1);
el14jz 0:411f355688a5 546 buzzer.beep(2000,0.1);
el14jz 0:411f355688a5 547 wait(0.1);
el14jz 0:411f355688a5 548 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 549 for(int i = 1;i<4;i++)
el14jz 0:411f355688a5 550 {
el14jz 0:411f355688a5 551 flip();
el14jz 0:411f355688a5 552 wait(0.5);
el14jz 0:411f355688a5 553 }
el14jz 0:411f355688a5 554 lcd.refresh();
el14jz 0:411f355688a5 555 }
el14jz 0:411f355688a5 556
el14jz 0:411f355688a5 557 void welcome() // when begin the game, display the name and beep to welcome the user
el14jz 0:411f355688a5 558 {
el14jz 0:411f355688a5 559 lcd.printString("Fast&Furious-8",0,0);
el14jz 0:411f355688a5 560 lcd.refresh();
el14jz 0:411f355688a5 561 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 562 wait(0.5);
el14jz 0:411f355688a5 563 buzzer.beep(2000,0.1);
el14jz 0:411f355688a5 564 wait(0.5);
el14jz 0:411f355688a5 565 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 566 wait(0.5);
el14jz 0:411f355688a5 567 buzzer.beep(30000,0.1);
el14jz 0:411f355688a5 568 wait(0.2);
el14jz 0:411f355688a5 569 buzzer.beep(30000,0.1);
el14jz 0:411f355688a5 570 wait(0.2);
el14jz 0:411f355688a5 571 buzzer.beep(30000,0.1);
el14jz 0:411f355688a5 572 wait(2);
el14jz 0:411f355688a5 573 lcd.clear();
el14jz 0:411f355688a5 574 }
el14jz 0:411f355688a5 575
el14jz 0:411f355688a5 576 void drawBorder() // 3 lines to distinguish 4 roadways
el14jz 0:411f355688a5 577 {
el14jz 0:411f355688a5 578 lcd.drawLine(0,12,83,12,1);
el14jz 0:411f355688a5 579 lcd.drawLine(0,24,83,24,1);
el14jz 0:411f355688a5 580 lcd.drawLine(0,36,83,36,1);
el14jz 0:411f355688a5 581 lcd.refresh();
el14jz 0:411f355688a5 582 }
el14jz 0:411f355688a5 583
el14jz 0:411f355688a5 584 void car_L1() // when the car is in the first roadway
el14jz 0:411f355688a5 585 {
el14jz 0:411f355688a5 586 lcd.clear();
el14jz 0:411f355688a5 587 drawBorder();
el14jz 0:411f355688a5 588 lcd.drawRect(0,4,15,3,1);
el14jz 0:411f355688a5 589 lcd.refresh();
el14jz 0:411f355688a5 590 }
el14jz 0:411f355688a5 591
el14jz 0:411f355688a5 592 void car_L2() // when the car is in the second roadway
el14jz 0:411f355688a5 593 {
el14jz 0:411f355688a5 594 lcd.clear();
el14jz 0:411f355688a5 595 drawBorder();
el14jz 0:411f355688a5 596 lcd.drawRect(0,17,15,3,1);
el14jz 0:411f355688a5 597 lcd.refresh();
el14jz 0:411f355688a5 598 }
el14jz 0:411f355688a5 599
el14jz 0:411f355688a5 600 void car_R1() // when the car is in the third roadway
el14jz 0:411f355688a5 601 {
el14jz 0:411f355688a5 602 lcd.clear();
el14jz 0:411f355688a5 603 drawBorder();
el14jz 0:411f355688a5 604 lcd.drawRect(0,28,15,3,1);
el14jz 0:411f355688a5 605 lcd.refresh();
el14jz 0:411f355688a5 606 }
el14jz 0:411f355688a5 607
el14jz 0:411f355688a5 608 void car_R2() // when the car is in the fourth roadway
el14jz 0:411f355688a5 609 {
el14jz 0:411f355688a5 610 lcd.clear();
el14jz 0:411f355688a5 611 drawBorder();
el14jz 0:411f355688a5 612 lcd.drawRect(0,40,15,3,1);
el14jz 0:411f355688a5 613 lcd.refresh();
el14jz 0:411f355688a5 614 }
el14jz 0:411f355688a5 615
el14jz 0:411f355688a5 616 void moveCar() // move the car between roadways by using the joystick, and when the car is moving, buzzer will beep
el14jz 0:411f355688a5 617 {
el14jz 0:411f355688a5 618 if(joystick.direction == LEFT) // move to roadway L1
el14jz 0:411f355688a5 619 {
el14jz 0:411f355688a5 620 car_L1();
el14jz 0:411f355688a5 621 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 622 wait(0.2);
el14jz 0:411f355688a5 623 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 624 }
el14jz 0:411f355688a5 625 if(joystick.direction == UP) // move to roadway L2
el14jz 0:411f355688a5 626 {
el14jz 0:411f355688a5 627 car_L2();
el14jz 0:411f355688a5 628 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 629 wait(0.2);
el14jz 0:411f355688a5 630 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 631 }
el14jz 0:411f355688a5 632 if(joystick.direction == DOWN) // move to roadway R1
el14jz 0:411f355688a5 633 {
el14jz 0:411f355688a5 634 car_R1();
el14jz 0:411f355688a5 635 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 636 wait(0.2);
el14jz 0:411f355688a5 637 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 638 }
el14jz 0:411f355688a5 639 if(joystick.direction == RIGHT) // move to roadway R2
el14jz 0:411f355688a5 640 {
el14jz 0:411f355688a5 641 car_R2();
el14jz 0:411f355688a5 642 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 643 wait(0.2);
el14jz 0:411f355688a5 644 buzzer.beep(3000,0.1);
el14jz 0:411f355688a5 645 }
el14jz 0:411f355688a5 646 }
el14jz 0:411f355688a5 647
el14jz 0:411f355688a5 648 //Basic theory for moving is to clear the original pixels and create new.
el14jz 0:411f355688a5 649 void roadBlock_L1(int x) // creat the roadblock on roadway L1
el14jz 0:411f355688a5 650 {
el14jz 0:411f355688a5 651 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 652 lcd.drawRect(x,0,1,12,1);
el14jz 0:411f355688a5 653 for(int b=0;b<12;b++)
el14jz 0:411f355688a5 654 {
el14jz 0:411f355688a5 655 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 656 }
el14jz 0:411f355688a5 657 for(int c=0;c<12;c++)
el14jz 0:411f355688a5 658 {
el14jz 0:411f355688a5 659 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 660 }
el14jz 0:411f355688a5 661 moveCar();
el14jz 0:411f355688a5 662 lcd.refresh();
el14jz 0:411f355688a5 663 }
el14jz 0:411f355688a5 664 void roadBlock_L2(int x) // creat the roadblock on roadway L2
el14jz 0:411f355688a5 665 {
el14jz 0:411f355688a5 666 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 667 lcd.drawRect(x,12,1,12,1);
el14jz 0:411f355688a5 668 for(int b=13;b<24;b++)
el14jz 0:411f355688a5 669 {
el14jz 0:411f355688a5 670 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 671 }
el14jz 0:411f355688a5 672 for(int c=13;c<24;c++)
el14jz 0:411f355688a5 673 {
el14jz 0:411f355688a5 674 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 675 }
el14jz 0:411f355688a5 676 moveCar();
el14jz 0:411f355688a5 677 lcd.refresh();
el14jz 0:411f355688a5 678 }
el14jz 0:411f355688a5 679 void roadBlock_R1(int x) // create the roadblock on roadway R1
el14jz 0:411f355688a5 680 {
el14jz 0:411f355688a5 681 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 682 lcd.drawRect(x,24,1,12,1);
el14jz 0:411f355688a5 683 for(int b=25;b<36;b++)
el14jz 0:411f355688a5 684 {
el14jz 0:411f355688a5 685 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 686 }
el14jz 0:411f355688a5 687 for(int c=25;c<36;c++)
el14jz 0:411f355688a5 688 {
el14jz 0:411f355688a5 689 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 690 }
el14jz 0:411f355688a5 691 moveCar();
el14jz 0:411f355688a5 692 lcd.refresh();
el14jz 0:411f355688a5 693 }
el14jz 0:411f355688a5 694 void roadBlock_R2(int x) // create the roadblock on roadway R2
el14jz 0:411f355688a5 695 {
el14jz 0:411f355688a5 696 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 697 lcd.drawRect(x,36,1,12,1);
el14jz 0:411f355688a5 698 for(int b=37;b<48;b++)
el14jz 0:411f355688a5 699 {
el14jz 0:411f355688a5 700 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 701 }
el14jz 0:411f355688a5 702 for(int c=37;c<48;c++)
el14jz 0:411f355688a5 703 {
el14jz 0:411f355688a5 704 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 705 }
el14jz 0:411f355688a5 706 moveCar();
el14jz 0:411f355688a5 707 lcd.refresh();
el14jz 0:411f355688a5 708 }
el14jz 0:411f355688a5 709 void roadBlock_L1L2(int x) // create the roadblock on roadway L1 and L2
el14jz 0:411f355688a5 710 {
el14jz 0:411f355688a5 711 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 712 lcd.drawRect(x,0,1,24,1);
el14jz 0:411f355688a5 713 for(int b=0;b<24;b++)
el14jz 0:411f355688a5 714 {
el14jz 0:411f355688a5 715 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 716 }
el14jz 0:411f355688a5 717 for(int c=0;c<24;c++)
el14jz 0:411f355688a5 718 {
el14jz 0:411f355688a5 719 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 720 }
el14jz 0:411f355688a5 721 moveCar();
el14jz 0:411f355688a5 722 lcd.refresh();
el14jz 0:411f355688a5 723 }
el14jz 0:411f355688a5 724 void roadBlock_L2R1(int x) // create the roadblock on roadway L2 and R1
el14jz 0:411f355688a5 725 {
el14jz 0:411f355688a5 726 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 727 lcd.drawRect(x,12,1,24,1);
el14jz 0:411f355688a5 728 for(int b=12;b<36;b++)
el14jz 0:411f355688a5 729 {
el14jz 0:411f355688a5 730 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 731 }
el14jz 0:411f355688a5 732 for(int c=12;c<36;c++)
el14jz 0:411f355688a5 733 {
el14jz 0:411f355688a5 734 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 735 }
el14jz 0:411f355688a5 736 moveCar();
el14jz 0:411f355688a5 737 lcd.refresh();
el14jz 0:411f355688a5 738 }
el14jz 0:411f355688a5 739 void roadBlock_R1R2(int x) //create the roadblock on roadway R1 and R2
el14jz 0:411f355688a5 740 {
el14jz 0:411f355688a5 741 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 742 lcd.drawRect(x,24,1,24,1);
el14jz 0:411f355688a5 743 for(int b=24;b<48;b++)
el14jz 0:411f355688a5 744 {
el14jz 0:411f355688a5 745 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 746 }
el14jz 0:411f355688a5 747 for(int c=24;c<48;c++)
el14jz 0:411f355688a5 748 {
el14jz 0:411f355688a5 749 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 750 }
el14jz 0:411f355688a5 751 moveCar();
el14jz 0:411f355688a5 752 lcd.refresh();
el14jz 0:411f355688a5 753 }
el14jz 0:411f355688a5 754 void roadBlock_L1L2R1(int x) // create the roadblock on roadway L1, L2 and R1
el14jz 0:411f355688a5 755 {
el14jz 0:411f355688a5 756 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 757 lcd.drawRect(x,0,1,36,1);
el14jz 0:411f355688a5 758 for(int b=0;b<36;b++)
el14jz 0:411f355688a5 759 {
el14jz 0:411f355688a5 760 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 761 }
el14jz 0:411f355688a5 762 for(int c=0;c<36;c++)
el14jz 0:411f355688a5 763 {
el14jz 0:411f355688a5 764 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 765 }
el14jz 0:411f355688a5 766 moveCar();
el14jz 0:411f355688a5 767 lcd.refresh();
el14jz 0:411f355688a5 768 }
el14jz 0:411f355688a5 769 void roadBlock_L2R1R2(int x) // create the roadblock on roadway L2, R1 and R2
el14jz 0:411f355688a5 770 {
el14jz 0:411f355688a5 771 drawBorder(); // draw the border to avoid if the lines are cleared by unexpected refresh/clear function call.
el14jz 0:411f355688a5 772 lcd.drawRect(x,12,1,36,1);
el14jz 0:411f355688a5 773 for(int b=12;b<48;b++)
el14jz 0:411f355688a5 774 {
el14jz 0:411f355688a5 775 lcd.clearPixel(x+1,b);
el14jz 0:411f355688a5 776 }
el14jz 0:411f355688a5 777 for(int c=12;c<48;c++)
el14jz 0:411f355688a5 778 {
el14jz 0:411f355688a5 779 lcd.clearPixel(0,c);
el14jz 0:411f355688a5 780 }
el14jz 0:411f355688a5 781 moveCar();
el14jz 0:411f355688a5 782 lcd.refresh();
el14jz 0:411f355688a5 783 }
el14jz 0:411f355688a5 784 // move theory is to clear the original pixels and create new one.
el14jz 0:411f355688a5 785 void moveRoadBlock_L1() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 786 {
el14jz 0:411f355688a5 787 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 788 {
el14jz 0:411f355688a5 789 roadBlock_L1(a);
el14jz 0:411f355688a5 790 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 791 wait(0.2);
el14jz 0:411f355688a5 792 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 793 wait(0.01);
el14jz 0:411f355688a5 794 if(joystick.direction == LEFT and a<18)
el14jz 0:411f355688a5 795 {
el14jz 0:411f355688a5 796 gameOver();
el14jz 0:411f355688a5 797 a=-1;
el14jz 0:411f355688a5 798 while(1)
el14jz 0:411f355688a5 799 {
el14jz 0:411f355688a5 800 flip();
el14jz 0:411f355688a5 801 }
el14jz 0:411f355688a5 802 }
el14jz 0:411f355688a5 803 }
el14jz 0:411f355688a5 804 wait(0.1);
el14jz 0:411f355688a5 805 }
el14jz 0:411f355688a5 806 void moveRoadBlock_L2() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 807 {
el14jz 0:411f355688a5 808 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 809 {
el14jz 0:411f355688a5 810 roadBlock_L2(a);
el14jz 0:411f355688a5 811 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 812 wait(0.2);
el14jz 0:411f355688a5 813 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 814 wait(0.01);
el14jz 0:411f355688a5 815 if(joystick.direction == UP and a<18)
el14jz 0:411f355688a5 816 {
el14jz 0:411f355688a5 817 gameOver();
el14jz 0:411f355688a5 818 a=-1;
el14jz 0:411f355688a5 819 while(1)
el14jz 0:411f355688a5 820 {
el14jz 0:411f355688a5 821 flip();
el14jz 0:411f355688a5 822 }
el14jz 0:411f355688a5 823 }
el14jz 0:411f355688a5 824 }
el14jz 0:411f355688a5 825 wait(0.1);
el14jz 0:411f355688a5 826 }
el14jz 0:411f355688a5 827 void moveRoadBlock_R1() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 828 {
el14jz 0:411f355688a5 829 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 830 {
el14jz 0:411f355688a5 831 roadBlock_R1(a);
el14jz 0:411f355688a5 832 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 833 wait(0.2);
el14jz 0:411f355688a5 834 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 835 wait(0.01);
el14jz 0:411f355688a5 836 if(joystick.direction == DOWN and a<18)
el14jz 0:411f355688a5 837 {
el14jz 0:411f355688a5 838 gameOver();
el14jz 0:411f355688a5 839 a=-1;
el14jz 0:411f355688a5 840 while(1)
el14jz 0:411f355688a5 841 {
el14jz 0:411f355688a5 842 flip();
el14jz 0:411f355688a5 843 }
el14jz 0:411f355688a5 844 }
el14jz 0:411f355688a5 845 }
el14jz 0:411f355688a5 846 wait(0.1);
el14jz 0:411f355688a5 847 }
el14jz 0:411f355688a5 848 void moveRoadBlock_R2() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 849 {
el14jz 0:411f355688a5 850 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 851 {
el14jz 0:411f355688a5 852 roadBlock_R2(a);
el14jz 0:411f355688a5 853 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 854 wait(0.2);
el14jz 0:411f355688a5 855 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 856 wait(0.01);
el14jz 0:411f355688a5 857 if(joystick.direction == RIGHT and a<18)
el14jz 0:411f355688a5 858 {
el14jz 0:411f355688a5 859 gameOver();
el14jz 0:411f355688a5 860 a=-1;
el14jz 0:411f355688a5 861 while(1)
el14jz 0:411f355688a5 862 {
el14jz 0:411f355688a5 863 flip();
el14jz 0:411f355688a5 864 }
el14jz 0:411f355688a5 865 }
el14jz 0:411f355688a5 866 }
el14jz 0:411f355688a5 867 wait(0.1);
el14jz 0:411f355688a5 868 }
el14jz 0:411f355688a5 869 void moveRoadBlock_L1L2() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 870 {
el14jz 0:411f355688a5 871 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 872 {
el14jz 0:411f355688a5 873 roadBlock_L1L2(a);
el14jz 0:411f355688a5 874 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 875 wait(0.2);
el14jz 0:411f355688a5 876 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 877 wait(0.01);
el14jz 0:411f355688a5 878 if((joystick.direction == LEFT or joystick.direction == UP) and a<18)
el14jz 0:411f355688a5 879 {
el14jz 0:411f355688a5 880 gameOver();
el14jz 0:411f355688a5 881 a=-1;
el14jz 0:411f355688a5 882 while(1)
el14jz 0:411f355688a5 883 {
el14jz 0:411f355688a5 884 flip();
el14jz 0:411f355688a5 885 }
el14jz 0:411f355688a5 886 }
el14jz 0:411f355688a5 887 }
el14jz 0:411f355688a5 888 wait(0.1);
el14jz 0:411f355688a5 889 }
el14jz 0:411f355688a5 890 void moveRoadBlock_L2R1() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 891 {
el14jz 0:411f355688a5 892 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 893 {
el14jz 0:411f355688a5 894 roadBlock_L2R1(a);
el14jz 0:411f355688a5 895 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 896 wait(0.2);
el14jz 0:411f355688a5 897 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 898 wait(0.01);
el14jz 0:411f355688a5 899 if((joystick.direction == UP or joystick.direction == DOWN) and a<18)
el14jz 0:411f355688a5 900 {
el14jz 0:411f355688a5 901 gameOver();
el14jz 0:411f355688a5 902 a=-1;
el14jz 0:411f355688a5 903 while(1)
el14jz 0:411f355688a5 904 {
el14jz 0:411f355688a5 905 flip();
el14jz 0:411f355688a5 906 }
el14jz 0:411f355688a5 907 }
el14jz 0:411f355688a5 908 }
el14jz 0:411f355688a5 909 wait(0.1);
el14jz 0:411f355688a5 910 }
el14jz 0:411f355688a5 911 void moveRoadBlock_R1R2() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 912 {
el14jz 0:411f355688a5 913 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 914 {
el14jz 0:411f355688a5 915 roadBlock_R1R2(a);
el14jz 0:411f355688a5 916 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 917 wait(0.2);
el14jz 0:411f355688a5 918 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 919 wait(0.01);
el14jz 0:411f355688a5 920 if((joystick.direction == DOWN or joystick.direction == RIGHT) and a<18)
el14jz 0:411f355688a5 921 {
el14jz 0:411f355688a5 922 gameOver();
el14jz 0:411f355688a5 923 a=-1;
el14jz 0:411f355688a5 924 while(1)
el14jz 0:411f355688a5 925 {
el14jz 0:411f355688a5 926 flip();
el14jz 0:411f355688a5 927 }
el14jz 0:411f355688a5 928 }
el14jz 0:411f355688a5 929 }
el14jz 0:411f355688a5 930 wait(0.1);
el14jz 0:411f355688a5 931 }
el14jz 0:411f355688a5 932 void moveRoadBlock_L1L2R1() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 933 {
el14jz 0:411f355688a5 934 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 935 {
el14jz 0:411f355688a5 936 roadBlock_L1L2R1(a);
el14jz 0:411f355688a5 937 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 938 wait(0.2);
el14jz 0:411f355688a5 939 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 940 wait(0.01);
el14jz 0:411f355688a5 941 if((joystick.direction == LEFT or joystick.direction == UP or joystick.direction == DOWN) and a<18)
el14jz 0:411f355688a5 942 {
el14jz 0:411f355688a5 943 gameOver();
el14jz 0:411f355688a5 944 a=-1;
el14jz 0:411f355688a5 945 while(1)
el14jz 0:411f355688a5 946 {
el14jz 0:411f355688a5 947 flip();
el14jz 0:411f355688a5 948 }
el14jz 0:411f355688a5 949 }
el14jz 0:411f355688a5 950 }
el14jz 0:411f355688a5 951 wait(0.1);
el14jz 0:411f355688a5 952 }
el14jz 0:411f355688a5 953 void moveRoadBlock_L2R1R2() // move roadblock down and call game over fuction if hitting occurs
el14jz 0:411f355688a5 954 {
el14jz 0:411f355688a5 955 for(int a=82;a>=0;a--)
el14jz 0:411f355688a5 956 {
el14jz 0:411f355688a5 957 roadBlock_L2R1R2(a);
el14jz 0:411f355688a5 958 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 959 wait(0.2);
el14jz 0:411f355688a5 960 buzzer.beep(1000,0.1);
el14jz 0:411f355688a5 961 wait(0.01);
el14jz 0:411f355688a5 962 if((joystick.direction == UP or joystick.direction == DOWN or joystick.direction == RIGHT) and a<18)
el14jz 0:411f355688a5 963 {
el14jz 0:411f355688a5 964 gameOver();
el14jz 0:411f355688a5 965 a=-1;
el14jz 0:411f355688a5 966 while(1)
el14jz 0:411f355688a5 967 {
el14jz 0:411f355688a5 968 flip();
el14jz 0:411f355688a5 969 }
el14jz 0:411f355688a5 970 }
el14jz 0:411f355688a5 971 }
el14jz 0:411f355688a5 972 wait(0.1);
el14jz 0:411f355688a5 973 }
el14jz 0:411f355688a5 974 void win() // when the car passes all of the roadblocks, the user wins the game, this function is used to display "win" interface
el14jz 0:411f355688a5 975 {
el14jz 0:411f355688a5 976 lcd.clear();
el14jz 0:411f355688a5 977 lcd.printString("YOU WIN!!!",0,0);
el14jz 0:411f355688a5 978 buzzer.beep(1000,0.2);
el14jz 0:411f355688a5 979 wait(0.1);
el14jz 0:411f355688a5 980 buzzer.beep(2000,0.2);
el14jz 0:411f355688a5 981 wait(0.1);
el14jz 0:411f355688a5 982 buzzer.beep(3000,0.2);
el14jz 0:411f355688a5 983 while(1)
el14jz 0:411f355688a5 984 {
el14jz 0:411f355688a5 985 flip();
el14jz 0:411f355688a5 986 }
el14jz 0:411f355688a5 987 }
el14jz 0:411f355688a5 988 void start() // to simplify the main function, this function is added which contains to call all above relative move functions.
el14jz 0:411f355688a5 989 {
el14jz 0:411f355688a5 990 moveRoadBlock_L1();
el14jz 0:411f355688a5 991 moveRoadBlock_L1L2R1();
el14jz 0:411f355688a5 992 moveRoadBlock_L2();
el14jz 0:411f355688a5 993 moveRoadBlock_L1L2();
el14jz 0:411f355688a5 994 moveRoadBlock_R1();
el14jz 0:411f355688a5 995 moveRoadBlock_R2();
el14jz 0:411f355688a5 996 moveRoadBlock_L2R1R2();
el14jz 0:411f355688a5 997 moveRoadBlock_R1R2();
el14jz 0:411f355688a5 998 win();
el14jz 0:411f355688a5 999 }
el14jz 0:411f355688a5 1000
el14jz 0:411f355688a5 1001
el14jz 0:411f355688a5 1002
el14jz 0:411f355688a5 1003 int main()
el14jz 0:411f355688a5 1004 {
el14jz 0:411f355688a5 1005 lcd.init(); // fist need to initilize the screen
el14jz 0:411f355688a5 1006
el14jz 0:411f355688a5 1007 welcome(); // dislpay welcome interface
el14jz 0:411f355688a5 1008
el14jz 0:411f355688a5 1009 calibrateJoystick(); // get centred values of joystick
el14jz 0:411f355688a5 1010 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
el14jz 0:411f355688a5 1011
el14jz 0:411f355688a5 1012 changeBrightness(); // used to read from buttons to turn on/off the backlight of the screen
el14jz 0:411f355688a5 1013
el14jz 0:411f355688a5 1014 start();
el14jz 0:411f355688a5 1015
el14jz 0:411f355688a5 1016 while(1)
el14jz 0:411f355688a5 1017 {
el14jz 0:411f355688a5 1018 }
el14jz 0:411f355688a5 1019 }