Library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website). Used & amended by D.Leaming, University of Lincoln, December 2021 v01

Committer:
legstar85
Date:
Fri Dec 17 08:26:58 2021 +0000
Revision:
53:200be703ee3b
Parent:
42:596c207519de
Used & amended by D.Leaming, University of Lincoln, December 2021

Who changed what in which revision?

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