Working Menu with selectable fields yet to add comparison with healthy temperature ranges

Dependencies:   TMP102_02

Committer:
ejh23
Date:
Fri Feb 04 19:10:09 2022 +0000
Revision:
10:62da82b9b6de
Parent:
1:e11018cf2c14
Final code for submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejh23 1:e11018cf2c14 1 #include "mbed.h"
ejh23 1:e11018cf2c14 2 #include "N5110.h"
ejh23 1:e11018cf2c14 3
ejh23 1:e11018cf2c14 4 // overloaded constructor includes power pin - LCD Vcc connected to GPIO pin
ejh23 1:e11018cf2c14 5 // this constructor works fine with LPC1768 - enough current sourced from GPIO
ejh23 1:e11018cf2c14 6 // to power LCD. Doesn't work well with K64F.
ejh23 1:e11018cf2c14 7 /*N5110::N5110(PinName const pwrPin,
ejh23 1:e11018cf2c14 8 PinName const scePin,
ejh23 1:e11018cf2c14 9 PinName const rstPin,
ejh23 1:e11018cf2c14 10 PinName const dcPin,
ejh23 1:e11018cf2c14 11 PinName const mosiPin,
ejh23 1:e11018cf2c14 12 PinName const sclkPin,
ejh23 1:e11018cf2c14 13 PinName const ledPin)
ejh23 1:e11018cf2c14 14 :
ejh23 1:e11018cf2c14 15 _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
ejh23 1:e11018cf2c14 16 _led(new PwmOut(ledPin)),
ejh23 1:e11018cf2c14 17 _pwr(new DigitalOut(pwrPin)),
ejh23 1:e11018cf2c14 18 _sce(new DigitalOut(scePin)),
ejh23 1:e11018cf2c14 19 _rst(new DigitalOut(rstPin)),
ejh23 1:e11018cf2c14 20 _dc(new DigitalOut(dcPin))
ejh23 1:e11018cf2c14 21 {}
ejh23 1:e11018cf2c14 22 */
ejh23 1:e11018cf2c14 23 // overloaded constructor does not include power pin - LCD Vcc must be tied to +3V3
ejh23 1:e11018cf2c14 24 // Best to use this with K64F as the GPIO hasn't sufficient output current to reliably
ejh23 1:e11018cf2c14 25 // drive the LCD.
ejh23 1:e11018cf2c14 26 N5110::N5110(PinName const scePin,
ejh23 1:e11018cf2c14 27 PinName const rstPin,
ejh23 1:e11018cf2c14 28 PinName const dcPin,
ejh23 1:e11018cf2c14 29 PinName const mosiPin,
ejh23 1:e11018cf2c14 30 PinName const sclkPin,
ejh23 1:e11018cf2c14 31 PinName const ledPin)
ejh23 1:e11018cf2c14 32 :
ejh23 1:e11018cf2c14 33 _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
ejh23 1:e11018cf2c14 34 _led(new PwmOut(ledPin)),
ejh23 1:e11018cf2c14 35 _pwr(NULL), // pwr not needed so null it to be safe
ejh23 1:e11018cf2c14 36 _sce(new DigitalOut(scePin)),
ejh23 1:e11018cf2c14 37 _rst(new DigitalOut(rstPin)),
ejh23 1:e11018cf2c14 38 _dc(new DigitalOut(dcPin))
ejh23 1:e11018cf2c14 39 {}
ejh23 1:e11018cf2c14 40
ejh23 1:e11018cf2c14 41 N5110::~N5110()
ejh23 1:e11018cf2c14 42 {
ejh23 1:e11018cf2c14 43 delete _spi;
ejh23 1:e11018cf2c14 44
ejh23 1:e11018cf2c14 45 if(_pwr) {
ejh23 1:e11018cf2c14 46 delete _pwr;
ejh23 1:e11018cf2c14 47 }
ejh23 1:e11018cf2c14 48
ejh23 1:e11018cf2c14 49 delete _led;
ejh23 1:e11018cf2c14 50 delete _sce;
ejh23 1:e11018cf2c14 51 delete _rst;
ejh23 1:e11018cf2c14 52 delete _dc;
ejh23 1:e11018cf2c14 53 }
ejh23 1:e11018cf2c14 54
ejh23 1:e11018cf2c14 55 // initialise function - powers up and sends the initialisation commands
ejh23 1:e11018cf2c14 56 void N5110::init()
ejh23 1:e11018cf2c14 57 {
ejh23 1:e11018cf2c14 58 turnOn(); // power up
ejh23 1:e11018cf2c14 59 //Wait(0.1); // small delay seems to prevent spurious pixels during mbed reset
ejh23 1:e11018cf2c14 60 reset(); // reset LCD - must be done within 100 ms
ejh23 1:e11018cf2c14 61 // initSPI();
ejh23 1:e11018cf2c14 62
ejh23 1:e11018cf2c14 63 setContrast(0.55); // this may need tuning (say 0.4 to 0.6)
ejh23 1:e11018cf2c14 64 setBias(3); // datasheet - 48:1 mux - don't mess with if you don't know what you're doing! (0 to 7)
ejh23 1:e11018cf2c14 65 setTempCoefficient(0); // datasheet - may need increasing (range 0 to 3) at very low temperatures
ejh23 1:e11018cf2c14 66 normalMode(); // normal video mode by default
ejh23 1:e11018cf2c14 67
ejh23 1:e11018cf2c14 68 clearRAM(); // RAM is undefined at power-up so clear to be sure
ejh23 1:e11018cf2c14 69 clear(); // clear buffer
ejh23 1:e11018cf2c14 70 setBrightness(0.5);
ejh23 1:e11018cf2c14 71 }
ejh23 1:e11018cf2c14 72
ejh23 1:e11018cf2c14 73 // sets normal video mode (black on white)
ejh23 1:e11018cf2c14 74 void N5110::normalMode()
ejh23 1:e11018cf2c14 75 {
ejh23 1:e11018cf2c14 76 sendCommand(0b00100000); // basic instruction
ejh23 1:e11018cf2c14 77 sendCommand(0b00001100); // normal video mode- datasheet
ejh23 1:e11018cf2c14 78 }
ejh23 1:e11018cf2c14 79
ejh23 1:e11018cf2c14 80 // sets normal video mode (white on black)
ejh23 1:e11018cf2c14 81 void N5110::inverseMode()
ejh23 1:e11018cf2c14 82 {
ejh23 1:e11018cf2c14 83 sendCommand(0b00100000); // basic instruction
ejh23 1:e11018cf2c14 84 sendCommand(0b00001101); // inverse video mode - datasheet
ejh23 1:e11018cf2c14 85 }
ejh23 1:e11018cf2c14 86
ejh23 1:e11018cf2c14 87 // function to power up the LCD and backlight - only works when using GPIO to power
ejh23 1:e11018cf2c14 88 void N5110::turnOn()
ejh23 1:e11018cf2c14 89 {
ejh23 1:e11018cf2c14 90 if (_pwr != NULL) {
ejh23 1:e11018cf2c14 91 _pwr->write(1); // apply power
ejh23 1:e11018cf2c14 92 }
ejh23 1:e11018cf2c14 93 }
ejh23 1:e11018cf2c14 94
ejh23 1:e11018cf2c14 95 // function to power down LCD
ejh23 1:e11018cf2c14 96 void N5110::turnOff()
ejh23 1:e11018cf2c14 97 {
ejh23 1:e11018cf2c14 98 clear(); // clear buffer
ejh23 1:e11018cf2c14 99 refresh();
ejh23 1:e11018cf2c14 100 setBrightness(0.0); // turn backlight off
ejh23 1:e11018cf2c14 101 clearRAM(); // clear RAM to ensure specified current consumption
ejh23 1:e11018cf2c14 102 // send command to ensure we are in basic mode
ejh23 1:e11018cf2c14 103
ejh23 1:e11018cf2c14 104 sendCommand(0b00100000); // basic mode
ejh23 1:e11018cf2c14 105 sendCommand(0b00001000); // clear display
ejh23 1:e11018cf2c14 106 sendCommand(0b00100001); // extended mode
ejh23 1:e11018cf2c14 107 sendCommand(0b00100100); // power down
ejh23 1:e11018cf2c14 108
ejh23 1:e11018cf2c14 109 // if we are powering the LCD using the GPIO then make it low to turn off
ejh23 1:e11018cf2c14 110 if (_pwr != NULL) {
ejh23 1:e11018cf2c14 111 //wait(0.10); // small delay and then turn off the power pin
ejh23 1:e11018cf2c14 112 _pwr->write(0); // turn off power
ejh23 1:e11018cf2c14 113 }
ejh23 1:e11018cf2c14 114
ejh23 1:e11018cf2c14 115 }
ejh23 1:e11018cf2c14 116
ejh23 1:e11018cf2c14 117 // function to change LED backlight brightness
ejh23 1:e11018cf2c14 118 void N5110::setBrightness(float brightness)
ejh23 1:e11018cf2c14 119 {
ejh23 1:e11018cf2c14 120 // check whether brightness is within range
ejh23 1:e11018cf2c14 121 if (brightness < 0.0f)
ejh23 1:e11018cf2c14 122 brightness = 0.0f;
ejh23 1:e11018cf2c14 123 if (brightness > 1.0f)
ejh23 1:e11018cf2c14 124 brightness = 1.0f;
ejh23 1:e11018cf2c14 125 // set PWM duty cycle
ejh23 1:e11018cf2c14 126 _led->write(brightness);
ejh23 1:e11018cf2c14 127 }
ejh23 1:e11018cf2c14 128
ejh23 1:e11018cf2c14 129 void N5110::setContrast(float contrast) {
ejh23 1:e11018cf2c14 130
ejh23 1:e11018cf2c14 131 // enforce limits
ejh23 1:e11018cf2c14 132 if (contrast > 1.0f)
ejh23 1:e11018cf2c14 133 contrast = 1.0f;
ejh23 1:e11018cf2c14 134 else if (contrast < 0.0f)
ejh23 1:e11018cf2c14 135 contrast = 0.0;
ejh23 1:e11018cf2c14 136
ejh23 1:e11018cf2c14 137 // convert to char in range 0 to 127 (i.e. 6 bits)
ejh23 1:e11018cf2c14 138 char ic = char(contrast*127.0f);
ejh23 1:e11018cf2c14 139
ejh23 1:e11018cf2c14 140 sendCommand(0b00100001); // extended instruction set
ejh23 1:e11018cf2c14 141 sendCommand(0b10000000 | ic); // set Vop (which controls contrast)
ejh23 1:e11018cf2c14 142 sendCommand(0b00100000); // back to basic instruction set
ejh23 1:e11018cf2c14 143 }
ejh23 1:e11018cf2c14 144
ejh23 1:e11018cf2c14 145 void N5110::setTempCoefficient(char tc) {
ejh23 1:e11018cf2c14 146
ejh23 1:e11018cf2c14 147 // enforce limits
ejh23 1:e11018cf2c14 148 if (tc>3) {
ejh23 1:e11018cf2c14 149 tc=3;
ejh23 1:e11018cf2c14 150 }
ejh23 1:e11018cf2c14 151
ejh23 1:e11018cf2c14 152 // temperature coefficient may need increasing at low temperatures
ejh23 1:e11018cf2c14 153
ejh23 1:e11018cf2c14 154 sendCommand(0b00100001); // extended instruction set
ejh23 1:e11018cf2c14 155 sendCommand(0b00000100 | tc);
ejh23 1:e11018cf2c14 156 sendCommand(0b00100000); // back to basic instruction set
ejh23 1:e11018cf2c14 157 }
ejh23 1:e11018cf2c14 158
ejh23 1:e11018cf2c14 159 void N5110::setBias(char bias) {
ejh23 1:e11018cf2c14 160
ejh23 1:e11018cf2c14 161 // from data sheet
ejh23 1:e11018cf2c14 162 // bias mux rate
ejh23 1:e11018cf2c14 163 // 0 1:100
ejh23 1:e11018cf2c14 164 // 1 1:80
ejh23 1:e11018cf2c14 165 // 2 1:65
ejh23 1:e11018cf2c14 166 // 3 1:48 (default)
ejh23 1:e11018cf2c14 167 // 4 1:40/1:34
ejh23 1:e11018cf2c14 168 // 5 1:24
ejh23 1:e11018cf2c14 169 // 6 1:18/1:16
ejh23 1:e11018cf2c14 170 // 7 1:10/1:9/1:8
ejh23 1:e11018cf2c14 171
ejh23 1:e11018cf2c14 172 // enforce limits
ejh23 1:e11018cf2c14 173 if (bias>7) {
ejh23 1:e11018cf2c14 174 bias=7;
ejh23 1:e11018cf2c14 175 }
ejh23 1:e11018cf2c14 176
ejh23 1:e11018cf2c14 177 sendCommand(0b00100001); // extended mode instruction
ejh23 1:e11018cf2c14 178 sendCommand(0b00010000 | bias);
ejh23 1:e11018cf2c14 179 sendCommand(0b00100000); // end of extended mode instruction
ejh23 1:e11018cf2c14 180 }
ejh23 1:e11018cf2c14 181
ejh23 1:e11018cf2c14 182 // pulse the active low reset line
ejh23 1:e11018cf2c14 183 void N5110::reset()
ejh23 1:e11018cf2c14 184 {
ejh23 1:e11018cf2c14 185 _rst->write(0); // reset the LCD
ejh23 1:e11018cf2c14 186 _rst->write(1);
ejh23 1:e11018cf2c14 187 }
ejh23 1:e11018cf2c14 188
ejh23 1:e11018cf2c14 189 // function to initialise SPI peripheral
ejh23 1:e11018cf2c14 190 void N5110::initSPI()
ejh23 1:e11018cf2c14 191 {
ejh23 1:e11018cf2c14 192 _spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge
ejh23 1:e11018cf2c14 193 _spi->frequency(4000000); // maximum of screen is 4 MHz
ejh23 1:e11018cf2c14 194 }
ejh23 1:e11018cf2c14 195
ejh23 1:e11018cf2c14 196 // send a command to the display
ejh23 1:e11018cf2c14 197 void N5110::sendCommand(unsigned char command)
ejh23 1:e11018cf2c14 198 {
ejh23 1:e11018cf2c14 199 _dc->write(0); // set DC low for command
ejh23 1:e11018cf2c14 200 _sce->write(0); // set CE low to begin frame
ejh23 1:e11018cf2c14 201 _spi->write(command); // send command
ejh23 1:e11018cf2c14 202 _dc->write(1); // turn back to data by default
ejh23 1:e11018cf2c14 203 _sce->write(1); // set CE high to end frame (expected for transmission of single byte)
ejh23 1:e11018cf2c14 204 }
ejh23 1:e11018cf2c14 205
ejh23 1:e11018cf2c14 206 // send data to the display at the current XY address
ejh23 1:e11018cf2c14 207 // dc is set to 1 (i.e. data) after sending a command and so should
ejh23 1:e11018cf2c14 208 // be the default mode.
ejh23 1:e11018cf2c14 209 void N5110::sendData(unsigned char data)
ejh23 1:e11018cf2c14 210 {
ejh23 1:e11018cf2c14 211 _sce->write(0); // set CE low to begin frame
ejh23 1:e11018cf2c14 212 _spi->write(data);
ejh23 1:e11018cf2c14 213 _sce->write(1); // set CE high to end frame (expected for transmission of single byte)
ejh23 1:e11018cf2c14 214 }
ejh23 1:e11018cf2c14 215
ejh23 1:e11018cf2c14 216 // this function writes 0 to the 504 bytes to clear the RAM
ejh23 1:e11018cf2c14 217 void N5110::clearRAM()
ejh23 1:e11018cf2c14 218 {
ejh23 1:e11018cf2c14 219 _sce->write(0); //set CE low to begin frame
ejh23 1:e11018cf2c14 220 for(int i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
ejh23 1:e11018cf2c14 221 _spi->write(0x00); // send 0's
ejh23 1:e11018cf2c14 222 }
ejh23 1:e11018cf2c14 223 _sce->write(1); // set CE high to end frame
ejh23 1:e11018cf2c14 224 }
ejh23 1:e11018cf2c14 225
ejh23 1:e11018cf2c14 226 // function to set the XY address in RAM for subsequenct data write
ejh23 1:e11018cf2c14 227 void N5110::setXYAddress(unsigned int const x,
ejh23 1:e11018cf2c14 228 unsigned int const y)
ejh23 1:e11018cf2c14 229 {
ejh23 1:e11018cf2c14 230 if (x<WIDTH && y<HEIGHT) { // check within range
ejh23 1:e11018cf2c14 231 sendCommand(0b00100000); // basic instruction
ejh23 1:e11018cf2c14 232 sendCommand(0b10000000 | x); // send addresses to display with relevant mask
ejh23 1:e11018cf2c14 233 sendCommand(0b01000000 | y);
ejh23 1:e11018cf2c14 234 }
ejh23 1:e11018cf2c14 235 }
ejh23 1:e11018cf2c14 236
ejh23 1:e11018cf2c14 237 // These functions are used to set, clear and get the value of pixels in the display
ejh23 1:e11018cf2c14 238 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh()
ejh23 1:e11018cf2c14 239 // function must be called after set and clear in order to update the display
ejh23 1:e11018cf2c14 240 void N5110::setPixel(unsigned int const x,
ejh23 1:e11018cf2c14 241 unsigned int const y,
ejh23 1:e11018cf2c14 242 bool const state)
ejh23 1:e11018cf2c14 243 {
ejh23 1:e11018cf2c14 244 if (x<WIDTH && y<HEIGHT) { // check within range
ejh23 1:e11018cf2c14 245 // calculate bank and shift 1 to required position in the data byte
ejh23 1:e11018cf2c14 246 if(state) buffer[x][y/8] |= (1 << y%8);
ejh23 1:e11018cf2c14 247 else buffer[x][y/8] &= ~(1 << y%8);
ejh23 1:e11018cf2c14 248 }
ejh23 1:e11018cf2c14 249 }
ejh23 1:e11018cf2c14 250
ejh23 1:e11018cf2c14 251 void N5110::clearPixel(unsigned int const x,
ejh23 1:e11018cf2c14 252 unsigned int const y)
ejh23 1:e11018cf2c14 253 {
ejh23 1:e11018cf2c14 254 if (x<WIDTH && y<HEIGHT) { // check within range
ejh23 1:e11018cf2c14 255 // calculate bank and shift 1 to required position (using bit clear)
ejh23 1:e11018cf2c14 256 buffer[x][y/8] &= ~(1 << y%8);
ejh23 1:e11018cf2c14 257 }
ejh23 1:e11018cf2c14 258 }
ejh23 1:e11018cf2c14 259
ejh23 1:e11018cf2c14 260 int N5110::getPixel(unsigned int const x,
ejh23 1:e11018cf2c14 261 unsigned int const y) const
ejh23 1:e11018cf2c14 262 {
ejh23 1:e11018cf2c14 263 if (x<WIDTH && y<HEIGHT) { // check within range
ejh23 1:e11018cf2c14 264 // return relevant bank and mask required bit
ejh23 1:e11018cf2c14 265
ejh23 1:e11018cf2c14 266 int pixel = (int) buffer[x][y/8] & (1 << y%8);
ejh23 1:e11018cf2c14 267
ejh23 1:e11018cf2c14 268 if (pixel)
ejh23 1:e11018cf2c14 269 return 1;
ejh23 1:e11018cf2c14 270 else
ejh23 1:e11018cf2c14 271 return 0;
ejh23 1:e11018cf2c14 272 }
ejh23 1:e11018cf2c14 273
ejh23 1:e11018cf2c14 274 return 0;
ejh23 1:e11018cf2c14 275
ejh23 1:e11018cf2c14 276 }
ejh23 1:e11018cf2c14 277
ejh23 1:e11018cf2c14 278 // function to refresh the display
ejh23 1:e11018cf2c14 279 void N5110::refresh()
ejh23 1:e11018cf2c14 280 {
ejh23 1:e11018cf2c14 281 setXYAddress(0,0); // important to set address back to 0,0 before refreshing display
ejh23 1:e11018cf2c14 282 // address auto increments after printing string, so buffer[0][0] will not coincide
ejh23 1:e11018cf2c14 283 // with top-left pixel after priting string
ejh23 1:e11018cf2c14 284
ejh23 1:e11018cf2c14 285 _sce->write(0); //set CE low to begin frame
ejh23 1:e11018cf2c14 286
ejh23 1:e11018cf2c14 287 for(int j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
ejh23 1:e11018cf2c14 288 for(int i = 0; i < WIDTH; i++) {
ejh23 1:e11018cf2c14 289 _spi->write(buffer[i][j]); // send buffer
ejh23 1:e11018cf2c14 290 }
ejh23 1:e11018cf2c14 291 }
ejh23 1:e11018cf2c14 292 _sce->write(1); // set CE high to end frame
ejh23 1:e11018cf2c14 293
ejh23 1:e11018cf2c14 294 }
ejh23 1:e11018cf2c14 295
ejh23 1:e11018cf2c14 296 // fills the buffer with random bytes. Can be used to test the display.
ejh23 1:e11018cf2c14 297 // The rand() function isn't seeded so it probably creates the same pattern everytime
ejh23 1:e11018cf2c14 298 void N5110::randomiseBuffer()
ejh23 1:e11018cf2c14 299 {
ejh23 1:e11018cf2c14 300 int i,j;
ejh23 1:e11018cf2c14 301 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
ejh23 1:e11018cf2c14 302 for(i = 0; i < WIDTH; i++) {
ejh23 1:e11018cf2c14 303 buffer[i][j] = rand()%256; // generate random byte
ejh23 1:e11018cf2c14 304 }
ejh23 1:e11018cf2c14 305 }
ejh23 1:e11018cf2c14 306
ejh23 1:e11018cf2c14 307 }
ejh23 1:e11018cf2c14 308
ejh23 1:e11018cf2c14 309 // function to print 5x7 font
ejh23 1:e11018cf2c14 310 void N5110::printChar(char const c,
ejh23 1:e11018cf2c14 311 unsigned int const x,
ejh23 1:e11018cf2c14 312 unsigned int const y)
ejh23 1:e11018cf2c14 313 {
ejh23 1:e11018cf2c14 314 if (y<BANKS) { // check if printing in range of y banks
ejh23 1:e11018cf2c14 315
ejh23 1:e11018cf2c14 316 for (int i = 0; i < 5 ; i++ ) {
ejh23 1:e11018cf2c14 317 int pixel_x = x+i;
ejh23 1:e11018cf2c14 318 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
ejh23 1:e11018cf2c14 319 break;
ejh23 1:e11018cf2c14 320 buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
ejh23 1:e11018cf2c14 321 // array is offset by 32 relative to ASCII, each character is 5 pixels wide
ejh23 1:e11018cf2c14 322 }
ejh23 1:e11018cf2c14 323
ejh23 1:e11018cf2c14 324 }
ejh23 1:e11018cf2c14 325 }
ejh23 1:e11018cf2c14 326
ejh23 1:e11018cf2c14 327 // function to print string at specified position
ejh23 1:e11018cf2c14 328 void N5110::printString(const char *str,
ejh23 1:e11018cf2c14 329 unsigned int const x,
ejh23 1:e11018cf2c14 330 unsigned int const y)
ejh23 1:e11018cf2c14 331 {
ejh23 1:e11018cf2c14 332 if (y<BANKS) { // check if printing in range of y banks
ejh23 1:e11018cf2c14 333
ejh23 1:e11018cf2c14 334 int n = 0 ; // counter for number of characters in string
ejh23 1:e11018cf2c14 335 // loop through string and print character
ejh23 1:e11018cf2c14 336 while(*str) {
ejh23 1:e11018cf2c14 337
ejh23 1:e11018cf2c14 338 // writes the character bitmap data to the buffer, so that
ejh23 1:e11018cf2c14 339 // text and pixels can be displayed at the same time
ejh23 1:e11018cf2c14 340 for (int i = 0; i < 5 ; i++ ) {
ejh23 1:e11018cf2c14 341 int pixel_x = x+i+n*6;
ejh23 1:e11018cf2c14 342 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
ejh23 1:e11018cf2c14 343 break;
ejh23 1:e11018cf2c14 344 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
ejh23 1:e11018cf2c14 345 }
ejh23 1:e11018cf2c14 346 str++; // go to next character in string
ejh23 1:e11018cf2c14 347 n++; // increment index
ejh23 1:e11018cf2c14 348 }
ejh23 1:e11018cf2c14 349 }
ejh23 1:e11018cf2c14 350 }
ejh23 1:e11018cf2c14 351
ejh23 1:e11018cf2c14 352 // function to clear the screen buffer
ejh23 1:e11018cf2c14 353 void N5110::clear()
ejh23 1:e11018cf2c14 354 {
ejh23 1:e11018cf2c14 355 memset(buffer,0,sizeof(buffer));
ejh23 1:e11018cf2c14 356 }
ejh23 1:e11018cf2c14 357
ejh23 1:e11018cf2c14 358 // function to plot array on display
ejh23 1:e11018cf2c14 359 void N5110::plotArray(float const array[])
ejh23 1:e11018cf2c14 360 {
ejh23 1:e11018cf2c14 361 for (int i=0; i<WIDTH; i++) { // loop through array
ejh23 1:e11018cf2c14 362 // elements are normalised from 0.0 to 1.0, so multiply
ejh23 1:e11018cf2c14 363 // by 47 to convert to pixel range, and subtract from 47
ejh23 1:e11018cf2c14 364 // since top-left is 0,0 in the display geometry
ejh23 1:e11018cf2c14 365 setPixel(i,47 - int(array[i]*47.0f),true);
ejh23 1:e11018cf2c14 366 }
ejh23 1:e11018cf2c14 367
ejh23 1:e11018cf2c14 368 }
ejh23 1:e11018cf2c14 369
ejh23 1:e11018cf2c14 370 // function to draw circle
ejh23 1:e11018cf2c14 371 void N5110:: drawCircle(unsigned int const x0,
ejh23 1:e11018cf2c14 372 unsigned int const y0,
ejh23 1:e11018cf2c14 373 unsigned int const radius,
ejh23 1:e11018cf2c14 374 FillType const fill)
ejh23 1:e11018cf2c14 375 {
ejh23 1:e11018cf2c14 376 // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
ejh23 1:e11018cf2c14 377 int x = radius;
ejh23 1:e11018cf2c14 378 int y = 0;
ejh23 1:e11018cf2c14 379 int radiusError = 1-x;
ejh23 1:e11018cf2c14 380
ejh23 1:e11018cf2c14 381 while(x >= y) {
ejh23 1:e11018cf2c14 382
ejh23 1:e11018cf2c14 383 // if transparent, just draw outline
ejh23 1:e11018cf2c14 384 if (fill == FILL_TRANSPARENT) {
ejh23 1:e11018cf2c14 385 setPixel( x + x0, y + y0,true);
ejh23 1:e11018cf2c14 386 setPixel(-x + x0, y + y0,true);
ejh23 1:e11018cf2c14 387 setPixel( y + x0, x + y0,true);
ejh23 1:e11018cf2c14 388 setPixel(-y + x0, x + y0,true);
ejh23 1:e11018cf2c14 389 setPixel(-y + x0, -x + y0,true);
ejh23 1:e11018cf2c14 390 setPixel( y + x0, -x + y0,true);
ejh23 1:e11018cf2c14 391 setPixel( x + x0, -y + y0,true);
ejh23 1:e11018cf2c14 392 setPixel(-x + x0, -y + y0,true);
ejh23 1:e11018cf2c14 393 } else { // drawing filled circle, so draw lines between points at same y value
ejh23 1:e11018cf2c14 394
ejh23 1:e11018cf2c14 395 int type = (fill==FILL_BLACK) ? 1:0; // black or white fill
ejh23 1:e11018cf2c14 396
ejh23 1:e11018cf2c14 397 drawLine(x+x0,y+y0,-x+x0,y+y0,type);
ejh23 1:e11018cf2c14 398 drawLine(y+x0,x+y0,-y+x0,x+y0,type);
ejh23 1:e11018cf2c14 399 drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
ejh23 1:e11018cf2c14 400 drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
ejh23 1:e11018cf2c14 401 }
ejh23 1:e11018cf2c14 402
ejh23 1:e11018cf2c14 403 y++;
ejh23 1:e11018cf2c14 404 if (radiusError<0) {
ejh23 1:e11018cf2c14 405 radiusError += 2 * y + 1;
ejh23 1:e11018cf2c14 406 } else {
ejh23 1:e11018cf2c14 407 x--;
ejh23 1:e11018cf2c14 408 radiusError += 2 * (y - x) + 1;
ejh23 1:e11018cf2c14 409 }
ejh23 1:e11018cf2c14 410 }
ejh23 1:e11018cf2c14 411
ejh23 1:e11018cf2c14 412 }
ejh23 1:e11018cf2c14 413
ejh23 1:e11018cf2c14 414 void N5110::drawLine(unsigned int const x0,
ejh23 1:e11018cf2c14 415 unsigned int const y0,
ejh23 1:e11018cf2c14 416 unsigned int const x1,
ejh23 1:e11018cf2c14 417 unsigned int const y1,
ejh23 1:e11018cf2c14 418 unsigned int const type)
ejh23 1:e11018cf2c14 419 {
ejh23 1:e11018cf2c14 420 // Note that the ranges can be negative so we have to turn the input values
ejh23 1:e11018cf2c14 421 // into signed integers first
ejh23 1:e11018cf2c14 422 int const y_range = static_cast<int>(y1) - static_cast<int>(y0);
ejh23 1:e11018cf2c14 423 int const x_range = static_cast<int>(x1) - static_cast<int>(x0);
ejh23 1:e11018cf2c14 424
ejh23 1:e11018cf2c14 425 // if dotted line, set step to 2, else step is 1
ejh23 1:e11018cf2c14 426 unsigned int const step = (type==2) ? 2:1;
ejh23 1:e11018cf2c14 427
ejh23 1:e11018cf2c14 428 // make sure we loop over the largest range to get the most pixels on the display
ejh23 1:e11018cf2c14 429 // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
ejh23 1:e11018cf2c14 430 // or else we'll only end up with 1 pixel in the x column
ejh23 1:e11018cf2c14 431 if ( abs(x_range) > abs(y_range) ) {
ejh23 1:e11018cf2c14 432
ejh23 1:e11018cf2c14 433 // ensure we loop from smallest to largest or else for-loop won't run as expected
ejh23 1:e11018cf2c14 434 unsigned int const start = x_range > 0 ? x0:x1;
ejh23 1:e11018cf2c14 435 unsigned int const stop = x_range > 0 ? x1:x0;
ejh23 1:e11018cf2c14 436
ejh23 1:e11018cf2c14 437 // loop between x pixels
ejh23 1:e11018cf2c14 438 for (unsigned int x = start; x<= stop ; x+=step) {
ejh23 1:e11018cf2c14 439 // do linear interpolation
ejh23 1:e11018cf2c14 440 int const dx = static_cast<int>(x)-static_cast<int>(x0);
ejh23 1:e11018cf2c14 441 unsigned int const y = y0 + y_range * dx / x_range;
ejh23 1:e11018cf2c14 442
ejh23 1:e11018cf2c14 443 // If the line type is '0', this will clear the pixel
ejh23 1:e11018cf2c14 444 // If it is '1' or '2', the pixel will be set
ejh23 1:e11018cf2c14 445 setPixel(x,y, type);
ejh23 1:e11018cf2c14 446 }
ejh23 1:e11018cf2c14 447 } else {
ejh23 1:e11018cf2c14 448
ejh23 1:e11018cf2c14 449 // ensure we loop from smallest to largest or else for-loop won't run as expected
ejh23 1:e11018cf2c14 450 unsigned int const start = y_range > 0 ? y0:y1;
ejh23 1:e11018cf2c14 451 unsigned int const stop = y_range > 0 ? y1:y0;
ejh23 1:e11018cf2c14 452
ejh23 1:e11018cf2c14 453 for (unsigned int y = start; y<= stop ; y+=step) {
ejh23 1:e11018cf2c14 454 // do linear interpolation
ejh23 1:e11018cf2c14 455 int const dy = static_cast<int>(y)-static_cast<int>(y0);
ejh23 1:e11018cf2c14 456 unsigned int const x = x0 + x_range * dy / y_range;
ejh23 1:e11018cf2c14 457
ejh23 1:e11018cf2c14 458 // If the line type is '0', this will clear the pixel
ejh23 1:e11018cf2c14 459 // If it is '1' or '2', the pixel will be set
ejh23 1:e11018cf2c14 460 setPixel(x,y, type);
ejh23 1:e11018cf2c14 461 }
ejh23 1:e11018cf2c14 462 }
ejh23 1:e11018cf2c14 463
ejh23 1:e11018cf2c14 464 }
ejh23 1:e11018cf2c14 465
ejh23 1:e11018cf2c14 466 void N5110::drawRect(unsigned int const x0,
ejh23 1:e11018cf2c14 467 unsigned int const y0,
ejh23 1:e11018cf2c14 468 unsigned int const width,
ejh23 1:e11018cf2c14 469 unsigned int const height,
ejh23 1:e11018cf2c14 470 FillType const fill)
ejh23 1:e11018cf2c14 471 {
ejh23 1:e11018cf2c14 472 if (fill == FILL_TRANSPARENT) { // transparent, just outline
ejh23 1:e11018cf2c14 473 drawLine(x0,y0,x0+(width-1),y0,1); // top
ejh23 1:e11018cf2c14 474 drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),1); // bottom
ejh23 1:e11018cf2c14 475 drawLine(x0,y0,x0,y0+(height-1),1); // left
ejh23 1:e11018cf2c14 476 drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),1); // right
ejh23 1:e11018cf2c14 477 } else { // filled rectangle
ejh23 1:e11018cf2c14 478 int type = (fill==FILL_BLACK) ? 1:0; // black or white fill
ejh23 1:e11018cf2c14 479 for (int y = y0; y<y0+height; y++) { // loop through rows of rectangle
ejh23 1:e11018cf2c14 480 drawLine(x0,y,x0+(width-1),y,type); // draw line across screen
ejh23 1:e11018cf2c14 481 }
ejh23 1:e11018cf2c14 482 }
ejh23 1:e11018cf2c14 483 }
ejh23 1:e11018cf2c14 484
ejh23 1:e11018cf2c14 485 void N5110::drawSprite(int x0,
ejh23 1:e11018cf2c14 486 int y0,
ejh23 1:e11018cf2c14 487 int nrows,
ejh23 1:e11018cf2c14 488 int ncols,
ejh23 1:e11018cf2c14 489 int *sprite)
ejh23 1:e11018cf2c14 490 {
ejh23 1:e11018cf2c14 491 for (int i = 0; i < nrows; i++) {
ejh23 1:e11018cf2c14 492 for (int j = 0 ; j < ncols ; j++) {
ejh23 1:e11018cf2c14 493
ejh23 1:e11018cf2c14 494 int pixel = *((sprite+i*ncols)+j);
ejh23 1:e11018cf2c14 495 setPixel(x0+j,y0+i, pixel);
ejh23 1:e11018cf2c14 496 }
ejh23 1:e11018cf2c14 497 }
ejh23 1:e11018cf2c14 498 }