增加状态管理
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c"
|
||||
idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c"
|
||||
PRIV_REQUIRES spi_flash esp_driver_i2c log
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "iic.h"
|
||||
#include "oled.h"
|
||||
#include "state.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
@@ -45,11 +46,11 @@ void app_main(void)
|
||||
|
||||
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
|
||||
|
||||
state_init();
|
||||
iic_init();
|
||||
oled_init();
|
||||
|
||||
while(1){
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "iic.h"
|
||||
#include "state.h"
|
||||
#include "driver/i2c_master.h"
|
||||
#include "esp_log.h"
|
||||
#include <stdint.h>
|
||||
@@ -36,6 +37,7 @@ esp_err_t iic_init(void)
|
||||
return ret;
|
||||
}
|
||||
ESP_LOGI(TAG, "IIC IO总线初始化成功");
|
||||
iic_scan();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -63,3 +65,22 @@ unsigned char iic_read_byte(unsigned char addr)
|
||||
i2c_master_receive(s_iic_dev, &byte, 1, IIC_TIMEOUT_MS);
|
||||
return (unsigned char)byte;
|
||||
}
|
||||
|
||||
void iic_scan(void)
|
||||
{
|
||||
if (s_iic_bus == NULL) {
|
||||
ESP_LOGE(TAG, "scan: bus not initialized");
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "IIC bus scan start");
|
||||
state_clear_iic_devices();
|
||||
unsigned char count = 0;
|
||||
for (unsigned char addr = 0x01; addr <= 0x7F; addr++) {
|
||||
if (i2c_master_probe(s_iic_bus, addr, IIC_SCAN_TIMEOUT_MS) == ESP_OK) {
|
||||
ESP_LOGI(TAG, " found @ 0x%02X", (unsigned)addr);
|
||||
state_add_iic_device(addr);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "IIC scan done, %u device(s) found", (unsigned)count);
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
#define IIC_SCL_IO 1
|
||||
#define IIC_FREQ_HZ 100000
|
||||
#define IIC_TIMEOUT_MS 1000
|
||||
#define IIC_SCAN_TIMEOUT_MS 50
|
||||
|
||||
esp_err_t iic_init(void);
|
||||
void iic_scan(void);
|
||||
esp_err_t iic_send_bytes(unsigned char addr, unsigned char *txd, unsigned int len);
|
||||
unsigned char iic_read_byte(unsigned char addr);
|
||||
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
#include "state.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_log.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static const char *TAG = "STATE";
|
||||
|
||||
static SemaphoreHandle_t s_mutex = NULL;
|
||||
|
||||
static system_state_t s_state = {
|
||||
.battery_percent = 0,
|
||||
.battery_valid = false,
|
||||
.iic_device_count = 0,
|
||||
.wifi_ssid_head = NULL,
|
||||
.wifi_ssid_count = 0,
|
||||
.wifi_connected_ssid = {0},
|
||||
};
|
||||
|
||||
static void state_lock(void)
|
||||
{
|
||||
if (s_mutex != NULL) {
|
||||
xSemaphoreTake(s_mutex, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
static void state_unlock(void)
|
||||
{
|
||||
if (s_mutex != NULL) {
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void state_init(void)
|
||||
{
|
||||
s_mutex = xSemaphoreCreateMutex();
|
||||
if (s_mutex == NULL) {
|
||||
ESP_LOGE(TAG, "互斥锁创建失败");
|
||||
}
|
||||
}
|
||||
|
||||
void state_set_battery(uint8_t percent)
|
||||
{
|
||||
if (percent > 100) {
|
||||
percent = 100;
|
||||
}
|
||||
state_lock();
|
||||
s_state.battery_percent = percent;
|
||||
s_state.battery_valid = true;
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
uint8_t state_get_battery(void)
|
||||
{
|
||||
state_lock();
|
||||
uint8_t val = s_state.battery_percent;
|
||||
state_unlock();
|
||||
return val;
|
||||
}
|
||||
|
||||
void state_clear_iic_devices(void)
|
||||
{
|
||||
state_lock();
|
||||
s_state.iic_device_count = 0;
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
void state_add_iic_device(uint8_t addr)
|
||||
{
|
||||
state_lock();
|
||||
if (s_state.iic_device_count < STATE_IIC_MAX_DEVICES) {
|
||||
s_state.iic_devices[s_state.iic_device_count] = addr;
|
||||
s_state.iic_device_count++;
|
||||
}
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
uint8_t state_get_iic_device_count(void)
|
||||
{
|
||||
state_lock();
|
||||
uint8_t count = s_state.iic_device_count;
|
||||
state_unlock();
|
||||
return count;
|
||||
}
|
||||
|
||||
uint8_t state_get_iic_device(uint8_t index)
|
||||
{
|
||||
state_lock();
|
||||
uint8_t addr = 0;
|
||||
if (index < s_state.iic_device_count) {
|
||||
addr = s_state.iic_devices[index];
|
||||
}
|
||||
state_unlock();
|
||||
return addr;
|
||||
}
|
||||
|
||||
void state_clear_wifi_ssids(void)
|
||||
{
|
||||
state_lock();
|
||||
wifi_ssid_node_t *node = s_state.wifi_ssid_head;
|
||||
while (node != NULL) {
|
||||
wifi_ssid_node_t *next = node->next;
|
||||
free(node);
|
||||
node = next;
|
||||
}
|
||||
s_state.wifi_ssid_head = NULL;
|
||||
s_state.wifi_ssid_count = 0;
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
esp_err_t state_add_wifi_ssid(const char *ssid)
|
||||
{
|
||||
if (ssid == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
wifi_ssid_node_t *node = (wifi_ssid_node_t *)malloc(sizeof(wifi_ssid_node_t));
|
||||
if (node == NULL) {
|
||||
ESP_LOGE(TAG, "WiFi SSID节点分配失败");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
strncpy(node->ssid, ssid, STATE_WIFI_SSID_MAX_LEN);
|
||||
node->ssid[STATE_WIFI_SSID_MAX_LEN] = '\0';
|
||||
node->next = NULL;
|
||||
|
||||
state_lock();
|
||||
if (s_state.wifi_ssid_head == NULL) {
|
||||
s_state.wifi_ssid_head = node;
|
||||
} else {
|
||||
wifi_ssid_node_t *tail = s_state.wifi_ssid_head;
|
||||
while (tail->next != NULL) {
|
||||
tail = tail->next;
|
||||
}
|
||||
tail->next = node;
|
||||
}
|
||||
s_state.wifi_ssid_count++;
|
||||
state_unlock();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
uint8_t state_get_wifi_ssid_count(void)
|
||||
{
|
||||
state_lock();
|
||||
uint8_t count = s_state.wifi_ssid_count;
|
||||
state_unlock();
|
||||
return count;
|
||||
}
|
||||
|
||||
const wifi_ssid_node_t *state_wifi_ssids_lock(void)
|
||||
{
|
||||
state_lock();
|
||||
return s_state.wifi_ssid_head;
|
||||
}
|
||||
|
||||
void state_wifi_ssids_unlock(void)
|
||||
{
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
void state_set_wifi_connected_ssid(const char *ssid)
|
||||
{
|
||||
state_lock();
|
||||
if (ssid != NULL) {
|
||||
strncpy(s_state.wifi_connected_ssid, ssid, STATE_WIFI_SSID_MAX_LEN);
|
||||
s_state.wifi_connected_ssid[STATE_WIFI_SSID_MAX_LEN] = '\0';
|
||||
} else {
|
||||
s_state.wifi_connected_ssid[0] = '\0';
|
||||
}
|
||||
state_unlock();
|
||||
}
|
||||
|
||||
const char *state_get_wifi_connected_ssid(void)
|
||||
{
|
||||
state_lock();
|
||||
const char *ssid = s_state.wifi_connected_ssid;
|
||||
state_unlock();
|
||||
return ssid;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef STATE_H_
|
||||
#define STATE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
#define STATE_IIC_MAX_DEVICES 16
|
||||
#define STATE_WIFI_SSID_MAX_LEN 32
|
||||
|
||||
typedef struct wifi_ssid_node {
|
||||
char ssid[STATE_WIFI_SSID_MAX_LEN + 1];
|
||||
struct wifi_ssid_node *next;
|
||||
} wifi_ssid_node_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t battery_percent;
|
||||
bool battery_valid;
|
||||
|
||||
uint8_t iic_devices[STATE_IIC_MAX_DEVICES];
|
||||
uint8_t iic_device_count;
|
||||
|
||||
wifi_ssid_node_t *wifi_ssid_head;
|
||||
uint8_t wifi_ssid_count;
|
||||
char wifi_connected_ssid[STATE_WIFI_SSID_MAX_LEN + 1];
|
||||
} system_state_t;
|
||||
|
||||
void state_init(void);
|
||||
|
||||
void state_set_battery(uint8_t percent);
|
||||
uint8_t state_get_battery(void);
|
||||
|
||||
void state_clear_iic_devices(void);
|
||||
void state_add_iic_device(uint8_t addr);
|
||||
uint8_t state_get_iic_device_count(void);
|
||||
uint8_t state_get_iic_device(uint8_t index);
|
||||
|
||||
void state_clear_wifi_ssids(void);
|
||||
esp_err_t state_add_wifi_ssid(const char *ssid);
|
||||
uint8_t state_get_wifi_ssid_count(void);
|
||||
const wifi_ssid_node_t *state_wifi_ssids_lock(void);
|
||||
void state_wifi_ssids_unlock(void);
|
||||
void state_set_wifi_connected_ssid(const char *ssid);
|
||||
const char *state_get_wifi_connected_ssid(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user