Skip to content

Commit 0cbad41

Browse files
committed
ability to change address and channel
1 parent 3c96191 commit 0cbad41

File tree

2 files changed

+161
-80
lines changed

2 files changed

+161
-80
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Flipper Zero application for nRF24L01 external board. Sends batch commands.
55
Можно использовать для настройки или чтения данных с удаленного устройства. На удаленной стороне требуется поддержка, ссылка на код для микроконтроллера AVR ниже.<br>
66
<br>
77
Сначала выбирается файл настройки с описанием команд.
8+
Можно отредактировать адрес и номер канала.
89
Затем стрелками влево или вправо выбирается нужный режим - Пакетное чтение (Read Batch), Чтение по одной команде (Read cmd), Пакетная запись (Write Batch).
910
<br><br>
1011
Есть два вида команд: запрос-ответ и запись.<br>

nrf24batch.c

+160-80
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
#define TAG "nrf24batch"
1717
#define VERSION "1.2"
1818

19-
#define SCAN_APP_PATH_FOLDER "/ext/nrf24batch"
20-
#define LOG_FILENAME "log"
21-
#define LOG_FILEEXT ".txt"
22-
#define NRF_READ_TIMEOUT 300UL // ms
23-
#define WORK_PERIOD 2 // ms, Timer period
19+
#define SCAN_APP_PATH_FOLDER "/ext/nrf24batch"
20+
#define LOG_FILEEXT ".txt"
21+
#define NRF_READ_TIMEOUT 300UL // ms
22+
#define WORK_PERIOD 2 // ms, Timer period
23+
#define MAX_CHANNEL 125
24+
#define FONT_5x7_SCREEN_WIDTH 25
2425

2526
const char SettingsFld_Info[] = "Info:";
2627
const char SettingsFld_Ch[] = "Ch:";
@@ -43,7 +44,6 @@ const char AskQuestion_Save[] = "SAVE BATCH?";
4344
#define Settings_i 'i'
4445
#define Settings_n 'n'
4546
#define VAR_EMPTY ((int32_t)0x80000000)
46-
#define FONT_5x7_SCREEN_WIDTH 25
4747

4848
nRF24Batch* APP;
4949
uint8_t what_doing = 0; // 0 - setup, 1 - cmd list, 2 - view send cmd
@@ -86,6 +86,7 @@ uint8_t NRF_last_packet_send_st = 0;
8686
uint8_t NRF_resend = 1; // number of transaction attempts
8787
uint8_t NRF_repeat = 0; // count number of repeated requests (until < NRF_resend)
8888
uint32_t NRF_time;
89+
uint32_t delay_between_pkt = 10;// ms
8990

9091
uint8_t addr[5]; // nRF24 address, MSB first
9192
uint8_t addr_len; // 2..5
@@ -96,11 +97,12 @@ uint8_t payload_fields = 0;
9697
uint8_t payload_size = 0; // bytes
9798
uint16_t view_Batch = 0; // view pos in Batch or inside WriteBatch (Log[view_Batch])
9899
uint16_t view_WriteBatch = 0; // view pos of WriteBatch list
99-
uint32_t delay_between_pkt = 10;// ms
100+
uint8_t setup_cursor = 0; // cursor position on Setup scr
100101
uint8_t Edit = 0;
101102
char *Edit_pos;
103+
char *Edit_start;
102104
bool Edit_hex; // 0 - decimal, 1 - hex
103-
bool Edited = false;
105+
bool Edited = false; // list of cmds edited
104106

105107
Stream* file_stream = NULL;
106108
FuriString *ReadDefault = NULL;
@@ -895,23 +897,46 @@ static void render_callback(Canvas* const canvas, void* ctx) {
895897
//canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
896898
if(what_doing == 0) {
897899
canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
898-
snprintf(screen_buf, sizeof(screen_buf), "Open: %s", file_name);
899-
canvas_draw_str(canvas, 10, 10, screen_buf);
900+
snprintf(screen_buf, sizeof(screen_buf), "%s: %s", addr_len ? "File" : "Open", file_name);
901+
canvas_draw_str(canvas, 8, 10, screen_buf);
900902
if(addr_len) {
901-
canvas_draw_str(canvas, 10, 22, Info);
902-
strcpy(screen_buf, "Address: ");
903-
add_to_str_hex_bytes(screen_buf, addr, addr_len);
904-
canvas_draw_str(canvas, 10, 32, screen_buf);
905-
snprintf(screen_buf, sizeof(screen_buf), "Ch: %d, Rate: %d", NRF_channel, NRF_rate);
906-
canvas_draw_str(canvas, 10, 42, screen_buf);
903+
if(Edit) {
904+
screen_buf[0] = *Edit_pos; screen_buf[1] = '\0';
905+
int n = canvas_string_width(canvas, screen_buf);
906+
int len = Edit_pos - Edit_start;
907+
memcpy(screen_buf, Edit_start, len);
908+
screen_buf[len] = '\0';
909+
int x = setup_cursor == 1 ? 45 : 55;
910+
x += canvas_string_width(canvas, screen_buf);
911+
int len2 = strlen(Edit_pos);
912+
memcpy(screen_buf + len, Edit_pos, len2);
913+
screen_buf[len + len2] = '\0';
914+
int y = 10 + setup_cursor * 10;
915+
if(setup_cursor == 1) canvas_draw_str(canvas, 45, 20, screen_buf);
916+
else if(setup_cursor == 2) canvas_draw_str(canvas, 55, 30, screen_buf);
917+
canvas_draw_line(canvas, x + (len ? 1 : 0), y + 1, x + n + (*Edit_pos == '1' && len ? 1 : 0), y + 1);
918+
}
919+
if(!Edit || setup_cursor != 1) {
920+
screen_buf[0] = '\0';
921+
add_to_str_hex_bytes(screen_buf, addr, addr_len);
922+
canvas_draw_str(canvas, 45, 20, screen_buf);
923+
}
924+
if(!Edit || setup_cursor != 2) {
925+
snprintf(screen_buf, sizeof(screen_buf), "%d", NRF_channel);
926+
canvas_draw_str(canvas, 55, 30, screen_buf);
927+
}
928+
canvas_draw_str(canvas, 8, 20, "Address:");
929+
snprintf(screen_buf, sizeof(screen_buf), "Rate: %d, Ch:", NRF_rate);
930+
canvas_draw_str(canvas, 8, 30, screen_buf);
907931
snprintf(screen_buf, sizeof(screen_buf), "RB: %d, R: %d, WB: %d", ReadBatch_cmd_Total, Read_cmd_Total, WriteBatch_cmd_Total);
908-
canvas_draw_str(canvas, 10, 52, screen_buf);
909-
//canvas_draw_str(canvas, 10, 60, screen_buf);
932+
canvas_draw_str(canvas, 8, 40, screen_buf);
933+
canvas_draw_str(canvas, 0, 64, Info);
934+
canvas_draw_str(canvas, 0, 10 + setup_cursor * 10, ">");
910935
} else {
911936
snprintf(screen_buf, sizeof(screen_buf), "Ver. %s, vad7", VERSION);
912937
canvas_draw_str(canvas, 10, 60, screen_buf);
938+
canvas_draw_str(canvas, 0, 10, ">");
913939
}
914-
canvas_draw_str(canvas, 0, 10, ">");
915940
} else if(what_doing == 1){
916941
canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
917942
if(rw_type == rwt_read_batch) {
@@ -1111,33 +1136,45 @@ int32_t nrf24batch_app(void* p) {
11111136
case InputKeyUp:
11121137
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
11131138
if(!ask_question) {
1114-
if(what_doing == 0) {
1139+
if(Edit) {
1140+
if(*Edit_pos < '9') (*Edit_pos)++;
1141+
else if(Edit_hex) {
1142+
if(*Edit_pos == '9') *Edit_pos = 'A';
1143+
else if((*Edit_pos & ~0x20) < 'F') (*Edit_pos)++;
1144+
} else if(Edit_pos > Edit_start && *(Edit_pos - 1) < '9') {
1145+
*Edit_pos = '0';
1146+
(*(Edit_pos - 1))++;
1147+
}
1148+
} else if(what_doing == 0) {
1149+
if(addr_len) {
1150+
if(setup_cursor > 0) setup_cursor--; else setup_cursor = 2;
1151+
}
11151152
} else if(what_doing == 1) {
11161153
if(view_cmd[rw_type]) view_cmd[rw_type]--;
11171154
} else if(what_doing == 2) {
1118-
if(Edit) {
1119-
if(*Edit_pos < '9') (*Edit_pos)++;
1120-
else if(Edit_hex) {
1121-
if(*Edit_pos == '9') *Edit_pos = 'A';
1122-
else if((*Edit_pos & ~0x20) < 'F') (*Edit_pos)++;
1123-
}
1124-
} else if(view_Batch) view_Batch--;
1155+
if(view_Batch) view_Batch--;
11251156
}
11261157
}
11271158
}
11281159
break;
11291160
case InputKeyDown:
11301161
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
11311162
if(!ask_question) {
1132-
if(what_doing == 0) {
1133-
what_doing = 1;
1163+
if(Edit) {
1164+
if(Edit_hex && (*Edit_pos & ~0x20) == 'A') (*Edit_pos) = '9';
1165+
else if(*Edit_pos > '0') (*Edit_pos)--;
1166+
else if(!Edit_hex && Edit_pos > Edit_start && *(Edit_pos - 1) > '0') {
1167+
*Edit_pos = '9';
1168+
(*(Edit_pos - 1))--;
1169+
}
1170+
} else if(what_doing == 0) {
1171+
if(addr_len) {
1172+
if(setup_cursor < 2) setup_cursor++; else setup_cursor = 0;
1173+
}
11341174
} else if(what_doing == 1) {
11351175
if(view_cmd[rw_type] + 1 < (rw_type == rwt_read_batch ? ReadBatch_cmd_Total : rw_type == rwt_read_cmd ? Read_cmd_Total : WriteBatch_cmd_Total)) view_cmd[rw_type]++;
11361176
} else if(what_doing == 2) {
1137-
if(Edit) {
1138-
if(Edit_hex && (*Edit_pos & ~0x20) == 'A') (*Edit_pos) = '9';
1139-
else if(*Edit_pos > '0') (*Edit_pos)--;
1140-
} else if(view_Batch < Log_Total - 1) view_Batch++;
1177+
if(view_Batch < Log_Total - 1) view_Batch++;
11411178
}
11421179
}
11431180
}
@@ -1146,17 +1183,21 @@ int32_t nrf24batch_app(void* p) {
11461183
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
11471184
if(ask_question) {
11481185
if(event.input.type == InputTypeShort) ask_question_answer ^= 1;
1186+
} else if(Edit) {
1187+
if(Edit_pos > Edit_start) {
1188+
if(is_digit(Edit_pos - 1, Edit_hex)) Edit_pos--;
1189+
else if(*(Edit_pos - 1) == 'x' && *(Edit_pos - 3) == ',') Edit_pos -= 4;
1190+
else if(*(Edit_pos - 1) == ',') Edit_pos -= 2;
1191+
}
11491192
} else if(what_doing == 0) {
1193+
rw_type = rwt_write_batch;
1194+
what_doing = 1;
11501195
} else if(what_doing == 1) {
11511196
if(event.input.type == InputTypeShort) {
11521197
if(--rw_type > rwt_write_batch) rw_type = rwt_write_batch;
11531198
} else if(view_x) view_x--;
11541199
} else if(what_doing == 2) {
1155-
if(Edit) {
1156-
if(is_digit(Edit_pos - 1, Edit_hex)) Edit_pos--;
1157-
else if(*(Edit_pos - 1) == 'x' && *(Edit_pos - 3) == ',') Edit_pos -= 4;
1158-
else if(*(Edit_pos - 1) == ',') Edit_pos -= 2;
1159-
} else if(view_x) view_x--;
1200+
if(view_x) view_x--;
11601201
}
11611202
} else if(event.input.type == InputTypeLong) {
11621203
if(!ask_question && view_x == 0 && what_doing == 2 && (rw_type == rwt_write_batch || rw_type == rwt_read_batch)
@@ -1170,20 +1211,21 @@ int32_t nrf24batch_app(void* p) {
11701211
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
11711212
if(ask_question) {
11721213
ask_question_answer ^= 1;
1214+
} else if(Edit) {
1215+
if(is_digit(Edit_pos + 1, Edit_hex)) Edit_pos++;
1216+
else if(*(Edit_pos + 1) == ',') {
1217+
Edit_pos += 2;
1218+
if(*(Edit_pos + 1) == 'x') Edit_pos += 2;
1219+
}
11731220
} else if(what_doing == 0) {
1221+
rw_type = rwt_read_batch;
11741222
what_doing = 1;
11751223
} else if(what_doing == 1) {
11761224
if(event.input.type == InputTypeShort) {
11771225
if(++rw_type > rwt_write_batch) rw_type = rwt_read_batch;
11781226
} else view_x++;
11791227
} else if(what_doing == 2) {
1180-
if(Edit) {
1181-
if(is_digit(Edit_pos + 1, Edit_hex)) Edit_pos++;
1182-
else if(*(Edit_pos + 1) == ',') {
1183-
Edit_pos += 2;
1184-
if(*(Edit_pos + 1) == 'x') Edit_pos += 2;
1185-
}
1186-
} else view_x++;
1228+
view_x++;
11871229
}
11881230
}
11891231
break;
@@ -1217,11 +1259,49 @@ int32_t nrf24batch_app(void* p) {
12171259
}
12181260
}
12191261
ask_question = 0;
1262+
} else if(Edit) { // insert digit
1263+
if(what_doing == 0) {
1264+
if(strlen(Edit_start) < (setup_cursor == 1 ? 5 * 2 : 3)) {
1265+
memmove(Edit_pos + 1, Edit_pos, strlen(Edit_pos) + 1);
1266+
*Edit_pos = '0';
1267+
}
1268+
} else {
1269+
FuriString *fs = Log[view_Batch];
1270+
FuriString *ns = furi_string_alloc();
1271+
if(ns) {
1272+
uint16_t len = Edit_pos - (char*)furi_string_get_cstr(fs);
1273+
furi_string_set_n(ns, fs, 0, len);
1274+
furi_string_cat_str(ns, "0");
1275+
furi_string_cat_str(ns, Edit_pos);
1276+
furi_string_free(fs);
1277+
Log[view_Batch] = ns;
1278+
Edit_pos = (char*)furi_string_get_cstr(ns) + len;
1279+
}
1280+
}
12201281
} else if(what_doing == 0) {
1221-
file_stream_close(file_stream);
1222-
if(select_settings_file()) {
1223-
uint8_t err = load_settings_file();
1224-
if(err) snprintf(file_name, sizeof(file_name), "LOAD ERROR #%d", err);
1282+
if(setup_cursor == 0) { // open file
1283+
file_stream_close(file_stream);
1284+
if(select_settings_file()) {
1285+
uint8_t err = load_settings_file();
1286+
if(err) snprintf(file_name, sizeof(file_name), "LOAD ERROR #%d", err);
1287+
}
1288+
} else if(setup_cursor == 1) { // change address
1289+
char *ebuf = (char*)payload;
1290+
ebuf[0] = '\0';
1291+
add_to_str_hex_bytes(ebuf, addr, addr_len);
1292+
Edit_hex = true;
1293+
Edit_pos = ebuf + strlen(ebuf) - 1;
1294+
Edit_start = ebuf;
1295+
Edit = 1;
1296+
NRF_INITED = false;
1297+
} else if(setup_cursor == 2) { // change channel
1298+
char *ebuf = (char*)payload;
1299+
snprintf(ebuf, sizeof(payload), "%d", NRF_channel);
1300+
Edit_hex = false;
1301+
Edit_pos = ebuf + strlen(ebuf) - 1;
1302+
Edit_start = ebuf;
1303+
Edit = 1;
1304+
NRF_INITED = false;
12251305
}
12261306
} else if(what_doing == 1) {
12271307
if(rw_type == rwt_read_batch) {
@@ -1256,19 +1336,7 @@ int32_t nrf24batch_app(void* p) {
12561336
ask_question = ask_save_batch;
12571337
ask_question_answer = 0;
12581338
} else if(rw_type == rwt_write_batch) {
1259-
if(Edit) { // insert digit
1260-
FuriString *fs = Log[view_Batch];
1261-
FuriString *ns = furi_string_alloc();
1262-
if(ns) {
1263-
uint16_t len = Edit_pos - (char*)furi_string_get_cstr(fs);
1264-
furi_string_set_n(ns, fs, 0, len);
1265-
furi_string_cat_str(ns, "0");
1266-
furi_string_cat_str(ns, Edit_pos);
1267-
furi_string_free(fs);
1268-
Log[view_Batch] = ns;
1269-
Edit_pos = (char*)furi_string_get_cstr(ns) + len;
1270-
}
1271-
} else {
1339+
if(!Edit) {
12721340
Edit = 1;
12731341
Edited = true;
12741342
Edit_hex = 0;
@@ -1281,25 +1349,30 @@ int32_t nrf24batch_app(void* p) {
12811349
p += 2;
12821350
Edit_hex = 1; // hex
12831351
}
1284-
Edit_pos = p;
1352+
Edit_start = Edit_pos = p;
12851353
} else Edit = 0;
12861354
}
12871355
}
12881356
}
12891357
}
12901358
} else if(event.input.type == InputTypeLong) {
1291-
if(what_doing == 2 && Log_Total) {
1292-
if(rw_type == rwt_write_batch) {
1293-
if(Edit) { // delete
1294-
FuriString *fs = Log[view_Batch];
1295-
if(is_digit(Edit_pos + 1, Edit_hex) || is_digit(Edit_pos - 1, Edit_hex)) {
1296-
memmove(Edit_pos, Edit_pos + 1, strlen(Edit_pos));
1297-
furi_string_left(fs, furi_string_size(fs) - 1);
1298-
}
1299-
} else {
1300-
ask_question = ask_write_batch;
1301-
ask_question_answer = 0;
1359+
if(Edit) { // delete
1360+
if(what_doing == 0) {
1361+
if(strlen(Edit_start) > 1) {
1362+
memmove(Edit_pos, Edit_pos + 1, strlen(Edit_pos) + 1);
1363+
if(*Edit_pos == '\0') Edit_pos--;
1364+
}
1365+
} else {
1366+
FuriString *fs = Log[view_Batch];
1367+
if(is_digit(Edit_pos + 1, Edit_hex) || (Edit_pos > Edit_start && is_digit(Edit_pos - 1, Edit_hex))) {
1368+
memmove(Edit_pos, Edit_pos + 1, strlen(Edit_pos));
1369+
furi_string_left(fs, furi_string_size(fs) - 1);
13021370
}
1371+
}
1372+
} else if(what_doing == 2 && Log_Total) {
1373+
if(rw_type == rwt_write_batch) {
1374+
ask_question = ask_write_batch;
1375+
ask_question_answer = 0;
13031376
} else if(rw_type == rwt_read_batch) {
13041377
ask_question = ask_save_batch;
13051378
ask_question_answer = 0;
@@ -1314,15 +1387,22 @@ int32_t nrf24batch_app(void* p) {
13141387
ask_question = ask_exit;
13151388
} else processing = false;
13161389
} else if(event.input.type == InputTypeShort) {
1317-
if(Edit) Edit = 0;
1318-
else if(ask_question) ask_question = 0;
1319-
else {
1390+
if(ask_question) ask_question = 0;
1391+
else if(Edit) {
1392+
if(what_doing == 0) {
1393+
if(setup_cursor == 1) {
1394+
addr_len = ConvertHexToArray((char*)payload, addr, 5);
1395+
} else if(setup_cursor == 2) {
1396+
NRF_channel = str_to_int((char*)payload);
1397+
if(NRF_channel > MAX_CHANNEL) NRF_channel = MAX_CHANNEL;
1398+
}
1399+
}
1400+
Edit = 0;
1401+
} else {
13201402
if(what_doing == 2 && Edited) {
13211403
ask_question = ask_return;
13221404
ask_question_answer = 1;
1323-
} else if(what_doing == 0) {
1324-
processing = false;
1325-
} else {
1405+
} else if(what_doing != 0) {
13261406
if(what_doing) what_doing--;
13271407
if(what_doing == 0) rw_type = rwt_read_batch;
13281408
if(what_doing <= 1) view_x = 0;

0 commit comments

Comments
 (0)