Compare commits
1 Commits
acb5f6dc0f
...
master
Author | SHA1 | Date |
---|---|---|
Yilin Sun | fed6863dc1 |
|
@ -0,0 +1,12 @@
|
|||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterEnum: false
|
||||
AfterStruct: false
|
||||
SplitEmptyFunction: false
|
||||
ColumnLimit: 120
|
|
@ -0,0 +1,4 @@
|
|||
/build
|
||||
/cmake-build-*
|
||||
/sdkconfig
|
||||
/sdkconfig.old
|
|
@ -1,7 +1,7 @@
|
|||
idf_component_register(SRCS
|
||||
"main.c"
|
||||
"app_wifi.c"
|
||||
"app_task_led.c"
|
||||
"app_led.c"
|
||||
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
menu "WiFi Station Configuration"
|
||||
menu "Application Specific Configuration"
|
||||
|
||||
config ESP_WIFI_SSID
|
||||
config APP_WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config ESP_WIFI_PASSWORD
|
||||
config APP_WIFI_PASS
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
|
||||
config ESP_MAXIMUM_RETRY
|
||||
config APP_WIFI_RETR
|
||||
int "Maximum retry"
|
||||
default 5
|
||||
help
|
||||
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
|
||||
Set the Maximum retry to avoid station reconnecting to the AP.
|
||||
endmenu
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
/* FreeRTOS */
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
/* IDF */
|
||||
#include "driver/rmt_tx.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define LOG_TAG "LED"
|
||||
|
||||
#define LED_NP_PIN (8)
|
||||
|
||||
static rmt_encoder_handle_t s_led_enc;
|
||||
static rmt_channel_handle_t s_led_chan;
|
||||
|
||||
int app_led_init(void) {
|
||||
esp_err_t ret = ESP_OK;
|
||||
|
||||
rmt_tx_channel_config_t led_chan_config = {
|
||||
.clk_src = RMT_CLK_SRC_DEFAULT,
|
||||
.gpio_num = LED_NP_PIN,
|
||||
.mem_block_symbols = 64,
|
||||
.resolution_hz = 10000000UL,
|
||||
.trans_queue_depth = 4,
|
||||
};
|
||||
|
||||
ret = rmt_new_tx_channel(&led_chan_config, &s_led_chan);
|
||||
if(ret != ESP_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
rmt_bytes_encoder_config_t led_enc_config = {
|
||||
.bit0 =
|
||||
{
|
||||
.level0 = 1,
|
||||
.duration0 = 3,
|
||||
.level1 = 0,
|
||||
.duration1 = 9,
|
||||
},
|
||||
.bit1 =
|
||||
{
|
||||
.level0 = 1,
|
||||
.duration0 = 9,
|
||||
.level1 = 0,
|
||||
.duration1 = 3,
|
||||
},
|
||||
};
|
||||
|
||||
ret = rmt_new_bytes_encoder(&led_enc_config, &s_led_enc);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "Failed to create LED encoder.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "driver/ledc.h"
|
||||
|
||||
#define LED_R_PIN (4)
|
||||
#define LED_G_PIN (3)
|
||||
#define LED_B_PIN (5)
|
||||
|
||||
#define LED_R_CHANNEL LEDC_CHANNEL_0
|
||||
#define LED_G_CHANNEL LEDC_CHANNEL_1
|
||||
#define LED_B_CHANNEL LEDC_CHANNEL_2
|
||||
|
||||
#define FADE_MS 250
|
||||
|
||||
TaskHandle_t xTaskDiscoLEDHandle = NULL;
|
||||
void vTaskDiscoLED(void *pvParameters) {
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.duty_resolution = LEDC_TIMER_13_BIT,
|
||||
.freq_hz = 7500,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.clk_cfg = LEDC_AUTO_CLK
|
||||
};
|
||||
|
||||
ledc_timer_config(&ledc_timer);
|
||||
|
||||
ledc_channel_config_t ledc_channels[3] = {
|
||||
{
|
||||
.channel = LED_R_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LED_R_PIN,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.flags.output_invert = 0
|
||||
},
|
||||
{
|
||||
.channel = LED_G_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LED_G_PIN,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.flags.output_invert = 0
|
||||
},
|
||||
{
|
||||
.channel = LED_B_CHANNEL,
|
||||
.duty = 0,
|
||||
.gpio_num = LED_B_PIN,
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.hpoint = 0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.flags.output_invert = 0
|
||||
}
|
||||
};
|
||||
|
||||
for(uint8_t i = 0; i < 3; i++) {
|
||||
ledc_channel_config(&ledc_channels[i]);
|
||||
}
|
||||
|
||||
ledc_fade_func_install(0);
|
||||
|
||||
uint32_t pwm_data[3];
|
||||
|
||||
for(;;) {
|
||||
|
||||
for(uint8_t i = 0; i < 3; i++) {
|
||||
uint32_t pwm_new;
|
||||
uint32_t delta;
|
||||
do {
|
||||
pwm_new = esp_random() / 0x80000; // 0-8191
|
||||
if(pwm_new > pwm_data[i]) delta = pwm_new - pwm_data[i];
|
||||
else delta = pwm_data[i] - pwm_new;
|
||||
} while(delta < 2 * FADE_MS);
|
||||
pwm_data[i] = pwm_new;
|
||||
}
|
||||
|
||||
for(uint8_t i = 0; i < 3; i++) {
|
||||
ledc_set_fade_with_time(ledc_channels[i].speed_mode,
|
||||
ledc_channels[i].channel, pwm_data[i], FADE_MS);
|
||||
ledc_fade_start(ledc_channels[i].speed_mode,
|
||||
ledc_channels[i].channel, LEDC_FADE_NO_WAIT);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(FADE_MS));
|
||||
}
|
||||
}
|
100
main/app_wifi.c
100
main/app_wifi.c
|
@ -1,66 +1,80 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
/* FreeRTOS */
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
/* IDF */
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
|
||||
/* LwIP */
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
static const char *TAG = "APP_WIFI";
|
||||
static const char *LOG_TAG = "APP_WIFI";
|
||||
|
||||
#define WIFI_SSID CONFIG_ESP_WIFI_SSID
|
||||
#define WIFI_PSK CONFIG_ESP_WIFI_PASSWORD
|
||||
#define WIFI_RETR CONFIG_ESP_MAXIMUM_RETRY
|
||||
#define WIFI_SSID CONFIG_APP_WIFI_SSID
|
||||
#define WIFI_PSK CONFIG_APP_WIFI_PASS
|
||||
#define WIFI_RETR CONFIG_APP_WIFI_RETR
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static uint8_t s_retry_num = 0;
|
||||
|
||||
EventGroupHandle_t g_wifi_event_group;
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
|
||||
if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if(s_retry_num < WIFI_RETR) {
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
|
||||
#if WIFI_RETR != 0
|
||||
if (s_retry_num < WIFI_RETR) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num ++;
|
||||
ESP_LOGI(TAG, "connection lost, reconnecting...");
|
||||
s_retry_num++;
|
||||
ESP_LOGI(LOG_TAG, "connection lost, reconnecting...");
|
||||
} else {
|
||||
xEventGroupSetBits(g_wifi_event_group, WIFI_FAIL_BIT);
|
||||
ESP_LOGE(TAG, "connection failed, no more retries scheduled.");
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
ESP_LOGE(LOG_TAG, "connection failed, no more retries scheduled.");
|
||||
}
|
||||
#else
|
||||
esp_wifi_connect();
|
||||
ESP_LOGI(LOG_TAG, "connection lost, reconnecting...");
|
||||
#endif
|
||||
}
|
||||
|
||||
else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
ESP_LOGI(TAG, "got IPv4 address: "IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(g_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
else if (event_base == IP_EVENT) {
|
||||
if (event_id == IP_EVENT_STA_GOT_IP) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
|
||||
else if(event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
|
||||
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
|
||||
ESP_LOGI(TAG, "got IPv6 address: "IPV6STR, IPV62STR(event->ip6_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(g_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
ip_event_got_ip_t *event = event_data;
|
||||
ESP_LOGI(LOG_TAG, "got IPv4 address: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
}
|
||||
|
||||
if (event_id == IP_EVENT_GOT_IP6) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
|
||||
ip_event_got_ip6_t *event = event_data;
|
||||
ESP_LOGI(LOG_TAG, "got IPv6 address: " IPV6STR, IPV62STR(event->ip6_info.ip));
|
||||
s_retry_num = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_wifi_init(void) {
|
||||
g_wifi_event_group = xEventGroupCreate();
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
@ -71,23 +85,27 @@ void app_wifi_init(void) {
|
|||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL, &instance_got_ip));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_GOT_IP6, &_wifi_event_handler, NULL, &instance_got_ip));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL,
|
||||
&instance_got_ip));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_GOT_IP6, &_wifi_event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PSK,
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.capable = true,
|
||||
.required = false
|
||||
.sta =
|
||||
{
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PSK,
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {.capable = true, .required = false},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
|
||||
void app_wifi_wait_connected(uint32_t timeout_ticks) {
|
||||
}
|
35
main/main.c
35
main/main.c
|
@ -1,46 +1,37 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
/* FreeRTOS */
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
/* IDF */
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "spi_flash_mmap.h"
|
||||
|
||||
static const char *TAG = "APP_MAIN";
|
||||
static const char *LOG_TAG = "APP_MAIN";
|
||||
|
||||
extern EventGroupHandle_t g_wifi_event_group;
|
||||
extern EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
void app_wifi_init(void);
|
||||
|
||||
// Tasks
|
||||
extern TaskHandle_t xTaskDiscoLEDHandle;
|
||||
void vTaskDiscoLED(void *pvParameters);
|
||||
|
||||
void app_main(void) {
|
||||
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
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);
|
||||
|
||||
printf("Playground started.\r\n");
|
||||
ESP_LOGI(LOG_TAG, "Playground started.\r\n");
|
||||
|
||||
app_wifi_init();
|
||||
EventBits_t bits = xEventGroupWaitBits(g_wifi_event_group, 0x03, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
if(bits & 0x01) {
|
||||
ESP_LOGI(TAG, "WiFi connected.");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "WiFi failed.");
|
||||
}
|
||||
|
||||
xTaskCreate(vTaskDiscoLED, "vTaskDisco", 1024, NULL, 3, &xTaskDiscoLEDHandle);
|
||||
|
||||
for(;;) {
|
||||
for (;;) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue