Laila Al Badwawi
200906179
SpaceInvaders
I declare this my own independent work and understand the university rules on plagiarism.
N5110 Class.
More...
#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 | normalMode () |
| Turn on normal video mode (default) Black on white.
|
void | inverseMode () |
| Turn on inverse video mode (default) White on black.
|
void | setBrightness (float const brightness) |
| 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) |
| Set a Pixel.
|
void | clearPixel (unsigned int const x, unsigned int const y) |
| 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.
|
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
- Author:
- Craig A. Evans
- Date:
- 7th February 2017
#include "mbed.h"
#include "N5110.h"
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
int main()
{
lcd.init();
while(1) {
lcd.normalMode();
lcd.setBrightness(0.5);
lcd.clear();
lcd.printString("Hello, World!",0,0);
char buffer[14];
int temperature = 27;
int length = sprintf(buffer,"T = %2d C",temperature);
if (length <= 14)
lcd.printString(buffer,0,1);
float pressure = 1012.3;
length = sprintf(buffer,"P = %.2f mb",pressure);
if (length <= 14)
lcd.printString(buffer,0,2);
lcd.printChar('X',5,3);
for (int i = 0; i < WIDTH; i++) {
lcd.setPixel(i,40);
}
lcd.refresh();
wait(5.0);
lcd.clear();
lcd.setPixel(2,2);
lcd.refresh();
wait(1.0);
int pixel_to_test = lcd.getPixel(2,2);
printf("2,2 Pixel value = %i\n",pixel_to_test);
if ( pixel_to_test ) {
lcd.printString("2,2 is set",0,4);
}
pixel_to_test = lcd.getPixel(3,3);
printf("3,3 Pixel value = %i\n",pixel_to_test);
if ( pixel_to_test == 0 ) {
lcd.printString("3,3 is clear",0,5);
}
lcd.refresh();
wait(4.0);
lcd.clear();
lcd.inverseMode();
lcd.setBrightness(1.0);
float array[84];
for (int i = 0; i < 84; i++) {
array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
}
lcd.plotArray(array);
lcd.refresh();
wait(5.0);
lcd.clear();
lcd.normalMode();
lcd.setBrightness(0.5);
for (int x = 0; x < WIDTH ; x+=10) {
lcd.drawLine(0,0,x,HEIGHT,2);
}
lcd.refresh();
wait(5.0);
lcd.clear();
lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK);
lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE);
lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT);
lcd.refresh();
wait(5.0);
lcd.clear();
lcd.drawRect(10,10,50,30,FILL_BLACK);
lcd.drawRect(15,15,20,10,FILL_WHITE);
lcd.drawRect(2,2,70,40, FILL_TRANSPARENT);
lcd.refresh();
wait(5.0);
}
}
Definition at line 180 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) |
Definition at line 7 of file N5110.cpp.
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) |
Definition at line 26 of file N5110.cpp.
Free allocated memory when object goes out of scope.
Definition at line 41 of file N5110.cpp.
Member Function Documentation
Clear.
Clears the screen buffer.
Definition at line 304 of file N5110.cpp.
void clearPixel |
( |
unsigned int const |
x, |
|
|
unsigned int const |
y |
|
) |
| |
Clear a Pixel.
This function clears pixel in the screen buffer
- Parameters:
-
x | - the x co-ordinate of the pixel (0 to 83) |
y | - the y co-ordinate of the pixel (0 to 47) |
Definition at line 202 of file N5110.cpp.
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.
- See also:
- http://en.wikipedia.org/wiki/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 |
Definition at line 322 of file N5110.cpp.
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 |
Definition at line 365 of file N5110.cpp.
void drawRect |
( |
unsigned int const |
x0, |
|
|
unsigned int const |
y0, |
|
|
unsigned int const |
width, |
|
|
unsigned int const |
height, |
|
|
FillType const |
fill |
|
) |
| |
Draw Rectangle.
This function draws a rectangle.
- Parameters:
-
x0 | - x-coordinate of origin (top-left) |
y0 | - y-coordinate of origin (top-left) |
width | - width of rectangle |
height | - height of rectangle |
fill | - fill-type for the shape |
Definition at line 417 of file N5110.cpp.
int getPixel |
( |
unsigned int const |
x, |
|
|
unsigned int const |
y |
|
) |
| const |
Get a Pixel.
This function gets the status of a pixel in the screen buffer.
- Parameters:
-
x | - the x co-ordinate of the pixel (0 to 83) |
y | - the y co-ordinate of the pixel (0 to 47) |
- Returns:
- 0 - pixel is clear 1 - pixel is set
Definition at line 211 of file N5110.cpp.
Initialise display.
Powers up the display and turns on backlight (50% brightness default). Sets the display up in horizontal addressing mode and with normal video mode.
Definition at line 56 of file N5110.cpp.
Turn on inverse video mode (default) White on black.
Definition at line 87 of file N5110.cpp.
Turn on normal video mode (default) Black on white.
Definition at line 81 of file N5110.cpp.
void plotArray |
( |
float const |
array[] ) |
|
Plot Array.
This function plots a one-dimensional array in the buffer.
- Parameters:
-
array[] | - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. |
Definition at line 310 of file N5110.cpp.
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 |
Definition at line 261 of file N5110.cpp.
void printString |
( |
char const * |
str, |
|
|
unsigned int const |
x, |
|
|
unsigned int const |
y |
|
) |
| |
Print String.
Prints a string of characters to the screen buffer. String is cut-off after the 83rd pixel.
- Parameters:
-
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 |
Definition at line 279 of file N5110.cpp.
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.
Definition at line 249 of file N5110.cpp.
Refresh display.
This functions sends the screen buffer to the display.
Definition at line 230 of file N5110.cpp.
void setBrightness |
( |
float const |
brightness ) |
|
Set Brightness.
Sets brightness of LED backlight.
- Parameters:
-
brightness | - float in range 0.0 to 1.0 |
Definition at line 124 of file N5110.cpp.
void setPixel |
( |
unsigned int const |
x, |
|
|
unsigned int const |
y |
|
) |
| |
Set a Pixel.
This function sets a pixel in the screen buffer.
- Parameters:
-
x | - the x co-ordinate of the pixel (0 to 83) |
y | - the y co-ordinate of the pixel (0 to 47) |
Definition at line 193 of file N5110.cpp.
Turn off.
Powers down the display and turns of the backlight. Needs to be reinitialised before being re-used.
Definition at line 101 of file N5110.cpp.