1313#include " Nokia_LCD_Fonts.h"
1414
1515namespace {
16- const uint8_t kDisplay_max_width = 84 ;
17- const uint8_t kDisplay_max_height = 48 ;
18-
1916// Instantiate the default font
2017const LcdFont nokiaFont{
2118 [](char c) { return Nokia_LCD_Fonts::kDefault_font [c - 0x20 ]; },
2219 Nokia_LCD_Fonts::kColumns_per_character , Nokia_LCD_Fonts::hSpace, 1 };
2320
2421// Each row is made of 8-bit columns
2522const unsigned int kTotal_rows =
26- kDisplay_max_height / Nokia_LCD_Fonts::kRows_per_character ;
27- const unsigned int kTotal_columns = kDisplay_max_width ;
28- const unsigned int kTotal_bits = kDisplay_max_width * kTotal_rows ;
23+ nokia_lcd:: kDisplay_max_height / Nokia_LCD_Fonts::kRows_per_character ;
24+ const unsigned int kTotal_columns = nokia_lcd:: kDisplay_max_width ;
25+ const unsigned int kTotal_bits = nokia_lcd:: kDisplay_max_width * kTotal_rows ;
2926const char kNull_char = ' \0 ' ;
3027const uint8_t kMax_number_length = 11 ; // Size of unsigned long (10) + null
3128} // namespace
@@ -136,7 +133,7 @@ void Nokia_LCD::setBacklight(bool enabled) {
136133}
137134
138135bool Nokia_LCD::setCursor (uint8_t x, uint8_t y) {
139- if (x >= kDisplay_max_width || y >= kTotal_rows ) {
136+ if (x >= nokia_lcd:: kDisplay_max_width || y >= kTotal_rows ) {
140137 return false ;
141138 }
142139
@@ -227,16 +224,19 @@ bool Nokia_LCD::printCharacter(char character) {
227224}
228225
229226bool Nokia_LCD::draw (const unsigned char bitmap[],
230- const unsigned int bitmap_size,
231- const bool read_from_progmem) {
227+ const unsigned int bitmap_size,
228+ const bool read_from_progmem,
229+ const unsigned int bitmap_width) {
232230 bool out_of_bounds = false ;
231+ const unsigned int initialX = mX_cursor ;
233232 for (unsigned int i = 0 ; i < bitmap_size; i++) {
234233 unsigned char pixel =
235234 read_from_progmem ? pgm_read_byte_near (bitmap + i) : bitmap[i];
236235 if (mInverted ) {
237236 pixel = ~pixel;
238237 }
239- out_of_bounds = sendData (pixel) || out_of_bounds;
238+ sendData (pixel, false );
239+ out_of_bounds = updateCursorPosition (initialX, bitmap_width) || out_of_bounds;
240240 }
241241
242242 return out_of_bounds;
@@ -246,9 +246,32 @@ void Nokia_LCD::sendCommand(const unsigned char command) {
246246 send (command, false );
247247}
248248
249- bool Nokia_LCD::sendData (const unsigned char data) { return send (data, true ); }
249+ bool Nokia_LCD::sendData (const unsigned char data) { return sendData (data, true ); }
250+
251+ bool Nokia_LCD::sendData (const unsigned char data, const bool update_cursor) { return send (data, true , update_cursor); }
252+
253+ bool Nokia_LCD::updateCursorPosition (const unsigned int x_start_position, const unsigned int x_end_position) {
254+ bool out_of_bounds = false ;
255+
256+ mX_cursor = (mX_cursor + 1 ) % (x_start_position + x_end_position); // Used to determine if X reached the right margin.
257+ // E.g. starts drawing on column 10, an image of 25px width,
258+ // X will reach the right margin at 35px, so we have to break line
259+ // Calculate the cursor position after the byte being sent
260+ if (mX_cursor == 0 ) {
261+ mX_cursor = x_start_position; // return X to the initial position
262+ // If the column just became 0, this means the row should change
263+ mY_cursor = (mY_cursor + 1 ) % kTotal_rows ; // Row
264+ if (mY_cursor == 0 ) {
265+ // If we are back to row 0 again, then we just went out of bounds
266+ out_of_bounds = true ;
267+ }
268+ }
250269
251- bool Nokia_LCD::send (const unsigned char lcd_byte, const bool is_data) {
270+ setCursor (mX_cursor , mY_cursor ); // updates the cursor position
271+ return out_of_bounds;
272+ }
273+
274+ bool Nokia_LCD::send (const unsigned char lcd_byte, const bool is_data, const bool update_cursor) {
252275 // Tell the LCD that we are writing either to data or a command
253276 digitalWrite (kDc_pin , is_data);
254277
@@ -269,22 +292,11 @@ bool Nokia_LCD::send(const unsigned char lcd_byte, const bool is_data) {
269292
270293 // If we just sent the command, there was no out-of-bounds error
271294 // and we don't have to calculate the new cursor position
272- if (!is_data) {
295+ if (!is_data || !update_cursor ) {
273296 return false ;
274297 }
275298
276- // Calculate the cursor position after the byte being sent
277- mX_cursor = (mX_cursor + 1 ) % kTotal_columns ; // Column
278- if (mX_cursor == 0 ) {
279- // If the column just became 0, this means the row should change
280- mY_cursor = (mY_cursor + 1 ) % kTotal_rows ; // Row
281- if (mY_cursor == 0 ) {
282- // If we are back to row 0 again, then we just went out of bounds
283- return true ;
284- }
285- }
286-
287- return false ;
299+ return updateCursorPosition ();
288300}
289301
290302bool Nokia_LCD::print (int number) {
0 commit comments