Yunting Zou 201199716

Dependencies:   mbed MotionSensor

Committer:
zhouyun123
Date:
Thu May 14 16:17:30 2020 +0000
Revision:
0:047e14f53977
first commit

Who changed what in which revision?

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