Junwei Zang / Mbed 2 deprecated Embedded_Systems_Project

Dependencies:   mbed

Committer:
el14jz
Date:
Thu May 07 14:46:11 2015 +0000
Revision:
1:ff57945c704c
Parent:
0:411f355688a5
Embedded Systems Project

Who changed what in which revision?

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