test

Dependencies:   mbed FXOS8700CQ

Committer:
Neowless
Date:
Fri May 15 20:36:00 2020 +0000
Revision:
4:c7dc43515215
Parent:
1:48b0bf0bcda8
test;

Who changed what in which revision?

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