ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Committer:
el17cd
Date:
Wed May 08 18:17:59 2019 +0000
Revision:
46:824ec81ff578
Parent:
0:efb5eec6b8ea
Potentiometer now changes screen contrast for use on other devices.; Final Submission.; I have read and agreed with the Statement of Academic Integrity.

Who changed what in which revision?

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