Skip to content

Commit 74d06d7

Browse files
committed
change bat measure to every 1 second
1 parent 6d98c3b commit 74d06d7

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

fw/application/src/bat.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ void saadc_callback(nrf_drv_saadc_evt_t const *p_event) { NRF_LOG_INFO("ADC even
4040

4141
chrg_data_t chrg = {0};
4242

43-
void chrg_read(void* p_context) {
44-
uint32_t new_stats = nrf_gpio_pin_read(CHRG_PIN);
45-
if (new_stats == chrg.stats) return;
46-
chrg.stats = new_stats;
47-
if (chrg.callback != NULL) chrg.callback();
43+
static void bat_measure(chrg_data_t *p_chrg);
44+
45+
void chrg_read(void *p_context) {
46+
uint8_t old_stats = chrg.stats;
47+
uint8_t old_level = chrg.level;
48+
bat_measure(&chrg);
49+
if ((old_level != chrg.level || old_stats != chrg.stats) && chrg.callback != NULL) {
50+
chrg.callback();
51+
}
4852
}
4953

5054
void chrg_set_callback(void *cb) { chrg.callback = cb; }
@@ -61,13 +65,16 @@ void chrg_init(void) {
6165

6266
if (settings_get_data()->bat_mode) {
6367
nrf_gpio_cfg_input(CHRG_PIN, NRF_GPIO_PIN_PULLUP);
68+
}
6469

65-
err_code = app_timer_create(&m_chrg_timer, APP_TIMER_MODE_REPEATED, chrg_read);
66-
APP_ERROR_CHECK(err_code);
70+
// measure once
71+
bat_measure(&chrg);
6772

68-
err_code = app_timer_start(m_chrg_timer, APP_TIMER_TICKS(200), NULL);
69-
APP_ERROR_CHECK(err_code);
70-
}
73+
err_code = app_timer_create(&m_chrg_timer, APP_TIMER_MODE_REPEATED, chrg_read);
74+
APP_ERROR_CHECK(err_code);
75+
76+
err_code = app_timer_start(m_chrg_timer, APP_TIMER_TICKS(1000), NULL);
77+
APP_ERROR_CHECK(err_code);
7178
}
7279

7380
void saadc_init(void) {
@@ -88,7 +95,9 @@ void saadc_uninit(void) {
8895
nrf_drv_saadc_uninit();
8996
}
9097

91-
uint8_t bat_get_level(void) {
98+
uint8_t bat_get_level(void) { return chrg.level; }
99+
100+
void bat_measure(chrg_data_t *p_chrg) {
92101

93102
nrf_saadc_value_t adc_value;
94103
ret_code_t err_code;
@@ -121,5 +130,7 @@ uint8_t bat_get_level(void) {
121130
// sprintf(txt, "BAT: %d, %.02fV, %d", adc_value, voltage, level);
122131
// NRF_LOG_INFO("%s", nrf_log_push(txt));
123132

124-
return level;
133+
p_chrg->level = level;
134+
p_chrg->voltage = voltage;
135+
p_chrg->stats = nrf_gpio_pin_read(CHRG_PIN);
125136
}

fw/application/src/bat.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@
1010
#include <stdint.h>
1111
#include <stdbool.h>
1212

13-
typedef enum {
14-
EMPTY,
15-
LEVEL_1,
16-
LEVEL_2,
17-
LEVEL_3,
18-
FULL
19-
} bat_level_t;
2013

2114
typedef void (*chrg_data_cb_t)(void);
2215
typedef struct chrg {
23-
uint32_t stats;
16+
uint8_t stats;
17+
uint8_t level;
18+
float voltage;
2419
chrg_data_cb_t callback;
2520
} chrg_data_t;
2621

0 commit comments

Comments
 (0)