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