Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Sun Apr 26 17:58:49 2020 +0000
Revision:
0:44c1a60f8732
Child:
8:b4a2954dd74f
Add library N5110 to the project

Who changed what in which revision?

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