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