moved a lot of lorcomm specific code into own files

This commit is contained in:
Morgan 'ARR\!' Allen 2020-04-22 22:22:09 -07:00
parent 7abc26a27d
commit 31cacd63b3
3 changed files with 111 additions and 88 deletions

40
include/lorcomm.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef __LORCOMM_H_H
#define __LORCOMM_H_H
// disable memory alignment
#pragma pack(push, 1)
typedef struct {
uint8_t src[3];
uint8_t dst[3];
uint8_t type;
void *payload;
} lora_packet;
// this is wrong, why is working correctly? should be 7
#define LORA_PACKET_HEADER_SIZE (8)
typedef struct {
uint8_t length;
char *msg;
} lora_msg;
#define LORA_MSG_HEADER_SIZE (1)
typedef struct {
uint8_t origin[3];
uint8_t type; // same as packet type
void *payload;
} lora_routing;
#define LORA_ROUTING_HEADER_SIZE (4)
#pragma pack(pop)
enum lora_msg_type {
TEXT = 0,
ROUTING,
};
void send_message(lora32_cfg_t *lora, char *text);
#endif

47
main/lorcomm.c Normal file
View File

@ -0,0 +1,47 @@
#include <string.h>
#include "esp_log.h"
#include "esp32-lora.h"
#include "lorcomm.h"
#define TAG "lorcomm"
uint8_t mac[6] = {0};
void send_message(lora32_cfg_t *lora, char *text) {
uint8_t len = strlen(text) + 1;
lora_packet packet = { 0 };
lora_msg msg = { 0 };
memcpy(packet.src, &mac[3], 3);
memset(packet.dst, 0xFF, 3);
packet.type = TEXT;
packet.payload = malloc(LORA_PACKET_HEADER_SIZE + len);
msg.length = len;
msg.msg = malloc(LORA_MSG_HEADER_SIZE + len);
memcpy(msg.msg, text, len);
uint8_t buf_len = LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE + len;
uint8_t *send_buf = malloc(buf_len);
ESP_LOGI(TAG, "packet length: %d", buf_len);
// set src, dst, length
memcpy(send_buf, (uint8_t*)&packet, LORA_PACKET_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE), (void*)&msg, LORA_MSG_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE), (void*)msg.msg, len);
ESP_LOGI(TAG, "src: [%02X:%02X:%02X]", packet.src[0], packet.src[1], packet.src[2]);
ESP_LOGI(TAG, "dst: [%02X:%02X:%02X]", packet.dst[0], packet.dst[1], packet.dst[2]);
lora32_send(lora, send_buf, buf_len);
free(packet.payload);
packet.payload = NULL;
free(msg.msg);
msg.msg = NULL;
free(send_buf);
}

View File

@ -7,52 +7,18 @@
#include "esp_log.h" #include "esp_log.h"
#include "esp_debug_helpers.h" #include "esp_debug_helpers.h"
#include "main.h"
#include "lorcomm.h"
#include "esp32-lora.h" #include "esp32-lora.h"
#include "console.h" #include "console.h"
#include "ble.h"
static const char *TAG = "lora-bigbin"; static const char *TAG = "loracom-main";
// disable memory alignment lora32_cfg_t lora;
#pragma pack(push, 1) uint8_t mac[6];
typedef struct {
uint8_t src[3];
uint8_t dst[3];
uint8_t type;
void *payload;
} lora_packet;
// this is wrong, why is working correctly? should be 7
#define LORA_PACKET_HEADER_SIZE (8)
typedef struct {
uint8_t length;
char *msg;
} lora_msg;
#define LORA_MSG_HEADER_SIZE (1)
typedef struct {
uint8_t origin[3];
uint8_t type; // same as packet type
void *payload;
} lora_routing;
#define LORA_ROUTING_HEADER_SIZE (4)
#pragma pack(pop)
enum lora_msg_type {
TEXT = 0,
ROUTING,
};
static lora32_cfg_t lora;
uint8_t mac[6] = {0};
static void handle_lora_txdone() { static void handle_lora_txdone() {
ESP_LOGI(TAG, "txdone: enabling continuous RX");
lora32_enable_continuous_rx(&lora); lora32_enable_continuous_rx(&lora);
} }
@ -120,73 +86,43 @@ receive_cleanup:
lora32_enable_continuous_rx(&lora); lora32_enable_continuous_rx(&lora);
} }
void send_message(lora32_cfg_t *lora, char *text) {
uint8_t len = strlen(text) + 1;
lora_packet packet = { 0 };
lora_msg msg = { 0 };
memcpy(packet.src, &mac[3], 3);
memset(packet.dst, 0xFF, 3);
packet.type = TEXT;
packet.payload = malloc(LORA_PACKET_HEADER_SIZE + len);
msg.length = len;
msg.msg = malloc(LORA_MSG_HEADER_SIZE + len);
memcpy(msg.msg, text, len);
uint8_t buf_len = LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE + len;
uint8_t *send_buf = malloc(buf_len);
ESP_LOGI(TAG, "packet length: %d", buf_len);
// set src, dst, length
memcpy(send_buf, (uint8_t*)&packet, LORA_PACKET_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE), (void*)&msg, LORA_MSG_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE), (void*)msg.msg, len);
ESP_LOGI(TAG, "src: [%02X:%02X:%02X]", packet.src[0], packet.src[1], packet.src[2]);
ESP_LOGI(TAG, "dst: [%02X:%02X:%02X]", packet.dst[0], packet.dst[1], packet.dst[2]);
lora32_send(lora, send_buf, buf_len);
free(packet.payload);
packet.payload = NULL;
free(msg.msg);
msg.msg = NULL;
free(send_buf);
}
void app_main(void) { void app_main(void) {
esp_efuse_mac_get_default((uint8_t*)&mac); esp_efuse_mac_get_default((uint8_t*)&mac);
ESP_LOGI(TAG, "MAC: [%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); ESP_LOGI(TAG, "MAC: [%02X:%02X:%02X:%02X:%02X:%02X]", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
lora.bandwidth = B250;
lora.codingRate = DEFAULT_CR; //lora.bandwidth = B208;
//lora.codingRate = 5;
lora.frequency = 915000000;
//lora.spreadingFactor = 8;
//lora.preamble = DEFAULT_PREAMBLE;
lora.dio0 = CONFIG_LORA32_DIO0_PIN; lora.dio0 = CONFIG_LORA32_DIO0_PIN;
lora.implicitHeader = false; lora.implicitHeader = false;
lora.nss = CONFIG_LORA32_NSS_PIN; lora.nss = CONFIG_LORA32_NSS_PIN;
lora.reset = CONFIG_LORA32_RESET_PIN; lora.reset = CONFIG_LORA32_RESET_PIN;
lora.frequency = 866000000;
lora.preamble = DEFAULT_PREAMBLE;
lora.spreadingFactor = DEFAULT_SF;
lora.receive = NULL;
lora.useCRC = false; lora.useCRC = false;
lora.fifoIdx = 0; lora.fifoIdx = 0;
lora.receive = &handle_lora_receive; lora.receive = &handle_lora_receive;
lora.tx_done = &handle_lora_txdone; lora.tx_done = &handle_lora_txdone;
lora.cad_done = &handle_lora_caddone; //lora.cad_done = &handle_lora_caddone;
//lora.cad_detected = &handle_lora_caddetected; //lora.cad_detected = &handle_lora_caddetected;
lora32_init(&lora); lora32_init(&lora);
//double dr = lora32_calc_datarate(&lora);
//ESP_LOGI(TAG, "data rate: %fbps", dr);
lora32_dump_regs(&lora);
xTaskCreate(console_task, "console", 4048, &lora, tskIDLE_PRIORITY + 3, NULL); xTaskCreate(console_task, "console", 4048, &lora, tskIDLE_PRIORITY + 3, NULL);
send_message(&lora, "hi\0"); //lora32_send(&lora, (uint8_t*)"hi\0", 3);
double dr = lora32_calc_datarate(&lora);
ESP_LOGI(TAG, "data rate: %fbps", dr); //ble_init();
lora32_enable_continuous_rx(&lora);
while(1) { while(1) {
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
} }