test

Dependencies:   mbed FXOS8700CQ

Committer:
Neowless
Date:
Fri May 15 12:32:47 2020 +0000
Revision:
1:48b0bf0bcda8
test;

Who changed what in which revision?

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