Skip to content

Instantly share code, notes, and snippets.

@tomoto
Last active June 8, 2021 18:50
Show Gist options
  • Save tomoto/9cf758768a5de0c3100a33935e7ff8cb to your computer and use it in GitHub Desktop.
Save tomoto/9cf758768a5de0c3100a33935e7ff8cb to your computer and use it in GitHub Desktop.
Generate M5StickC font sample image
#include <M5StickC.h>
void setup() {
M5.begin();
M5.Lcd.setRotation(3);
Serial.begin(115200);
}
void renderText(TFT_eSprite& s, int font, const GFXfont* freeFont) {
s.setTextColor(WHITE);
s.setTextSize(1);
if (freeFont) {
s.setFreeFont(freeFont);
s.setCursor(0, freeFont->yAdvance);
} else {
s.setTextFont(font);
s.setCursor(0, 0);
}
for (int i = 32; i < 256; i++) {
if (i < 128) {
s.printf("%c", i);
} else {
s.printf("%c%c", 0xc0 | (i >> 6), 0x80 | (i & 0x3f)); // UTF-8
}
}
}
void outputRLE(const char* buf, int w, int h) {
Serial.println(w);
Serial.println(h);
bool current = false;
int run = 0;
for (int i = 0; i < w * h; i++) {
if (!!buf[i] == current) {
run++;
} else {
Serial.println(run);
current = !current;
run = 1;
}
}
if (run) {
Serial.println(run);
}
}
struct FontSampleDef {
int width;
int height;
int font;
const GFXfont* freeFont;
};
FontSampleDef fontSampleDefs[] = {
{320, 80, 1, NULL}, // 8px ASCII + 0x80-0xff
{400, 80, 2, NULL}, // 16px ASCII
{640, 80, 4, NULL}, // 26px ASCII
{640, 160, 6, NULL}, // 48px number + am/pm
{640, 160, 7, NULL}, // 48px 7-seg number
{640, 160, 8, NULL}, // 75px number
{640, 160, 0, &Orbitron_Light_24},
{640, 160, 0, &Orbitron_Light_32},
{640, 160, 0, &Roboto_Thin_24},
{640, 160, 0, &Satisfy_24},
{640, 160, 0, &Yellowtail_32},
{0, 0, 0, NULL},
};
int fontSample = 0;
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) {
const FontSampleDef& fs = fontSampleDefs[fontSample];
TFT_eSprite sprite = TFT_eSprite(&M5.Lcd);
sprite.setColorDepth(8);
const char* buf = (char*)sprite.createSprite(fs.width, fs.height);
renderText(sprite, fs.font, fs.freeFont);
outputRLE(buf, fs.width, fs.height);
sprite.pushSprite(0, 0);
sprite.deleteSprite();
fontSample++;
if (!fontSampleDefs[fontSample].width) {
fontSample = 0;
}
}
delay(100);
}
import processing.serial.*;
Serial myPort;
void setup()
{
size(660, 340);
String portName = "COM3";
myPort = new Serial(this, portName, 115200);
}
final color BLACK = color(0, 0, 0);
final color WHITE = color(255, 255, 255);
int readInt() {
while (true) {
String s = myPort.readStringUntil('\n');
if (s != null && s.trim() != "") {
return Integer.parseInt(s.trim());
}
}
}
void draw()
{
if (myPort.available() > 0) {
int width = readInt();
int height = readInt();
System.out.println("width=" + width + ", height=" + height);
background(WHITE);
int i = 0;
boolean pixel = false;
while (i < width * height) {
int count = readInt();
stroke(pixel ? BLACK : WHITE);
while (count > 0) {
point(10 + i % width, 10 + i / width);
i++;
count--;
}
pixel = !pixel;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment