Skip to content

Commit 465af89

Browse files
committed
refactor driver code
1 parent 87f96ee commit 465af89

File tree

11 files changed

+205
-264
lines changed

11 files changed

+205
-264
lines changed

EPD/EPD_config.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void epd_config_init(epd_config_t *cfg)
2828
}
2929
}
3030

31-
void epd_config_load(epd_config_t *cfg)
31+
void epd_config_read(epd_config_t *cfg)
3232
{
3333
fds_flash_record_t flash_record;
3434
fds_record_desc_t record_desc;
@@ -54,21 +54,7 @@ void epd_config_load(epd_config_t *cfg)
5454
fds_record_close(&record_desc);
5555
}
5656

57-
void epd_config_clear(epd_config_t *cfg)
58-
{
59-
fds_record_desc_t record_desc;
60-
fds_find_token_t ftok;
61-
62-
memset(&ftok, 0x00, sizeof(fds_find_token_t));
63-
if (fds_record_find(CONFIG_FILE_ID, CONFIG_REC_KEY, &record_desc, &ftok) != NRF_SUCCESS) {
64-
NRF_LOG_DEBUG("epd_config_clear: record not found\n");
65-
return;
66-
}
67-
68-
fds_record_delete(&record_desc);
69-
}
70-
71-
void epd_config_save(epd_config_t *cfg)
57+
void epd_config_write(epd_config_t *cfg)
7258
{
7359
ret_code_t ret;
7460
fds_record_t record;
@@ -100,6 +86,20 @@ void epd_config_save(epd_config_t *cfg)
10086
}
10187
}
10288

89+
void epd_config_clear(epd_config_t *cfg)
90+
{
91+
fds_record_desc_t record_desc;
92+
fds_find_token_t ftok;
93+
94+
memset(&ftok, 0x00, sizeof(fds_find_token_t));
95+
if (fds_record_find(CONFIG_FILE_ID, CONFIG_REC_KEY, &record_desc, &ftok) != NRF_SUCCESS) {
96+
NRF_LOG_DEBUG("epd_config_clear: record not found\n");
97+
return;
98+
}
99+
100+
fds_record_delete(&record_desc);
101+
}
102+
103103
bool epd_config_empty(epd_config_t *cfg)
104104
{
105105
for (uint8_t i = 0; i < EPD_CONFIG_SIZE; i++) {

EPD/EPD_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ typedef struct
2222
#define EPD_CONFIG_EMPTY 0xFF
2323

2424
void epd_config_init(epd_config_t *cfg);
25-
void epd_config_load(epd_config_t *cfg);
25+
void epd_config_read(epd_config_t *cfg);
26+
void epd_config_write(epd_config_t *cfg);
2627
void epd_config_clear(epd_config_t *cfg);
27-
void epd_config_save(epd_config_t *cfg);
2828
bool epd_config_empty(epd_config_t *cfg);
2929

3030
#endif

EPD/EPD_driver.c

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,18 @@
2222
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
2323

2424
// GPIO Pins
25-
uint32_t EPD_MOSI_PIN = 5;
26-
uint32_t EPD_SCLK_PIN = 8;
27-
uint32_t EPD_CS_PIN = 9;
28-
uint32_t EPD_DC_PIN = 10;
29-
uint32_t EPD_RST_PIN = 11;
30-
uint32_t EPD_BUSY_PIN = 12;
31-
uint32_t EPD_BS_PIN = 13;
32-
uint32_t EPD_EN_PIN = 0xFF;
33-
uint32_t EPD_LED_PIN = 0xFF;
34-
35-
// Display resolution
36-
uint16_t EPD_WIDTH = 400;
37-
uint16_t EPD_HEIGHT = 300;
38-
39-
// BWR mode
40-
bool EPD_BWR_MODE = true;
25+
static uint32_t EPD_MOSI_PIN = 5;
26+
static uint32_t EPD_SCLK_PIN = 8;
27+
static uint32_t EPD_CS_PIN = 9;
28+
static uint32_t EPD_DC_PIN = 10;
29+
static uint32_t EPD_RST_PIN = 11;
30+
static uint32_t EPD_BUSY_PIN = 12;
31+
static uint32_t EPD_BS_PIN = 13;
32+
static uint32_t EPD_EN_PIN = 0xFF;
33+
static uint32_t EPD_LED_PIN = 0xFF;
34+
35+
// EPD model
36+
static epd_model_t *EPD = NULL;
4137

4238
// Arduino like function wrappers
4339

@@ -224,9 +220,11 @@ void EPD_Reset(uint32_t value, uint16_t duration)
224220

225221
void EPD_WaitBusy(uint32_t value, uint16_t timeout)
226222
{
223+
uint32_t led_status = digitalRead(EPD_LED_PIN);
224+
227225
NRF_LOG_DEBUG("[EPD]: check busy\n");
228226
while (digitalRead(EPD_BUSY_PIN) == value) {
229-
if (timeout % 100 == 0) EPD_LED_TOGGLE();
227+
if (timeout % 100 == 0) EPD_LED_Toggle();
230228
delay(1);
231229
timeout--;
232230
if (timeout == 0) {
@@ -235,9 +233,29 @@ void EPD_WaitBusy(uint32_t value, uint16_t timeout)
235233
}
236234
}
237235
NRF_LOG_DEBUG("[EPD]: busy release\n");
236+
237+
// restore led status
238+
if (led_status == LOW)
239+
EPD_LED_ON();
240+
else
241+
EPD_LED_OFF();
238242
}
239243

240244
// GPIO
245+
void EPD_GPIO_Load(epd_config_t *cfg)
246+
{
247+
if (cfg == NULL) return;
248+
EPD_MOSI_PIN = cfg->mosi_pin;
249+
EPD_SCLK_PIN = cfg->sclk_pin;
250+
EPD_CS_PIN = cfg->cs_pin;
251+
EPD_DC_PIN = cfg->dc_pin;
252+
EPD_RST_PIN = cfg->rst_pin;
253+
EPD_BUSY_PIN = cfg->busy_pin;
254+
EPD_BS_PIN = cfg->bs_pin;
255+
EPD_EN_PIN = cfg->en_pin;
256+
EPD_LED_PIN = cfg->led_pin;
257+
}
258+
241259
void EPD_GPIO_Init(void)
242260
{
243261
pinMode(EPD_CS_PIN, OUTPUT);
@@ -253,16 +271,12 @@ void EPD_GPIO_Init(void)
253271
pinMode(EPD_BS_PIN, OUTPUT);
254272
digitalWrite(EPD_BS_PIN, LOW);
255273

256-
EPD_SPI_Init();
257-
258274
digitalWrite(EPD_DC_PIN, LOW);
259275
digitalWrite(EPD_CS_PIN, LOW);
260276
digitalWrite(EPD_RST_PIN, HIGH);
261277

262-
if (EPD_LED_PIN != 0xFF) {
278+
if (EPD_LED_PIN != 0xFF)
263279
pinMode(EPD_LED_PIN, OUTPUT);
264-
EPD_LED_ON();
265-
}
266280
}
267281

268282
void EPD_GPIO_Uninit(void)
@@ -289,7 +303,7 @@ void EPD_LED_OFF(void)
289303
digitalWrite(EPD_LED_PIN, HIGH);
290304
}
291305

292-
void EPD_LED_TOGGLE(void)
306+
void EPD_LED_Toggle(void)
293307
{
294308
if (EPD_LED_PIN != 0xFF)
295309
nrf_gpio_pin_toggle(EPD_LED_PIN);
@@ -310,19 +324,19 @@ static epd_model_t *epd_models[] = {
310324
&epd_uc8276_420_bwr,
311325
};
312326

313-
static epd_model_t *epd_model_get(epd_model_id_t id)
327+
epd_model_t *epd_get(void)
328+
{
329+
return EPD == NULL ? epd_models[0] : EPD;
330+
}
331+
332+
epd_model_t *epd_init(epd_model_id_t id)
314333
{
315334
for (uint8_t i = 0; i < ARRAY_SIZE(epd_models); i++) {
316335
if (epd_models[i]->id == id) {
317-
return epd_models[i];
336+
EPD = epd_models[i];
318337
}
319338
}
320-
return epd_models[0];
321-
}
322-
323-
epd_driver_t *epd_model_init(epd_model_id_t id)
324-
{
325-
epd_model_t *model = epd_model_get(id);
326-
model->drv->init(model->res, model->bwr);
327-
return model->drv;
339+
if (EPD == NULL) EPD = epd_models[0];
340+
EPD->drv->init();
341+
return EPD;
328342
}

EPD/EPD_driver.h

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,17 @@
1818
#include <stdbool.h>
1919
#include <stdint.h>
2020
#include <stdlib.h>
21+
#include "EPD_config.h"
2122

2223
#define BIT(n) (1UL << (n))
2324

24-
// Display resolution
25-
typedef enum
26-
{
27-
EPD_RES_400x300,
28-
EPD_RES_320x300,
29-
EPD_RES_320x240,
30-
EPD_RES_200x300,
31-
} epd_res_t;
32-
3325
/**@brief EPD driver structure.
3426
*
3527
* @details This structure contains epd driver functions.
3628
*/
3729
typedef struct
3830
{
39-
void (*init)(epd_res_t res, bool bwr); /**< Initialize the e-Paper register */
31+
void (*init)(); /**< Initialize the e-Paper register */
4032
void (*clear)(void); /**< Clear screen */
4133
void (*write_image)(uint8_t *black, uint8_t *color, uint16_t x, uint16_t y, uint16_t w, uint16_t h); /**< write image */
4234
void (*refresh)(void); /**< Sends the image buffer in RAM to e-Paper and displays */
@@ -58,25 +50,12 @@ typedef struct
5850
{
5951
epd_model_id_t id;
6052
epd_driver_t *drv;
61-
epd_res_t res;
53+
uint16_t width;
54+
uint16_t height;
6255
bool bwr;
56+
bool invert_color;
6357
} epd_model_t;
6458

65-
extern uint32_t EPD_MOSI_PIN;
66-
extern uint32_t EPD_SCLK_PIN;
67-
extern uint32_t EPD_CS_PIN;
68-
extern uint32_t EPD_DC_PIN;
69-
extern uint32_t EPD_RST_PIN;
70-
extern uint32_t EPD_BUSY_PIN;
71-
extern uint32_t EPD_BS_PIN;
72-
extern uint32_t EPD_EN_PIN;
73-
extern uint32_t EPD_LED_PIN;
74-
75-
extern uint16_t EPD_WIDTH;
76-
extern uint16_t EPD_HEIGHT;
77-
78-
extern bool EPD_BWR_MODE;
79-
8059
#define LOW (0x0)
8160
#define HIGH (0x1)
8261

@@ -92,6 +71,7 @@ uint32_t digitalRead(uint32_t pin);
9271
void delay(uint32_t ms);
9372

9473
// GPIO
74+
void EPD_GPIO_Load(epd_config_t *cfg);
9575
void EPD_GPIO_Init(void);
9676
void EPD_GPIO_Uninit(void);
9777

@@ -115,8 +95,9 @@ void EPD_WaitBusy(uint32_t value, uint16_t timeout);
11595
// lED
11696
void EPD_LED_ON(void);
11797
void EPD_LED_OFF(void);
118-
void EPD_LED_TOGGLE(void);
98+
void EPD_LED_Toggle(void);
11999

120-
epd_driver_t *epd_model_init(epd_model_id_t id);
100+
epd_model_t *epd_get(void);
101+
epd_model_t *epd_init(epd_model_id_t id);
121102

122103
#endif

0 commit comments

Comments
 (0)