Skip to content

Commit 9af84db

Browse files
committed
optimize firmware
1 parent 45931e2 commit 9af84db

File tree

10 files changed

+101
-80
lines changed

10 files changed

+101
-80
lines changed

EPD/EPD_ble.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "nrf_delay.h"
1717
#include "nrf_gpio.h"
1818
#include "nrf_soc.h"
19+
#include "nrf_nvic.h"
1920
#include "fstorage.h"
2021
#include "EPD_ble.h"
2122
#define NRF_LOG_MODULE_NAME "EPD_ble"
@@ -157,12 +158,10 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
157158

158159
case EPD_CMD_DISPLAY:
159160
p_epd->driver->display();
160-
DEV_Delay_ms(500);
161161
break;
162162

163163
case EPD_CMD_SLEEP:
164164
p_epd->driver->sleep();
165-
DEV_Delay_ms(200);
166165
break;
167166

168167
case EPD_CMD_SET_CONFIG:
@@ -172,7 +171,7 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
172171
break;
173172

174173
case EPD_CMD_SYS_RESET:
175-
NVIC_SystemReset();
174+
sd_nvic_SystemReset();
176175
break;
177176

178177
case EPD_CMD_SYS_SLEEP:
@@ -183,7 +182,7 @@ static void epd_service_process(ble_epd_t * p_epd, uint8_t * p_data, uint16_t le
183182
case EPD_CMD_CFG_ERASE:
184183
epd_config_clear(&p_epd->config);
185184
nrf_delay_ms(10); // required
186-
NVIC_SystemReset();
185+
sd_nvic_SystemReset();
187186
break;
188187

189188
default:

GUI/Adafruit_GFX.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,29 @@ POSSIBILITY OF SUCH DAMAGE.
3232
*/
3333

3434
#include <stdarg.h>
35+
#include <stddef.h>
3536
#include <stdio.h>
3637
#include <stdlib.h>
3738
#include <string.h>
3839
#include "Adafruit_GFX.h"
3940

40-
#ifndef abs
41-
#define abs(x) ((x)>0?(x):-(x))
41+
#ifndef ABS
42+
#define ABS(x) ((x) > 0 ? (x) : -(x))
4243
#endif
43-
#ifndef min
44-
#define min(a, b) (((a) < (b)) ? (a) : (b))
44+
#ifndef MIN
45+
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
4546
#endif
46-
47-
#ifndef _swap_int16_t
48-
#define _swap_int16_t(a, b) \
49-
{ \
50-
int16_t t = a; \
51-
a = b; \
52-
b = t; \
53-
}
47+
#ifndef SWAP
48+
#define SWAP(a, b, T) do { T t = a; a = b; b = t; } while (0)
49+
#endif
50+
#ifndef CONTAINER_OF
51+
#define CONTAINER_OF(ptr, type, member) (type *)((char *)ptr - offsetof(type, member))
5452
#endif
5553

56-
static void GFX_u8g2_draw_hv_line(int16_t x, int16_t y, int16_t len,
57-
uint8_t dir, uint16_t color, void *arg)
54+
static void GFX_u8g2_draw_hv_line(u8g2_font_t *u8g2, int16_t x, int16_t y,
55+
int16_t len, uint8_t dir, uint16_t color)
5856
{
59-
Adafruit_GFX *gfx = (Adafruit_GFX *)arg;
57+
Adafruit_GFX *gfx = CONTAINER_OF(u8g2, Adafruit_GFX, u8g2);
6058
switch(dir) {
6159
case 0:
6260
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) {
8785
gfx->WIDTH = gfx->_width = w;
8886
gfx->HEIGHT = gfx->_height = h;
8987
gfx->u8g2.draw_hv_line = GFX_u8g2_draw_hv_line;
90-
gfx->u8g2.draw_hv_line_arg = gfx;
9188
gfx->buffer = malloc(((gfx->WIDTH + 7) / 8) * buffer_height);
9289
gfx->page_height = buffer_height;
9390
gfx->total_pages = (gfx->HEIGHT / gfx->page_height) + (gfx->HEIGHT % gfx->page_height > 0);
@@ -119,7 +116,7 @@ void GFX_firstPage(Adafruit_GFX *gfx) {
119116

120117
bool GFX_nextPage(Adafruit_GFX *gfx, buffer_callback callback) {
121118
int16_t page_y = gfx->current_page * gfx->page_height;
122-
int16_t height = min(gfx->page_height, gfx->HEIGHT - page_y);
119+
int16_t height = MIN(gfx->page_height, gfx->HEIGHT - page_y);
123120
if (callback)
124121
callback(gfx->buffer, gfx->color, 0, page_y, gfx->WIDTH, height);
125122

@@ -167,15 +164,15 @@ void GFX_drawPixel(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t color) {
167164
case GFX_ROTATE_0:
168165
break;
169166
case GFX_ROTATE_90:
170-
_swap_int16_t(x, y);
167+
SWAP(x, y, int16_t);
171168
x = gfx->WIDTH - x - 1;
172169
break;
173170
case GFX_ROTATE_180:
174171
x = gfx->WIDTH - x - 1;
175172
y = gfx->HEIGHT - y - 1;
176173
break;
177174
case GFX_ROTATE_270:
178-
_swap_int16_t(x, y);
175+
SWAP(x, y, int16_t);
179176
y = gfx->HEIGHT - y - 1;
180177
break;
181178
}
@@ -211,20 +208,20 @@ void GFX_drawPixel(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t color) {
211208
/**************************************************************************/
212209
void GFX_drawLine(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1, int16_t y1,
213210
uint16_t color) {
214-
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
211+
int16_t steep = ABS(y1 - y0) > ABS(x1 - x0);
215212
if (steep) {
216-
_swap_int16_t(x0, y0);
217-
_swap_int16_t(x1, y1);
213+
SWAP(x0, y0, int16_t);
214+
SWAP(x1, y1, int16_t);
218215
}
219216

220217
if (x0 > x1) {
221-
_swap_int16_t(x0, x1);
222-
_swap_int16_t(y0, y1);
218+
SWAP(x0, x1, int16_t);
219+
SWAP(y0, y1, int16_t);
223220
}
224221

225222
int16_t dx, dy;
226223
dx = x1 - x0;
227-
dy = abs(y1 - y0);
224+
dy = ABS(y1 - y0);
228225

229226
int16_t err = dx / 2;
230227
int16_t ystep;
@@ -571,16 +568,16 @@ void GFX_fillTriangle(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1,
571568

572569
// Sort coordinates by Y order (y2 >= y1 >= y0)
573570
if (y0 > y1) {
574-
_swap_int16_t(y0, y1);
575-
_swap_int16_t(x0, x1);
571+
SWAP(y0, y1, int16_t);
572+
SWAP(x0, x1, int16_t);
576573
}
577574
if (y1 > y2) {
578-
_swap_int16_t(y2, y1);
579-
_swap_int16_t(x2, x1);
575+
SWAP(y2, y1, int16_t);
576+
SWAP(x2, x1, int16_t);
580577
}
581578
if (y0 > y1) {
582-
_swap_int16_t(y0, y1);
583-
_swap_int16_t(x0, x1);
579+
SWAP(y0, y1, int16_t);
580+
SWAP(x0, x1, int16_t);
584581
}
585582

586583
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,
622619
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
623620
*/
624621
if (a > b)
625-
_swap_int16_t(a, b);
622+
SWAP(a, b, int16_t);
626623
GFX_drawFastHLine(gfx, a, y, b - a + 1, color);
627624
}
628625

@@ -640,7 +637,7 @@ void GFX_fillTriangle(Adafruit_GFX *gfx, int16_t x0, int16_t y0, int16_t x1,
640637
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
641638
*/
642639
if (a > b)
643-
_swap_int16_t(a, b);
640+
SWAP(a, b, int16_t);
644641
GFX_drawFastHLine(gfx, a, y, b - a + 1, color);
645642
}
646643
}

GUI/Calendar.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
#include "EPD_driver.h"
44
#include "Lunar.h"
55
#include "Calendar.h"
6+
#define NRF_LOG_MODULE_NAME "Calendar"
7+
#include "nrf_log.h"
68

7-
#define PAGE_HEIGHT 32
9+
#define PAGE_HEIGHT 72
810

911
static void DrawDateHeader(Adafruit_GFX *gfx, int16_t x, int16_t y, tm_t *tm, struct Lunar_Date *Lunar)
1012
{
@@ -89,18 +91,19 @@ void DrawCalendar(uint32_t timestamp)
8991

9092
GFX_firstPage(&gfx);
9193
do {
94+
NRF_LOG_DEBUG("page %d\n", gfx.current_page);
9295
GFX_fillScreen(&gfx, GFX_WHITE);
9396

9497
DrawDateHeader(&gfx, 10, 22, &tm, &Lunar);
9598
DrawWeekHeader(&gfx, 10, 26);
9699

97100
for (uint8_t i = 0; i < monthMaxDays; i++)
98-
{
99101
DrawMonthDay(&gfx, 22 + (firstDayWeek + i) % 7 * 55, 60 + (firstDayWeek + i) / 7 * 50, &tm, &Lunar, i + 1);
100-
}
101102
} while(GFX_nextPage(&gfx, driver->write_image));
102103

103104
GFX_end(&gfx);
104105

106+
NRF_LOG_DEBUG("display start\n");
105107
driver->display();
108+
NRF_LOG_DEBUG("display end\n");
106109
}

GUI/u8g2_font.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ static void u8g2_font_decode_len(u8g2_font_t *u8g2, uint8_t len, uint8_t is_fore
288288
{
289289
if ( is_foreground )
290290
{
291-
u8g2->draw_hv_line(x, y, current, decode->dir, decode->fg_color, u8g2->draw_hv_line_arg);
291+
u8g2->draw_hv_line(u8g2, x, y, current, decode->dir, decode->fg_color);
292292
}
293293
else if ( decode->is_transparent == 0 )
294294
{
295-
u8g2->draw_hv_line(x, y, current, decode->dir, decode->bg_color, u8g2->draw_hv_line_arg);
295+
u8g2->draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color);
296296
}
297297
}
298298

GUI/u8g2_font.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ typedef struct _u8g2_font_t
140140

141141
int8_t glyph_x_offset; /* set by u8g2_GetGlyphWidth as a side effect */
142142

143-
void *draw_hv_line_arg;
144-
void (*draw_hv_line)(int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color, void *arg);
143+
void (*draw_hv_line)(struct _u8g2_font_t *u8g2, int16_t x, int16_t y,
144+
int16_t len, uint8_t dir, uint16_t color);
145145
} u8g2_font_t;
146146

147147
uint8_t u8g2_IsGlyph(u8g2_font_t *u8g2, uint16_t requested_encoding);

Keil/EPD.uvprojx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@
340340
<MiscControls></MiscControls>
341341
<Define>BLE_STACK_SUPPORT_REQD NRF51822 NRF_SD_BLE_API_VERSION=2 S130 NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0</Define>
342342
<Undefine></Undefine>
343-
<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>
343+
<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>
344344
</VariousControls>
345345
</Cads>
346346
<Aads>
@@ -358,7 +358,7 @@
358358
<MiscControls></MiscControls>
359359
<Define>BLE_STACK_SUPPORT_REQD NRF51822 NRF_SD_BLE_API_VERSION=2 S130 NRF51 SOFTDEVICE_PRESENT SWI_DISABLE0</Define>
360360
<Undefine></Undefine>
361-
<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>
361+
<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>
362362
</VariousControls>
363363
</Aads>
364364
<LDads>
@@ -445,14 +445,14 @@
445445
<FilePath>..\GUI\fonts.c</FilePath>
446446
</File>
447447
<File>
448-
<FileName>Adafruit_GFX.c</FileName>
448+
<FileName>u8g2_font.c</FileName>
449449
<FileType>1</FileType>
450-
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
450+
<FilePath>..\GUI\u8g2_font.c</FilePath>
451451
</File>
452452
<File>
453-
<FileName>u8g2_font.c</FileName>
453+
<FileName>Adafruit_GFX.c</FileName>
454454
<FileType>1</FileType>
455-
<FilePath>..\GUI\u8g2_font.c</FilePath>
455+
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
456456
</File>
457457
</Files>
458458
</Group>
@@ -519,6 +519,11 @@
519519
<FileType>1</FileType>
520520
<FilePath>..\components\libraries\util\app_error_weak.c</FilePath>
521521
</File>
522+
<File>
523+
<FileName>app_scheduler.c</FileName>
524+
<FileType>1</FileType>
525+
<FilePath>..\components\libraries\scheduler\app_scheduler.c</FilePath>
526+
</File>
522527
<File>
523528
<FileName>app_timer.c</FileName>
524529
<FileType>1</FileType>
@@ -1037,14 +1042,14 @@
10371042
<FilePath>..\GUI\fonts.c</FilePath>
10381043
</File>
10391044
<File>
1040-
<FileName>Adafruit_GFX.c</FileName>
1045+
<FileName>u8g2_font.c</FileName>
10411046
<FileType>1</FileType>
1042-
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
1047+
<FilePath>..\GUI\u8g2_font.c</FilePath>
10431048
</File>
10441049
<File>
1045-
<FileName>u8g2_font.c</FileName>
1050+
<FileName>Adafruit_GFX.c</FileName>
10461051
<FileType>1</FileType>
1047-
<FilePath>..\GUI\u8g2_font.c</FilePath>
1052+
<FilePath>..\GUI\Adafruit_GFX.c</FilePath>
10481053
</File>
10491054
</Files>
10501055
</Group>
@@ -1111,6 +1116,11 @@
11111116
<FileType>1</FileType>
11121117
<FilePath>..\components\libraries\util\app_error_weak.c</FilePath>
11131118
</File>
1119+
<File>
1120+
<FileName>app_scheduler.c</FileName>
1121+
<FileType>1</FileType>
1122+
<FilePath>..\components\libraries\scheduler\app_scheduler.c</FilePath>
1123+
</File>
11141124
<File>
11151125
<FileName>app_timer.c</FileName>
11161126
<FileType>1</FileType>

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SRC_FILES += \
1616
$(SDK_ROOT)/components/libraries/util/app_error.c \
1717
$(SDK_ROOT)/components/libraries/util/app_error_weak.c \
1818
$(SDK_ROOT)/components/libraries/timer/app_timer.c \
19+
$(SDK_ROOT)/components/libraries/scheduler/app_scheduler.c \
1920
$(SDK_ROOT)/components/libraries/util/app_util_platform.c \
2021
$(SDK_ROOT)/components/libraries/fstorage/fstorage.c \
2122
$(SDK_ROOT)/components/drivers_nrf/common/nrf_drv_common.c \
@@ -62,6 +63,7 @@ INC_FOLDERS += \
6263
$(SDK_ROOT)/components/libraries/log \
6364
$(SDK_ROOT)/components/libraries/log/src \
6465
$(SDK_ROOT)/components/libraries/timer \
66+
$(SDK_ROOT)/components/libraries/scheduler \
6567
$(SDK_ROOT)/components/libraries/util \
6668
$(SDK_ROOT)/components/device \
6769
$(SDK_ROOT)/components/toolchain \

components/toolchain/arm/arm_startup_nrf51.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Heap_Size EQU __STARTUP_CONFIG_HEAP_SIZE
4646
ELIF :DEF: __HEAP_SIZE
4747
Heap_Size EQU __HEAP_SIZE
4848
ELSE
49-
Heap_Size EQU 2048
49+
Heap_Size EQU 4096
5050
ENDIF
5151

5252
AREA HEAP, NOINIT, READWRITE, ALIGN=3

config/sdk_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,7 +2949,7 @@
29492949
// <e> APP_SCHEDULER_ENABLED - app_scheduler - Events scheduler
29502950
//==========================================================
29512951
#ifndef APP_SCHEDULER_ENABLED
2952-
#define APP_SCHEDULER_ENABLED 0
2952+
#define APP_SCHEDULER_ENABLED 1
29532953
#endif
29542954
#if APP_SCHEDULER_ENABLED
29552955
// <q> APP_SCHEDULER_WITH_PAUSE - Enabling pause feature
@@ -3566,7 +3566,7 @@
35663566
// <i> Log data is buffered and can be processed in idle.
35673567
//==========================================================
35683568
#ifndef NRF_LOG_DEFERRED
3569-
#define NRF_LOG_DEFERRED 1
3569+
#define NRF_LOG_DEFERRED 0
35703570
#endif
35713571
#if NRF_LOG_DEFERRED
35723572
// <o> NRF_LOG_DEFERRED_BUFSIZE - Size of the buffer for logs in words.

0 commit comments

Comments
 (0)