ELEC2645 (2017/18) / Mbed 2 deprecated el15ww

Dependencies:   mbed

Committer:
weiway
Date:
Mon Apr 30 14:10:48 2018 +0000
Revision:
0:d557f30e3a95
I deleted my previous program , and make a new program as a final submitted project

Who changed what in which revision?

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