Compare commits

...

10 Commits

Author SHA1 Message Date
true 11c5f1d161 Update platformio.ini to build with current upstream 2024-02-23 05:24:42 -08:00
true cbf5148b0f Toaster render fix, faster toasters with preload 2023-11-05 16:35:52 -08:00
true e9cee7a232 Added flying toasters 2023-11-05 14:43:52 -08:00
true 744a4418f1 Abstract, complicate, fix some minor bugs
Also attempted to add png support but shit is broken
2023-11-05 13:54:47 -08:00
true aa7bcf6c03 Added button handler, program selects 2023-11-05 11:56:41 -08:00
true 7013cac8f1 Multiple program support 2023-11-05 11:14:53 -08:00
true e3f23a1427 Add radar sweep background pattern 2023-11-05 10:28:50 -08:00
true 04759ec9df Split text rotation rendering into multiple steps
Doing this I can reuse code for other text transforms
2023-11-05 09:27:22 -08:00
true fba3dc7639 Update README.md
Add long filename warning for Windows users.
2023-11-04 19:32:31 -07:00
true dc22a163ae Add U8g2 dependency
U8g2 is a dependency of something but isn't being included on some people's computers. Adding as a strict dependency.
2023-11-04 14:46:13 -07:00
15 changed files with 990 additions and 184 deletions

View File

@ -2,6 +2,9 @@
This code implements a rotating nametag.
Edit main.cpp to set your name.
[Install PlatformIO](https://platformio.org/install) to build this firmware.
Edit main.cpp to set your name.
**NOTE FOR WINDOWS USERS:** Long filenames must be enabled to correctly open this repository in PlatformIO. *BEFORE* opening this repository, follow the instructions located on the [Arduino-Pico docs page](https://arduino-pico.readthedocs.io/en/latest/platformio.html#important-steps-for-windows-users-before-installing).

BIN
data/toast0.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/toast1.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/toast2.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/toast3.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/toast4.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
data/toast5.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

254
include/bmp.h Normal file
View File

@ -0,0 +1,254 @@
/*******************************************************************************
* BMP Class
*
* Rewrite from: https://github.com/Jaycar-Electronics/Arduino-Picture-Frame.git
******************************************************************************/
#ifndef _BMPCLASS_H_
#define _BMPCLASS_H_
#include <SD.h>
typedef void(BMP_DRAW_CALLBACK)(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h);
class BmpClass
{
public:
void draw(
File *f, BMP_DRAW_CALLBACK *bmpDrawCallback, bool useBigEndian,
int16_t x, int16_t y, int16_t widthLimit, int16_t heightLimit)
{
_bmpDrawCallback = bmpDrawCallback;
_useBigEndian = useBigEndian;
_heightLimit = heightLimit;
int16_t u, v;
uint32_t xend;
getbmpparms(f);
//validate bitmap
if ((bmtype == 19778) && (bmwidth > 0) && (bmheight > 0) && (bmbpp > 0))
{
//centre image
u = (widthLimit - bmwidth) / 2;
v = (heightLimit - bmheight) / 2;
u = (u < 0) ? x : x + u;
v = (v < 0) ? y : y + v;
xend = (bmwidth > widthLimit) ? widthLimit : bmwidth;
bmpRow = (uint16_t *)malloc(xend * 2);
if (!bmpRow)
{
Serial.println(F("bmpRow malloc failed."));
}
if (bmbpp < 9)
{
bmplt = (uint16_t *)malloc(bmpltsize * 2);
if (!bmplt)
{
Serial.println(F("bmplt malloc failed."));
}
bmloadplt(f); //load palette if palettized
drawbmpal(f, u, v, xend);
free(bmplt);
}
else if (bmbpp == 16)
{
// TODO: bpp 16 should have 3 pixel types
drawbmRgb565(f, u, v, xend);
}
else
{
drawbmtrue(f, u, v, xend);
}
free(bmpRow);
}
}
private:
void bmloadplt(File *f)
{
byte r, g, b;
if (bmpltsize == 0)
{
bmpltsize = 1 << bmbpp; //load default palette size
}
f->seek(54); //palette position in type 0x28 bitmaps
for (int16_t i = 0; i < bmpltsize; i++)
{
b = f->read();
g = f->read();
r = f->read();
f->read(); //dummy byte
bmplt[i] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
}
}
void drawbmpal(File *f, int16_t u, int16_t v, uint32_t xend)
{
byte bmbitmask;
int16_t i, ystart, bmppb, p, d;
int16_t x, y;
uint16_t c;
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line
bmppb = 8 / bmbpp; //pixels/byte
bmbitmask = ((1 << bmbpp) - 1); //mask for each pixel
ystart = 0;
if (bmheight > _heightLimit)
{
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
}
for (y = ystart; y < bmheight; y++)
{ //invert in calculation (y=0 is bottom)
f->seek(bmdataptr + y * bmbpl); //seek to start of line
x = 0;
p = 0;
while (x < xend)
{
if (p < 1)
{
d = f->read();
p = bmppb;
}
d = d << bmbpp;
c = bmplt[(bmbitmask & (d >> 8))];
bmpRow[x] = (_useBigEndian) ? ((c >> 8) | (c << 8)) : c;
p--;
x++;
}
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
}
}
// draw 16-bit colour (RGB565) bitmap
void drawbmRgb565(File *f, int16_t u, int16_t v, uint32_t xend)
{
int16_t i, ystart;
uint32_t x, y;
byte lo, hi;
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line, due to 32bit chunks
ystart = 0;
if (bmheight > _heightLimit)
{
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
}
Serial.println(xend);
for (y = ystart; y < bmheight; y++)
{ //invert in calculation (y=0 is bottom)
f->seek(bmdataptr + (y * bmbpl)); //seek at start of line
for (x = 0; x < xend; x++)
{
lo = f->read();
hi = f->read();
if (_useBigEndian)
{
bmpRow[x] = hi | lo << 8;
}
else
{
bmpRow[x] = lo | hi << 8;
}
}
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
}
}
// draw true colour bitmap at (u,v) handles 24/32 not 16bpp yet
void drawbmtrue(File *f, int16_t u, int16_t v, uint32_t xend)
{
int16_t i, ystart;
uint32_t x, y;
byte r, g, b;
bmbpl = ((bmbpp * bmwidth + 31) / 32) * 4; //bytes per line, due to 32bit chunks
ystart = 0;
if (bmheight > _heightLimit)
{
ystart = bmheight - _heightLimit; //don't draw if it's outside screen
}
for (y = ystart; y < bmheight; y++)
{ //invert in calculation (y=0 is bottom)
f->seek(bmdataptr + y * bmbpl); //seek at start of line
for (x = 0; x < xend; x++)
{
b = f->read();
g = f->read();
r = f->read();
if (bmbpp == 32)
{
f->read(); //dummy byte for 32bit
}
bmpRow[x] = (_useBigEndian) ? ((r & 0xf8) | (g >> 5) | ((g & 0x1c) << 11) | ((b & 0xf8) << 5)) : (((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3));
}
_bmpDrawCallback(u, v + bmheight - 1 - y, bmpRow, xend, 1);
}
}
void getbmpparms(File *f)
{ //load into globals as ints-some parameters are 32 bit, but we can't handle this size anyway
byte h[48]; //header is 54 bytes typically, but we don't need it all
int16_t i;
f->seek(0); //set start of file
for (i = 0; i < 48; i++)
{
h[i] = f->read(); //read header
}
bmtype = h[0] + (h[1] << 8); //offset 0 'BM'
bmdataptr = h[10] + (h[11] << 8); //offset 0xA pointer to image data
bmhdrsize = h[14] + (h[15] << 8); //dib header size (0x28 is usual)
//files may vary here, if !=28, unsupported type, put default values
bmwidth = 0;
bmheight = 0;
bmbpp = 0;
bmpltsize = 0;
if ((bmhdrsize == 0x28) || (bmhdrsize == 0x38))
{
bmwidth = h[18] + (h[19] << 8); //width
bmheight = h[22] + (h[23] << 8); //height
bmbpp = h[28] + (h[29] << 8); //bits per pixel
bmpltsize = h[46] + (h[47] << 8); //palette size
}
// Serial.printf("bmtype: %d, bmhdrsize: %d, bmwidth: %d, bmheight: %d, bmbpp: %d\n", bmtype, bmhdrsize, bmwidth, bmheight, bmbpp);
}
byte isbmp(char n[])
{ //check if bmp extension
int16_t k;
k = strlen(n);
if (k < 5)
{
return 0; //name not long enough
}
if (n[k - 1] != 'P')
{
return 0;
}
if (n[k - 2] != 'M')
{
return 0;
}
if (n[k - 3] != 'B')
{
return 0;
}
if (n[k - 4] != '.')
{
return 0;
}
return 1; //passes all tests
}
BMP_DRAW_CALLBACK *_bmpDrawCallback;
bool _useBigEndian;
int16_t _heightLimit;
uint16_t bmtype, bmdataptr; //from header
uint32_t bmhdrsize, bmwidth, bmheight, bmbpp, bmpltsize; //from DIB Header
uint16_t bmbpl; //bytes per line- derived
uint16_t *bmplt; //palette- stored encoded for LCD
uint16_t *bmpRow;
};
#endif // _BMPCLASS_H_

25
include/btn.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __INC_BTN_H
#define __INC_BTN_H
#include <stdint.h>
#define SR_CLK 0
#define SR_DAT 1
#define SR_LAT 16
#define BTN_STICK_UP (1 << 14)
#define BTN_STICK_DN (1 << 11)
#define BTN_STICK_LF (1 << 12)
#define BTN_STICK_RT (1 << 15)
#define BTN_STICK_SEL (1 << 13)
#define BTN_READ0 17
#define BTN_READ1 18
uint16_t btn_read();
#endif

17
include/pngfile.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef _INC_PNGFILE_H
#define _ING_PNGFILE_H
#include <PNGDEC.h>
#include <stdint.h>
#include <FS.h>
void *png_open(const char *filename, int32_t *size);
void png_close(void *handle);
int32_t png_read(PNGFILE *handle, uint8_t *buffer, int32_t length);
int32_t png_seek(PNGFILE *handle, int32_t position);
#endif

View File

@ -10,10 +10,10 @@
[env:supercon7-badge-pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = rpipico
board = pico
framework = arduino
board_build.filesystem_size = 1m
board_build.core = earlephilhower
board_build.filesystem_size = 1.4m
debug_build_flags =
-g
@ -21,6 +21,8 @@ debug_build_flags =
lib_deps =
moononournation/GFX Library for Arduino@^1.3.8
olikraus/U8g2@^2.35.7
bitbank2/PNGdec@^1.0.1
platform_packages =
toolchain-gccarmnoneeabi@1.100301.220327

47
src/btn.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <Arduino.h>
#include "btn.h"
uint16_t btn_mask;
uint16_t btn_last;
uint16_t btn_flip;
void sr_send(uint16_t dat)
{
digitalWrite(SR_LAT, LOW);
uint16_t mask = 0x8000;
for (uint8_t i = 0; i < 16; i++) {
digitalWrite(SR_DAT, dat & mask ? HIGH : LOW);
digitalWrite(SR_CLK, HIGH);
mask >>= 1;
digitalWrite(SR_CLK, LOW);
}
digitalWrite(SR_LAT, HIGH);
__NOP(); __NOP();
digitalWrite(SR_LAT, LOW);
}
uint16_t btn_read()
{
btn_last = btn_mask;
// read joystick only for now
for (uint8_t i = 11; i < 16; i++) {
sr_send(1 << i);
btn_mask &= ~(1 << i);
if (!digitalRead(BTN_READ1)) {
btn_mask |= (1 << i);
}
}
btn_flip = btn_mask ^ btn_last;
return btn_flip & btn_mask;
}

View File

@ -9,142 +9,236 @@ extern "C" {
#include "sin1.h"
}
#include <LittleFS.h>
#include <SD.h>
#include "bmp.h"
static BmpClass bmp;
File fbmp;
#include "btn.h"
const char name[] = "Your Name";
uint8_t fgprog = 1; // default
uint8_t bgprog = 7; // programs
#define SPI0_SCK 2
#define SPI0_MISO GFX_NOT_DEFINED
#define SPI0_MOSI 3
#define DISP_NSS 1
#define DISP_RESET 4
#define DISP_DC 5
#define SPI0_SCK 2
#define SPI0_MISO GFX_NOT_DEFINED
#define SPI0_MOSI 3
#define TEXT_SIZE 5
#define BORDER_COLOR DARKGREY
#define ROT_SIZE 70
#define DISP_NSS 1
#define DISP_RESET 4
#define DISP_DC 5
#define LETTER_SPACING 1942 // user-configurable
#define TEXT_MAX_SIZE 5
#define TEXT_BORDER_COLOR DARKGREY
#define ROT_SIZE (14 * TEXT_MAX_SIZE)
#define TRANSPARENT_BG CYAN
#define LETTER_SPACING 1942 // user-configurable
#define MAX_TOASTERS 9
#define TOAST_RANDOM 9
Arduino_DataBus *bus = new Arduino_RPiPicoSPI(DISP_DC, DISP_NSS, SPI0_SCK, SPI0_MOSI, SPI0_MISO, spi0);
Arduino_GFX *disp = new Arduino_GC9A01(bus, DISP_RESET, 0 /* rotation */, true /* IPS */);
Arduino_Canvas *dout = new Arduino_Canvas(240 /* width */, 240 /* height */, disp);
Arduino_Canvas *drot = new Arduino_Canvas(ROT_SIZE, ROT_SIZE, disp);
Arduino_Canvas *drot = new Arduino_Canvas(ROT_SIZE, ROT_SIZE, NULL);
Arduino_Canvas *d64 = new Arduino_Canvas(64, 64, NULL);
Arduino_Canvas *toastbmp[6] = {
new Arduino_Canvas(64, 64, NULL),
new Arduino_Canvas(64, 64, NULL),
new Arduino_Canvas(64, 64, NULL),
new Arduino_Canvas(64, 64, NULL),
new Arduino_Canvas(64, 64, NULL),
new Arduino_Canvas(64, 64, NULL)
};
color_hsv hsv = {0, 255, 255};
color_rgb rgb;
void setup()
uint8_t toast_cb_idx = 0;
static void bmp_cb(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h)
{
Serial.begin(115200);
uint16_t *src, *dst;
Serial.println("starting...");
// Serial.printf("Draw pos = %d, %d. size = %d x %d\n", x, y, w, h);
d64->draw16bitRGBBitmap(x, y, bitmap, w, h);
delay(50);
// was having problems printing directly to the toastbmp buffer
// so rendering to temporary one and then copying
src = d64->getFramebuffer();
dst = toastbmp[toast_cb_idx]->getFramebuffer();
bus->begin(90000000);
if (!disp->begin()) {
Serial.println("failed to start disp");
}
dout->begin();
dout->fillScreen(BLACK);
dout->flush();
drot->begin();
drot->setFont(u8g2_font_cubic11_h_cjk); // u8g2_font_spleen16x32_mr
drot->setTextSize(TEXT_SIZE);
for (x = 0; x < (64 * 64); x++) {
*dst++ = *src++; // [(x % 64) + ((x / 64) * ROT_SIZE)];
}
}
void rotate_letter(uint8_t idx, int16_t rot, int16_t out_x, int16_t out_y, uint16_t color)
void setup()
{
int16_t x, y;
int16_t nx, ny;
int16_t sx, sy;
Serial.begin(115200);
int16_t start_x, start_y;
Serial.println("starting...");
int16_t x_width;
delay(50);
uint16_t *src, *dst;
uint16_t src_pxl;
uint16_t bgcolor = CYAN;
bus->begin(90000000);
uint8_t is_color;
disp->begin();
dout->begin();
dout->fillScreen(BLACK);
dout->flush();
// clear the rotation buffer
drot->fillScreen(CYAN);
drot->begin();
drot->setFont(u8g2_font_cubic11_h_cjk); // u8g2_font_spleen16x32_mr
// print the letter to the center of the rotation buffer
drot->setCursor(1, (TEXT_SIZE * 10));
drot->setTextColor(color);
drot->print(name[idx]);
x_width = drot->getCursorX() - TEXT_SIZE + 1;
d64->begin();
d64->fillScreen(BLACK);
// framebuffer pointers
src = drot->getFramebuffer();
dst = dout->getFramebuffer();
// buttons
pinMode(SR_CLK, OUTPUT);
pinMode(SR_DAT, OUTPUT);
pinMode(SR_LAT, OUTPUT);
// naive bordering
for (y = 0; y < ROT_SIZE; y++) {
is_color = 0;
for (x = 0; x < x_width; x++) {
if (is_color) {
if (src[x + (y * ROT_SIZE)] != color) {
is_color = 0;
src[x + (y * ROT_SIZE)] = BORDER_COLOR;
}
} else {
if (src[x + (y * ROT_SIZE)] == color) {
is_color = 1;
src[x + (y * ROT_SIZE) - 1] = BORDER_COLOR;
}
}
}
}
pinMode(BTN_READ0, INPUT);
pinMode(BTN_READ1, INPUT);
for (x = 0; x < x_width; x++) {
// filesystem
LittleFS.begin();
// preload toasters
char fn[20];
for (uint8_t i = 0; i < 6; i++) {
// start toast
toastbmp[i] = new Arduino_Canvas(64, 64, NULL);
toastbmp[i]->begin();
toastbmp[i]->fillScreen(BLUE); // should not be visible
// select toast file
sprintf(fn, "toast%i.bmp", i);
fbmp = LittleFS.open(fn, "r");
toast_cb_idx = i;
bmp.draw(&fbmp, bmp_cb, false /* useBigEndian */, 0 /* x */, 0 /* y */, 64, 64);
fbmp.close();
}
}
int16_t render_drot_printchar(const char *str, uint8_t idx, uint8_t size, uint16_t color)
{
if (size > TEXT_MAX_SIZE) size = TEXT_MAX_SIZE;
// clear the rotation buffer
drot->fillScreen(TRANSPARENT_BG);
// print the letter to the center of the rotation buffer
drot->setCursor(1, (size * 10));
drot->setTextColor(color);
drot->print(str[idx]);
return drot->getCursorX() - size + 1;
}
void render_drot_border(int8_t x_width, uint16_t text_color, uint16_t border_color)
{
uint16_t *src, *dst;
int16_t x, y;
uint8_t is_color;
// framebuffer pointers
src = drot->getFramebuffer();
dst = dout->getFramebuffer();
// naive bordering
for (y = 0; y < ROT_SIZE; y++) {
is_color = 0;
for (x = 0; x < x_width; x++) {
if (is_color) {
if (src[x + (y * ROT_SIZE)] != text_color) {
is_color = 0;
src[x + (y * ROT_SIZE)] = border_color;
}
} else {
if (src[x + (y * ROT_SIZE)] == text_color) {
is_color = 1;
src[x + (y * ROT_SIZE) - 1] = border_color;
}
}
}
}
for (x = 0; x < x_width; x++) {
is_color = 0;
for (y = 0; y < ROT_SIZE; y++) {
if (is_color) {
if (src[x + (y * ROT_SIZE)] != color) {
if (src[x + (y * ROT_SIZE)] != text_color) {
is_color = 0;
src[x + (y * ROT_SIZE)] = BORDER_COLOR;
src[x + (y * ROT_SIZE)] = border_color;
}
} else {
if (src[x + (y * ROT_SIZE)] == color) {
if (src[x + (y * ROT_SIZE)] == text_color) {
is_color = 1;
src[x + (y * ROT_SIZE) - ROT_SIZE] = BORDER_COLOR;
src[x + (y * ROT_SIZE) - ROT_SIZE] = border_color;
}
}
}
}
}
// position
start_x = out_x + 120;
start_y = out_y + 120;
/*
int16_t render_drot_wobble(int16_t x_width, int16_t rot, uint16_t idx, uint8_t len)
{
int16_t x, y;
int16_t nx, ny;
int16_t sx, sy;
// rotate+copy the buffer
for (y = 0; y < ROT_SIZE; y++) {
uint16_t *src, *dst;
uint16_t src_pxl;
uint8_t r;
r = rot >> 4;
// rotation
rx = cos1(rot);
rx = (out_x >> 9) + (out_x >> 11) + (out_x >> 12);
ry = sin1(rot);
ry = (out_y >> 9) + (out_y >> 11) + (out_y >> 12);
// position
int16_t start_x = out_x + 120;
int16_t start_y = out_y + 120;
rot += 0x6000;
// framebuffer pointers
src = drot->getFramebuffer();
dst = dout->getFramebuffer();
// rotate+copy the buffer
for (y = 0; y < ROT_SIZE; y++) {
for (x = 0; x < x_width; x++) {
src_pxl = src[x + (y * ROT_SIZE)];
sx = x + 1 - (x_width >> 1);
sy = y - (ROT_SIZE/2) + 1;
if (src_pxl != bgcolor) {
src_pxl = src[x + (y * ROT_SIZE)];
sx = x + 1 - (x_width >> 1);
sy = y - (ROT_SIZE/2) + 1;
if (src_pxl != TRANSPARENT_BG) {
nx = ((sx * cos1(rot)) - (sy * sin1(rot))) >> 15;
ny = ((sx * sin1(rot)) + (sy * cos1(rot))) >> 15;
nx += start_x;
ny += start_y;
if ((nx >= 0) && (nx < 240)) {
ny += start_y;
if ((nx >= 0) && (nx < 240)) {
if ((ny >= 0) && (ny < 240)) {
dst[nx + (ny*240)] = src_pxl;
}
@ -153,134 +247,444 @@ void rotate_letter(uint8_t idx, int16_t rot, int16_t out_x, int16_t out_y, uint1
}
}
}
*/
int16_t rot = 0;
int16_t render_drot_rotate(int16_t x_width, int16_t rot, int16_t idx, uint8_t len)
{
int16_t x, y;
int16_t nx, ny;
int16_t sx, sy;
#define HUE_MIN 0x2000-300
#define HUE_MAX 0x2000+300
#define HUE_STEP 3
int16_t out_x, out_y;
uint16_t *src, *dst;
uint16_t src_pxl;
int16_t spacing;
// spacing
spacing = rot - LETTER_SPACING;
// rotation
out_x = cos1(rot);
out_x = (out_x >> 9) + (out_x >> 11) + (out_x >> 12);
out_y = sin1(rot);
out_y = (out_y >> 9) + (out_y >> 11) + (out_y >> 12);
// position
int16_t start_x = out_x + 120;
int16_t start_y = out_y + 120;
rot += 0x6000;
// framebuffer pointers
src = drot->getFramebuffer();
dst = dout->getFramebuffer();
// rotate+copy the buffer
for (y = 0; y < ROT_SIZE; y++) {
for (x = 0; x < x_width; x++) {
src_pxl = src[x + (y * ROT_SIZE)];
sx = x + 1 - (x_width >> 1);
sy = y - (ROT_SIZE/2) + 1;
if (src_pxl != TRANSPARENT_BG) {
nx = ((sx * cos1(rot)) - (sy * sin1(rot))) >> 15;
ny = ((sx * sin1(rot)) + (sy * cos1(rot))) >> 15;
nx += start_x;
ny += start_y;
if ((nx >= 0) && (nx < 240)) {
if ((ny >= 0) && (ny < 240)) {
dst[nx + (ny*240)] = src_pxl;
}
}
}
}
}
return spacing;
}
void text_print_standard(
const char *str,
uint8_t text_size,
uint16_t fcolor,
uint16_t bcolor,
int16_t (*render)(int16_t, int16_t, int16_t, uint8_t),
int16_t rot)
{
uint8_t i;
int16_t x_width;
if (text_size > TEXT_MAX_SIZE) text_size = TEXT_MAX_SIZE;
drot->setTextSize(text_size);
for (i = 0; i < strlen(str); i++) {
x_width = render_drot_printchar(str, i, text_size, fcolor); // render character
render_drot_border(x_width, fcolor, bcolor); // add border to character
if (render) {
rot = render(x_width, rot, i, strlen(str));
}
}
}
int16_t trot = 0;
int16_t brot = 0;
int16_t huecycle = 0;
uint8_t huedir = 0;
int16_t satcycle = 0;
uint8_t satdir = 0;
void bg_huesatcycle()
void bg_huesatcycle(uint16_t hue_max, uint16_t hue_min, uint16_t hue_step)
{
dout->fillScreen(BLACK);
dout->fillScreen(BLACK);
// hue between limits
if (!huedir) huecycle += HUE_STEP; else huecycle -= HUE_STEP;
if (huecycle > HUE_MAX) {
huedir = 1; huecycle = HUE_MAX;
}
if (huecycle < HUE_MIN) {
huedir = 0; huecycle = HUE_MIN;
}
// hue between limits
if (!huedir) huecycle += hue_step; else huecycle -= hue_step;
if (huecycle > hue_max) {
huedir = 1; huecycle = hue_max;
}
if (huecycle < hue_min) {
huedir = 0; huecycle = hue_min;
}
// saturation
if (!satdir) satcycle++; else satcycle--;
if (satcycle > 240) {
satdir = 1;
satcycle = 240;
}
if (satcycle < 20) {
satdir = 0;
satcycle = 60;
}
// saturation
if (!satdir) satcycle++; else satcycle--;
if (satcycle > 240) {
satdir = 1;
satcycle = 240;
}
if (satcycle < 20) {
satdir = 0;
satcycle = 60;
}
hsv.h = huecycle >> 3;
hsv.s = satcycle;
hsv.h = huecycle >> 3;
hsv.s = satcycle;
// draw some circles
for (int8_t i = 120; i > 0; i--) {
hsv.h += 2;
hsv.h %= 1536;
hsv2rgb_8b(hsv.h, hsv.s, (i * 2), &rgb.r, &rgb.g, &rgb.b);
// draw some circles
for (int8_t i = 120; i > 0; i--) {
hsv.h += 2;
hsv.h %= 1536;
hsv2rgb_8b(hsv.h, hsv.s, (i * 2), &rgb.r, &rgb.g, &rgb.b);
dout->drawCircle(120, 120, i, dout->color565(rgb.r, rgb.g, rgb.b));
}
dout->drawCircle(120, 120, i, dout->color565(rgb.r, rgb.g, rgb.b));
}
}
void bg_rainbow_circles(uint8_t saturation)
void bg_rainbow_circles(uint8_t saturation, uint16_t hue_offset, uint8_t tunnelmode, uint16_t fillcolor)
{
uint16_t hue;
uint16_t hue;
uint8_t val;
// speed
hsv.h += 24;
// speed
hsv.h += 24;
// background fill ("X" pattern)
// hue = hsv.h + 512;
// hsv.h %= 1536;
// hsv2rgb_8b(hue, 255, 128, &rgb.r, &rgb.g, &rgb.b);
// dout->fillScreen(dout->color565(rgb.r, rgb.g, rgb.b));
dout->fillScreen(BLACK);
// background fill ("X" pattern)
// hue = hsv.h + 512;
// hsv.h %= 1536;
// hsv2rgb_8b(hue, 255, 128, &rgb.r, &rgb.g, &rgb.b);
// dout->fillScreen(dout->color565(rgb.r, rgb.g, rgb.b));
dout->fillScreen(fillcolor);
// draw some circles
hue = hsv.h;
for (int8_t i = 120; i > 0; i--) {
hue += 2;
hue %= 1536;
// "flat" mode, same brightness
hsv2rgb_8b(hue, saturation, 192, &rgb.r, &rgb.g, &rgb.b);
// "tunnel" mode, darker center
// hsv2rgb_8b(hue, saturation, (i * 2), &rgb.r, &rgb.g, &rgb.b);
// draw some circles
hue = hsv.h;
for (int8_t i = 120; i > 0; i--) {
hue += hue_offset;
hue %= 1536;
dout->drawCircle(120, 120, i, dout->color565(rgb.r, rgb.g, rgb.b));
}
val = tunnelmode ? (i * 2) : 192;
hsv2rgb_8b(hue, saturation, val, &rgb.r, &rgb.g, &rgb.b);
dout->drawCircle(120, 120, i, dout->color565(rgb.r, rgb.g, rgb.b));
}
}
void texthue_180degree()
void bg_radarsweep(int16_t rot, int16_t offset, uint8_t lines)
{
uint16_t hue;
color_rgb rgb;
uint16_t w;
hue = hsv.h + 768;
hue %= 1536;
hsv2rgb_8b(hue, 224, 64, &rgb.r, &rgb.g, &rgb.b);
int16_t x, y;
if (lines == 0xff) lines--;
dout->fillScreen(BLACK);
for (uint8_t i = 0; i < lines; i++) {
// linear fade
w = 256 * lines;
w -= (256 * i);
w /= lines;
w &= 0xff;
// todo: exponential fade or something
// get rotational point
x = cos1(rot);
x = (x >> 8);
y = sin1(rot);
y = (y >> 8);
hsv2rgb_8b((1536/3), 200, (i > 4) ? (w >> 1) : w, &rgb.r, &rgb.g, &rgb.b);
dout->drawLine(x + 120, y + 120, 128, 128, dout->color565(rgb.r, rgb.g, rgb.b));
rot -= offset;
}
}
void texthue_huesatcycle_circles()
struct Toasters {
int16_t x;
int16_t y;
uint16_t speed;
uint8_t type;
uint8_t frame;
} Toasters;
static struct Toasters toast[MAX_TOASTERS];
uint8_t toaster_count;
uint8_t toaster_frametimer;
void print_toast(uint8_t idx, int16_t out_x, int16_t out_y)
{
uint16_t hue;
hue = (huecycle - 0x2000);
if (hue > (1536 << 3)) hue += (1536 << 3);
hsv2rgb_8b(hue >> 3, 224, 64, &rgb.r, &rgb.g, &rgb.b);
int16_t x, y;
int16_t nx, ny;
uint16_t *src, *dst;
uint16_t src_pxl;
uint8_t frame = toast[idx].frame;
// framebuffer pointers
src = toastbmp[frame]->getFramebuffer();
dst = dout->getFramebuffer();
// copy to destination with transparency
for (y = 0; y < 64; y++) {
for (x = 0; x < 64; x++) {
src_pxl = src[x + (y * 64)];
if (src_pxl != TRANSPARENT_BG) {
nx = x + out_x;
ny = y + out_y;
if ((nx < 0) || (ny < 0)) continue;
if ((nx >= 240) || (ny >= 240)) continue;
dst[nx + (ny*240)] = src_pxl;
}
}
}
}
void bg_toasters()
{
uint16_t i;
// frame counter
toaster_frametimer++;
if (toaster_frametimer >= 2) {
toaster_frametimer = 0;
for (i = 0; i < MAX_TOASTERS; i++) {
if (toast[i].type) {
toast[i].frame++;
if (toast[i].frame >= 6) toast[i].frame = 0;
}
}
}
// clear screen
dout->fillScreen(BLACK);
// determine if we are doing new toasts
if (random(200) < TOAST_RANDOM) {
for (i = 0; i < MAX_TOASTERS; i++) {
if (!toast[i].type) {
toast[i].type = 1;
toast[i].frame = 0;
toast[i].x = 240;
toast[i].y = 64 - random(240);
break;
}
}
}
for (i = 0; i < MAX_TOASTERS; i++) {
switch (toast[i].type) {
case 0: continue;
case 1: { // normal toast
if (toast[i].x < -64) {
// toast is toasted
toast[i].type = 0;
} else {
// move toast
toast[i].x -= 3;
toast[i].y += 2;
print_toast(i, toast[i].x, toast[i].y);
}
}
}
}
}
void texthue_180degree(color_rgb *fc, color_rgb *bc)
{
uint16_t hue;
hue = hsv.h + 768;
hue %= 1536;
hsv2rgb_8b(hue, 224, 64, &fc->r, &fc->g, &fc->b);
// white border, screw it
bc->r = bc->g = bc->b = 255;
}
void texthue_huesatcycle(color_rgb *fc, color_rgb *bc)
{
uint16_t hue;
hue = (huecycle - 0x2000);
if (hue > (1536 << 3)) hue += (1536 << 3);
hsv2rgb_8b(hue >> 3, 224, 64, &fc->r, &fc->g, &fc->b);
// white border, screw it
bc->r = bc->g = bc->b = 255;
}
void loop()
{
uint16_t hue;
uint16_t hue;
color_rgb fc, bc;
// rainbow circles program
bg_rainbow_circles(200);
// texthue_180degree();
rgb.r = rgb.g = rgb.b = 0;
switch (bgprog) {
case 0: { // rainbow
bg_rainbow_circles(200, 2, 0, BLACK);
texthue_180degree(&fc, &bc);
break;
}
case 1: { // more colorful rainbow
hue = hsv.h;
hue += 768;
hue %= 1536;
hsv2rgb_8b(hue, 255, 128, &fc.r, &fc.g, &fc.b);
bg_rainbow_circles(200, 6, 0, dout->color565(fc.r, fc.g, fc.b));
texthue_180degree(&fc, &bc);
break;
}
case 2: { // rainbow tunnel
bg_rainbow_circles(200, 2, 1, BLACK);
texthue_180degree(&fc, &bc);
break;
}
// small hue shift with larger saturation shifts circles
// bg_huesatcycle_circles();
// texthue_huesatcycle();
case 3: { // desat red tunnel
bg_huesatcycle(0x0000, 0x0300, 3);
texthue_huesatcycle(&fc, &bc);
break;
}
case 4: { // desat green tunnel
bg_huesatcycle(0x0d00, 0x1300, 3);
texthue_huesatcycle(&fc, &bc);
break;
}
case 5: { // desat blue tunnel
bg_huesatcycle(0x1d00, 0x2300, 3);
texthue_huesatcycle(&fc, &bc);
break;
}
// text rotation
rot += 420 - 69;
case 6: { // radar sweep
brot += 400;
bg_radarsweep(brot, 25, 200);
fc.r = fc.g = fc.b = 240;
bc.r = bc.g = bc.b = 32;
break;
}
int16_t x, y;
int16_t d;
d = rot;
for (uint8_t i = 0; i < strlen(name); i++) {
x = cos1(d);
x = (x >> 9) + (x >> 11) + (x >> 12);
y = sin1(d); // sin1(d) * 21) / 32;
y = (y >> 9) + (y >> 11) + (y >> 12);
case 7: { // toasters
bg_toasters();
fc.r = fc.g = fc.b = 240;
bc.r = bc.g = bc.b = 32;
break;
}
}
rotate_letter(i, d + 0x6000, x, y, dout->color565(rgb.r, rgb.g, rgb.b));
// d -= 9; // for 7-bit version
d -= LETTER_SPACING; // for 15-bit version
}
switch (fgprog) {
case 0: { // circular rotation text
trot += 100;
break;
}
case 1: {
trot += 420 - 69;
break;
}
case 2: {
trot += 888;
break;
}
case 3: {
trot += 1500;
break;
}
}
dout->flush();
text_print_standard(name, TEXT_MAX_SIZE,
dout->color565(fc.r, fc.g, fc.b),
dout->color565(bc.r, bc.g, bc.b),
&render_drot_rotate, trot);
/*
case 2: { // sine loop
text_print_standard(name, TEXT_MAX_SIZE,
dout->color565(fc.r, fc.g, fc.b),
dout->color565(bc.r, bc.g, bc.b),
&render_drot_wobble, trot);
}
}
case 3: { // wobbly + sine loop
}
}
*/
dout->flush();
// read buttons
uint16_t btn;
btn = btn_read();
switch (btn) {
case BTN_STICK_UP: {
bgprog++;
bgprog %= 8;
break;
}
case BTN_STICK_DN: {
if (!bgprog) {
bgprog = 7;
} else bgprog--;
break;
}
case BTN_STICK_RT: {
fgprog++;
fgprog %= 4;
break;
}
case BTN_STICK_LF: {
if (!fgprog) {
fgprog = 3;
} else fgprog--;
}
}
}

42
src/pngfile.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "pngfile.h"
#include <LittleFS.h>
File fpng;
void *png_open(const char *filename, int32_t *size)
{
fpng = LittleFS.open(filename, "r");
if (!fpng || fpng.isDirectory()) {
// error
} else {
// good
}
return &fpng;
}
void png_close(void *handle)
{
if (fpng) fpng.close();
}
int32_t png_read(PNGFILE *handle, uint8_t *buffer, int32_t length)
{
if (!fpng) return 0;
Serial.println("png: read");
return fpng.read(buffer, length);
}
int32_t png_seek(PNGFILE *handle, int32_t position)
{
if (!fpng) return 0;
Serial.printf("png: seek %i\n", position);
return fpng.seek(position);
}

View File

@ -22,7 +22,7 @@
* "5 bit" large table = 32 values. The mask: all bit belonging to the table
* are 1, the all above 0.
*/
#define TABLE_BITS (5)
#define TABLE_BITS (7)
#define TABLE_SIZE (1<<TABLE_BITS)
#define TABLE_MASK (TABLE_SIZE-1)
@ -43,13 +43,25 @@
* at 0deg, 11.25deg, 22.5deg etc. The values are from -1 to 1 in Q15.
*/
static int16_t sin90[TABLE_SIZE+1] = {
0x0000,0x0647,0x0c8b,0x12c7,0x18f8,0x1f19,0x2527,0x2b1e,
0x30fb,0x36b9,0x3c56,0x41cd,0x471c,0x4c3f,0x5133,0x55f4,
0x5a81,0x5ed6,0x62f1,0x66ce,0x6a6c,0x6dc9,0x70e1,0x73b5,
0x7640,0x7883,0x7a7c,0x7c29,0x7d89,0x7e9c,0x7f61,0x7fd7,
0x0000,0x0192,0x0324,0x04b6,0x0647,0x07d9,0x096a,0x0afb,
0x0c8b,0x0e1b,0x0fab,0x1139,0x12c7,0x1455,0x15e1,0x176d,
0x18f8,0x1a82,0x1c0b,0x1d93,0x1f19,0x209f,0x2223,0x23a6,
0x2527,0x26a7,0x2826,0x29a3,0x2b1e,0x2c98,0x2e10,0x2f86,
0x30fb,0x326d,0x33de,0x354d,0x36b9,0x3824,0x398c,0x3af2,
0x3c56,0x3db7,0x3f16,0x4073,0x41cd,0x4325,0x447a,0x45cc,
0x471c,0x4869,0x49b3,0x4afa,0x4c3f,0x4d80,0x4ebf,0x4ffa,
0x5133,0x5268,0x539a,0x54c9,0x55f4,0x571d,0x5842,0x5963,
0x5a81,0x5b9c,0x5cb3,0x5dc6,0x5ed6,0x5fe2,0x60eb,0x61f0,
0x62f1,0x63ee,0x64e7,0x65dd,0x66ce,0x67bc,0x68a5,0x698b,
0x6a6c,0x6b4a,0x6c23,0x6cf8,0x6dc9,0x6e95,0x6f5e,0x7022,
0x70e1,0x719d,0x7254,0x7306,0x73b5,0x745e,0x7503,0x75a4,
0x7640,0x76d8,0x776b,0x77f9,0x7883,0x7908,0x7989,0x7a04,
0x7a7c,0x7aee,0x7b5c,0x7bc4,0x7c29,0x7c88,0x7ce2,0x7d38,
0x7d89,0x7dd5,0x7e1c,0x7e5e,0x7e9c,0x7ed4,0x7f08,0x7f37,
0x7f61,0x7f86,0x7fa6,0x7fc1,0x7fd7,0x7fe8,0x7ff5,0x7ffc,
0x7fff
};
/**
* Sine calculation using interpolated table lookup.
* Instead of radiants or degrees we use "turns" here. Means this