Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of N5110 by
Revision 42:c9262294f2e1, committed 2017-03-08
- Comitter:
- valavanisalex
- Date:
- Wed Mar 08 15:48:48 2017 +0000
- Parent:
- 41:5998f0d22113
- Child:
- 43:6c046786be6c
- Commit message:
- Add Bitmap rendering code
Changed in this revision
--- a/Bitmap.cpp Wed Mar 08 14:20:01 2017 +0000
+++ b/Bitmap.cpp Wed Mar 08 15:48:48 2017 +0000
@@ -2,22 +2,29 @@
#include <iostream>
-Bitmap::Bitmap(std::vector<int> const &contents,
+#include "N5110.h"
+
+Bitmap::Bitmap(int const *contents,
unsigned int const height,
unsigned int const width)
:
- _contents(contents),
+ _contents(std::vector<int>(height*width)),
_height(height),
_width(width)
{
// Perform a quick sanity check of the dimensions
- if (contents.size() != height * height) {
- std::cerr << "Contents of bitmap has size " << contents.size()
+ if (_contents.size() != height * height) {
+ std::cerr << "Contents of bitmap has size " << _contents.size()
<< " pixels, but its dimensions were specified as "
<< width << " * " << height << " = " << width * height;
}
+
+ for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
}
+/**
+ * @returns the value of the pixel at the given position
+ */
int Bitmap::get_pixel(unsigned int const row,
unsigned int const column) const
{
@@ -31,4 +38,59 @@
// Now return the pixel value, using row-major indexing
return _contents[row * _width + column];
+}
+
+/**
+ * @brief Prints the contents of the bitmap to the terminal
+ */
+void Bitmap::print() const
+{
+ for (unsigned int row = 0; row < _height; ++row)
+ {
+ // Print each element of the row
+ for (unsigned int column = 0; column < _width; ++column)
+ {
+ int pixel = get_pixel(row, column);
+ std::cout << pixel;
+ }
+
+ // And then terminate with a new-line character
+ std::cout << std::endl;
+ }
+}
+
+/**
+ * @brief Renders the contents of the bitmap onto an N5110 screen
+ *
+ * @param[in] lcd The screen to use for rendering
+ * @param[in] x0 The horizontal position in pixels at which to render the bitmap
+ * @param[in] y0 The vertical position in pixels at which to render the bitmap
+ *
+ * @details Note that x0, y0 gives the location of the top-left of the bitmap on
+ * the screen.
+ * This function only updates the buffer on the screen. You still need
+ * to refresh the screen in order to actually see the bitmap.
+ */
+void Bitmap::render(N5110 &lcd,
+ unsigned int const x0,
+ unsigned int const y0) const
+{
+ // Loop through each row of the bitmap image
+ for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
+ {
+ // Row index on the screen for rendering the row of pixels
+ unsigned int screen_row = y0 + bitmap_row;
+
+ // Render each pixel in the row
+ for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
+ {
+ // Column index on the screen for rendering this pixel
+ int screen_col = x0 + bitmap_col;
+
+ int pixel = get_pixel(bitmap_row, bitmap_col);
+
+ if(pixel) lcd.setPixel(screen_col, screen_row);
+ else lcd.clearPixel(screen_col, screen_row);
+ }
+ }
}
\ No newline at end of file
--- a/Bitmap.h Wed Mar 08 14:20:01 2017 +0000
+++ b/Bitmap.h Wed Mar 08 15:48:48 2017 +0000
@@ -3,8 +3,37 @@
#include <vector>
+// Forward declarations
+class N5110;
+
/**
- * A monochrome bitmap drawing
+ * @brief A black & white bitmap that can be rendered on an N5110 screen
+ * @author Alex Valavanis <a.valavanis@leeds.ac.uk>
+ *
+ * @code
+ // First declare the pixel map data using '1' for black,
+ // or '0' for white pixels
+ static int sprite_data[] = {
+ 0,0,1,0,0,
+ 0,1,1,1,0,
+ 0,0,1,0,0,
+ 0,1,1,1,0,
+ 1,1,1,1,1,
+ 1,1,1,1,1,
+ 1,1,0,1,1,
+ 1,1,0,1,1
+ };
+
+ // Instantiate the Bitmap object using the data above
+ Bitmap sprite(sprite_data, 8, 5); // Specify rows and columns in sprite
+
+ // We can render the bitmap wherever we want on the screen
+ sprite.render(lcd, 20, 6); // x and y locations for rendering
+ sprite.render(lcd, 30, 10);
+
+ // We can also print its values to the terminal
+ sprite.print();
+ * @endcode
*/
class Bitmap
{
@@ -19,12 +48,18 @@
unsigned int _width; ///< The width of the drawing in pixels
public:
- Bitmap(std::vector<int> const &contents,
- unsigned int const height,
- unsigned int const width);
+ Bitmap(int const *contents,
+ unsigned int const height,
+ unsigned int const width);
int get_pixel(unsigned int const row,
unsigned int const column) const;
+
+ void print() const;
+
+ void render(N5110 &lcd,
+ unsigned int const x0,
+ unsigned int const y0) const;
};
-#endif // SPRITE_H
\ No newline at end of file
+#endif // BITMAP_H
\ No newline at end of file
--- a/N5110.h Wed Mar 08 14:20:01 2017 +0000
+++ b/N5110.h Wed Mar 08 15:48:48 2017 +0000
@@ -427,7 +427,7 @@
int nrows,
int ncols,
int *sprite);
-
+
private:
// methods
void setXYAddress(unsigned int const x,
