Skip to content

led_matrix: add support for text using ArduinoGraphics APIs #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// To use ArduinoGraphics APIs, please include BEFORE Arduino_LED_Matrix
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

void setup() {
Serial.begin(115200);
matrix.begin();

matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
// add some static text
// will only show "UNO" (not enough space on the display)
const char text[] = "UNO r4";
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText();

matrix.endDraw();

delay(2000);
}

void loop() {

// Make it scroll!
matrix.beginDraw();

matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(50);

// add the text
const char text[] = " Scrolling text! ";
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(SCROLL_LEFT);

matrix.endDraw();
}
51 changes: 47 additions & 4 deletions libraries/Arduino_LED_Matrix/src/Arduino_LED_Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#define NUM_LEDS 96

#if __has_include("ArduinoGraphics.h")
#include <ArduinoGraphics.h>
#define MATRIX_WITH_ARDUINOGRAPHICS
#endif

static const int pin_zero_index = 28;

static const uint8_t pins[][2] = {
Expand Down Expand Up @@ -138,10 +143,18 @@ static uint32_t reverse(uint32_t x)

static uint8_t __attribute__((aligned)) framebuffer[NUM_LEDS / 8];

class ArduinoLEDMatrix {
class ArduinoLEDMatrix
#ifdef MATRIX_WITH_ARDUINOGRAPHICS
: public ArduinoGraphics
#endif
{

public:
ArduinoLEDMatrix() {}
ArduinoLEDMatrix()
#ifdef MATRIX_WITH_ARDUINOGRAPHICS
: ArduinoGraphics(canvasWidth, canvasHeight)
#endif
{}
// TODO: find a better name
// autoscroll will be slower than calling next() at precise times
void autoscroll(uint32_t interval_ms) {
Expand All @@ -153,7 +166,7 @@ class ArduinoLEDMatrix {
void off(size_t pin) {
turnLed(pin, false);
}
void begin() {
int begin() {
uint8_t type;
uint8_t ch = FspTimer::get_available_timer(type);
// TODO: avoid passing "this" argument to remove autoscroll
Expand Down Expand Up @@ -231,7 +244,37 @@ class ArduinoLEDMatrix {
void setCallback(voidFuncPtr callBack){
_callBack = callBack;
}


#ifdef MATRIX_WITH_ARDUINOGRAPHICS
virtual void set(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
if (y >= canvasHeight || x >= canvasWidth) {
return;
}
// the r parameter is (mis)used to set the character to draw with
_canvasBuffer[y][x] = (r | g | b) > 0 ? 1 : 0;
}

void endText(int scrollDirection = NO_SCROLL) {
ArduinoGraphics::endText(scrollDirection);
renderBitmap(_canvasBuffer, canvasHeight, canvasWidth);
}

// display the drawing
void endDraw() {
ArduinoGraphics::endDraw();
// clear first line (no idea why it gets filled with random bits, probably some math not working fine for super small displays)
for (int i = 0; i < canvasWidth; i++) {
_canvasBuffer[0][i] = 0;
}
renderBitmap(_canvasBuffer, canvasHeight, canvasWidth);
}

private:
static const byte canvasWidth = 12;
static const byte canvasHeight = 8;
uint8_t _canvasBuffer[canvasHeight][canvasWidth] = {{0}};
#endif

private:
int _currentFrame = 0;
uint32_t _frameHolder[3];
Expand Down