Library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website).

Dependents:   LV7_LCDtest LV7_Grupa5_Tim003_Zadatak1 lv7_Grupa5_Tim008_zad1 LV7_PAI_Grupa5_tim10_Zadatak1 ... more

This library is designed to make it easy to interface an mbed with a Nokia 5110 LCD display.

These can be found at Sparkfun (https://www.sparkfun.com/products/10168) and Adafruit (http://www.adafruit.com/product/338).

The library uses the SPI peripheral on the mbed which means it is much faster sending data to the display than other libraries available on other platforms that use software SPI.

The library can print strings as well as controlling individual pixels, meaning that both text and primitive graphics can be displayed.

Committer:
eencae
Date:
Mon Mar 19 13:44:23 2018 +0000
Revision:
49:93355c01e261
Parent:
42:596c207519de
Changed default contrast (0.55). This seems to work better with the majority of screens.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
valavanisalex 38:92fad278c2c3 1 #include "Bitmap.h"
valavanisalex 38:92fad278c2c3 2
valavanisalex 38:92fad278c2c3 3 #include <iostream>
valavanisalex 38:92fad278c2c3 4
valavanisalex 40:c9262294f2e1 5 #include "N5110.h"
valavanisalex 40:c9262294f2e1 6
valavanisalex 40:c9262294f2e1 7 Bitmap::Bitmap(int const *contents,
valavanisalex 38:92fad278c2c3 8 unsigned int const height,
valavanisalex 38:92fad278c2c3 9 unsigned int const width)
valavanisalex 38:92fad278c2c3 10 :
valavanisalex 40:c9262294f2e1 11 _contents(std::vector<int>(height*width)),
valavanisalex 38:92fad278c2c3 12 _height(height),
valavanisalex 38:92fad278c2c3 13 _width(width)
valavanisalex 38:92fad278c2c3 14 {
valavanisalex 38:92fad278c2c3 15 // Perform a quick sanity check of the dimensions
valavanisalex 41:6c046786be6c 16 if (_contents.size() != height * width) {
valavanisalex 40:c9262294f2e1 17 std::cerr << "Contents of bitmap has size " << _contents.size()
valavanisalex 38:92fad278c2c3 18 << " pixels, but its dimensions were specified as "
valavanisalex 41:6c046786be6c 19 << width << " * " << height << " = " << width * height << std::endl;
valavanisalex 38:92fad278c2c3 20 }
valavanisalex 40:c9262294f2e1 21
valavanisalex 40:c9262294f2e1 22 for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
valavanisalex 38:92fad278c2c3 23 }
valavanisalex 38:92fad278c2c3 24
valavanisalex 40:c9262294f2e1 25 /**
valavanisalex 40:c9262294f2e1 26 * @returns the value of the pixel at the given position
valavanisalex 40:c9262294f2e1 27 */
valavanisalex 38:92fad278c2c3 28 int Bitmap::get_pixel(unsigned int const row,
valavanisalex 38:92fad278c2c3 29 unsigned int const column) const
valavanisalex 38:92fad278c2c3 30 {
valavanisalex 38:92fad278c2c3 31 // First check that row and column indices are within bounds
valavanisalex 38:92fad278c2c3 32 if(column >= _width || row >= _height)
valavanisalex 38:92fad278c2c3 33 {
valavanisalex 38:92fad278c2c3 34 std::cerr << "The requested pixel with index " << row << "," << column
valavanisalex 38:92fad278c2c3 35 << "is outside the bitmap dimensions: " << _width << ","
valavanisalex 41:6c046786be6c 36 << _height << std::endl;
valavanisalex 38:92fad278c2c3 37 }
valavanisalex 38:92fad278c2c3 38
valavanisalex 38:92fad278c2c3 39 // Now return the pixel value, using row-major indexing
valavanisalex 38:92fad278c2c3 40 return _contents[row * _width + column];
valavanisalex 40:c9262294f2e1 41 }
valavanisalex 40:c9262294f2e1 42
valavanisalex 40:c9262294f2e1 43 /**
valavanisalex 40:c9262294f2e1 44 * @brief Prints the contents of the bitmap to the terminal
valavanisalex 40:c9262294f2e1 45 */
valavanisalex 40:c9262294f2e1 46 void Bitmap::print() const
valavanisalex 40:c9262294f2e1 47 {
valavanisalex 40:c9262294f2e1 48 for (unsigned int row = 0; row < _height; ++row)
valavanisalex 40:c9262294f2e1 49 {
valavanisalex 40:c9262294f2e1 50 // Print each element of the row
valavanisalex 40:c9262294f2e1 51 for (unsigned int column = 0; column < _width; ++column)
valavanisalex 40:c9262294f2e1 52 {
valavanisalex 40:c9262294f2e1 53 int pixel = get_pixel(row, column);
valavanisalex 40:c9262294f2e1 54 std::cout << pixel;
valavanisalex 40:c9262294f2e1 55 }
valavanisalex 40:c9262294f2e1 56
valavanisalex 40:c9262294f2e1 57 // And then terminate with a new-line character
valavanisalex 40:c9262294f2e1 58 std::cout << std::endl;
valavanisalex 40:c9262294f2e1 59 }
valavanisalex 40:c9262294f2e1 60 }
valavanisalex 40:c9262294f2e1 61
valavanisalex 40:c9262294f2e1 62 /**
valavanisalex 40:c9262294f2e1 63 * @brief Renders the contents of the bitmap onto an N5110 screen
valavanisalex 40:c9262294f2e1 64 *
valavanisalex 40:c9262294f2e1 65 * @param[in] lcd The screen to use for rendering
valavanisalex 40:c9262294f2e1 66 * @param[in] x0 The horizontal position in pixels at which to render the bitmap
valavanisalex 40:c9262294f2e1 67 * @param[in] y0 The vertical position in pixels at which to render the bitmap
valavanisalex 40:c9262294f2e1 68 *
valavanisalex 40:c9262294f2e1 69 * @details Note that x0, y0 gives the location of the top-left of the bitmap on
valavanisalex 40:c9262294f2e1 70 * the screen.
valavanisalex 40:c9262294f2e1 71 * This function only updates the buffer on the screen. You still need
valavanisalex 40:c9262294f2e1 72 * to refresh the screen in order to actually see the bitmap.
valavanisalex 40:c9262294f2e1 73 */
valavanisalex 40:c9262294f2e1 74 void Bitmap::render(N5110 &lcd,
valavanisalex 40:c9262294f2e1 75 unsigned int const x0,
valavanisalex 40:c9262294f2e1 76 unsigned int const y0) const
valavanisalex 40:c9262294f2e1 77 {
valavanisalex 40:c9262294f2e1 78 // Loop through each row of the bitmap image
valavanisalex 40:c9262294f2e1 79 for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
valavanisalex 40:c9262294f2e1 80 {
valavanisalex 40:c9262294f2e1 81 // Row index on the screen for rendering the row of pixels
valavanisalex 40:c9262294f2e1 82 unsigned int screen_row = y0 + bitmap_row;
valavanisalex 40:c9262294f2e1 83
valavanisalex 40:c9262294f2e1 84 // Render each pixel in the row
valavanisalex 40:c9262294f2e1 85 for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
valavanisalex 40:c9262294f2e1 86 {
valavanisalex 40:c9262294f2e1 87 // Column index on the screen for rendering this pixel
valavanisalex 40:c9262294f2e1 88 int screen_col = x0 + bitmap_col;
valavanisalex 40:c9262294f2e1 89
valavanisalex 42:596c207519de 90 // Find the required value of the pixel at the given location within
valavanisalex 42:596c207519de 91 // the bitmap data and then write it to the LCD screen
valavanisalex 40:c9262294f2e1 92 int pixel = get_pixel(bitmap_row, bitmap_col);
valavanisalex 42:596c207519de 93 lcd.setPixel(screen_col, screen_row, pixel);
valavanisalex 40:c9262294f2e1 94 }
valavanisalex 40:c9262294f2e1 95 }
valavanisalex 38:92fad278c2c3 96 }