From 0d0935c99524343e40fceb2d5ce727d924e16e89 Mon Sep 17 00:00:00 2001 From: Morgan Allen Date: Sun, 29 Jul 2018 14:00:59 -0700 Subject: [PATCH] added set coding rate with tests --- include/esp32-lora.h | 1 + main/esp32-lora.c | 9 ++++++ test/lora32/main/main.c | 12 +++++++- test/test.js | 64 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/include/esp32-lora.h b/include/esp32-lora.h index ea8b415..7602b0e 100644 --- a/include/esp32-lora.h +++ b/include/esp32-lora.h @@ -105,5 +105,6 @@ void lora32_send(lora32_cfg_t *config, uint8_t *data, uint8_t len); void lora32_set_spreadfactor(lora32_cfg_t *lora, uint8_t factor); void lora32_dump_regs(lora32_cfg_t *lora); void lora32_enable_continous_rx(lora32_cfg_t *lora); +void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t d); #endif // _LORA32_H__ diff --git a/main/esp32-lora.c b/main/esp32-lora.c index 12928af..9620475 100644 --- a/main/esp32-lora.c +++ b/main/esp32-lora.c @@ -299,6 +299,15 @@ void lora32_enable_continous_rx(lora32_cfg_t *lora) { lora32_write_reg(lora, REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); } +void lora32_set_coding_rate(lora32_cfg_t *lora, uint8_t d) { + if(d < 5) d = 5; + else if(d > 8) d = 8; + + uint8_t cr = d - 4; + + lora32_write_reg(lora, REG_MODEM_CONFIG_1, (lora32_read_reg(lora, REG_MODEM_CONFIG_1) & 0xF1) | (cr << 1)); +} + void lora32_handle_dio0(void *arg) { lora32_cfg_t *lora = (lora32_cfg_t*)arg; static uint8_t msg[MAX_PKT_LENGTH]; diff --git a/test/lora32/main/main.c b/test/lora32/main/main.c index 41de52f..7467b83 100644 --- a/test/lora32/main/main.c +++ b/test/lora32/main/main.c @@ -122,17 +122,26 @@ void set_mode(void *args) { void send(void *args) { ESP_LOGI(TAG, "sending: %s\n", (char*)args); + lora32_send(&lora, (uint8_t *)args, 12); }; void set_spreadfactor(void *args) { uint32_t sf = atoi(args); - ESP_LOGI(TAG, "spreadfactor: %d", sf); + printf("spreadfactor: %d\n", sf); lora32_set_spreadfactor(&lora, sf); } +void set_coderate(void *args) { + uint8_t cr = atoi(args); + + printf("coding rate: %d\n", cr); + + lora32_set_coding_rate(&lora, cr); +} + void app_main() { esp_err_t ret; @@ -158,6 +167,7 @@ void app_main() { ucmdr_install("send", send); ucmdr_install("dump", dump_mem); ucmdr_install("sf", set_spreadfactor); + ucmdr_install("cr", set_coderate); uart_config_t uart_config = { .baud_rate = 115200, diff --git a/test/test.js b/test/test.js index 90bdd1c..f33222c 100644 --- a/test/test.js +++ b/test/test.js @@ -185,21 +185,25 @@ async.series([ port.write(msg); }; - }, "spreadfactor"); + }, "set spreadfactor"); test(function(t) { t.plan(4); var i = 0; - async.doUntil(function(done) { + async.doWhilst(function(done) { var write = ports["dev" + (1 + i % 2)]; var read = ports["dev" + (1 + (i + 1) % 2)]; var buf = ""; var msg = `tick${i}`; - read.on("data", function onData(c) { + read.on("data", onData); + + write.write(`send${msg}\n`); + + function onData(c) { buf += c.toString(); if(buf.indexOf(msg) !== -1) { @@ -209,11 +213,57 @@ async.series([ done(null); } - }); - - write.write(`send${msg}\n`); + } }, function() { - return ++i > 5; + return i++ < 3; }) }, "exchange"); + + test(function(t) { + t.plan(2); + + var cr = 7; + var msg = `cr${cr}\n`; + + testSetCodingRate(ports.dev1); + testSetCodingRate(ports.dev2); + + function testSetCodingRate(port) { + var buf = ""; + + port.on("data", function onData(c) { + buf += c.toString(); + + if(buf.indexOf(`coding rate: ${cr}`) !== -1) { + t.pass("coding factor set"); + + port.removeListener("data", onData); + } + }); + + port.write(msg); + }; + }, "set coding rate"); + + test(function(t) { + t.plan(1); + + ports.dev1.on("data", function onData(chunk) { + //process.stdout.write(chunk); + + buf += chunk.toString(); + + if(buf.indexOf(`msg: ${message}`) !== -1) { + ports.dev1.removeListener("data", onData); + + t.pass("got round-trip message!"); + } + }); + + ports.dev2.on("data", function(chunk) { + //process.stdout.write(chunk); + }); + + ports.dev2.write(`send${message}\n`); + }, "round-trip message"); });