From 31cacd63b3f75c8860cd4ddead48b247cdaa835d Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Wed, 22 Apr 2020 22:22:09 -0700 Subject: [PATCH] moved a lot of lorcomm specific code into own files --- include/lorcomm.h | 40 +++++++++++++++++ main/lorcomm.c | 47 +++++++++++++++++++ main/main.c | 112 ++++++++++------------------------------------ 3 files changed, 111 insertions(+), 88 deletions(-) create mode 100644 include/lorcomm.h create mode 100644 main/lorcomm.c diff --git a/include/lorcomm.h b/include/lorcomm.h new file mode 100644 index 0000000..8beedf3 --- /dev/null +++ b/include/lorcomm.h @@ -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 diff --git a/main/lorcomm.c b/main/lorcomm.c new file mode 100644 index 0000000..907fdb6 --- /dev/null +++ b/main/lorcomm.c @@ -0,0 +1,47 @@ +#include + +#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); +} + diff --git a/main/main.c b/main/main.c index cee5e0c..2b40152 100644 --- a/main/main.c +++ b/main/main.c @@ -7,52 +7,18 @@ #include "esp_log.h" #include "esp_debug_helpers.h" +#include "main.h" +#include "lorcomm.h" #include "esp32-lora.h" #include "console.h" +#include "ble.h" -static const char *TAG = "lora-bigbin"; +static const char *TAG = "loracom-main"; -// 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, -}; - -static lora32_cfg_t lora; - -uint8_t mac[6] = {0}; +lora32_cfg_t lora; +uint8_t mac[6]; static void handle_lora_txdone() { - ESP_LOGI(TAG, "txdone: enabling continuous RX"); - lora32_enable_continuous_rx(&lora); } @@ -120,73 +86,43 @@ receive_cleanup: 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) { 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]); - 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.implicitHeader = false; lora.nss = CONFIG_LORA32_NSS_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.fifoIdx = 0; lora.receive = &handle_lora_receive; lora.tx_done = &handle_lora_txdone; - lora.cad_done = &handle_lora_caddone; + //lora.cad_done = &handle_lora_caddone; //lora.cad_detected = &handle_lora_caddetected; 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); - send_message(&lora, "hi\0"); - double dr = lora32_calc_datarate(&lora); - ESP_LOGI(TAG, "data rate: %fbps", dr); + //lora32_send(&lora, (uint8_t*)"hi\0", 3); + + //ble_init(); + + lora32_enable_continuous_rx(&lora); while(1) { - vTaskDelay(100 / portTICK_PERIOD_MS); + vTaskDelay(1000 / portTICK_PERIOD_MS); } }