From 76339f03ea0cdff549b1dfbf07dbff3d1ffd0097 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Tue, 11 Feb 2025 23:02:07 +0800 Subject: [PATCH] optimize firmware --- EPD/EPD_ble.c | 6 +- GUI/Adafruit_GFX.c | 65 ++++++++++---------- GUI/Calendar.c | 8 ++- GUI/u8g2_font.c | 4 +- GUI/u8g2_font.h | 4 +- Keil/EPD.uvprojx | 27 ++++++-- components/toolchain/arm/arm_startup_nrf51.s | 2 +- main.c | 51 +++++++++------ 8 files changed, 96 insertions(+), 71 deletions(-) diff --git a/EPD/EPD_ble.c b/EPD/EPD_ble.c index f87b172..477843f 100644 --- a/EPD/EPD_ble.c +++ b/EPD/EPD_ble.c @@ -143,12 +143,10 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le case EPD_CMD_DISPLAY: p_epd->driver->display(); - DEV_Delay_ms(500); break; case EPD_CMD_SLEEP: p_epd->driver->sleep(); - DEV_Delay_ms(200); break; case EPD_CMD_SET_CONFIG: @@ -158,7 +156,7 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le break; case EPD_CMD_SYS_RESET: - NVIC_SystemReset(); + sd_nvic_SystemReset(); break; case EPD_CMD_SYS_SLEEP: @@ -169,7 +167,7 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le case EPD_CMD_CFG_ERASE: epd_config_clear(&p_epd->config); nrf_delay_ms(10); // required - NVIC_SystemReset(); + sd_nvic_SystemReset(); break; default: diff --git a/GUI/Adafruit_GFX.c b/GUI/Adafruit_GFX.c index 9898129..5dc1b08 100644 --- a/GUI/Adafruit_GFX.c +++ b/GUI/Adafruit_GFX.c @@ -32,31 +32,29 @@ POSSIBILITY OF SUCH DAMAGE. */ #include +#include #include #include #include #include "Adafruit_GFX.h" -#ifndef abs -#define abs(x) ((x)>0?(x):-(x)) +#ifndef ABS +#define ABS(x) ((x) > 0 ? (x) : -(x)) #endif -#ifndef min -#define min(a, b) (((a) < (b)) ? (a) : (b)) +#ifndef MIN +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif +#ifndef SWAP +#define SWAP(a, b, T) do { T t = a; a = b; b = t; } while (0) +#endif +#ifndef CONTAINER_OF +#define CONTAINER_OF(ptr, type, member) (type *)((char *)ptr - offsetof(type, member)) #endif -#ifndef _swap_int16_t -#define _swap_int16_t(a, b) \ - { \ - int16_t t = a; \ - a = b; \ - b = t; \ - } -#endif - -static void GFX_u8g2_draw_hv_line(int16_t x, int16_t y, int16_t len, - uint8_t dir, uint16_t color, void *arg) +static void GFX_u8g2_draw_hv_line(u8g2_font_t *u8g2, int16_t x, int16_t y, + int16_t len, uint8_t dir, uint16_t color) { - Adafruit_GFX *gfx = (Adafruit_GFX *)arg; + Adafruit_GFX *gfx = CONTAINER_OF(u8g2, Adafruit_GFX, u8g2); switch(dir) { case 0: GFX_drawFastHLine(gfx, x, y, len, color); @@ -87,7 +85,6 @@ void GFX_begin(Adafruit_GFX *gfx, int16_t w, int16_t h, int16_t buffer_height) { gfx->WIDTH = gfx->_width = w; gfx->HEIGHT = gfx->_height = h; gfx->u8g2.draw_hv_line = GFX_u8g2_draw_hv_line; - gfx->u8g2.draw_hv_line_arg = gfx; gfx->buffer = malloc(((gfx->WIDTH + 7) / 8) * buffer_height); gfx->page_height = buffer_height; gfx->total_pages = (gfx->HEIGHT / gfx->page_height) + (gfx->HEIGHT % gfx->page_height > 0); @@ -119,7 +116,7 @@ void GFX_firstPage(Adafruit_GFX *gfx) { bool GFX_nextPage(Adafruit_GFX *gfx, buffer_callback callback) { int16_t page_y = gfx->current_page * gfx->page_height; - int16_t height = min(gfx->page_height, gfx->HEIGHT - page_y); + int16_t height = MIN(gfx->page_height, gfx->HEIGHT - page_y); if (callback) callback(gfx->buffer, gfx->color, 0, page_y, gfx->WIDTH, height); @@ -167,7 +164,7 @@ void GFX_drawPixel(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t color) { case GFX_ROTATE_0: break; case GFX_ROTATE_90: - _swap_int16_t(x, y); + SWAP(x, y, int16_t); x = gfx->WIDTH - x - 1; break; case GFX_ROTATE_180: @@ -175,7 +172,7 @@ void GFX_drawPixel(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t color) { y = gfx->HEIGHT - y - 1; break; case GFX_ROTATE_270: - _swap_int16_t(x, y); + SWAP(x, y, int16_t); y = gfx->HEIGHT - y - 1; break; } @@ -211,20 +208,20 @@ void GFX_drawPixel(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t color) { /**************************************************************************/ void GFX_drawLine(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) { - int16_t steep = abs(y1 - y0) > abs(x1 - x0); + int16_t steep = ABS(y1 - y0) > ABS(x1 - x0); if (steep) { - _swap_int16_t(x0, y0); - _swap_int16_t(x1, y1); + SWAP(x0, y0, int16_t); + SWAP(x1, y1, int16_t); } if (x0 > x1) { - _swap_int16_t(x0, x1); - _swap_int16_t(y0, y1); + SWAP(x0, x1, int16_t); + SWAP(y0, y1, int16_t); } int16_t dx, dy; dx = x1 - x0; - dy = abs(y1 - y0); + dy = ABS(y1 - y0); int16_t err = dx / 2; int16_t ystep; @@ -571,16 +568,16 @@ void GFX_fillTriangle(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1, // Sort coordinates by Y order (y2 >= y1 >= y0) if (y0 > y1) { - _swap_int16_t(y0, y1); - _swap_int16_t(x0, x1); + SWAP(y0, y1, int16_t); + SWAP(x0, x1, int16_t); } if (y1 > y2) { - _swap_int16_t(y2, y1); - _swap_int16_t(x2, x1); + SWAP(y2, y1, int16_t); + SWAP(x2, x1, int16_t); } if (y0 > y1) { - _swap_int16_t(y0, y1); - _swap_int16_t(x0, x1); + SWAP(y0, y1, int16_t); + SWAP(x0, x1, int16_t); } if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing @@ -622,7 +619,7 @@ void GFX_fillTriangle(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1, b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); */ if (a > b) - _swap_int16_t(a, b); + SWAP(a, b, int16_t); GFX_drawFastHLine(gfx, a, y, b - a + 1, color); } @@ -640,7 +637,7 @@ void GFX_fillTriangle(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1, b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); */ if (a > b) - _swap_int16_t(a, b); + SWAP(a, b, int16_t); GFX_drawFastHLine(gfx, a, y, b - a + 1, color); } } diff --git a/GUI/Calendar.c b/GUI/Calendar.c index b9b8d6d..f597787 100644 --- a/GUI/Calendar.c +++ b/GUI/Calendar.c @@ -3,8 +3,9 @@ #include "EPD_driver.h" #include "Lunar.h" #include "Calendar.h" +#include "nrf_log.h" -#define PAGE_HEIGHT 32 +#define PAGE_HEIGHT 72 static void DrawDateHeader(Adafruit_GFX *gfx, int16_t x, int16_t y, tm_t *tm, struct Lunar_Date *Lunar) { @@ -89,18 +90,19 @@ void DrawCalendar(uint32_t timestamp) GFX_firstPage(&gfx); do { + NRF_LOG_PRINTF("page %d\n", gfx.current_page); GFX_fillScreen(&gfx, GFX_WHITE); DrawDateHeader(&gfx, 10, 22, &tm, &Lunar); DrawWeekHeader(&gfx, 10, 26); for (uint8_t i = 0; i < monthMaxDays; i++) - { DrawMonthDay(&gfx, 22 + (firstDayWeek + i) % 7 * 55, 60 + (firstDayWeek + i) / 7 * 50, &tm, &Lunar, i + 1); - } } while(GFX_nextPage(&gfx, driver->write_image)); GFX_end(&gfx); + NRF_LOG_PRINTF("display start\n"); driver->display(); + NRF_LOG_PRINTF("display end\n"); } diff --git a/GUI/u8g2_font.c b/GUI/u8g2_font.c index 92ec0cd..9ae2248 100644 --- a/GUI/u8g2_font.c +++ b/GUI/u8g2_font.c @@ -288,11 +288,11 @@ static void u8g2_font_decode_len(u8g2_font_t *u8g2, uint8_t len, uint8_t is_fore { if ( is_foreground ) { - u8g2->draw_hv_line(x, y, current, decode->dir, decode->fg_color, u8g2->draw_hv_line_arg); + u8g2->draw_hv_line(u8g2, x, y, current, decode->dir, decode->fg_color); } else if ( decode->is_transparent == 0 ) { - u8g2->draw_hv_line(x, y, current, decode->dir, decode->bg_color, u8g2->draw_hv_line_arg); + u8g2->draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color); } } diff --git a/GUI/u8g2_font.h b/GUI/u8g2_font.h index 1e6e0d5..fc015c1 100644 --- a/GUI/u8g2_font.h +++ b/GUI/u8g2_font.h @@ -140,8 +140,8 @@ typedef struct _u8g2_font_t int8_t glyph_x_offset; /* set by u8g2_GetGlyphWidth as a side effect */ - void *draw_hv_line_arg; - void (*draw_hv_line)(int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color, void *arg); + void (*draw_hv_line)(struct _u8g2_font_t *u8g2, int16_t x, int16_t y, + int16_t len, uint8_t dir, uint16_t color); } u8g2_font_t; uint8_t u8g2_IsGlyph(u8g2_font_t *u8g2, uint16_t requested_encoding); diff --git a/Keil/EPD.uvprojx b/Keil/EPD.uvprojx index 3db5da7..96df430 100644 --- a/Keil/EPD.uvprojx +++ b/Keil/EPD.uvprojx @@ -340,7 +340,7 @@ BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 - ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers + ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\libraries\scheduler;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers @@ -358,7 +358,7 @@ BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 - ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers + ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\libraries\scheduler;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers @@ -534,6 +534,11 @@ 1 ..\components\libraries\util\app_error.c + + app_scheduler.c + 1 + ..\components\libraries\scheduler\app_scheduler.c + app_timer.c 1 @@ -859,7 +864,7 @@ 1 0x18000 - 0x8000 + 0x18000 1 @@ -922,7 +927,7 @@ BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 DEBUG NRF_LOG_USES_RTT=1 - ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers + ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\scheduler;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers @@ -938,9 +943,9 @@ 1 - BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 + BLE_STACK_SUPPORT_REQD S110 SWI_DISABLE0 SOFTDEVICE_PRESENT NRF51 __HEAP_SIZE=2048 - ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers + ..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\config;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\pstorage;..\components\drivers_nrf\pstorage\config;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\scheduler;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s110\headers @@ -1116,6 +1121,11 @@ 1 ..\components\libraries\util\app_error.c + + app_scheduler.c + 1 + ..\components\libraries\scheduler\app_scheduler.c + app_timer.c 1 @@ -1698,6 +1708,11 @@ 1 ..\components\libraries\util\app_error.c + + app_scheduler.c + 1 + ..\components\libraries\scheduler\app_scheduler.c + app_timer.c 1 diff --git a/components/toolchain/arm/arm_startup_nrf51.s b/components/toolchain/arm/arm_startup_nrf51.s index ee2c1c0..2a46480 100644 --- a/components/toolchain/arm/arm_startup_nrf51.s +++ b/components/toolchain/arm/arm_startup_nrf51.s @@ -44,7 +44,7 @@ __initial_sp IF :DEF: __HEAP_SIZE Heap_Size EQU __HEAP_SIZE ELSE -Heap_Size EQU 2048 +Heap_Size EQU 4096 ENDIF AREA HEAP, NOINIT, READWRITE, ALIGN=3 diff --git a/main.c b/main.c index 5461926..0831947 100644 --- a/main.c +++ b/main.c @@ -26,6 +26,7 @@ #include "pstorage.h" #include "app_error.h" #include "app_timer.h" +#include "app_scheduler.h" #include "app_util_platform.h" #include "nrf_drv_gpiote.h" #include "EPD_ble.h" @@ -48,16 +49,19 @@ #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */ #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ +#define SCHED_MAX_EVENT_DATA_SIZE 0 /**< Maximum size of scheduler events. */ +#define SCHED_QUEUE_SIZE 10 /**< Maximum number of events in the scheduler queue. */ + #define CLOCK_TIMER_INTERVAL APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER) /**< Clock timer interval (ticks). */ #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ static uint16_t m_driver_refs = 0; static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ -static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_EPD_SERVICE, EPD_SERVICE_UUID_TYPE}}; /**< Universally unique service identifier. */ +static ble_uuid_t m_adv_uuids[] = {{BLE_UUID_EPD_SERVICE, \ + EPD_SERVICE_UUID_TYPE}}; /**< Universally unique service identifier. */ static ble_epd_t m_epd; /**< Structure to identify the EPD Service. */ static uint32_t m_timestamp = 1735689600; /**< Current timestamp. */ -static bool m_update_calendar = false; /**< Update calendar if true */ static bool m_calendar_mode = false; /**< Whether we are in calendar mode */ APP_TIMER_DEF(m_clock_timer_id); /**< Clock timer. */ @@ -80,6 +84,20 @@ static void epd_driver_exit() } } +static void calendar_update(void * p_event_data, uint16_t event_size) +{ + m_calendar_mode = true; + epd_driver_init(); + m_epd.driver->init(); + DrawCalendar(m_timestamp); + epd_driver_exit(); +} + +static uint32_t calendar_update_schedule(void) +{ + return app_sched_event_put(NULL, 0, calendar_update); +} + /**@brief Callback function for asserts in the SoftDevice. * * @details This function will be called in case of an assert in the SoftDevice. @@ -104,9 +122,14 @@ static void clock_timer_timeout_handler(void * p_context) // Update calendar on 00:00:00 if (m_calendar_mode && m_timestamp % 86400 == 0) - { - m_update_calendar = true; - } + calendar_update_schedule(); +} + +/**@brief Function for the Event Scheduler initialization. + */ +static void scheduler_init(void) +{ + APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE); } /**@brief Function for the Timer initialization. @@ -154,8 +177,7 @@ bool epd_cmd_callback(uint8_t cmd, uint8_t *data, uint16_t len) m_timestamp += (len > 4 ? (int8_t)data[4] : 8) * 60 * 60; // timezone app_timer_start(m_clock_timer_id, CLOCK_TIMER_INTERVAL, NULL); - m_calendar_mode = true; - m_update_calendar = true; + calendar_update_schedule(); return true; case EPD_CMD_CLEAR: case EPD_CMD_DISPLAY: @@ -491,17 +513,6 @@ static void power_manage(void) NRF_LOG_PRINTF("timestamp: %d\n", m_timestamp); } -static void calendar_update(void) -{ - if (!m_update_calendar) return; - - m_update_calendar = false; - epd_driver_init(); - m_epd.driver->init(); - DrawCalendar(m_timestamp); - epd_driver_exit(); -} - /**@brief Function for application main entry. */ int main(void) @@ -515,6 +526,7 @@ int main(void) timers_init(); ble_stack_init(); + scheduler_init(); gap_params_init(); services_init(); advertising_init(); @@ -529,7 +541,8 @@ int main(void) for (;;) { - calendar_update(); + app_sched_execute(); + power_manage(); } }