添加智能灯固件代码

This commit is contained in:
kerwincui
2021-07-13 17:14:51 +08:00
parent 332f74dd17
commit ecc0b91b8b
2568 changed files with 229441 additions and 0 deletions

View File

@@ -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(select)

View File

@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := select
include $(IDF_PATH)/make/project.mk

View File

@@ -0,0 +1,11 @@
# Synchronous I/O multiplexing example
The example demonstrates the use of synchronous I/O multiplexing by the select()
function with UART and socket file descriptors. The example starts three tasks:
1. The first task writes periodically to the UART1 file descriptor.
2. The second task writes periodically to the socket descriptor.
3. Both UART1 and the socket are configured to act as loopbacks. The third
task detects by the use of select() whether it is possible to read from
UART1 or the socket, and receives the sent messages from the other tasks.
See the README.md file in the upper level 'examples' directory for more information about examples.

View File

@@ -0,0 +1,37 @@
from __future__ import unicode_literals
from tiny_test_fw import Utility
import os
import ttfw_idf
def get_socket_msgs(i):
msg = 'Socket message S{}'.format(i)
return ['uart_select_example: {} bytes were written to socket: {}'.format(len(msg), msg),
'uart_select_example: {} bytes were received through socket: {}'.format(len(msg), msg)]
def get_uart_msgs(i):
msg = 'UART message U{}'.format(i)
return ['uart_select_example: {} bytes were sent to UART1: {}'.format(len(msg), msg),
'uart_select_example: {} bytes were received through UART1: {}'.format(len(msg), msg)]
@ttfw_idf.idf_example_test(env_tag='Example_GENERIC')
def test_examples_select(env, extra_data):
dut = env.get_dut('select', 'examples/system/select')
dut.start_app()
dut.expect('cpu_start: Starting scheduler', timeout=30)
exp_list = []
for i in range(1, 10):
exp_list += get_socket_msgs(i)
exp_list += get_uart_msgs(i)
Utility.console_log('Expecting:{}{}'.format(os.linesep, os.linesep.join(exp_list)))
dut.expect_all(*exp_list, timeout=60)
if __name__ == '__main__':
test_examples_select()

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "select_example.c"
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

View File

@@ -0,0 +1,207 @@
/* 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/param.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"
#include "esp_netif.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
static const char* TAG = "uart_select_example";
static int uart_fd = -1;
static int socket_fd = -1;
static void socket_deinit(void)
{
close(socket_fd);
socket_fd = -1;
}
static void socket_init(void)
{
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM,
};
struct addrinfo *res;
int err;
struct sockaddr_in saddr = { 0 };
ESP_ERROR_CHECK(esp_netif_init());
err = getaddrinfo("localhost", "80", &hints, &res);
if (err != 0 || res == NULL) {
ESP_LOGE(TAG, "DNS lookup failed: %d", errno);
return;
}
socket_fd = socket(res->ai_family, res->ai_socktype, 0);
if (socket_fd < 0) {
ESP_LOGE(TAG, "Failed to allocate socket.");
freeaddrinfo(res);
return;
}
saddr.sin_family = PF_INET;
saddr.sin_port = htons(80);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
err = bind(socket_fd, (struct sockaddr *) &saddr, sizeof(struct sockaddr_in));
if (err < 0) {
ESP_LOGE(TAG, "Failed to bind socket");
freeaddrinfo(res);
socket_deinit();
return;
}
if (connect(socket_fd, res->ai_addr, res->ai_addrlen) != 0) {
ESP_LOGE(TAG, "Socket connection failed: %d", errno);
freeaddrinfo(res);
socket_deinit();
return;
}
freeaddrinfo(res);
}
static void uart1_deinit(void)
{
close(uart_fd);
uart_fd = -1;
uart_driver_delete(UART_NUM_1);
}
static void uart1_init(void)
{
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_1, 256, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &uart_config);
uart_set_loop_back(UART_NUM_1, true);
if ((uart_fd = open("/dev/uart/1", O_RDWR | O_NONBLOCK)) == -1) {
ESP_LOGE(TAG, "Cannot open UART1");
uart1_deinit();
}
esp_vfs_dev_uart_use_driver(1);
}
static void uart1_write_task(void *param)
{
char buf[20];
uart1_init();
for (uint8_t i = 1;; ++i) {
vTaskDelay(4000 / portTICK_PERIOD_MS);
snprintf(buf, sizeof(buf), "UART message U%d", i);
int write_bytes = write(uart_fd, buf, strlen(buf));
if (write_bytes < 0) {
ESP_LOGE(TAG, "UART1 write error");
} else {
ESP_LOGI(TAG, "%d bytes were sent to UART1: %s", write_bytes, buf);
}
}
uart1_deinit();
vTaskDelete(NULL);
}
static void socket_write_task(void *param)
{
char buf[20];
socket_init();
for (uint8_t i = 1;; ++i) {
vTaskDelay(3000 / portTICK_PERIOD_MS);
snprintf(buf, sizeof(buf), "Socket message S%d", i);
int write_bytes = write(socket_fd, buf, strlen(buf));
if (write_bytes < 0) {
ESP_LOGE(TAG, "Socket write error");
} else {
ESP_LOGI(TAG, "%d bytes were written to socket: %s", write_bytes, buf);
}
}
socket_deinit();
vTaskDelete(NULL);
}
static void check_and_print(int fd, const fd_set *rfds, const char *src_msg)
{
char buf[100];
int read_bytes;
if (FD_ISSET(fd, rfds)) {
if ((read_bytes = read(fd, buf, sizeof(buf)-1)) > 0) {
buf[read_bytes] = '\0';
ESP_LOGI(TAG, "%d bytes were received through %s: %s", read_bytes, src_msg, buf);
} else {
ESP_LOGE(TAG, "%s read error", src_msg);
}
}
}
static void select_task(void *param)
{
while (1) {
int s;
fd_set rfds;
struct timeval tv = {
.tv_sec = 1,
.tv_usec = 0,
};
FD_ZERO(&rfds);
FD_SET(uart_fd, &rfds);
FD_SET(socket_fd, &rfds);
s = select(MAX(uart_fd, socket_fd) + 1, &rfds, NULL, NULL, &tv);
if (s < 0) {
ESP_LOGE(TAG, "Select failed: errno %d", errno);
} else if (s == 0) {
ESP_LOGI(TAG, "Timeout has been reached and nothing has been received");
} else {
check_and_print(socket_fd, &rfds, "socket");
check_and_print(uart_fd, &rfds, "UART1");
}
}
vTaskDelete(NULL);
}
void app_main(void)
{
xTaskCreate(uart1_write_task, "uart1_write_task", 4*1024, NULL, 5, NULL);
xTaskCreate(socket_write_task, "socket_write_task", 4*1024, NULL, 5, NULL);
vTaskDelay(1000 / portTICK_PERIOD_MS);
xTaskCreate(select_task, "select_task", 4*1024, NULL, 5, NULL);
}