mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 17:35:54 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(uart_select)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := uart_select
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,68 @@
|
||||
# UART Select Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
The UART select example is for demonstrating the use of `select()` for
|
||||
synchronous I/O multiplexing on the UART interface. The example waits for a
|
||||
character from UART using `select()` until a blocking read without delay or a
|
||||
successful non-blocking read is possible.
|
||||
|
||||
Please note that the same result can be achieved by using `uart_read_bytes()`
|
||||
but the use of `select()` allows to use it together with other virtual
|
||||
file system (VFS) drivers, e.g. LWIP sockets.
|
||||
|
||||
This example can be used to develop applications for non-blocking read and write from/to various sources (UART,
|
||||
sockets, ...) where a ready resource can be served without being blocked by a busy resource.
|
||||
|
||||
For a more comprehensive example please refer to `system/select`.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
The example can be run on any ESP32 development board connected to a PC with a single USB cable for communication
|
||||
through UART.
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
You can see a similar output after flashing and monitoring the application:
|
||||
|
||||
```
|
||||
...
|
||||
I (276) cpu_start: Pro cpu start user code
|
||||
I (294) cpu_start: Starting scheduler on PRO CPU.
|
||||
I (0) cpu_start: Starting scheduler on APP CPU.
|
||||
I (10295) uart_select_example: Timeout has been reached and nothing has been received
|
||||
I (15295) uart_select_example: Timeout has been reached and nothing has been received
|
||||
I (20295) uart_select_example: Timeout has been reached and nothing has been received
|
||||
```
|
||||
|
||||
You can push any key on your keyboard to see the result of a successful detection by `select()` and subsequent
|
||||
blocking read (without delay). The following output shows the result of pushing `a` on the keyboard:
|
||||
|
||||
```
|
||||
...
|
||||
I (15295) uart_select_example: Timeout has been reached and nothing has been received
|
||||
I (20295) uart_select_example: Timeout has been reached and nothing has been received
|
||||
I (20945) uart_select_example: Received: a
|
||||
I (25955) uart_select_example: Timeout has been reached and nothing has been received
|
||||
...
|
||||
```
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "uart_select_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
@@ -0,0 +1,94 @@
|
||||
/* UART Select Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/select.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_dev.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
static const char* TAG = "uart_select_example";
|
||||
|
||||
static void uart_select_task(void *arg)
|
||||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_APB,
|
||||
};
|
||||
uart_driver_install(UART_NUM_0, 2*1024, 0, 0, NULL, 0);
|
||||
uart_param_config(UART_NUM_0, &uart_config);
|
||||
|
||||
while (1) {
|
||||
int fd;
|
||||
|
||||
if ((fd = open("/dev/uart/0", O_RDWR)) == -1) {
|
||||
ESP_LOGE(TAG, "Cannot open UART");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
// We have a driver now installed so set up the read/write functions to use driver also.
|
||||
esp_vfs_dev_uart_use_driver(0);
|
||||
|
||||
while (1) {
|
||||
int s;
|
||||
fd_set rfds;
|
||||
struct timeval tv = {
|
||||
.tv_sec = 5,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
|
||||
s = select(fd + 1, &rfds, NULL, NULL, &tv);
|
||||
|
||||
if (s < 0) {
|
||||
ESP_LOGE(TAG, "Select failed: errno %d", errno);
|
||||
break;
|
||||
} else if (s == 0) {
|
||||
ESP_LOGI(TAG, "Timeout has been reached and nothing has been received");
|
||||
} else {
|
||||
if (FD_ISSET(fd, &rfds)) {
|
||||
char buf;
|
||||
if (read(fd, &buf, 1) > 0) {
|
||||
ESP_LOGI(TAG, "Received: %c", buf);
|
||||
// Note: Only one character was read even the buffer contains more. The other characters will
|
||||
// be read one-by-one by subsequent calls to select() which will then return immediately
|
||||
// without timeout.
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UART read error");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "No FD has been set in select()");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
xTaskCreate(uart_select_task, "uart_select_task", 4*1024, NULL, 5, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user