普通三色LED灯的软硬件机制和接口介绍

[English]

重要

目前SDK的普通三色LED灯工程为默认工程, 其使用的 BK_MESH_DEMO_SUPPORT_PWM_LED 外部工程宏默认为y 目前SDK的普通三色LED灯工程不支持PWM! PWM工程配置与实现请参考:支持PWM的三色LED灯的软硬件机制和接口介绍

普通三色LED灯的硬件配置

SDK使用的是内部测试模组测试普通三色LED灯工程,其使用BK7236模组如下图所示:

bk7236

普通三色LED灯工程的bk7236模组

下表是SDK默认的普通三色LED灯工程的GPIO引脚配置:

颜色引脚

GPIO引脚

BK_MESH_PWM_RED_PIN

18

BK_MESH_PWM_GREEN_PIN

24

BK_MESH_PWM_BLUE_PIN

19

备注

请确保在 BK_MESH_DEMO_SUPPORT_PWM_LED 下面配置!

普通三色LED灯的软件配置

普通三色LED灯的软件配置同样遵循Vendor的软件配置,即占用Vendor Bitmap的bit0位置和Bitmap0 Content字段。 当然SDK默认的字段设计只是提供了一种实现方法,用户可以自行设计Bitmap0 Content的内容, 下面将介绍一下SDK默认的设计形式。

普通三色LED灯工程实现

demo路径:

components/demos/bk_mesh_led_demo.c

备注

普通三色LED灯工程默认支持,编译命令如下(括号内的部分可选):

make bk7236 -j8 (PROJECT=bk_mesh/3_led)

普通三色LED灯工程上使用的内部API

在demo工程中,我们只使用这些API的介绍如下:

Header File

Functions

bk_err_t bk_alarm_register(aon_rtc_id_t id, alarm_info_t *alarm_info_p)

timer API

Register an alarm to AON_RTC id timer !!! NOTES: the callback function is in ISR, so can’t block too much time. F.E:can’t call sleep/suspend; !!! !!! NOTES: freertos forbid call free/malloc in ISR which include suspend and resume. !!! @id: register to which aon_rtc id

参数

alarm_info_p – includes below info: name_p: the name of the registered alarm period_time: what’s the alarm time, milliseconds * AON_RTC_MS_TICK_CNT period_cnt: 0xFFFFFFF means always period until user unregister it, or means period how many times callback: if the alarm comes, will call this callback param_p: the param will be used for callback

返回

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_alarm_unregister(aon_rtc_id_t id, uint8_t *name_p)

Unregister an alarm from AON_RTC id timer.

Attention

1. Please don’t unregister self in the alarm’s callback.

参数
  • id – unregister the aon_rtc id alarm

  • name_p – the name of the registered alarm which will be unregistered.

返回

  • BK_OK: succeed

  • others: other errors.

Structures

struct _alarm_info_t

Public Members

uint8_t name[ALARM_NAME_MAX_LEN + 1]

TIMER NAME

uint32_t period_tick

TIMER TICK

uint32_t period_cnt

TIMER CNT, total period count == 0Xffffffff means forever period;else period how many times aon_rtc_isr_t callback; /**< TIMER CALLBACK FUNC

void *param_p

TIMER CALLBACK FUNC PARAMS

Type Definitions

typedef struct _alarm_info_t alarm_info_t

另外,对于GPIO的操作,直接使用宏函数完成:

- #define BK_MESH_GPIO_UP(id) *(volatile uint32_t*) (SOC_AON_GPIO_REG_BASE + ((id) << 2)) = 2

- #define BK_MESH_GPIO_DOWN(id) *(volatile uint32_t*) (SOC_AON_GPIO_REG_BASE + ((id) << 2)) = 0

普通三色LED灯工程使用的对外API

普通三色LED灯工程对外API接口如下:

Header File

Functions

int bk_mesh_led_demo_set_vendor_info(uint8_t *vendor_ptr, uint8_t type, uint8_t layer)

APP set LED Color and blink time.

参数
  • vendor_ptr – vendor_ptr is the pointer formatted by Vendor format,it contains the RGB value and blink time

  • type – type is not use, the parameter is retained to align with the global variable function pointer

  • layer – layer is not use, the parameter is retained to align with the global variable function pointer

返回

  • BK_OK: succeed

  • others: other errors

uint8_t *bk_mesh_led_demo_get_vendor_info(struct bk_mesh_light_msg *led_info, uint8_t *len)

APP get LED Color and blink time.

参数
  • led_info – led_info is the pointer of struct bk_mesh_256_color_light_msg,it contains the RGB value and blink time

  • len – len is used for BK MESH NET to used to form a reply frame

返回

  • reply frame head ptr

int bk_mesh_led_demo_inform_vendor_info(uint8_t type, uint8_t layer)

SDK set LED Color and blink time through specified events.

参数
  • type – type is the event type

  • layer – layer is the specified layer.diff layer shows diff color and blink time

返回

  • BK_OK: succeed

  • others: other errors

int bk_mesh_light()

led init.

返回

  • BK_OK: succeed

  • others: other errors

备注

这几个函数的具体实现可以由客户自行实现,也可以使用demo中已经写好的函数实现。

这几个函数只是普通三色LED灯工程内部的具体设置实现,下面我们要做的就是将这些API统一到对应的vendor API上。

所有的vendor信息都存储在 g_vendor_info 这个全局结构体下,而在普通三色LED灯工程上,我们需要关注的则是 struct bk_mesh_light_msg led_control

结构体struct bk_mesh_light_msg:

Header File

Structures

struct bk_mesh_light_msg

Public Members

u8 type_msg

SDK event type

u8 layer

SDK layer

u16 time

LED Blink time

u16 color_value

LED color value

u8 color_id

LED color id(internal use)

每个成员的解释如下:

变量

类型 | 说明

type_msg

u8

上一次配置的消息类型,在inform函数中使用

layer

u8

上一次配置的层级信息,在inform函数中使用

time

u16

上一次配置的闪烁时间

color_value

u16

上一次配置的颜色值

此外,需要在初始化函数中执行下列操作:

g_vendor_info.vendor_bitmap |= BK_MESH_DEMO_SUPPORT_LED;

其中, BK_MESH_DEMO_SUPPORT_LED 占用Bitmap中的BIT0!

备注

普通三色LED灯工程需要开启CONFIG_BK_WIFI_MESH_DEMO_LED!

普通三色LED灯工程上使用的对外API的注册

对外的API整体通过以 g_vendor_info.vendor_bitmap 存储的内部bit值和 Vendor API 所携带的外部bit值进行比较而判断执行哪个工程(当然也有对应宏的保护) 下图为普通三色LED灯的Vendor API内部的执行流程图:

Vendor API flow for LED

Vendor API flow for LED

demo工程上使用的外部消息区域的Vendor字段格式

普通三色LED灯工程使用的部消息区域的Vendor字段格式如下:

- 普通三色LED灯工程使用bitmap中的BIT0为标记

- 普通三色LED灯工程使用bitmap0 Content字段

下面介绍一下普通三色LED灯工程相关Vendor字段格式

Bitmap0

  • Bitmap0目前被普通三色LED灯工程所用,用户的外部设备和节点之间通信,如涉及到PWM LED交互,则需要将此bit置1。

    • 当该bit为1时代表本命令支持LED控制

    • 当该bit为0时代表本命令不支持LED控制

备注

本字段在 CONFIG_BK_WIFI_MESH_DEMO_LED 配置为true的情况下生效!

Bitmap0 Content

备注

本字段在 CONFIG_BK_WIFI_MESH_DEMO_LED 配置为true的情况下生效!

Bitmap0 Content的结构如下:

vendor_bitmap0

vendor_bitmap0

下面介绍一下vendor_bitmap0里面各个字段的含义:

  • color bitmap:想要配置或者查询的颜色,LED三色灯不支持PWM,因此颜色受限于7种,因此长度为1个字节,因此本字段格式如下:

    3led_color_bitmap

    color_bitmap

    在整个bitmap上,从低到高表示R/G/B三个原色的使能bit组,目前为了扩展,一个原色组占用了两个bit,其结构如图:

    3led_rgbbit

    原色组的使能bit结构图

由上图可看出,目前我们每个原色的使能都只占用了使能组的低bit位置,高bit位置预留。

内部状态设置机制

下表是目前使用到的内部消息:

枚举

事件说明

NODE_POWER_ON

节点开机

NODE_START_NET

开始组网

NODE_NET_FAIL

组网失败

NODE_NET_LAYER

根据层级配置颜色,SDK默认,颜色见下表

NODE_BREAKDOWN

节点失效

NODE_REVOVER

节点恢复

NODE_LAYER_CHANGE

节点层级更改

NODE_STOP

节点停止

目前SDK默认的层级-颜色对照表:

层级

颜色

1

red

2

green

3

blue

4

yellow

5

magenta

6

cyan

特殊事件颜色:

事件

颜色

NODE_POWER_ON

white

NODE_BREAKDOWN

当前颜色闪烁

NODE_STOP

white