Helios Lyons 201239214

Dependencies:   mbed

Brief

My aim for this project was to create a FRDM K64F adapted version of the classic Space Invaders by Tomohiro Nishikado. The game itself has a number of clear features to implement;

  • Left to right movement for the player 'canon'
  • A fixed amount of player lives
  • Firing mechanics for both canon and invaders (hence collision systems)
  • Random firing from remaining invaders
  • Wave based combat

My own addition to these established ideas was Boss waves, featuring a single, larger sprite which fires at a faster interval than previous waves. The addition of a movement system using a basic for loop, as opposed to a velocity based system, will enhance the nostalgic feel of the game.

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.12.00.png

Gameplay

Movement is controlled with the joystick, moving the canon left or right. Fire by pressing A. Invaders spawn at set positions, but randomly fire at a set interval, which is higher for boss waves. Time is taken during each wave, and displayed at wave intervals, and if the play wins.

Controls are shown on the Gamepad below: (attribution: Craig A. Evans, ELEC2645 University of Leeds)

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.20.18.png

Committer:
helioslyons
Date:
Wed May 27 15:40:47 2020 +0000
Revision:
13:1472c1637bfc
Parent:
1:a3f43007030e
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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