(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Final_Project_V15_DLeaming_25574043

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bitmap.cpp Source File

Bitmap.cpp

00001 /* * Print String
00002 *
00003 * Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
00004 * @param x - the column number (0 to 83)
00005 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
00006 * @author - David Leaming - 25574043
00007 * @ Date - December 2021
00008 *
00009 * Acknowledgements 
00010 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
00011 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
00012 *
00013 */ 
00014 
00015 #include "Bitmap.h"
00016 
00017 #include <iostream>
00018 
00019 #include "N5110.h"
00020 
00021 Bitmap::Bitmap(int const               *contents,
00022                unsigned int const       height,
00023                unsigned int const       width)
00024     :
00025     _contents(std::vector<int>(height*width)),
00026     _height(height),
00027     _width(width)
00028 {
00029     // Perform a quick sanity check of the dimensions
00030     if (_contents.size() != height * width) {
00031         std::cerr << "Contents of bitmap has size " << _contents.size()
00032                   << " pixels, but its dimensions were specified as "
00033                   << width << " * " << height << " = " << width * height << std::endl;
00034     }
00035 
00036     for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
00037 }
00038 
00039 /**
00040  * @returns the value of the pixel at the given position
00041  */
00042 int Bitmap::get_pixel (unsigned int const row,
00043                       unsigned int const column) const
00044 {
00045     // First check that row and column indices are within bounds
00046     if(column >= _width || row >= _height)
00047     {
00048         std::cerr << "The requested pixel with index " << row << "," << column
00049                   << "is outside the bitmap dimensions: " << _width << ","
00050                   << _height << std::endl;
00051     }
00052 
00053     // Now return the pixel value, using row-major indexing
00054     return _contents[row * _width + column];
00055 }
00056 
00057 /**
00058  * @brief Prints the contents of the bitmap to the terminal
00059  */
00060 void Bitmap::print() const
00061 {
00062     for (unsigned int row = 0; row < _height; ++row)
00063     {
00064         // Print each element of the row
00065         for (unsigned int column = 0; column < _width; ++column)
00066         {
00067             int pixel = get_pixel (row, column);
00068             std::cout << pixel;
00069         }
00070 
00071         // And then terminate with a new-line character
00072         std::cout << std::endl;
00073     }
00074 }
00075 
00076 /**
00077  * @brief Renders the contents of the bitmap onto an N5110 screen
00078  *
00079  * @param[in] lcd The screen to use for rendering
00080  * @param[in] x0  The horizontal position in pixels at which to render the bitmap
00081  * @param[in] y0  The vertical position in pixels at which to render the bitmap
00082  *
00083  * @details Note that x0, y0 gives the location of the top-left of the bitmap on
00084  *          the screen.
00085  *          This function only updates the buffer on the screen.  You still need
00086  *          to refresh the screen in order to actually see the bitmap.
00087  */
00088 void Bitmap::render(N5110 &lcd,
00089                     unsigned int const x0,
00090                     unsigned int const y0) const
00091 {
00092     // Loop through each row of the bitmap image
00093     for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
00094     {
00095         // Row index on the screen for rendering the row of pixels
00096         unsigned int screen_row = y0 + bitmap_row;
00097                 
00098         // Render each pixel in the row
00099         for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
00100         {
00101             // Column index on the screen for rendering this pixel
00102             int screen_col = x0 + bitmap_col;
00103 
00104             // Find the required value of the pixel at the given location within
00105             // the bitmap data and then write it to the LCD screen
00106             int pixel = get_pixel (bitmap_row, bitmap_col);
00107             lcd.setPixel(screen_col, screen_row, pixel);
00108         }
00109     }
00110 }