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