-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathASCIIDraw.ino
107 lines (85 loc) · 3.29 KB
/
ASCIIDraw.ino
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
/*
ASCIIDraw
Use the ArduinoGraphics library to draw ASCII art on the Serial Monitor.
This is intended primarily to allow testing of the library.
See the Arduino_MKRRGB library for a more useful demonstration of the ArduinoGraphics library.
The circuit:
- Arduino board
This example code is in the public domain.
*/
#include <ArduinoGraphics.h>
const byte canvasWidth = 61;
const byte canvasHeight = 27;
class ASCIIDrawClass : public ArduinoGraphics {
public:
// can be used with an object of any class that inherits from the Print class
ASCIIDrawClass(Print &printObject = (Print &)Serial) :
ArduinoGraphics(canvasWidth, canvasHeight),
_printObject(&printObject) {}
// this function is called by the ArduinoGraphics library's functions
virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
// the r parameter is (mis)used to set the character to draw with
_canvasBuffer[x][y] = r;
// cast unused parameters to void to fix "unused parameter" warning
(void)g;
(void)b;
}
// display the drawing
void endDraw() {
ArduinoGraphics::endDraw();
for (byte row = 0; row < canvasHeight; row++) {
for (byte column = 0; column < canvasWidth; column++) {
// handle unset parts of buffer
if (_canvasBuffer[column][row] == 0) {
_canvasBuffer[column][row] = ' ';
}
_printObject->print(_canvasBuffer[column][row]);
}
_printObject->println();
}
}
private:
Print *_printObject;
char _canvasBuffer[canvasWidth][canvasHeight] = {{0}};
};
ASCIIDrawClass ASCIIDraw;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
ASCIIDraw.beginDraw();
// configure the character used to fill the background. The second and third parameters are ignored
ASCIIDraw.background('+', 0, 0);
ASCIIDraw.clear();
// add the outer border
ASCIIDraw.stroke('-', 0, 0);
ASCIIDraw.fill('*', 0, 0);
const byte outerBorderThickness = 1;
ASCIIDraw.rect(outerBorderThickness, outerBorderThickness, canvasWidth - outerBorderThickness * 2, canvasHeight - outerBorderThickness * 2);
// add the inner border
ASCIIDraw.stroke('+', 0, 0);
ASCIIDraw.fill('O', 0, 0);
const byte borderThickness = outerBorderThickness + 6;
ASCIIDraw.rect(borderThickness, borderThickness, canvasWidth - borderThickness * 2, canvasHeight - borderThickness * 2);
// add the text
ASCIIDraw.background(' ', 0, 0);
ASCIIDraw.stroke('@', 0, 0);
const char text[] = "ARDUINO";
ASCIIDraw.textFont(Font_5x7);
const byte textWidth = strlen(text) * ASCIIDraw.textFontWidth();
const byte textHeight = ASCIIDraw.textFontHeight();
const byte textX = (canvasWidth - textWidth) / 2;
const byte textY = (canvasHeight - textHeight) / 2;
ASCIIDraw.text(text, textX, textY);
// underline the text
ASCIIDraw.stroke('-', 0, 0);
ASCIIDraw.line(textX, textY + textHeight - 1, textX + textWidth - 1, textY + textHeight - 1);
// add some accents to the underline
ASCIIDraw.stroke('+', 0, 0);
ASCIIDraw.point(textX + 4, textY + textHeight - 1);
ASCIIDraw.point(textX + textWidth - 1 - 4, textY + textHeight - 1);
// print the drawing to the Serial Monitor
ASCIIDraw.endDraw();
}
void loop() {}