Skip to content

Commit

Permalink
Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
smadurange committed Jan 8, 2024
1 parent d9d9e52 commit fdb7290
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
10 changes: 7 additions & 3 deletions main/epd.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,21 @@ 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, "busy...");

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);
} while (busy == 0);

vTaskDelay((TickType_t) 200 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "ready");
ESP_LOGI(TAG, "display ready");
}

static inline void reset(void)
Expand Down
11 changes: 8 additions & 3 deletions main/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -922,24 +922,29 @@ 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 y_step = row_n / (price_max - price_min);

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) * y_step;
int y = row_n - ((data->prices[i] - price_min) / 100) * y_step;
if (y < 0)
y = line_width - 1;
if (y >= row_n)
Expand Down
11 changes: 7 additions & 4 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const static char *TAG = "app";

void app_main(void)
{
int prev_day;
int prev_day, ntp_rc;
time_t t;
char ts[20];
struct tm now;
Expand All @@ -38,17 +38,20 @@ void app_main(void)
wifi_connect();

ntp_init();
ntp_sync();
dht_init();
news_init();
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();

t = time(NULL);
now = *localtime(&t);
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", &now);
Expand All @@ -58,7 +61,7 @@ void app_main(void)
gui_draw_humid(&sc);
gui_draw_date(&sc, &now);

if (!stock || prev_day != now.tm_mday) {
if (!stock || (ntp_rc && prev_day != now.tm_mday)) {
stock_update();
stock = stock_get_item();
if (stock) {
Expand All @@ -82,7 +85,7 @@ void app_main(void)
epd_wake();
vTaskDelay(500 / portTICK_PERIOD_MS);
epd_draw(sc.fb, MAXLEN);
vTaskDelay(2000 / portTICK_PERIOD_MS);
vTaskDelay(1000 / portTICK_PERIOD_MS);
epd_sleep();

ESP_LOGI(TAG, "last updated at %s", ts);
Expand Down
16 changes: 11 additions & 5 deletions main/ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ void ntp_init(void)
setenv("TZ", "CST-8", 1);
tzset();

ESP_LOGI(TAG, "Initializing SNTP");
ESP_LOGI(TAG, "initializing SNTP");
esp_sntp_config_t sntp_conf = ESP_NETIF_SNTP_DEFAULT_CONFIG(CONFIG_SNTP_TIME_SERVER);
ESP_ERROR_CHECK(esp_netif_sntp_init(&sntp_conf));
}

void ntp_sync(void)
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)
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
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");
return 0;
}
}

ESP_LOGI(TAG, "System time set.");
ESP_LOGI(TAG, "system time set");
return 1;
}
2 changes: 1 addition & 1 deletion main/ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

void ntp_init(void);

void ntp_sync(void);
int ntp_sync(void);

#endif /* NTP_H */
1 change: 1 addition & 0 deletions main/stock.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static esp_err_t http_evt_handler(esp_http_client_event_t *evt)
read_len = 0;
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
ESP_LOGD(TAG, "%s", *(char **) evt->user_data);
ESP_LOGD(TAG, "response status=%d", esp_http_client_get_status_code(evt->client));
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
Expand Down

0 comments on commit fdb7290

Please sign in to comment.