Skip to content

Commit

Permalink
main: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
redchenjs committed Nov 10, 2020
1 parent 313676d commit ebcc1ac
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
2 changes: 0 additions & 2 deletions main/inc/user/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#ifndef INC_USER_LED_H_
#define INC_USER_LED_H_

#include <stdint.h>

typedef enum {
LED_MODE_IDX_BLINK_S1 = 0x00,
LED_MODE_IDX_BLINK_S0 = 0x01,
Expand Down
3 changes: 1 addition & 2 deletions main/src/chip/i2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ static i2s_config_t i2s_output_config = {
.use_apll = 1, // use APLL
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL3,
.tx_desc_auto_clear = true, // auto clear tx descriptor on underflow
.dma_buf_count = 8,
.dma_buf_count = 2,
.dma_buf_len = 128,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT // 2-channels
};
Expand Down
14 changes: 7 additions & 7 deletions main/src/core/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ esp_err_t app_getenv(const char *key, void *out_value, size_t *length)
{
nvs_handle_t handle;

esp_err_t err = nvs_open("storage", NVS_READONLY, &handle);
esp_err_t err = nvs_open("storage", NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "open nvs failed");
ESP_LOGE(TAG, "failed to open nvs");
return err;
}

err = nvs_get_blob(handle, key, out_value, length);
if (err == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGW(TAG, "env \"%s\" not found", key);
ESP_LOGW(TAG, "env not found: %s", key);
nvs_close(handle);
return err;
} else if (err != ESP_OK) {
ESP_LOGE(TAG, "read env \"%s\" failed", key);
ESP_LOGE(TAG, "failed to read env: %s", key);
nvs_close(handle);
return err;
}
Expand All @@ -59,20 +59,20 @@ esp_err_t app_setenv(const char *key, const void *value, size_t length)

esp_err_t err = nvs_open("storage", NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "open nvs failed");
ESP_LOGE(TAG, "failed to open nvs");
return err;
}

err = nvs_set_blob(handle, key, value, length);
if (err != ESP_OK) {
ESP_LOGE(TAG, "set nvs blob failed");
ESP_LOGE(TAG, "failed to set nvs blob");
nvs_close(handle);
return err;
}

err = nvs_commit(handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "write env \"%s\" failed", key);
ESP_LOGE(TAG, "failed to write env: %s", key);
nvs_close(handle);
return err;
}
Expand Down
17 changes: 9 additions & 8 deletions main/src/core/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include "core/os.h"
#include "chip/wifi.h"

#include "user/gui.h"
#include "user/led.h"
#include "user/ntp.h"
#include "user/key.h"
#include "user/led.h"
#include "user/gui.h"
#include "user/nfc_app.h"
#include "user/http_app_ota.h"

Expand Down Expand Up @@ -58,11 +58,14 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,
vTaskDelay(2000 / portTICK_RATE_MS);
#endif
os_pwr_reset_wait(OS_PWR_DUMMY_BIT);
break;
}

xEventGroupClearBits(wifi_event_group, WIFI_RDY_BIT);

if (!(uxBits & WIFI_CFG_BIT)) {
esp_wifi_connect();
}
xEventGroupClearBits(wifi_event_group, WIFI_RDY_BIT);
break;
}
default:
Expand All @@ -74,11 +77,11 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
switch (event_id) {
case IP_EVENT_STA_GOT_IP: {
case IP_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, WIFI_RDY_BIT);

#ifdef CONFIG_ENABLE_SC_KEY
key_set_scan_mode(KEY_SCAN_MODE_IDX_OFF);

#endif
ntp_sync_time();
#ifdef CONFIG_ENABLE_OTA
http_app_check_for_updates();
Expand All @@ -92,7 +95,6 @@ static void ip_event_handler(void* arg, esp_event_base_t event_base,
nfc_app_set_mode(NFC_APP_MODE_IDX_ON);

break;
}
default:
break;
}
Expand Down Expand Up @@ -121,7 +123,6 @@ static void sc_event_handler(void* arg, esp_event_base_t event_base,
#ifdef CONFIG_ENABLE_LED
led_set_mode(LED_MODE_IDX_PULSE_D1);
#endif

smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
Expand Down
2 changes: 1 addition & 1 deletion main/src/user/http_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "esp_http_client.h"

#include "core/os.h"
#include "user/gui.h"
#include "user/led.h"
#include "user/gui.h"
#include "user/http_app.h"
#include "user/audio_player.h"
#include "user/http_app_ota.h"
Expand Down
2 changes: 1 addition & 1 deletion main/src/user/http_app_ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

static uint32_t data_length = 0;

static const esp_partition_t *update_partition = NULL;
static esp_ota_handle_t update_handle = 0;
static const esp_partition_t *update_partition = NULL;

esp_err_t http_app_ota_event_handler(esp_http_client_event_t *evt)
{
Expand Down
4 changes: 3 additions & 1 deletion main/src/user/nfc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ static void nfc_app_task_handle(void *pvParameter)
if (res > 0) {
if (strstr((char *)rx_data, RX_FRAME_PRFX) != NULL &&
strlen((char *)rx_data + RX_FRAME_PRFX_LEN) == RX_FRAME_DATA_LEN) {
ESP_LOGW(TAG, "token is %32s", (char *)rx_data + RX_FRAME_PRFX_LEN);
ESP_LOGW(TAG, "token: %32s", (char *)rx_data + RX_FRAME_PRFX_LEN);
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
audio_player_play_file(MP3_FILE_IDX_NOTIFY);
#endif
http_app_verify_token((char *)rx_data + RX_FRAME_PRFX_LEN);
} else {
ESP_LOGW(TAG, "unexpected frame");
Expand Down
2 changes: 1 addition & 1 deletion main/src/user/ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#include "core/os.h"

#include "user/gui.h"
#include "user/led.h"
#include "user/gui.h"
#include "user/nfc_app.h"
#include "user/http_app_ota.h"

Expand Down

0 comments on commit ebcc1ac

Please sign in to comment.