So, I noticed there wasn't a "built-in" function for displaying text on the R4 wifi. I decided to make my own sketch that could reliably display up to twelve characters in binary form via the 8x12 LED matrix.
In this example sketch you can send up to 128 characters through the serial monitor and it will display them via scrolling.
Fair warning - this isn't the most optimal way of doing things and there is a very real risk of fragmenting the heap.
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
const int bufferSize = 128;
byte frame[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
//Takes a char array, slices the char at postion zero and shifts
//the entire array to the right so that position one becomes zero
void removeFirstChar(char* charArray) {
int length = strlen(charArray);
if (length > 0) {
for (int i = 0; i < length; i++) {
charArray[i] = charArray[i + 1];
}
charArray[length - 1] = '\0'; // Null-terminate the string
}
}
//==================================================================
//This is a char array that is used for display purposes.
char str[bufferSize];
//Takes the str array and converts the individual chars into binary
//If the array is 12 or less it will display everything in a single frame
//If it is longer than 12, it will display the first 12 chars of array and
//call the removeFirstChar() function
void translate(char snip[]){
if (strlen(snip) <= 12){
for (int i=0;i<strlen(snip);i++) // loop through all chars in string
{
for (int j=7;j>=0;j--) // loop from bit-7 down to bit-0 (high-bit to low-bit)
//from str[i] read the bit-j and print it to matrix
frame[j][i] = bitRead(snip[i],j);
}
}
else{
for (int i=0;i<12;i++) // loop through all chars in string
{
for (int j=7;j>=0;j--) // loop from bit-7 down to bit-0 (high-bit to low-bit)
//from str[i] read the bit-j and print it to matrix
frame[j][i] = bitRead(snip[i],j);
}
removeFirstChar(str);
}
}
//=====================================================================================
void clearFrame() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 12; col++) {
frame[row][col] = 0; // Set each element to zero
}
}
matrix.renderBitmap(frame, 8, 12);
}
void cleanCharArray() {
for (int i = 0; i < bufferSize; i++) {
str[i] = '\0'; // Overwrite with null characters
}
}
String serialString;
void setup() {
// Starts the matrix and displays an empty matrix
matrix.begin();
matrix.renderBitmap(frame, 8, 12);
delay(1000);
Serial.begin(9600);
}
void loop() {
if (Serial.available()){
cleanCharArray();
clearFrame();
delay(100);
serialString = Serial.readString();
serialString.remove(serialString.length()-1); // Needed to remove excess character
serialString.toCharArray(str, bufferSize);
}
//Serial outputs str array and length
Serial.println(str);
Serial.println(strlen(str));
//translate() is used to display the binary portion
translate(str);
//Serial outputs str array and length after translate()
Serial.println(str);
Serial.println(strlen(str));
//Displays binary on led matrix
matrix.renderBitmap(frame, 8, 12);
delay(1000);
}