Class used to interface with the Nokia N5110 LCD.

Fork of N5110 by Craig Evans

Committer:
valavanisalex
Date:
Wed Mar 08 16:13:08 2017 +0000
Revision:
41:6c046786be6c
Parent:
40:c9262294f2e1
Child:
42:596c207519de
Fix Bitmap error checking

Who changed what in which revision?

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