Yunting Zou 201199716

Dependencies:   mbed MotionSensor

Committer:
zhouyun123
Date:
Thu May 14 17:18:58 2020 +0000
Revision:
3:4bd22344278d
Parent:
0:047e14f53977
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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