# dimming-light **Repository Path**: ohossmart-home-soulation/dimming-light ## Basic Information - **Project Name**: dimming-light - **Description**: 可以调节光线的照明设备 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-04 - **Last Updated**: 2025-03-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # WS63 星闪物联网设备开发模板 ### 模块介绍 #### sle_driver 此模块完成星闪设备连接,独立完成连接到服务器并自动断线重连 | 接口 | 作用 | 头文件 | |---|---|---| | `void SetSleReciveDataCallback(sle_recive_data_callback_t callback)` | 设置处理sle接收消息的回调 | `sle_server.h` | | `int SleSendData(uint8_t *data, uint8_t length)` | 向服务端广播消息 | `sle_server.h` | | `void SetSleMacAddr(uint8_t *mac_addr)` | 设置sle的mac地址,保证每个设备mac地址要不一样,mac地址为6位`uint8_t`数组,如果不手动设置mac地址默认为随机mac | `sle_server.h` | | `void SetSleDeviceName(uint8_t *name)` | 设置sle设备名称,不能大于16字节 | `sle_server.h` | | `void InitSle()` | 创建sle进程,启动sle | `sle_server_init.h` | | `void SetSleConnectedToServerCallback(sle_connected_to_server_callback_t callback)` | 设置sle连接成功的回调 | `sle_server.h` | - sle回调函数要满足 `void sle_recive_data_process(const unsigned char *data, unsigned int length)` 的形参列表和返回值 在你的代码中应该先设置回调再初始化sle ```c SetSleReciveDataCallback(sle_recive_data_process); initSle(); ``` 回调函数形参列表要求为 ```c void sle_recive_data_process(const unsigned char *data, unsigned int length); ``` data表示接收到的数据,length表示数据长度 - sle连接成功的回调其形参列表为 `void sle_connected_to_server_process()` #### easy_wifi 此模块完成wifi的连接功能 | 接口 | 作用 | 头文件 | |---|---|---| | `void CreateWifiConnectTask(void)` | 创建一个进程去连接wifi,不建议直接使用 | `wifi_connect.h` | | `void WifiConnectTask(int *arg)` | wifi连接函数 | `wifi_connect.h` | | `void SetWiFiSSID(char *_ssid)` | 设置WiFi的SSID | `wifi_connect.h` | | `void SetWiFiPreSharedKey(char *_preSharedKey)` | 设置WiFi的预共享密钥 | `wifi_connect.h` | | `void SetWiFiSSIDAndPreSharedKey(char *_ssid, char *_preSharedKey)` | 同时设置WiFi的SSID和预共享密钥 | `wifi_connect.h` | | `void CreateUdpTask(void)` | 创建udp进程,会自动调用WifiConnectTask先连接wifi再启动udp广播服务 | `udp_connect.h` | | `void SetUdpReciveDataCallback(UdpDataCallback callback)` | 设置udp接收数据处理函数 | `udp_connect.h` | | `void SendToServer(const char *data, unsigned int len)` | 向服务端发送数据 | `udp_connect.h` | | `int GetUdpIsReady(void)` | 获取udp是否准备好 | `udp_connect.h` | | `uint32_t ConvertStrToIntIp(const char *ip)` | 将字符串形式的IP地址转换为一个uint32_t的数值 | `udp_connect.h` | | `int ConnectToHotspot(WifiDeviceConfig* apConfig)` | 连接到热点 | `wifi_connecter.h` | | `void DisconnectWithHotspot(int netId)` | 断开与热点的连接 | `wifi_connecter.h` | | `int GetWifiState(void)` | 获取WiFi状态 | `wifi_connecter.h` | | `uint32_t GetLocalIpAddressInt()` | 获取本地IP地址(整数形式) | `wifi_connecter.h` | | `void GetLocalIpAddress(char* ip)` | 获取本地IP地址(字符串形式) | `wifi_connecter.h` | 在你的代码中应该先设置回调再初始化udp ```c SetUdpReciveDataCallback(udp_recive_data_process); CreateUdpTask(); ``` 回调函数形参列表要求为 ```c void udp_recive_data_process(const char * data,unsigned int length); ``` data表示接收到的数据,length表示数据长度 #### smarthome_device 此模块完成智能家居设备的注册和信息设置 | 接口 | 作用 | 头文件 | |---|---|---| | `void SleRegisterSmarthomeDevice(void)` | 注册Sle智能家居设备,这里注意必须等待sle连接成功后才能发送注册包 | `smarthome_device.h` | | `void SetSmarthomeDeviceName(const char *name)` | 设置智能家居设备的名称 | `smarthome_device.h` | | `void SetSmarthomeDeviceType(uint8_t type)` | 设置智能家居设备的类型 | `smarthome_device.h` | | `void SetSmarthomeDeviceID(uint16_t id)` | 设置智能家居设备的ID | `smarthome_device.h` | | `void SetSmarthomeAbilities(uint32_t ability)` | 设置智能家居设备的能力 | `smarthome_device.h` | | `const char* GetSmarthomeDeviceName(void)` | 获取智能家居设备的名称 | `smarthome_device.h` | | `uint8_t GetSmarthomeDeviceType(void)` | 获取智能家居设备的类型 | `smarthome_device.h` | | `uint16_t GetSmarthomeDeviceID(void)` | 获取智能家居设备的ID | `smarthome_device.h` | | `uint32_t GetSmarthomeAbilities(void)` | 获取智能家居设备的能力 | `smarthome_device.h` | | `int SleQuerySmarthomeDeviceStatus()` | 查询智能家居设备的状态 | `smarthome_device.h` | | `void SleRegisterSmarthomeDeviceInUdpChannel()` | 在UDP通道中注册智能家居设备 | `smarthome_device.h` | | `void SleQuerySmarthomeDeviceStatusUtilSucess()` | 查询智能家居设备状态直到成功 | `smarthome_device.h` | | `void CreateSleQuerySmarthomeDeviceStatusUtilSucessTask(void)` | 创建任务以查询智能家居设备状态直到成功 | `smarthome_device.h` | | `void SetFirstDeviceIdRecivedFromServerCallback(void (*callback)(uint16_t deviceId))` | 设置接收到的第一个设备ID的回调函数 | `smarthome_device.h` | | `void PeedingSleRegisterSmarthomeDeviceInUdpChannelUtilUdpIsReady()` | 等待在UDP通道中注册智能家居设备直到UDP准备好 | `smarthome_device.h` | | `void CreatePeedingSleRegisterSmarthomeDeviceInUdpChannelUtilUdpIsReadyTask(void)` | 创建任务以等待在UDP通道中注册智能家居设备直到UDP准备好 | `smarthome_device.h` | 在你的代码中可以这样使用这些接口: ```c #include "smarthome_device.h" int main() { // 设置设备信息 SetSmarthomeDeviceName("light"); SetSmarthomeDeviceType(2); SetSmarthomeDeviceID(12345); SetSmarthomeAbilities(1); return 0; } ``` 在smart_home_api/DeviceAttributes下的头文件用于描述smart home设备的基础信息 ,详见server仓库的介绍 #### utils 此模块提供了一些实用工具函数和数据结构,用于支持其他模块的功能。 | 接口 | 作用 | 头文件 | |---|---|---| | `void generateRandomCode(char *code, size_t length)` | 生成一个指定长度的随机字母数字代码 | `utils.h` | | `int generateRandomId()` | 生成一个随机ID | `utils.h` | | `void addPendingPacket(PendingPacketList *list, int id, int type, uint8_t *externalData)` | 向待处理数据包列表中添加一个数据包 | `res_pending_list.h` | | `void removePendingPacket(PendingPacketList *list, int id)` | 从待处理数据包列表中移除一个数据包 | `res_pending_list.h` | | `int checkPendingPacket(PendingPacketList *list, int id, int type)` | 检查待处理数据包列表中是否存在指定ID和类型的数据包 | `res_pending_list.h` | | `void getExternalData(PendingPacketList *list, uint8_t *externalData)` | 获取待处理数据包列表中的外部数据 | `res_pending_list.h` | #### oled_driver 此模块提供了OLED显示屏的初始化和显示功能。 | 接口 | 作用 | 头文件 | |---|---|---| | `void InitOled(void)` | 初始化OLED显示屏 | `oled_init.h` | | `void SetOledInitedCallback(oled_init_callback_t callback)` | 注册OLED初始化完成的回调函数 | `oled_init.h` | | `uint32_t OledInit(void)` | 初始化OLED显示屏并返回状态码 | `oled_ssd1306.h` | | `void OledSetPosition(uint8_t x, uint8_t y)` | 设置OLED显示屏的光标位置 | `oled_ssd1306.h` | | `void OledFillScreen(uint8_t fillData)` | 填充OLED显示屏 | `oled_ssd1306.h` | | `void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font)` | 在OLED显示屏上显示字符 | `oled_ssd1306.h` | | `void OledShowString(uint8_t x, uint8_t y, const char* str, Font font)` | 在OLED显示屏上显示字符串 | `oled_ssd1306.h` | `oled_init.h` 文件包含初始化OLED显示屏的函数 `oled_ssd1306.h` 文件定义了OLED显示屏的操作函数和字体类型枚举 - 字体类型 ```c typedef enum Font { FONT6x8 = 1, FONT8x16 } Font; ``` - 使用示例 ```c #include "oled_init.h" #include "oled_ssd1306.h" void OledInitCallback(void) { // OLED初始化完成后的处理 printf("OLED initialized.\n"); } int main() { // 初始化OLED显示屏 InitOled(); OledInit(); // 设置OLED初始化完成的回调函数 SetOledInitedCallback(OledInitCallback); // 设置光标位置 OledSetPosition(0, 0); // 填充屏幕 OledFillScreen(0x00); // 显示字符和字符串 OledShowChar(0, 0, 'A', FONT8x16); OledShowString(0, 2, "Hello, OLED!", FONT6x8); return 0; } ``` #### keyboard4x4_driver 此模块提供了4x4矩阵键盘的初始化和按键扫描功能。 | 接口 | 作用 | 头文件 | |---|---|---| | `void KeypadInit(void)` | 初始化4x4矩阵键盘 | `matrix_keypad.h` | | `void SetKeypadCallback(keypad_callback_t callback)` | 设置键盘回调函数 | `matrix_keypad.h` | `matrix_keypad.h` 文件包含4x4矩阵键盘的初始化和按键扫描函数: ```c #include "matrix_keypad.h" void KeypadCallback(char *key) { // 处理按键值 printf("Key pressed: %c\n", *key); } int main() { // 初始化键盘 KeypadInit(); // 设置键盘回调函数 SetKeypadCallback(KeypadCallback); } ``` #### main 此模块为用户代码 ### 如何使用 1. 下载并解压sdk0628 2. 将本模块放在applications/sample/wifi-iot/app/下,如图所示 ![代码文件图片](./pics/file_of_code.png) 3. 在device/soc/hisilicon/ws63v100/sdkv100/build/config/target_config/ws63/config.py中第95行后加入 ```python "sle_driver", "easy_wifi", "smarthome_server", "smart_home_api", "utils", "oled_driver", "keyboard4x4_driver", ``` 如图所示 ![config.py修改](./pics/config.py_modify.png) 4. 在device/soc/hisilicon/ws63v100/sdkv100/libs_url/ws63/cmake/ohos.cmake第17行加入 ```cmake "sle_driver" "easy_wifi" "smarthome_server" "smart_home_api" "utils" "oled_driver" "keyboard4x4_driver" ``` 如图所示 ![cmake修改](./pics/cmake_modify.png) 5. 修改applications/sample/wifi-iot/app/BUILD.gn为以下内容 ```gn import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ "smarthome_server_templete/main:smarthome_server", "smarthome_server_templete/wifi_driver:easy_wifi", "smarthome_server_templete/sle_driver:sle_driver", "smarthome_server_templete/smart_home_api:smart_home_api", "smarthome_server_templete/utils:utils", "smarthome_server_templete/oled_driver:oled_driver", "smarthome_server_templete/keyboard4x4_driver:keyboard4x4_driver" ] } ``` 6. 使用deveco device进行编译 编译在 out/nearlink_dk_3863/nearlink_dk_3863/ws63-liteos-app/ws63-liteos-app_all.fwpkg 7. 使用BurnTool进行烧录 ### 已知问题 - set_sle_device_name 目前暂时不建议使用,设置为非16字节的名称会造成奇怪的错误