-
Notifications
You must be signed in to change notification settings - Fork 9
/
basic_display.hpp
586 lines (529 loc) · 18.3 KB
/
basic_display.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2020 Embedded Artistry LLC
// SPDX-License-Identifier: GPL-3.0-only OR Embedded Virtual Machine Commercial License
#ifndef BASIC_DISPLAY_HPP_
#define BASIC_DISPLAY_HPP_
#include "driver.hpp"
#include <cassert>
#include <cstring>
// In this case, names like "x" and "y" are acceptable.
// NOLINTBEGIN(readability-identifier-length)
namespace embvm
{
/** This class provides an interface for a basic 2-color display device.
*
* The basicDisplay class defines common interfaces for basic 2-color display drivers, which can be
* LED or OLED.
*
* # Define an basic display driver
*
* To create a basic display driver, derive from the basicDisplay object:
*
* @code
* class ssd1306 final : public basicDisplay
* { ... };
* @endcode
*
* Derived classes must implement the following functions:
* - pixel(coord_t x, coord_t y, color c, mode m), which enables the other draw functions
* - display()
* - clear()
* - invert()
* - contrast()
* - cursor()
* - screenWidth()
* - screenHeight()
* - scrollRight()
* - scrollLeft()
* - scrollVertRight()
* - scrollVertLeft()
* - scrollStop()
* - flipVertical()
* - flipHorizontal()
*
*
* Derived classes must also implement pure virtual embvm::DriverBase functions:
* - start_()
* - stop_()
*
* Turning on a dispay should be handled in the start_() function, and turning off the display
* should be handled in the stop_() function.
*
* # Using a basic display
*
* The display is turned on by calling start(), and turned off by calling stop().
*
* Most display functions update the screen buffer, which is the representation of the display
* buffer contents in local memory.
*
* @code
* snprintf(tof_string_, 32, "ToF Range: %u mm\n", v);
* screen0.printString(0, 0, tof_string_);
* screen0.printString(0, 32, tof_mode_string_);
* @endcode
*
* Changes to the screen buffer are not written to the display's hardware buffer until the
* display() function is called.
*
* @code
* screen0.display();
* @endcode
*
* The screen buffer can be updated incrementally. If clearing the entire buffer is desired,
* call the clear() function.
*
* @code
* screen0.clear();
* @endcode
*
* @ingroup FrameworkDriver
*/
class basicDisplay : public embvm::DriverBase
{
public:
/// Display color inversion options.
enum class invert : uint8_t
{
/// Use the default display color setting.
normal = 0,
/// Invert the display colors.
invert
};
/// Set the drawing mode, if supported by the driver.
enum class mode : uint8_t
{
/// Either sets or clears a pixel as requested, overwriting the value set in the
/// screen buffer.
normal = 0,
/// The pixel output in the screen buffer will be an XOR of the current value and the
/// input value.
XOR
};
/// Basic displays have two pixel colors: black and white.
enum class color : uint8_t
{
black = 0,
white
};
/// Coordinate representation type.
using coord_t = uint8_t;
protected:
/** Default constructor.
*
* Initializes the DriverBase class with a BASIC_DISPLAY type ID
*/
basicDisplay() noexcept : embvm::DriverBase(embvm::DriverType::BASIC_DISPLAY)
{
// empty constructor
}
public:
/** Basic Display Driver Type ID
*
* @returns Basic Display type ID.
*/
static constexpr auto type() noexcept -> embvm::DriverType
{
return embvm::DriverType::BASIC_DISPLAY;
}
/// Clear the screen contents.
virtual void clear() noexcept = 0;
/// Flush the screen buffer contents to the hardware display buffer.
virtual void display() noexcept = 0;
/** Invert the display color.
* The WHITE color of the display will turn to BLACK and the BLACK will turn to WHITE.
*
* @param inv invert::invert uses color inversion, invert::normal uses the display standard.
*/
virtual void invert(invert inv) noexcept = 0;
/** Set contrast.
*
* @param contrast Target display contrast value, ranging from 0 to 255.
*/
virtual void contrast(uint8_t contrast) noexcept = 0;
/** Set the cusor within the screen buffer.
*
* Contents in the screen buffer are not modified by setting the cursor position.
*
* @param x X-coordinate of the cursor.
* @param y Y-coordinate of the cursor.
*/
virtual void cursor(coord_t x, coord_t y) noexcept = 0;
/** Draw a pixel in the screen buffer.
*
* Draw pixel using the configured color setting and draw mode in the screen buffer's x,y
* position.
*
* @param x X-coordinate of the pixel.
* @param y Y-coordinate of the pixel.
*/
void pixel(coord_t x, coord_t y) noexcept
{
pixel(x, y, color_, mode_);
}
/** Draw a pixel with a specific color and mode.
*
* Draw a pixel at position (x,y) with a specific color setting and draw mode.
*
* @param x X-coordinate of the pixel.
* @param y Y-coordinate of the pixel.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void pixel(coord_t x, coord_t y, color c, mode m) noexcept = 0;
/** Draw a line.
*
* Draw a line using the configured color setting and draw mode from (x0,y0) to (x1,y1) of the
* screen buffer.
*
* @param x0 X-coordinate where the line originates.
* @param y0 Y-coordinate where the line originates.
* @param x1 X-coordinate where the line terminates.
* @param y1 Y-coordinate where the line terminates.
*/
void line(coord_t x0, coord_t y0, coord_t x1, coord_t y1) noexcept
{
line(x0, y0, x1, y1, color_, mode_);
}
/** Draw a line with a specific color and mode.
*
* Draw line with a specific color setting and draw mode from (x0,y0) to (x1,y1) of the screen
* buffer.
*
* @param x0 X-coordinate where the line originates.
* @param y0 Y-coordinate where the line originates.
* @param x1 X-coordinate where the line terminates.
* @param y1 Y-coordinate where the line terminates.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void line(coord_t x0, coord_t y0, coord_t x1, coord_t y1, color c, mode m) noexcept = 0;
/** Draw a horizontal line.
*
* Draw a horizontal line using the configured color setting and draw mode from (x,y) to
* (x+width,y) in the screen buffer.
*
* @param x X-coordinate where the line originates.
* @param y Y-coordinate where the line originates.
* @param width Length of the line, in pixels. The line will terminate at coordinate
* (x+width, y).
*/
void lineH(coord_t x, coord_t y, uint8_t width) noexcept
{
lineH(x, y, width, color_, mode_);
}
/** Draw a horizontal line with a specific color and mode.
*
* Draw a horizontal line with a specific color setting and draw mode from (x,y) to
* (x+width,y) in the screen buffer.
*
* @param x X-coordinate where the line originates.
* @param y Y-coordinate where the line originates.
* @param width Length of the line, in pixels. The line will terminate at coordinate
* (x+width, y).
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
void lineH(coord_t x, coord_t y, uint8_t width, color c, mode m) noexcept
{
line(x, y, x + width, y, c, m);
}
/** Draw a vertical line.
*
* Draw a vertical line using the configured color setting and draw mode from (x,y) to
* (x,y+height) in the screen buffer.
*
* @param x X-coordinate where the line originates.
* @param y Y-coordinate where the line originates.
* @param height Length of the line, in pixels. The line will terminate at coordinate
* (x, y+height).
*/
void lineV(coord_t x, coord_t y, uint8_t height) noexcept
{
lineV(x, y, height, color_, mode_);
}
/** Draw a vertical line with a specific color and mode.
*
* Draw a vertical line using a specific color setting and draw mode from (x,y) to
* (x,y+height) in the screen buffer.
*
* @param x X-coordinate where the line originates.
* @param y Y-coordinate where the line originates.
* @param height Length of the line, in pixels. The line will terminate at coordinate
* (x, y+height).
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
void lineV(coord_t x, coord_t y, uint8_t height, color c, mode m) noexcept
{
line(x, y, x, y + height, c, m);
}
/** Draw a rectangle.
*
* Draw a rectangle using the configured color setting and draw mode from (x,y)
* to (x+width,y+height) in the screen buffer. Only the boundary of the rectangle will be drawn.
*
* @param x X-coordinate where the rectangle originates.
* @param y Y-coordinate where the rectangle originates.
* @param width Width of the rectangle, in pixels.
* The rectangle width spans from x to x+width.
* @param height Height of the rectangle, in pixels.
* The rectangle height spans from y + y+height.
*/
void rect(coord_t x, coord_t y, uint8_t width, uint8_t height) noexcept
{
rect(x, y, width, height, color_, mode_);
}
/** Draw a rectangle with a specific color and mode.
*
* Draw a rectangle with a specific color setting and draw mode from (x,y) to
* (x+width,y+height) in the screen buffer. Only the boundary of the rectangle will be drawn.
*
* @param x X-coordinate where the rectangle originates.
* @param y Y-coordinate where the rectangle originates.
* @param width Width of the rectangle, in pixels.
* The rectangle width spans from x to x+width.
* @param height Height of the rectangle, in pixels.
* The rectangle height spans from y + y+height.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void rect(coord_t x, coord_t y, uint8_t width, uint8_t height, color c,
mode m) noexcept = 0;
/** Draw a filled rectangle.
*
* Draw a filled rectangle using the configured color setting and draw mode from (x,y) to
* (x+width,y+height) in the screen buffer. The interior of the rectangle will be filled with
* the configured color setting.
*
* @param x X-coordinate where the rectangle originates.
* @param y Y-coordinate where the rectangle originates.
* @param width Width of the rectangle, in pixels.
* The rectangle width spans from x to x+width.
* @param height Height of the rectangle, in pixels.
* The rectangle height spans from y + y+height.
*/
void rectFill(coord_t x, coord_t y, uint8_t width, uint8_t height) noexcept
{
rectFill(x, y, width, height, color_, mode_);
}
/** Draw a filled rectangle with a specific color and mode.
*
* Draw a filled rectangle using a specific color setting and draw mode from (x,y) to
* (x+width,y+height) in the screen buffer. The interior of the rectangle will be filled with
* the specified color setting.
*
* @param x X-coordinate where the rectangle originates.
* @param y Y-coordinate where the rectangle originates.
* @param width Width of the rectangle, in pixels.
* The rectangle width spans from x to x+width.
* @param height Height of the rectangle, in pixels.
* The rectangle height spans from y + y+height.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void rectFill(coord_t x, coord_t y, uint8_t width, uint8_t height, color c,
mode m) noexcept = 0;
/** Draw a circle.
*
* Draw a circle with center (x,y) and the specified radius using the configured color setting
* and draw mode in the screen buffer. Only the boundary of the circle will be drawn.
*
* @param x X-coorindate for the center of the circle.
* @param y Y-coordinate for the center of the circle.
* @param radius The radius of the circle.
*/
void circle(coord_t x, coord_t y, uint8_t radius) noexcept
{
circle(x, y, radius, color_, mode_);
}
/** Draw a circle with a specific color and mode.
*
* Draw a circle with center (x,y) and the specified radius using a specific color setting
* and draw mode in the screen buffer. Only the boundary of the circle will be drawn.
*
* @param x X-coorindate for the center of the circle.
* @param y Y-coordinate for the center of the circle.
* @param radius The radius of the circle.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void circle(coord_t x, coord_t y, uint8_t radius, color c, mode m) noexcept = 0;
/** Draw a filled circle.
*
* Draw a filled circle with center (x,y) and the specified radius using the configured color
* setting and draw mode in the screen buffer. The interior of the circle will be filled with
* the specified color setting.
*
* @param x X-coorindate for the center of the circle.
* @param y Y-coordinate for the center of the circle.
* @param radius The radius of the circle.
*/
void circleFill(coord_t x, coord_t y, uint8_t radius) noexcept
{
circleFill(x, y, radius, color_, mode_);
}
/** Draw a filled circle with a specific color and mode.
*
* Draw a filled circle with center (x,y) and the specified radius using a specific color
* setting and draw mode in the screen buffer. The interior of the circle will be filled with
* the specified color setting.
*
* @param x X-coorindate for the center of the circle.
* @param y Y-coordinate for the center of the circle.
* @param radius The radius of the circle.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void circleFill(coord_t x, coord_t y, uint8_t radius, color c, mode m) noexcept = 0;
/** Draw an ASCII character.
*
* Draw an ASCII character using the configured color and draw mode with the upper left corner
* of the character located at (x,y).
*
* @param x X-coordinate representing the top-left of the character.
* @param y Y-coordinate representing the top-left of the character.
* @param character the ASCII character to draw. Must be present in the font file. If the
* requested character is not present, nothing will be drawn on the screen buffer.
*/
void drawChar(coord_t x, coord_t y, uint8_t character) noexcept
{
drawChar(x, y, character, color_, mode_);
}
/// Print a character on the display
/// @param c the character to print to the display
virtual void putchar(uint8_t c) noexcept = 0;
/** Draw an ASCII character with a specific color and mode.
*
* Draw an ASCII character using a specific color and draw mode with the upper left corner
* of the character located at (x,y).
*
* @param x X-coordinate representing the top-left of the character.
* @param y Y-coordinate representing the top-left of the character.
* @param character the ASCII character to draw. Must be present in the font file. If the
* requested character is not present, nothing will be drawn on the screen buffer.
* @param c Pixel color.
* @param m Mode used to update the screen buffer.
*/
virtual void drawChar(coord_t x, coord_t y, uint8_t character, color c, mode m) noexcept = 0;
/// Print a string on the display
/// @param x The x-coordinate to start the string.
/// @param y The y-coordinate to start the string.
/// @param str The string to print to the display.
void printString(coord_t x, coord_t y, const char* str) noexcept
{
printString(x, y, str, strlen(str));
}
/// Print a string on the display
/// @param x The x-coordinate to start the string.
/// @param y The y-coordinate to start the string.
/// @param str The string to print to the display.
/// @param length The number of characters in str.
void printString(coord_t x, coord_t y, const char* str, size_t length) noexcept
{
cursor(x, y);
for(size_t i = 0; i < length; i++)
{
putchar(static_cast<uint8_t>(str[i]));
}
}
/** Replace the screen buffer with a bitmap.
*
* Supply a bitmap to write to the screen buffer. If the size
* of the bitmap is less than the size of the screen buffer, the remainder of the screen buffer
* will not be modified.
*
* @param bitmap Bitmap array which will be used to overwrite the screen buffer.
*/
virtual void drawBitmap(uint8_t* bitmap) noexcept = 0;
/** Check the screen width.
*
* The drived class must implement this function.
*
* @returns The width of the screen in pixels.
*/
[[nodiscard]] virtual auto screenWidth() const noexcept -> uint8_t = 0;
/** Check the screen height.
*
* The drived class must implement this function.
*
* @returns The height of the screen in pixels.
*/
[[nodiscard]] virtual auto screenHeight() const noexcept -> uint8_t = 0;
/** Set color.
*
* Set the current draw's color. Only WHITE and BLACK available.
*
* @param c The desired draw color setting.
*/
void drawColor(color c) noexcept
{
assert(c == color::white || c == color::black);
color_ = c;
}
/** Set draw mode.
*
* @param m Desired draw mode: NORM or XOR.
*/
virtual void drawMode(mode m) noexcept
{
assert(m == mode::normal || m == mode::XOR);
mode_ = m;
}
#pragma mark - LCD Rotate Scroll functions -
/** Enable right scrolling.
* Set row start to row stop on the display to scroll right.
*
* @param start The start row index.
* @param stop The stop row index.
*
* @pre Scrolling is not enabled, start < stop.
* @post The display is scrolling to the right.
*/
virtual void scrollRight(coord_t start, coord_t stop) noexcept = 0;
/** Enable left scrolling.
* Set row start to row stop on the display to scroll left.
*
* @param start The start row index.
* @param stop The stop row index.
*
* @pre Scrolling is not enabled, stop < start.
* @post The display is scrolling to the left.
*/
virtual void scrollLeft(coord_t start, coord_t stop) noexcept = 0;
// TODO: document
virtual void scrollVertRight(coord_t start, coord_t stop) noexcept = 0;
// TODO: document
virtual void scrollVertLeft(coord_t start, coord_t stop) noexcept = 0;
/** Stop scrolling.
*
* Stop the scrolling of graphics on the display.
*/
virtual void scrollStop() noexcept = 0;
/** Vertical flip.
*
* Flip the graphics on the display vertically.
*
* @param flip True enables vertical flip, false disables vertical flip.
*/
virtual void flipVertical(bool flip) noexcept = 0;
/** Horizontal flip.
*
* Flip the graphics on the display horizontally.
*
* @param flip True enables horizontal flip, false disables horizontal flip.
*/
virtual void flipHorizontal(bool flip) noexcept = 0;
protected:
// embvm::DriverBase function that derived classes must implement.
void start_() noexcept override = 0;
// embvm::DriverBase function that derived classes must implement.
void stop_() noexcept override = 0;
/// Default destructor.
~basicDisplay() noexcept;
/// The current display mode setting.
mode mode_ = mode::normal;
/// The current display color setting.
color color_ = color::black;
};
} // namespace embvm
// NOLINTEND(readability-identifier-length)
#endif // BASIC_DISPLAY_HPP_