diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml index 47ac50a..53a9f54 100644 --- a/.github/workflows/sync-labels.yml +++ b/.github/workflows/sync-labels.yml @@ -114,7 +114,7 @@ jobs: path: ${{ env.CONFIGURATIONS_FOLDER }} - name: Remove unneeded artifact - uses: geekyeggo/delete-artifact@v4 + uses: geekyeggo/delete-artifact@v5 with: name: ${{ env.CONFIGURATIONS_ARTIFACT }} diff --git a/README.adoc b/README.adoc index eeee79b..83baac9 100644 --- a/README.adoc +++ b/README.adoc @@ -32,6 +32,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA == How-to generate bitmaps from the fonts == [source,bash] ---- -cd extra +cd extras ./generate_font.py 5x7.bdf Font_5x7.c Font_5x7 ---- diff --git a/docs/api.md b/docs/api.md index a870920..004b2b6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -6,7 +6,6 @@ #### Description - Initializes the graphics device. #### Syntax @@ -15,7 +14,6 @@ Initializes the graphics device. YourScreen.begin() ``` - #### Parameters None @@ -27,14 +25,12 @@ None #### Example ``` -if (!YourScreen.begin() { - Serial.println(“Failed to initialize the display!”); +if (!YourScreen.begin()) { + Serial.println("Failed to initialize the display!"); while (1); } ``` - - ### `end()` #### Description @@ -45,16 +41,12 @@ Stops the graphics device. ``` YourScreen.end() - ``` - #### Parameters - None - #### Returns Nothing @@ -65,26 +57,20 @@ Nothing YourScreen.end(); ``` - - ### `width()` #### Description - Returns the pixel width of the graphics device. #### Syntax ``` YourScreen.width() - ``` - #### Parameters - None #### Returns @@ -97,25 +83,20 @@ Returns the pixel width of the graphics device. int w = YourScreen.width(); ``` - ### `height()` #### Description - Returns the pixel height of the graphics device. #### Syntax ``` YourScreen.height() - ``` - #### Parameters - None #### Returns @@ -128,62 +109,50 @@ Returns the pixel height of the graphics device. int h = YourScreen.height(); ``` - ### `beginDraw()` #### Description - Begins a drawing operation. #### Syntax ``` YourScreen.beginDraw() - ``` - #### Parameters - None - #### Returns Nothing #### Example - +``` YourScreen.beginDraw(); YourScreen.set(0, 0, 255, 0, 0); YourScreen.endDraw(); - - +``` ### `endDraw()` #### Description - Ends a drawing operation, any drawing operations after beginDraw() is called will be displayed to the screen. #### Syntax ``` YourScreen.endDraw() - ``` - #### Parameters - None - #### Returns Nothing @@ -196,12 +165,10 @@ YourScreen.set(0, 0, 255, 0, 0); YourScreen.endDraw(); ``` - ### `background()` #### Description - Set the background color of drawing operations. Used when calling clear() or drawing text. #### Syntax @@ -209,13 +176,10 @@ Set the background color of drawing operations. Used when calling clear() or dra ``` YourScreen.background(r, g, b) YourScreen.background(color) - ``` - #### Parameters - - r: red color value (0 - 255) - g: green color value (0 - 255) - b: blue color value (0 - 255) @@ -234,27 +198,23 @@ YourScreen.clear(); YourScreen.endDraw(); ``` - ### `clear()` #### Description - -Set clear the screen contents, uses the background colour set in background(). +Clear the screen contents or a specific pixel, uses the background colour set in background(). #### Syntax ``` YourScreen.clear() - +YourScreen.clear(x, y) ``` - #### Parameters - -None - +- x: x position of the pixel to clear +- y: y position of the pixel to clear #### Returns @@ -269,13 +229,10 @@ YourScreen.clear(); YourScreen.endDraw(); ``` - - ### `fill()` #### Description - Set the fill color of drawing operations. #### Syntax @@ -283,13 +240,10 @@ Set the fill color of drawing operations. ``` YourScreen.fill(r, g, b) YourScreen.fill(color) - ``` - #### Parameters - - r: red color value (0 - 255) - g: green color value (0 - 255) - b: blue color value (0 - 255) @@ -310,28 +264,22 @@ YourScreen.rect(0, 0, YourScreen.width(), YourScreen.height()); YourScreen.endDraw(); ``` - ### `noFill()` #### Description - Clears the fill color of drawing operations. #### Syntax ``` YourScreen.noFill() - ``` - #### Parameters - None - #### Returns Nothing @@ -347,12 +295,10 @@ YourScreen.rect(0, 0, YourScreen.width(), YourScreen.height()); YourScreen.endDraw(); ``` - ### `stroke()` #### Description - Set the stroke color of drawing operations. #### Syntax @@ -360,13 +306,10 @@ Set the stroke color of drawing operations. ``` YourScreen.stroke(r, g, b) YourScreen.stroke(color) - ``` - #### Parameters - - r: red color value (0 - 255) - g: green color value (0 - 255) - b: blue color value (0 - 255) @@ -387,29 +330,22 @@ YourScreen.rect(0, 0, YourScreen.width(), YourScreen.height()); YourScreen.endDraw(); ``` - - ### `noStroke()` #### Description - Clears the stroke color of drawing operations. #### Syntax ``` YourScreen.noStroke() - ``` - #### Parameters - None - #### Returns Nothing @@ -425,25 +361,20 @@ YourScreen.rect(0, 0, YourScreen.width(), YourScreen.height()); YourScreen.endDraw(); ``` - ### `line()` #### Description - Stroke a line, uses the stroke color set in stroke(). #### Syntax ``` YourScreen.line(x1, y1, x2, y2) - ``` - #### Parameters - - x1: x position of the starting point of the line - y1: y position of the starting point of the line - x2: x position of the end point of the line @@ -463,25 +394,20 @@ YourScreen.line(0, 0, YourScreen.width() - 1, YourScreen.height() - 1); YourScreen.endDraw(); ``` - ### `point()` #### Description - Stroke a point, uses the stroke color set in stroke(). #### Syntax ``` YourScreen.point(x, y) - ``` - #### Parameters - x: x position of the point y: y position of the point @@ -499,25 +425,20 @@ YourScreen.point(1, 1); YourScreen.endDraw(); ``` - ### `rect()` #### Description - Stroke and fill a rectangle, uses the stroke color set in stroke() and the fill color set in fill(). #### Syntax ``` YourScreen.rect(x, y, width, height) - ``` - #### Parameters - - x: x position of the rectangle - y: y position of the rectangle - width: width of the rectangle @@ -538,25 +459,20 @@ YourScreen.rect(0, 0, YourScreen.width(), YourScreen.height()); YourScreen.endDraw(); ``` - ### `circle()` #### Description - Stroke and fill a circle, uses the stroke color set in stroke() and the fill color set in fill(). #### Syntax ``` YourScreen.circle(x, y, diameter) - ``` - #### Parameters - - x: x center position of the circle - y: y center position of the circle - diameter: diameter of the circle @@ -576,25 +492,20 @@ YourScreen.circle(YourScreen.width()/2, YourScreen.height()/2, YourScreen.height YourScreen.endDraw(); ``` - ### `ellipse()` #### Description - Stroke and fill an ellipse, uses the stroke color set in stroke() and the fill color set in fill(). #### Syntax ``` YourScreen.ellipse(x, y, width, height) - ``` - #### Parameters - - x: x center position of the ellipse - y: y center position of the ellipse - width: width of the ellipse @@ -615,12 +526,10 @@ YourScreen.ellipse(YourScreen.width()/2, YourScreen.height()/2, YourScreen.width YourScreen.endDraw(); ``` - ### `text()` #### Description - Draw some text, uses the stroke color set in stroke() and the background color set in background(). #### Syntax @@ -628,13 +537,10 @@ Draw some text, uses the stroke color set in stroke() and the background color s ``` YourScreen.text(string) YourScreen.text(string, x, y) - ``` - #### Parameters - - string: string to draw - x: x position for the start of the text - y: y position for the start of the text @@ -653,28 +559,22 @@ YourScreen.text("abc", 0, 1); YourScreen.endDraw(); ``` - ### `textFont()` #### Description - -Sets the font uses for text. The library current has the Font_4x6 and Font_5x7 built in. +Sets the font used for text. The library current has the Font_4x6 and Font_5x7 built in. #### Syntax ``` YourScreen.textFont(font) - ``` - #### Parameters - font: font to set - #### Returns Nothing @@ -690,12 +590,10 @@ YourScreen.text("abc", 0, 1); YourScreen.endDraw(); ``` - ### `textFontWidth()` #### Description - Returns the width, in pixels, of the current font. #### Syntax @@ -705,13 +603,10 @@ YourScreen.textFontWidth() ``` - #### Parameters - None - #### Returns Nothing @@ -722,28 +617,22 @@ Nothing int w = YourScreen.textFontWidth(); ``` - ### `textFontHeight()` #### Description - Returns the height, in pixels, of the current font. #### Syntax ``` YourScreen.textFontHeight() - ``` - #### Parameters - None - #### Returns Nothing @@ -754,12 +643,45 @@ Nothing int h = YourScreen.textFontHeight(); ``` +### `textSize()` + +#### Description + +Set a text scale factor + +#### Syntax + +``` +YourScreen.textSize(scale) +YourScreen.textSize(scaleX, scaleY) +``` + +#### Parameters + +scale: scale factor used for both x and y +scaleX: x scale factor +scaleY: y scale factor + +#### Returns + +Nothing + +#### Example + +``` +YourScreen.beginDraw(); +YourScreen.clear(); +YourScreen.stroke(255, 255, 255); +YourScreen.textFont(Font_5x7); +YourScreen.textSize(5); +YourScreen.text("abc", 0, 1); +YourScreen.endDraw(); +``` ### `set()` #### Description - Set a pixel’s color value. #### Syntax @@ -767,13 +689,10 @@ Set a pixel’s color value. ``` YourScreen.set(x, y, r, g, b) YourScreen.set(x, y, color) - ``` - #### Parameters - x: x position of the pixel y: y position of the pixel r: red color value (0 - 255) @@ -793,12 +712,10 @@ YourScreen.point(1, 1, 0, 255, 0); YourScreen.endDraw(); ``` - ### `beginText()` #### Description - Start the process of displaying and optionally scrolling text. The Print interface can be used to set the text. #### Syntax @@ -807,13 +724,10 @@ Start the process of displaying and optionally scrolling text. The Print interfa YourScreen.beginText() YourScreen.beginText(x, y, r, g, b) YourScreen.beginText(x, y, color) - ``` - #### Parameters - x: x position of the text y: y position of the text r: red color value (0 - 255) @@ -833,12 +747,10 @@ YourScreen.print("Hi"); YourScreen.endText(); ``` - ### `endText()` #### Description - End the process of displaying and optionally scrolling text. #### Syntax @@ -846,13 +758,10 @@ End the process of displaying and optionally scrolling text. ``` YourScreen.endText() YourScreen.endText(scrollDirection) - ``` - #### Parameters - scrollDirection: (optional) the direction to scroll, defaults to NO_SCROLL if not provided. Valid options are NO_SCROLL, SCROLL_LEFT, SCROLL_RIGHT, SCROLL_UP, SCROLL_DOWN #### Returns @@ -867,28 +776,22 @@ YourScreen.print("Hi"); YourScreen.endText(); ``` - ### `textScrollSpeed()` #### Description - Sets the text scrolling speed, the speed controls the delay in milliseconds between scrolling each pixel. #### Syntax ``` YourScreen.textScrollSpeed(speed) - ``` - #### Parameters - speed: scroll speed - #### Returns Nothing @@ -901,4 +804,3 @@ YourScreen.textScrollSpeed(500); YourScreen.print("Hello There!"); YourScreen.endText(true); ``` - diff --git a/docs/readme.md b/docs/readme.md index 92ff8db..4260bca 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,6 +1,6 @@ # Arduino Graphics Library -This is a library that allows you to draw and write on screens with graphical primitives; it requires a specific hardware interfacce library to drive the screen you are using, therefore every screen type should have its own hardware specific library. +This is a library that allows you to draw and write on screens with graphical primitives; it requires a specific hardware interface library to drive the screen you are using, therefore every screen type should have its own hardware specific library. To use this library diff --git a/examples/ASCIIDraw/ASCIIDraw.ino b/examples/ASCIIDraw/ASCIIDraw.ino index 398a40d..dfb6bb3 100644 --- a/examples/ASCIIDraw/ASCIIDraw.ino +++ b/examples/ASCIIDraw/ASCIIDraw.ino @@ -14,8 +14,9 @@ #include -const byte canvasWidth = 61; -const byte canvasHeight = 27; +const byte fontSize = 3; +const byte canvasWidth = fontSize * (5 * 7) + 26; +const byte canvasHeight = fontSize * 7 + 20; class ASCIIDrawClass : public ArduinoGraphics { public: @@ -85,6 +86,7 @@ void setup() { ASCIIDraw.stroke('@', 0, 0); const char text[] = "ARDUINO"; ASCIIDraw.textFont(Font_5x7); + ASCIIDraw.textSize(fontSize); const byte textWidth = strlen(text) * ASCIIDraw.textFontWidth(); const byte textHeight = ASCIIDraw.textFontHeight(); const byte textX = (canvasWidth - textWidth) / 2; diff --git a/extras/generate_font.py b/extras/generate_font.py index 016f634..48efb84 100755 --- a/extras/generate_font.py +++ b/extras/generate_font.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# This file is part of the MKRRGBMatrix library. +# This file is part of the ArduinoGraphics library. # Copyright (c) 2018 Arduino SA. All rights reserved. # # This library is free software; you can redistribute it and/or diff --git a/library.properties b/library.properties index 3ee6369..8378e06 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoGraphics -version=1.1.2 +version=1.1.3 author=Arduino maintainer=Arduino sentence=Core graphics library for Arduino. diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index b47c2b5..852c273 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -26,7 +26,9 @@ ArduinoGraphics::ArduinoGraphics(int width, int height) : _width(width), _height(height), - _font(NULL) + _font(NULL), + _textSizeX(1), + _textSizeY(1) { } @@ -94,6 +96,11 @@ void ArduinoGraphics::clear() } } +void ArduinoGraphics::clear(int x, int y) +{ + set(x, y, _backgroundR, _backgroundB, _backgroundG); +} + void ArduinoGraphics::fill(uint8_t r, uint8_t g, uint8_t b) { _fill = true; @@ -241,7 +248,7 @@ void ArduinoGraphics::text(const char* str, int x, int y) uint8_t const c = (uint8_t)*str++; if (c == '\n') { - y += _font->height; + y += _font->height * _textSizeY; } else if (c == '\r') { x = 0; } else if (c == 0xc2 || c == 0xc3) { @@ -254,10 +261,10 @@ void ArduinoGraphics::text(const char* str, int x, int y) } if (b) { - bitmap(b, x, y, _font->width, _font->height); + bitmap(b, x, y, _font->width, _font->height, _textSizeX, _textSizeY); } - x += _font->width; + x += _font->width * _textSizeX; } } } @@ -269,38 +276,51 @@ void ArduinoGraphics::textFont(const Font& which) int ArduinoGraphics::textFontWidth() const { - return (_font ? _font->width : 0); + return (_font ? _font->width * _textSizeX : 0); } int ArduinoGraphics::textFontHeight() const { - return (_font ? _font->height : 0); + return (_font ? _font->height* _textSizeY : 0); } -void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int width, int height) +void ArduinoGraphics::textSize(uint8_t sx, uint8_t sy) { - if (!_stroke) { + _textSizeX = (sx > 0)? sx : 1; + _textSizeY = (sy > 0)? sy : 1; +} + + +void ArduinoGraphics::bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x, uint8_t scale_y) { + if (!_stroke || !scale_x || !scale_y) { return; } - if ((data == NULL) || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > _height)) { + if ((data == nullptr) || ((x + (w * scale_x) < 0)) || ((y + (h * scale_y) < 0)) || (x > _width) || (y > _height)) { // offscreen return; } - for (int j = 0; j < height; j++) { + int xStart = x; + for (int j = 0; j < h; j++) { uint8_t b = data[j]; - - for (int i = 0; i < width; i++) { - if (b & (1 << (7 - i))) { - set(x + i, y + j, _strokeR, _strokeG, _strokeB); - } else { - set(x + i, y + j, _backgroundR, _backgroundG, _backgroundB); + for (uint8_t ys = 0; ys < scale_y; ys++) { + if (ys >= _height) return; + x = xStart; // reset for each row + for (int i = 0; i < w; i++) { + if (b & (1 << (7 - i))) { + for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _strokeR, _strokeG, _strokeB); + } else { + for (uint8_t xs = 0; xs < scale_x; xs++) set(x++, y, _backgroundR, _backgroundG, _backgroundB); + } + if (x >= _width) break; } + y++; } } } + void ArduinoGraphics::imageRGB(const Image& img, int x, int y, int width, int height) { const uint8_t* data = img.data(); @@ -359,7 +379,7 @@ void ArduinoGraphics::image(const Image& img, int x, int y) void ArduinoGraphics::image(const Image& img, int x, int y, int width, int height) { - if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > height)) { + if (!img || ((x + width) < 0) || ((y + height) < 0) || (x > _width) || (y > _height)) { // offscreen return; } @@ -438,7 +458,7 @@ void ArduinoGraphics::endText(int scrollDirection) beginDraw(); int const text_x = _textX - i; text(_textBuffer, text_x, _textY); - bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height); + bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textSizeX, _textSizeY); endDraw(); delay(_textScrollSpeed); @@ -450,7 +470,7 @@ void ArduinoGraphics::endText(int scrollDirection) beginDraw(); int const text_x = _textX - (scrollLength - i - 1); text(_textBuffer, text_x, _textY); - bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height); + bitmap(_font->data[0x20], text_x - 1, _textY, 1, _font->height, _textSizeX, _textSizeY); endDraw(); delay(_textScrollSpeed); @@ -462,7 +482,7 @@ void ArduinoGraphics::endText(int scrollDirection) beginDraw(); int const text_y = _textY - i; text(_textBuffer, _textX, text_y); - bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1); + bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textSizeX, _textSizeY); endDraw(); delay(_textScrollSpeed); @@ -474,7 +494,7 @@ void ArduinoGraphics::endText(int scrollDirection) beginDraw(); int const text_y = _textY - (scrollLength - i - 1); text(_textBuffer, _textX, text_y); - bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1); + bitmap(_font->data[0x20], _textX, text_y - 1, _font->width, 1, _textSizeX, _textSizeY); endDraw(); delay(_textScrollSpeed); diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index 1f1c914..6776f86 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -51,6 +51,7 @@ class ArduinoGraphics : public Print { void background(uint8_t r, uint8_t g, uint8_t b); void background(uint32_t color); void clear(); + void clear(int x, int y); void fill(uint8_t r, uint8_t g, uint8_t b); void fill(uint32_t color); void noFill(); @@ -70,6 +71,8 @@ class ArduinoGraphics : public Print { virtual void text(const char* str, int x = 0, int y = 0); virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); } virtual void textFont(const Font& which); + virtual void textSize(uint8_t s) {textSize(s, s);} + virtual void textSize(uint8_t sx, uint8_t sy); virtual int textFontWidth() const; virtual int textFontHeight() const; @@ -91,7 +94,8 @@ class ArduinoGraphics : public Print { virtual void textScrollSpeed(unsigned long speed = 150); protected: - virtual void bitmap(const uint8_t* data, int x, int y, int width, int height); + virtual void bitmap(const uint8_t* data, int x, int y, int w, int h, uint8_t scale_x = 1, + uint8_t scale_y = 1); virtual void imageRGB(const Image& img, int x, int y, int width, int height); virtual void imageRGB24(const Image& img, int x, int y, int width, int height); virtual void imageRGB16(const Image& img, int x, int y, int width, int height); @@ -114,6 +118,8 @@ class ArduinoGraphics : public Print { uint8_t _textR, _textG, _textB; int _textX; int _textY; + uint8_t _textSizeX; + uint8_t _textSizeY; unsigned long _textScrollSpeed; };