vad7 2 лет назад
Родитель
Сommit
4978e53f30
8 измененных файлов с 2116 добавлено и 1 удалено
  1. 56 0
      Distr/nrf24batch/CO2_mini.txt
  2. 18 1
      README.md
  3. 20 0
      application.fam
  4. 536 0
      lib/nrf24/nrf24.c
  5. 381 0
      lib/nrf24/nrf24.h
  6. 1071 0
      nrf24batch.c
  7. 34 0
      nrf24batch.h
  8. BIN
      nrf24batch_10px.png

+ 56 - 0
Distr/nrf24batch/CO2_mini.txt

@@ -0,0 +1,56 @@
+Info: CO2 sensor mini
+Rate: 1
+Ch: 121
+CRC: 2
+Address: C8C8CF
+Delay_ms: 10
+
+Payload struct: 2,1,1
+EEPROM=0; RAM=1; PROGMEM=2; ID=3; RESET=4
+
+R default: ,EEPROM,0xC1
+W default: n,,0x81
+Write start: ,,0x8F
+
+R: ID*=,ID
+R: OSCCAL=0x51,RAM
+R: OSCCAL_EMEM=0
+R: CO2=0x67,RAM,0xC2
+
+R: RxAddr=1
+W: RxAddr=,1
+
+R: Ch=2
+W: Ch=,2
+
+R: nRF RETR=3
+W: nRF RETR 1/750uS=0x21,3
+
+R: AnswerDelay=9
+W: AnswerDelay=,9
+
+R: Send period=4
+W: Send period=,4
+
+R: CO2 threshold*2=5,,0xC2
+W: CO2 threshold=,5,0x82
+
+R: CO2 correct*2=7,,0xC2
+W: CO2 correct=,7,0x82
+
+R: FanLSB[10]=i:10
+W: FanLSB=,i:19
+
+W: Reset=,RESET,0xC1
+
+RBatch: Settings: ID;OSCCAL;RxAddr;Ch;nRF RETR;CO2 threshold;CO2 correct;FanLSB
+RBatch: Settings2: OSCCAL;OSCCAL_EMEM;RxAddr;Ch;nRF RETR;CO2 threshold;CO2 correct;FanLSB
+
+WBatch: Default Ch-121: RxAddr=0xCF;Ch=121;CO2 threshold=1000;CO2 correct=0;Send period=30;FanLSB={0xC1,0xC2,0xC3,0};Reset
+WBatch: Default Ch-10: RxAddr=0xCF;Ch=10;CO2 threshold=1000;CO2 correct=0;Send period=31;FanLSB={0xC1,0};Reset
+WBatch: CO2: CO2 threshold=1000;CO2 correct=1
+WBatch: Fan: FanLSB={0xC2,0xC3,0};Reset
+WBatch: DELAY=0: AnswerDelay=0
+WBatch: DELAY=100: AnswerDelay=100
+WBatch: DELAY=200: AnswerDelay=200
+WBatch: DELAY=255: AnswerDelay=255

+ 18 - 1
README.md

@@ -1,2 +1,19 @@
 # nRF24-Batch
-Flipper Zero application for nRF24L01 external board. Send batch commands.
+Flipper Zero application for nRF24L01 external board. Send batch commands. (TESTING...)
+
+<b>Приложение для Flipper Zero, предназначено для отправки пакетных команд на удаленные устройства, использующие nRF24L01.</b><br>
+Можно использовать для настройки или чтения данных с удаленного устройства. На удаленной стороне для команды чтения требуется поддержка.<br>
+Два вида команд: запрос-ответ и запись.<br>
+Запрос-ответ - отправка пакета, переключение на прием и отображение на экране, что получили.<br>
+Запись - фактически отправка пакетов подряд с нужными данными.<br>
+<br>
+Описание команд в текстовом файле.<br>
+<br>
+Формат пакета (payload) в виде размера полей структуры в байтах задается, например, так "Payload struct: 2,1,1",<br>
+что означает структуру из 3 полей: 2 байта, 1 байт, 1 байт.<br>
+Перед отправкой пакета, он заполняется сначала шаблону по умолчанию: "R default" - запрос чтения, "W default" - запись.<br>
+Можно использовать константы, они указываются в формате "имя=число", число либо десятичное или шестнадцатеричное с префиксом 0x.<br>
+Затем берутся заполненные значения из самой команды.<br>
+Пакет состоит из списка команд, перечисленных через ";".<br>
+<br>
+Пример файл [CO2_mini]<br>

+ 20 - 0
application.fam

@@ -0,0 +1,20 @@
+App(
+    appid="nRF24_Batch",
+    name="[NRF24] Batch",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="nrf24batch_app",
+    cdefines=["APP_NRF24BATCH"],
+    requires=["gui"],
+    stack_size=2 * 1024,
+    order=60,
+    fap_icon="nrf24batch_10px.png",
+    fap_category="GPIO",
+    fap_private_libs=[
+        Lib(
+            name="nrf24",
+            sources=[
+                "nrf24.c",
+            ],
+        ),
+    ],
+)

+ 536 - 0
lib/nrf24/nrf24.c

@@ -0,0 +1,536 @@
+// Modified by vad7, 25.11.2022
+//
+#include "nrf24.h"
+#include <furi.h>
+#include <furi_hal.h>
+#include <furi_hal_resources.h>
+#include <assert.h>
+#include <string.h>
+
+void nrf24_init() {
+    furi_hal_spi_bus_handle_init(nrf24_HANDLE);
+    furi_hal_spi_acquire(nrf24_HANDLE);
+    furi_hal_gpio_init(nrf24_CE_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
+    furi_hal_gpio_write(nrf24_CE_PIN, false);
+}
+
+void nrf24_deinit() {
+    furi_hal_spi_release(nrf24_HANDLE);
+    furi_hal_spi_bus_handle_deinit(nrf24_HANDLE);
+    furi_hal_gpio_write(nrf24_CE_PIN, false);
+    furi_hal_gpio_init(nrf24_CE_PIN, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
+}
+
+void nrf24_spi_trx(
+    FuriHalSpiBusHandle* handle,
+    uint8_t* tx,
+    uint8_t* rx,
+    uint8_t size) {
+    furi_hal_gpio_write(handle->cs, false);
+    furi_hal_spi_bus_trx(handle, tx, rx, size, nrf24_TIMEOUT);
+    furi_hal_gpio_write(handle->cs, true);
+}
+
+uint8_t nrf24_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
+    uint8_t tx[2] = {W_REGISTER | (REGISTER_MASK & reg), data};
+    uint8_t rx[2] = {0};
+    nrf24_spi_trx(handle, tx, rx, 2);
+    //FURI_LOG_D("NRF_WR", " #%02X=%02X", reg, data);
+    return rx[0];
+}
+
+uint8_t nrf24_write_buf_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
+    uint8_t tx[size + 1];
+    uint8_t rx[size + 1];
+    memset(rx, 0, size + 1);
+    tx[0] = W_REGISTER | (REGISTER_MASK & reg);
+    memcpy(&tx[1], data, size);
+    nrf24_spi_trx(handle, tx, rx, size + 1);
+    //FURI_LOG_D("NRF_WR", " #%02X(%02X)=0x%02X%02X%02X%02X%02X", reg, size, data[0], data[1], data[2], data[3], data[4] );
+    return rx[0];
+}
+
+uint8_t nrf24_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size) {
+    uint8_t tx[size + 1];
+    uint8_t rx[size + 1];
+    memset(rx, 0, size + 1);
+    tx[0] = R_REGISTER | (REGISTER_MASK & reg);
+    memset(&tx[1], 0, size);
+    nrf24_spi_trx(handle, tx, rx, size + 1);
+    memcpy(data, &rx[1], size);
+    return rx[0];
+}
+
+uint8_t nrf24_flush_rx(FuriHalSpiBusHandle* handle) {
+    uint8_t tx[] = {FLUSH_RX};
+    uint8_t rx[] = {0};
+    nrf24_spi_trx(handle, tx, rx, 1);
+    return rx[0];
+}
+
+uint8_t nrf24_flush_tx(FuriHalSpiBusHandle* handle) {
+    uint8_t tx[] = {FLUSH_TX};
+    uint8_t rx[] = {0};
+    nrf24_spi_trx(handle, tx, rx, 1);
+    return rx[0];
+}
+
+uint8_t nrf24_get_maclen(FuriHalSpiBusHandle* handle) {
+    uint8_t maclen;
+    nrf24_read_reg(handle, REG_SETUP_AW, &maclen, 1);
+    maclen &= 3;
+    return maclen + 2;
+}
+
+uint8_t nrf24_set_maclen(FuriHalSpiBusHandle* handle, uint8_t maclen) {
+    assert(maclen > 1 && maclen < 6);
+    uint8_t status = 0;
+    status = nrf24_write_reg(handle, REG_SETUP_AW, maclen - 2);
+    return status;
+}
+
+uint8_t nrf24_status(FuriHalSpiBusHandle* handle) {
+    uint8_t status;
+    uint8_t tx[] = {R_REGISTER | (REGISTER_MASK & REG_STATUS)};
+    nrf24_spi_trx(handle, tx, &status, 1);
+    return status;
+}
+
+uint32_t nrf24_get_rate(FuriHalSpiBusHandle* handle) {
+    uint8_t setup = 0;
+    uint32_t rate = 0;
+    nrf24_read_reg(handle, REG_RF_SETUP, &setup, 1);
+    setup &= 0x28;
+    if(setup == 0x20)
+        rate = 250000; // 250kbps
+    else if(setup == 0x08)
+        rate = 2000000; // 2Mbps
+    else if(setup == 0x00)
+        rate = 1000000; // 1Mbps
+
+    return rate;
+}
+
+uint8_t nrf24_set_rate(FuriHalSpiBusHandle* handle, uint32_t rate) {
+    uint8_t r6 = 0;
+    uint8_t status = 0;
+    if(!rate) rate = 2000000;
+
+    nrf24_read_reg(handle, REG_RF_SETUP, &r6, 1); // RF_SETUP register
+    r6 = r6 & (~0x28); // Clear rate fields.
+    if(rate == 2000000)
+        r6 = r6 | 0x08;
+    else if(rate == 1000000)
+        r6 = r6;
+    else if(rate == 250000)
+        r6 = r6 | 0x20;
+
+    status = nrf24_write_reg(handle, REG_RF_SETUP, r6); // Write new rate.
+    return status;
+}
+
+uint8_t nrf24_get_chan(FuriHalSpiBusHandle* handle) {
+    uint8_t channel = 0;
+    nrf24_read_reg(handle, REG_RF_CH, &channel, 1);
+    return channel;
+}
+
+uint8_t nrf24_set_chan(FuriHalSpiBusHandle* handle, uint8_t chan) {
+    uint8_t status;
+    status = nrf24_write_reg(handle, REG_RF_CH, chan);
+    return status;
+}
+
+uint8_t nrf24_get_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac) {
+    uint8_t size = 0;
+    uint8_t status = 0;
+    size = nrf24_get_maclen(handle);
+    status = nrf24_read_reg(handle, REG_RX_ADDR_P0, mac, size);
+    return status;
+}
+
+uint8_t nrf24_set_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
+    uint8_t status = 0;
+    uint8_t clearmac[] = {0, 0, 0, 0, 0};
+    nrf24_set_maclen(handle, size);
+    nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, clearmac, 5);
+    status = nrf24_write_buf_reg(handle, REG_RX_ADDR_P0, mac, size);
+    return status;
+}
+
+uint8_t nrf24_get_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac) {
+    uint8_t size = 0;
+    uint8_t status = 0;
+    size = nrf24_get_maclen(handle);
+    status = nrf24_read_reg(handle, REG_TX_ADDR, mac, size);
+    return status;
+}
+
+uint8_t nrf24_set_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size) {
+    uint8_t status = 0;
+    uint8_t clearmac[] = {0, 0, 0, 0, 0};
+    nrf24_set_maclen(handle, size);
+    nrf24_write_buf_reg(handle, REG_TX_ADDR, clearmac, 5);
+    status = nrf24_write_buf_reg(handle, REG_TX_ADDR, mac, size);
+    return status;
+}
+
+uint8_t nrf24_get_packetlen(FuriHalSpiBusHandle* handle, uint8_t pipe) {
+    uint8_t len = 0;
+    if(pipe > 5) pipe = 0;
+    nrf24_read_reg(handle, RX_PW_P0 + pipe, &len, 1);
+    return len;
+}
+
+uint8_t nrf24_set_packetlen(FuriHalSpiBusHandle* handle, uint8_t len) {
+    uint8_t status = 0;
+    status = nrf24_write_reg(handle, RX_PW_P0, len);
+    return status;
+}
+
+// packet_size: 0 - dyn payload (read from PL_WID), 1 - read from pipe size, >1 - override
+uint8_t nrf24_rxpacket(FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* ret_packetsize, uint8_t packet_size) {
+    uint8_t status = 0;
+    uint8_t tx_cmd[33] = {0}; // 32 max payload size + 1 for command
+    uint8_t tmp_packet[33] = {0};
+
+    status = nrf24_status(handle);
+    if(!(status & RX_DR)) {
+        tx_cmd[0] = R_REGISTER | (REGISTER_MASK & REG_FIFO_STATUS);
+        nrf24_spi_trx(handle, tx_cmd, tmp_packet, 2);
+        if((tmp_packet[1] & 1) == 0) status |= RX_DR; // packet in FIFO buffer
+    }
+    if(status & RX_DR) {
+        if(packet_size == 1)
+            packet_size = nrf24_get_packetlen(handle, (status >> 1) & 7);
+        else if(packet_size == 0){
+            tx_cmd[0] = R_RX_PL_WID; tx_cmd[1] = 0;
+            nrf24_spi_trx(handle, tx_cmd, tmp_packet, 2);
+            packet_size = tmp_packet[1];
+        }
+        if(packet_size > 32 || packet_size == 0) packet_size = 32;
+        tx_cmd[0] = R_RX_PAYLOAD; tx_cmd[1] = 0;
+        nrf24_spi_trx(handle, tx_cmd, tmp_packet, packet_size + 1);
+        memcpy(packet, &tmp_packet[1], packet_size);
+        nrf24_write_reg(handle, REG_STATUS, RX_DR); // clear RX_DR
+    }
+    if(status & (TX_DS | MAX_RT)) { // MAX_RT, TX_DS
+        nrf24_write_reg(handle, REG_STATUS, (TX_DS | MAX_RT)); // clear RX_DR, MAX_RT.
+    }
+
+    *ret_packetsize = packet_size;
+    return status;
+}
+
+// Return 0 when error
+uint8_t nrf24_txpacket(FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack) {
+    uint8_t status = 0;
+    uint8_t tx[size + 1];
+    uint8_t rx[size + 1];
+    memset(tx, 0, size + 1);
+    memset(rx, 0, size + 1);
+
+    if(!ack)
+        tx[0] = W_TX_PAYLOAD_NOACK;
+    else
+        tx[0] = W_TX_PAYLOAD;
+
+    memcpy(&tx[1], payload, size);
+    nrf24_spi_trx(handle, tx, rx, size + 1);
+    nrf24_set_tx_mode(handle);
+
+    uint32_t start_time = furi_get_tick();
+    do {
+        furi_delay_ms(1);
+        status = nrf24_status(handle);
+    } while(!(status & (TX_DS | MAX_RT)) && furi_get_tick() - start_time < 500UL);
+
+    if(status & MAX_RT) nrf24_flush_tx(handle);
+
+    //nrf24_set_idle(handle);
+    if(status & (TX_DS | MAX_RT)) nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
+    return status & TX_DS;
+}
+
+uint8_t nrf24_power_up(FuriHalSpiBusHandle* handle) {
+    uint8_t status = 0;
+    uint8_t cfg = 0;
+    nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
+    cfg = cfg | 2;
+    status = nrf24_write_reg(handle, REG_CONFIG, cfg);
+    //furi_delay_ms(1000);
+    return status;
+}
+
+uint8_t nrf24_set_idle(FuriHalSpiBusHandle* handle) {
+    uint8_t status = 0;
+    uint8_t cfg = 0;
+    nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
+    cfg &= 0xfc; // clear bottom two bits to power down the radio
+    status = nrf24_write_reg(handle, REG_CONFIG, cfg);
+    //nr204_write_reg(handle, REG_EN_RXADDR, 0x0);
+    furi_hal_gpio_write(nrf24_CE_PIN, false);
+    return status;
+}
+
+uint8_t nrf24_set_rx_mode(FuriHalSpiBusHandle* handle) {
+    uint8_t status = 0;
+    uint8_t cfg = 0;
+    //status = nrf24_write_reg(handle, REG_CONFIG, 0x0F); // enable 2-byte CRC, PWR_UP, and PRIM_RX
+    nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
+    cfg |= 0x03; // PWR_UP, and PRIM_RX
+    status = nrf24_write_reg(handle, REG_CONFIG, cfg);
+    //nr204_write_reg(REG_EN_RXADDR, 0x03) // Set RX Pipe 0 and 1
+    furi_hal_gpio_write(nrf24_CE_PIN, true);
+    //furi_delay_ms(2);
+    return status;
+}
+
+uint8_t nrf24_set_tx_mode(FuriHalSpiBusHandle* handle) {
+    uint8_t status = 0;
+    uint8_t cfg = 0;
+    furi_hal_gpio_write(nrf24_CE_PIN, false);
+    nrf24_write_reg(handle, REG_STATUS, TX_DS | MAX_RT);
+    //status = nrf24_write_reg(handle, REG_CONFIG, 0x0E); // enable 2-byte CRC, PWR_UP
+    nrf24_read_reg(handle, REG_CONFIG, &cfg, 1);
+    cfg &= 0xFE; // disable PRIM_RX
+    cfg |= 0x02; // PWR_UP
+    status = nrf24_write_reg(handle, REG_CONFIG, cfg);
+    furi_hal_gpio_write(nrf24_CE_PIN, true);
+    //furi_delay_ms(2);
+    return status;
+}
+
+void nrf24_configure(
+    FuriHalSpiBusHandle* handle,
+    uint8_t rate,
+    uint8_t* srcmac,
+    uint8_t* dstmac,
+    uint8_t maclen,
+    uint8_t channel,
+    bool noack,
+    bool disable_aa) {
+    assert(channel <= 125);
+    assert(rate == 1 || rate == 2);
+    if(rate == 2)
+        rate = 8; // 2Mbps
+    else
+        rate = 0; // 1Mbps
+
+    nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
+    nrf24_set_idle(handle);
+    nrf24_write_reg(handle, REG_STATUS, 0x70); // clear interrupts
+    if(disable_aa)
+        nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
+    else
+        nrf24_write_reg(handle, REG_EN_AA, 0x1F); // Enable Shockburst
+
+    nrf24_write_reg(handle, REG_DYNPD, 0x3F); // enable dynamic payload length on all pipes
+    if(noack)
+        nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
+    else {
+        nrf24_write_reg(handle, REG_CONFIG, 0x0C); // 2 byte CRC
+        nrf24_write_reg(handle, REG_FEATURE, 0x07); // enable dyn payload and ack
+        nrf24_write_reg(
+            handle, REG_SETUP_RETR, 0x1f); // 15 retries for AA, 500us auto retransmit delay
+    }
+
+    nrf24_set_idle(handle);
+    nrf24_flush_rx(handle);
+    nrf24_flush_tx(handle);
+
+    if(maclen) nrf24_set_maclen(handle, maclen);
+    if(srcmac) nrf24_set_src_mac(handle, srcmac, maclen);
+    if(dstmac) nrf24_set_dst_mac(handle, dstmac, maclen);
+
+    nrf24_write_reg(handle, REG_RF_CH, channel);
+    nrf24_write_reg(handle, REG_RF_SETUP, rate);
+    furi_delay_ms(200);
+}
+
+void nrf24_init_promisc_mode(FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate) {
+    //uint8_t preamble[] = {0x55, 0x00}; // little endian
+    uint8_t preamble[] = {0xAA, 0x00}; // little endian
+    //uint8_t preamble[] = {0x00, 0x55}; // little endian
+    //uint8_t preamble[] = {0x00, 0xAA}; // little endian
+    nrf24_write_reg(handle, REG_CONFIG, 0x00); // Stop nRF
+    nrf24_write_reg(handle, REG_STATUS, 0x70); // clear interrupts
+    nrf24_write_reg(handle, REG_DYNPD, 0x0); // disable shockburst
+    nrf24_write_reg(handle, REG_EN_AA, 0x00); // Disable Shockburst
+    nrf24_write_reg(handle, REG_FEATURE, 0x05); // disable payload-with-ack, enable noack
+    nrf24_set_maclen(handle, 2); // shortest address
+    nrf24_set_src_mac(handle, preamble, 2); // set src mac to preamble bits to catch everything
+    nrf24_set_packetlen(handle, 32); // set max packet length
+    nrf24_set_idle(handle);
+    nrf24_flush_rx(handle);
+    nrf24_flush_tx(handle);
+    nrf24_write_reg(handle, REG_RF_CH, channel);
+    nrf24_write_reg(handle, REG_RF_SETUP, rate);
+
+    // prime for RX, no checksum
+    nrf24_write_reg(handle, REG_CONFIG, 0x03); // PWR_UP and PRIM_RX, disable AA and CRC
+    furi_hal_gpio_write(nrf24_CE_PIN, true);
+    furi_delay_ms(100);
+}
+
+void hexlify(uint8_t* in, uint8_t size, char* out) {
+    memset(out, 0, size * 2);
+    for(int i = 0; i < size; i++)
+        snprintf(out + strlen(out), sizeof(out + strlen(out)), "%02X", in[i]);
+}
+
+uint64_t bytes_to_int64(uint8_t* bytes, uint8_t size, bool bigendian) {
+    uint64_t ret = 0;
+    for(int i = 0; i < size; i++)
+        if(bigendian)
+            ret |= bytes[i] << ((size - 1 - i) * 8);
+        else
+            ret |= bytes[i] << (i * 8);
+
+    return ret;
+}
+
+void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian) {
+    for(int i = 0; i < 8; i++) {
+        if(bigendian)
+            out[i] = (val >> ((7 - i) * 8)) & 0xff;
+        else
+            out[i] = (val >> (i * 8)) & 0xff;
+    }
+}
+
+uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian) {
+    uint32_t ret = 0;
+    for(int i = 0; i < 4; i++)
+        if(bigendian)
+            ret |= bytes[i] << ((3 - i) * 8);
+        else
+            ret |= bytes[i] << (i * 8);
+
+    return ret;
+}
+
+void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian) {
+    for(int i = 0; i < 4; i++) {
+        if(bigendian)
+            out[i] = (val >> ((3 - i) * 8)) & 0xff;
+        else
+            out[i] = (val >> (i * 8)) & 0xff;
+    }
+}
+
+uint64_t bytes_to_int16(uint8_t* bytes, bool bigendian) {
+    uint16_t ret = 0;
+    for(int i = 0; i < 2; i++)
+        if(bigendian)
+            ret |= bytes[i] << ((1 - i) * 8);
+        else
+            ret |= bytes[i] << (i * 8);
+
+    return ret;
+}
+
+void int16_to_bytes(uint16_t val, uint8_t* out, bool bigendian) {
+    for(int i = 0; i < 2; i++) {
+        if(bigendian)
+            out[i] = (val >> ((1 - i) * 8)) & 0xff;
+        else
+            out[i] = (val >> (i * 8)) & 0xff;
+    }
+}
+
+// handle iffyness with preamble processing sometimes being a bit (literally) off
+void alt_address_old(uint8_t* packet, uint8_t* altaddr) {
+    uint8_t macmess_hi_b[4];
+    uint8_t macmess_lo_b[2];
+    uint32_t macmess_hi;
+    uint16_t macmess_lo;
+    uint8_t preserved;
+
+    // get first 6 bytes into 32-bit and 16-bit variables
+    memcpy(macmess_hi_b, packet, 4);
+    memcpy(macmess_lo_b, packet + 4, 2);
+
+    macmess_hi = bytes_to_int32(macmess_hi_b, true);
+
+    //preserve least 7 bits from hi that will be shifted down to lo
+    preserved = macmess_hi & 0x7f;
+    macmess_hi >>= 7;
+
+    macmess_lo = bytes_to_int16(macmess_lo_b, true);
+    macmess_lo >>= 7;
+    macmess_lo = (preserved << 9) | macmess_lo;
+    int32_to_bytes(macmess_hi, macmess_hi_b, true);
+    int16_to_bytes(macmess_lo, macmess_lo_b, true);
+    memcpy(altaddr, &macmess_hi_b[1], 3);
+    memcpy(altaddr + 3, macmess_lo_b, 2);
+}
+
+bool validate_address(uint8_t* addr) {
+    uint8_t bad[][3] = {{0x55, 0x55}, {0xAA, 0xAA}, {0x00, 0x00}, {0xFF, 0xFF}};
+    for(int i = 0; i < 4; i++)
+        for(int j = 0; j < 2; j++)
+            if(!memcmp(addr + j * 2, bad[i], 2)) return false;
+
+    return true;
+}
+
+bool nrf24_sniff_address(FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address) {
+    bool found = false;
+    uint8_t packet[32] = {0};
+    uint8_t packetsize;
+    //char printit[65];
+    uint8_t status = 0;
+    status = nrf24_rxpacket(handle, packet, &packetsize, true);
+    if(status & 0x40) {
+        if(validate_address(packet)) {
+            for(int i = 0; i < maclen; i++) address[i] = packet[maclen - 1 - i];
+
+            /*
+            alt_address(packet, packet);
+
+            for(i = 0; i < maclen; i++)
+                address[i + 5] = packet[maclen - 1 - i];
+            */
+
+            //memcpy(address, packet, maclen);
+            //hexlify(packet, packetsize, printit);
+            found = true;
+        }
+    }
+
+    return found;
+}
+
+uint8_t nrf24_find_channel(
+    FuriHalSpiBusHandle* handle,
+    uint8_t* srcmac,
+    uint8_t* dstmac,
+    uint8_t maclen,
+    uint8_t rate,
+    uint8_t min_channel,
+    uint8_t max_channel,
+    bool autoinit) {
+    uint8_t ping_packet[] = {0x0f, 0x0f, 0x0f, 0x0f}; // this can be anything, we just need an ack
+    uint8_t ch = max_channel + 1; // means fail
+    nrf24_configure(handle, rate, srcmac, dstmac, maclen, 2, false, false);
+    for(ch = min_channel; ch <= max_channel + 1; ch++) {
+        nrf24_write_reg(handle, REG_RF_CH, ch);
+        if(nrf24_txpacket(handle, ping_packet, 4, true)) break;
+    }
+
+    if(autoinit) {
+        FURI_LOG_D("nrf24", "initializing radio for channel %d", ch);
+        nrf24_configure(handle, rate, srcmac, dstmac, maclen, ch, false, false);
+        return ch;
+    }
+
+    return ch;
+}
+
+uint8_t nrf24_set_mac(uint8_t mac_addr, uint8_t *mac, uint8_t mlen)
+{
+    uint8_t addr[5];
+	for(int i = 0; i < mlen; i++) addr[i] = mac[mlen - i - 1];
+	return nrf24_write_buf_reg(nrf24_HANDLE, mac_addr, addr, mlen);
+}

+ 381 - 0
lib/nrf24/nrf24.h

@@ -0,0 +1,381 @@
+#pragma once
+#include <stdbool.h>
+#include <stdint.h>
+#include <furi_hal_spi.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define R_REGISTER 0x00
+#define W_REGISTER 0x20
+#define REGISTER_MASK 0x1F
+#define ACTIVATE 0x50
+#define R_RX_PL_WID 0x60
+#define R_RX_PAYLOAD 0x61
+#define W_TX_PAYLOAD 0xA0
+#define W_TX_PAYLOAD_NOACK 0xB0
+#define W_ACK_PAYLOAD 0xA8
+#define FLUSH_TX 0xE1
+#define FLUSH_RX 0xE2
+#define REUSE_TX_PL 0xE3
+#define RF24_NOP 0xFF
+
+#define REG_CONFIG 0x00
+#define REG_EN_AA 0x01
+#define REG_EN_RXADDR 0x02
+#define REG_SETUP_AW 0x03
+#define REG_SETUP_RETR 0x04
+#define REG_DYNPD 0x1C
+#define REG_FEATURE 0x1D
+#define REG_RF_SETUP 0x06
+#define REG_STATUS 0x07
+#define REG_RX_ADDR_P0 0x0A
+#define REG_RX_ADDR_P1 0x0B
+#define REG_RX_ADDR_P2 0x0C
+#define REG_RX_ADDR_P3 0x0D
+#define REG_RX_ADDR_P4 0x0E
+#define REG_RX_ADDR_P5 0x0F
+#define REG_RF_CH 0x05
+#define REG_TX_ADDR 0x10
+#define REG_FIFO_STATUS 0x17
+
+#define RX_PW_P0 0x11
+#define RX_PW_P1 0x12
+#define RX_PW_P2 0x13
+#define RX_PW_P3 0x14
+#define RX_PW_P4 0x15
+#define RX_PW_P5 0x16
+#define RX_DR    0x40
+#define TX_DS    0x20
+#define MAX_RT   0x10
+
+#define nrf24_TIMEOUT 500
+#define nrf24_CE_PIN &gpio_ext_pb2
+#define nrf24_HANDLE &furi_hal_spi_bus_handle_external
+
+/* Low level API */
+
+/** Write device register
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      reg     - register
+ * @param      data    - data to write
+ *
+ * @return     device status
+ */
+uint8_t nrf24_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data);
+
+/** Write buffer to device register
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      reg     - register
+ * @param      data    - data to write
+ * @param      size    - size of data to write
+ *
+ * @return     device status
+ */
+uint8_t nrf24_write_buf_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
+
+/** Read device register
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      reg     - register
+ * @param[out] data    - pointer to data
+ *
+ * @return     device status
+ */
+uint8_t nrf24_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data, uint8_t size);
+
+/** Power up the radio for operation
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_power_up(FuriHalSpiBusHandle* handle);
+
+/** Power down the radio
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_idle(FuriHalSpiBusHandle* handle);
+
+/** Sets the radio to RX mode
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_rx_mode(FuriHalSpiBusHandle* handle);
+
+/** Sets the radio to TX mode
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_tx_mode(FuriHalSpiBusHandle* handle);
+
+/*=============================================================================================================*/
+
+/* High level API */
+
+/** Must call this before using any other nrf24 API
+ * 
+ */
+void nrf24_init();
+
+/** Must call this when we end using nrf24 device
+ * 
+ */
+void nrf24_deinit();
+
+/** Send flush rx command
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ *
+ * @return     device status
+ */
+uint8_t nrf24_flush_rx(FuriHalSpiBusHandle* handle);
+
+/** Send flush tx command
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ *
+ * @return     device status
+ */
+uint8_t nrf24_flush_tx(FuriHalSpiBusHandle* handle);
+
+/** Gets the RX packet length in data pipe 0
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ *             pipe - pipe index (0..5)
+ * @return     packet length in data pipe 0
+ */
+uint8_t nrf24_get_packetlen(FuriHalSpiBusHandle* handle, uint8_t pipe);
+
+/** Sets the RX packet length in data pipe 0
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      len - length to set
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_packetlen(FuriHalSpiBusHandle* handle, uint8_t len);
+
+/** Gets configured length of MAC address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     MAC address length
+ */
+uint8_t nrf24_get_maclen(FuriHalSpiBusHandle* handle);
+
+/** Sets configured length of MAC address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      maclen - length to set MAC address to, must be greater than 1 and less than 6
+ * 
+ * @return     MAC address length
+ */
+uint8_t nrf24_set_maclen(FuriHalSpiBusHandle* handle, uint8_t maclen);
+
+/** Gets the current status flags from the STATUS register
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     status flags
+ */
+uint8_t nrf24_status(FuriHalSpiBusHandle* handle);
+
+/** Gets the current transfer rate
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     transfer rate in bps
+ */
+uint32_t nrf24_get_rate(FuriHalSpiBusHandle* handle);
+
+/** Sets the transfer rate
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      rate - the transfer rate in bps
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_rate(FuriHalSpiBusHandle* handle, uint32_t rate);
+
+/** Gets the current channel
+ * In nrf24, the channel number is multiplied times 1MHz and added to 2400MHz to get the frequency
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * 
+ * @return     channel
+ */
+uint8_t nrf24_get_chan(FuriHalSpiBusHandle* handle);
+
+/** Sets the channel
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      frequency - the frequency in hertz
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_chan(FuriHalSpiBusHandle* handle, uint8_t chan);
+
+/** Gets the source mac address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param[out] mac - the source mac address
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_get_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac);
+
+/** Sets the source mac address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      mac - the mac address to set
+ * @param      size - the size of the mac address (2 to 5)
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_src_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
+
+/** Gets the dest mac address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param[out] mac - the source mac address
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_get_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac);
+
+/** Sets the dest mac address
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      mac - the mac address to set
+ * @param      size - the size of the mac address (2 to 5)
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_set_dst_mac(FuriHalSpiBusHandle* handle, uint8_t* mac, uint8_t size);
+
+/** Reads RX packet
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param[out] packet - the packet contents
+ * @param[out] ret_packetsize - size of the received packet
+ * @param      packet_size: >1 - size, 1 - packet length is determined by RX_PW_P0 register, 0 - it is determined by dynamic payload length command
+ * 
+ * @return     device status
+ */
+uint8_t
+    nrf24_rxpacket(FuriHalSpiBusHandle* handle, uint8_t* packet, uint8_t* ret_packetsize, uint8_t packet_size_flag);
+
+/** Sends TX packet
+ *
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      packet - the packet contents
+ * @param      size - packet size
+ * @param      ack - boolean to determine whether an ACK is required for the packet or not
+ * 
+ * @return     device status
+ */
+uint8_t nrf24_txpacket(FuriHalSpiBusHandle* handle, uint8_t* payload, uint8_t size, bool ack);
+
+/** Configure the radio
+ * This is not comprehensive, but covers a lot of the common configuration options that may be changed
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      rate - transfer rate in Mbps (1 or 2)
+ * @param      srcmac - source mac address
+ * @param      dstmac - destination mac address
+ * @param      maclen - length of mac address
+ * @param      channel - channel to tune to
+ * @param      noack - if true, disable auto-acknowledge
+ * @param      disable_aa - if true, disable ShockBurst
+ * 
+ */
+void nrf24_configure(
+    FuriHalSpiBusHandle* handle,
+    uint8_t rate,
+    uint8_t* srcmac,
+    uint8_t* dstmac,
+    uint8_t maclen,
+    uint8_t channel,
+    bool noack,
+    bool disable_aa);
+
+// Set mac address (MSB first), Return: Status
+uint8_t  nrf24_set_mac(uint8_t mac_addr, uint8_t *mac, uint8_t mlen);
+
+/** Configures the radio for "promiscuous mode" and primes it for rx
+ * This is not an actual mode of the nrf24, but this function exploits a few bugs in the chip that allows it to act as if it were.
+ * See http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html for details.
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      channel - channel to tune to
+ * @param      rate - transfer rate in Mbps (1 or 2) 
+ */
+void nrf24_init_promisc_mode(FuriHalSpiBusHandle* handle, uint8_t channel, uint8_t rate);
+
+/** Listens for a packet and returns first possible address sniffed
+ * Call this only after calling nrf24_init_promisc_mode
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      maclen - length of target mac address
+ * @param[out] addresses - sniffed address
+ * 
+ * @return     success
+ */
+bool nrf24_sniff_address(FuriHalSpiBusHandle* handle, uint8_t maclen, uint8_t* address);
+
+/** Sends ping packet on each channel for designated tx mac looking for ack
+ * 
+ * @param      handle  - pointer to FuriHalSpiHandle
+ * @param      srcmac - source address
+ * @param      dstmac - destination address
+ * @param      maclen - length of address
+ * @param      rate - transfer rate in Mbps (1 or 2) 
+ * @param      min_channel - channel to start with
+ * @param      max_channel - channel to end at
+ * @param      autoinit - if true, automatically configure radio for this channel
+ * 
+ * @return     channel that the address is listening on, if this value is above the max_channel param, it failed
+ */
+uint8_t nrf24_find_channel(
+    FuriHalSpiBusHandle* handle,
+    uint8_t* srcmac,
+    uint8_t* dstmac,
+    uint8_t maclen,
+    uint8_t rate,
+    uint8_t min_channel,
+    uint8_t max_channel,
+    bool autoinit);
+
+/** Converts 64 bit value into uint8_t array
+ * @param      val  - 64-bit integer
+ * @param[out] out - bytes out
+ * @param      bigendian - if true, convert as big endian, otherwise little endian
+ */
+void int64_to_bytes(uint64_t val, uint8_t* out, bool bigendian);
+
+/** Converts 32 bit value into uint8_t array
+ * @param      val  - 32-bit integer
+ * @param[out] out - bytes out
+ * @param      bigendian - if true, convert as big endian, otherwise little endian
+ */
+void int32_to_bytes(uint32_t val, uint8_t* out, bool bigendian);
+
+/** Converts uint8_t array into 32 bit value
+ * @param      bytes  - uint8_t array
+ * @param      bigendian - if true, convert as big endian, otherwise little endian
+ * 
+ * @return     32-bit value
+ */
+uint32_t bytes_to_int32(uint8_t* bytes, bool bigendian);
+
+#ifdef __cplusplus
+}
+#endif

+ 1071 - 0
nrf24batch.c

@@ -0,0 +1,1071 @@
+//
+// Written by vad7, 10.01.2023. vad7@yahoo.com
+//
+#include "nrf24batch.h"
+ 
+#include <furi.h>
+#include <furi_hal.h>
+#include <gui/gui.h>
+#include <dialogs/dialogs.h>
+#include <input/input.h>
+#include <stdlib.h>
+#include <dolphin/dolphin.h>
+#include <nrf24.h>
+#include <u8g2.h>
+
+#define TAG 		"nrf24batch"
+#define VERSION		"1.0"
+#define MAX_CHANNEL	125
+
+#define SCAN_APP_PATH_FOLDER "/ext/nrf24batch"
+#define LOG_FILENAME 		"log"
+#define LOG_FILEEXT	 		".txt"
+#define MAX_LOG_RECORDS		200
+#define MAX_FOUND_RECORDS	70
+#define LOG_REC_SIZE		34		// max packet size
+#define VIEW_LOG_MAX_X		22
+#define VIEW_LOG_WIDTH_B	10		// bytes
+#define NRF_REPEAT_ATTEMPTS	3
+#define NRF_READ_TIMEOUT	700UL	// ms
+#define WORK_PERIOD			10		// ms, frequency of individual cmds
+
+const char SettingsFld_Info[] = "Info:";
+const char SettingsFld_Ch[] = "Ch:";
+const char SettingsFld_Rate[] = "Rate:";
+const char SettingsFld_DPL[] = "DPL:";
+const char SettingsFld_CRC[] = "CRC:";
+const char SettingsFld_Address[] = "Address:";
+const char SettingsFld_Delay[] = "Delay_ms:";
+const char SettingsFld_WriteStart[] = "Write start:";
+const char SettingsFld_Payload[] = "Payload struct:";
+const char SettingsFld_ReadDefault[] = "R default:";
+const char SettingsFld_WriteDefault[] = "W default:";
+const char SettingsFld_Read[] = "R:";
+const char SettingsFld_Write[] = "W:";
+const char SettingsFld_ReadBatch[] = "RBatch:";
+const char SettingsFld_WriteBatch[] = "WBatch:";
+const char AskQuestion_Save[] = "SAVE BATCH?";
+#define Settings_i 'i'
+#define Settings_n 'n'
+#define VAR_EMPTY ((int32_t)0x80000000)
+
+nRF24Batch* APP;
+uint8_t what_doing = 0; // 0 - setup, 1 - cmd list, 2 - view send cmd
+enum {
+	rwt_read_batch = 0,
+	rwt_read_cmd,
+	rwt_write_batch
+};
+uint8_t rw_type = rwt_read_batch;	// What to do: rwt_*
+enum {
+	sst_none = 0,
+	sst_sending,
+	sst_receiving,
+	sst_ok,
+	sst_error,
+	sst_timeout
+};
+uint8_t send_status = sst_none;// sst_*
+bool cmd_array = false;
+uint8_t cmd_array_idx;
+uint8_t cmd_array_cnt = 0;
+uint8_t save_settings = 0;
+uint16_t view_cmd[3] = {0, 0, 0}; // ReadBatch, Read, WriteBatch
+uint8_t view_x = 0;
+char Info[20] = "";
+char screen_buf[64];
+char file_name[32];
+char ERR_STR[32];
+uint8_t ERR = 0;
+uint8_t NRF_rate; 	// 0 - 250Kbps, 1 - 1Mbps, 2 - 2Mbps
+uint8_t NRF_channel;// 0..125
+uint8_t NRF_DPL;	// 1 - Dynamic Payload Length
+uint8_t NRF_CRC;	// 1 - No, 1 - CRC 1byte, 2 - CRC 2byte
+uint8_t NRF_Payload;// Payload len in bytes or Minimum payload in sniff mode, 0..32
+uint8_t NRF_AA_OFF;	// Disable Auto Acknowledgement
+bool NRF_ERROR = 0;
+bool NRF_INITED = false;
+uint8_t NRF_last_packet_send_st = 0;
+uint8_t NRF_repeat = 0;
+uint32_t NRF_time;
+
+uint8_t addr[5];			// nRF24 address, MSB first
+uint8_t addr_len;			// 2..5
+uint8_t payload[32];
+uint8_t payload_receive[32];
+uint8_t payload_struct[32]; 	// sizeof(1..4) in bytes of each field, example: 2,1,1
+uint8_t payload_fields = 0;
+uint8_t payload_size = 0;		// bytes
+FuriString *ReadDefault = NULL;
+FuriString *WriteDefault = NULL;
+uint32_t delay_between_pkt = 5;// ms
+FuriString *Constants = NULL;		// text of STR=x
+FuriString **Read_cmd = NULL;		// Names of read cmd
+uint16_t Read_cmd_Total = 0;
+FuriString **Log = NULL;			// Strings: var=n
+uint16_t Log_Total = 0;
+uint8_t ask_question = 0;			// 1 - Ask now
+uint8_t ask_question_answer = 0;	// 0 - no, 1 - yes
+
+FuriString **ReadBatch_cmd = NULL;	// Names of read batch cmd
+uint16_t ReadBatch_cmd_Total = 0;
+char *ReadBatch_cmd_curr = NULL;	// =0xFFFFFFFF - finish
+uint16_t view_Batch = 0;		// view pos
+
+FuriString **WriteBatch_cmd = NULL;	// Names of write batch cmd
+uint16_t WriteBatch_cmd_Total = 0;
+char *WriteBatch_cmd_curr = NULL;	// =0xFFFFFFFF - finish
+uint16_t view_WriteBatch = 0;		// view pos
+Stream* file_stream = NULL;
+
+//#define MIN(a, b)  ((a<b)?a:b)
+
+static uint8_t GetHexVal(char hex) {
+	return (uint8_t)hex - ((uint8_t)hex < 58 ? 48 : ((uint8_t)hex < 97 ? 55 : 87));
+}
+
+// Return num bytes in array
+static uint8_t ConvertHexToArray(char * hex, uint8_t * array, uint8_t maxlen) {
+	uint8_t len = 0;
+	while(maxlen) {
+		uint8_t ch = *hex++;
+		if(ch < ' ') break;
+		if(ch < '0') continue;
+		*array++ = (GetHexVal(ch) << 4) + GetHexVal(*hex++);
+		len++;
+		maxlen--;
+	}
+	return len;
+}
+
+int32_t str_to_int(char *p)
+{
+	if(*(p+1) == 'x') { // hex
+		return strtol(p + 2, NULL, 16);
+	} else return strtol(p, NULL, 10);
+}
+
+void str_rtrim(char *p)
+{
+	char *delim_col = strchr(p, '\r'); 
+	if(delim_col) *delim_col = '\0';
+	else { 
+		delim_col = strchr(p, '\n'); 
+		if(delim_col) *delim_col = '\0';
+	}
+}
+
+static void add_to_str_hex_bytes(char *out, uint8_t *arr, int bytes)
+{
+	if(bytes <= 0) return;
+	out += strlen(out);
+	do {
+		snprintf(out, 3, "%02X", *arr++);
+		out += 2;
+	} while(--bytes);
+}
+
+void free_Log()
+{
+	if(Log_Total) {
+		for(uint16_t i = 0; i < Log_Total; i++) furi_string_free(Log[i]);
+		Log_Total = 0;
+	}
+	if(Log) {
+		free(Log);
+		Log = NULL;
+	}
+}
+
+void free_store(void)
+{
+	if(Constants) {
+		furi_string_free(Constants);
+		Constants = NULL;
+	}
+	if(ReadDefault) {
+		furi_string_free(ReadDefault);
+		ReadDefault = NULL;
+	}
+	if(WriteDefault) {
+		furi_string_free(WriteDefault);
+		WriteDefault = NULL;
+	}
+	if(Read_cmd_Total) {
+		for(uint16_t i = 0; i < Read_cmd_Total; i++) furi_string_free(Read_cmd[i]);
+		Read_cmd_Total = 0;
+	}
+	if(Read_cmd) {
+		free(Read_cmd);
+		Read_cmd = NULL;
+	}
+	if(ReadBatch_cmd_Total) {
+		for(uint16_t i = 0; i < ReadBatch_cmd_Total; i++) furi_string_free(ReadBatch_cmd[i]);
+		ReadBatch_cmd_Total = 0;
+	}
+	if(ReadBatch_cmd) {
+		free(ReadBatch_cmd);
+		ReadBatch_cmd = NULL;
+	}
+	if(WriteBatch_cmd_Total) {
+		for(uint16_t i = 0; i < WriteBatch_cmd_Total; i++) furi_string_free(WriteBatch_cmd[i]);
+		WriteBatch_cmd_Total = 0;
+	}
+	if(WriteBatch_cmd) {
+		free(WriteBatch_cmd);
+		WriteBatch_cmd = NULL;
+	}
+	free_Log();
+}
+
+static bool select_settings_file() {
+	DialogsApp* dialogs = furi_record_open("dialogs");
+	bool result = false;
+	FuriString* path;
+	path = furi_string_alloc();
+	furi_string_set(path, SCAN_APP_PATH_FOLDER);
+
+	DialogsFileBrowserOptions browser_options;
+	dialog_file_browser_set_basic_options(&browser_options, ".txt", NULL);
+	browser_options.hide_ext = false;
+
+	bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options);
+
+	furi_record_close("dialogs");
+	if(ret) {
+		if(!file_stream_open(file_stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
+			FURI_LOG_D(TAG, "Cannot open file \"%s\"", furi_string_get_cstr(path));
+			file_stream_close(file_stream);
+		} else {
+			FURI_LOG_D(TAG, "Open file \"%s\"", furi_string_get_cstr(path));
+			strncpy(file_name, furi_string_get_cstr(path) + sizeof(SCAN_APP_PATH_FOLDER), sizeof(file_name));
+			result = true;
+		}
+	}
+	furi_string_free(path);
+	return result;
+}
+
+static void prepare_nrf24(void)
+{
+	if(NRF_INITED) return;
+	nrf24_write_reg(nrf24_HANDLE, REG_STATUS, 0x70); // clear interrupts
+	nrf24_write_reg(nrf24_HANDLE, REG_RF_CH, NRF_channel);
+	nrf24_write_reg(nrf24_HANDLE, REG_RF_SETUP, (NRF_rate == 0 ? 0b00100000 : NRF_rate == 1 ? 0 : 0b00001000) | 0b111); // +TX high power
+	nrf24_write_reg(nrf24_HANDLE, REG_CONFIG, 0x70 | ((NRF_CRC == 1 ? 0b1000 : NRF_CRC == 2 ? 0b1100 : 0))); // Mask all interrupts
+	nrf24_write_reg(nrf24_HANDLE, REG_SETUP_RETR, 0b00010111); // Automatic Retransmission, 0.5ms, 7 times
+	nrf24_write_reg(nrf24_HANDLE, REG_EN_AA, 0x01); // Auto acknowledgement
+	nrf24_write_reg(nrf24_HANDLE, REG_FEATURE, NRF_DPL ? 4+1 : 1); // Enables the W_TX_PAYLOAD_NOACK command, Disable Payload with ACK, set Dynamic Payload
+	nrf24_write_reg(nrf24_HANDLE, REG_DYNPD, NRF_DPL ? 0x3F : 0); // Enable dynamic payload reg
+	nrf24_write_reg(nrf24_HANDLE, RX_PW_P0, payload_size);
+	nrf24_set_maclen(nrf24_HANDLE, addr_len);
+	nrf24_set_mac(REG_RX_ADDR_P0, addr, addr_len);
+	uint8_t tmp[5] = { 0 };
+	nrf24_read_reg(nrf24_HANDLE, REG_RX_ADDR_P0, tmp, addr_len);
+	for(uint8_t i = 0; i < addr_len / 2; i++) {
+		uint8_t tb = tmp[i];
+		tmp[i] = tmp[addr_len - i - 1];
+		tmp[addr_len - i - 1] = tb;
+	}
+	NRF_ERROR = memcmp(addr, tmp, addr_len) != 0;
+	nrf24_set_mac(REG_TX_ADDR, addr, addr_len);
+	nrf24_write_reg(nrf24_HANDLE, REG_EN_RXADDR, 1);
+	nrf24_flush_rx(nrf24_HANDLE);
+	nrf24_flush_tx(nrf24_HANDLE);
+	//nrf24_set_idle(nrf24_HANDLE);
+	NRF_INITED = true;
+}
+
+// true - ok
+uint8_t nrf24_send_packet()
+{
+	if(furi_log_get_level() == FuriLogLevelDebug) {
+		char buf[64]; buf[0] = 0; add_to_str_hex_bytes(buf, payload, payload_size); FURI_LOG_D(TAG, "SEND: %s", buf);
+	}
+	// nrf24_write_reg(nrf24_HANDLE, REG_STATUS, RX_DR | TX_DS | MAX_RT);
+	// nrf24_flush_rx(nrf24_HANDLE);
+	if(nrf24_status(nrf24_HANDLE) & MAX_RT) {
+		nrf24_flush_tx(nrf24_HANDLE);
+		nrf24_write_reg(nrf24_HANDLE, REG_STATUS, MAX_RT);
+	}
+	NRF_last_packet_send_st = nrf24_txpacket(nrf24_HANDLE, payload, payload_size, true);
+	NRF_time = furi_get_tick();
+	FURI_LOG_D(TAG, "Send packet = %d", NRF_last_packet_send_st);
+	if(NRF_last_packet_send_st) { 
+		if((rw_type == rwt_read_cmd || rw_type == rwt_read_batch) && send_status == sst_sending) { // Read
+			nrf24_set_rx_mode(nrf24_HANDLE);
+			send_status = sst_receiving; // receiving
+			FURI_LOG_D(TAG, "Receiving...");
+		}
+
+	} else notification_message(APP->notification, &sequence_blink_red_100);
+	return NRF_last_packet_send_st;
+}
+
+// true - new packet
+bool nrf24_read_newpacket() {
+	bool found = false;
+	uint8_t packetsize;
+	uint8_t st = nrf24_rxpacket(nrf24_HANDLE, payload_receive, &packetsize, NRF_DPL ? 0 : payload_size);
+	if(st & RX_DR) {
+		NRF_time = furi_get_tick();
+		if(furi_log_get_level() == FuriLogLevelDebug) {
+			char buf[64]; buf[0] = 0; add_to_str_hex_bytes(buf, payload_receive, packetsize); FURI_LOG_D(TAG, "READ: %s", buf);
+		}
+		if(Log_Total) {
+			FuriString *str = Log[Log_Total - 1];
+			uint8_t size = 1;
+			char *p = strchr((char*)furi_string_get_cstr(str), '*');
+			if(p) {
+				p++;
+				if(*p == '=') size = 0; // string
+				else {
+					size = *p - '0';
+					if(size > 4) size = 0;
+				}
+			}
+			int32_t var;
+			if(size <= 1) var = *payload_receive;
+			else if(size == 2) var = *(int16_t*)payload_receive;
+			else if(size == 3) var = (*(uint32_t*)payload_receive) & 0xFFFFFF;
+			else var = *(int32_t*)payload_receive;
+			//FURI_LOG_D(TAG, "VAR(%d): %ld", size, var);
+			if(size == 0) furi_string_cat_printf(str, "%c", (char)var); 
+			else {
+				if(var >= 0 && var <= 9) furi_string_cat_printf(str, "%ld", var);
+				else {
+					char hex[9];
+					snprintf(hex, sizeof(hex), "%lX", var);
+					furi_string_cat_printf(str, "%ld (%s)", var, hex + (var < 0 ? 8 - size * 2 : 0));
+				}
+			}
+			if(cmd_array) {
+				if(--cmd_array_cnt) {
+					furi_string_cat_str(str, ",");
+					payload[cmd_array_idx]++;
+					furi_delay_ms(delay_between_pkt);
+					send_status = sst_sending;
+					if(!nrf24_send_packet()) send_status = sst_error;
+				} else send_status = sst_ok;
+			} else {
+				if(size == 0) { // string, until '\0'
+					if(var == 0) send_status = sst_ok;
+				} else send_status = sst_ok;
+			}
+		}
+		//notification_message(APP->notification, &sequence_blink_white_100);
+		found = true;
+	}
+	return found;
+}
+
+// Search in constatnt pull (Const1=n; Const2=n;...)
+// -32768 - not found
+int32_t subs_constant(char *p, uint8_t len)
+{
+	char *c = (char*)furi_string_get_cstr(Constants);
+	while((c = strchr(c, *p))) {
+		if(strncmp(c, p, len) != 0) {
+			c++;
+			continue;
+		}
+		if(c == (char*)furi_string_get_cstr(Constants) || *(c-1) == ';' ||  *(c-1) <= ' ') {
+			c += len;
+			if(*c == '=') {
+				c++;
+				return str_to_int(c);
+			}
+		} else c += len;
+	}
+	return VAR_EMPTY;
+}
+
+// fill payload with default = p
+// if var_n = VAR_EMPTY - skip filling var_*
+bool fill_payload(char *p, uint8_t *idx_i, int32_t var_n)
+{
+	if(idx_i) *idx_i = 255;
+	uint8_t fld = 0; // field #
+	uint8_t idx = 0; // byte index
+	do {
+		int32_t b = 0;
+		char *end = strchr(p, ',');
+		if(*p >= '0' && *p <= '9') { // Number found
+			b = str_to_int(p);
+		} else if(*p == 'i' && *(p + 1) == ':') { // 'i:' array index
+			b = str_to_int(p + 2);
+		 	if(idx_i) *idx_i = idx;
+		} else if(*p == 'n' && *(p + 1) < '0') { // var_n
+			if(var_n != VAR_EMPTY) b = var_n;
+		} else if(*p >= 'A') { // constant found
+			b = subs_constant(p, end ? (uint8_t)(end - p) : strlen(p));
+			if(b == VAR_EMPTY) {
+				ERR = 1;
+				strcpy(ERR_STR, "No ");
+				strcat(ERR_STR, p);
+				FURI_LOG_D(TAG, "Constant not found: %s", p);
+				return false;
+			}
+		} else if(end == p) {
+			idx += payload_struct[fld];
+		} else {
+			ERR = 2;
+			strcpy(ERR_STR, "char: ");
+			uint8_t l = strlen(ERR_STR);
+			ERR_STR[l] = *p;
+			ERR_STR[l+1] = '\0';
+			FURI_LOG_D(TAG, "Wrong format char(%c)", *p);
+			return false;
+		}
+		if(end != p) {
+			payload[idx++] = b;
+			if(payload_struct[fld] > 1) payload[idx++] = b >> 8;
+			if(payload_struct[fld] > 2) payload[idx++] = b >> 16;
+			if(payload_struct[fld] > 3) payload[idx++] = b >> 24;
+		}
+		if(++fld == payload_fields || idx >= sizeof(payload) || end == NULL) break;
+		p = end + 1;
+	} while(1);
+	return true;
+}
+
+// Cmd: "name=payload"
+bool Run_Read_cmd(FuriString *cmd)
+{
+	char *p = (char*)furi_string_get_cstr(cmd);
+	p = strchr(p, '=');
+	if(p == NULL) return false;
+	if(Log == NULL) Log = malloc(sizeof(Log));
+	else Log = realloc(Log, sizeof(Log) * (Log_Total + 1));
+	if(Log == NULL) {
+		ERR = 3;
+		strcpy(ERR_STR, "Memory low");
+		FURI_LOG_D(TAG, ERR_STR);
+		return false;
+	}
+	FuriString *fs = furi_string_alloc();
+	furi_string_set_strn(fs, (char*)furi_string_get_cstr(cmd), p - (char*)furi_string_get_cstr(cmd));
+	furi_string_cat_str(fs, ": ");
+	Log[Log_Total++] = fs;
+	p++;
+	memset(payload, 0, sizeof(payload));
+	if(ReadDefault && !fill_payload((char*)furi_string_get_cstr(ReadDefault), NULL, VAR_EMPTY)) return false;
+	if(!fill_payload(p, &cmd_array_idx, VAR_EMPTY)) return false;
+	memset(payload_receive, 0, sizeof(payload_receive));
+	cmd_array = false;
+	if(*(p - 2) == ']' && cmd_array_idx != 255) { // array
+		p = strchr(furi_string_get_cstr(cmd), '[');
+		if(p) {
+			cmd_array_cnt = str_to_int(p + 1);
+			if(cmd_array_cnt > 1) cmd_array = true; // array
+		}
+	}
+	NRF_repeat = 0;
+	prepare_nrf24();
+	if(NRF_ERROR) return false;
+	what_doing = 2;
+	send_status = sst_sending; // Read - sending
+	nrf24_send_packet();
+	return true;
+}
+
+// run commands one by one, true - command running
+bool Run_ReadBatch_cmd(FuriString *cmd)
+{
+	char *p;
+	if(cmd) {
+		p = strchr((char*)furi_string_get_cstr(cmd), ':');
+		if(p == NULL) return false;
+		p += 2;
+		ReadBatch_cmd_curr = NULL;
+		free_Log();
+	} else {
+		if(ReadBatch_cmd_curr) p = ReadBatch_cmd_curr; else return false;
+	}
+	char *end = strchr(p, ';');
+	uint8_t len;
+	if(end) len = end - p;
+	else {
+		str_rtrim(p);
+		len = strlen(p);
+	}
+	for(uint16_t i = 0; i < Read_cmd_Total; i++) {
+		FuriString *fs = Read_cmd[i];
+		if(strncmp((char*)furi_string_get_cstr(fs), p, len) == 0) {
+			char c = *((char*)furi_string_get_cstr(fs) + len);
+			if(c != '=' && c != '*' && c != '[') continue;
+			if(end) ReadBatch_cmd_curr = end + 1; else ReadBatch_cmd_curr = (char*)0xFFFFFFFF;
+			Run_Read_cmd(fs);
+			return true;
+		}
+	}
+	ERR = 4;
+	strcpy(ERR_STR, "Not found");
+	FURI_LOG_D(TAG, "CMD %s: %s", ERR_STR, p == NULL ? "" : p);
+	return false;
+}
+
+bool Run_WriteBatch_cmd(FuriString *cmd)
+{
+	char *p;
+	send_status = sst_none;
+	if(cmd) {
+		p = strchr((char*)furi_string_get_cstr(cmd), ':');
+		if(p == NULL) return false;
+		p += 2;
+		WriteBatch_cmd_curr = NULL;
+		free_Log();
+	} else {
+		if(WriteBatch_cmd_curr) p = WriteBatch_cmd_curr; else return false;
+	}
+	char *end = strchr(p, ';');
+	uint8_t len;
+	if(end) {
+		len = end - p;
+		WriteBatch_cmd_curr = end + 1;
+	} else {
+		str_rtrim(p);
+		len = strlen(p);
+		WriteBatch_cmd_curr = (char*)0xFFFFFFFF;
+	}
+	FuriString *fs = furi_string_alloc();
+	if(Log == NULL) Log = malloc(sizeof(Log));
+	else Log = realloc(Log, sizeof(Log) * (Log_Total + 1));
+	if(Log == NULL) {
+		ERR = 3;
+		strcpy(ERR_STR, "Memory low");
+		FURI_LOG_D(TAG, ERR_STR);
+		return false;
+	}
+	furi_string_set_strn(fs, p, len);
+	Log[Log_Total++] = fs;
+	char *arr = NULL;
+	cmd_array = false;
+	int32_t new = 0;
+	for(uint8_t i = 0; i < len; i++) {
+		if(p[i] == '=') {
+			len = i;
+			char *p2 = p + i + 1;
+			if(*p2 == '{') {
+				arr = ++p2; // array
+				cmd_array = true;
+			}
+			new = str_to_int(p2);
+			break;
+		}
+	}
+	FURI_LOG_D(TAG, "WriteBatch: =%d, (%d)%s", (int)new, len, p);
+	FuriString* str = furi_string_alloc();
+	stream_rewind(file_stream);
+	while(stream_read_line(file_stream, str)) {
+		char *w = (char*)furi_string_get_cstr(str);
+		if(strncmp(w, SettingsFld_Write, sizeof(SettingsFld_Write)-1) != 0) continue;
+		w +=  sizeof(SettingsFld_Write);
+		char* delim_col = strchr(w, '=');
+		if(delim_col == NULL || len != delim_col - w) continue;
+		if(strncmp(p, w, len) != 0) continue;
+		delim_col++;
+		str_rtrim(delim_col);
+		NRF_repeat = 0;
+		prepare_nrf24();
+		if(NRF_ERROR) return false;
+		cmd_array_cnt = 255;
+		do {
+			memset(payload, 0, sizeof(payload));
+			if(WriteDefault && !fill_payload((char*)furi_string_get_cstr(WriteDefault), NULL, new)) return false;
+			if(!fill_payload(delim_col, &cmd_array_idx, VAR_EMPTY)) return false;
+			if(cmd_array && cmd_array_idx != 255) {
+ 				if(cmd_array_cnt != 255) payload[cmd_array_idx] = cmd_array_cnt;
+			} else cmd_array = false;
+			send_status = sst_sending; // Read - sending
+			uint8_t i = 0;
+			for(; i < NRF_repeat; i++) {
+				if(nrf24_send_packet()) break;
+				furi_delay_ms(delay_between_pkt);
+			}
+			if(i < NRF_repeat) {
+				if(cmd_array) { // array
+					for(; arr != NULL;) {
+						if(*arr == ',') break;
+						if(*arr == '}' || *arr < ' ') arr = NULL; else arr++;
+					}
+					if(arr == NULL) {
+						send_status = sst_ok; 
+						break;
+					}
+					arr++;
+					new = str_to_int(arr);
+					cmd_array_cnt = payload[cmd_array_idx] + 1;
+					continue;
+				} else send_status = sst_ok; 
+			}
+			break;
+		} while(1);
+		if(send_status != sst_ok) { 
+			send_status = sst_error;
+			furi_string_cat_str(fs, "!");
+		}
+		return send_status == sst_ok;
+	}
+	ERR = 7;
+	strcpy(ERR_STR, "NOT FOUND!");
+	send_status = sst_error;
+	return false;
+}
+
+// Return 0 - success, otherwise an error
+static uint8_t load_settings_file() {
+	uint8_t err = 0;
+	FURI_LOG_D(TAG, "Loading settings file");
+	FuriString* str = furi_string_alloc();
+	free_store();
+	NRF_INITED = false;
+	while(stream_read_line(file_stream, str)) {
+		char *p = (char*)furi_string_get_cstr(str);
+		if(*p <= ' ') continue;
+		//char* delim_eq = strchr(p, '=');
+		char* delim_col = strchr(p, ':');
+		if(delim_col == NULL) { // Constant found - no ':'
+			if(Constants == NULL) {
+				Constants = furi_string_alloc_set(str);
+			} else furi_string_cat(Constants, str);
+		} else {
+			str_rtrim(p);
+			if(strncmp(p, SettingsFld_Rate, sizeof(SettingsFld_Rate)-1) == 0) {
+				NRF_rate = atoi(p + sizeof(SettingsFld_Rate));
+			} else if(strncmp(p, SettingsFld_Info, sizeof(SettingsFld_Info)-1) == 0) {
+				strncpy(Info, p + sizeof(SettingsFld_Info), sizeof(Info)-1);
+			} else if(strncmp(p, SettingsFld_Ch, sizeof(SettingsFld_Ch)-1) == 0) {
+				NRF_channel = atoi(p + sizeof(SettingsFld_Ch));
+			} else if(strncmp(p, SettingsFld_Address, sizeof(SettingsFld_Address)-1) == 0) {
+				p += sizeof(SettingsFld_Address);
+				addr_len = ConvertHexToArray(p, addr, 5);
+			} else if(strncmp(p, SettingsFld_CRC, sizeof(SettingsFld_CRC)-1) == 0) {
+				NRF_CRC = atoi(p + sizeof(SettingsFld_CRC));
+			} else if(strncmp(p, SettingsFld_DPL, sizeof(SettingsFld_DPL)-1) == 0) {
+				NRF_DPL = atoi(p + sizeof(SettingsFld_DPL));
+			} else if(strncmp(p, SettingsFld_Delay, sizeof(SettingsFld_Delay)-1) == 0) {
+				delay_between_pkt = atoi(p + sizeof(SettingsFld_Delay));
+			} else if(strncmp(p, SettingsFld_Payload, sizeof(SettingsFld_Payload)-1) == 0) {
+				p += sizeof(SettingsFld_Payload);
+				payload_fields = 0;
+				payload_size = 0;
+				do {
+					uint8_t b = atoi(p);
+					if(b < 1 || b > 4) {
+						FURI_LOG_D(TAG, "Wrong payload format (%d)", b);
+						err = 3;
+						break;
+					}
+					payload_struct[payload_fields++] = b;
+					payload_size += b;
+					if(payload_fields == sizeof(payload_struct) - 1) break;
+					if((p = strchr(p, ',')) == NULL) break;
+					p++;
+				} while(1);
+				FURI_LOG_D(TAG, "Payload fields %d: %d,%d,%d", payload_fields, payload_struct[0], payload_struct[1], payload_struct[2]);
+			} else if(strncmp(p, SettingsFld_ReadDefault, sizeof(SettingsFld_ReadDefault)-1) == 0) {
+				ReadDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_ReadDefault));
+			} else if(strncmp(p, SettingsFld_WriteDefault, sizeof(SettingsFld_WriteDefault)-1) == 0) {
+				WriteDefault = furi_string_alloc_set_str(p + sizeof(SettingsFld_WriteDefault));
+			} else if(strncmp(p, SettingsFld_Read, sizeof(SettingsFld_Read)-1) == 0) {
+				p += sizeof(SettingsFld_Read);
+				if(Read_cmd == NULL) Read_cmd = malloc(sizeof(Read_cmd));
+				else {
+					Read_cmd = realloc(Read_cmd, sizeof(Read_cmd) * (Read_cmd_Total + 1));
+				}
+				if(Read_cmd == NULL) {
+					FURI_LOG_D(TAG, "Memory low, err 4");
+					err = 4;
+					break;
+				}
+				Read_cmd[Read_cmd_Total++] = furi_string_alloc_set_str(p);
+			} else if(strncmp(p, SettingsFld_ReadBatch, sizeof(SettingsFld_ReadBatch)-1) == 0) {
+				p += sizeof(SettingsFld_ReadBatch);
+				if(ReadBatch_cmd == NULL) ReadBatch_cmd = malloc(sizeof(ReadBatch_cmd));
+				else {
+					ReadBatch_cmd = realloc(ReadBatch_cmd, sizeof(ReadBatch_cmd) * (ReadBatch_cmd_Total + 1));
+				}
+				if(ReadBatch_cmd == NULL) {
+					FURI_LOG_D(TAG, "Memory low, err 5");
+					err = 5;
+					break;
+				}
+				ReadBatch_cmd[ReadBatch_cmd_Total++] = furi_string_alloc_set_str(p);
+			} else if(strncmp(p, SettingsFld_WriteBatch, sizeof(SettingsFld_WriteBatch)-1) == 0) {
+				p += sizeof(SettingsFld_WriteBatch);
+				if(WriteBatch_cmd == NULL) WriteBatch_cmd = malloc(sizeof(WriteBatch_cmd));
+				else {
+					WriteBatch_cmd = realloc(WriteBatch_cmd, sizeof(WriteBatch_cmd) * (WriteBatch_cmd_Total + 1));
+				}
+				if(WriteBatch_cmd == NULL) {
+					FURI_LOG_D(TAG, "Memory low, err 6");
+					err = 6;
+					break;
+				}
+				WriteBatch_cmd[WriteBatch_cmd_Total++] = furi_string_alloc_set_str(p);
+			}
+		}
+	}
+	furi_string_free(str);
+	return err;
+}
+
+static void save_batch(void)
+{
+// to do...
+}
+
+static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
+	furi_assert(event_queue);
+	PluginEvent event = {.type = EventTypeKey, .input = *input_event};
+	furi_message_queue_put(event_queue, &event, FuriWaitForever);
+}
+
+static void render_callback(Canvas* const canvas, void* ctx) {
+	const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
+	if(plugin_state == NULL) return;
+	//canvas_draw_frame(canvas, 0, 0, 128, 64); // border around the edge of the screen
+	if(what_doing == 0) {
+		canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
+		snprintf(screen_buf, sizeof(screen_buf), "Open: %s", file_name);
+		canvas_draw_str(canvas, 10, 10, screen_buf);
+		if(addr_len) {
+			canvas_draw_str(canvas, 10, 22, Info);
+			strcpy(screen_buf, "Address: ");
+			add_to_str_hex_bytes(screen_buf, addr, addr_len);
+			canvas_draw_str(canvas, 10, 32, screen_buf);
+			snprintf(screen_buf, sizeof(screen_buf), "Ch: %d, Rate: %d", NRF_channel, NRF_rate);
+			canvas_draw_str(canvas, 10, 42, screen_buf);
+			snprintf(screen_buf, sizeof(screen_buf), "RB: %d, R: %d, WB: %d", ReadBatch_cmd_Total, Read_cmd_Total, WriteBatch_cmd_Total);
+			canvas_draw_str(canvas, 10, 52, screen_buf);
+			//canvas_draw_str(canvas, 10, 60, screen_buf);
+		} else {
+			snprintf(screen_buf, sizeof(screen_buf), "ver. %s (vad7)", VERSION);
+			canvas_draw_str(canvas, 10, 60, screen_buf);
+		}
+		canvas_draw_str(canvas, 0, 10, ">");
+	} else if(what_doing == 1){
+		canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
+		char delim;
+		uint16_t max_i;
+		FuriString ***fsa;
+		if(rw_type == rwt_read_batch) {
+			canvas_draw_str(canvas, 0, 7, "Read Batch:");
+			max_i = ReadBatch_cmd_Total;
+			fsa = &ReadBatch_cmd;
+			delim = ':';
+		} else if(rw_type == rwt_read_cmd) {
+			canvas_draw_str(canvas, 0, 7, "Read Command:");
+			max_i = Read_cmd_Total;
+			fsa = &Read_cmd;
+			delim = '=';
+		} else { // rwt_write_batch
+			if(ask_question) {
+				snprintf(screen_buf, sizeof(screen_buf), "RUN WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
+			} else {
+				strcpy(screen_buf, "Write Batch:");
+			}
+			canvas_draw_str(canvas, 0, 7, screen_buf);
+			max_i = WriteBatch_cmd_Total;
+			fsa = &WriteBatch_cmd;
+			delim = ':';
+		}
+		if(NRF_ERROR) canvas_draw_str(canvas, 70, 7, "nRF24 ERROR!");
+		uint16_t page = view_cmd[rw_type] & ~7;
+		for(uint8_t i = 0; i < 8 && page + i < max_i; i++) {
+			uint16_t y = 14 + i * 7;
+			char *p = (char*) furi_string_get_cstr((*fsa)[page + i]);
+			char *end = strchr(p, delim);
+			if(end) {
+				if(*(end - 2) == '*') end -= 2; // *n - var size set
+				uint16_t len = MIN((end - p), 30);
+				strncpy(screen_buf, p, len);
+				screen_buf[len] = '\0';
+				canvas_draw_str(canvas, 5, y, screen_buf);
+			}
+			if((view_cmd[rw_type] & 7) == i) {
+				canvas_draw_str(canvas, 0, y, ">");
+				canvas_draw_str(canvas, -1, y, ">");
+			}
+		}
+	} else { // what_doing == 2
+		if(rw_type == rwt_read_cmd) {	// Read command
+			canvas_set_font(canvas, FontSecondary); // 8x10 font, 6 lines
+			strcpy(screen_buf, "Read cmd:");
+			if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
+			else if(ERR) {
+				snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR);
+				canvas_draw_str(canvas, 0, 60, ERR_STR);
+			} else if(send_status == sst_sending) strcat(screen_buf, " sending");
+			else if(send_status == sst_receiving) strcat(screen_buf, " receiving");
+			else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
+			else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
+			else if(send_status == sst_ok) strcat(screen_buf, " Ok");
+			canvas_draw_str(canvas, 0, 10, screen_buf);
+			if(Log_Total) {
+				char *p = (char*)furi_string_get_cstr(Log[Log_Total - 1]);
+				strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
+				canvas_draw_str(canvas, 0, 15 + 10, screen_buf);
+			}
+
+		} else { // if(rw_type == rwt_read_batch || rw_type == rwt_write_batch)
+			canvas_set_font(canvas, FontBatteryPercent); // 5x7 font, 9 lines, 25 cols
+			if(ask_question && rw_type == rwt_read_batch) {
+				snprintf(screen_buf, sizeof(screen_buf), "SAVE AS WRITE BATCH? %s", ask_question_answer ? "YES" : "NO");
+			} else {
+				strcpy(screen_buf, rw_type == rwt_read_batch ? "Read Batch:" : "Write Batch:");
+				if(NRF_ERROR) strcat(screen_buf, "nRF24 ERROR!");
+				else if(ERR) snprintf(screen_buf + strlen(screen_buf), 16, " Error %d", ERR); 
+				else if(send_status == sst_error) strcat(screen_buf, " NO ACK!");
+				else if(send_status == sst_timeout) strcat(screen_buf, " TIMEOUT!");
+				else if(send_status == sst_ok && ((rw_type == rwt_read_batch && (uint32_t)ReadBatch_cmd_curr == 0xFFFFFFFF) 
+					|| (rw_type == rwt_write_batch && (uint32_t)WriteBatch_cmd_curr == 0xFFFFFFFF)))
+					strcat(screen_buf, " Ok");
+				else strcat(screen_buf, " working");
+			}
+			canvas_draw_str(canvas, 0, 7, screen_buf);
+			if(Log_Total) {
+				uint16_t page = view_Batch & ~7;
+				for(uint8_t i = 0; i < 8 && page + i < Log_Total; i++) {
+					uint16_t y = 14 + i * 7;
+					screen_buf[sizeof(screen_buf) - 1] = '\0';
+					char *p = (char*)furi_string_get_cstr(Log[page + i]);
+					strncpy(screen_buf, p + MIN(view_x, strlen(p)), 30);
+					if(ERR && page + i == Log_Total - 1) strcat(screen_buf, ERR_STR);
+					canvas_draw_str(canvas, 5, y, screen_buf);
+					if((view_Batch & 7) == i) {
+						canvas_draw_str(canvas, 0, y, ">");
+						canvas_draw_str(canvas, -1, y, ">");
+					}
+				}
+			}
+		}
+	}
+	release_mutex((ValueMutex*)ctx, plugin_state);
+} 
+
+void work_timer_callback(void* ctx)
+{
+	UNUSED(ctx);
+	if(what_doing == 2) {
+		if(rw_type == rwt_write_batch) {
+			if(send_status == sst_ok) {
+				if((uint32_t)WriteBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
+					Run_WriteBatch_cmd(NULL);
+				}
+			}
+		} else if(send_status == sst_sending) { // sending
+			if(!NRF_last_packet_send_st) { // No ACK on last attempt
+				if(furi_get_tick() - NRF_time >= delay_between_pkt) {
+					if(++NRF_repeat < NRF_REPEAT_ATTEMPTS) nrf24_send_packet(); else send_status = sst_error; // error
+				}
+			}
+		} else if(send_status == sst_receiving) { // receiving
+			for(uint8_t i = 0; i < 10; i++) {
+				bool new = nrf24_read_newpacket();
+				if(new) {
+					if(send_status != sst_receiving) {
+						NRF_repeat = 0;
+						break;
+					}
+				} else if(furi_get_tick() - NRF_time >= NRF_READ_TIMEOUT) {
+					if(++NRF_repeat < NRF_REPEAT_ATTEMPTS) nrf24_send_packet();
+					else {
+						FURI_LOG_D(TAG, "TIMEOUT: %lu", furi_get_tick() - NRF_time);
+						send_status = sst_timeout;
+						break;
+					}
+				}
+			}
+		} else if(send_status == sst_ok) {
+			if(rw_type == rwt_read_batch) {
+				if((uint32_t)ReadBatch_cmd_curr != 0xFFFFFFFF && ERR == 0 && furi_get_tick() - NRF_time >= delay_between_pkt) {
+					Run_ReadBatch_cmd(NULL);
+				}
+			}
+		}
+	}
+}
+
+int32_t nrf24batch_app(void* p) {
+	UNUSED(p);
+	APP = malloc(sizeof(nRF24Batch));
+	APP->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
+	APP->plugin_state = malloc(sizeof(PluginState));
+	ValueMutex state_mutex;
+	if(!init_mutex(&state_mutex, APP->plugin_state, sizeof(PluginState))) {
+		furi_message_queue_free(APP->event_queue);
+		FURI_LOG_E(TAG, "cannot create mutex");
+		free(APP->plugin_state);
+		return 255;
+	}
+	nrf24_init();
+
+	// Set system callbacks
+	APP->view_port = view_port_alloc();
+	view_port_draw_callback_set(APP->view_port, render_callback, &state_mutex);
+	view_port_input_callback_set(APP->view_port, input_callback, APP->event_queue);
+
+	// Open GUI and register view_port
+	APP->gui = furi_record_open(RECORD_GUI);
+	gui_add_view_port(APP->gui, APP->view_port, GuiLayerFullscreen);
+	APP->notification = furi_record_open(RECORD_NOTIFICATION);
+	APP->storage = furi_record_open(RECORD_STORAGE);
+	storage_common_mkdir(APP->storage, SCAN_APP_PATH_FOLDER);
+	file_stream = file_stream_alloc(APP->storage);
+	FuriTimer *work_timer = furi_timer_alloc(work_timer_callback, FuriTimerTypePeriodic, NULL);
+	furi_timer_start(work_timer, WORK_PERIOD);
+
+	PluginEvent event;
+	for(bool processing = true; processing;) {
+		FuriStatus event_status = furi_message_queue_get(APP->event_queue, &event, 200);
+		PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
+
+		if(event_status == FuriStatusOk) {
+			// press events
+			if(event.type == EventTypeKey) {
+				switch(event.input.key) {
+				case InputKeyUp:
+					if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
+						if(!ask_question) {
+							if(what_doing == 0) {
+							} else if(what_doing == 1) {
+								if(view_cmd[rw_type]) view_cmd[rw_type]--;
+							} else if(what_doing == 2) {
+								if(view_Batch) view_Batch--;
+							}
+						}
+					}
+					break;
+				case InputKeyDown:
+					if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
+						if(!ask_question) {
+							if(what_doing == 0) {
+								what_doing = 1;
+							} else if(what_doing == 1) {
+								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]++;
+							} else if(what_doing == 2) {
+								if(view_Batch < Log_Total - 1) view_Batch++;
+							}
+						}
+					}
+					break;
+				case InputKeyLeft:
+					if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
+						if(ask_question) {
+							ask_question_answer ^= 1;
+						} else if(what_doing == 0) {
+						} else if(what_doing == 1) {
+							if(--rw_type > rwt_write_batch) rw_type = rwt_write_batch;
+						} else if(what_doing == 2) {
+							if(view_x) view_x--;
+						}
+					}
+					break;
+				case InputKeyRight:
+					if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
+						if(ask_question) {
+							ask_question_answer ^= 1;
+						} else if(what_doing == 0) {
+							what_doing = 1;
+						} else if(what_doing == 1) {
+							if(++rw_type > rwt_write_batch) rw_type = rwt_read_batch;
+						} else if(what_doing == 2) {
+							view_x++;
+						}
+					}
+					break;
+				case InputKeyOk:
+					if(event.input.type == InputTypeShort) {
+						if(ask_question) {
+							ask_question = 0;
+							if(what_doing == 2 && rw_type == rwt_read_batch) {
+								if(ask_question_answer) save_batch();
+							} else if(what_doing == 1 && rw_type == rwt_write_batch) {
+								if(ask_question_answer) {
+									ERR = 0;
+									Run_WriteBatch_cmd(WriteBatch_cmd[view_cmd[rwt_write_batch]]);
+									view_Batch = 0;
+									what_doing = 2;
+								}
+							}
+						} else if(what_doing == 0) {
+							file_stream_close(file_stream);
+							if(select_settings_file()) {
+								uint8_t err = load_settings_file();
+								if(err) snprintf(file_name, sizeof(file_name), "LOAD ERROR #%d", err);
+							}
+						} else if(what_doing == 1) {
+							if(rw_type == rwt_read_batch) {
+								if(ReadBatch_cmd_Total) {
+									ERR = 0;
+									Run_ReadBatch_cmd(ReadBatch_cmd[view_cmd[rwt_read_batch]]);
+									view_Batch = 0;
+									what_doing = 2;
+								}
+							} else if(rw_type == rwt_read_cmd) {
+								if(Read_cmd_Total) {
+									ERR = 0;
+									free_Log();
+									Run_Read_cmd(Read_cmd[view_cmd[rwt_read_cmd]]);
+									what_doing = 2;
+								}
+							} else if(rw_type == rwt_write_batch) {
+								if(WriteBatch_cmd_Total) {
+									ask_question = 1;
+									ask_question_answer = 0;
+								}
+							}
+						} else if(what_doing == 2) {		
+							if(rw_type == rwt_read_batch && Log_Total) {
+								ask_question = 1;
+								ask_question_answer = 0;
+							}
+						}
+					// } else if(event.input.type == InputTypeLong) {
+					// 	if(what_doing == 0) {
+					// 	} else if(what_doing == 1 || what_doing == 2) {
+					// 	}
+					}
+					break;
+				case InputKeyBack:
+					if(event.input.type == InputTypeLong) processing = false; 
+					else if(event.input.type == InputTypeShort) {
+						if(what_doing) what_doing--;
+						if(what_doing == 0) rw_type = rwt_read_batch;
+						if(what_doing <= 1) view_x = 0;
+						ERR = 0;
+						send_status = sst_none;
+					}
+					break;
+				default:
+					break;
+				}
+			}
+		}
+
+		view_port_update(APP->view_port);
+		release_mutex(&state_mutex, plugin_state);
+	}
+	nrf24_set_idle(nrf24_HANDLE);
+	nrf24_deinit();
+
+	view_port_enabled_set(APP->view_port, false);
+	gui_remove_view_port(APP->gui, APP->view_port);
+	furi_record_close(RECORD_GUI);
+	furi_record_close(RECORD_NOTIFICATION);
+	furi_record_close(RECORD_STORAGE);
+	if(file_stream) {
+		file_stream_close(file_stream);
+		stream_free(file_stream);
+	}
+	view_port_free(APP->view_port);
+	furi_message_queue_free(APP->event_queue);
+	free_store();
+	furi_timer_stop(work_timer);
+	furi_timer_free(work_timer);
+	free(APP->plugin_state);
+	free(APP);
+	return 0;
+}

+ 34 - 0
nrf24batch.h

@@ -0,0 +1,34 @@
+#pragma once
+
+#include <gui/gui.h>
+#include <gui/view.h>
+#include <gui/view_dispatcher.h>
+#include <gui/modules/submenu.h>
+#include <gui/modules/dialog_ex.h>
+#include <toolbox/stream/file_stream.h>
+#include <notification/notification_messages.h>
+
+typedef enum {
+	EventTypeTick,
+	EventTypeKey,
+} EventType;
+
+typedef struct {
+	EventType type;
+	InputEvent input;
+} PluginEvent;
+
+typedef struct {
+	int x;
+	int y;
+} PluginState;
+
+typedef struct {
+    Gui* gui;
+    FuriMessageQueue* event_queue;
+    PluginState* plugin_state;
+    ViewPort* view_port;
+	Storage* storage;
+	NotificationApp* notification;
+} nRF24Batch;
+

BIN
nrf24batch_10px.png