Final Submission. I have read and agreed with Statement of Academic Integrity.

Dependencies:   mbed

Committer:
Nicholas75179
Date:
Fri May 22 10:28:29 2020 +0000
Revision:
2:bb50c3a9a05c
Parent:
0:fde420d18f42
published 11:27 22/5/2020

Who changed what in which revision?

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