发帖
1 0 0

【Ai-WB2-32S 测评】web RGB 灯

大猫的鱼
论坛元老

19

主题

385

回帖

6491

积分

论坛元老

积分
6491
Ai-WB2系列 57 1 前天 18:32

前段时间申请的Ai-WB2-32S Kit到了,本次主要测评他的PWM功能,另外单独测试gpio太单调了,所以加上http server,做了一个网页调色的RGB灯。

1、硬件部分

硬件就是Ai-WB2-32S Kit板载的RGB灯。主要弄清楚RGB对应的引脚和PWM通道就行。由下图可见

R -- IO14 -- PWMCh4

G -- IO17 -- PWMCh2

B -- IO3 -- PWMCh3

1.png

2.png

2、软件部分

软件部分整个工作流程就是:

<1>设备初始化,STA模式连接WIFI

<2>连接成功后,PWM初始化,开始http任务

<3>等待网页端的post请求

<4>解析rgb值,并修改RGB占空比输出

网页端效果图(背景是动态变化的)如下:

3.png

pwm.c

#include "pwm.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bl_gpio.h"
#include "bl_pwm.h"

uint8_t LED_R = 0;
uint8_t LED_G = 0;
uint8_t LED_B = 0;

#define R_PWMCHx    4
#define G_PWMCHx    2
#define B_PWMCHx    3

void Led_Init(void)
{
    bl_pwm_init(B_PWMCHx, 3, 5000);
    bl_pwm_stop(B_PWMCHx);
    bl_pwm_init(R_PWMCHx, 14, 5000);
    bl_pwm_stop(R_PWMCHx);
    bl_pwm_init(G_PWMCHx, 17, 5000);
    bl_pwm_stop(G_PWMCHx);
}

void Set_RGB(uint8_t r,uint8_t g,uint8_t b)
{
    bl_pwm_stop(R_PWMCHx);
    bl_pwm_stop(G_PWMCHx);
    bl_pwm_stop(B_PWMCHx);
  
    bl_pwm_set_duty(R_PWMCHx, r * 100 / 255);
    bl_pwm_set_duty(G_PWMCHx, g * 100 / 255);
    bl_pwm_set_duty(B_PWMCHx, b * 100 / 255);

    bl_pwm_start(R_PWMCHx);
    bl_pwm_start(G_PWMCHx);
    bl_pwm_start(B_PWMCHx);
}


void Get_RGB(char *buf, uint8_t *r, uint8_t *g, uint8_t *b) {
    // 创建可修改的副本
    char *copy = strdup(buf);
    if (copy == NULL) {
        perror("内存分配失败");
    }
  
    // 检查并移除前缀"rgb("和后缀")"
    char *start = strstr(copy, "rgb(");
    if (start != copy) {
        fprintf(stderr, "无效的RGB格式: 缺少'rgb('前缀\n");
        free(copy);
        return;
    }
  
    char *end = strrchr(copy, ')');
    if (end == NULL) {
        fprintf(stderr, "无效的RGB格式: 缺少')'后缀\n");
        free(copy);
        return;
    }
    *end = '\0'; // 终止字符串在右括号处
  
    // 移动到第一个数字位置
    char *numbers = copy + 4; // 跳过"rgb("
  
    // 使用strtok分割逗号分隔的值
    char *token = strtok(numbers, ",");
    if (token) *r = atoi(token);
  
    token = strtok(NULL, ",");
    if (token) *g = atoi(token);
  
    token = strtok(NULL, ",");
    if (token) *b = atoi(token);
  
    free(copy);
}



pwm.h

#ifndef __PWM_H_
#define __PWM_H_

#include <stdint.h>

extern uint8_t LED_R;
extern uint8_t LED_G;
extern uint8_t LED_B;


void Led_Init(void);
void Set_RGB(uint8_t r,uint8_t g,uint8_t b);

void Get_RGB(char *buf, uint8_t *r, uint8_t *g, uint8_t *b);

#endif

具体代码见附件。

3、实物演示

有兴趣做的话记得先修改 wifi ssid和password

4、附件

upload 附件:simple_server.rar

──── 0人觉得很赞 ────

使用道具 举报

赞~
您需要登录后才可以回帖 立即登录
高级模式
返回
统计信息
  • 会员数: 28861 个
  • 话题数: 41335 篇