Li Ruofan 201199450

Dependencies:   mbed

Committer:
DannyLee
Date:
Sun May 24 08:16:35 2020 +0000
Revision:
1:bd7c99a5bd10
Parent:
0:80a59a49d504
Li Ruofan 201199450

Who changed what in which revision?

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