Du Xianjie 201089059

Dependencies:   mbed FXOS8700Q

Committer:
VivianDu
Date:
Mon May 06 04:25:09 2019 +0000
Revision:
13:9a6aff771774
Parent:
12:5846e92948ad
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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