Simon Atkinson 201255483

Dependencies:   mbed

Snake Game

Summary

Hello and welcome to my Snake Game. Snake is a simple game where you control a snake and eat an apple which increases your size. Normally touching the walls will kill you, always touching yourself will make you die!

Controls

The Controls for this game are simple the Start button starts the game (who would have thought) this button is the left button on the bottom of the gamepad next to the two blue potentiometers. The Reset button on the right of the potentiometers resets the game. Once the game starts use the left thumbstick to control the movement of the snake.

If you hit the wall with your snake you will die and the game will end.

Bugs/Missing Features

Unfortunatley I wasn't able to get the game eating the apple to work, when I tried I could never get it to detect the collisions I tried a few different ways but ran out of time. Because that doesn't work the score doesn't increase and the apple doesn't spawn in a different place. I will try continue to work on this when I have time as I enjoyed doing this project even though it was very frustrating at times!

Committer:
Psy1990
Date:
Fri Jun 05 22:57:33 2020 +0000
Revision:
21:e8d66c5f68cc
Parent:
0:7423345f87c5
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:7423345f87c5 1 #ifndef N5110_H
eencae 0:7423345f87c5 2 #define N5110_H
eencae 0:7423345f87c5 3
eencae 0:7423345f87c5 4 #include "mbed.h"
eencae 0:7423345f87c5 5
eencae 0:7423345f87c5 6 // number of pixels on display
eencae 0:7423345f87c5 7 #define WIDTH 84
eencae 0:7423345f87c5 8 #define HEIGHT 48
eencae 0:7423345f87c5 9 #define BANKS 6
eencae 0:7423345f87c5 10
eencae 0:7423345f87c5 11 /// Fill types for 2D shapes
eencae 0:7423345f87c5 12 enum FillType {
eencae 0:7423345f87c5 13 FILL_TRANSPARENT, ///< Transparent with outline
eencae 0:7423345f87c5 14 FILL_BLACK, ///< Filled black
eencae 0:7423345f87c5 15 FILL_WHITE, ///< Filled white (no outline)
eencae 0:7423345f87c5 16 };
eencae 0:7423345f87c5 17
eencae 0:7423345f87c5 18 /** N5110 Class
eencae 0:7423345f87c5 19 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
eencae 0:7423345f87c5 20 @brief The display is powered from a GPIO pin meaning it can be controlled via software. The LED backlight is also software-controllable (via PWM pin).
eencae 0:7423345f87c5 21 @brief Can print characters and strings to the display using the included 5x7 font.
eencae 0:7423345f87c5 22 @brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read.
eencae 0:7423345f87c5 23 @brief The library can print primitive shapes (lines, circles, rectangles)
eencae 0:7423345f87c5 24 @brief Acknowledgements to Chris Yan's Nokia_5110 Library.
eencae 0:7423345f87c5 25
eencae 0:7423345f87c5 26 @brief Revision 1.3
eencae 0:7423345f87c5 27
eencae 0:7423345f87c5 28 @author Craig A. Evans
eencae 0:7423345f87c5 29 @date 7th February 2017
eencae 0:7423345f87c5 30
eencae 0:7423345f87c5 31 @code
eencae 0:7423345f87c5 32
eencae 0:7423345f87c5 33 #include "mbed.h"
eencae 0:7423345f87c5 34 #include "N5110.h"
eencae 0:7423345f87c5 35
eencae 0:7423345f87c5 36 // rows,cols
eencae 0:7423345f87c5 37 int sprite[8][5] = {
eencae 0:7423345f87c5 38 { 0,0,1,0,0 },
eencae 0:7423345f87c5 39 { 0,1,1,1,0 },
eencae 0:7423345f87c5 40 { 0,0,1,0,0 },
eencae 0:7423345f87c5 41 { 0,1,1,1,0 },
eencae 0:7423345f87c5 42 { 1,1,1,1,1 },
eencae 0:7423345f87c5 43 { 1,1,1,1,1 },
eencae 0:7423345f87c5 44 { 1,1,0,1,1 },
eencae 0:7423345f87c5 45 { 1,1,0,1,1 },
eencae 0:7423345f87c5 46 };
eencae 0:7423345f87c5 47
eencae 0:7423345f87c5 48 // VCC,SCE,RST,D/C,MOSI,SCLK,LED
eencae 0:7423345f87c5 49 //N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO
eencae 0:7423345f87c5 50 N5110 lcd(p8,p9,p10,p11,p13,p21); // LPC1768 - powered from +3V3 - JP1 in 2/3 position
eencae 0:7423345f87c5 51 //N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
eencae 0:7423345f87c5 52
eencae 0:7423345f87c5 53 int main()
eencae 0:7423345f87c5 54 {
eencae 0:7423345f87c5 55 // first need to initialise display
eencae 0:7423345f87c5 56 lcd.init();
eencae 0:7423345f87c5 57
eencae 0:7423345f87c5 58 // change set contrast in range 0.0 to 1.0
eencae 0:7423345f87c5 59 // 0.4 appears to be a good starting point
eencae 0:7423345f87c5 60 lcd.setContrast(0.4);
eencae 0:7423345f87c5 61
eencae 0:7423345f87c5 62 while(1) {
eencae 0:7423345f87c5 63
eencae 0:7423345f87c5 64 // these are default settings so not strictly needed
eencae 0:7423345f87c5 65 lcd.normalMode(); // normal colour mode
eencae 0:7423345f87c5 66
eencae 0:7423345f87c5 67 lcd.clear();
eencae 0:7423345f87c5 68 // x origin, y origin, rows, cols, sprite
eencae 0:7423345f87c5 69 lcd.drawSprite(20,6,8,5,(int *)sprite);
eencae 0:7423345f87c5 70 lcd.refresh();
eencae 0:7423345f87c5 71 wait(5.0);
eencae 0:7423345f87c5 72
eencae 0:7423345f87c5 73 lcd.clear(); // clear buffer at start of every loop
eencae 0:7423345f87c5 74 // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
eencae 0:7423345f87c5 75 lcd.printString("Hello, World!",0,0);
eencae 0:7423345f87c5 76
eencae 0:7423345f87c5 77 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
eencae 0:7423345f87c5 78 // so can display a string of a maximum 14 characters in length
eencae 0:7423345f87c5 79 // or create formatted strings - ensure they aren't more than 14 characters long
eencae 0:7423345f87c5 80 int temperature = 27;
eencae 0:7423345f87c5 81 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
eencae 0:7423345f87c5 82 // it is important the format specifier ensures the length will fit in the buffer
eencae 0:7423345f87c5 83 if (length <= 14) // if string will fit on display (assuming printing at x=0)
eencae 0:7423345f87c5 84 lcd.printString(buffer,0,1); // display on screen
eencae 0:7423345f87c5 85
eencae 0:7423345f87c5 86 float pressure = 1012.3; // same idea with floats
eencae 0:7423345f87c5 87 length = sprintf(buffer,"P = %.2f mb",pressure);
eencae 0:7423345f87c5 88 if (length <= 14)
eencae 0:7423345f87c5 89 lcd.printString(buffer,0,2);
eencae 0:7423345f87c5 90
eencae 0:7423345f87c5 91 // can also print individual characters at specified place
eencae 0:7423345f87c5 92 lcd.printChar('X',5,3);
eencae 0:7423345f87c5 93
eencae 0:7423345f87c5 94 // draw a line across the display at y = 40 pixels (origin top-left)
eencae 0:7423345f87c5 95 for (int i = 0; i < WIDTH; i++) {
eencae 0:7423345f87c5 96 lcd.setPixel(i,40,true);
eencae 0:7423345f87c5 97 }
eencae 0:7423345f87c5 98 // need to refresh display after setting pixels or writing strings
eencae 0:7423345f87c5 99 lcd.refresh();
eencae 0:7423345f87c5 100 wait(5.0);
eencae 0:7423345f87c5 101
eencae 0:7423345f87c5 102 // can check status of pixel using getPixel(x,y);
eencae 0:7423345f87c5 103 lcd.clear(); // clear buffer
eencae 0:7423345f87c5 104 lcd.setPixel(2,2,true); // set random pixel in buffer
eencae 0:7423345f87c5 105 lcd.refresh();
eencae 0:7423345f87c5 106 wait(1.0);
eencae 0:7423345f87c5 107
eencae 0:7423345f87c5 108 int pixel_to_test = lcd.getPixel(2,2);
eencae 0:7423345f87c5 109
eencae 0:7423345f87c5 110 if ( pixel_to_test ) {
eencae 0:7423345f87c5 111 lcd.printString("2,2 is set",0,4);
eencae 0:7423345f87c5 112 }
eencae 0:7423345f87c5 113
eencae 0:7423345f87c5 114 // this one shouldn't be set
eencae 0:7423345f87c5 115 lcd.setPixel(3,3,false); // clear random pixel in buffer
eencae 0:7423345f87c5 116 lcd.refresh();
eencae 0:7423345f87c5 117 pixel_to_test = lcd.getPixel(3,3);
eencae 0:7423345f87c5 118
eencae 0:7423345f87c5 119 if ( pixel_to_test == 0 ) {
eencae 0:7423345f87c5 120 lcd.printString("3,3 is clear",0,5);
eencae 0:7423345f87c5 121 }
eencae 0:7423345f87c5 122
eencae 0:7423345f87c5 123 lcd.refresh();
eencae 0:7423345f87c5 124 wait(4.0);
eencae 0:7423345f87c5 125
eencae 0:7423345f87c5 126 lcd.clear(); // clear buffer
eencae 0:7423345f87c5 127 lcd.inverseMode(); // invert colours
eencae 0:7423345f87c5 128 lcd.setBrightness(1.0); // put LED backlight on full
eencae 0:7423345f87c5 129
eencae 0:7423345f87c5 130 float array[84];
eencae 0:7423345f87c5 131
eencae 0:7423345f87c5 132 for (int i = 0; i < 84; i++) {
eencae 0:7423345f87c5 133 array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
eencae 0:7423345f87c5 134 }
eencae 0:7423345f87c5 135
eencae 0:7423345f87c5 136 // can also plot graphs - 84 elements only
eencae 0:7423345f87c5 137 // values must be in range 0.0 - 1.0
eencae 0:7423345f87c5 138 lcd.plotArray(array);
eencae 0:7423345f87c5 139 lcd.refresh();
eencae 0:7423345f87c5 140 wait(5.0);
eencae 0:7423345f87c5 141
eencae 0:7423345f87c5 142 lcd.clear();
eencae 0:7423345f87c5 143 lcd.normalMode(); // normal colour mode back
eencae 0:7423345f87c5 144 lcd.setBrightness(0.5); // put LED backlight on 50%
eencae 0:7423345f87c5 145
eencae 0:7423345f87c5 146 // example of drawing lines
eencae 0:7423345f87c5 147 for (int x = 0; x < WIDTH ; x+=10) {
eencae 0:7423345f87c5 148 // x0,y0,x1,y1,type 0-white,1-black,2-dotted
eencae 0:7423345f87c5 149 lcd.drawLine(0,0,x,HEIGHT,2);
eencae 0:7423345f87c5 150 }
eencae 0:7423345f87c5 151 lcd.refresh(); // refresh after drawing shapes
eencae 0:7423345f87c5 152 wait(5.0);
eencae 0:7423345f87c5 153
eencae 0:7423345f87c5 154
eencae 0:7423345f87c5 155 lcd.clear();
eencae 0:7423345f87c5 156 // example of how to draw circles
eencae 0:7423345f87c5 157 lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK); // x,y,radius,black fill
eencae 0:7423345f87c5 158 lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE); // x,y,radius,white fill
eencae 0:7423345f87c5 159 lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT); // x,y,radius,transparent with outline
eencae 0:7423345f87c5 160 lcd.refresh(); // refresh after drawing shapes
eencae 0:7423345f87c5 161 wait(5.0);
eencae 0:7423345f87c5 162
eencae 0:7423345f87c5 163 lcd.clear();
eencae 0:7423345f87c5 164 // example of how to draw rectangles
eencae 0:7423345f87c5 165 // origin x,y,width,height,type
eencae 0:7423345f87c5 166 lcd.drawRect(10,10,50,30,FILL_BLACK); // filled black rectangle
eencae 0:7423345f87c5 167 lcd.drawRect(15,15,20,10,FILL_WHITE); // filled white rectange (no outline)
eencae 0:7423345f87c5 168 lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); // transparent, just outline
eencae 0:7423345f87c5 169 lcd.refresh(); // refresh after drawing shapes
eencae 0:7423345f87c5 170 wait(5.0);
eencae 0:7423345f87c5 171
eencae 0:7423345f87c5 172 }
eencae 0:7423345f87c5 173 }
eencae 0:7423345f87c5 174
eencae 0:7423345f87c5 175
eencae 0:7423345f87c5 176 @endcode
eencae 0:7423345f87c5 177 */
eencae 0:7423345f87c5 178 class N5110
eencae 0:7423345f87c5 179 {
eencae 0:7423345f87c5 180 private:
eencae 0:7423345f87c5 181 // objects
eencae 0:7423345f87c5 182 SPI *_spi;
eencae 0:7423345f87c5 183 DigitalOut *_led;
eencae 0:7423345f87c5 184 DigitalOut *_pwr;
eencae 0:7423345f87c5 185 DigitalOut *_sce;
eencae 0:7423345f87c5 186 DigitalOut *_rst;
eencae 0:7423345f87c5 187 DigitalOut *_dc;
eencae 0:7423345f87c5 188
eencae 0:7423345f87c5 189 // variables
eencae 0:7423345f87c5 190 unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits;
eencae 0:7423345f87c5 191
eencae 0:7423345f87c5 192 public:
eencae 0:7423345f87c5 193 /** Create a N5110 object connected to the specified pins
eencae 0:7423345f87c5 194 *
eencae 0:7423345f87c5 195 * @param pwr Pin connected to Vcc on the LCD display (pin 1)
eencae 0:7423345f87c5 196 * @param sce Pin connected to chip enable (pin 3)
eencae 0:7423345f87c5 197 * @param rst Pin connected to reset (pin 4)
eencae 0:7423345f87c5 198 * @param dc Pin connected to data/command select (pin 5)
eencae 0:7423345f87c5 199 * @param mosi Pin connected to data input (MOSI) (pin 6)
eencae 0:7423345f87c5 200 * @param sclk Pin connected to serial clock (SCLK) (pin 7)
eencae 0:7423345f87c5 201 * @param led Pin connected to LED backlight (must be PWM) (pin 8)
eencae 0:7423345f87c5 202 *
eencae 0:7423345f87c5 203 */
eencae 0:7423345f87c5 204 N5110(PinName const pwrPin,
eencae 0:7423345f87c5 205 PinName const scePin,
eencae 0:7423345f87c5 206 PinName const rstPin,
eencae 0:7423345f87c5 207 PinName const dcPin,
eencae 0:7423345f87c5 208 PinName const mosiPin,
eencae 0:7423345f87c5 209 PinName const sclkPin,
eencae 0:7423345f87c5 210 PinName const ledPin);
eencae 0:7423345f87c5 211
eencae 0:7423345f87c5 212 /** Create a N5110 object connected to the specified pins (Vcc to +3V3)
eencae 0:7423345f87c5 213 *
eencae 0:7423345f87c5 214 * @param sce Pin connected to chip enable (pin 3)
eencae 0:7423345f87c5 215 * @param rst Pin connected to reset (pin 4)
eencae 0:7423345f87c5 216 * @param dc Pin connected to data/command select (pin 5)
eencae 0:7423345f87c5 217 * @param mosi Pin connected to data input (MOSI) (pin 6)
eencae 0:7423345f87c5 218 * @param sclk Pin connected to serial clock (SCLK) (pin 7)
eencae 0:7423345f87c5 219 * @param led Pin connected to LED backlight (must be PWM) (pin 8)
eencae 0:7423345f87c5 220 *
eencae 0:7423345f87c5 221 */
eencae 0:7423345f87c5 222 N5110(PinName const scePin,
eencae 0:7423345f87c5 223 PinName const rstPin,
eencae 0:7423345f87c5 224 PinName const dcPin,
eencae 0:7423345f87c5 225 PinName const mosiPin,
eencae 0:7423345f87c5 226 PinName const sclkPin,
eencae 0:7423345f87c5 227 PinName const ledPin);
eencae 0:7423345f87c5 228
eencae 0:7423345f87c5 229
eencae 0:7423345f87c5 230 /** Creates a N5110 object with the New Gamepad (Rev 2.1) pin mapping
eencae 0:7423345f87c5 231 */
eencae 0:7423345f87c5 232 N5110();
eencae 0:7423345f87c5 233
eencae 0:7423345f87c5 234 /**
eencae 0:7423345f87c5 235 * Free allocated memory when object goes out of scope
eencae 0:7423345f87c5 236 */
eencae 0:7423345f87c5 237 ~N5110();
eencae 0:7423345f87c5 238
eencae 0:7423345f87c5 239 /** Initialise display
eencae 0:7423345f87c5 240 *
eencae 0:7423345f87c5 241 * Powers up the display and turns on backlight (50% brightness default).
eencae 0:7423345f87c5 242 * Sets the display up in horizontal addressing mode and with normal video mode.
eencae 0:7423345f87c5 243 */
eencae 0:7423345f87c5 244 void init();
eencae 0:7423345f87c5 245
eencae 0:7423345f87c5 246 /** Turn off
eencae 0:7423345f87c5 247 *
eencae 0:7423345f87c5 248 * Powers down the display and turns of the backlight.
eencae 0:7423345f87c5 249 * Needs to be reinitialised before being re-used.
eencae 0:7423345f87c5 250 */
eencae 0:7423345f87c5 251 void turnOff();
eencae 0:7423345f87c5 252
eencae 0:7423345f87c5 253 /** Clear
eencae 0:7423345f87c5 254 *
eencae 0:7423345f87c5 255 * Clears the screen buffer.
eencae 0:7423345f87c5 256 */
eencae 0:7423345f87c5 257 void clear();
eencae 0:7423345f87c5 258
eencae 0:7423345f87c5 259 /** Set screen constrast
eencae 0:7423345f87c5 260 * @param constrast - float in range 0.0 to 1.0 (0.40 to 0.60 is usually a good value)
eencae 0:7423345f87c5 261 */
eencae 0:7423345f87c5 262 void setContrast(float contrast);
eencae 0:7423345f87c5 263
eencae 0:7423345f87c5 264 /** Turn on normal video mode (default)
eencae 0:7423345f87c5 265 * Black on white
eencae 0:7423345f87c5 266 */
eencae 0:7423345f87c5 267 void normalMode();
eencae 0:7423345f87c5 268
eencae 0:7423345f87c5 269 /** Turn on inverse video mode (default)
eencae 0:7423345f87c5 270 * White on black
eencae 0:7423345f87c5 271 */
eencae 0:7423345f87c5 272 void inverseMode();
eencae 0:7423345f87c5 273
eencae 0:7423345f87c5 274 /** Backlight On
eencae 0:7423345f87c5 275 *
eencae 0:7423345f87c5 276 * Turns backlight on
eencae 0:7423345f87c5 277 */
eencae 0:7423345f87c5 278 void backLightOn();
eencae 0:7423345f87c5 279
eencae 0:7423345f87c5 280 /** Set Brightness
eencae 0:7423345f87c5 281 *
eencae 0:7423345f87c5 282 * Turns backlight off
eencae 0:7423345f87c5 283 */
eencae 0:7423345f87c5 284 void backLightOff();
eencae 0:7423345f87c5 285
eencae 0:7423345f87c5 286 /** Print String
eencae 0:7423345f87c5 287 *
eencae 0:7423345f87c5 288 * Prints a string of characters to the screen buffer. String is cut-off after the 83rd pixel.
eencae 0:7423345f87c5 289 * @param x - the column number (0 to 83)
eencae 0:7423345f87c5 290 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
eencae 0:7423345f87c5 291 */
eencae 0:7423345f87c5 292 void printString(char const *str,
eencae 0:7423345f87c5 293 unsigned int const x,
eencae 0:7423345f87c5 294 unsigned int const y);
eencae 0:7423345f87c5 295
eencae 0:7423345f87c5 296 /** Print Character
eencae 0:7423345f87c5 297 *
eencae 0:7423345f87c5 298 * Sends a character to the screen buffer. Printed at the specified location. Character is cut-off after the 83rd pixel.
eencae 0:7423345f87c5 299 * @param c - the character to print. Can print ASCII as so printChar('C').
eencae 0:7423345f87c5 300 * @param x - the column number (0 to 83)
eencae 0:7423345f87c5 301 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
eencae 0:7423345f87c5 302 */
eencae 0:7423345f87c5 303 void printChar(char const c,
eencae 0:7423345f87c5 304 unsigned int const x,
eencae 0:7423345f87c5 305 unsigned int const y);
eencae 0:7423345f87c5 306
eencae 0:7423345f87c5 307 /**
eencae 0:7423345f87c5 308 * @brief Set a Pixel
eencae 0:7423345f87c5 309 *
eencae 0:7423345f87c5 310 * @param x The x co-ordinate of the pixel (0 to 83)
eencae 0:7423345f87c5 311 * @param y The y co-ordinate of the pixel (0 to 47)
eencae 0:7423345f87c5 312 * @param state The state of the pixel [true=black (default), false=white]
eencae 0:7423345f87c5 313 *
eencae 0:7423345f87c5 314 * @details This function sets the state of a pixel in the screen buffer.
eencae 0:7423345f87c5 315 * The third parameter can be omitted,
eencae 0:7423345f87c5 316 */
eencae 0:7423345f87c5 317 void setPixel(unsigned int const x,
eencae 0:7423345f87c5 318 unsigned int const y,
eencae 0:7423345f87c5 319 bool const state = true);
eencae 0:7423345f87c5 320
eencae 0:7423345f87c5 321 /**
eencae 0:7423345f87c5 322 * @brief Clear a Pixel
eencae 0:7423345f87c5 323 *
eencae 0:7423345f87c5 324 * @param x - the x co-ordinate of the pixel (0 to 83)
eencae 0:7423345f87c5 325 * @param y - the y co-ordinate of the pixel (0 to 47)
eencae 0:7423345f87c5 326 *
eencae 0:7423345f87c5 327 * @details This function clears pixel in the screen buffer
eencae 0:7423345f87c5 328 *
eencae 0:7423345f87c5 329 * @deprecated Use setPixel(x, y, false) instead
eencae 0:7423345f87c5 330 */
eencae 0:7423345f87c5 331 void clearPixel(unsigned int const x,
eencae 0:7423345f87c5 332 unsigned int const y)
eencae 0:7423345f87c5 333 __attribute__((deprecated("Use setPixel(x,y,false) instead")));
eencae 0:7423345f87c5 334
eencae 0:7423345f87c5 335 /** Get a Pixel
eencae 0:7423345f87c5 336 *
eencae 0:7423345f87c5 337 * This function gets the status of a pixel in the screen buffer.
eencae 0:7423345f87c5 338 * @param x - the x co-ordinate of the pixel (0 to 83)
eencae 0:7423345f87c5 339 * @param y - the y co-ordinate of the pixel (0 to 47)
eencae 0:7423345f87c5 340 * @returns
eencae 0:7423345f87c5 341 * 0 - pixel is clear
eencae 0:7423345f87c5 342 * 1 - pixel is set
eencae 0:7423345f87c5 343 */
eencae 0:7423345f87c5 344 int getPixel(unsigned int const x,
eencae 0:7423345f87c5 345 unsigned int const y) const;
eencae 0:7423345f87c5 346
eencae 0:7423345f87c5 347 /** Refresh display
eencae 0:7423345f87c5 348 *
eencae 0:7423345f87c5 349 * This functions sends the screen buffer to the display.
eencae 0:7423345f87c5 350 */
eencae 0:7423345f87c5 351 void refresh();
eencae 0:7423345f87c5 352
eencae 0:7423345f87c5 353 /** Randomise buffer
eencae 0:7423345f87c5 354 *
eencae 0:7423345f87c5 355 * This function fills the buffer with random data. Can be used to test the display.
eencae 0:7423345f87c5 356 * A call to refresh() must be made to update the display to reflect the change in pixels.
eencae 0:7423345f87c5 357 * The seed is not set and so the generated pattern will probably be the same each time.
eencae 0:7423345f87c5 358 * TODO: Randomise the seed - maybe using the noise on the AnalogIn pins.
eencae 0:7423345f87c5 359 */
eencae 0:7423345f87c5 360 void randomiseBuffer();
eencae 0:7423345f87c5 361
eencae 0:7423345f87c5 362 /** Plot Array
eencae 0:7423345f87c5 363 *
eencae 0:7423345f87c5 364 * This function plots a one-dimensional array in the buffer.
eencae 0:7423345f87c5 365 * @param array[] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted.
eencae 0:7423345f87c5 366 */
eencae 0:7423345f87c5 367 void plotArray(float const array[]);
eencae 0:7423345f87c5 368
eencae 0:7423345f87c5 369 /** Draw Circle
eencae 0:7423345f87c5 370 *
eencae 0:7423345f87c5 371 * This function draws a circle at the specified origin with specified radius in the screen buffer
eencae 0:7423345f87c5 372 * Uses the midpoint circle algorithm.
eencae 0:7423345f87c5 373 * @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
eencae 0:7423345f87c5 374 * @param x0 - x-coordinate of centre
eencae 0:7423345f87c5 375 * @param y0 - y-coordinate of centre
eencae 0:7423345f87c5 376 * @param radius - radius of circle in pixels
eencae 0:7423345f87c5 377 * @param fill - fill-type for the shape
eencae 0:7423345f87c5 378 */
eencae 0:7423345f87c5 379 void drawCircle(unsigned int const x0,
eencae 0:7423345f87c5 380 unsigned int const y0,
eencae 0:7423345f87c5 381 unsigned int const radius,
eencae 0:7423345f87c5 382 FillType const fill);
eencae 0:7423345f87c5 383
eencae 0:7423345f87c5 384 /** Draw Line
eencae 0:7423345f87c5 385 *
eencae 0:7423345f87c5 386 * This function draws a line between the specified points using linear interpolation.
eencae 0:7423345f87c5 387 * @param x0 - x-coordinate of first point
eencae 0:7423345f87c5 388 * @param y0 - y-coordinate of first point
eencae 0:7423345f87c5 389 * @param x1 - x-coordinate of last point
eencae 0:7423345f87c5 390 * @param y1 - y-coordinate of last point
eencae 0:7423345f87c5 391 * @param type - 0 white,1 black,2 dotted
eencae 0:7423345f87c5 392 */
eencae 0:7423345f87c5 393 void drawLine(unsigned int const x0,
eencae 0:7423345f87c5 394 unsigned int const y0,
eencae 0:7423345f87c5 395 unsigned int const x1,
eencae 0:7423345f87c5 396 unsigned int const y1,
eencae 0:7423345f87c5 397 unsigned int const type);
eencae 0:7423345f87c5 398
eencae 0:7423345f87c5 399 /** Draw Rectangle
eencae 0:7423345f87c5 400 *
eencae 0:7423345f87c5 401 * This function draws a rectangle.
eencae 0:7423345f87c5 402 * @param x0 - x-coordinate of origin (top-left)
eencae 0:7423345f87c5 403 * @param y0 - y-coordinate of origin (top-left)
eencae 0:7423345f87c5 404 * @param width - width of rectangle
eencae 0:7423345f87c5 405 * @param height - height of rectangle
eencae 0:7423345f87c5 406 * @param fill - fill-type for the shape
eencae 0:7423345f87c5 407 */
eencae 0:7423345f87c5 408 void drawRect(unsigned int const x0,
eencae 0:7423345f87c5 409 unsigned int const y0,
eencae 0:7423345f87c5 410 unsigned int const width,
eencae 0:7423345f87c5 411 unsigned int const height,
eencae 0:7423345f87c5 412 FillType const fill);
eencae 0:7423345f87c5 413
eencae 0:7423345f87c5 414 /** Draw Sprite
eencae 0:7423345f87c5 415 *
eencae 0:7423345f87c5 416 * This function draws a sprite as defined in a 2D array
eencae 0:7423345f87c5 417 * @param x0 - x-coordinate of origin (top-left)
eencae 0:7423345f87c5 418 * @param y0 - y-coordinate of origin (top-left)
eencae 0:7423345f87c5 419 * @param nrows - number of rows in sprite
eencae 0:7423345f87c5 420 * @param ncols - number of columns in sprite
eencae 0:7423345f87c5 421 * @param sprite - 2D array representing the sprite
eencae 0:7423345f87c5 422 */
eencae 0:7423345f87c5 423 void drawSprite(int x0,
eencae 0:7423345f87c5 424 int y0,
eencae 0:7423345f87c5 425 int nrows,
eencae 0:7423345f87c5 426 int ncols,
eencae 0:7423345f87c5 427 int *sprite);
eencae 0:7423345f87c5 428
eencae 0:7423345f87c5 429
eencae 0:7423345f87c5 430 private:
eencae 0:7423345f87c5 431 // methods
eencae 0:7423345f87c5 432 void setXYAddress(unsigned int const x,
eencae 0:7423345f87c5 433 unsigned int const y);
eencae 0:7423345f87c5 434 void initSPI();
eencae 0:7423345f87c5 435 void turnOn();
eencae 0:7423345f87c5 436 void reset();
eencae 0:7423345f87c5 437 void clearRAM();
eencae 0:7423345f87c5 438 void sendCommand(unsigned char command);
eencae 0:7423345f87c5 439 void sendData(unsigned char data);
eencae 0:7423345f87c5 440 void setTempCoefficient(char tc); // 0 to 3
eencae 0:7423345f87c5 441 void setBias(char bias); // 0 to 7
eencae 0:7423345f87c5 442 };
eencae 0:7423345f87c5 443
eencae 0:7423345f87c5 444 const unsigned char font5x7[480] = {
eencae 0:7423345f87c5 445 0x00, 0x00, 0x00, 0x00, 0x00,// (space)
eencae 0:7423345f87c5 446 0x00, 0x00, 0x5F, 0x00, 0x00,// !
eencae 0:7423345f87c5 447 0x00, 0x07, 0x00, 0x07, 0x00,// "
eencae 0:7423345f87c5 448 0x14, 0x7F, 0x14, 0x7F, 0x14,// #
eencae 0:7423345f87c5 449 0x24, 0x2A, 0x7F, 0x2A, 0x12,// $
eencae 0:7423345f87c5 450 0x23, 0x13, 0x08, 0x64, 0x62,// %
eencae 0:7423345f87c5 451 0x36, 0x49, 0x55, 0x22, 0x50,// &
eencae 0:7423345f87c5 452 0x00, 0x05, 0x03, 0x00, 0x00,// '
eencae 0:7423345f87c5 453 0x00, 0x1C, 0x22, 0x41, 0x00,// (
eencae 0:7423345f87c5 454 0x00, 0x41, 0x22, 0x1C, 0x00,// )
eencae 0:7423345f87c5 455 0x08, 0x2A, 0x1C, 0x2A, 0x08,// *
eencae 0:7423345f87c5 456 0x08, 0x08, 0x3E, 0x08, 0x08,// +
eencae 0:7423345f87c5 457 0x00, 0x50, 0x30, 0x00, 0x00,// ,
eencae 0:7423345f87c5 458 0x08, 0x08, 0x08, 0x08, 0x08,// -
eencae 0:7423345f87c5 459 0x00, 0x60, 0x60, 0x00, 0x00,// .
eencae 0:7423345f87c5 460 0x20, 0x10, 0x08, 0x04, 0x02,// /
eencae 0:7423345f87c5 461 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
eencae 0:7423345f87c5 462 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
eencae 0:7423345f87c5 463 0x42, 0x61, 0x51, 0x49, 0x46,// 2
eencae 0:7423345f87c5 464 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
eencae 0:7423345f87c5 465 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
eencae 0:7423345f87c5 466 0x27, 0x45, 0x45, 0x45, 0x39,// 5
eencae 0:7423345f87c5 467 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
eencae 0:7423345f87c5 468 0x01, 0x71, 0x09, 0x05, 0x03,// 7
eencae 0:7423345f87c5 469 0x36, 0x49, 0x49, 0x49, 0x36,// 8
eencae 0:7423345f87c5 470 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
eencae 0:7423345f87c5 471 0x00, 0x36, 0x36, 0x00, 0x00,// :
eencae 0:7423345f87c5 472 0x00, 0x56, 0x36, 0x00, 0x00,// ;
eencae 0:7423345f87c5 473 0x00, 0x08, 0x14, 0x22, 0x41,// <
eencae 0:7423345f87c5 474 0x14, 0x14, 0x14, 0x14, 0x14,// =
eencae 0:7423345f87c5 475 0x41, 0x22, 0x14, 0x08, 0x00,// >
eencae 0:7423345f87c5 476 0x02, 0x01, 0x51, 0x09, 0x06,// ?
eencae 0:7423345f87c5 477 0x32, 0x49, 0x79, 0x41, 0x3E,// @
eencae 0:7423345f87c5 478 0x7E, 0x11, 0x11, 0x11, 0x7E,// A
eencae 0:7423345f87c5 479 0x7F, 0x49, 0x49, 0x49, 0x36,// B
eencae 0:7423345f87c5 480 0x3E, 0x41, 0x41, 0x41, 0x22,// C
eencae 0:7423345f87c5 481 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
eencae 0:7423345f87c5 482 0x7F, 0x49, 0x49, 0x49, 0x41,// E
eencae 0:7423345f87c5 483 0x7F, 0x09, 0x09, 0x01, 0x01,// F
eencae 0:7423345f87c5 484 0x3E, 0x41, 0x41, 0x51, 0x32,// G
eencae 0:7423345f87c5 485 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
eencae 0:7423345f87c5 486 0x00, 0x41, 0x7F, 0x41, 0x00,// I
eencae 0:7423345f87c5 487 0x20, 0x40, 0x41, 0x3F, 0x01,// J
eencae 0:7423345f87c5 488 0x7F, 0x08, 0x14, 0x22, 0x41,// K
eencae 0:7423345f87c5 489 0x7F, 0x40, 0x40, 0x40, 0x40,// L
eencae 0:7423345f87c5 490 0x7F, 0x02, 0x04, 0x02, 0x7F,// M
eencae 0:7423345f87c5 491 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
eencae 0:7423345f87c5 492 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
eencae 0:7423345f87c5 493 0x7F, 0x09, 0x09, 0x09, 0x06,// P
eencae 0:7423345f87c5 494 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
eencae 0:7423345f87c5 495 0x7F, 0x09, 0x19, 0x29, 0x46,// R
eencae 0:7423345f87c5 496 0x46, 0x49, 0x49, 0x49, 0x31,// S
eencae 0:7423345f87c5 497 0x01, 0x01, 0x7F, 0x01, 0x01,// T
eencae 0:7423345f87c5 498 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
eencae 0:7423345f87c5 499 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
eencae 0:7423345f87c5 500 0x7F, 0x20, 0x18, 0x20, 0x7F,// W
eencae 0:7423345f87c5 501 0x63, 0x14, 0x08, 0x14, 0x63,// X
eencae 0:7423345f87c5 502 0x03, 0x04, 0x78, 0x04, 0x03,// Y
eencae 0:7423345f87c5 503 0x61, 0x51, 0x49, 0x45, 0x43,// Z
eencae 0:7423345f87c5 504 0x00, 0x00, 0x7F, 0x41, 0x41,// [
eencae 0:7423345f87c5 505 0x02, 0x04, 0x08, 0x10, 0x20,// "\"
eencae 0:7423345f87c5 506 0x41, 0x41, 0x7F, 0x00, 0x00,// ]
eencae 0:7423345f87c5 507 0x04, 0x02, 0x01, 0x02, 0x04,// ^
eencae 0:7423345f87c5 508 0x40, 0x40, 0x40, 0x40, 0x40,// _
eencae 0:7423345f87c5 509 0x00, 0x01, 0x02, 0x04, 0x00,// `
eencae 0:7423345f87c5 510 0x20, 0x54, 0x54, 0x54, 0x78,// a
eencae 0:7423345f87c5 511 0x7F, 0x48, 0x44, 0x44, 0x38,// b
eencae 0:7423345f87c5 512 0x38, 0x44, 0x44, 0x44, 0x20,// c
eencae 0:7423345f87c5 513 0x38, 0x44, 0x44, 0x48, 0x7F,// d
eencae 0:7423345f87c5 514 0x38, 0x54, 0x54, 0x54, 0x18,// e
eencae 0:7423345f87c5 515 0x08, 0x7E, 0x09, 0x01, 0x02,// f
eencae 0:7423345f87c5 516 0x08, 0x14, 0x54, 0x54, 0x3C,// g
eencae 0:7423345f87c5 517 0x7F, 0x08, 0x04, 0x04, 0x78,// h
eencae 0:7423345f87c5 518 0x00, 0x44, 0x7D, 0x40, 0x00,// i
eencae 0:7423345f87c5 519 0x20, 0x40, 0x44, 0x3D, 0x00,// j
eencae 0:7423345f87c5 520 0x00, 0x7F, 0x10, 0x28, 0x44,// k
eencae 0:7423345f87c5 521 0x00, 0x41, 0x7F, 0x40, 0x00,// l
eencae 0:7423345f87c5 522 0x7C, 0x04, 0x18, 0x04, 0x78,// m
eencae 0:7423345f87c5 523 0x7C, 0x08, 0x04, 0x04, 0x78,// n
eencae 0:7423345f87c5 524 0x38, 0x44, 0x44, 0x44, 0x38,// o
eencae 0:7423345f87c5 525 0x7C, 0x14, 0x14, 0x14, 0x08,// p
eencae 0:7423345f87c5 526 0x08, 0x14, 0x14, 0x18, 0x7C,// q
eencae 0:7423345f87c5 527 0x7C, 0x08, 0x04, 0x04, 0x08,// r
eencae 0:7423345f87c5 528 0x48, 0x54, 0x54, 0x54, 0x20,// s
eencae 0:7423345f87c5 529 0x04, 0x3F, 0x44, 0x40, 0x20,// t
eencae 0:7423345f87c5 530 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
eencae 0:7423345f87c5 531 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
eencae 0:7423345f87c5 532 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
eencae 0:7423345f87c5 533 0x44, 0x28, 0x10, 0x28, 0x44,// x
eencae 0:7423345f87c5 534 0x0C, 0x50, 0x50, 0x50, 0x3C,// y
eencae 0:7423345f87c5 535 0x44, 0x64, 0x54, 0x4C, 0x44,// z
eencae 0:7423345f87c5 536 0x00, 0x08, 0x36, 0x41, 0x00,// {
eencae 0:7423345f87c5 537 0x00, 0x00, 0x7F, 0x00, 0x00,// |
eencae 0:7423345f87c5 538 0x00, 0x41, 0x36, 0x08, 0x00,// }
eencae 0:7423345f87c5 539 0x08, 0x08, 0x2A, 0x1C, 0x08,// ->
eencae 0:7423345f87c5 540 0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
eencae 0:7423345f87c5 541 };
eencae 0:7423345f87c5 542
eencae 0:7423345f87c5 543 #endif