mirror of
https://github.com/gozfree/gear-lib.git
synced 2026-02-06 02:54:22 +08:00
[esp32] update demo with button, led and camera
This commit is contained in:
8
build/esp32-env/TODO
Normal file
8
build/esp32-env/TODO
Normal file
@@ -0,0 +1,8 @@
|
||||
1.add libcmd, support shell on eps32
|
||||
2.fix libhomekit on linux
|
||||
3.dump esp32 camera media file to host via uart or webserver
|
||||
|
||||
supported:
|
||||
gpio button interrupt ok
|
||||
gpio led ok
|
||||
camera capture ok
|
||||
@@ -3,21 +3,30 @@
|
||||
#include <libmedia-io.h>
|
||||
#include <libavcap.h>
|
||||
#include <libfile.h>
|
||||
#include <libhal.h>
|
||||
//#include <libdarray.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_log.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <esp_event_legacy.h>
|
||||
#include <driver/gpio.h>
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#define OUTPUT_V4L2 "v4l2_640_480.jpg"
|
||||
|
||||
#define GPIO_LED_RED 21
|
||||
#define GPIO_LED_WHITE 22
|
||||
#define GPIO_BUTTON 15
|
||||
#define GPIO_BOOT 0
|
||||
|
||||
#define TAG "esp32"
|
||||
static struct file *fp;
|
||||
#if 0
|
||||
static void *thread(void *arg)
|
||||
{
|
||||
logi("thread\n");
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int on_video(struct avcap_ctx *c, struct video_frame *video)
|
||||
{
|
||||
@@ -53,7 +62,7 @@ static int on_frame(struct avcap_ctx *c, struct media_frame *frm)
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#if 1
|
||||
|
||||
static void cam_test()
|
||||
{
|
||||
struct avcap_config conf = {
|
||||
@@ -80,10 +89,110 @@ static void cam_test()
|
||||
file_close(fp);
|
||||
avcap_close(avcap);
|
||||
}
|
||||
#endif
|
||||
|
||||
static xQueueHandle gpio_evt_queue = NULL;
|
||||
static int g_wifi_state = SYSTEM_EVENT_WIFI_READY;
|
||||
|
||||
static void IRAM_ATTR gpio_isr_handler(void* arg)
|
||||
{
|
||||
uint32_t gpio_num = (uint32_t) arg;
|
||||
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
||||
}
|
||||
|
||||
static void button_task(void* arg)
|
||||
{
|
||||
uint32_t io_num;
|
||||
for(;;) {
|
||||
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
||||
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void white_led_task(void *arg)
|
||||
{
|
||||
while(1) {
|
||||
switch (g_wifi_state) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
gpio_set_level(GPIO_LED_WHITE, 0);
|
||||
usleep(300*1000);
|
||||
gpio_set_level(GPIO_LED_WHITE, 1);
|
||||
usleep(300*1000);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
gpio_set_level(GPIO_LED_WHITE, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
usleep(10*1000);
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_button_init()
|
||||
{
|
||||
gpio_config_t gpio_conf = {0};
|
||||
|
||||
gpio_conf.pin_bit_mask = 1ULL << GPIO_BUTTON | 1ULL << GPIO_BOOT;
|
||||
gpio_conf.mode = GPIO_MODE_INPUT;
|
||||
gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
gpio_conf.intr_type = GPIO_INTR_ANYEDGE;
|
||||
ESP_ERROR_CHECK(gpio_config(&gpio_conf));
|
||||
|
||||
//ESP_ERROR_CHECK(gpio_set_intr_type(GPIO_BUTTON, GPIO_INTR_ANYEDGE));
|
||||
//ESP_ERROR_CHECK(gpio_set_intr_type(GPIO_BOOT, GPIO_INTR_ANYEDGE));
|
||||
ESP_ERROR_CHECK(gpio_install_isr_service(0));
|
||||
gpio_isr_handler_add(GPIO_BUTTON, gpio_isr_handler, (void *)GPIO_BUTTON);
|
||||
gpio_isr_handler_add(GPIO_BOOT, gpio_isr_handler, (void *)GPIO_BOOT);
|
||||
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
|
||||
xTaskCreate(button_task, "button_task", 2048, NULL, 10, NULL);
|
||||
}
|
||||
|
||||
void gpio_led_init()
|
||||
{
|
||||
gpio_config_t gpio_conf;
|
||||
gpio_conf.mode = GPIO_MODE_OUTPUT;
|
||||
gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
gpio_conf.pin_bit_mask = 1LL << GPIO_LED_RED;
|
||||
gpio_config(&gpio_conf);
|
||||
gpio_conf.pin_bit_mask = 1LL << GPIO_LED_WHITE;
|
||||
gpio_config(&gpio_conf);
|
||||
xTaskCreate(white_led_task, "white_led_task", 2048, NULL, 10, NULL);
|
||||
}
|
||||
|
||||
static void wifi_event(void *ctx)
|
||||
{
|
||||
system_event_t *event = (system_event_t *)ctx;
|
||||
g_wifi_state = event->event_id;
|
||||
switch(event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
ESP_LOGI(TAG, "STA start blink");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
ESP_LOGI(TAG, "STA GOT IP: %s",
|
||||
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
ESP_LOGI(TAG, "STA disconnected, retry...");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
ESP_LOGI(TAG, "STA connected");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGI(TAG, "unknown system event %d", event->event_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
logi("%s:%d hello world!\n", __func__, __LINE__);
|
||||
gpio_button_init();
|
||||
gpio_led_init();
|
||||
wifi_setup(CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD, wifi_event);
|
||||
if (0)
|
||||
cam_test();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS = . ./libposix ./libthread ./libdarray ./libmedia-io ./libqueue ./liblog ./libavcap ./libfile
|
||||
COMPONENT_SRCDIRS = libposix liblog libdarray libqueue libthread libmedia-io librtmpc libgevent libavcap libfile
|
||||
COMPONENT_OBJEXCLUDE = libposix/libposix4win.o libposix/libposix4nix.o libposix/libposix4rtthread.o libposix/test_libposix.o libfile/test_libfile.o libfile/filewatcher.o \
|
||||
COMPONENT_ADD_INCLUDEDIRS = . ./libposix ./libthread ./libdarray ./libmedia-io ./libqueue ./liblog ./libavcap ./libfile ./libhal
|
||||
COMPONENT_SRCDIRS = libposix liblog libdarray libqueue libthread libmedia-io librtmpc libgevent libavcap libfile libhal
|
||||
COMPONENT_OBJEXCLUDE = libposix/libposix4win.o libposix/libposix4nix.o libposix/libposix4rtthread.o libposix/test_libposix.o \
|
||||
libavcap/dshow.o libavcap/xcbgrab.o libavcap/v4l2.o libavcap/test_libavcap.o libavcap/pulseaudio.o libavcap/uvc.o \
|
||||
libfile/test_libfile.o libfile/filewatcher.o \
|
||||
libdarray/test_libdarray.o libqueue/test_libqueue.o libthread/test_libthread.o libmedia-io/test_libmedia-io.o \
|
||||
librtmpc/test_librtmpc.o liblog/test_liblog.o libgevent/iocp.o libgevent/epoll.o libgevent/wepoll.o \
|
||||
libavcap/dshow.o libavcap/xcbgrab.o libavcap/v4l2.o libavcap/test_libavcap.o libavcap/pulseaudio.o libavcap/uvc.o
|
||||
libhal/hal_nix.o libhal/hal_win.o libhal/test_hal.o
|
||||
COMPONENT_DEPENDS := newlib
|
||||
CFLAGS += -DFREERTOS -DESP32 #for libposix
|
||||
CFLAGS += -DNO_CRYPTO #for librtmpc
|
||||
|
||||
@@ -3,4 +3,6 @@ This is a Hardware Abstraction Layer library.
|
||||
|
||||
* support open/read/write/close api to I2C/SPI/UART/SDIO/...
|
||||
|
||||
* get wifi ssid need root
|
||||
|
||||
* sdcard/network information
|
||||
|
||||
57
gear-lib/libhal/hal_esp32.c
Normal file
57
gear-lib/libhal/hal_esp32.c
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
#include "libhal.h"
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_err.h>
|
||||
#include <nvs_flash.h>
|
||||
|
||||
#define TAG "esp32"
|
||||
|
||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
wifi_event_cb_t event_cb = (wifi_event_cb_t)ctx;
|
||||
switch(event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
event_cb((void *)event);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void wifi_setup(const char *ssid, const char *pswd, wifi_event_cb_t event_cb)
|
||||
{
|
||||
wifi_config_t wifi_cfg;
|
||||
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_ERROR_CHECK( nvs_flash_erase() );
|
||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||
}
|
||||
|
||||
tcpip_adapter_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, event_cb));
|
||||
|
||||
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
|
||||
memset(&wifi_cfg, 0, sizeof(wifi_config_t));
|
||||
strncpy((char *)wifi_cfg.sta.ssid, ssid, sizeof(wifi_cfg.sta.ssid));
|
||||
strncpy((char *)wifi_cfg.sta.password, pswd, sizeof(wifi_cfg.sta.password));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_setup finished.");
|
||||
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
|
||||
(char *)wifi_cfg.sta.ssid, (char *)wifi_cfg.sta.password);
|
||||
}
|
||||
@@ -39,6 +39,9 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <linux/if.h>
|
||||
#include <linux/wireless.h>
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
#define is_str_equal(a,b) \
|
||||
((strlen(a) == strlen(b)) && (0 == strcasecmp(a,b)))
|
||||
@@ -117,7 +120,6 @@ static ssize_t file_read_path(const char *path, void *data, size_t len)
|
||||
|
||||
int network_get_info(const char *interface, struct network_info *info)
|
||||
{
|
||||
#if 0
|
||||
int ret = -1;
|
||||
int fd = 0;
|
||||
do {
|
||||
@@ -132,6 +134,7 @@ int network_get_info(const char *interface, struct network_info *info)
|
||||
char net_dev[128] = {0};
|
||||
snprintf(net_dev, sizeof(net_dev), "%s/%s", SYS_CLASS_NET, interface);
|
||||
if (-1 == access(net_dev, F_OK|W_OK|R_OK)) {
|
||||
printf("access %s failed: %d\n", net_dev, errno);
|
||||
info->is_probed = false;
|
||||
break;
|
||||
} else {
|
||||
@@ -166,6 +169,7 @@ int network_get_info(const char *interface, struct network_info *info)
|
||||
if (-1 == ioctl(fd, SIOCGIWESSID, &wreq)) {
|
||||
//printf("ioctl SIOCGIWESSID failed: %s\n", strerror(errno));
|
||||
}
|
||||
printf("ssid = %s\n", (char *)wreq.u.essid.pointer);
|
||||
|
||||
//ipaddr macaddr
|
||||
struct ifconf ifc;
|
||||
@@ -210,8 +214,6 @@ int network_get_info(const char *interface, struct network_info *info)
|
||||
close(fd);
|
||||
}
|
||||
return ret;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sdcard_get_info(const char *mount_point, struct sdcard_info *info)
|
||||
|
||||
@@ -87,9 +87,17 @@ struct network_ports {
|
||||
uint16_t udp_cnt;
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* network
|
||||
******************************************************************************/
|
||||
|
||||
int network_get_info(const char *inf, struct network_info *info);
|
||||
int network_get_port_occupied(struct network_ports *ports);
|
||||
|
||||
typedef void (*wifi_event_cb_t)(void *ctx);
|
||||
void wifi_setup(const char *ssid, const char *pswd, wifi_event_cb_t event_cb);
|
||||
|
||||
|
||||
int sdcard_get_info(const char *mount_point, struct sdcard_info *info);
|
||||
int cpu_get_info(struct cpu_info *info);
|
||||
int memory_get_info(struct memory_info *info);
|
||||
|
||||
Reference in New Issue
Block a user