Final tidy of code following installation of new sensor, more comments added prior to submission

Dependencies:   mbed

Committer:
legstar85
Date:
Fri Feb 04 09:20:18 2022 +0000
Revision:
18:fc63b51a0302
Parent:
13:5ad65a688f3f
Final tidy of code following installation of new sensor, more comments added prior to submission

Who changed what in which revision?

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