Functional prototype!
This commit is contained in:
+13
-2
@@ -1,2 +1,13 @@
|
||||
idf_component_register(SRCS mcu-code.cpp peripherals/usb_serial.cpp peripherals/usb_serial.h
|
||||
INCLUDE_DIRS ".")
|
||||
idf_component_register(SRCS
|
||||
mcu-code.cpp
|
||||
peripherals/usb_serial.cpp
|
||||
peripherals/usb_serial.h
|
||||
peripherals/uart_serial.cpp
|
||||
peripherals/uart_serial.h
|
||||
peripherals/wireless_comms.cpp
|
||||
peripherals/wireless_comms.h
|
||||
peripherals/lp_uart_serial.cpp
|
||||
peripherals/lp_uart_serial.h
|
||||
oled_display/oled_display.cpp
|
||||
oled_display/oled_display.h
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
+58
-5
@@ -1,6 +1,8 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "peripherals/uart_serial.h"
|
||||
#include "peripherals/usb_serial.h"
|
||||
#include "peripherals/wireless_comms.h"
|
||||
|
||||
///
|
||||
/// So i lost all the code from the previous version of this due to messing with git.
|
||||
@@ -10,11 +12,62 @@
|
||||
/// Nicholas - 23/08/2026 @ 21:36
|
||||
///
|
||||
|
||||
const char* test = "amai zeg";
|
||||
const char *test = "amai zeg\n";
|
||||
|
||||
#define NODE_NUMBER 1
|
||||
#define TOTAL_NUMBER_OF_NODES 4
|
||||
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
usb_serial::init();
|
||||
usb_serial::write(test,strlen(test));
|
||||
uint8_t node_mac[ESP_NOW_ETH_ALEN] = {0xDE, 0xD5, 0xEC, 0x00, 0x00, 0x02};
|
||||
|
||||
uint8_t peer_mac[ESP_NOW_ETH_ALEN] = {0xDE, 0xD5, 0xEC, 0x00, 0x00, 0x01};
|
||||
|
||||
uint8_t peer_list[TOTAL_NUMBER_OF_NODES][ESP_NOW_ETH_ALEN];
|
||||
|
||||
typedef struct communications {
|
||||
uart_serial uart;
|
||||
wireless_comms *wireless;
|
||||
} communications;
|
||||
|
||||
|
||||
void main_task(void *pvParameter) {
|
||||
auto comms = reinterpret_cast<communications *>(pvParameter);
|
||||
uart_serial uart = comms->uart;
|
||||
char data;
|
||||
while (true) {
|
||||
if (usb_serial::read_byte(&data)) {
|
||||
wireless_comms::send_byte(data);
|
||||
uart.write_byte(&data);
|
||||
}
|
||||
if (uart.read_byte(&data)) {
|
||||
ESP_LOGI("uart", "received data");
|
||||
wireless_comms::send_byte(data);
|
||||
usb_serial::write_byte(&data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void app_main(void) {
|
||||
esp_log_level_set("*", ESP_LOG_NONE);
|
||||
usb_serial::init();
|
||||
uart_serial uart(UART_NUM_0, GPIO_NUM_18, GPIO_NUM_19, 9600);
|
||||
wireless_comms wireless;
|
||||
|
||||
//usb_serial::write(test,strlen(test));
|
||||
communications comms = {.uart = uart, .wireless = &wireless};
|
||||
/*
|
||||
uint8_t peer_gen[ESP_NOW_ETH_ALEN] = {0xDE, 0xD5, 0xEC, 0x00, 0x00, 0x00};
|
||||
for(int i=2;i<TOTAL_NUMBER_OF_NODES;i++) {
|
||||
peer_gen[ESP_NOW_ETH_ALEN-1] = i;
|
||||
memcpy(peer_list[i],peer_gen,ESP_NOW_ETH_ALEN);
|
||||
test_comms.add_peer(peer_gen);
|
||||
}*/
|
||||
wireless_comms::add_peer(peer_mac);
|
||||
//test_comms.add_peer(peer_mac);
|
||||
wireless.create_rtos_task(uart);
|
||||
wireless_comms::send_byte('e');
|
||||
const char testchar = 'e';
|
||||
uart.write_byte(&testchar);
|
||||
|
||||
xTaskCreate(main_task, "main_task", 4096, &comms, 5,NULL);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by nano on 9/25/26.
|
||||
//
|
||||
|
||||
#include "oled_display.h"
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by nano on 9/25/26.
|
||||
//
|
||||
|
||||
#ifndef MCU_CODE_OLED_DISPLAY_H
|
||||
#define MCU_CODE_OLED_DISPLAY_H
|
||||
|
||||
|
||||
class oled_display {
|
||||
};
|
||||
|
||||
|
||||
#endif //MCU_CODE_OLED_DISPLAY_H
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by nano on 9/16/26.
|
||||
//
|
||||
|
||||
#include "lp_uart_serial.h"
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by nano on 9/16/26.
|
||||
//
|
||||
|
||||
#ifndef MCU_CODE_LP_UART_SERIAL_H
|
||||
#define MCU_CODE_LP_UART_SERIAL_H
|
||||
#include "lp_uart_serial.h"
|
||||
#include "uart_serial.h"
|
||||
|
||||
|
||||
class lp_uart_serial : public uart_serial {
|
||||
public:
|
||||
lp_uart_serial(const gpio_num_t tx = GPIO_NUM_NC,
|
||||
const gpio_num_t rx = GPIO_NUM_NC,
|
||||
int baudrate = 9600
|
||||
) : uart_serial(LP_UART_NUM_0,tx,rx,baudrate) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //MCU_CODE_LP_UART_SERIAL_H
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by nano on 8/23/26.
|
||||
//
|
||||
|
||||
#include "uart_serial.h"
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
void uart_serial::init() {
|
||||
ESP_ERROR_CHECK(uart_driver_install(uart_num, buffer_size * 2, buffer_size * 2, 10, &uart_queue, 0));
|
||||
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
|
||||
|
||||
ESP_ERROR_CHECK(
|
||||
uart_set_pin(uart_num, tx_pin_num, rx_pin_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE,
|
||||
UART_PIN_NO_CHANGE));
|
||||
uart_flush(uart_num);
|
||||
}
|
||||
|
||||
void uart_serial::write(const char* data, int len) {
|
||||
uart_write_bytes(uart_num, data, len);
|
||||
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 100));
|
||||
}
|
||||
void uart_serial::write_byte(const char* data) {
|
||||
uart_write_bytes(uart_num, data, 1);
|
||||
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 100));
|
||||
}
|
||||
|
||||
int uart_serial::read(char* data, int size) {
|
||||
int length = 0;
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num,(size_t *) &length));
|
||||
if (length > size) {
|
||||
uart_read_bytes(uart_num,data,size,100);
|
||||
return size;
|
||||
}
|
||||
uart_read_bytes(uart_num,data,length,100);
|
||||
return length;
|
||||
}
|
||||
|
||||
/*int uart_serial::read_byte(char* data) {
|
||||
int length = 0;
|
||||
ESP_LOGI("sanity check","uart num: %d",uart_num);
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num,(size_t *) &length));
|
||||
if (length) {
|
||||
uart_read_bytes(uart_num,data,1,100);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
int uart_serial::read_byte(char* data) {
|
||||
size_t length = 0;
|
||||
esp_err_t err = uart_get_buffered_data_len(this->uart_num, &length);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE("uart_serial", "get_buffered_data_len failed on port %d: %s",
|
||||
uart_num, esp_err_to_name(err));
|
||||
return 0;
|
||||
}
|
||||
if (length > 0) {
|
||||
int n = uart_read_bytes(uart_num, data, 1, pdMS_TO_TICKS(100));
|
||||
return (n == 1) ? 1 : 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by nano on 8/23/26.
|
||||
//
|
||||
|
||||
#ifndef MCU_CODE_UART_SERIAL_H
|
||||
#define MCU_CODE_UART_SERIAL_H
|
||||
#include "driver/uart.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "hal/uart_types.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
class uart_serial {
|
||||
public:
|
||||
explicit uart_serial(uart_port_t uart_port_num = UART_NUM_0, const gpio_num_t tx = GPIO_NUM_NC,
|
||||
const gpio_num_t rx = GPIO_NUM_NC, int baudrate = 9600) : uart_num(uart_port_num), tx_pin_num(tx),
|
||||
rx_pin_num(rx) {
|
||||
uart_config.baud_rate = baudrate;
|
||||
uart_config.data_bits = UART_DATA_8_BITS;
|
||||
uart_config.parity = UART_PARITY_DISABLE;
|
||||
uart_config.stop_bits = UART_STOP_BITS_1;
|
||||
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
|
||||
|
||||
init();
|
||||
};
|
||||
|
||||
void uart_serial_dtor() const {
|
||||
uart_driver_delete(uart_num);
|
||||
}
|
||||
void write(const char* data, int len);
|
||||
void write_byte(const char* data);
|
||||
|
||||
int read(char* data,int len);
|
||||
int read_byte(char* data);
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
QueueHandle_t uart_queue = {};
|
||||
const uart_port_t uart_num;
|
||||
uart_config_t uart_config = {};
|
||||
const gpio_num_t tx_pin_num;
|
||||
const gpio_num_t rx_pin_num;
|
||||
constexpr static int buffer_size = (1024);
|
||||
};
|
||||
|
||||
|
||||
#endif //MCU_CODE_UART_SERIAL_H
|
||||
@@ -16,7 +16,6 @@ void usb_serial::init() {
|
||||
};
|
||||
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config));
|
||||
ESP_LOGI("usb_serial", "USB_SERIAL_JTAG init done");
|
||||
|
||||
}
|
||||
|
||||
void usb_serial::write(const char* byte, int len) {
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
#include <cstdint>
|
||||
|
||||
namespace usb_serial {
|
||||
static void write(const char* byte, int len);
|
||||
static int read(char* byte,int len);
|
||||
void write(const char* byte, int len);
|
||||
int read(char* byte,int len);
|
||||
|
||||
static void write_byte(char* byte);
|
||||
static int read_byte(char* byte);
|
||||
static void init();
|
||||
void write_byte(char* byte);
|
||||
int read_byte(char* byte);
|
||||
void init();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
//
|
||||
// Created by nano on 8/23/26.
|
||||
//
|
||||
|
||||
#include "wireless_comms.h"
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_crc.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_random.h"
|
||||
#include "usb_serial.h"
|
||||
|
||||
const char *TAG = "wireless_comms";
|
||||
|
||||
#define ESPNOW_MAXDELAY 512
|
||||
|
||||
static QueueHandle_t espnow_send_queue = NULL;
|
||||
|
||||
static uint8_t s_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
extern uint8_t node_mac[ESP_NOW_ETH_ALEN];
|
||||
|
||||
void wireless_comms::wifi_init(int channel) {
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(ESPNOW_WIFI_MODE));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mac(ESPNOW_WIFI_IF,node_mac));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
ESP_ERROR_CHECK(esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE));
|
||||
|
||||
|
||||
#ifdef CONFIG_ESPNOW_ENABLE_LONG_RANGE
|
||||
ESP_ERROR_CHECK(
|
||||
esp_wifi_set_protocol(ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR));
|
||||
#endif
|
||||
}
|
||||
|
||||
void wireless_comms::espnow_send_callback(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) {
|
||||
espnow_event_t evt;
|
||||
espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
|
||||
|
||||
if (tx_info == NULL) {
|
||||
ESP_LOGE(TAG, "Send cb arg error");
|
||||
return;
|
||||
}
|
||||
|
||||
evt.id = ESPNOW_SEND_CB;
|
||||
memcpy(send_cb->mac_addr, tx_info->des_addr, ESP_NOW_ETH_ALEN);
|
||||
send_cb->status = status;
|
||||
if (xQueueSend(espnow_send_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
|
||||
ESP_LOGW(TAG, "Send send queue fail");
|
||||
}
|
||||
}
|
||||
|
||||
void wireless_comms::espnow_recv_callback(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len) {
|
||||
espnow_event_t evt;
|
||||
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
|
||||
uint8_t *mac_addr = recv_info->src_addr;
|
||||
|
||||
if (mac_addr == NULL || data == NULL || len <= 0) {
|
||||
ESP_LOGE(TAG, "Receive cb arg error");
|
||||
return;
|
||||
}
|
||||
|
||||
evt.id = ESPNOW_RECV_CB;
|
||||
memcpy(recv_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
|
||||
recv_cb->data = (uint8_t *) malloc(len);
|
||||
if (recv_cb->data == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc receive data fail");
|
||||
return;
|
||||
}
|
||||
memcpy(recv_cb->data, data, len);
|
||||
recv_cb->data_len = len;
|
||||
if (xQueueSend(espnow_send_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
|
||||
ESP_LOGW(TAG, "Send receive queue fail");
|
||||
free(recv_cb->data);
|
||||
}
|
||||
}
|
||||
|
||||
/* Parse received ESPNOW data. */
|
||||
int wireless_comms::espnow_data_parse(uint8_t *data, uint16_t data_len) {
|
||||
auto *buf = (espnow_data_t *) data;
|
||||
uint16_t crc, crc_cal = 0;
|
||||
|
||||
if (data_len < sizeof(espnow_data_t)) {
|
||||
ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
crc = buf->crc;
|
||||
buf->crc = 0;
|
||||
crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *) buf, data_len);
|
||||
|
||||
if (crc_cal == crc) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Prepare ESPNOW data to be sent. */
|
||||
void wireless_comms::espnow_send_data_prepare(espnow_send_param_t *send_param) {
|
||||
espnow_data_t *buf = (espnow_data_t *) send_param->buffer;
|
||||
assert(send_param->len >= sizeof(espnow_data_t));
|
||||
buf->crc = 0;
|
||||
buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *) buf, send_param->len);
|
||||
ESP_LOGI("espnow send_data_prepare", "%d %s", send_param->len - sizeof(espnow_data_t), buf->payload);
|
||||
}
|
||||
|
||||
esp_err_t wireless_comms::espnow_init(int wifi_channel) {
|
||||
// Create send queue
|
||||
espnow_send_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(espnow_event_t));
|
||||
if (espnow_send_queue == NULL) {
|
||||
ESP_LOGE(TAG, "Create queue fail");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Initialize ESPNOW and register sending and receiving callback function. */
|
||||
ESP_ERROR_CHECK(esp_now_init());
|
||||
ESP_ERROR_CHECK(esp_now_register_send_cb(espnow_send_callback));
|
||||
ESP_ERROR_CHECK(esp_now_register_recv_cb(espnow_recv_callback));
|
||||
#if CONFIG_ESPNOW_ENABLE_POWER_SAVE
|
||||
ESP_ERROR_CHECK(esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW));
|
||||
ESP_ERROR_CHECK(esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL));
|
||||
#endif
|
||||
/* Set primary master key. */
|
||||
ESP_ERROR_CHECK(esp_now_set_pmk((uint8_t *)ESPNOW_PMK));
|
||||
|
||||
/* Add broadcast peer information to peer list. */
|
||||
auto *peer = static_cast<esp_now_peer_info_t *>(malloc(sizeof(esp_now_peer_info_t)));
|
||||
if (peer == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc peer information fail");
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memset(peer, 0, sizeof(esp_now_peer_info_t));
|
||||
peer->channel = wifi_channel;
|
||||
peer->ifidx = ESPNOW_WIFI_IF;
|
||||
peer->encrypt = false;
|
||||
memcpy(peer->peer_addr, s_broadcast_mac, ESP_NOW_ETH_ALEN);
|
||||
ESP_ERROR_CHECK(esp_now_add_peer(peer));
|
||||
free(peer);
|
||||
|
||||
/* Initialize sending parameters. */
|
||||
send_param = (espnow_send_param_t *) malloc(sizeof(espnow_send_param_t));
|
||||
if (send_param == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc send parameter fail");
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memset(send_param, 0, sizeof(espnow_send_param_t));
|
||||
send_param->buffer = (uint8_t *) malloc(ESPNOW_SEND_LEN);
|
||||
send_param->len = ESPNOW_SEND_LEN;
|
||||
if (send_param->buffer == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc send buffer fail");
|
||||
free(send_param);
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memcpy(send_param->dest_mac, s_broadcast_mac, ESP_NOW_ETH_ALEN);
|
||||
espnow_send_data_prepare(send_param);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void wireless_comms::espnow_deinit(espnow_send_param_t *send_param) {
|
||||
free(send_param->buffer);
|
||||
free(send_param);
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
}
|
||||
|
||||
void wireless_comms::espnow_task(void *pvParameter) {
|
||||
espnow_event_t evt;
|
||||
int ret;
|
||||
auto task_params = (espnow_task_params_t *) pvParameter;
|
||||
espnow_send_param_t send_param = *task_params->send_param;
|
||||
uart_serial uart = task_params->uart;
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
|
||||
while (xQueueReceive(espnow_send_queue, &evt, portMAX_DELAY) == pdPASS) {
|
||||
switch (evt.id) {
|
||||
case ESPNOW_SEND_CB: {
|
||||
espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
|
||||
/* Delay a while before sending the next data. */
|
||||
ESP_LOGI(TAG, "send data to " MACSTR "", MAC2STR(send_cb->mac_addr));
|
||||
break;
|
||||
}
|
||||
case ESPNOW_RECV_CB: {
|
||||
espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
|
||||
|
||||
ret = espnow_data_parse(recv_cb->data, recv_cb->data_len);
|
||||
if (ret == 1) {
|
||||
auto *rx = (espnow_data_t *) recv_cb->data;
|
||||
ESP_LOGI(TAG, "Receive data from: " MACSTR ", len: %d, byte0: 0x%02x",
|
||||
MAC2STR(recv_cb->mac_addr), recv_cb->data_len, rx->payload[0]);
|
||||
usb_serial::write(reinterpret_cast<const char *>(&rx->payload), PAYLOAD_SIZE);
|
||||
uart.write(reinterpret_cast<const char *>(&rx->payload), PAYLOAD_SIZE);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Receive error (bad CRC) from: " MACSTR "", MAC2STR(recv_cb->mac_addr));
|
||||
}
|
||||
free(recv_cb->data);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ESP_LOGE(TAG, "Callback type error: %d", evt.id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG,"ENDED TASK WIRELESS");
|
||||
}
|
||||
|
||||
void wireless_comms::set_dest_mac(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]) {
|
||||
ESP_ERROR_CHECK(add_peer(dest_mac));
|
||||
memcpy(send_param->dest_mac, dest_mac, ESP_NOW_ETH_ALEN);
|
||||
espnow_send_data_prepare(send_param);
|
||||
}
|
||||
|
||||
esp_err_t wireless_comms::add_peer(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]) {
|
||||
auto *peer = static_cast<esp_now_peer_info_t *>(malloc(sizeof(esp_now_peer_info_t)));
|
||||
if (peer == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc peer information fail");
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memset(peer, 0, sizeof(esp_now_peer_info_t));
|
||||
peer->channel = ESPNOW_CHANNEL;
|
||||
peer->ifidx = ESPNOW_WIFI_IF;
|
||||
peer->encrypt = false;
|
||||
memcpy(peer->peer_addr, dest_mac, ESP_NOW_ETH_ALEN);
|
||||
ESP_ERROR_CHECK(esp_now_add_peer(peer));
|
||||
free(peer);
|
||||
ESP_LOGI(TAG, "Peer " MACSTR " added", MAC2STR(dest_mac));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void wireless_comms::delete_peer(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]) {
|
||||
ESP_ERROR_CHECK(esp_now_del_peer(dest_mac));
|
||||
}
|
||||
|
||||
esp_err_t wireless_comms::send_byte(uint8_t data) {
|
||||
auto send_parameters = (espnow_send_param_t *) malloc(sizeof(espnow_send_param_t));
|
||||
memset(send_parameters, 0, sizeof(espnow_send_param_t));
|
||||
send_parameters->len = sizeof(espnow_data_t);
|
||||
send_parameters->buffer = (uint8_t *) malloc(sizeof(espnow_data_t));
|
||||
if (send_parameters->buffer == NULL) {
|
||||
ESP_LOGE(TAG, "Malloc send buffer fail");
|
||||
free(send_parameters);
|
||||
vQueueDelete(espnow_send_queue);
|
||||
espnow_send_queue = NULL;
|
||||
esp_now_deinit();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
((espnow_data_t *) (send_parameters->buffer))->payload[0] = data; // i am disgusted at myself
|
||||
memset(((espnow_data_t *) (send_parameters->buffer))->payload + 1, 0,PAYLOAD_SIZE - 1);
|
||||
// fill the rest of the payload with zeroes
|
||||
|
||||
memcpy(send_parameters->dest_mac, s_broadcast_mac, ESP_NOW_ETH_ALEN); // TODO: THIS IS IGNORED FOR NOW
|
||||
espnow_send_data_prepare(send_parameters);
|
||||
if (esp_now_send(NULL, send_parameters->buffer, send_parameters->len) != ESP_OK) {
|
||||
// send to all peers
|
||||
ESP_LOGE(TAG, "Send error");
|
||||
espnow_deinit(send_parameters);
|
||||
}
|
||||
free(send_parameters);
|
||||
ESP_LOGI(TAG, "SENT DATA");
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// Created by nano on 8/23/26.
|
||||
//
|
||||
|
||||
#ifndef MCU_CODE_WIRELESS_COMMS_H
|
||||
#define MCU_CODE_WIRELESS_COMMS_H
|
||||
#include "esp_now.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "uart_serial.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#define PAYLOAD_SIZE 1
|
||||
|
||||
#define ESPNOW_PMK "pmk1234567890123" // ESPNOW primary master for the example to use. The length of ESPNOW primary master must be 16 bytes.
|
||||
#define ESPNOW_LMK "lmk1234567890123" // ESPNOW local master for the example to use. The length of ESPNOW local master must be 16 bytes.
|
||||
#define ESPNOW_SEND_COUNT 1 // range 1-65535; Total count of unicast ESPNOW data to be sent.
|
||||
#define ESPNOW_SEND_DELAY 0 // range 0-65535; default 1000; Delay between sending two ESPNOW data, unit: ms.
|
||||
#define ESPNOW_SEND_LEN (2 + PAYLOAD_SIZE) // range 10-1470; Length of ESPNOW data to be sent, unit: byte.
|
||||
|
||||
#define ESPNOW_CHANNEL 11
|
||||
#define ESPNOW_QUEUE_SIZE 512
|
||||
|
||||
#define CONFIG_ESPNOW_ENABLE_LONG_RANGE
|
||||
#define CONFIG_ESPNOW_WIFI_MODE_STATION
|
||||
// #define CONFIG_ESPNOW_ENABLE_POWER_SAVE
|
||||
|
||||
#ifdef CONFIG_ESPNOW_WIFI_MODE_STATION
|
||||
#define ESPNOW_WIFI_MODE WIFI_MODE_STA
|
||||
#define ESPNOW_WIFI_IF WIFI_IF_STA
|
||||
#else
|
||||
#define ESPNOW_WIFI_MODE WIFI_MODE_AP
|
||||
#define ESPNOW_WIFI_IF WIFI_IF_AP
|
||||
#endif
|
||||
|
||||
|
||||
#define IS_BROADCAST_ADDR(addr) (memcmp(addr, s_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
|
||||
|
||||
enum {
|
||||
ESPNOW_DATA_BROADCAST,
|
||||
ESPNOW_DATA_UNICAST,
|
||||
ESPNOW_DATA_MAX,
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
ESPNOW_SEND_CB,
|
||||
ESPNOW_RECV_CB,
|
||||
} espnow_event_id_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
|
||||
esp_now_send_status_t status;
|
||||
} espnow_event_send_cb_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
|
||||
uint8_t *data;
|
||||
int data_len;
|
||||
} espnow_event_recv_cb_t;
|
||||
|
||||
typedef union {
|
||||
espnow_event_send_cb_t send_cb;
|
||||
espnow_event_recv_cb_t recv_cb;
|
||||
} espnow_event_info_t;
|
||||
|
||||
/* When ESPNOW sending or receiving callback function is called, post event to ESPNOW task. */
|
||||
typedef struct {
|
||||
espnow_event_id_t id;
|
||||
espnow_event_info_t info;
|
||||
} espnow_event_t;
|
||||
|
||||
/* User defined field of ESPNOW data in this example. */
|
||||
typedef struct {
|
||||
uint16_t crc; //CRC16 value of ESPNOW data.
|
||||
uint8_t payload[PAYLOAD_SIZE]; //Real payload of ESPNOW data.
|
||||
} __attribute__((packed)) espnow_data_t;
|
||||
|
||||
/* Parameters of sending ESPNOW data. */
|
||||
typedef struct {
|
||||
int len; //Length of ESPNOW data to be sent, unit: byte.
|
||||
uint8_t *buffer; //Buffer pointing to ESPNOW data.
|
||||
uint8_t dest_mac[ESP_NOW_ETH_ALEN]; //MAC address of destination device.
|
||||
} espnow_send_param_t;
|
||||
|
||||
typedef struct espnow_task_params_t {
|
||||
espnow_send_param_t *send_param;
|
||||
uart_serial uart;
|
||||
|
||||
} espnow_task_params_t;
|
||||
|
||||
class wireless_comms {
|
||||
public:
|
||||
wireless_comms(int wifi_channel = ESPNOW_CHANNEL) {
|
||||
// Initialize NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK( nvs_flash_erase() );
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK( ret );
|
||||
|
||||
wifi_init(wifi_channel);
|
||||
espnow_init(wifi_channel);
|
||||
}
|
||||
void set_dest_mac(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]);
|
||||
void create_rtos_task(const uart_serial &uart) const {
|
||||
auto *task_params = new espnow_task_params_t{.send_param = send_param, .uart = uart}; // heap, outlives this call
|
||||
xTaskCreate(espnow_task, "espnow_task", 2048, task_params, 4, NULL);
|
||||
// note: task_params is intentionally leaked for the task's lifetime;
|
||||
// free it inside espnow_task if the task can ever exit
|
||||
}
|
||||
|
||||
static esp_err_t add_peer(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]);
|
||||
static void delete_peer(const uint8_t dest_mac[ESP_NOW_ETH_ALEN]);
|
||||
|
||||
static esp_err_t send_byte(uint8_t data) ;
|
||||
private:
|
||||
|
||||
espnow_send_param_t * send_param = nullptr;
|
||||
static void wifi_init(int channel);
|
||||
|
||||
static void espnow_send_callback(const esp_now_send_info_t *tx_info, esp_now_send_status_t status);
|
||||
|
||||
static void espnow_recv_callback(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len);
|
||||
|
||||
static int espnow_data_parse(uint8_t *data, uint16_t data_len);
|
||||
|
||||
static void espnow_send_data_prepare(espnow_send_param_t *send_param);
|
||||
|
||||
esp_err_t espnow_init(int wifi_channel);
|
||||
|
||||
static void espnow_deinit(espnow_send_param_t *send_param);
|
||||
static void espnow_task(void *pvParameter);
|
||||
};
|
||||
|
||||
|
||||
#endif //MCU_CODE_WIRELESS_COMMS_H
|
||||
Reference in New Issue
Block a user