ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jgb

Dependencies:   mbed

Committer:
el18jgb
Date:
Wed Apr 22 16:00:38 2020 +0000
Revision:
1:1144f48b5965
there's a basket thing that moves side to side! woo!

Who changed what in which revision?

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