added set coding rate with tests

This commit is contained in:
Morgan Allen 2018-07-29 14:00:59 -07:00
parent 9e93b06c2a
commit 0d0935c995
4 changed files with 78 additions and 8 deletions

View file

@ -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__

View file

@ -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];

View file

@ -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,

View file

@ -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");
});