initial convert from arduino to esp-idf
This commit is contained in:
224
DinnerRecv/main/main.c
Normal file
224
DinnerRecv/main/main.c
Normal file
@ -0,0 +1,224 @@
|
||||
#include "driver/rmt_tx.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "led_strip_encoder.h"
|
||||
#include "nvs_flash.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LED_STRIP_GPIO_NUM 8
|
||||
#define LED_STRIP_LED_NUMBERS 4
|
||||
#define LED_STRIP_RMT_RES_HZ \
|
||||
(10 * 1000 * 1000) // 10MHz resolution, 1 tick = 0.1us
|
||||
|
||||
#define ESPNOW_WIFI_CHANNEL 6
|
||||
|
||||
static const char *TAG = "DinnerRecv";
|
||||
|
||||
// Global variables
|
||||
static bool dinner_alert = false;
|
||||
static uint64_t dinner_start_time = 0;
|
||||
static uint64_t last_blink_time = 0;
|
||||
static bool led_state = false;
|
||||
static uint8_t breath = 0;
|
||||
static uint64_t last_breath_time = 0;
|
||||
|
||||
// RMT and LED strip handles
|
||||
static rmt_channel_handle_t led_chan = NULL;
|
||||
static rmt_encoder_handle_t led_encoder = NULL;
|
||||
|
||||
typedef struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} led_color_t;
|
||||
|
||||
static led_color_t led_strip_buf[LED_STRIP_LED_NUMBERS];
|
||||
|
||||
static void led_strip_set_pixel(int index, uint8_t red, uint8_t green,
|
||||
uint8_t blue) {
|
||||
if (index < LED_STRIP_LED_NUMBERS) {
|
||||
led_strip_buf[index].r = red;
|
||||
led_strip_buf[index].g = green;
|
||||
led_strip_buf[index].b = blue;
|
||||
}
|
||||
}
|
||||
|
||||
static void led_strip_clear(void) {
|
||||
for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) {
|
||||
led_strip_set_pixel(i, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void led_strip_show(void) {
|
||||
rmt_transmit_config_t tx_config = {
|
||||
.loop_count = 0, // no transfer loop
|
||||
};
|
||||
|
||||
rmt_transmit(led_chan, led_encoder, led_strip_buf, sizeof(led_strip_buf),
|
||||
&tx_config);
|
||||
rmt_tx_wait_all_done(led_chan, portMAX_DELAY);
|
||||
}
|
||||
|
||||
static uint64_t get_time_ms(void) { return esp_timer_get_time() / 1000; }
|
||||
|
||||
static void on_data_received(const esp_now_recv_info_t *mac_addr,
|
||||
const uint8_t *data, int data_len) {
|
||||
char message[data_len + 1];
|
||||
memcpy(message, data, data_len);
|
||||
message[data_len] = '\0';
|
||||
|
||||
ESP_LOGI(TAG, "Received message: %s", message);
|
||||
|
||||
if (strcmp(message, "dinner") == 0) {
|
||||
ESP_LOGI(TAG, "Dinner alert received!");
|
||||
dinner_alert = true;
|
||||
dinner_start_time = get_time_ms();
|
||||
last_blink_time = get_time_ms();
|
||||
led_state = false;
|
||||
}
|
||||
}
|
||||
|
||||
static void blink_all_leds(void) {
|
||||
if (get_time_ms() - last_blink_time >= 2000) {
|
||||
last_blink_time = get_time_ms();
|
||||
led_state = !led_state;
|
||||
|
||||
if (led_state) {
|
||||
for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) {
|
||||
led_strip_set_pixel(i, 255, 0, 0);
|
||||
}
|
||||
} else {
|
||||
led_strip_clear();
|
||||
}
|
||||
led_strip_show();
|
||||
}
|
||||
}
|
||||
|
||||
static void breathe(void) {
|
||||
uint8_t val = breath > 128 ? 256 - breath : breath;
|
||||
for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) {
|
||||
led_strip_set_pixel(i, 0, 0, val);
|
||||
}
|
||||
led_strip_show();
|
||||
}
|
||||
|
||||
static void dinner_animation(void) {
|
||||
if (get_time_ms() - dinner_start_time > 15 * 1000) {
|
||||
dinner_alert = false;
|
||||
ESP_LOGI(TAG, "Dinner alert timeout - returning to normal mode");
|
||||
led_strip_clear();
|
||||
led_strip_show();
|
||||
} else {
|
||||
blink_all_leds();
|
||||
}
|
||||
}
|
||||
|
||||
static void breath_animation(void) {
|
||||
uint64_t time = get_time_ms();
|
||||
if (time % 25 == 0 && time != last_breath_time) {
|
||||
breath++;
|
||||
last_breath_time = time;
|
||||
breathe();
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t init_wifi(void) {
|
||||
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);
|
||||
|
||||
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_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
ESP_ERROR_CHECK(
|
||||
esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE));
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t init_espnow(void) {
|
||||
esp_err_t ret = esp_now_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error initializing ESP-NOW: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_now_register_recv_cb(on_data_received));
|
||||
ESP_LOGI(TAG, "ESP-NOW initialized and ready to receive messages");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t init_led_strip(void) {
|
||||
ESP_LOGI(TAG, "Create RMT TX channel");
|
||||
rmt_tx_channel_config_t tx_chan_config = {
|
||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
.gpio_num = LED_STRIP_GPIO_NUM,
|
||||
.mem_block_symbols = 64,
|
||||
.resolution_hz = LED_STRIP_RMT_RES_HZ,
|
||||
.trans_queue_depth = 4,
|
||||
};
|
||||
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
|
||||
|
||||
ESP_LOGI(TAG, "Install led strip encoder");
|
||||
led_strip_encoder_config_t encoder_config = {
|
||||
.resolution = LED_STRIP_RMT_RES_HZ,
|
||||
};
|
||||
ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder));
|
||||
|
||||
ESP_LOGI(TAG, "Enable RMT TX channel");
|
||||
ESP_ERROR_CHECK(rmt_enable(led_chan));
|
||||
|
||||
// Clear all LEDs initially
|
||||
led_strip_clear();
|
||||
led_strip_show();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
ESP_LOGI(TAG, "Dinner RECV");
|
||||
|
||||
// Initialize LED strip
|
||||
ESP_ERROR_CHECK(init_led_strip());
|
||||
|
||||
// Initialize WiFi
|
||||
ESP_ERROR_CHECK(init_wifi());
|
||||
|
||||
// Initialize ESP-NOW
|
||||
ESP_ERROR_CHECK(init_espnow());
|
||||
|
||||
// Print WiFi parameters
|
||||
uint8_t mac[6];
|
||||
esp_wifi_get_mac(WIFI_IF_STA, mac);
|
||||
ESP_LOGI(TAG, "Wi-Fi parameters:");
|
||||
ESP_LOGI(TAG, " Mode: STA");
|
||||
ESP_LOGI(TAG, " MAC Address: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1],
|
||||
mac[2], mac[3], mac[4], mac[5]);
|
||||
ESP_LOGI(TAG, " Channel: %d", ESPNOW_WIFI_CHANNEL);
|
||||
|
||||
// Main loop
|
||||
while (1) {
|
||||
if (dinner_alert) {
|
||||
dinner_animation();
|
||||
} else {
|
||||
breath_animation();
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(10)); // Small delay to prevent watchdog timeout
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user