Skip to content

Commit

Permalink
Fix memory issue when using a sprite to draw the step chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
smadurange committed Jan 28, 2024
1 parent fdb7290 commit aa4f9ef
Show file tree
Hide file tree
Showing 12 changed files with 898 additions and 945 deletions.
6 changes: 3 additions & 3 deletions fonttool.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
print("Image={}, size={}x{}".format(src, width, height))

if i == 0:
o.write("const int font_{0}_height = {1};\n\n".format(font_size, height))
o.write("int font_{0}_height = {1};\n\n".format(font_size, height))

subprocess.run(["convert", src, "-threshold", "80%", "mon.jpg"])
subprocess.run(["convert", "mon.jpg", "-depth", "1", "-format", "'txt'", "mon.txt"])
Expand All @@ -48,8 +48,8 @@
res = width * height
image_size = (res // 8) + (res % 8 > 0)

o.write("const int {0}_width = {1};\n\n".format(id, width))
o.write("const unsigned char {0}_bmp[{1}] = {{\n\t".format(id, image_size))
o.write("int {0}_width = {1};\n\n".format(id, width))
o.write("unsigned char {0}_bmp[{1}] = {{\n\t".format(id, image_size))

with open("mon.txt", "r") as fd:
fd.readline()
Expand Down
10 changes: 5 additions & 5 deletions main/epd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define EPD_DC_PIN GPIO_NUM_27
#define EPD_RST_PIN GPIO_NUM_26
#define EPD_CLK_PIN GPIO_NUM_13
#define EPD_PWR_PIN GPIO_NUM_16
#define EPD_MOSI_PIN GPIO_NUM_14
#define EPD_BUSY_PIN GPIO_NUM_25

Expand Down Expand Up @@ -109,14 +110,9 @@ static void send_data(unsigned char data)
static inline void wait_until_idle(void)
{
int busy;
int retry = 0, max_retry = 100;

ESP_LOGI(TAG, "display busy...");
do {
if (retry >= max_retry) {
ESP_LOGE(TAG, "display is not responding");
return;
}
vTaskDelay((TickType_t) 20 / portTICK_PERIOD_MS);
send_cmd(0x71);
busy = gpio_get_level(EPD_BUSY_PIN);
Expand Down Expand Up @@ -150,6 +146,7 @@ void epd_init(void)
{
gpio_config_t io_cfg = {
.pin_bit_mask = ((1ULL << EPD_DC_PIN) |
(1ULL << EPD_PWR_PIN) |
(1ULL << EPD_RST_PIN) |
(1ULL << EPD_BUSY_PIN)),
.pull_up_en = true
Expand All @@ -158,9 +155,12 @@ void epd_init(void)
ESP_ERROR_CHECK(gpio_config(&io_cfg));

ESP_ERROR_CHECK(gpio_set_direction(EPD_DC_PIN, GPIO_MODE_OUTPUT));
ESP_ERROR_CHECK(gpio_set_direction(EPD_PWR_PIN, GPIO_MODE_OUTPUT));
ESP_ERROR_CHECK(gpio_set_direction(EPD_RST_PIN, GPIO_MODE_OUTPUT));
ESP_ERROR_CHECK(gpio_set_direction(EPD_BUSY_PIN, GPIO_MODE_INPUT));

gpio_set_level(EPD_PWR_PIN, 1);

spi_bus_config_t bus_cfg = {
.miso_io_num = -1,
.mosi_io_num = EPD_MOSI_PIN,
Expand Down
74 changes: 25 additions & 49 deletions main/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ static inline void gui_set_char(struct sprite *sp, char c, int bold)
}
break;
default:
sp->bmp = NULL;
sp->width = 0;
ESP_LOGW(TAG, "unknown char: %c(%d)", c, c);
break;
}
Expand All @@ -884,19 +886,21 @@ int gui_draw_str(struct scrn *sc, const char *s, int x0, int y0, int x_max, int
for (i = 0, x = x0, y = y0; s[i]; i++) {
if (s[i] != ' ') {
gui_set_char(&sp, s[i], bold);
if (sp.width + x < x_max) {
sp.offset_x = x;
sp.offset_y = y;
x += sp.width;
} else {
sp.offset_x = x0;
x = x0 + sp.width;
y += sp.height;
if (y > y_max)
return y;
sp.offset_y = y;
if (sp.width > 0) {
if (sp.width + x < x_max) {
sp.offset_x = x;
sp.offset_y = y;
x += sp.width;
} else {
sp.offset_x = x0;
x = x0 + sp.width;
y += sp.height;
if (y > y_max)
return y;
sp.offset_y = y;
}
scrn_draw(sc, &sp);
}
scrn_draw(sc, &sp);
} else {
x += space_width;
}
Expand All @@ -907,8 +911,6 @@ int gui_draw_str(struct scrn *sc, const char *s, int x0, int y0, int x_max, int

void gui_plot_stocks(struct scrn *sc, struct stock_item *data)
{
struct sprite sp;

int x_min = 18, x_max = 310;
int y_min = 92, y_max = 356;

Expand All @@ -922,56 +924,39 @@ void gui_plot_stocks(struct scrn *sc, struct stock_item *data)
int price_min = data->price_min;
int price_max = data->price_max;

ESP_LOGD(TAG, "price min=%d, price max=%d", price_min, price_max);

int x_step = col_n % data->prices_len >= data->prices_len / 2
? col_n / data->prices_len + 1
: col_n / data->prices_len;
if (x_step == 0)
x_step = 1;

int dy = (price_max - price_min) / 100;

int y_step = dy != 0 ? row_n / dy : 0;
if (y_step == 0)
y_step = 1;

ESP_LOGD(TAG, "x-step=%d, y-step=%d", x_step, y_step);

int n = col_n * row_n / 8;
unsigned char *buf = malloc(sizeof(char) * n);

for (int i = 0; i < n; i++)
buf[i] = 0x0;

for (int i = 0, x = 0, y_prev = 0; i < data->prices_len && x < col_n; i++, x += x_step) {
int y = row_n - ((data->prices[i] - price_min) / 100) * y_step;
if (y < 0)
y = line_width - 1;
if (y >= row_n)
y = row_n - 1;

ESP_LOGD(TAG, "price=%d, ref=%d, x=%d, y=%d, x_max=%d, y_max=%d",
data->prices[i],
data->price_ref,
x,
y,
col_n,
row_n);

// vertical step
if (x > 0) {
if (y_prev < y) {
for (int k = y_prev - line_width + 1; k <= y; k++) {
for (int j = x - line_width / 2, c = 0; c < line_width; j++, c++) {
int px = k * col_n + j;
buf[px / 8] |= (1ULL << (7 - px % 8));
int px = (k + y_min) * sc->width + (j + x_min);
sc->fb[px / 8] |= (1ULL << (7 - px % 8));
}
}
} else if (y_prev > y) {
for (int k = y_prev; k > y - line_width; k--) {
for (int j = x - line_width / 2, c = 0; c < line_width; j++, c++) {
int px = k * col_n + j;
buf[px / 8] |= (1ULL << (7 - px % 8));
int px = (k + y_min) * sc->width + (j + x_min);
sc->fb[px / 8] |= (1ULL << (7 - px % 8));
}
}
}
Expand All @@ -980,8 +965,8 @@ void gui_plot_stocks(struct scrn *sc, struct stock_item *data)
// horizontal step
for (int j = x; j < x + x_step; j++) {
for (int k = y, c = 0; c < line_width; k--, c++) {
int px = k * col_n + j;
buf[px / 8] |= (1ULL << (7 - px % 8));
int px = (k + y_min) * sc->width + (j + x_min);
sc->fb[px / 8] |= (1ULL << (7 - px % 8));
}
}

Expand Down Expand Up @@ -1010,21 +995,12 @@ void gui_plot_stocks(struct scrn *sc, struct stock_item *data)
if (i % 2 == 0) {
for (int j = x; j < x + dash_len; j++) {
for (int k = y_ref, c = 0; c < line_width; k--, c++) {
int px = k * col_n + j;
buf[px / 8] |= (1ULL << (7 - px % 8));
int px = (k + y_min) * sc->width + (j + x_min);
sc->fb[px / 8] |= (1ULL << (7 - px % 8));
}
}
}
}

sp.width = col_n;
sp.height = row_n;
sp.offset_x = x_min;
sp.offset_y = y_min;
sp.bmp = buf;

scrn_draw(sc, &sp);
free(buf);
}

static inline void gui_draw_panel_data(struct scrn *sc, char *s, int x)
Expand Down
23 changes: 5 additions & 18 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ const static char *TAG = "app";

void app_main(void)
{
int prev_day, ntp_rc;
time_t t;
char ts[20];
struct tm now;
struct scrn sc;
struct news_item *news;
struct stock_item *stock = NULL;

int ntp_rc = 0;

ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

Expand All @@ -43,11 +44,6 @@ void app_main(void)
stock_init();
epd_init();

ntp_rc = ntp_sync();
t = time(NULL);
now = *localtime(&t);
prev_day = now.tm_mday;

for (;;) {
if (!ntp_rc)
ntp_rc = ntp_sync();
Expand All @@ -61,18 +57,9 @@ void app_main(void)
gui_draw_humid(&sc);
gui_draw_date(&sc, &now);

if (!stock || (ntp_rc && prev_day != now.tm_mday)) {
stock_update();
stock = stock_get_item();
if (stock) {
gui_plot_stocks(&sc, stock);
ESP_LOGI(TAG, "updated stock data at %s", ts);
}
} else {
stock = stock_get_item();
if (stock)
gui_plot_stocks(&sc, stock);
}
stock = stock_get_item();
if (stock)
gui_plot_stocks(&sc, stock);

news = news_local_get();
if (news)
Expand Down
2 changes: 1 addition & 1 deletion main/ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int ntp_sync(void)
int retry = 0;
const int retry_count = 15;

while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT) {
ESP_LOGI(TAG, "waiting for system time to be set... (%d/%d)", retry, retry_count);
if (++retry >= retry_count) {
ESP_LOGE(TAG, "Failed to sync system time");
Expand Down
2 changes: 1 addition & 1 deletion main/scrn.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct sprite {
int height;
int offset_x;
int offset_y;
const unsigned char *bmp;
unsigned char *bmp;
};

void scrn_clear(struct scrn *sc);
Expand Down
Loading

0 comments on commit aa4f9ef

Please sign in to comment.