diff --git a/CMakeLists.txt b/CMakeLists.txt index 56deb93..dfd8949 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,10 @@ idf_component_register( esp_driver_spi esp_timer esp_lcd + esp_event + esp_netif + esp_wifi + nvs_flash ) target_compile_options(${COMPONENT_LIB} PRIVATE diff --git a/blk_box.cpp b/blk_box.cpp index 042577d..b51bc4a 100644 --- a/blk_box.cpp +++ b/blk_box.cpp @@ -6,6 +6,9 @@ #include "blk_box_drivers/char_lcd.hpp" #include "blk_box_drivers/ssegs.hpp" #include "blk_box_drivers/tft.hpp" +#include "blk_box_drivers/nvs.hpp" +#include "blk_box_drivers/radio.hpp" +#include "blk_box_drivers/bbnow.hpp" void init_blk_box(BlkBoxInitConfig cfg) { init_main_i2c(); @@ -14,4 +17,8 @@ void init_blk_box(BlkBoxInitConfig cfg) { init_lcd(); init_ssegs(); init_tft(); + + init_nvs(); + init_radio(); + init_espnow(); } diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index a54465c..e47f745 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -7,6 +7,8 @@ set(SOURCES "i2c.cpp" "lcd2004.cpp" "leds.cpp" + "nvs.cpp" + "radio.cpp" "ssegs.cpp" "tft.cpp" "tm1640.cpp" diff --git a/drivers/bbnow.cpp b/drivers/bbnow.cpp index 0149d5c..0ed8e84 100644 --- a/drivers/bbnow.cpp +++ b/drivers/bbnow.cpp @@ -1,3 +1,66 @@ #include "blk_box_drivers/bbnow.hpp" +#include "esp_now.h" +#include "esp_log.h" +#include "esp_mac.h" +#include + +const static char TAG[] = "bbnow"; + +static void example_espnow_send_cb(const esp_now_send_info_t *tx_info, esp_now_send_status_t status); +static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len); + +/* ESPNOW sending or receiving callback function is called in WiFi task. + * Users should not do lengthy operations from this task. Instead, post + * necessary data to a queue and handle it from a lower priority task. */ +static void example_espnow_send_cb(const esp_now_send_info_t *tx_info, esp_now_send_status_t status) +{ + if (tx_info == NULL) { + ESP_LOGE(TAG, "Send cb arg error"); + return; + } + ESP_LOGI(TAG, "sending espnow packet of size: %d to " MACSTR, tx_info->data_len, MAC2STR(tx_info->des_addr)); +} + +static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len) +{ + uint8_t * src_addr = recv_info->src_addr; + uint8_t * des_addr = recv_info->des_addr; + + if (src_addr == NULL || data == NULL || len <= 0) { + ESP_LOGE(TAG, "Receive cb arg error"); + return; + } + + ESP_LOGI(TAG, "got espnow packet of size: %d from " MACSTR " to " MACSTR, MAC2STR(src_addr), MAC2STR(des_addr)); + + // if (IS_BROADCAST_ADDR(des_addr)) { + // /* If added a peer with encryption before, the receive packets may be + // * encrypted as peer-to-peer message or unencrypted over the broadcast channel. + // * Users can check the destination address to distinguish it. + // */ + // ESP_LOGD(TAG, "Receive broadcast ESPNOW data"); + // } else { + // ESP_LOGD(TAG, "Receive unicast ESPNOW data"); + // } +} +// TODO: add "initializing _____..." +// and "____ initialized!" logs to all driver init functions. +void init_espnow(void) { + ESP_ERROR_CHECK( esp_now_init() ); + ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) ); + ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) ); + + /* Set primary master key. */ + // TODO: add provisions for encryption + // ESP_ERROR_CHECK( esp_now_set_pmk(ESPNOW_PMK)); + + // Add broadcast peer information to peer list, allowing us to send to it + esp_now_peer_info_t peer = {0}; + peer.channel = BBNOW_DEFAULT_CHANNEL; + peer.ifidx = WIFI_IF_AP; + peer.encrypt = false; + memcpy(peer.peer_addr, BROADCAST_MAC, ESP_NOW_ETH_ALEN); + ESP_ERROR_CHECK( esp_now_add_peer(&peer) ); +} diff --git a/drivers/nvs.cpp b/drivers/nvs.cpp new file mode 100644 index 0000000..faa6728 --- /dev/null +++ b/drivers/nvs.cpp @@ -0,0 +1,13 @@ +#include "blk_box_drivers/nvs.hpp" +#include "nvs_flash.h" + +void init_nvs() { + // TODO: do more once we are doing more with nvs. + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + // TODO: dont just erase, but also handle the case where we have to upgrade the nvs partition. + ESP_ERROR_CHECK( nvs_flash_erase() ); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK( ret ); +} diff --git a/drivers/radio.cpp b/drivers/radio.cpp new file mode 100644 index 0000000..0027623 --- /dev/null +++ b/drivers/radio.cpp @@ -0,0 +1,18 @@ +#include "blk_box_drivers/radio.hpp" +#include "blk_box_drivers/bbnow.hpp" +#include "esp_wifi.h" + +void init_radio() { + // TODO: Do more once we are doing wifi in addition to espnow + 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(WIFI_MODE_AP) ); + ESP_ERROR_CHECK( esp_wifi_start()); + ESP_ERROR_CHECK( esp_wifi_set_channel(BBNOW_DEFAULT_CHANNEL, WIFI_SECOND_CHAN_NONE)); + + // 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) ); +} diff --git a/include/blk_box_drivers/bbnow.hpp b/include/blk_box_drivers/bbnow.hpp index 65d8637..3c3d934 100644 --- a/include/blk_box_drivers/bbnow.hpp +++ b/include/blk_box_drivers/bbnow.hpp @@ -1,6 +1,12 @@ #ifndef BBNOW_HPP #define BBNOW_HPP +#include "esp_now.h" +/// The channel to use if not on a WIFI network +#define BBNOW_DEFAULT_CHANNEL 6 +const static uint8_t BROADCAST_MAC[ESP_NOW_ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + +void init_espnow(); #endif /* BBNOW_HPP */ diff --git a/include/blk_box_drivers/nvs.hpp b/include/blk_box_drivers/nvs.hpp new file mode 100644 index 0000000..8e383c6 --- /dev/null +++ b/include/blk_box_drivers/nvs.hpp @@ -0,0 +1,6 @@ +#ifndef NVS_HPP +#define NVS_HPP + +void init_nvs(); + +#endif /* NVS_HPP */ diff --git a/include/blk_box_drivers/radio.hpp b/include/blk_box_drivers/radio.hpp new file mode 100644 index 0000000..d079fc5 --- /dev/null +++ b/include/blk_box_drivers/radio.hpp @@ -0,0 +1,6 @@ +#ifndef RADIO_HPP +#define RADIO_HPP + +void init_radio(); + +#endif /* RADIO_HPP */