Skip to content

Commit 3ff2f5e

Browse files
committed
show battery current
1 parent 0696369 commit 3ff2f5e

File tree

4 files changed

+102
-66
lines changed

4 files changed

+102
-66
lines changed

Distr/nrf24batch/CO2_mini.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ W: CO2 correct=,7,0x82
4444
R: FanLSB[10]=i:9#
4545
W: FanLSB=,i:9
4646

47+
R: Send pause=19
48+
W: Send pause=,19
49+
4750
W: Reset=,RESET,0xC1
4851

4952
RBatch: Settings: ID;RxAddr;Ch;Send period;CO2 threshold;CO2 correct;FanLSB;nRF RETR;OSCCAL
5053

5154
WBatch: Default: RxAddr=0xCF;Ch=122;Send period=30;CO2 threshold=1000;CO2 correct=0;FanLSB={0xC1,0};nRF RETR=0x17;Reset
5255
WBatch: CO2: CO2 threshold=1000;CO2 correct=0
5356
WBatch: Fan2: FanLSB={0xC1,0,0,0,0,0,0};Reset
54-
WBatch: RETR: nRF RETR=0x0F;Reset
57+
WBatch: RETR: nRF RETR=0x2F;Reset
58+
WBatch: Send pause: Send pause=30
5559
WBatch: Reset: Reset

lib/nrf24/nrf24.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ uint8_t nrf24_rxpacket(FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* re
215215
memcpy(packet, &buf[1], packet_size);
216216
nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
217217
}
218-
// if(status & (TX_DS | MAX_RT)) { // MAX_RT, TX_DS
219-
// nrf24_write_reg(handle, REG_STATUS, (TX_DS | MAX_RT)); // clear RX_DR, MAX_RT.
220-
// }
218+
if(status & (MAX_RT)) { // MAX_RT
219+
nrf24_write_reg(handle, REG_STATUS, (MAX_RT)); // clear MAX_RT.
220+
}
221221

222222
*ret_packetsize = packet_size;
223223
return status;

nrf24batch.c

+90-57
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ char *ReadBatch_cmd_curr = NULL; // =0xFFFFFFFF - finish
133133
FuriString **WriteBatch_cmd = NULL; // Names of write batch cmd
134134
uint16_t WriteBatch_cmd_Total = 0;
135135
uint16_t WriteBatch_cmd_curr = 0; // == _Total - finish
136+
#define POWER_READ_PERIOD 10 // ms
137+
uint16_t pwr_read_timer = 0;
138+
int Current = 0;
139+
int CurrentStart = 0;
136140

137141
enum {
138142
ask_write_batch = 1,
@@ -283,6 +287,25 @@ void free_store(void)
283287
free_Log();
284288
}
285289

290+
void update_power(void)
291+
{
292+
Current = furi_hal_power_get_battery_current(FuriHalPowerICFuelGauge) * 1000;
293+
}
294+
295+
void check_en_power_5V(void)
296+
{
297+
if(!furi_hal_power_is_otg_enabled()) {
298+
FURI_LOG_D("PWR", "NO 5V, TURN ON");
299+
furi_delay_ms(11);
300+
update_power();
301+
CurrentStart = Current;
302+
furi_hal_power_enable_otg();
303+
NRF_BOARD_POWER_5V = true;
304+
furi_delay_ms(100);
305+
NRF_INITED = 0;
306+
}
307+
}
308+
286309
static bool select_settings_file() {
287310
DialogsApp* dialogs = furi_record_open("dialogs");
288311
bool result = false;
@@ -310,7 +333,9 @@ static bool select_settings_file() {
310333

311334
static void prepare_nrf24(void)
312335
{
313-
if(!NRF_INITED || !((rw_type == rwt_listen) == (NRF_INITED == rwt_listen))) {
336+
check_en_power_5V();
337+
if(!NRF_INITED || rw_type == rwt_listen || NRF_INITED == rwt_listen) {
338+
FURI_LOG_D("NRF", "Prepare");
314339
uint8_t adrlen, *adr;
315340
if(rw_type == rwt_listen) {
316341
adrlen = listen_addr_len;
@@ -321,6 +346,7 @@ static void prepare_nrf24(void)
321346
adr = addr;
322347
NRF_INITED = 1;
323348
}
349+
furi_hal_gpio_write(nrf24_CE_PIN, false);
324350
nrf24_set_mac(REG_RX_ADDR_P0, adr, adrlen);
325351
uint8_t tmp[5] = { 0 };
326352
nrf24_read_reg(nrf24_HANDLE, REG_RX_ADDR_P0, tmp, adrlen);
@@ -334,24 +360,23 @@ static void prepare_nrf24(void)
334360
NRF_INITED = 0;
335361
return;
336362
}
363+
// EN_DYN_ACK(0x01) option for W_TX_PAYLOAD_NOACK cmd broke AA on some fake nRF24l01+ modules
364+
nrf24_write_reg(nrf24_HANDLE, REG_FEATURE, (NRF_DPL ? 4 : 0)); // Dynamic Payload, Payload with ACK, W_TX_PAYLOAD_NOACK command
337365
nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, NRF_channel);
338366
nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, (NRF_rate == 0 ? 0b00100000 : NRF_rate == 1 ? 0 : 0b00001000) | 0b111); // +TX high power
339367
nrf24_write_reg(nrf24_HANDLE, REG_CONFIG, 0x70 | ((NRF_CRC == 1 ? 0b1000 : NRF_CRC == 2 ? 0b1100 : 0))); // Mask all interrupts
340368
nrf24_write_reg(nrf24_HANDLE, REG_SETUP_RETR, NRF_RETR); // Automatic Retransmission, ARD<<4 + ARC
341369
nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x01); // Auto acknowledgement
342-
nrf24_write_reg(nrf24_HANDLE, REG_FEATURE, NRF24_EN_DYN_ACK | (NRF_DPL ? 4 : 0)); // Enables the W_TX_PAYLOAD_NOACK command, Disable Payload with ACK, set Dynamic Payload
343370
nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? 0x3F : 0); // Enable dynamic payload reg
344371
nrf24_write_reg(nrf24_HANDLE, RX_PW_P0, payload_size);
345372
nrf24_set_maclen(nrf24_HANDLE, adrlen);
346373
nrf24_set_mac(REG_TX_ADDR, adr, adrlen);
347374
nrf24_write_reg(nrf24_HANDLE, REG_EN_RXADDR, 1);
348375
//nrf24_set_idle(nrf24_HANDLE);
349-
NRF_INITED = true;
350376
}
351377
nrf24_flush_tx(nrf24_HANDLE);
352378
nrf24_flush_rx(nrf24_HANDLE);
353379
nrf24_write_reg(nrf24_HANDLE, REG_STATUS, MAX_RT | RX_DR | TX_DS);
354-
furi_hal_gpio_write(nrf24_CE_PIN, false);
355380
}
356381

357382
// true - ok
@@ -957,8 +982,10 @@ void display_edit_ttf_font(Canvas* const canvas, uint8_t start_x, uint8_t start_
957982
}
958983

959984
static void render_callback(Canvas* const canvas, void* ctx) {
960-
const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
961-
if(plugin_state == NULL) return;
985+
if(ctx == NULL) return;
986+
const PluginState* plugin_state = ctx;
987+
if(furi_mutex_acquire(plugin_state->mutex, 25) != FuriStatusOk) return;
988+
962989
//canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
963990
if(what_doing == 0) {
964991
canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
@@ -1001,6 +1028,8 @@ static void render_callback(Canvas* const canvas, void* ctx) {
10011028
add_to_str_hex_bytes(screen_buf, listen_addr, listen_addr_len);
10021029
canvas_draw_str(canvas, 40, 25, screen_buf);
10031030
}
1031+
snprintf(screen_buf, sizeof(screen_buf), "I: %d +(%d) mA", Current - CurrentStart, CurrentStart);
1032+
canvas_draw_str(canvas, 0, 60, screen_buf);
10041033
} else {
10051034
canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
10061035
if(rw_type == rwt_read_batch) {
@@ -1048,6 +1077,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
10481077
snprintf(screen_buf + strlen(screen_buf), 16, "%02d:%02d:%02d", ListenLastTime.hour, ListenLastTime.minute, ListenLastTime.second);
10491078
if(ListenPrev) snprintf(screen_buf + strlen(screen_buf), 16, " (%lu)", ListenLast - ListenPrev);
10501079
} else strcat(screen_buf, "receiving");
1080+
snprintf(screen_buf + strlen(screen_buf), 16, " %dmA", Current - CurrentStart);
10511081
canvas_draw_str(canvas, 0, 10, screen_buf);
10521082
if(ListenFields) {
10531083
char *p2, *p = ListenFields;
@@ -1142,13 +1172,19 @@ static void render_callback(Canvas* const canvas, void* ctx) {
11421172
}
11431173
}
11441174
}
1145-
release_mutex((ValueMutex*)ctx, plugin_state);
1175+
furi_mutex_release(plugin_state->mutex);
11461176
}
11471177

11481178
void work_timer_callback(void* ctx)
11491179
{
1150-
UNUSED(ctx);
1180+
if(ctx == NULL) return;
1181+
if((pwr_read_timer += WORK_PERIOD) >= POWER_READ_PERIOD) {
1182+
pwr_read_timer = 0;
1183+
update_power();
1184+
}
11511185
if(what_doing == 2) {
1186+
const PluginState* plugin_state = ctx;
1187+
if(furi_mutex_acquire(plugin_state->mutex, 0) != FuriStatusOk) return;
11521188
if(rw_type == rwt_write_batch) {
11531189
if(send_status == sst_ok) {
11541190
if(ERR == 0 && WriteBatch_cmd_curr < Log_Total && furi_get_tick() - NRF_time >= delay_between_pkt) {
@@ -1197,47 +1233,43 @@ void work_timer_callback(void* ctx)
11971233
}
11981234
}
11991235
}
1236+
furi_mutex_release(plugin_state->mutex);
12001237
}
12011238
}
12021239

12031240
int32_t nrf24batch_app(void* p) {
12041241
UNUSED(p);
12051242
APP = malloc(sizeof(nRF24Batch));
1206-
APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
1243+
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
12071244
APP->plugin_state = malloc(sizeof(PluginState));
1208-
ValueMutex state_mutex;
1209-
if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) {
1210-
furi_message_queue_free(APP->event_queue);
1245+
APP->plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
1246+
if(!APP->plugin_state->mutex) {
1247+
furi_message_queue_free(event_queue);
12111248
FURI_LOG_E(TAG, "cannot create mutex");
12121249
free(APP->plugin_state);
12131250
return 255;
12141251
}
1215-
12161252
// Set system callbacks
1217-
APP->view_port = view_port_alloc();
1218-
view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex);
1219-
view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue);
1253+
ViewPort* view_port = view_port_alloc();
1254+
view_port_draw_callback_set(view_port, render_callback, APP->plugin_state);
1255+
view_port_input_callback_set(view_port, input_callback, event_queue);
12201256

12211257
// Open GUI and register view_port
12221258
APP->gui = furi_record_open(RECORD_GUI);
1223-
gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen);
1259+
gui_add_view_port(APP->gui, view_port, GuiLayerFullscreen);
12241260
APP->notification = furi_record_open(RECORD_NOTIFICATION);
12251261
APP->storage = furi_record_open(RECORD_STORAGE);
12261262
storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER);
12271263
file_stream = file_stream_alloc(APP->storage);
1228-
FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, NULL);
1264+
FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, APP->plugin_state);
12291265
furi_timer_start(work_timer, WORK_PERIOD);
1230-
if(!furi_hal_power_is_otg_enabled()) {
1231-
furi_hal_power_enable_otg();
1232-
NRF_BOARD_POWER_5V = true;
1233-
furi_delay_ms(100);
1234-
}
12351266
nrf24_init();
1267+
check_en_power_5V();
12361268

12371269
PluginEvent event;
12381270
for(bool processing = true; processing;) {
1239-
FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 200);
1240-
PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
1271+
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
1272+
furi_mutex_acquire(APP->plugin_state->mutex, FuriWaitForever);
12411273

12421274
static FuriLogLevel FuriLogLevel = FuriLogLevelDefault;
12431275
if(furi_log_get_level() != FuriLogLevel) {
@@ -1415,15 +1447,15 @@ int32_t nrf24batch_app(void* p) {
14151447
Edit_pos = ebuf + strlen(ebuf) - 1;
14161448
Edit_start = ebuf;
14171449
Edit = 1;
1418-
NRF_INITED = false;
1450+
NRF_INITED = 0;
14191451
} else if(setup_cursor == 2) { // change channel
14201452
char *ebuf = (char*)payload;
14211453
snprintf(ebuf, sizeof(payload), "%d", NRF_channel);
14221454
Edit_hex = false;
14231455
Edit_pos = ebuf + strlen(ebuf) - 1;
14241456
Edit_start = ebuf;
14251457
Edit = 1;
1426-
NRF_INITED = false;
1458+
NRF_INITED = 0;
14271459
}
14281460
} else if(what_doing == 1) {
14291461
if(rw_type == rwt_read_batch) {
@@ -1471,27 +1503,8 @@ int32_t nrf24batch_app(void* p) {
14711503
ask_question = ask_save_batch;
14721504
ask_question_answer = 0;
14731505
} else if(rw_type == rwt_write_batch) {
1474-
if(!Edit) {
1475-
Edit = 0;
1476-
Edit_hex = 0;
1477-
char *s = (char*)furi_string_get_cstr(Log[view_Batch]);
1478-
char *p = strchr(s, '=');
1479-
if(p) {
1480-
p++;
1481-
if(*p == '{') p++; // array
1482-
if(*(p + 1) == 'x') {
1483-
p += 2;
1484-
Edit_hex = 1; // hex
1485-
}
1486-
if(is_digit(p, Edit_hex)) {
1487-
Edit_start = p;
1488-
while(is_digit(p, Edit_hex)) p++;
1489-
Edit_pos = p - 1;
1490-
Edited = true;
1491-
Edit = 1;
1492-
}
1493-
}
1494-
}
1506+
ask_question = ask_write_batch;
1507+
ask_question_answer = 0;
14951508
}
14961509
}
14971510
}
@@ -1518,15 +1531,34 @@ int32_t nrf24batch_app(void* p) {
15181531
Edit_pos = ebuf + strlen(ebuf) - 1;
15191532
Edit_start = ebuf;
15201533
Edit = 1;
1521-
NRF_INITED = false;
1534+
NRF_INITED = 0;
15221535
}
15231536
} else if(what_doing == 2) {
15241537
if(rw_type == rwt_read_cmd) {
15251538
ReadRepeat = !ReadRepeat;
15261539
} else if(Log_Total) {
15271540
if(rw_type == rwt_write_batch) {
1528-
ask_question = ask_write_batch;
1529-
ask_question_answer = 0;
1541+
if(!Edit) {
1542+
Edit = 0;
1543+
Edit_hex = 0;
1544+
char *s = (char*)furi_string_get_cstr(Log[view_Batch]);
1545+
char *p = strchr(s, '=');
1546+
if(p) {
1547+
p++;
1548+
if(*p == '{') p++; // array
1549+
if(*(p + 1) == 'x') {
1550+
p += 2;
1551+
Edit_hex = 1; // hex
1552+
}
1553+
if(is_digit(p, Edit_hex)) {
1554+
Edit_start = p;
1555+
while(is_digit(p, Edit_hex)) p++;
1556+
Edit_pos = p - 1;
1557+
Edited = true;
1558+
Edit = 1;
1559+
}
1560+
}
1561+
}
15301562
} else if(rw_type == rwt_read_batch) {
15311563
ask_question = ask_save_batch;
15321564
ask_question_answer = 0;
@@ -1575,27 +1607,28 @@ int32_t nrf24batch_app(void* p) {
15751607
}
15761608
}
15771609

1578-
view_port_update(APP->view_port);
1579-
release_mutex(&state_mutex, plugin_state);
1610+
view_port_update(view_port);
1611+
furi_mutex_release(APP->plugin_state->mutex);
15801612
}
15811613
nrf24_set_idle(nrf24_HANDLE);
15821614
nrf24_deinit();
15831615
if(NRF_BOARD_POWER_5V) furi_hal_power_disable_otg();
15841616

1585-
view_port_enabled_set(APP->view_port, false);
1586-
gui_remove_view_port(APP->gui, APP->view_port);
1617+
view_port_enabled_set(view_port, false);
1618+
gui_remove_view_port(APP->gui, view_port);
15871619
furi_record_close(RECORD_GUI);
15881620
furi_record_close(RECORD_NOTIFICATION);
15891621
furi_record_close(RECORD_STORAGE);
15901622
if(file_stream) {
15911623
file_stream_close(file_stream);
15921624
stream_free(file_stream);
15931625
}
1594-
view_port_free(APP->view_port);
1595-
furi_message_queue_free(APP->event_queue);
1626+
view_port_free(view_port);
1627+
furi_message_queue_free(event_queue);
15961628
free_store();
15971629
furi_timer_stop(work_timer);
15981630
furi_timer_free(work_timer);
1631+
furi_mutex_free(APP->plugin_state->mutex);
15991632
free(APP->plugin_state);
16001633
free(APP);
16011634
return 0;

nrf24batch.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <gui/modules/dialog_ex.h>
88
#include <toolbox/stream/file_stream.h>
99
#include <notification/notification_messages.h>
10+
#include <power/power_service/power.h>
11+
#include <power/power_service/power_i.h>
1012

1113
typedef enum {
1214
EventTypeTick,
@@ -19,16 +21,13 @@ typedef struct {
1921
} PluginEvent;
2022

2123
typedef struct {
22-
int x;
23-
int y;
24+
FuriMutex* mutex;
2425
} PluginState;
2526

2627
typedef struct {
2728
Gui* gui;
28-
FuriMessageQueue* event_queue;
29-
PluginState* plugin_state;
30-
ViewPort* view_port;
3129
Storage* storage;
3230
NotificationApp* notification;
31+
PluginState* plugin_state;
3332
} nRF24Batch;
3433

0 commit comments

Comments
 (0)