ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Wed May 08 08:46:11 2019 +0000
Revision:
23:9be87557b89a
Parent:
1:0001cb3eb053
Child:
24:67dc71a8f009
Added a sprite for the doodler and the enemy.

Who changed what in which revision?

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