Skip to content

Commit

Permalink
optimize firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 13, 2025
1 parent 45931e2 commit 9af84db
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 80 deletions.
7 changes: 3 additions & 4 deletions EPD/EPD_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_soc.h"
#include "nrf_nvic.h"
#include "fstorage.h"
#include "EPD_ble.h"
#define NRF_LOG_MODULE_NAME "EPD_ble"
Expand Down Expand Up @@ -157,12 +158,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:
Expand All @@ -172,7 +171,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:
Expand All @@ -183,7 +182,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:
Expand Down
63 changes: 30 additions & 33 deletions GUI/Adafruit_GFX.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,29 @@ POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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_int16_t
#define _swap_int16_t(a, b) \
{ \
int16_t t = a; \
a = b; \
b = t; \
}
#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

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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -167,15 +164,15 @@ 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:
x = gfx->WIDTH - x - 1;
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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand Down
9 changes: 6 additions & 3 deletions GUI/Calendar.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "EPD_driver.h"
#include "Lunar.h"
#include "Calendar.h"
#define NRF_LOG_MODULE_NAME "Calendar"
#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)
{
Expand Down Expand Up @@ -89,18 +91,19 @@ void DrawCalendar(uint32_t timestamp)

GFX_firstPage(&gfx);
do {
NRF_LOG_DEBUG("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_DEBUG("display start\n");
driver->display();
NRF_LOG_DEBUG("display end\n");
}
4 changes: 2 additions & 2 deletions GUI/u8g2_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions GUI/u8g2_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 20 additions & 10 deletions Keil/EPD.uvprojx
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
<MiscControls></MiscControls>
<Define>BLE_STACK_SUPPORT_REQD NRF51822 NRF_SD_BLE_API_VERSION=2 S130 NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0</Define>
<Undefine></Undefine>
<IncludePath>..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\clock;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\fstorage;..\components\libraries\experimental_section_vars;..\components\libraries\log;..\components\libraries\log\src;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s130\headers;..\components\softdevice\s130\headers\nrf51</IncludePath>
<IncludePath>..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\clock;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\fstorage;..\components\libraries\experimental_section_vars;..\components\libraries\log;..\components\libraries\log\src;..\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\s130\headers;..\components\softdevice\s130\headers\nrf51</IncludePath>
</VariousControls>
</Cads>
<Aads>
Expand All @@ -358,7 +358,7 @@
<MiscControls></MiscControls>
<Define>BLE_STACK_SUPPORT_REQD NRF51822 NRF_SD_BLE_API_VERSION=2 S130 NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0</Define>
<Undefine></Undefine>
<IncludePath>..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\clock;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\fstorage;..\components\libraries\experimental_section_vars;..\components\libraries\log;..\components\libraries\log\src;..\components\libraries\trace;..\components\libraries\timer;..\components\libraries\util;..\components\ble\common;..\components\ble\ble_advertising;..\components\softdevice\common\softdevice_handler;..\components\softdevice\s130\headers;..\components\softdevice\s130\headers\nrf51</IncludePath>
<IncludePath>..\config;..\EPD;..\GUI;..\components\toolchain;..\components\toolchain\cmsis\include;..\components\drivers_nrf\clock;..\components\drivers_nrf\common;..\components\drivers_nrf\delay;..\components\drivers_nrf\gpiote;..\components\drivers_nrf\hal;..\components\drivers_nrf\spi_master;..\components\drivers_nrf\twi_master;..\components\drivers_ext\segger_rtt;..\components\libraries\fstorage;..\components\libraries\experimental_section_vars;..\components\libraries\log;..\components\libraries\log\src;..\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\s130\headers;..\components\softdevice\s130\headers\nrf51</IncludePath>
</VariousControls>
</Aads>
<LDads>
Expand Down Expand Up @@ -445,14 +445,14 @@
<FilePath>..\GUI\fonts.c</FilePath>
</File>
<File>
<FileName>Adafruit_GFX.c</FileName>
<FileName>u8g2_font.c</FileName>
<FileType>1</FileType>
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
<FilePath>..\GUI\u8g2_font.c</FilePath>
</File>
<File>
<FileName>u8g2_font.c</FileName>
<FileName>Adafruit_GFX.c</FileName>
<FileType>1</FileType>
<FilePath>..\GUI\u8g2_font.c</FilePath>
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
</File>
</Files>
</Group>
Expand Down Expand Up @@ -519,6 +519,11 @@
<FileType>1</FileType>
<FilePath>..\components\libraries\util\app_error_weak.c</FilePath>
</File>
<File>
<FileName>app_scheduler.c</FileName>
<FileType>1</FileType>
<FilePath>..\components\libraries\scheduler\app_scheduler.c</FilePath>
</File>
<File>
<FileName>app_timer.c</FileName>
<FileType>1</FileType>
Expand Down Expand Up @@ -1037,14 +1042,14 @@
<FilePath>..\GUI\fonts.c</FilePath>
</File>
<File>
<FileName>Adafruit_GFX.c</FileName>
<FileName>u8g2_font.c</FileName>
<FileType>1</FileType>
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
<FilePath>..\GUI\u8g2_font.c</FilePath>
</File>
<File>
<FileName>u8g2_font.c</FileName>
<FileName>Adafruit_GFX.c</FileName>
<FileType>1</FileType>
<FilePath>..\GUI\u8g2_font.c</FilePath>
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
</File>
</Files>
</Group>
Expand Down Expand Up @@ -1111,6 +1116,11 @@
<FileType>1</FileType>
<FilePath>..\components\libraries\util\app_error_weak.c</FilePath>
</File>
<File>
<FileName>app_scheduler.c</FileName>
<FileType>1</FileType>
<FilePath>..\components\libraries\scheduler\app_scheduler.c</FilePath>
</File>
<File>
<FileName>app_timer.c</FileName>
<FileType>1</FileType>
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SRC_FILES += \
$(SDK_ROOT)/components/libraries/util/app_error.c \
$(SDK_ROOT)/components/libraries/util/app_error_weak.c \
$(SDK_ROOT)/components/libraries/timer/app_timer.c \
$(SDK_ROOT)/components/libraries/scheduler/app_scheduler.c \
$(SDK_ROOT)/components/libraries/util/app_util_platform.c \
$(SDK_ROOT)/components/libraries/fstorage/fstorage.c \
$(SDK_ROOT)/components/drivers_nrf/common/nrf_drv_common.c \
Expand Down Expand Up @@ -62,6 +63,7 @@ INC_FOLDERS += \
$(SDK_ROOT)/components/libraries/log \
$(SDK_ROOT)/components/libraries/log/src \
$(SDK_ROOT)/components/libraries/timer \
$(SDK_ROOT)/components/libraries/scheduler \
$(SDK_ROOT)/components/libraries/util \
$(SDK_ROOT)/components/device \
$(SDK_ROOT)/components/toolchain \
Expand Down
2 changes: 1 addition & 1 deletion components/toolchain/arm/arm_startup_nrf51.s
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Heap_Size EQU __STARTUP_CONFIG_HEAP_SIZE
ELIF :DEF: __HEAP_SIZE
Heap_Size EQU __HEAP_SIZE
ELSE
Heap_Size EQU 2048
Heap_Size EQU 4096
ENDIF

AREA HEAP, NOINIT, READWRITE, ALIGN=3
Expand Down
4 changes: 2 additions & 2 deletions config/sdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2949,7 +2949,7 @@
// <e> APP_SCHEDULER_ENABLED - app_scheduler - Events scheduler
//==========================================================
#ifndef APP_SCHEDULER_ENABLED
#define APP_SCHEDULER_ENABLED 0
#define APP_SCHEDULER_ENABLED 1
#endif
#if APP_SCHEDULER_ENABLED
// <q> APP_SCHEDULER_WITH_PAUSE - Enabling pause feature
Expand Down Expand Up @@ -3566,7 +3566,7 @@
// <i> Log data is buffered and can be processed in idle.
//==========================================================
#ifndef NRF_LOG_DEFERRED
#define NRF_LOG_DEFERRED 1
#define NRF_LOG_DEFERRED 0
#endif
#if NRF_LOG_DEFERRED
// <o> NRF_LOG_DEFERRED_BUFSIZE - Size of the buffer for logs in words.
Expand Down
Loading

0 comments on commit 9af84db

Please sign in to comment.