|
| 1 | +/* |
| 2 | + ASCIIDraw |
| 3 | +
|
| 4 | + Use the ArduinoGraphics library to draw ASCII art on the Serial Monitor. |
| 5 | +
|
| 6 | + This is intended primarily to allow testing of the library. |
| 7 | + See the Arduino_MKRRGB library for a more useful demonstration of the ArduinoGraphics library. |
| 8 | +
|
| 9 | + The circuit: |
| 10 | + - Arduino board |
| 11 | +
|
| 12 | + This example code is in the public domain. |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <ArduinoGraphics.h> |
| 16 | + |
| 17 | +const byte canvasWidth = 61; |
| 18 | +const byte canvasHeight = 27; |
| 19 | + |
| 20 | +class ASCIIDrawClass : public ArduinoGraphics { |
| 21 | + public: |
| 22 | + // can be used with an object of any class that inherits from the Print class |
| 23 | + ASCIIDrawClass(Print &printObject = (Print &)Serial) : |
| 24 | + ArduinoGraphics(canvasWidth, canvasHeight), |
| 25 | + _printObject(&printObject) {} |
| 26 | + |
| 27 | + // this function is called by the ArduinoGraphics library's functions |
| 28 | + virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b) { |
| 29 | + // the r parameter is (mis)used to set the character to draw with |
| 30 | + _canvasBuffer[x][y] = r; |
| 31 | + // cast unused parameters to void to fix "unused parameter" warning |
| 32 | + (void)g; |
| 33 | + (void)b; |
| 34 | + } |
| 35 | + |
| 36 | + // display the drawing |
| 37 | + void endDraw() { |
| 38 | + ArduinoGraphics::endDraw(); |
| 39 | + |
| 40 | + for (byte row = 0; row < canvasHeight; row++) { |
| 41 | + for (byte column = 0; column < canvasWidth; column++) { |
| 42 | + // handle unset parts of buffer |
| 43 | + if (_canvasBuffer[column][row] == 0) { |
| 44 | + _canvasBuffer[column][row] = ' '; |
| 45 | + } |
| 46 | + _printObject->print(_canvasBuffer[column][row]); |
| 47 | + } |
| 48 | + _printObject->println(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private: |
| 53 | + Print *_printObject; |
| 54 | + char _canvasBuffer[canvasWidth][canvasHeight] = {{0}}; |
| 55 | +}; |
| 56 | + |
| 57 | +ASCIIDrawClass ASCIIDraw; |
| 58 | + |
| 59 | +void setup() { |
| 60 | + Serial.begin(9600); |
| 61 | + while (!Serial) { |
| 62 | + ; // wait for serial port to connect. Needed for native USB port only |
| 63 | + } |
| 64 | + |
| 65 | + ASCIIDraw.beginDraw(); |
| 66 | + |
| 67 | + // configure the character used to fill the background. The second and third parameters are ignored |
| 68 | + ASCIIDraw.background('+', 0, 0); |
| 69 | + ASCIIDraw.clear(); |
| 70 | + |
| 71 | + // add the outer border |
| 72 | + ASCIIDraw.stroke('-', 0, 0); |
| 73 | + ASCIIDraw.fill('*', 0, 0); |
| 74 | + const byte outerBorderThickness = 1; |
| 75 | + ASCIIDraw.rect(outerBorderThickness, outerBorderThickness, canvasWidth - outerBorderThickness * 2, canvasHeight - outerBorderThickness * 2); |
| 76 | + |
| 77 | + // add the inner border |
| 78 | + ASCIIDraw.stroke('+', 0, 0); |
| 79 | + ASCIIDraw.fill('O', 0, 0); |
| 80 | + const byte borderThickness = outerBorderThickness + 6; |
| 81 | + ASCIIDraw.rect(borderThickness, borderThickness, canvasWidth - borderThickness * 2, canvasHeight - borderThickness * 2); |
| 82 | + |
| 83 | + // add the text |
| 84 | + ASCIIDraw.background(' ', 0, 0); |
| 85 | + ASCIIDraw.stroke('@', 0, 0); |
| 86 | + const char text[] = "ARDUINO"; |
| 87 | + ASCIIDraw.textFont(Font_5x7); |
| 88 | + const byte textWidth = strlen(text) * ASCIIDraw.textFontWidth(); |
| 89 | + const byte textHeight = ASCIIDraw.textFontHeight(); |
| 90 | + const byte textX = (canvasWidth - textWidth) / 2; |
| 91 | + const byte textY = (canvasHeight - textHeight) / 2; |
| 92 | + ASCIIDraw.text(text, textX, textY); |
| 93 | + |
| 94 | + // underline the text |
| 95 | + ASCIIDraw.stroke('-', 0, 0); |
| 96 | + ASCIIDraw.line(textX, textY + textHeight - 1, textX + textWidth - 1, textY + textHeight - 1); |
| 97 | + |
| 98 | + // add some accents to the underline |
| 99 | + ASCIIDraw.stroke('+', 0, 0); |
| 100 | + ASCIIDraw.point(textX + 4, textY + textHeight - 1); |
| 101 | + ASCIIDraw.point(textX + textWidth - 1 - 4, textY + textHeight - 1); |
| 102 | + |
| 103 | + // print the drawing to the Serial Monitor |
| 104 | + ASCIIDraw.endDraw(); |
| 105 | +} |
| 106 | + |
| 107 | +void loop() {} |
0 commit comments