Working Menu with selectable fields yet to add comparison with healthy temperature ranges

Dependencies:   TMP102_02

Committer:
ejh23
Date:
Tue Feb 01 15:31:24 2022 +0000
Revision:
1:e11018cf2c14
added menu with time out function - after X seconds screen is off and device sleeps

Who changed what in which revision?

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