Pong game for ELEC1620 board.
N5110 Class Reference
#include <N5110.h>
Public Member Functions | |
N5110 (PinName const pwrPin, PinName const scePin, PinName const rstPin, PinName const dcPin, PinName const mosiPin, PinName const sclkPin, PinName const ledPin) | |
Create a N5110 object connected to the specified pins. | |
N5110 (PinName const scePin, PinName const rstPin, PinName const dcPin, PinName const mosiPin, PinName const sclkPin, PinName const ledPin) | |
Create a N5110 object connected to the specified pins (Vcc to +3V3) | |
~N5110 () | |
Free allocated memory when object goes out of scope. | |
void | init () |
Initialise display. | |
void | turnOff () |
Turn off. | |
void | clear () |
Clear. | |
void | setContrast (float contrast) |
Set screen constrast. | |
void | normalMode () |
Turn on normal video mode (default) Black on white. | |
void | inverseMode () |
Turn on inverse video mode (default) White on black. | |
void | backLightOn () |
Backlight On. | |
void | backLightOff () |
Set Brightness. | |
void | printString (char const *str, unsigned int const x, unsigned int const y) |
Print String. | |
void | printChar (char const c, unsigned int const x, unsigned int const y) |
Print Character. | |
void | setPixel (unsigned int const x, unsigned int const y, bool const state=true) |
Set a Pixel. | |
void | clearPixel (unsigned int const x, unsigned int const y) __attribute__((deprecated("Use setPixel(x |
Clear a Pixel. | |
int | getPixel (unsigned int const x, unsigned int const y) const |
Get a Pixel. | |
void | refresh () |
Refresh display. | |
void | randomiseBuffer () |
Randomise buffer. | |
void | plotArray (float const array[]) |
Plot Array. | |
void | drawCircle (unsigned int const x0, unsigned int const y0, unsigned int const radius, FillType const fill) |
Draw Circle. | |
void | drawLine (unsigned int const x0, unsigned int const y0, unsigned int const x1, unsigned int const y1, unsigned int const type) |
Draw Line. | |
void | drawRect (unsigned int const x0, unsigned int const y0, unsigned int const width, unsigned int const height, FillType const fill) |
Draw Rectangle. | |
void | drawSprite (int x0, int y0, int nrows, int ncols, int *sprite) |
Draw Sprite. |
Detailed Description
N5110 Class.
Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. 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). Can print characters and strings to the display using the included 5x7 font. The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. The library can print primitive shapes (lines, circles, rectangles) Acknowledgements to Chris Yan's Nokia_5110 Library.
Revision 1.3
- Date:
- 7th February 2017
#include "mbed.h" #include "N5110.h" // rows,cols int sprite[8][5] = { { 0,0,1,0,0 }, { 0,1,1,1,0 }, { 0,0,1,0,0 }, { 0,1,1,1,0 }, { 1,1,1,1,1 }, { 1,1,1,1,1 }, { 1,1,0,1,1 }, { 1,1,0,1,1 }, }; // VCC,SCE,RST,D/C,MOSI,SCLK,LED //N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO N5110 lcd(p8,p9,p10,p11,p13,p21); // LPC1768 - powered from +3V3 - JP1 in 2/3 position //N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 int main() { // first need to initialise display lcd.init(); // change set contrast in range 0.0 to 1.0 // 0.4 appears to be a good starting point lcd.setContrast(0.4); while(1) { // these are default settings so not strictly needed lcd.normalMode(); // normal colour mode lcd.clear(); // x origin, y origin, rows, cols, sprite lcd.drawSprite(20,6,8,5,(int *)sprite); lcd.refresh(); wait(5.0); lcd.clear(); // clear buffer at start of every loop // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) lcd.printString("Hello, World!",0,0); char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) // so can display a string of a maximum 14 characters in length // or create formatted strings - ensure they aren't more than 14 characters long int temperature = 27; int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer // it is important the format specifier ensures the length will fit in the buffer if (length <= 14) // if string will fit on display (assuming printing at x=0) lcd.printString(buffer,0,1); // display on screen float pressure = 1012.3; // same idea with floats length = sprintf(buffer,"P = %.2f mb",pressure); if (length <= 14) lcd.printString(buffer,0,2); // can also print individual characters at specified place lcd.printChar('X',5,3); // draw a line across the display at y = 40 pixels (origin top-left) for (int i = 0; i < WIDTH; i++) { lcd.setPixel(i,40,true); } // need to refresh display after setting pixels or writing strings lcd.refresh(); wait(5.0); // can check status of pixel using getPixel(x,y); lcd.clear(); // clear buffer lcd.setPixel(2,2,true); // set random pixel in buffer lcd.refresh(); wait(1.0); int pixel_to_test = lcd.getPixel(2,2); if ( pixel_to_test ) { lcd.printString("2,2 is set",0,4); } // this one shouldn't be set lcd.setPixel(3,3,false); // clear random pixel in buffer lcd.refresh(); pixel_to_test = lcd.getPixel(3,3); if ( pixel_to_test == 0 ) { lcd.printString("3,3 is clear",0,5); } lcd.refresh(); wait(4.0); lcd.clear(); // clear buffer lcd.inverseMode(); // invert colours lcd.setBrightness(1.0); // put LED backlight on full float array[84]; for (int i = 0; i < 84; i++) { array[i] = 0.5 + 0.5*sin(i*2*3.14/84); } // can also plot graphs - 84 elements only // values must be in range 0.0 - 1.0 lcd.plotArray(array); lcd.refresh(); wait(5.0); lcd.clear(); lcd.normalMode(); // normal colour mode back lcd.setBrightness(0.5); // put LED backlight on 50% // example of drawing lines for (int x = 0; x < WIDTH ; x+=10) { // x0,y0,x1,y1,type 0-white,1-black,2-dotted lcd.drawLine(0,0,x,HEIGHT,2); } lcd.refresh(); // refresh after drawing shapes wait(5.0); lcd.clear(); // example of how to draw circles lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK); // x,y,radius,black fill lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE); // x,y,radius,white fill lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT); // x,y,radius,transparent with outline lcd.refresh(); // refresh after drawing shapes wait(5.0); lcd.clear(); // example of how to draw rectangles // origin x,y,width,height,type lcd.drawRect(10,10,50,30,FILL_BLACK); // filled black rectangle lcd.drawRect(15,15,20,10,FILL_WHITE); // filled white rectange (no outline) lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); // transparent, just outline lcd.refresh(); // refresh after drawing shapes wait(5.0); } }
Definition at line 178 of file N5110.h.
Constructor & Destructor Documentation
N5110 | ( | PinName const | pwrPin, |
PinName const | scePin, | ||
PinName const | rstPin, | ||
PinName const | dcPin, | ||
PinName const | mosiPin, | ||
PinName const | sclkPin, | ||
PinName const | ledPin | ||
) |
Create a N5110 object connected to the specified pins.
- Parameters:
-
pwr Pin connected to Vcc on the LCD display (pin 1) sce Pin connected to chip enable (pin 3) rst Pin connected to reset (pin 4) dc Pin connected to data/command select (pin 5) mosi Pin connected to data input (MOSI) (pin 6) sclk Pin connected to serial clock (SCLK) (pin 7) led Pin connected to LED backlight (must be PWM) (pin 8)
N5110 | ( | PinName const | scePin, |
PinName const | rstPin, | ||
PinName const | dcPin, | ||
PinName const | mosiPin, | ||
PinName const | sclkPin, | ||
PinName const | ledPin | ||
) |
Create a N5110 object connected to the specified pins (Vcc to +3V3)
- Parameters:
-
sce Pin connected to chip enable (pin 3) rst Pin connected to reset (pin 4) dc Pin connected to data/command select (pin 5) mosi Pin connected to data input (MOSI) (pin 6) sclk Pin connected to serial clock (SCLK) (pin 7) led Pin connected to LED backlight (must be PWM) (pin 8)
~N5110 | ( | ) |
Member Function Documentation
void clearPixel | ( | unsigned int const | x, |
unsigned int const | y | ||
) |
void drawCircle | ( | unsigned int const | x0, |
unsigned int const | y0, | ||
unsigned int const | radius, | ||
FillType const | fill | ||
) |
Draw Circle.
This function draws a circle at the specified origin with specified radius in the screen buffer Uses the midpoint circle algorithm.
- Parameters:
-
x0 - x-coordinate of centre y0 - y-coordinate of centre radius - radius of circle in pixels fill - fill-type for the shape
void drawLine | ( | unsigned int const | x0, |
unsigned int const | y0, | ||
unsigned int const | x1, | ||
unsigned int const | y1, | ||
unsigned int const | type | ||
) |
Draw Line.
This function draws a line between the specified points using linear interpolation.
- Parameters:
-
x0 - x-coordinate of first point y0 - y-coordinate of first point x1 - x-coordinate of last point y1 - y-coordinate of last point type - 0 white,1 black,2 dotted
void drawRect | ( | unsigned int const | x0, |
unsigned int const | y0, | ||
unsigned int const | width, | ||
unsigned int const | height, | ||
FillType const | fill | ||
) |
void drawSprite | ( | int | x0, |
int | y0, | ||
int | nrows, | ||
int | ncols, | ||
int * | sprite | ||
) |
Draw Sprite.
This function draws a sprite as defined in a 2D array
- Parameters:
-
x0 - x-coordinate of origin (top-left) y0 - y-coordinate of origin (top-left) nrows - number of rows in sprite ncols - number of columns in sprite sprite - 2D array representing the sprite
int getPixel | ( | unsigned int const | x, |
unsigned int const | y | ||
) | const |
void init | ( | ) |
void inverseMode | ( | ) |
void normalMode | ( | ) |
void plotArray | ( | float const | array[] ) |
void printChar | ( | char const | c, |
unsigned int const | x, | ||
unsigned int const | y | ||
) |
Print Character.
Sends a character to the screen buffer. Printed at the specified location. Character is cut-off after the 83rd pixel.
- Parameters:
-
c - the character to print. Can print ASCII as so printChar('C'). x - the column number (0 to 83) y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
void printString | ( | char const * | str, |
unsigned int const | x, | ||
unsigned int const | y | ||
) |
void randomiseBuffer | ( | ) |
Randomise buffer.
This function fills the buffer with random data. Can be used to test the display. A call to refresh() must be made to update the display to reflect the change in pixels. The seed is not set and so the generated pattern will probably be the same each time. TODO: Randomise the seed - maybe using the noise on the AnalogIn pins.
void refresh | ( | ) |
void setContrast | ( | float | contrast ) |
void setPixel | ( | unsigned int const | x, |
unsigned int const | y, | ||
bool const | state = true |
||
) |
Set a Pixel.
- Parameters:
-
x The x co-ordinate of the pixel (0 to 83) y The y co-ordinate of the pixel (0 to 47) state The state of the pixel [true=black (default), false=white]
This function sets the state of a pixel in the screen buffer. The third parameter can be omitted,
Generated on Thu Jul 14 2022 15:46:36 by 1.7.2