esp32 联网,控制WS2812 彩灯的工作模式

#include <stdio.h>

#include <string.h>

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "freertos/event_groups.h"

#include "freertos/queue.h"

#include "driver/rmt.h"

#include "esp_system.h"

#include "esp_wifi.h"

#include "esp_event.h"

#include "esp_log.h"

#include "esp_http_client.h"

#include "esp_tls.h"

#include "nvs_flash.h"

#include "lwip/err.h"

#include "lwip/sys.h"



 

// 模式0:所有LED显示相同颜色循环

// 模式1:流水灯效果(绿色)

// 模式2:彩虹效果

// 模式3:呼吸灯效果(红色)

// 模式4:彩色循环流水灯

// ========== LED配置 ==========     // s3 38, esp32 16

#define RMT_TX_CHANNEL RMT_CHANNEL_0

#define RMT_GPIO_PIN GPIO_NUM_16

#define LED_COUNT 5

// RMT 时钟源为 80MHz,分频后得到 RMT tick 频率

#define RMT_CLK_DIV 2

#define RMT_TICK_10NS (80000000 / 100000000)

// WS2812 时序定义(单位:纳秒)

#define T0H 350

#define T0L 900

#define T1H 900

#define T1L 350

// 将纳秒转换为 RMT tick 数(向上取整)

#define NS_TO_TICKS(ns) (((ns) * 80 / 1000 + (RMT_CLK_DIV - 1)) / RMT_CLK_DIV)

// 计算 duration(以 RMT ticks 为单位)

#define DURATION_0H NS_TO_TICKS(T0H)

#define DURATION_0L NS_TO_TICKS(T0L)

#define DURATION_1H NS_TO_TICKS(T1H)

#define DURATION_1L NS_TO_TICKS(T1L)

// ========== WiFi配置 ==========

#define EXAMPLE_ESP_WIFI_SSID "ChinaNet-S5u8"

#define EXAMPLE_ESP_WIFI_PASS "2e2w1i68"

#define EXAMPLE_ESP_MAXIMUM_RETRY 5

// 证书声明

// extern const char newkaiyuan_root_cert_pem_start[] asm("_binary_newkaiyuan_root_cert_pem_start");

// extern const char newkaiyuan_root_cert_pem_end[] asm("_binary_newkaiyuan_root_cert_pem_end");

////////////////////必须放在和.c文件同级的目录下,可以到申请https证书的地方,去导出证书。必须///pem格式的, 保存名字格式为 ccc2024_root_cert_pem ,才能对应下边的变量

extern const char ddd2024_root_cert_pem_start[] asm("_binary_ccc2024_root_cert_pem_start");

extern const char ddd2024_root_cert_pem_end[] asm("_binary_ccc2024_root_cert_pem_end");

// 事件组和标签

static EventGroupHandle_t s_wifi_event_group;

#define WIFI_CONNECTED_BIT BIT0

#define WIFI_FAIL_BIT BIT1

static const char *TAG = "WS2812_WiFi";

// 全局变量

static int s_retry_num = 0;

static int led_mode = 0; // LED工作模式,由HTTP响应决定

static int led_position = 0;

static int led_myposition = 0;

static uint8_t led_hue = 0;

// 队列和任务

QueueHandle_t xHttpRequestQueue = NULL;

typedef enum {

    DO_HTTPS_REQUEST

} http_command_t;

// LED像素结构

typedef struct {

    uint8_t r;

    uint8_t g;

    uint8_t b;

} ws2812_pixel_t;

// 颜色定义

const ws2812_pixel_t RED = {.r = 255, .g = 0, .b = 0};

const ws2812_pixel_t GREEN = {.r = 0, .g = 255, .b = 0};

const ws2812_pixel_t BLUE = {.r = 0, .g = 0, .b = 255};

const ws2812_pixel_t YELLOW = {.r = 255, .g = 255, .b = 0};

const ws2812_pixel_t PURPLE = {.r = 128, .g = 0, .b = 128};

const ws2812_pixel_t CYAN = {.r = 0, .g = 255, .b = 255};

const ws2812_pixel_t WHITE = {.r = 255, .g = 255, .b = 255};

const ws2812_pixel_t OFF = {.r = 0, .g = 0, .b = 0};

// 函数声明

void ws2812_init();

void ws2812_send_pixels(ws2812_pixel_t *pixels, size_t count);

void set_all_leds(ws2812_pixel_t color, ws2812_pixel_t *leds);

void set_single_led(int index, ws2812_pixel_t color, ws2812_pixel_t *leds);

ws2812_pixel_t wheel(uint8_t wheel_pos);

//void https_get_request(void);

void https_task(void *pvParameters);

void wifi_init_sta(void);

static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);

static void https_get_request(void);

// WS2812初始化

void ws2812_init() {

    rmt_config_t config = {

        .rmt_mode = RMT_MODE_TX,

        .channel = RMT_TX_CHANNEL,

        .gpio_num = RMT_GPIO_PIN,

        .clk_div = RMT_CLK_DIV,

        .mem_block_num = 1,

        .tx_config = {

            .loop_en = false,

            .carrier_freq_hz = 0,

            .carrier_duty_percent = 0,

            .carrier_level = RMT_CARRIER_LEVEL_LOW,

            .carrier_en = false,

            .idle_level = RMT_IDLE_LEVEL_LOW,

        },

        .flags = {0},

    };

    ESP_ERROR_CHECK(rmt_config(&config));

    ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));

}

// 发送像素数据到WS2812

void ws2812_send_pixels(ws2812_pixel_t *pixels, size_t count) {

    size_t items_size = count * 24 * sizeof(rmt_item32_t);

    rmt_item32_t *items = (rmt_item32_t *)calloc(items_size, 1);

    if (!items) {

        ESP_LOGE(TAG, "Failed to allocate memory");

        return;

    }

    for (int i = 0; i < count; i++) {

        uint8_t data[3] = {pixels[i].g, pixels[i].r, pixels[i].b}; // WS2812 是 GRB 顺序

        for (int j = 0; j < 3; j++) {

            uint8_t byte = data[j];

            for (int b = 0; b < 8; b++) {

                int bit = (byte >> (7 - b)) & 1;

                rmt_item32_t *item = &items[i * 24 + j * 8 + b];

                if (bit) {

                    item->level0 = 1;

                    item->duration0 = DURATION_1H;

                    item->level1 = 0;</

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值