Be a good c-tizen and split paper.h into header and implementation

This commit is contained in:
arne 2025-10-05 09:45:14 +02:00
commit f702715aa3
3 changed files with 319 additions and 311 deletions

309
main/paper.c Normal file
View file

@ -0,0 +1,309 @@
#include "paper.h"
// this file defines lua bindings to the epdiy e-ink library.
//
// see https://epdiy.readthedocs.io/en/latest/api.html for epdiy documentation.
// see https://www.lua.org/manual/5.4/manual.html for lua documentation.
EpdiyHighlevelState hl;
uint8_t* fb;
static int init (lua_State *L) {
epd_init(&epd_board_lilygo_t5_47, &ED047TC1, EPD_LUT_64K);
// epd_set_vcom(1560);
hl = epd_hl_init(WAVEFORM);
epd_set_rotation(EPD_ROT_LANDSCAPE);
fb = epd_hl_get_framebuffer(&hl);
return 0;
}
static int clear (lua_State *L){
epd_clear();
return 0;
}
static int get_width (lua_State *L) {
int width = epd_rotated_display_width();
lua_pushnumber(L, width);
return 1;
}
static int get_height (lua_State *L) {
int height = epd_rotated_display_height();
lua_pushnumber(L, height);
return 1;
}
static int set_rotation (lua_State *L) {
char *rotation = lua_tostring(L, -1);
luaL_argcheck(
L,
strcmp(rotation, "landscape") == 0 ||
strcmp(rotation, "portrait") == 0 ||
strcmp(rotation, "inverted_landscape") == 0 ||
strcmp(rotation, "inverted_portrait") == 0,
1,
"expected `rotation` to be either 'landscape', 'portrait', 'inverted_landscape' or 'inverted_portrait'"
);
epd_set_rotation(
strcmp(rotation, "landscape") == 0
? EPD_ROT_LANDSCAPE
: strcmp(rotation, "portrait") == 0
? EPD_ROT_PORTRAIT
: strcmp(rotation, "inverted_landscape") == 0
? EPD_ROT_INVERTED_LANDSCAPE
: EPD_ROT_INVERTED_PORTRAIT
);
return 0;
}
static int get_rotation (lua_State *L) {
int rotation = epd_get_rotation();
lua_pushstring(
L,
rotation == EPD_ROT_LANDSCAPE
? "landscape"
: rotation == EPD_ROT_PORTRAIT
? "portrait"
: rotation == EPD_ROT_INVERTED_LANDSCAPE
? "inverted_landscape"
: "inverted_portrait"
);
return 1;
}
static int draw_pixel (lua_State *L) {
int x, y, color;
int isnum;
x = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x` to be a number");
y = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y` to be a number");
color = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 3, "expected `color` to be a number within [0, 255]");
epd_draw_pixel(x, y, color, fb);
return 0;
}
static int draw_line (lua_State *L) {
int x1, y1, x2, y2, color;
int isnum;
x1 = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x1` to be a number");
y1 = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y1` to be a number");
x2 = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `x2` to be a number");
y2 = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum, 4, "expected `y2` to be a number");
color = lua_tonumberx(L, 5, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 5, "expected `color` to be a number within [0, 255]");
epd_draw_line(x1, y1, x2, y2, color, fb);
return 0;
}
static int draw_hline (lua_State *L) {
int x, y, length, color;
int isnum;
x = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x` to be a number");
y = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y` to be a number");
length = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `length` to be a number");
color = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 4, "expected `color` to be a number within [0, 255]");
epd_draw_hline(x, y, length, color, fb);
return 0;
}
static int draw_vline (lua_State *L) {
int x, y, length, color;
int isnum;
x = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x` to be a number");
y = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y` to be a number");
length = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `length` to be a number");
color = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 4, "expected `color` to be a number within [0, 255]");
epd_draw_vline(x, y, length, color, fb);
return 0;
}
static int draw_circle (lua_State *L) {
int x, y, radius, color;
int isnum;
x = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x` to be a number");
y = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y` to be a number");
radius = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `radius` to be a number");
color = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 4, "expected `color` to be a number within [0, 255]");
epd_draw_circle(x, y, radius, color, fb);
return 0;
}
static int fill_circle (lua_State *L) {
int x, y, radius, color;
int isnum;
x = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x` to be a number");
y = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y` to be a number");
radius = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `radius` to be a number");
color = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 4, "expected `color` to be a number within [0, 255]");
epd_fill_circle(x, y, radius, color, fb);
return 0;
}
static int draw_rect (lua_State *L) {
int x1, y1, x2, y2, color;
int isnum;
x1 = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x1` to be a number");
y1 = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y1` to be a number");
x2 = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `x2` to be a number");
y2 = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum, 4, "expected `y2` to be a number");
color = lua_tonumberx(L, 5, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 5, "expected `color` to be a number within [0, 255]");
EpdRect rect = {
.x = x1,
.y = y1,
.width = x2 - x1,
.height = y2 - y1
};
epd_draw_rect(rect, color, fb);
return 0;
}
static int fill_rect (lua_State *L) {
int x1, y1, x2, y2, color;
int isnum;
x1 = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x1` to be a number");
y1 = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y1` to be a number");
x2 = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `x2` to be a number");
y2 = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum, 4, "expected `y2` to be a number");
color = lua_tonumberx(L, 5, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 5, "expected `color` to be a number within [0, 255]");
EpdRect rect = {
.x = x1,
.y = y1,
.width = x2 - x1,
.height = y2 - y1
};
epd_fill_rect(rect, color, fb);
return 0;
}
static int draw_triangle (lua_State *L) {
int x1, y1, x2, y2, x3, y3, color;
int isnum;
x1 = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x1` to be a number");
y1 = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y1` to be a number");
x2 = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `x2` to be a number");
y2 = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum, 4, "expected `y2` to be a number");
x3 = lua_tonumberx(L, 5, &isnum);
luaL_argcheck(L, isnum, 5, "expected `x3` to be a number");
y3 = lua_tonumberx(L, 6, &isnum);
luaL_argcheck(L, isnum, 6, "expected `y3` to be a number");
color = lua_tonumberx(L, 7, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 7, "expected `color` to be a number within [0, 255]");
epd_draw_triangle(x1, y1, x2, y2, x3, y3, color, fb);
return 0;
}
static int fill_triangle (lua_State *L) {
int x1, y1, x2, y2, x3, y3, color;
int isnum;
x1 = lua_tonumberx(L, 1, &isnum);
luaL_argcheck(L, isnum, 1, "expected `x1` to be a number");
y1 = lua_tonumberx(L, 2, &isnum);
luaL_argcheck(L, isnum, 2, "expected `y1` to be a number");
x2 = lua_tonumberx(L, 3, &isnum);
luaL_argcheck(L, isnum, 3, "expected `x2` to be a number");
y2 = lua_tonumberx(L, 4, &isnum);
luaL_argcheck(L, isnum, 4, "expected `y2` to be a number");
x3 = lua_tonumberx(L, 5, &isnum);
luaL_argcheck(L, isnum, 5, "expected `x3` to be a number");
y3 = lua_tonumberx(L, 6, &isnum);
luaL_argcheck(L, isnum, 6, "expected `y3` to be a number");
color = lua_tonumberx(L, 7, &isnum);
luaL_argcheck(L, isnum && color >= 0 && color < 256, 7, "expected `color` to be a number within [0, 255]");
epd_fill_triangle(x1, y1, x2, y2, x3, y3, color, fb);
return 0;
}
static int update (lua_State *L) {
int temperature = 25;
epd_poweron();
epd_hl_update_screen(&hl, MODE_GC16, temperature);
epd_poweroff();
return 0;
}
static const struct luaL_Reg paper[] = {
{"init", init},
{"clear", clear},
{"get_width", get_width},
{"get_height", get_height},
{"set_rotation", set_rotation},
{"get_rotation", get_rotation},
{"draw_pixel", draw_pixel},
{"draw_line", draw_line},
{"draw_hline", draw_hline},
{"draw_vline", draw_vline},
{"draw_circle", draw_circle},
{"fill_circle", fill_circle},
{"draw_rect", draw_rect},
{"fill_rect", fill_rect},
{"draw_triangle", draw_triangle},
{"fill_triangle", fill_triangle},
{"update", update},
{NULL, NULL},
};
int luaopen_paper (lua_State *L) {
luaL_newlib(L, paper);
return 1;
}