ccc

Dependencies:   mbed FXOS8700Q

Committer:
VivianDu
Date:
Sun May 05 14:06:00 2019 +0000
Revision:
0:bc1d36f5f772
Child:
2:b891a5841ee2
version_change

Who changed what in which revision?

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