remember display mode

This commit is contained in:
Shuanglei Tao
2025-05-31 19:33:52 +08:00
parent 93d62cae05
commit 1f58efac20
6 changed files with 50 additions and 17 deletions

View File

@@ -16,6 +16,7 @@ typedef struct
uint8_t wakeup_pin;
uint8_t led_pin;
uint8_t en_pin;
uint8_t display_mode;
} epd_config_t;
#define EPD_CONFIG_SIZE (sizeof(epd_config_t) / sizeof(uint8_t))

View File

@@ -47,7 +47,7 @@ static void epd_gui_update(void * p_event_data, uint16_t event_size)
.temperature = epd->drv->read_temp(),
.voltage = EPD_ReadVoltage(),
};
DrawGUI(&data, epd->drv->write_image, p_epd->display_mode);
DrawGUI(&data, epd->drv->write_image, (display_mode_t)p_epd->config.display_mode);
epd->drv->refresh();
EPD_GPIO_Uninit();
@@ -77,6 +77,14 @@ static void on_disconnect(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
EPD_GPIO_Uninit();
}
static void epd_update_display_mode(ble_epd_t * p_epd, display_mode_t mode)
{
if (p_epd->config.display_mode != mode) {
p_epd->config.display_mode = mode;
epd_config_write(&p_epd->config);
}
}
static void epd_service_on_write(ble_epd_t * p_epd, uint8_t * p_data, uint16_t length)
{
NRF_LOG_DEBUG("[EPD]: on_write LEN=%d\n", length);
@@ -114,7 +122,7 @@ static void epd_service_on_write(ble_epd_t * p_epd, uint8_t * p_data, uint16_t l
} break;
case EPD_CMD_CLEAR:
p_epd->display_mode = MODE_NONE;
epd_update_display_mode(p_epd, MODE_PICTURE);
p_epd->epd->drv->clear(length > 1 ? p_data[1] : true);
break;
@@ -128,7 +136,7 @@ static void epd_service_on_write(ble_epd_t * p_epd, uint8_t * p_data, uint16_t l
break;
case EPD_CMD_REFRESH:
p_epd->display_mode = MODE_NONE;
epd_update_display_mode(p_epd, MODE_PICTURE);
p_epd->epd->drv->refresh();
break;
@@ -145,7 +153,7 @@ static void epd_service_on_write(ble_epd_t * p_epd, uint8_t * p_data, uint16_t l
uint32_t timestamp = (p_data[1] << 24) | (p_data[2] << 16) | (p_data[3] << 8) | p_data[4];
timestamp += (length > 5 ? (int8_t)p_data[5] : 8) * 60 * 60; // timezone
set_timestamp(timestamp);
p_epd->display_mode = length > 6 ? (display_mode_t)p_data[6] : MODE_CALENDAR;
epd_update_display_mode(p_epd, length > 6 ? (display_mode_t)p_data[6] : MODE_CALENDAR);
ble_epd_on_timer(p_epd, timestamp, true);
} break;
@@ -335,6 +343,8 @@ uint32_t ble_epd_init(ble_epd_t * p_epd)
{
uint8_t cfg[] = EPD_CFG_DEFAULT;
memcpy(&p_epd->config, cfg, sizeof(cfg));
if (p_epd->config.display_mode == 0xFF)
p_epd->config.display_mode = MODE_CALENDAR;
epd_config_write(&p_epd->config);
}
@@ -371,8 +381,8 @@ void ble_epd_on_timer(ble_epd_t * p_epd, uint32_t timestamp, bool force_update)
{
// Update calendar on 00:00:00, clock on every minute
if (force_update ||
(p_epd->display_mode == MODE_CALENDAR && timestamp % 86400 == 0) ||
(p_epd->display_mode == MODE_CLOCK && timestamp % 60 == 0)) {
(p_epd->config.display_mode == MODE_CALENDAR && timestamp % 86400 == 0) ||
(p_epd->config.display_mode == MODE_CLOCK && timestamp % 60 == 0)) {
epd_gui_update_event_t event = { p_epd, timestamp };
app_sched_event_put(&event, sizeof(epd_gui_update_event_t), epd_gui_update);
}

View File

@@ -94,7 +94,6 @@ typedef struct
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
epd_model_t *epd; /**< current EPD model */
epd_config_t config; /**< EPD config */
display_mode_t display_mode; /**< GUI display mode */
} ble_epd_t;
typedef struct

View File

@@ -119,16 +119,30 @@ static bool GetFestival(uint16_t year, uint8_t mon, uint8_t day, uint8_t week,
if (GetJieQi(year, mon, day, &JQdate) && JQdate == day) {
uint8_t JQ = (mon - 1) * 2;
if (day >= 15) JQ++;
strcpy(festival, JieQiStr[JQ]);
if (JQ == 6) // 清明
sprintf(festival, "%s", JieQiStr[JQ]);
else
strcpy(festival, JieQiStr[JQ]);
strcat(festival, "");
return true;
}
return false;
}
static void DrawTimeSyncTip(Adafruit_GFX *gfx)
{
GFX_setFont(gfx, u8g2_font_wqy12_t_lunar);
GFX_fillRect(gfx, gfx->_width / 2 - 100, gfx->_height / 2 - 25, 200, 50, GFX_WHITE);
GFX_drawRoundRect(gfx, gfx->_width / 2 - 100, gfx->_height / 2 - 25, 200, 50, 5, GFX_BLACK);
GFX_setTextColor(gfx, GFX_RED, GFX_WHITE);
GFX_setCursor(gfx, 149, 145);
GFX_printf(gfx, "SYNC TIME!");
GFX_setTextColor(gfx, GFX_BLACK, GFX_WHITE);
GFX_setCursor(gfx, 110, 164);
GFX_setFont(gfx, u8g2_font_wqy9_t_lunar);
GFX_printf(gfx, "https://tsl0922.github.io/EPD-nRF5");
}
static void DrawBattery(Adafruit_GFX *gfx, int16_t x, int16_t y, float voltage)
{
uint8_t level = (uint8_t)(voltage * 100 / 4.2);
@@ -165,7 +179,7 @@ static void DrawDateHeader(Adafruit_GFX *gfx, int16_t x, int16_t y, tm_t *tm, st
GFX_setFont(gfx, u8g2_font_wqy9_t_lunar);
GFX_printf(gfx, "星期%s", Lunar_DayString[tm->tm_wday]);
DrawBattery(gfx, 365, 4, data->voltage);
DrawBattery(gfx, 365, 6, data->voltage);
GFX_setCursor(gfx, x + 270, y);
GFX_printf(gfx, "%s%s%s %s%s", Lunar_MonthLeapString[Lunar->IsLeap], Lunar_MonthString[Lunar->Month],
@@ -357,6 +371,10 @@ void DrawGUI(gui_data_t *data, buffer_callback draw, display_mode_t mode)
default:
break;
}
if ((mode == MODE_CALENDAR || mode == MODE_CLOCK) &&
(tm.tm_year + YEAR0 == 2025 && tm.tm_mon + 1 == 1)) {
DrawTimeSyncTip(&gfx);
}
} while(GFX_nextPage(&gfx, draw));
GFX_end(&gfx);

View File

@@ -8,7 +8,7 @@
#endif
typedef enum {
MODE_NONE = 0,
MODE_PICTURE = 0,
MODE_CALENDAR = 1,
MODE_CLOCK = 2,
} display_mode_t;

13
main.c
View File

@@ -417,6 +417,9 @@ void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
nrf_drv_gpiote_in_uninit(pin);
nrf_drv_gpiote_uninit();
// blink LED on wakeup
EPD_LED_BLINK();
advertising_start();
}
@@ -445,10 +448,10 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
case BLE_ADV_EVT_IDLE:
NRF_LOG_INFO("advertising timeout\n");
if (m_epd.config.wakeup_pin != 0xFF) {
if (m_epd.display_mode != MODE_NONE)
setup_wakeup_pin(m_epd.config.wakeup_pin);
else
if (m_epd.config.display_mode == MODE_PICTURE)
sleep_mode_enter();
else
setup_wakeup_pin(m_epd.config.wakeup_pin);
} else {
advertising_start();
}
@@ -788,8 +791,10 @@ int main(void)
NRF_LOG_DEBUG("done.\n");
if (m_resetreas & NRF_POWER_RESETREAS_DOG_MASK) {
m_epd.display_mode = MODE_CALENDAR;
m_epd.config.display_mode = MODE_CALENDAR;
ble_epd_on_timer(&m_epd, 0, true);
} else {
ble_epd_on_timer(&m_epd, m_timestamp, true);
}
for (;;)