├── .codespellrc ├── .github └── workflows │ └── spell-check.yml ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── README_zh.md ├── boards.txt ├── cores └── tuya_open │ ├── Arduino.h │ ├── ArduinoMain.cpp │ ├── ArduinoMain.h │ ├── Interrupts.cpp │ ├── Print.h │ ├── SerialUART.cpp │ ├── SerialUART.h │ ├── Tone.cpp │ ├── WMath.cpp │ ├── api │ ├── ArduinoAPI.h │ ├── Binary.h │ ├── Client.h │ ├── Common.cpp │ ├── Common.h │ ├── Compat.h │ ├── HardwareI2C.h │ ├── HardwareSPI.h │ ├── HardwareSerial.h │ ├── IPAddress.cpp │ ├── IPAddress.h │ ├── Interrupts.h │ ├── PluggableUSB.cpp │ ├── PluggableUSB.h │ ├── Print.cpp │ ├── Print.h │ ├── Printable.h │ ├── RingBuffer.h │ ├── Server.h │ ├── Stream.cpp │ ├── Stream.h │ ├── String.cpp │ ├── String.h │ ├── USBAPI.h │ ├── Udp.h │ ├── WCharacter.h │ ├── deprecated-avr-comp │ │ └── avr │ │ │ ├── dtostrf.c.impl │ │ │ ├── dtostrf.h │ │ │ ├── interrupt.h │ │ │ └── pgmspace.h │ ├── deprecated │ │ ├── Client.h │ │ ├── HardwareSerial.h │ │ ├── IPAddress.h │ │ ├── Print.h │ │ ├── Printable.h │ │ ├── Server.h │ │ ├── Stream.h │ │ ├── Udp.h │ │ └── WString.h │ └── itoa.h │ ├── cbuf.cpp │ ├── cbuf.h │ ├── pgmspace.h │ ├── tuya_app_main.c │ ├── tuya_arduino_version.h │ ├── wiring.cpp │ ├── wiring_analog.cpp │ ├── wiring_digital.cpp │ └── wiring_shift.cpp ├── libraries ├── BLE │ ├── examples │ │ └── ble_server │ │ │ └── ble_server.ino │ ├── library.properties │ └── src │ │ ├── BLEAdvertising.cpp │ │ ├── BLEAdvertising.h │ │ ├── BLECharacteristic.cpp │ │ ├── BLECharacteristic.h │ │ ├── BLEDEV.cpp │ │ ├── BLEDEV.h │ │ ├── BLEServer.cpp │ │ ├── BLEServer.h │ │ ├── BLEService.cpp │ │ └── BLEService.h ├── DNSServer │ ├── examples │ │ └── CaptivePortal │ │ │ └── CaptivePortal.ino │ ├── library.properties │ └── src │ │ ├── DNSServer.cpp │ │ └── DNSServer.h ├── FS │ ├── library.properties │ └── src │ │ ├── file.cpp │ │ ├── file.h │ │ └── fs_api.h ├── HTTPClient │ ├── examples │ │ ├── BasicHttpClient │ │ │ └── BasicHttpClient.ino │ │ └── BasicHttpsClient │ │ │ └── BasicHttpsClient.ino │ ├── library.properties │ └── src │ │ ├── HTTPClient.cpp │ │ └── HTTPClient.h ├── LittleFS │ ├── examples │ │ └── LittleFSDemo │ │ │ └── LittleFSDemo.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── LittleFS.cpp │ │ └── LittleFS.h ├── Log │ ├── examples │ │ └── logOutput │ │ │ └── logOutput.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── Log.cpp │ │ └── Log.h ├── MQTTClient │ ├── examples │ │ ├── mqtt_auth │ │ │ └── mqtt_auth.ino │ │ ├── mqtt_basic │ │ │ └── mqtt_basic.ino │ │ └── mqtt_publish_in_callback │ │ │ └── mqtt_publish_in_callback.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── MQTTClient.cpp │ │ └── MQTTClient.h ├── SPI │ ├── examples │ │ └── spiDemo │ │ │ └── spiDemo.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── SPI.cpp │ │ └── SPI.h ├── Ticker │ ├── examples │ │ ├── Blinker │ │ │ └── Blinker.ino │ │ └── TickerParameter │ │ │ └── TickerParameter.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── Ticker.cpp │ │ └── Ticker.h ├── TuyaIoT │ ├── examples │ │ ├── dpType │ │ │ ├── appConfig.json │ │ │ └── dpType.ino │ │ ├── quickStart │ │ │ ├── README.md │ │ │ ├── README_zh.md │ │ │ ├── appConfig.json │ │ │ ├── quickStart.ino │ │ │ └── tLed.h │ │ └── weatherGet │ │ │ ├── appConfig.json │ │ │ ├── tLed.h │ │ │ └── weatherGet.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── ArduinoTuyaIoTClient.c │ │ ├── ArduinoTuyaIoTClient.h │ │ ├── TuyaIoT.cpp │ │ ├── TuyaIoT.h │ │ ├── TuyaIoTWeather.cpp │ │ └── TuyaIoTWeather.h ├── WiFi │ ├── examples │ │ ├── SimpleWiFiServer │ │ │ └── SimpleWiFiServer.ino │ │ ├── WiFiAccessPoint │ │ │ └── WiFiAccessPoint.ino │ │ ├── WiFiClient │ │ │ └── WiFiClient.ino │ │ ├── WiFiClientBasic │ │ │ └── WiFiClientBasic.ino │ │ ├── WiFiClientConnect │ │ │ └── WiFiClientConnect.ino │ │ ├── WiFiClientEvents │ │ │ └── WiFiClientEvents.ino │ │ ├── WiFiClientStaticIP │ │ │ └── WiFiClientStaticIP.ino │ │ ├── WiFiMulti │ │ │ └── WiFiMulti.ino │ │ ├── WiFiScan │ │ │ └── WiFiScan.ino │ │ ├── WiFiScanDualAntenna │ │ │ └── WiFiScanDualAntenna.ino │ │ ├── WiFiTelnetToSerial │ │ │ └── WiFiTelnetToSerial.ino │ │ └── WiFiUDPClient │ │ │ ├── WiFiUDPClient.ino │ │ │ ├── udp_server.py │ │ │ └── udp_server.rb │ ├── keywords.txt │ ├── library.properties │ └── src │ │ ├── WiFi.cpp │ │ ├── WiFi.h │ │ ├── WiFiAP.cpp │ │ ├── WiFiAP.h │ │ ├── WiFiClient.cpp │ │ ├── WiFiClient.h │ │ ├── WiFiGeneric.cpp │ │ ├── WiFiGeneric.h │ │ ├── WiFiMulti.cpp │ │ ├── WiFiMulti.h │ │ ├── WiFiSTA.cpp │ │ ├── WiFiSTA.h │ │ ├── WiFiScan.cpp │ │ ├── WiFiScan.h │ │ ├── WiFiServer.cpp │ │ ├── WiFiServer.h │ │ ├── WiFiType.h │ │ ├── WiFiUdp.cpp │ │ └── WiFiUdp.h └── Wire │ ├── examples │ └── masterWriter │ │ └── masterWriter.ino │ ├── keywords.txt │ ├── library.properties │ └── src │ ├── Wire.cpp │ └── Wire.h ├── package.json ├── package_cn.json ├── platform.txt ├── programmers.txt ├── script └── make_boards.py ├── tools ├── README.md ├── export_binary.py ├── fileutils │ ├── __init__.py │ └── file_operations.py ├── objcopy.py ├── packager │ ├── __init__.py │ ├── chip_info.py │ ├── packager.py │ ├── packager_esp32.py │ ├── packager_ln882h.py │ ├── packager_t2.py │ └── packager_t3_t5.py ├── prebuild.py └── sketch_version │ ├── __init__.py │ └── sketch_version.py └── variants ├── XH_WB5E ├── pins_arduino.h ├── project_elf_src.c └── t5_hw_port.c ├── esp32 ├── esp32_hw_port.c └── pins_arduino.h ├── ln882h ├── ln882h_hw_port.c └── pins_arduino.h ├── t2 ├── pins_arduino.h └── t2_hw_port.c ├── t3 ├── pins_arduino.h ├── project_elf_src.c └── t3_hw_port.c └── t5 ├── pins_arduino.h ├── project_elf_src.c └── t5_hw_port.c /.codespellrc: -------------------------------------------------------------------------------- 1 | # See: https://github.com/codespell-project/codespell#using-a-config-file 2 | [codespell] 3 | # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: 4 | ignore-words-list = hel,shiftin 5 | check-filenames = 6 | check-hidden = 7 | skip = ./.git 8 | -------------------------------------------------------------------------------- /.github/workflows/spell-check.yml: -------------------------------------------------------------------------------- 1 | name: Spell Check 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | pull_request: 7 | schedule: 8 | # Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates. 9 | - cron: "0 8 * * TUE" 10 | workflow_dispatch: 11 | repository_dispatch: 12 | 13 | jobs: 14 | spellcheck: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v2 20 | 21 | - name: Spell check 22 | uses: codespell-project/actions-codespell@master 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "ArduinoCore-API"] 2 | path = ArduinoCore-API 3 | url = https://github.com/arduino/ArduinoCore-API.git 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ## 2 | # @file CMakeLists.txt 3 | # @brief 4 | #/ 5 | 6 | # MODULE_PATH 7 | set(MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) 8 | 9 | # MODULE_NAME 10 | get_filename_component(MODULE_NAME ${MODULE_PATH} NAME) 11 | 12 | # Arduino Board (t2/t3) 13 | if(NOT DEFINED CONFIG_ARDUINO_BOARD) 14 | message(FATAL_ERROR "Use [menuconfig] choice [ARDUINO_BOARD].") 15 | endif() 16 | if(${CONFIG_ARDUINO_BOARD} STREQUAL "ARDUINO_T3") 17 | set(ARDUINO_BOARD "t3") 18 | elseif(${CONFIG_ARDUINO_BOARD} STREQUAL "ARDUINO_T5") 19 | set(ARDUINO_BOARD "t5") 20 | elseif(${CONFIG_ARDUINO_BOARD} STREQUAL "ARDUINO_LN882H") 21 | set(ARDUINO_BOARD "ln882h") 22 | else() 23 | set(CONFIG_ARDUINO_BOARD "ARDUINO_T2") 24 | set(ARDUINO_BOARD "t2") 25 | endif() 26 | 27 | # LIB_SRCS 28 | file(GLOB_RECURSE 29 | LIB_SRCS_CORE 30 | "${MODULE_PATH}/cores/tuya_open/*.cpp" 31 | "${MODULE_PATH}/cores/tuya_open/*.c" 32 | "${MODULE_PATH}/cores/tuya_open/api/*.cpp" 33 | ) 34 | file(GLOB_RECURSE 35 | LIB_SRCS_LIB 36 | "${MODULE_PATH}/libraries/BLE/src/*.cpp" 37 | "${MODULE_PATH}/libraries/BLE/src/*.c" 38 | "${MODULE_PATH}/libraries/FS/src/*.cpp" 39 | "${MODULE_PATH}/libraries/FS/src/*.c" 40 | "${MODULE_PATH}/libraries/HTTPClient/src/*.cpp" 41 | "${MODULE_PATH}/libraries/HTTPClient/src/*.c" 42 | "${MODULE_PATH}/libraries/LittleFS/src/*.cpp" 43 | "${MODULE_PATH}/libraries/LittleFS/src/*.c" 44 | "${MODULE_PATH}/libraries/Log/src/*.cpp" 45 | "${MODULE_PATH}/libraries/Log/src/*.c" 46 | "${MODULE_PATH}/libraries/MQTTClient/src/*.cpp" 47 | "${MODULE_PATH}/libraries/MQTTClient/src/*.c" 48 | "${MODULE_PATH}/libraries/SPI/src/*.cpp" 49 | "${MODULE_PATH}/libraries/SPI/src/*.c" 50 | "${MODULE_PATH}/libraries/Ticker/src/*.cpp" 51 | "${MODULE_PATH}/libraries/Ticker/src/*.c" 52 | "${MODULE_PATH}/libraries/TuyaIoT/src/*.cpp" 53 | "${MODULE_PATH}/libraries/TuyaIoT/src/*.c" 54 | ) 55 | 56 | file(GLOB_RECURSE 57 | LIB_SRCS_VAR 58 | "${MODULE_PATH}/variants/${ARDUINO_BOARD}/*.c" 59 | ) 60 | list(APPEND LIB_SRCS ${LIB_SRCS_CORE}) 61 | list(APPEND LIB_SRCS ${LIB_SRCS_LIB}) 62 | list(APPEND LIB_SRCS ${LIB_SRCS_VAR}) 63 | 64 | set(LIBRARIES_INC 65 | "${MODULE_PATH}/libraries/BLE/src/" 66 | "${MODULE_PATH}/libraries/FS/src/" 67 | "${MODULE_PATH}/libraries/HTTPClient/src/" 68 | "${MODULE_PATH}/libraries/LittleFS/src/" 69 | "${MODULE_PATH}/libraries/Log/src/" 70 | "${MODULE_PATH}/libraries/MQTTClient/src/" 71 | "${MODULE_PATH}/libraries/SPI/src/" 72 | "${MODULE_PATH}/libraries/Ticker/src/" 73 | "${MODULE_PATH}/libraries/TuyaIoT/src/" 74 | ) 75 | 76 | set(LIB_PUBLIC_INC 77 | "${MODULE_PATH}/cores/tuya_open" 78 | "${LIBRARIES_INC}" 79 | "${MODULE_PATH}/variants/${ARDUINO_BOARD}" 80 | ) 81 | 82 | ######################################## 83 | # Target Configure 84 | ######################################## 85 | add_library(${MODULE_NAME}) 86 | 87 | target_sources(${MODULE_NAME} 88 | PRIVATE 89 | ${LIB_SRCS} 90 | ) 91 | 92 | target_include_directories(${MODULE_NAME} 93 | PUBLIC 94 | ${LIB_PUBLIC_INC} 95 | ) 96 | 97 | target_compile_options(${MODULE_NAME} 98 | PRIVATE 99 | -D${CONFIG_ARDUINO_BOARD} 100 | ) 101 | 102 | 103 | ######################################## 104 | # Layer Configure 105 | ######################################## 106 | list(APPEND COMPONENT_LIBS ${MODULE_NAME}) 107 | # set(COMPONENT_LIBS "${COMPONENT_LIBS}" PARENT_SCOPE) 108 | list(APPEND COMPONENT_PUBINC ${LIB_PUBLIC_INC}) 109 | # set(COMPONENT_PUBINC "${COMPONENT_PUBINC}" PARENT_SCOPE) 110 | 111 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # Home 2 | 3 | ![Spell Check](https://img.shields.io/github/actions/workflow/status/tuya/arduino-tuyaopen/spell-check.yml?style=plastic&label=Spell%20Check) 4 | 5 | [English](README.md) | [简体中文](README_zh.md) 6 | 7 | ## 简介 8 | 9 | arduino-tuyaopen 是 tuya 针对 Arduino 平台提供的开源 IoT 开发框架,让众多喜欢 Arduino 的开发者可以快速开发出基于涂鸦云平台的物联网设备,实现设备远程智能控制。 10 | 11 | arduino-tuyaopen 基于 [tuya-open-sdk-for-device](https://github.com/tuya/tuya-open-sdk-for-device) 开发,tuya-open-sdk-for-device 是一款跨芯片平台、操作系统的 IoT 开发框架。它基于通用南向接口设计,支持 Bluetooth、Wi-Fi、Ethernet 等通信协议,提供了物联网开发的核心功能,包括配网,激活,控制,升级等;它具备强大的安全合规能力,包括设备认证、数据加密、通信加密等,满足全球各个国家和地区的数据合规需求。 12 | 13 | 基于 tuya-open-sdk-for-device 开发的 IoT 产品,如果使用 tuya_cloud_service 组件的功能,就可以使用涂鸦APP、云服务提供的强大生态能力,并与 Power By Tuya 设备互联互通。 14 | 15 | 同时 tuya-open-sdk-for-device 将不断拓展,提供更多云平台接入功能,及语音、视频、人脸识别等功能,arduino-tuyaopen 也会同步更新支持更多功能。 16 | 17 | ## 支持的芯片 18 | 目前该项目已支持 T2,T3 和 T5 芯片。 19 | | 芯片 | Datasheet | 20 | | :--: | :----------------------------------------------------------: | 21 | | T2 | [T2](https://developer.tuya.com/cn/docs/iot/T2-U-module-datasheet?id=Kce1tncb80ldq) | 22 | | T3 | [T3](https://developer.tuya.com/cn/docs/iot/T3-U-Module-Datasheet?id=Kdd4pzscwf0il) | 23 | | T5 | [T5](https://developer.tuya.com/cn/docs/iot/T5-E1-Module-Datasheet?id=Kdar6hf0kzmfi) | 24 | 25 | ## 支持的操作系统 26 | 27 | | 芯片 | Windows | Linux | macOS | 28 | | :--: | :------: | :---: | :------: | 29 | | T2 | 支持 | 支持 | 暂不支持 | 30 | | T3 | 支持 | 支持 | 支持 | 31 | | T5 | 支持 | 支持 | 支持 | 32 | | LN882H | 支持 | 支持 | 支持 | 33 | 34 | > 注意:当前部分芯片未支持某些操作系统,我们正在全力支持中,敬请期待! 35 | 36 | 37 | ## arduino-tuyaopen 使用 38 | 39 | 推荐安装使用最新版本的 Arduino IDE 2,可点击 arduino 官网 [arduino.cc](https://www.arduino.cc/en/software) 下载 Arduino IDE,该项目的所有编译烧录测试都是在 Arduino IDE 2 上进行的。 40 | 41 | + 复制下方开发板管理地址: 42 | 43 | ``` 44 | https://github.com/tuya/arduino-tuyaopen/releases/download/global/package_tuya_open_index.json 45 | ``` 46 | 47 | 或使用 gitee 地址: 48 | 49 | ``` 50 | https://gitee.com/tuya-open/arduino-tuyaopen/releases/download/global/package_tuya_open_index_cn.json 51 | ``` 52 | 53 | + 启动 Arduino IDE 2,并依次点击 “文件” -> “首选项” ,打开首选项窗口。 54 | 55 | ![Preferences](https://images.tuyacn.com/fe-static/docs/img/9f354b85-5f9d-4af6-be60-c114d7b1e822.png) 56 | 57 | + 在 “其他开发板管理器地址” 中输入上述开发板管理地址。 58 | 59 | ![BoardManagerURL](https://images.tuyacn.com/fe-static/docs/img/3e53a3ce-c603-481a-9ac0-fd20d6fa6525.png) 60 | 61 | + 在 “开发板管理器”,输入 "Tuya Open" 查找并安装最新版本。 62 | 63 | ## 如何使用 arduino-tuyaopen 进行云连接 64 | 65 | + [连接 Tuya IoT 平台](./libraries/TuyaIoT/examples/quickStart/README_zh.md) 66 | 67 | ## 开发板硬件简介 68 | 69 | + [T2-U 开发板](https://developer.tuya.com/cn/docs/iot/t2-u-board?id=Kce6cq9e9vlmv) 70 | + [T3-U 模组规格书](https://developer.tuya.com/cn/docs/iot/T3-U-Module-Datasheet?id=Kdd4pzscwf0il) 71 | + [T5-E1 模组规格书](https://developer.tuya.com/cn/docs/iot/T5-E1-Module-Datasheet?id=Kdar6hf0kzmfi) 72 | 73 | ## 支持 74 | 75 | 在使用该项目的过程中,如果您遇到问题或者有新的功能需求或想法,可以在该项目下通过 issues 的方式进行沟通交流。 76 | -------------------------------------------------------------------------------- /cores/tuya_open/Arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_H 2 | #define ARDUINO_H 3 | 4 | #include "pins_arduino.h" 5 | #include "api/ArduinoAPI.h" 6 | 7 | #if defined(__cplusplus) && !defined(c_plusplus) 8 | 9 | using namespace arduino; 10 | 11 | #include "SerialUART.h" 12 | #define Serial _SerialUART0_ 13 | 14 | #endif // __cplusplus 15 | 16 | #endif // ARDUINO_H 17 | -------------------------------------------------------------------------------- /cores/tuya_open/ArduinoMain.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ArduinoMain.cpp 3 | * @author www.tuya.com 4 | * @brief ArduinoMain module is used to 5 | * @version 0.1 6 | * @date 2024-08-20 7 | * 8 | * @copyright Copyright (c) tuya.inc 2024 9 | * 10 | */ 11 | 12 | extern "C" { 13 | #include "tuya_iot_config.h" 14 | #include "tal_api.h" 15 | } 16 | 17 | #include 18 | 19 | /*********************************************************** 20 | ************************macro define************************ 21 | ***********************************************************/ 22 | 23 | /*********************************************************** 24 | ***********************typedef define*********************** 25 | ***********************************************************/ 26 | 27 | /*********************************************************** 28 | ********************function declaration******************** 29 | ***********************************************************/ 30 | 31 | /*********************************************************** 32 | ***********************variable define********************** 33 | ***********************************************************/ 34 | 35 | 36 | /*********************************************************** 37 | ***********************function define********************** 38 | ***********************************************************/ 39 | 40 | extern "C" void ArduinoMain(void) 41 | { 42 | setup(); 43 | 44 | for (;;) { 45 | loop(); 46 | tal_system_sleep(1); 47 | } 48 | 49 | return; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /cores/tuya_open/ArduinoMain.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARDUINO_MAIM_H__ 2 | #define __ARDUINO_MAIM_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | void ArduinoMain(void); 9 | 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | 14 | #endif // __ARDUINO_MAIM_H__ 15 | 16 | -------------------------------------------------------------------------------- /cores/tuya_open/Interrupts.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | 3 | #include "tkl_gpio.h" 4 | 5 | void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode) 6 | { 7 | TUYA_GPIO_IRQ_T irqConfig; 8 | irqConfig.cb = (TUYA_GPIO_IRQ_CB)callback; 9 | irqConfig.arg = NULL; 10 | 11 | switch (mode) { 12 | case FALLING: 13 | irqConfig.mode = TUYA_GPIO_IRQ_FALL; 14 | break; 15 | case RISING: 16 | irqConfig.mode = TUYA_GPIO_IRQ_RISE; 17 | break; 18 | default : return; 19 | } 20 | 21 | tkl_gpio_irq_init((TUYA_GPIO_NUM_E)interruptNumber, &irqConfig); 22 | tkl_gpio_irq_enable((TUYA_GPIO_NUM_E)interruptNumber); 23 | 24 | return; 25 | } 26 | void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param) 27 | { 28 | TUYA_GPIO_IRQ_T irqConfig; 29 | irqConfig.cb = (TUYA_GPIO_IRQ_CB)callback; 30 | irqConfig.arg = param; 31 | 32 | switch (mode) { 33 | case FALLING: 34 | irqConfig.mode = TUYA_GPIO_IRQ_FALL; 35 | break; 36 | case RISING: 37 | irqConfig.mode = TUYA_GPIO_IRQ_RISE; 38 | break; 39 | default : return; 40 | } 41 | 42 | tkl_gpio_irq_init((TUYA_GPIO_NUM_E)interruptNumber, &irqConfig); 43 | tkl_gpio_irq_enable((TUYA_GPIO_NUM_E)interruptNumber); 44 | 45 | return; 46 | } 47 | void detachInterrupt(pin_size_t interruptNumber) 48 | { 49 | tkl_gpio_irq_disable((TUYA_GPIO_NUM_E)interruptNumber); 50 | 51 | return; 52 | } 53 | -------------------------------------------------------------------------------- /cores/tuya_open/Print.h: -------------------------------------------------------------------------------- 1 | #include "api/Print.h" -------------------------------------------------------------------------------- /cores/tuya_open/SerialUART.h: -------------------------------------------------------------------------------- 1 | #ifndef __SERIAL_UART_H__ 2 | #define __SERIAL_UART_H__ 3 | 4 | #include "tkl_uart.h" 5 | #include "tal_mutex.h" 6 | 7 | #include "Arduino.h" 8 | #include "api/RingBuffer.h" 9 | #include "api/HardwareSerial.h" 10 | 11 | namespace arduino { 12 | 13 | class SerialUART : public HardwareSerial 14 | { 15 | public: 16 | SerialUART(uint8_t id); 17 | ~SerialUART(); 18 | 19 | void begin(unsigned long); 20 | void begin(unsigned long baudrate, uint16_t config); 21 | void end(); 22 | int available(void); 23 | int peek(void); 24 | int read(void); 25 | void flush(void); 26 | size_t write(uint8_t); 27 | size_t write(const uint8_t*, size_t); 28 | using Print::write; // pull in write(str) and write(buf, size) from Print 29 | operator bool(); 30 | 31 | // rx callback function 32 | int __rxBufWrite(uint8_t c); 33 | private: 34 | uint8_t __uartID = TUYA_UART_NUM_MAX; 35 | MUTEX_HANDLE __mutex; 36 | RingBufferN<256> _rxBuffer; 37 | }; 38 | 39 | } 40 | 41 | extern arduino::SerialUART _SerialUART0_; 42 | 43 | #endif // __SERIAL_UART_H__ 44 | -------------------------------------------------------------------------------- /cores/tuya_open/Tone.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "tkl_pwm.h" 3 | 4 | void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) 5 | { 6 | OPERATE_RET rt = OPRT_OK; 7 | static uint8_t is_init = 0; 8 | 9 | TUYA_PWM_NUM_E pwmNum = pwmPinToNum(_pin); 10 | if (TUYA_PWM_NUM_MAX == pwmNum) { 11 | return; 12 | } 13 | 14 | TUYA_PWM_BASE_CFG_T pwmCfg = pwmCfgGet(_pin); 15 | pwmCfg.frequency = frequency; 16 | 17 | pwmCfg.duty = 5000; // duty 0-10000, 5000 is 50% 18 | 19 | if (0 == is_init) { 20 | rt = tkl_pwm_init(pwmNum, &pwmCfg); 21 | if (OPRT_OK != rt) { 22 | return; 23 | } 24 | is_init = 1; 25 | } 26 | 27 | tkl_pwm_start(pwmNum); 28 | 29 | return; 30 | } 31 | 32 | void noTone(uint8_t _pin) 33 | { 34 | tkl_pwm_stop(pwmPinToNum(_pin)); 35 | 36 | return; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /cores/tuya_open/WMath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "tal_system.h" 4 | 5 | long random(long howbig) 6 | { 7 | uint32_t rang = 0; 8 | 9 | if (howbig == 0) { 10 | return 0; 11 | } 12 | 13 | rang = howbig & 0xFFFFFFFF; 14 | 15 | return tal_system_get_random(rang); 16 | } 17 | 18 | long random(long howsmall, long howbig) 19 | { 20 | if (howsmall >= howbig) { 21 | return howsmall; 22 | } 23 | long diff = howbig - howsmall; 24 | return random(diff) + howsmall; 25 | } 26 | 27 | void randomSeed(unsigned long seed) 28 | { 29 | // TODO: 30 | return; 31 | } 32 | -------------------------------------------------------------------------------- /cores/tuya_open/api/ArduinoAPI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/ArduinoAPI.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Binary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Binary.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Client.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Client.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Common.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/Common.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Common.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Compat.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Compat.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/HardwareI2C.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/HardwareI2C.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/HardwareSPI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/HardwareSPI.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/HardwareSerial.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/IPAddress.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/IPAddress.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/IPAddress.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/IPAddress.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Interrupts.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Interrupts.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/PluggableUSB.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/PluggableUSB.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/PluggableUSB.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/PluggableUSB.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Print.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/Print.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Print.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Print.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Printable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Printable.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/RingBuffer.h: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/RingBuffer.h" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Server.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Server.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Stream.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/Stream.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Stream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Stream.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/String.cpp: -------------------------------------------------------------------------------- 1 | #include "../../../ArduinoCore-API/api/String.cpp" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/String.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/String.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/USBAPI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/USBAPI.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/Udp.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/Udp.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/WCharacter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/WCharacter.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated-avr-comp/avr/dtostrf.c.impl: -------------------------------------------------------------------------------- 1 | #include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/dtostrf.c.impl" 2 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated-avr-comp/avr/dtostrf.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/dtostrf.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated-avr-comp/avr/interrupt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/interrupt.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated-avr-comp/avr/pgmspace.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../../../ArduinoCore-API/api/deprecated-avr-comp/avr/pgmspace.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Client.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Client.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including HardwareSerial.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../HardwareSerial.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/IPAddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including IPAddress.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../IPAddress.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Print.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Print.h" 24 | 25 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Printable.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Printable.h" 24 | 25 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Server.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Server.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Stream.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Stream.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/Udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Udp.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../Udp.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /cores/tuya_open/api/deprecated/WString.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2016, Arduino LLC. All Right Reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including WString.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be remove on the next 22 | // major release of the API 23 | #include "../String.h" 24 | 25 | -------------------------------------------------------------------------------- /cores/tuya_open/api/itoa.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../../ArduinoCore-API/api/itoa.h" 3 | -------------------------------------------------------------------------------- /cores/tuya_open/cbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | cbuf.h - Circular buffer implementation 3 | Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. 4 | This file is part of the esp8266 core for Arduino environment. 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 2.1 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this library; if not, write to the Free Software 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #ifndef __cbuf_h 22 | #define __cbuf_h 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | class cbuf 29 | { 30 | public: 31 | cbuf(size_t size); 32 | ~cbuf(); 33 | 34 | size_t resizeAdd(size_t addSize); 35 | size_t resize(size_t newSize); 36 | size_t available() const; 37 | size_t size(); 38 | 39 | size_t room() const; 40 | 41 | inline bool empty() const 42 | { 43 | return _begin == _end; 44 | } 45 | 46 | inline bool full() const 47 | { 48 | return wrap_if_bufend(_end + 1) == _begin; 49 | } 50 | 51 | int peek(); 52 | size_t peek(char *dst, size_t size); 53 | 54 | int read(); 55 | size_t read(char* dst, size_t size); 56 | 57 | size_t write(char c); 58 | size_t write(const char* src, size_t size); 59 | 60 | void flush(); 61 | size_t remove(size_t size); 62 | 63 | cbuf *next; 64 | 65 | protected: 66 | inline char* wrap_if_bufend(char* ptr) const 67 | { 68 | return (ptr == _bufend) ? _buf : ptr; 69 | } 70 | 71 | size_t _size; 72 | char* _buf; 73 | const char* _bufend; 74 | char* _begin; 75 | char* _end; 76 | 77 | }; 78 | 79 | #endif//__cbuf_h 80 | -------------------------------------------------------------------------------- /cores/tuya_open/pgmspace.h: -------------------------------------------------------------------------------- 1 | #include "api/deprecated-avr-comp/avr/pgmspace.h" -------------------------------------------------------------------------------- /cores/tuya_open/tuya_app_main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file tuya_app_main.cpp 3 | * @author www.tuya.com 4 | * @brief tuya_app_main module is used to 5 | * @version 0.1 6 | * @date 2024-08-20 7 | * 8 | * @copyright Copyright (c) tuya.inc 2024 9 | * 10 | */ 11 | #include "tuya_iot_config.h" 12 | 13 | #include "ArduinoMain.h" 14 | 15 | #include "cJSON.h" 16 | #include "tkl_uart.h" 17 | 18 | #if defined(ENABLE_WIFI) && (ENABLE_WIFI == 1) 19 | #include "netconn_wifi.h" 20 | #endif 21 | #if defined(ENABLE_WIRED) && (ENABLE_WIRED == 1) 22 | #include "netconn_wired.h" 23 | #endif 24 | #if defined(ENABLE_LIBLWIP) && (ENABLE_LIBLWIP == 1) 25 | #include "lwip_init.h" 26 | #endif 27 | 28 | /*********************************************************** 29 | ************************macro define************************ 30 | ***********************************************************/ 31 | 32 | /*********************************************************** 33 | ***********************typedef define*********************** 34 | ***********************************************************/ 35 | 36 | /*********************************************************** 37 | ********************function declaration******************** 38 | ***********************************************************/ 39 | 40 | /*********************************************************** 41 | ***********************variable define********************** 42 | ***********************************************************/ 43 | static THREAD_HANDLE arduino_thrd_hdl = NULL; 44 | static char thread_name[] = "arduino_thread"; 45 | 46 | /*********************************************************** 47 | ***********************function define********************** 48 | ***********************************************************/ 49 | static void __wifi_callback_event(WF_EVENT_E event, void *arg) 50 | { 51 | return; 52 | } 53 | 54 | void app_open_sdk_init(void) 55 | { 56 | // cJSON init 57 | cJSON_Hooks hooks = { 58 | .malloc_fn = tal_malloc, 59 | .free_fn = tal_free 60 | }; 61 | cJSON_InitHooks(&hooks); 62 | 63 | // file system init 64 | tal_kv_cfg_t kv_cfg = { 65 | .seed = "vmlkasdh93dlvlcy", 66 | .key = "dflfuap134ddlduq", 67 | }; 68 | tal_kv_init(&kv_cfg); 69 | // software timer init 70 | tal_sw_timer_init(); 71 | // work queue init 72 | tal_workq_init(); 73 | 74 | // lwip init 75 | #if defined(ENABLE_LIBLWIP) && (ENABLE_LIBLWIP == 1) 76 | TUYA_LwIP_Init(); 77 | #endif 78 | 79 | // wifi init 80 | // TODO: set country code 81 | // TODO: use netconn_wifi functions 82 | #if (!defined(ARDUINO_T5) && !defined(ARDUINO_ESP32)) 83 | tal_wifi_init(__wifi_callback_event); 84 | tal_wifi_set_country_code("CN"); 85 | #endif 86 | } 87 | 88 | static void ArduinoThread(void *arg) 89 | { 90 | 91 | #if defined(ARDUINO_T2) 92 | extern char get_rx2_flag(void); 93 | while (get_rx2_flag() == 0) { 94 | tal_system_sleep(1); 95 | } 96 | #endif // defined(ARDUINO_T2) 97 | 98 | #if (!defined(ARDUINO_LN882H)&& !defined(ARDUINO_ESP32)) 99 | tkl_uart_deinit(TUYA_UART_NUM_0); 100 | #if (!defined(ARDUINO_T3) && !defined(ARDUINO_T5) && !defined(ARDUINO_ESP32)) 101 | tkl_uart_deinit(TUYA_UART_NUM_1); // TODO: close vendor log 102 | #endif 103 | #endif // (!defined(ARDUINO_LN882H)) 104 | 105 | app_open_sdk_init(); 106 | 107 | ArduinoMain(); 108 | } 109 | 110 | void tuya_app_main(void) 111 | { 112 | #if (!defined(ARDUINO_T5) && !defined(ARDUINO_ESP32)) 113 | __asm("BL __libc_init_array"); 114 | #endif 115 | 116 | THREAD_CFG_T thrd_param = {1024 * 4, THREAD_PRIO_1, thread_name}; 117 | tal_thread_create_and_start(&arduino_thrd_hdl, NULL, NULL, ArduinoThread, NULL, &thrd_param); 118 | } 119 | -------------------------------------------------------------------------------- /cores/tuya_open/tuya_arduino_version.h: -------------------------------------------------------------------------------- 1 | #ifndef __TUYA_ARDUINO_VERSION_H__ 2 | #define __TUYA_ARDUINO_VERSION_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | // Major version number (X.x.x) 8 | #define VERSION_ARDUINO_TUYA_MAJOR 1 9 | // Minor version number (x.X.x) 10 | #define VERSION_ARDUINO_TUYA_MINOR 1 11 | // Patch version number (x.x.X) 12 | #define VERSION_ARDUINO_TUYA_PATCH 6 13 | 14 | // Version number (in numeric form) 15 | #define VERSION_ARDUINO_TUYA ((VERSION_ARDUINO_TUYA_MAJOR << 16) | (VERSION_ARDUINO_TUYA_MINOR << 8) | VERSION_ARDUINO_TUYA_PATCH) 16 | 17 | // Version string (in string form) 18 | #define df2str(x) #x 19 | #define d2str(x) df2str(x) 20 | #define VERSION_ARDUINO_TUYA_STR d2str(VERSION_ARDUINO_TUYA_MAJOR) "." d2str(VERSION_ARDUINO_TUYA_MINOR) "." d2str(VERSION_ARDUINO_TUYA_PATCH) 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif // __TUYA_ARDUINO_VERSION_H__ 27 | -------------------------------------------------------------------------------- /cores/tuya_open/wiring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "tal_system.h" 4 | 5 | unsigned long micros(void) 6 | { 7 | unsigned long us = tal_system_get_millisecond() * 1000; 8 | return us; 9 | } 10 | 11 | unsigned long millis() 12 | { 13 | unsigned long ms = tal_system_get_millisecond(); 14 | return ms; 15 | } 16 | 17 | void delay(unsigned long ms) 18 | { 19 | tal_system_sleep((uint32_t)ms); 20 | } 21 | 22 | void delayMicroseconds(unsigned int us) 23 | { 24 | // Note: tuya open sdk not support delayMicroseconds 25 | unsigned int ms = ((us / 1000) == 0) ? 1 : (us / 1000); 26 | 27 | tal_system_sleep((uint32_t)ms); 28 | } 29 | 30 | void yield(void) 31 | { 32 | tal_system_sleep(1); 33 | } 34 | -------------------------------------------------------------------------------- /cores/tuya_open/wiring_analog.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | 3 | #include "tkl_adc.h" 4 | #include "tkl_pwm.h" 5 | 6 | int analogRead(pin_size_t pinNumber) 7 | { 8 | OPERATE_RET rt = OPRT_OK; 9 | int32_t readValue = 0; 10 | 11 | uint8_t adcChannel = adcPinToChannel(pinNumber); 12 | if (TUYA_ADC_INVALID_VALUE == adcChannel) { 13 | return 0; 14 | } 15 | 16 | TUYA_ADC_NUM_E adcNum = adcPinToNum(pinNumber); 17 | 18 | TUYA_ADC_BASE_CFG_T adcCfg = adcCfgGet(pinNumber); 19 | 20 | rt = tkl_adc_init(adcNum, &adcCfg); 21 | if (OPRT_OK != rt) { 22 | return 0; 23 | } 24 | 25 | tkl_adc_read_single_channel(adcNum, adcChannel, &readValue); 26 | 27 | tkl_adc_deinit(adcNum); 28 | 29 | return readValue; 30 | } 31 | 32 | void analogReference(uint8_t mode) 33 | { 34 | return; 35 | } 36 | 37 | // value: 0-100 38 | void analogWrite(pin_size_t pinNumber, int value) 39 | { 40 | OPERATE_RET rt = OPRT_OK; 41 | 42 | TUYA_PWM_NUM_E pwmNum = pwmPinToNum(pinNumber); 43 | if (TUYA_PWM_NUM_MAX == pwmNum) { 44 | return; 45 | } 46 | 47 | TUYA_PWM_BASE_CFG_T pwmCfg = pwmCfgGet(pinNumber); 48 | 49 | pwmCfg.duty = value * 100; // duty 0-10000, 5000 is 50% 50 | 51 | rt = tkl_pwm_init(pwmNum, &pwmCfg); 52 | if (OPRT_OK != rt) { 53 | return; 54 | } 55 | tkl_pwm_start(pwmNum); 56 | 57 | return; 58 | } 59 | -------------------------------------------------------------------------------- /cores/tuya_open/wiring_digital.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | 3 | #include "tkl_gpio.h" 4 | 5 | void pinMode(pin_size_t pinNumber, PinMode pinMode) 6 | { 7 | TUYA_GPIO_BASE_CFG_T gpioCfg; 8 | 9 | TUYA_GPIO_NUM_E tuyaPin = (TUYA_GPIO_NUM_E)pinNumber; 10 | 11 | switch (pinMode) { 12 | case INPUT: { 13 | gpioCfg.direct = TUYA_GPIO_INPUT; 14 | gpioCfg.mode = TUYA_GPIO_FLOATING; 15 | } break; 16 | case OUTPUT: { 17 | gpioCfg.direct = TUYA_GPIO_OUTPUT; 18 | gpioCfg.mode = TUYA_GPIO_PUSH_PULL; 19 | gpioCfg.level = TUYA_GPIO_LEVEL_LOW; 20 | } break; 21 | case INPUT_PULLUP: { 22 | gpioCfg.direct = TUYA_GPIO_INPUT; 23 | gpioCfg.mode = TUYA_GPIO_PULLUP; 24 | } break; 25 | case INPUT_PULLDOWN: { 26 | gpioCfg.direct = TUYA_GPIO_INPUT; 27 | gpioCfg.mode = TUYA_GPIO_PULLDOWN; 28 | } break; 29 | case OUTPUT_OPENDRAIN:{ 30 | gpioCfg.direct = TUYA_GPIO_OUTPUT; 31 | gpioCfg.mode = TUYA_GPIO_OPENDRAIN; 32 | gpioCfg.level = TUYA_GPIO_LEVEL_LOW; 33 | } break; 34 | default : return; 35 | } 36 | 37 | tkl_gpio_init(tuyaPin, &gpioCfg); 38 | 39 | return; 40 | } 41 | 42 | void digitalWrite(pin_size_t pinNumber, PinStatus status) 43 | { 44 | TUYA_GPIO_NUM_E tuyaPin = (TUYA_GPIO_NUM_E)pinNumber; 45 | TUYA_GPIO_LEVEL_E tuyaLevel = (status == HIGH) ? (TUYA_GPIO_LEVEL_HIGH) : (TUYA_GPIO_LEVEL_LOW); 46 | 47 | tkl_gpio_write(tuyaPin, tuyaLevel); 48 | 49 | return; 50 | } 51 | 52 | PinStatus digitalRead(pin_size_t pinNumber) 53 | { 54 | TUYA_GPIO_LEVEL_E level = TUYA_GPIO_LEVEL_LOW; 55 | TUYA_GPIO_NUM_E tuyaPin = (TUYA_GPIO_NUM_E)pinNumber; 56 | 57 | tkl_gpio_read(tuyaPin, &level); 58 | 59 | return ((level==TUYA_GPIO_LEVEL_HIGH) ? (HIGH) : (LOW)); 60 | } 61 | -------------------------------------------------------------------------------- /cores/tuya_open/wiring_shift.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | 3 | uint8_t shiftIn(pin_size_t dataPin, uint8_t clockPin, BitOrder bitOrder) 4 | { 5 | uint8_t value = 0; 6 | uint8_t i; 7 | 8 | for (i = 0; i < 8; ++i) { 9 | digitalWrite(clockPin, HIGH); 10 | if (bitOrder == LSBFIRST) 11 | value |= digitalRead(dataPin) << i; 12 | else 13 | value |= digitalRead(dataPin) << (7 - i); 14 | digitalWrite(clockPin, LOW); 15 | } 16 | return value; 17 | } 18 | 19 | void shiftOut(pin_size_t dataPin, uint8_t clockPin, BitOrder bitOrder, uint8_t val) 20 | { 21 | uint8_t i; 22 | 23 | for (i = 0; i < 8; i++) { 24 | if (bitOrder == LSBFIRST) 25 | digitalWrite(dataPin, !!(val & (1 << i)) ? HIGH : LOW); 26 | else 27 | digitalWrite(dataPin, !!(val & (1 << (7 - i))) ? HIGH : LOW); 28 | 29 | digitalWrite(clockPin, HIGH); 30 | digitalWrite(clockPin, LOW); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /libraries/BLE/library.properties: -------------------------------------------------------------------------------- 1 | name= BLE Arduino 2 | version=1.0.0 3 | includes=BLEDev.h, BLEAdvertising.h 4 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEAdvertising.cpp: -------------------------------------------------------------------------------- 1 | #include "BLEAdvertising.h" 2 | #include "tal_api.h" 3 | 4 | void BLEAdvertising::setMinInterval(uint16_t mininterval) 5 | { 6 | advParams.adv_interval_min = mininterval; 7 | } 8 | 9 | void BLEAdvertising::setMaxInterval(uint16_t maxinterval) 10 | { 11 | advParams.adv_interval_max = maxinterval; 12 | } 13 | 14 | void BLEAdvertising::setScanResponse(bool set) 15 | { 16 | scanResp = set; 17 | } 18 | 19 | void BLEAdvertising::setAdvertisementData(uint8_t *adv_data, uint8_t len) 20 | { 21 | TKL_BLE_DATA_T p_adv; 22 | p_adv.p_data = (uint8_t *)tal_malloc(sizeof(uint8_t)*len); 23 | memcpy(p_adv.p_data, adv_data, len); 24 | p_adv.length = len; 25 | tkl_ble_gap_adv_rsp_data_set(&p_adv, NULL); 26 | } 27 | 28 | void BLEAdvertising::setScanResponseData(uint8_t *adv_rsp_data,uint8_t len) 29 | { 30 | TKL_BLE_DATA_T p_scan_rsp; 31 | p_scan_rsp.p_data = (uint8_t *)tal_malloc(sizeof(uint8_t)*len); 32 | memcpy(p_scan_rsp.p_data, adv_rsp_data, len); 33 | p_scan_rsp.length = len; 34 | tkl_ble_gap_adv_rsp_data_set(NULL, &p_scan_rsp); 35 | } 36 | 37 | void BLEAdvertising::start() 38 | { 39 | advParams.adv_type = TKL_BLE_GAP_ADV_TYPE_CONN_SCANNABLE_UNDIRECTED; 40 | advParams.adv_channel_map = 0x01 | 0x02 | 0x04; 41 | tkl_ble_gap_adv_start(&advParams); 42 | } 43 | 44 | void BLEAdvertising::stop() 45 | { 46 | tkl_ble_gap_adv_stop(); 47 | } 48 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEAdvertising.h: -------------------------------------------------------------------------------- 1 | #ifndef _BLEADVERTISING_H_ 2 | #define _BLEADVERTISING_H_ 3 | 4 | #include "tkl_bluetooth.h" 5 | 6 | class BLEAdvertising { 7 | public: 8 | BLEAdvertising(){advParams.adv_interval_min=160; advParams.adv_interval_max=160;} 9 | void start(); 10 | void stop(); 11 | void setMaxInterval(uint16_t maxinterval); 12 | void setMinInterval(uint16_t mininterval); 13 | void setAdvertisementData(uint8_t *adv_data,uint8_t len); 14 | void setScanResponseData(uint8_t *adv_rsp_data,uint8_t len); 15 | void setScanResponse(bool); 16 | 17 | private: 18 | bool scanResp = true; 19 | TKL_BLE_GAP_ADV_PARAMS_T advParams; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLECharacteristic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tkl_bluetooth.h" 4 | #include "BLECharacteristic.h" 5 | #include "BLEService.h" 6 | #include "BLEDEV.h" 7 | 8 | BLECharacteristic::BLECharacteristic(std::string uuid, uint32_t properties) 9 | { 10 | int ntf_idx = 0; 11 | int prop = 0; 12 | int perm = 0; 13 | if(uuid.length() == 4 ) 14 | { 15 | (BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid_type = TKL_BLE_UUID_TYPE_16; 16 | hexstr2bin(uuid.data(),(uint8_t*)&(BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid.uuid16,uuid.length()/2); 17 | } 18 | else if(uuid.length() == 8 ) 19 | { 20 | (BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid_type = TKL_BLE_UUID_TYPE_32; 21 | hexstr2bin(uuid.data(),(uint8_t*)&(BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid.uuid32,uuid.length()/2); 22 | } 23 | else if(uuid.length() == 32) 24 | { 25 | (BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid_type = TKL_BLE_UUID_TYPE_128; 26 | hexstr2bin(uuid.data(),(uint8_t*)&(BLEService::ble_service_char + BLEService::char_index )->char_uuid.uuid.uuid128,uuid.length()/2); 27 | } 28 | 29 | if(properties & PROPERTY_WRITE) 30 | { 31 | prop |= TKL_BLE_GATT_CHAR_PROP_WRITE; 32 | perm |= (TKL_BLE_GATT_PERM_READ | TKL_BLE_GATT_PERM_WRITE); 33 | } 34 | if(properties & PROPERTY_NOTIFY) 35 | { 36 | prop |= TKL_BLE_GATT_CHAR_PROP_NOTIFY; 37 | perm |= (TKL_BLE_GATT_PERM_READ | TKL_BLE_GATT_PERM_WRITE); 38 | } 39 | if(properties & PROPERTY_READ) 40 | { 41 | prop |= TKL_BLE_GATT_CHAR_PROP_READ; 42 | perm |= TKL_BLE_GATT_PERM_READ; 43 | } 44 | if(properties & PROPERTY_INDICATE) 45 | { 46 | prop |= TKL_BLE_GATT_CHAR_PROP_INDICATE; 47 | perm |= (TKL_BLE_GATT_PERM_READ | TKL_BLE_GATT_PERM_WRITE); 48 | 49 | } 50 | ntf_idx = BLEService::char_index; 51 | (BLEService::ble_service_char + BLEService::char_index )->property = prop; 52 | (BLEService::ble_service_char + BLEService::char_index )->permission = perm; 53 | (BLEService::ble_service_char + BLEService::char_index )->handle = TKL_BLE_GATT_INVALID_HANDLE; 54 | (BLEService::ble_service_char + BLEService::char_index )->value_len = 244; 55 | BLEService::char_index ++; 56 | notify_handle = ntf_idx; 57 | } 58 | 59 | void BLECharacteristic::setValue(uint8_t* data, size_t length) 60 | { 61 | if(length < 128) 62 | { 63 | memcpy(notify_data,data,length); 64 | notify_data_len = length; 65 | } 66 | else 67 | { 68 | memcpy(notify_data,data,128); 69 | notify_data_len = 128; 70 | } 71 | } 72 | 73 | void BLECharacteristic::notify() 74 | { 75 | if (notify_data_len != 0) 76 | { 77 | tkl_ble_gatts_value_notify(BLEDEV::ble_common_connect_handle, (BLEService::ble_service_char + notify_handle )->handle, notify_data, notify_data_len); 78 | } 79 | } 80 | 81 | void BLECharacteristic::indicate() 82 | { 83 | if (notify_data_len != 0) 84 | { 85 | tkl_ble_gatts_value_indicate(BLEDEV::ble_common_connect_handle, (BLEService::ble_service_char + notify_handle )->handle, notify_data, notify_data_len); 86 | } 87 | } -------------------------------------------------------------------------------- /libraries/BLE/src/BLECharacteristic.h: -------------------------------------------------------------------------------- 1 | #ifndef _BLECHARACTERISTIC_H_ 2 | #define _BLECHARACTERISTIC_H_ 3 | 4 | #include 5 | 6 | class BLECharacteristic { 7 | public: 8 | BLECharacteristic(std::string uuid, uint32_t properties = 0); 9 | 10 | void indicate(); 11 | void notify(); 12 | void setValue(uint8_t* data, size_t size); 13 | 14 | static const uint32_t PROPERTY_READ = 1<<0; 15 | static const uint32_t PROPERTY_WRITE = 1<<1; 16 | static const uint32_t PROPERTY_NOTIFY = 1<<2; 17 | static const uint32_t PROPERTY_BROADCAST = 1<<3; 18 | static const uint32_t PROPERTY_INDICATE = 1<<4; 19 | static const uint32_t PROPERTY_WRITE_NO_RSP = 1<<5; 20 | 21 | private: 22 | uint16_t notify_handle; 23 | uint8_t notify_data[128]; 24 | uint16_t notify_data_len = 0; 25 | friend class BLEService; 26 | friend class BLEDEV; 27 | 28 | }; 29 | 30 | extern int hexstr2bin(const char *hex, uint8_t *buf, size_t len); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEDEV.cpp: -------------------------------------------------------------------------------- 1 | #include "BLEDEV.h" 2 | #include "tkl_bluetooth.h" 3 | #include "tal_log.h" 4 | 5 | int BLEDEV:: ble_common_connect_handle = TKL_BLE_GATT_INVALID_HANDLE; 6 | 7 | static void ble_kernel_gap_event_callback(TKL_BLE_GAP_PARAMS_EVT_T *p_event) 8 | { 9 | BLEServerCallbacks* callback = BLEServer::getCallbacks(); 10 | if(p_event == NULL) return; 11 | 12 | switch(p_event->type) 13 | { 14 | case TKL_BLE_EVT_STACK_INIT: { 15 | PR_INFO("ble stack ok\n"); 16 | } break; 17 | 18 | case TKL_BLE_EVT_STACK_DEINIT: { 19 | PR_INFO("ble deinit stack ok\n"); 20 | } break; 21 | 22 | case TKL_BLE_GAP_EVT_CONNECT: { 23 | BLEDEV:: ble_common_connect_handle = p_event->conn_handle; 24 | PR_INFO("addr : %s",p_event->gap_event.connect.peer_addr.addr); 25 | callback->onConnect(); 26 | } break; 27 | 28 | case TKL_BLE_GAP_EVT_DISCONNECT:{ 29 | if(p_event->gap_event.disconnect.role == TKL_BLE_ROLE_SERVER && p_event->conn_handle == BLEDEV::ble_common_connect_handle ) { 30 | BLEDEV:: ble_common_connect_handle = TKL_BLE_GATT_INVALID_HANDLE; 31 | } 32 | callback->onDisconnect(); 33 | } break; 34 | 35 | case TKL_BLE_GAP_EVT_ADV_REPORT: { 36 | 37 | } break; 38 | 39 | case TKL_BLE_GAP_EVT_CONN_PARAM_REQ: { 40 | 41 | } break; 42 | 43 | case TKL_BLE_GAP_EVT_CONN_PARAM_UPDATE: { 44 | 45 | } break; 46 | 47 | case TKL_BLE_GAP_EVT_CONN_RSSI: { 48 | 49 | } break; 50 | 51 | default: { 52 | 53 | }return; 54 | } 55 | } 56 | 57 | static void ble_kernel_gatt_event_callback(TKL_BLE_GATT_PARAMS_EVT_T *p_event) 58 | { 59 | BLEServerCallbacks* callback = BLEServer::getCallbacks(); 60 | if(p_event == NULL) return; 61 | 62 | switch(p_event->type) 63 | { 64 | case TKL_BLE_GATT_EVT_MTU_REQUEST: { 65 | 66 | } break; 67 | 68 | case TKL_BLE_GATT_EVT_NOTIFY_TX: { 69 | callback->notify_done(); 70 | } break; 71 | 72 | case TKL_BLE_GATT_EVT_WRITE_REQ: { 73 | callback->onwrite(p_event->gatt_event.write_report.report.p_data, p_event->gatt_event.write_report.report.length ); 74 | PR_INFO("write :%02x",p_event->gatt_event.write_report.report.p_data[0]); 75 | } break; 76 | 77 | case TKL_BLE_GATT_EVT_NOTIFY_INDICATE_RX:{ 78 | 79 | } break; 80 | 81 | case TKL_BLE_GATT_EVT_SUBSCRIBE:{ 82 | if(p_event->result == OPRT_OK) { 83 | 84 | } 85 | }break; 86 | 87 | case TKL_BLE_GATT_EVT_READ_CHAR_VALUE:{ 88 | 89 | }break; 90 | 91 | default: { 92 | // Not Support 93 | } return; 94 | } 95 | } 96 | 97 | BLEAdvertising* BLEDEV::g_bleAdvertising = nullptr; 98 | BLEServer* BLEDEV::g_pServer = nullptr; 99 | 100 | BLEAdvertising* BLEDEV::getAdvertising() { 101 | if(g_bleAdvertising == nullptr) { 102 | g_bleAdvertising = new BLEAdvertising(); 103 | } 104 | return g_bleAdvertising; 105 | } 106 | 107 | BLEServer* BLEDEV::createServer() { 108 | g_pServer = new BLEServer(); 109 | return g_pServer; 110 | } 111 | 112 | void BLEDEV::init() 113 | { 114 | tkl_ble_gap_callback_register(ble_kernel_gap_event_callback); 115 | tkl_ble_gatt_callback_register(ble_kernel_gatt_event_callback); 116 | } -------------------------------------------------------------------------------- /libraries/BLE/src/BLEDEV.h: -------------------------------------------------------------------------------- 1 | #ifndef _BLEDEV_H_ 2 | #define _BLEDEV_H_ 3 | 4 | #include "BLEAdvertising.h" 5 | #include "BLEServer.h" 6 | 7 | class BLEDEV { 8 | public: 9 | static int ble_common_connect_handle; 10 | 11 | static BLEServer* createServer(); 12 | static BLEAdvertising* getAdvertising(); 13 | static void init(); 14 | 15 | private: 16 | static BLEServer* g_pServer; 17 | static BLEAdvertising* g_bleAdvertising; 18 | 19 | }; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEServer.cpp: -------------------------------------------------------------------------------- 1 | #include "BLEServer.h" 2 | #include "BLEService.h" 3 | 4 | static BLEServerCallbacks* m_pServerCallbacks; 5 | 6 | BLEService* BLEServer::createService(const char* uuid) { 7 | BLEService* pService = new BLEService(uuid); 8 | return pService; 9 | } 10 | 11 | void BLEServer::setCallbacks(BLEServerCallbacks* pCallbacks) { 12 | m_pServerCallbacks = pCallbacks; 13 | } 14 | 15 | BLEServerCallbacks* BLEServer::getCallbacks() { 16 | return m_pServerCallbacks; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEServer.h: -------------------------------------------------------------------------------- 1 | #ifndef _BK_BLESERVER_H_ 2 | #define _BK_BLESERVER_H_ 3 | 4 | #include "BLEService.h" 5 | #include 6 | 7 | class BLEServerCallbacks { 8 | public: 9 | virtual ~BLEServerCallbacks() {}; 10 | virtual void onConnect() {}; 11 | virtual void onDisconnect() {}; 12 | virtual void onwrite(uint8_t* data,uint16_t data_len){}; 13 | virtual void notify_done() {}; 14 | 15 | }; 16 | 17 | class BLEServer { 18 | public: 19 | BLEService* createService(const char* uuid); 20 | void setCallbacks(BLEServerCallbacks* pCallbacks); 21 | static BLEServerCallbacks* getCallbacks(); 22 | 23 | private: 24 | friend class BLEService; 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEService.cpp: -------------------------------------------------------------------------------- 1 | #include "BLEService.h" 2 | #include "BLEServer.h" 3 | #include "BLECharacteristic.h" 4 | #include 5 | #include "tal_api.h" 6 | 7 | static int hex2num(char c) 8 | { 9 | if (c >= '0' && c <= '9') 10 | return c - '0'; 11 | if (c >= 'a' && c <= 'f') 12 | return c - 'a' + 10; 13 | if (c >= 'A' && c <= 'F') 14 | return c - 'A' + 10; 15 | return -1; 16 | } 17 | 18 | static int hex2byte(const char *hex) 19 | { 20 | int a, b; 21 | a = hex2num(*hex++); 22 | if (a < 0) 23 | return -1; 24 | b = hex2num(*hex++); 25 | if (b < 0) 26 | return -1; 27 | return (a << 4) | b; 28 | } 29 | 30 | int hexstr2bin(const char *hex, uint8_t *buf, size_t len) 31 | { 32 | size_t i; 33 | int a; 34 | const char *ipos = hex; 35 | uint8_t *opos = buf; 36 | 37 | for (i = 0; i < len; i++) { 38 | a = hex2byte(ipos); 39 | if (a < 0) 40 | return -1; 41 | *opos++ = a; 42 | ipos += 2; 43 | } 44 | return 0; 45 | } 46 | 47 | TKL_BLE_GATTS_PARAMS_T* BLEService:: ble_gatt_service = NULL; 48 | TKL_BLE_SERVICE_PARAMS_T* BLEService:: ble_service = NULL; 49 | TKL_BLE_CHAR_PARAMS_T* BLEService :: ble_service_char = NULL; 50 | uint8_t BLEService::char_index ; 51 | 52 | BLEService::BLEService(std::string uuid) 53 | { 54 | char_index = 0; 55 | 56 | if (!ble_gatt_service) { 57 | ble_gatt_service = (TKL_BLE_GATTS_PARAMS_T*)tal_malloc(sizeof(TKL_BLE_GATTS_PARAMS_T)); 58 | } 59 | if (!ble_service) { 60 | ble_service = (TKL_BLE_SERVICE_PARAMS_T*)tal_malloc(sizeof(TKL_BLE_SERVICE_PARAMS_T)); 61 | } 62 | if (!ble_service_char) { 63 | ble_service_char = (TKL_BLE_CHAR_PARAMS_T *)tal_malloc(sizeof(TKL_BLE_CHAR_PARAMS_T) * TKL_BLE_GATT_CHAR_MAX_NUM); 64 | } 65 | 66 | ble_gatt_service->svc_num = 1 ; 67 | ble_gatt_service->p_service = ble_service; 68 | ble_service->handle = TKL_BLE_GATT_INVALID_HANDLE; 69 | 70 | if( uuid.length() == 4) 71 | { 72 | ble_service->svc_uuid.uuid_type = TKL_BLE_UUID_TYPE_16; 73 | hexstr2bin(uuid.data(),(uint8_t*)&ble_service->svc_uuid.uuid.uuid16,uuid.length()/2); 74 | } 75 | else if( uuid.length() == 8) 76 | { 77 | ble_service->svc_uuid.uuid_type = TKL_BLE_UUID_TYPE_32; 78 | hexstr2bin(uuid.data(),(uint8_t*)&ble_service->svc_uuid.uuid.uuid32,uuid.length()/2); 79 | } 80 | else if( uuid.length() == 32) 81 | { 82 | ble_service->svc_uuid.uuid_type = TKL_BLE_UUID_TYPE_128; 83 | hexstr2bin(uuid.data(),(uint8_t*)&ble_service->svc_uuid.uuid.uuid128,uuid.length()/2); 84 | } 85 | ble_service->type = TKL_BLE_UUID_SERVICE_PRIMARY; 86 | ble_service->char_num = TKL_BLE_GATT_CHAR_MAX_NUM, 87 | ble_service->p_char = ble_service_char; 88 | } 89 | 90 | BLECharacteristic* BLEService::createCharacteristic(const char* uuid, uint32_t properties) 91 | { 92 | BLECharacteristic* pCharacteristic = new BLECharacteristic(uuid, properties); 93 | return pCharacteristic; 94 | } 95 | 96 | void BLEService::start() 97 | { 98 | tkl_ble_gatts_service_add(ble_gatt_service); 99 | tkl_ble_stack_init(TKL_BLE_ROLE_SERVER); 100 | } 101 | -------------------------------------------------------------------------------- /libraries/BLE/src/BLEService.h: -------------------------------------------------------------------------------- 1 | #ifndef _BK_BLESERVICE_H_ 2 | #define _BK_BLESERVICE_H_ 3 | 4 | #include "BLECharacteristic.h" 5 | #include 6 | #include "tkl_bluetooth.h" 7 | 8 | class BLEService { 9 | public: 10 | static TKL_BLE_GATTS_PARAMS_T * ble_gatt_service; 11 | static TKL_BLE_SERVICE_PARAMS_T * ble_service; 12 | static TKL_BLE_CHAR_PARAMS_T * ble_service_char; 13 | static uint8_t char_index ; 14 | 15 | BLECharacteristic* createCharacteristic(const char* uuid, uint32_t properties); 16 | void start(); 17 | 18 | private: 19 | 20 | BLEService(std::string uuid); 21 | friend class BLEServer; 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const byte DNS_PORT = 53; 5 | IPAddress apIP(8,8,4,4); // The default android DNS 6 | DNSServer dnsServer; 7 | WiFiServer server; 8 | 9 | String responseHTML = "" 10 | "CaptivePortal" 11 | "

Hello World!

This is a captive portal example. All requests will " 12 | "be redirected here.

"; 13 | 14 | void setup() { 15 | WiFi.mode(WIFI_AP); 16 | WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); 17 | WiFi.softAP("your_ssid","your_passwd"); 18 | // if DNSServer is started with "*" for domain name, it will reply with 19 | // provided IP to all DNS request 20 | dnsServer.start(DNS_PORT, "*", apIP); 21 | 22 | server.begin(80); 23 | } 24 | 25 | void loop() { 26 | dnsServer.processNextRequest(); 27 | WiFiClient client = server.available(); // listen for incoming clients 28 | 29 | if (client) { 30 | String currentLine = ""; 31 | while (client.connected()) { 32 | if (client.available()) { 33 | char c = client.read(); 34 | if (c == '\n') { 35 | if (currentLine.length() == 0) { 36 | client.println("HTTP/1.1 200 OK"); 37 | client.println("Content-type:text/html"); 38 | client.println(); 39 | client.print(responseHTML); 40 | break; 41 | } else { 42 | currentLine = ""; 43 | } 44 | } else if (c != '\r') { 45 | currentLine += c; 46 | } 47 | } 48 | } 49 | client.stop(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /libraries/DNSServer/library.properties: -------------------------------------------------------------------------------- 1 | name=DNSServer 2 | 3 | architectures=* 4 | -------------------------------------------------------------------------------- /libraries/DNSServer/src/DNSServer.h: -------------------------------------------------------------------------------- 1 | #ifndef __DNSServer_H 2 | #define __DNSServer_H 3 | #include 4 | 5 | #define DNS_QR_QUERY 0 6 | #define DNS_QR_RESPONSE 1 7 | #define DNS_OPCODE_QUERY 0 8 | #define DNS_DEFAULT_TTL 60 // Default Time To Live : time interval in seconds that the resource record should be cached before being discarded 9 | #define DNS_OFFSET_DOMAIN_NAME 12 // Offset in bytes to reach the domain name in the DNS message 10 | #define DNS_HEADER_SIZE 12 11 | 12 | enum class DNSReplyCode 13 | { 14 | NoError = 0, 15 | FormError = 1, 16 | ServerFailure = 2, 17 | NonExistentDomain = 3, 18 | NotImplemented = 4, 19 | Refused = 5, 20 | YXDomain = 6, 21 | YXRRSet = 7, 22 | NXRRSet = 8 23 | }; 24 | 25 | enum DNSType 26 | { 27 | DNS_TYPE_HOST_ADDRESS = 1, // Host Address 28 | DNS_TYPE_IPV6_ADDRESS = 28, // IPv6 Address 29 | DNS_TYPE_SOA = 6, // Start Of a zone of Authority 30 | DNS_TYPE_DOMAIN_PTR = 12, // Domain name PoinTeR 31 | DNS_TYPE_DELENAME = 39 // Delegation Name 32 | } ; 33 | 34 | enum DNSClass 35 | { 36 | DNS_CLASS_IN = 1, // INternet 37 | DNS_CLASS_CH = 3 // CHaos 38 | } ; 39 | 40 | enum DNSRDLength 41 | { 42 | DNS_RDLENGTH_IPV4 = 4 // 4 bytes for an IPv4 address 43 | } ; 44 | 45 | struct DNSHeader 46 | { 47 | uint16_t ID; // identification number 48 | union { 49 | struct { 50 | uint16_t RD : 1; // recursion desired 51 | uint16_t Trunc : 1; // truncated message 52 | uint16_t AuthAns : 1; // authoritative answer 53 | uint16_t OPCode : 4; // message_type 54 | uint16_t QRFlag : 1; // query/response flag 55 | uint16_t RspCode : 4; // response code 56 | uint16_t Z : 3; // its z! reserved 57 | uint16_t RecAvail : 1; // recursion available 58 | }; 59 | uint16_t Flags; 60 | }; 61 | uint16_t QuesCnt; // number of question entries 62 | uint16_t AnsCnt; // number of answer entries 63 | uint16_t AuthCnt; // number of authority entries 64 | uint16_t ResCnt; // number of resource entries 65 | }; 66 | 67 | struct DNSQuestion 68 | { 69 | uint8_t QName[256] ; //need 1 Byte for zero termination! 70 | uint16_t QNameLength ; 71 | uint16_t QType ; 72 | uint16_t QClass ; 73 | } ; 74 | 75 | class DNSServer 76 | { 77 | public: 78 | DNSServer(); 79 | ~DNSServer(); 80 | void processNextRequest(); 81 | void setErrorReplyCode(const DNSReplyCode &replyCode); 82 | void setTTL(const uint32_t &ttl); 83 | 84 | // Returns true if successful, false if there are no sockets available 85 | bool start(const uint16_t &port, 86 | const String &domainName, 87 | const IPAddress &resolvedIP); 88 | // stops the DNS server 89 | void stop(); 90 | 91 | private: 92 | WiFiUDP _Udp; 93 | uint16_t _port; 94 | String _domain; 95 | unsigned char _ResolveIP[4]; 96 | int _CurPacketSize; 97 | unsigned char* _buf; 98 | DNSHeader* _DnsHeader; 99 | uint32_t _ttl; 100 | DNSReplyCode _ErrReplyCode; 101 | DNSQuestion* _DnsQuestion ; 102 | 103 | 104 | void downcaseAndRemoveWwwPrefix(String &domainName); 105 | String getDomainNameWithoutWwwPrefix(); 106 | bool requestIncludesOnlyOneQuestion(); 107 | void replyWithIP(); 108 | void replyWithCustomCode(); 109 | }; 110 | #endif 111 | -------------------------------------------------------------------------------- /libraries/FS/library.properties: -------------------------------------------------------------------------------- 1 | name=FS 2 | version=2.0.0 3 | author=tuya 4 | maintainer=tuya 5 | sentence=tuya 6 | paragraph= 7 | category=Data Storage 8 | url= 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/FS/src/file.h: -------------------------------------------------------------------------------- 1 | #ifndef __FILE_H__ 2 | #define __FILE_H__ 3 | 4 | #include "fs_api.h" 5 | #include "Arduino.h" 6 | 7 | enum{ 8 | FS_NULL=0, 9 | LITTLEFS, 10 | FATFS, 11 | }; 12 | 13 | class VFSFILE 14 | { 15 | public : 16 | VFSFILE(int type); 17 | ~VFSFILE(); 18 | 19 | int mkdir(const char *path); 20 | int mkdir(const String& path); 21 | 22 | int remove(const char *path); 23 | int remove(const String& path); 24 | 25 | int exist(const char *path); 26 | int exist(const String& path); 27 | 28 | int rename(const char *pathFrom,const char *pathTo); 29 | int rename(const String& pathFrom, const String& pathTo); 30 | 31 | TUYA_DIR openDir(const char *path); 32 | TUYA_DIR openDir(const String& path); 33 | int closeDir(TUYA_DIR dir); 34 | TUYA_FILEINFO readDir(TUYA_DIR dir); 35 | int getDirName(TUYA_FILEINFO info,const char** name); 36 | int isDirectory(const char *path); 37 | 38 | TUYA_FILE open(const char *path); 39 | TUYA_FILE open(const String& path); 40 | 41 | TUYA_FILE open(const char *path, const char * mode); 42 | TUYA_FILE open(const String& path, const char * mode); 43 | 44 | int close(TUYA_FILE fd); 45 | char read(TUYA_FILE fd); 46 | int read(const char *buf,int size,TUYA_FILE fd); 47 | int readtillN(char *buf, int size,TUYA_FILE fd) ; 48 | int write(const char *buf,int size,TUYA_FILE fd); 49 | void flush(TUYA_FILE fd); 50 | int feof(TUYA_FILE fd); 51 | int lseek(TUYA_FILE fd,int offs, int whence); 52 | int position(TUYA_FILE fd); 53 | int filesize(const char *filepath); 54 | 55 | private : 56 | FS_API *impl; 57 | }; 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /libraries/FS/src/fs_api.h: -------------------------------------------------------------------------------- 1 | #ifndef __FS_API_H__ 2 | #define __FS_API_H__ 3 | 4 | extern "C" 5 | { 6 | #include "tal_fs.h" 7 | } 8 | 9 | class FS_API 10 | { 11 | public : 12 | FS_API() {} 13 | virtual int mkdir(const char *path) = 0; 14 | virtual int remove(const char *path) = 0; 15 | virtual int exist(const char *path) = 0; 16 | virtual int rename(const char *pathFrom,const char *pathTo) = 0; 17 | 18 | virtual TUYA_DIR openDir(const char *path) = 0; 19 | virtual int closeDir(TUYA_DIR dir) = 0; 20 | virtual TUYA_FILEINFO readDir(TUYA_DIR dir) = 0; 21 | virtual int getDirName(TUYA_FILEINFO info,const char** name) = 0; 22 | virtual int isDirectory(const char *path) = 0; 23 | 24 | virtual TUYA_FILE open(const char *path) = 0; 25 | virtual TUYA_FILE open(const char *path, const char * mode) = 0; 26 | virtual int close(TUYA_FILE fd) = 0; 27 | virtual char read(TUYA_FILE fd) = 0; 28 | virtual int read(const char *buf,int size,TUYA_FILE fd) = 0; 29 | virtual int readtillN(char *buf, int size,TUYA_FILE fd) = 0; 30 | virtual int write(const char *buf,int size,TUYA_FILE fd) = 0; 31 | virtual void flush(TUYA_FILE fd) = 0; 32 | virtual int feof(TUYA_FILE fd) = 0; 33 | virtual int lseek(TUYA_FILE fd,int offs, int whence) = 0; 34 | virtual int position(TUYA_FILE fd) = 0; 35 | virtual int filesize(const char *filepath) = 0; 36 | 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /libraries/HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * BasicHTTPClient.ino 3 | * 4 | * Created on: 24.05.2015 5 | * 6 | */ 7 | 8 | #include 9 | 10 | #include 11 | 12 | #include 13 | #define USE_SERIAL Serial 14 | 15 | HTTPClient http; 16 | void setup() { 17 | 18 | USE_SERIAL.begin(115200); 19 | 20 | USE_SERIAL.println(); 21 | USE_SERIAL.println(); 22 | USE_SERIAL.println(); 23 | 24 | for(uint8_t t = 4; t > 0; t--) { 25 | USE_SERIAL.print("[SETUP] WAIT ..."); 26 | USE_SERIAL.println(t); 27 | USE_SERIAL.flush(); 28 | delay(1000); 29 | } 30 | 31 | WiFi.begin("your_ssid", "your_passwd"); 32 | 33 | } 34 | 35 | void loop() { 36 | // wait for WiFi connection 37 | if((WiFi.status() == WSS_GOT_IP)) { 38 | USE_SERIAL.println("HTTP ..."); 39 | http.begin("http://www.moe.gov.cn/jyb_sjzl/"); 40 | 41 | http_client_response_t http_response ={0}; 42 | 43 | http_client_header_t headers[] = {{.key = "Content-Type", .value = "application/json"}}; 44 | 45 | int headers_length = sizeof(headers)/sizeof(http_client_header_t); 46 | USE_SERIAL.println("GET ..."); 47 | http_client_status_t http_status = http.GET(headers,(uint8_t)headers_length,NULL,0,&http_response); 48 | 49 | if(http_status != HTTP_CLIENT_SUCCESS) 50 | { 51 | USE_SERIAL.print("http_request_send error:"); 52 | USE_SERIAL.println(http_status); 53 | }else{ 54 | char * payload = (char *)http_response.body; 55 | USE_SERIAL.println(payload); 56 | } 57 | http.end(&http_response); 58 | } 59 | delay(5000); 60 | } 61 | -------------------------------------------------------------------------------- /libraries/HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino: -------------------------------------------------------------------------------- 1 | /** 2 | BasicHTTPSClient.ino 3 | 4 | Created on: 14.10.2018 5 | 6 | */ 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | #define DPID_SWITCH 1 16 | 17 | const char* rootCACertificate = R"EOF( 18 | -----BEGIN CERTIFICATE----- 19 | MIIEXjCCA0agAwIBAgITB3MSSkvL1E7HtTvq8ZSELToPoTANBgkqhkiG9w0BAQsF 20 | ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6 21 | b24gUm9vdCBDQSAxMB4XDTIyMDgyMzIyMjUzMFoXDTMwMDgyMzIyMjUzMFowPDEL 22 | MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEcMBoGA1UEAxMTQW1hem9uIFJT 23 | QSAyMDQ4IE0wMjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALtDGMZa 24 | qHneKei1by6+pUPPLljTB143Si6VpEWPc6mSkFhZb/6qrkZyoHlQLbDYnI2D7hD0 25 | sdzEqfnuAjIsuXQLG3A8TvX6V3oFNBFVe8NlLJHvBseKY88saLwufxkZVwk74g4n 26 | WlNMXzla9Y5F3wwRHwMVH443xGz6UtGSZSqQ94eFx5X7Tlqt8whi8qCaKdZ5rNak 27 | +r9nUThOeClqFd4oXych//Rc7Y0eX1KNWHYSI1Nk31mYgiK3JvH063g+K9tHA63Z 28 | eTgKgndlh+WI+zv7i44HepRZjA1FYwYZ9Vv/9UkC5Yz8/yU65fgjaE+wVHM4e/Yy 29 | C2osrPWE7gJ+dXMCAwEAAaOCAVowggFWMBIGA1UdEwEB/wQIMAYBAf8CAQAwDgYD 30 | VR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAdBgNV 31 | HQ4EFgQUwDFSzVpQw4J8dHHOy+mc+XrrguIwHwYDVR0jBBgwFoAUhBjMhTTsvAyU 32 | lC4IWZzHshBOCggwewYIKwYBBQUHAQEEbzBtMC8GCCsGAQUFBzABhiNodHRwOi8v 33 | b2NzcC5yb290Y2ExLmFtYXpvbnRydXN0LmNvbTA6BggrBgEFBQcwAoYuaHR0cDov 34 | L2NydC5yb290Y2ExLmFtYXpvbnRydXN0LmNvbS9yb290Y2ExLmNlcjA/BgNVHR8E 35 | ODA2MDSgMqAwhi5odHRwOi8vY3JsLnJvb3RjYTEuYW1hem9udHJ1c3QuY29tL3Jv 36 | b3RjYTEuY3JsMBMGA1UdIAQMMAowCAYGZ4EMAQIBMA0GCSqGSIb3DQEBCwUAA4IB 37 | AQAtTi6Fs0Azfi+iwm7jrz+CSxHH+uHl7Law3MQSXVtR8RV53PtR6r/6gNpqlzdo 38 | Zq4FKbADi1v9Bun8RY8D51uedRfjsbeodizeBB8nXmeyD33Ep7VATj4ozcd31YFV 39 | fgRhvTSxNrrTlNpWkUk0m3BMPv8sg381HhA6uEYokE5q9uws/3YkKqRiEz3TsaWm 40 | JqIRZhMbgAfp7O7FUwFIb7UIspogZSKxPIWJpxiPo3TcBambbVtQOcNRWz5qCQdD 41 | slI2yayq0n2TXoHyNCLEH8rpsJRVILFsg0jc7BaFrMnF462+ajSehgj12IidNeRN 42 | 4zl+EoNaWdpnWndvSpAEkq2P 43 | -----END CERTIFICATE----- 44 | )EOF"; 45 | 46 | HTTPClient http; 47 | void setup() { 48 | 49 | Serial.begin(115200); 50 | Log.setLevel(LogClass::DEBUG); 51 | 52 | Serial.println(); 53 | Serial.println(); 54 | Serial.println(); 55 | 56 | for(uint8_t t = 4; t > 0; t--) { 57 | Serial.print("[SETUP] WAIT ..."); 58 | Serial.println(t); 59 | Serial.flush(); 60 | delay(1000); 61 | } 62 | 63 | WiFi.begin("your_ssid", "your_passwd"); 64 | } 65 | 66 | http_client_response_t http_response ={0}; 67 | 68 | void loop() { 69 | // wait for WiFi connection 70 | if((WiFi.status() == WSS_GOT_IP)) { 71 | delay(2000); 72 | Serial.println("http get"); 73 | 74 | http.begin("https://httpbin.org/get"); 75 | 76 | http_client_header_t headers[] = {{.key = "Content-Type", .value = "application/json"}}; 77 | 78 | int headers_length = sizeof(headers)/sizeof(http_client_header_t); 79 | 80 | uint16_t cacert_len = strlen(rootCACertificate)+1; 81 | 82 | http_client_status_t http_status = http.GET(headers,(uint8_t)headers_length,(uint8_t *)rootCACertificate,cacert_len,&http_response); 83 | 84 | if(http_status != HTTP_CLIENT_SUCCESS) 85 | { 86 | Serial.print("http_request_send error:"); 87 | Serial.println(http_status); 88 | }else{ 89 | char * payload = (char *)http_response.body; 90 | Serial.println(payload); 91 | } 92 | http.end(&http_response); 93 | } 94 | delay(5000); 95 | } 96 | 97 | -------------------------------------------------------------------------------- /libraries/HTTPClient/library.properties: -------------------------------------------------------------------------------- 1 | name=HTTPClient 2 | architectures=* 3 | -------------------------------------------------------------------------------- /libraries/HTTPClient/src/HTTPClient.h: -------------------------------------------------------------------------------- 1 | #ifndef HTTPClient_H_ 2 | #define HTTPClient_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C"{ 6 | #endif 7 | 8 | #include "http_client_interface.h" 9 | 10 | class HTTPClient 11 | { 12 | public: 13 | HTTPClient(); 14 | ~HTTPClient(); 15 | 16 | bool begin(String url); 17 | http_client_status_t GET (http_client_header_t *headers,uint8_t headers_length,const uint8_t *ca, size_t ca_len, http_client_response_t *response); 18 | http_client_status_t POST (http_client_header_t *headers,uint8_t headers_length, const uint8_t *ca, size_t ca_len, const uint8_t *body, http_client_response_t *response); 19 | http_client_status_t PUT (http_client_header_t *headers,uint8_t headers_length, const uint8_t *ca, size_t ca_len, const uint8_t *body, http_client_response_t *response); 20 | http_client_status_t PATCH(http_client_header_t *headers,uint8_t headers_length, const uint8_t *ca, size_t ca_len, const uint8_t *body, http_client_response_t *response); 21 | bool end(http_client_response_t *response); 22 | 23 | protected: 24 | String _host; 25 | uint16_t _port = 0; 26 | String _path; 27 | String _protocol; 28 | int sendRequest(char * method ,const uint8_t *body, http_client_response_t *response); 29 | bool beginInternal(String url); 30 | }; 31 | 32 | 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /libraries/LittleFS/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For LittleFS 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | FS_LITTLEFS KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | mkdir KEYWORD2 16 | remove KEYWORD2 17 | exist KEYWORD2 18 | rename KEYWORD2 19 | 20 | openDir KEYWORD2 21 | closeDir KEYWORD2 22 | readDir KEYWORD2 23 | getDirName KEYWORD2 24 | isDirectory KEYWORD2 25 | 26 | open KEYWORD2 27 | close KEYWORD2 28 | read KEYWORD2 29 | readtillN KEYWORD2 30 | write KEYWORD2 31 | flush KEYWORD2 32 | feof KEYWORD2 33 | lseek KEYWORD2 34 | position KEYWORD2 35 | filesize KEYWORD2 36 | 37 | ####################################### 38 | # Constants (LITERAL1) 39 | ####################################### 40 | 41 | -------------------------------------------------------------------------------- /libraries/LittleFS/library.properties: -------------------------------------------------------------------------------- 1 | name=LittleFS 2 | version=0.0.2 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence= 6 | paragraph= 7 | category=Signal Input/Output 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/LittleFS/src/LittleFS.h: -------------------------------------------------------------------------------- 1 | #ifndef _LITTLEFS_H_ 2 | #define _LITTLEFS_H_ 3 | 4 | #include 5 | #include "fs_api.h" 6 | 7 | extern "C"{ 8 | #include "lfs.h" 9 | #include "tal_kv.h" 10 | #include "tkl_flash.h" 11 | #include "tal_fs.h" 12 | #include "tal_log.h" 13 | 14 | } 15 | 16 | class FS_LITTLEFS : public FS_API 17 | { 18 | public : 19 | FS_LITTLEFS(); 20 | ~FS_LITTLEFS(); 21 | 22 | virtual int mkdir(const char *path); 23 | virtual int remove(const char *path); 24 | virtual int exist(const char *path); 25 | virtual int rename(const char *pathFrom,const char *pathTo); 26 | 27 | virtual TUYA_DIR openDir(const char *path); 28 | virtual int closeDir(TUYA_DIR dir); 29 | virtual TUYA_FILEINFO readDir(TUYA_DIR dir); 30 | virtual int getDirName(TUYA_FILEINFO info,const char** name); 31 | virtual int isDirectory(const char *path); 32 | 33 | 34 | virtual TUYA_FILE open(const char *path); 35 | virtual TUYA_FILE open(const char *path, const char* mode); 36 | virtual int close(TUYA_FILE fd); 37 | virtual char read(TUYA_FILE fd); 38 | virtual int read(const char *buf,int size,TUYA_FILE fd); 39 | virtual int readtillN(char *buf, int size,TUYA_FILE fd); 40 | virtual int write(const char *buf,int size,TUYA_FILE fd); 41 | virtual void flush(TUYA_FILE fd); 42 | virtual int feof(TUYA_FILE fd); 43 | virtual int lseek(TUYA_FILE fd,int offs, int whence); 44 | virtual int position(TUYA_FILE fd); 45 | virtual int filesize(const char *filepath); 46 | 47 | 48 | protected: 49 | bool ismounted; 50 | }; 51 | 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /libraries/Log/examples/logOutput/logOutput.ino: -------------------------------------------------------------------------------- 1 | #include "Log.h" 2 | 3 | void setup(void) 4 | { 5 | Serial.begin(115200); 6 | Log.begin(1024); 7 | } 8 | 9 | void loop() 10 | { 11 | PR_DEBUG("Hello"); 12 | delay(1000); 13 | } -------------------------------------------------------------------------------- /libraries/Log/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Log 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Log KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | setLevel KEYWORD2 16 | enableMsInfo KEYWORD2 17 | enableColor KEYWORD2 18 | setColor KEYWORD2 19 | 20 | ####################################### 21 | # Constants (LITERAL1) 22 | ####################################### 23 | 24 | ERROR LITERAL1 25 | WARN LITERAL1 26 | NOTICE LITERAL1 27 | INFO LITERAL1 28 | DEBUG LITERAL1 29 | TRACE LITERAL1 30 | 31 | MODE_DEFAULT LITERAL1 32 | MODE_HIGH_LIGHT LITERAL1 33 | MODE_UNDER_LINE LITERAL1 34 | MODE_FLASH LITERAL1 35 | MODE_REVERSE LITERAL1 36 | 37 | FONT_BLACK LITERAL1 38 | FONT_RED LITERAL1 39 | FONT_GREEN LITERAL1 40 | FONT_YELLOW LITERAL1 41 | FONT_BLUE LITERAL1 42 | FONT_PURPLE LITERAL1 43 | FONT_CYAN LITERAL1 44 | FONT_WHITE LITERAL1 45 | FONT_DEFAULT LITERAL1 46 | 47 | BG_BLACK LITERAL1 48 | BG_RED LITERAL1 49 | BG_GREEN LITERAL1 50 | BG_YELLOW LITERAL1 51 | BG_BLUE LITERAL1 52 | BG_PURPLE LITERAL1 53 | BG_CYAN LITERAL1 54 | BG_WHITE LITERAL1 55 | BG_DEFAULT LITERAL1 56 | -------------------------------------------------------------------------------- /libraries/Log/library.properties: -------------------------------------------------------------------------------- 1 | name=Log 2 | version=0.0.3 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence=Tuya open sdk log config. 6 | paragraph= 7 | category=Other 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/Log/src/Log.cpp: -------------------------------------------------------------------------------- 1 | #include "Log.h" 2 | 3 | using namespace arduino; 4 | 5 | extern "C" void _log_output_cb(const char *str) 6 | { 7 | Serial.print(str); 8 | } 9 | 10 | LogClass::LogClass(void) 11 | { 12 | 13 | } 14 | 15 | LogClass::~LogClass() 16 | { 17 | 18 | } 19 | 20 | void LogClass::begin(int buffer_size) 21 | { 22 | tal_log_init(TAL_LOG_LEVEL_DEBUG, buffer_size, (TAL_LOG_OUTPUT_CB)_log_output_cb); 23 | tal_log_color_enable_set(FALSE); 24 | } 25 | 26 | void LogClass::begin() 27 | { 28 | begin(1024); 29 | } 30 | 31 | void LogClass::setLevel(LogLevel level) 32 | { 33 | tal_log_set_level(static_cast(level)); 34 | } 35 | 36 | void LogClass::enableMsInfo(bool enable) 37 | { 38 | tal_log_set_ms_info(enable); 39 | } 40 | 41 | void LogClass::enableColor(bool enable) 42 | { 43 | tal_log_color_enable_set(enable); 44 | } 45 | 46 | void LogClass::setColor(LogLevel level, ColorMode mode, ColorFont font, ColorBG background) 47 | { 48 | tal_log_color_set(static_cast(level), static_cast(mode), static_cast(font), static_cast(background)); 49 | } 50 | 51 | LogClass Log; 52 | -------------------------------------------------------------------------------- /libraries/Log/src/Log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Arduino.h" 4 | 5 | extern "C" { 6 | #include "tal_log.h" 7 | } 8 | 9 | namespace arduino { 10 | 11 | class LogClass { 12 | public: 13 | enum LogLevel { 14 | ERROR, WARN, NOTICE, INFO, DEBUG, TRACE 15 | }; 16 | enum ColorMode { 17 | MODE_DEFAULT = 0, MODE_HIGH_LIGHT, MODE_UNDER_LINE = 4, MODE_FLASH = 5, MODE_REVERSE = 7 18 | }; 19 | enum ColorFont { 20 | FONT_BLACK = 30, FONT_RED, FONT_GREEN, FONT_YELLOW, FONT_BLUE, FONT_PURPLE, FONT_CYAN, FONT_WHITE, FONT_DEFAULT = 39 21 | }; 22 | enum ColorBG { // background color 23 | BG_BLACK = 40, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_PURPLE, BG_CYAN, BG_WHITE, BG_DEFAULT = 49 24 | }; 25 | 26 | LogClass(); 27 | ~LogClass(); 28 | 29 | void begin(); 30 | void begin(int buffer); 31 | 32 | void setLevel(LogLevel level); 33 | 34 | void enableMsInfo(bool enable); 35 | 36 | void enableColor(bool enable); 37 | void setColor(LogLevel level, ColorMode mode, ColorFont font, ColorBG background); 38 | }; 39 | 40 | } // namespace arduino 41 | 42 | extern arduino::LogClass Log; 43 | -------------------------------------------------------------------------------- /libraries/MQTTClient/examples/mqtt_auth/mqtt_auth.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Basic MQTT example with Authentication 3 | 4 | - connects to an MQTT server, providing username 5 | and password 6 | - publishes "hello world" to the topic "outTopic" 7 | - subscribes to the topic "inTopic" 8 | */ 9 | 10 | 11 | #include 12 | #include 13 | 14 | // Update these with values suitable for your network. 15 | const char *ssid = "your_ssid"; 16 | const char *pass = "your_passwd"; 17 | 18 | //MQTT broker 19 | const char *mqtt_broker = "broker.emqx.io"; 20 | const char *topic = "youroutTopic"; 21 | const char *mqtt_username = "emqx"; // No necessarily 22 | const char *mqtt_password = "public"; // No necessarily 23 | const int mqtt_port = 1883; 24 | 25 | void *cli; 26 | MQTTClient mqtt; 27 | 28 | void callback(void *client, uint16_t msgid, const mqtt_client_message_t *msg, void *userdata){ 29 | // handle message arrived 30 | } 31 | 32 | void setup() 33 | { 34 | Serial.begin(115200); 35 | WiFi.begin(ssid,pass); 36 | while(WiFi.status() != WSS_GOT_IP) 37 | { 38 | delay(500); 39 | Serial.println("..."); 40 | } 41 | Serial.println(""); 42 | Serial.println(""); 43 | Serial.println("WiFi connected!"); 44 | Serial.println("IP Address:"); 45 | Serial.println(WiFi.localIP()); 46 | 47 | mqtt_client_config_t config; 48 | config.cacert = NULL; 49 | config.cacert_len = 0; 50 | config.host = mqtt_broker; 51 | config.port = mqtt_port; 52 | config.keepalive = 15; 53 | config.timeout_ms = 100; 54 | config.username = mqtt_username; 55 | config.password = mqtt_password; 56 | config.clientid = "ty_test_id"; 57 | 58 | const char *payload="asadadaadadasd"; 59 | mqtt.SetCallback(callback); 60 | cli = mqtt.init(&config); 61 | mqtt.connect(cli); 62 | mqtt.publish(cli,topic,payload); 63 | mqtt.subscribe(cli,topic); 64 | } 65 | 66 | void loop() 67 | { 68 | mqtt.loop(cli); 69 | } 70 | -------------------------------------------------------------------------------- /libraries/MQTTClient/examples/mqtt_basic/mqtt_basic.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Basic MQTT example 3 | 4 | This sketch demonstrates the basic capabilities of the library. 5 | It connects to an MQTT server then: 6 | - publishes "hello world" to the topic "outTopic" 7 | - subscribes to the topic "inTopic", printing out any messages 8 | it receives. NB - it assumes the received payloads are strings not binary 9 | 10 | It will reconnect to the server if the connection is lost using a blocking 11 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to 12 | achieve the same result without blocking the main loop. 13 | 14 | */ 15 | /* 16 | Basic MQTT example 17 | 18 | This sketch demonstrates the basic capabilities of the library. 19 | It connects to an MQTT server then: 20 | - publishes "hello world" to the topic "outTopic" 21 | - subscribes to the topic "inTopic", printing out any messages 22 | it receives. NB - it assumes the received payloads are strings not binary 23 | 24 | It will reconnect to the server if the connection is lost using a blocking 25 | reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to 26 | achieve the same result without blocking the main loop. 27 | 28 | */ 29 | 30 | #include 31 | #include 32 | 33 | // Update these with values suitable for your network. 34 | const char * ssid ="your_ssid"; 35 | const char * pass = "your_passwd"; 36 | 37 | //MQTT broker 38 | const char *mqtt_broker = "broker.emqx.io"; 39 | const char *intopic = "inTopic"; 40 | const char *outtopic = "youroutTopic"; 41 | const char *mqtt_username = "emqx"; 42 | const char *mqtt_password ="public"; 43 | const int mqtt_port = 1883; 44 | 45 | void *cli; 46 | MQTTClient mqtt; 47 | 48 | void mqtt_message_cb(void *client, uint16_t msgid, const mqtt_client_message_t *msg, void *userdata) 49 | { 50 | Serial.print("recv message TopicName: "); 51 | Serial.print( msg->topic); 52 | Serial.print("payload len:"); 53 | Serial.println(msg->length); 54 | char message [256] = {0}; 55 | memcpy(message ,msg->payload ,msg->length); 56 | Serial.print("message:"); 57 | Serial.println(message); 58 | } 59 | void setup() 60 | { 61 | Serial.begin(115200); 62 | WiFi.begin(ssid,pass); 63 | 64 | while (WiFi.status() != WSS_GOT_IP) { 65 | delay(500); 66 | Serial.print("."); 67 | } 68 | 69 | Serial.println(""); 70 | Serial.println("WiFi connected"); 71 | Serial.println("IP address: "); 72 | Serial.println(WiFi.localIP()); 73 | Serial.println("wifi connected "); 74 | mqtt_client_config_t config; 75 | config.cacert = NULL; 76 | config.cacert_len = 0; 77 | config.host = mqtt_broker; 78 | config.port = mqtt_port; 79 | config.keepalive = 15; 80 | config.timeout_ms = 100; 81 | config.username = mqtt_username; 82 | config.password = mqtt_password; 83 | config.clientid = "ty_test_id"; 84 | 85 | const char *payload="asadadaadadasd"; 86 | 87 | mqtt.SetCallback(mqtt_message_cb); 88 | cli = mqtt.init(&config); 89 | mqtt.connect(cli); 90 | mqtt.publish(cli,outtopic,payload); 91 | mqtt.subscribe(cli,intopic); 92 | } 93 | 94 | void loop(){ 95 | 96 | mqtt.loop(cli); 97 | } -------------------------------------------------------------------------------- /libraries/MQTTClient/examples/mqtt_publish_in_callback/mqtt_publish_in_callback.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Publishing in the callback 3 | 4 | - connects to an MQTT server 5 | - subscribes to the topic "inTopic" 6 | - when a message is received, republishes it to "outTopic" 7 | 8 | This example shows how to publish messages within the 9 | callback function. The callback function header needs to 10 | be declared before the PubSubClient constructor and the 11 | actual callback defined afterwards. 12 | This ensures the client reference in the callback function 13 | is valid. 14 | 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | // Update these with values suitable for your network. 21 | 22 | const char* ssid = "your_ssid"; 23 | const char* password = "your_passwd"; 24 | const char* mqtt_server = "broker.emqx.io"; 25 | const char *mqtt_username = "emqx"; 26 | const char *mqtt_password ="public"; 27 | 28 | void *cli; 29 | MQTTClient mqtt; 30 | 31 | // Callback function 32 | void mqtt_message_cb(void *client, uint16_t msgid, const mqtt_client_message_t *msg, void *userdata) 33 | { 34 | char message[256] = {0}; 35 | memcpy(message ,msg->payload ,msg->length); 36 | Serial.print("message:"); 37 | Serial.println(message); 38 | mqtt.publish(cli,"youroutTopic",message); 39 | } 40 | 41 | void setup() 42 | { 43 | Log.setLevel(LogClass::DEBUG); 44 | Serial.begin(115200); 45 | Serial.println(); 46 | Serial.print("Connecting to "); 47 | Serial.println(ssid); 48 | 49 | WiFi.begin(ssid, password); 50 | 51 | while (WiFi.status() != WSS_GOT_IP) { 52 | delay(500); 53 | Serial.print("."); 54 | } 55 | 56 | Serial.println(""); 57 | Serial.println("WiFi connected"); 58 | Serial.println("IP address: "); 59 | Serial.println(WiFi.localIP()); 60 | 61 | mqtt_client_config_t config = {0}; 62 | config.cacert = NULL; 63 | config.cacert_len = 0; 64 | config.host = mqtt_server; 65 | config.port = 1883; 66 | config.keepalive = 120; 67 | config.timeout_ms = 8000; 68 | config.username = mqtt_username; 69 | config.password = mqtt_password; 70 | config.clientid = "ty_test_id"; 71 | 72 | mqtt.SetCallback(mqtt_message_cb); 73 | cli = mqtt.init(&config); 74 | } 75 | 76 | void loop() 77 | { 78 | if (!mqtt.connected()) { 79 | mqtt.connect(cli); 80 | mqtt.publish(cli,"youroutTopic","hello world"); 81 | mqtt.subscribe(cli,"inTopic"); 82 | } 83 | mqtt.loop(cli); 84 | } 85 | -------------------------------------------------------------------------------- /libraries/MQTTClient/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For MQTTClient 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | MQTTClient KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | connect KEYWORD2 16 | disconnect KEYWORD2 17 | publish KEYWORD2 18 | publish_P KEYWORD2 19 | beginPublish KEYWORD2 20 | endPublish KEYWORD2 21 | write KEYWORD2 22 | subscribe KEYWORD2 23 | unsubscribe KEYWORD2 24 | loop KEYWORD2 25 | connected KEYWORD2 26 | setServer KEYWORD2 27 | setCallback KEYWORD2 28 | setClient KEYWORD2 29 | setStream KEYWORD2 30 | setKeepAlive KEYWORD2 31 | setBufferSize KEYWORD2 32 | setSocketTimeout KEYWORD2 33 | 34 | ####################################### 35 | # Constants (LITERAL1) 36 | ####################################### 37 | -------------------------------------------------------------------------------- /libraries/MQTTClient/library.properties: -------------------------------------------------------------------------------- 1 | name=MQTTClient 2 | 3 | architectures=* 4 | -------------------------------------------------------------------------------- /libraries/MQTTClient/src/MQTTClient.h: -------------------------------------------------------------------------------- 1 | #ifndef __MQTTCLIENT_H__ 2 | #define __MQTTCLIENT_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | #include "mqtt_client_interface.h" 8 | #include "tal_log.h" 9 | #define MQTT_MSG_CB void (*callback)(void *client, uint16_t msgid, const mqtt_client_message_t *msg, void *userdata) 10 | class MQTTClient 11 | { 12 | private: 13 | MQTT_MSG_CB; 14 | bool is_connected =false; 15 | public: 16 | MQTTClient(); 17 | ~MQTTClient(); 18 | 19 | MQTTClient& SetCallback(MQTT_MSG_CB); 20 | void* init(mqtt_client_config_t *config); 21 | int connect(void *client); 22 | int connected(); 23 | int disconnect(void *client); 24 | int publish(void *client, const char *topic, const char *payload); 25 | 26 | int subscribe(void *client,const char *topic); 27 | int unsubscribe(void *client,const char *topic); 28 | int loop(void *client); 29 | void free(void *client); 30 | }; 31 | 32 | 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /libraries/SPI/examples/spiDemo/spiDemo.ino: -------------------------------------------------------------------------------- 1 | // SPI Pin T2 2 | // SCK -----> 14 3 | // MOSI -----> 16 4 | // MISO -----> 17 5 | // CS -----> 15 6 | 7 | #include "SPI.h" 8 | 9 | void setup() { 10 | SPI.begin(); 11 | } 12 | 13 | char str[] = "Hello Arduino."; 14 | 15 | void loop() { 16 | // T2 clock setting suggest 1M ~ 8M 17 | // T2 only support MSBFIRST 18 | SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); 19 | SPI.transfer(str, 15); 20 | SPI.endTransaction(); 21 | 22 | delay(3*1000); 23 | } 24 | -------------------------------------------------------------------------------- /libraries/SPI/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For SPI 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | SPI KEYWORD1 10 | SPIClassTuyaOpen KEYWORD1 11 | 12 | ####################################### 13 | # Methods and Functions (KEYWORD2) 14 | ####################################### 15 | 16 | transfer KEYWORD2 17 | transfer16 KEYWORD2 18 | 19 | usingInterrupt KEYWORD2 20 | notUsingInterrupt KEYWORD2 21 | beginTransaction KEYWORD2 22 | endTransaction KEYWORD2 23 | 24 | attachInterrupt KEYWORD2 25 | detachInterrupt KEYWORD2 26 | 27 | begin KEYWORD2 28 | end KEYWORD2 29 | 30 | setCS KEYWORD2 31 | 32 | ####################################### 33 | # Constants (LITERAL1) 34 | ####################################### 35 | 36 | -------------------------------------------------------------------------------- /libraries/SPI/library.properties: -------------------------------------------------------------------------------- 1 | name=SPI 2 | version=0.0.2 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence=Enables the communication with devices that use the Serial Peripheral Interface (SPI) Bus. 6 | paragraph= 7 | category=Signal Input/Output 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/SPI/src/SPI.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | extern "C" { 7 | #include "tkl_spi.h" 8 | } 9 | 10 | namespace arduino { 11 | 12 | class SPIClassTuyaOpen : public HardwareSPI { 13 | public: 14 | SPIClassTuyaOpen(); 15 | SPIClassTuyaOpen(TUYA_SPI_NUM_E port); 16 | SPIClassTuyaOpen(TUYA_SPI_NUM_E port, TUYA_SPI_BASE_CFG_T cfg); 17 | ~SPIClassTuyaOpen(); 18 | 19 | uint8_t transfer(uint8_t data); 20 | uint16_t transfer16(uint16_t data); 21 | void transfer(void *buf, size_t count); 22 | 23 | // Transaction Functions 24 | void usingInterrupt(int interruptNumber) override { /* noop */ } 25 | void notUsingInterrupt(int interruptNumber) override { /* noop */ } 26 | // void beginTransaction(void) { /* noop */ } 27 | void beginTransaction(SPISettings settings) override; 28 | void endTransaction(void) override; 29 | 30 | // SPI Configuration methods 31 | void attachInterrupt() override { /* noop */ } 32 | void detachInterrupt() override { /* noop */ } 33 | 34 | void begin(); 35 | void begin(int csPin); 36 | void end(); 37 | 38 | void setCS(int csPin) { _csPin = csPin; } 39 | private: 40 | void _spiConfigInitAlwaysInline(TUYA_SPI_NUM_E port, TUYA_SPI_BASE_CFG_T cfg) __attribute__((__always_inline__)) { 41 | this->_port = port; 42 | memcpy(&_cfg, &cfg, sizeof(TUYA_SPI_BASE_CFG_T)); 43 | } 44 | 45 | TUYA_SPI_NUM_E _port; 46 | TUYA_SPI_BASE_CFG_T _cfg; 47 | 48 | int _csPin = -1; 49 | 50 | uint8_t _isBeginTransaction = 0; 51 | }; 52 | 53 | } // namespace arduino 54 | 55 | extern arduino::SPIClassTuyaOpen SPI; 56 | -------------------------------------------------------------------------------- /libraries/Ticker/examples/Blinker/Blinker.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Ticker blinker; 4 | Ticker changer; 5 | 6 | float blinkerPace = 0.1; //seconds 7 | 8 | int ledStatus = 0; 9 | 10 | void blinkCallback() { 11 | ledStatus = !ledStatus; 12 | digitalWrite(LED_BUILTIN, ledStatus); 13 | } 14 | 15 | void changeCallback() { 16 | blinkerPace = 0.5; 17 | 18 | blinker.detach(); 19 | blinker.attach(blinkerPace, blinkCallback); 20 | } 21 | 22 | void setup() { 23 | pinMode(LED_BUILTIN, OUTPUT); 24 | digitalWrite(LED_BUILTIN, LOW); 25 | ledStatus = LOW; 26 | 27 | blinker.attach(blinkerPace, blinkCallback); 28 | changer.once(10, changeCallback); 29 | } 30 | 31 | void loop() {} 32 | 33 | -------------------------------------------------------------------------------- /libraries/Ticker/examples/TickerParameter/TickerParameter.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int arg_1s = 1; 4 | Ticker ticker_1s; 5 | 6 | int arg_5s = 5; 7 | Ticker ticker_5s; 8 | 9 | void ticker_callback(int *arg) 10 | { 11 | Serial.print("arg: "); 12 | Serial.println(*arg); 13 | } 14 | 15 | void setup() { 16 | Serial.begin(115200); 17 | 18 | // every 1 s, call ticker_callback() 19 | ticker_1s.attach(1, ticker_callback, &arg_1s); 20 | 21 | // every 5 s, call ticker_callback() 22 | ticker_5s.attach(5, ticker_callback, &arg_5s); 23 | } 24 | 25 | void loop() {} 26 | -------------------------------------------------------------------------------- /libraries/Ticker/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Ticker 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | Ticker KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | attach KEYWORD2 16 | attach_ms KEYWORD2 17 | once KEYWORD2 18 | once_ms KEYWORD2 19 | detach KEYWORD2 20 | active KEYWORD2 21 | 22 | ####################################### 23 | # Constants (LITERAL1) 24 | ####################################### 25 | 26 | -------------------------------------------------------------------------------- /libraries/Ticker/library.properties: -------------------------------------------------------------------------------- 1 | name=Ticker 2 | version=0.0.1 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence=Software timer 6 | paragraph=Allows a function to be called within a given time interval. 7 | category=Timing 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/Ticker/src/Ticker.cpp: -------------------------------------------------------------------------------- 1 | #include "Ticker.h" 2 | 3 | extern "C" { 4 | #include "tal_log.h" 5 | } 6 | 7 | Ticker::Ticker() : 8 | _timer_handle(nullptr), 9 | _callback_function(nullptr), 10 | _arg(nullptr), 11 | _callback_function_with_arg(nullptr) 12 | { 13 | 14 | } 15 | 16 | Ticker::~Ticker() { 17 | detach(); 18 | } 19 | 20 | void Ticker::_attach_ms(uint64_t ms, bool repeat, TAL_TIMER_CB callback, void *arg) 21 | { 22 | OPERATE_RET rt = OPRT_OK; 23 | TIMER_TYPE timerType = TAL_TIMER_ONCE; 24 | 25 | if (!_timer_handle) { 26 | rt = tal_sw_timer_create(callback, arg, &_timer_handle); 27 | if (OPRT_OK != rt) { 28 | PR_ERR("Create software timer fail, %d", rt); 29 | return; 30 | } 31 | } 32 | 33 | timerType = (repeat) ? (TAL_TIMER_CYCLE) : (TAL_TIMER_ONCE); 34 | tal_sw_timer_start(_timer_handle, ms, timerType); 35 | } 36 | 37 | void Ticker::detach() { 38 | if (_timer_handle) { 39 | tal_sw_timer_stop(_timer_handle); 40 | tal_sw_timer_delete(_timer_handle); 41 | _timer_handle = nullptr; 42 | _callback_function = nullptr; 43 | _arg = nullptr; 44 | _callback_function_with_arg = nullptr; 45 | } 46 | } 47 | 48 | bool Ticker::active() const { 49 | if (!_timer_handle) { 50 | return false; 51 | } 52 | return tal_sw_timer_is_running(_timer_handle); 53 | } 54 | 55 | void Ticker::_static_callback(TIMER_ID timer_id, void *arg) { 56 | Ticker *_this = reinterpret_cast(arg); 57 | if (_this && _this->_callback_function) { 58 | _this->_callback_function(); 59 | } 60 | if (_this && _this->_callback_function_with_arg) { 61 | _this->_callback_function_with_arg(_this->_arg); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /libraries/Ticker/src/Ticker.h: -------------------------------------------------------------------------------- 1 | #ifndef __TICKER_H__ 2 | #define __TICKER_H__ 3 | 4 | extern "C" { 5 | #include "tal_sw_timer.h" 6 | } 7 | 8 | 9 | class Ticker { 10 | public: 11 | Ticker(); 12 | ~Ticker(); 13 | 14 | typedef void (*callback_with_arg_t)(void* arg); 15 | typedef void (*callback_function_t)(void); 16 | 17 | void attach(float seconds, callback_function_t callback) { 18 | _callback_function = callback; 19 | 20 | _arg = nullptr; 21 | _callback_function_with_arg = nullptr; 22 | _attach_ms(1000ULL * seconds, true, _static_callback, this); 23 | } 24 | 25 | void attach_ms(uint64_t milliseconds, callback_function_t callback) { 26 | _callback_function = callback; 27 | 28 | _arg = nullptr; 29 | _callback_function_with_arg = nullptr; 30 | _attach_ms(milliseconds, true, _static_callback, this); 31 | } 32 | 33 | template void attach(float seconds, void (*callback)(TArg), TArg arg) { 34 | _callback_function = nullptr; 35 | 36 | _arg = reinterpret_cast(arg); 37 | _callback_function_with_arg = reinterpret_cast(callback); 38 | _attach_ms(1000ULL * seconds, true, _static_callback, this); 39 | } 40 | 41 | template void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) { 42 | _callback_function = nullptr; 43 | 44 | _arg = reinterpret_cast(arg); 45 | _callback_function_with_arg = reinterpret_cast(callback); 46 | _attach_ms(milliseconds, true, _static_callback, this); 47 | } 48 | 49 | void once(float seconds, callback_function_t callback) { 50 | _callback_function = callback; 51 | 52 | _arg = nullptr; 53 | _callback_function_with_arg = nullptr; 54 | _attach_ms(1000ULL * seconds, false, _static_callback, this); 55 | } 56 | 57 | void once_ms(uint64_t milliseconds, callback_function_t callback) { 58 | _callback_function = callback; 59 | 60 | _arg = nullptr; 61 | _callback_function_with_arg = nullptr; 62 | _attach_ms(milliseconds, false, _static_callback, this); 63 | } 64 | 65 | template void once(float seconds, void (*callback)(TArg), TArg arg) { 66 | _callback_function = nullptr; 67 | 68 | _arg = reinterpret_cast(arg); 69 | _callback_function_with_arg = reinterpret_cast(callback); 70 | _attach_ms(1000ULL * seconds, false, _static_callback, this); 71 | } 72 | 73 | template void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) { 74 | _callback_function = nullptr; 75 | 76 | _arg = reinterpret_cast(arg); 77 | _callback_function_with_arg = reinterpret_cast(callback); 78 | _attach_ms(milliseconds, false, _static_callback, this); 79 | } 80 | 81 | void detach(); 82 | bool active() const; 83 | 84 | protected: 85 | static void _static_callback(TIMER_ID timer_id, void *arg); 86 | TIMER_ID _timer_handle; 87 | 88 | callback_function_t _callback_function; 89 | 90 | void* _arg; 91 | callback_with_arg_t _callback_function_with_arg; 92 | private: 93 | void _attach_ms(uint64_t ms, bool repeat, TAL_TIMER_CB callback, void *arg); 94 | }; 95 | 96 | #endif //__TICKER_H__ 97 | -------------------------------------------------------------------------------- /libraries/TuyaIoT/examples/dpType/appConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "PROJECT_VERSION": "1.0.0" 3 | } -------------------------------------------------------------------------------- /libraries/TuyaIoT/examples/quickStart/README.md: -------------------------------------------------------------------------------- 1 | # Tuya IoT Quick Start 2 | 3 | Tuya Cloud Application is an application provided by the Tuya IoT platform. With Tuya Cloud Application, developers can quickly implement remote device control, device management, and other functions. 4 | 5 | The `quickStart` demonstrates a simple, cross-platform, cross-system switch example that supports multiple connections. Through the Tuya APP and Tuya Cloud Service, you can remotely control this LED. 6 | 7 | ## Product Creation 8 | 9 | Refer to the [Product Creation Documentation](https://developer.tuya.com/en/docs/iot-device-dev/application-creation?id=Kbxw7ket3aujc) to create a product on the [Tuya IoT](https://iot.tuya.com) platform and obtain the PID of the created product. 10 | 11 | Then replace the PID in `quickStart` with the PID you obtained. 12 | 13 | ```c 14 | void setup() { 15 | ... 16 | TuyaIoT.begin("YOUR PID", PROJECT_VERSION); 17 | } 18 | ``` 19 | 20 | ## Confirm TuyaOpen License Code 21 | 22 | Products developed through this project need to use the TuyaOpen-specific license code. Using other license codes will not connect to the Tuya Cloud properly. 23 | 24 | Modify the license code in the code as follows: 25 | 26 | ```c 27 | // Tuya license 28 | #define TUYA_DEVICE_UUID "uuidxxxxxxxxxxxxxxxx" 29 | #define TUYA_DEVICE_AUTHKEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 30 | ``` 31 | 32 | If you do not have a TuyaOpen license code, you can obtain it through the following methods: 33 | 34 | + Purchase a module with the TuyaOpen license code pre-burned. This license code is burned into the corresponding module at the factory and will not be lost. TuyaOpen reads the license code through the `TuyaIoT.readBoardLicense(&license);` interface at startup. Please confirm whether the current device has the TuyaOpen license code burned. 35 | 36 | + If the current module does not have the TuyaOpen license code burned, you can purchase the TuyaOpen license code through the [TuyaIoT platform](https://platform.tuya.com/purchase/index?type=6). 37 | 38 | + Obtain a free TuyaOpen license code through the following activities: 39 | 40 | To allow developers to freely experience the TuyaOpen Framework, you can now get a free TuyaOpen Framework-specific license code by starring the TuyaOpen Framework repository on GitHub, including: 41 | 42 | + [tuyaopen](https://github.com/tuya/tuyaopen) 43 | + [arduino-tuyaopen](https://github.com/tuya/arduino-tuyaopen) 44 | + [luanode-tuyaopen](https://github.com/tuya/luanode-tuyaopen) 45 | 46 | With your GitHub account and screenshot, send an email to `chenyisong@tuya.com` or join the QQ group `796221529` and contact the group owner to receive a free TuyaOpen Framework-specific license code. Limited to 500 codes, first come first served, while supplies last. Scan the QR code below to join the group and claim your code👇: 47 | 48 | ![qq_qrcode](https://github.com/tuya/tuyaopen/blob/master/docs/images/zh/qq_qrcode.png?raw=true) 49 | -------------------------------------------------------------------------------- /libraries/TuyaIoT/examples/quickStart/README_zh.md: -------------------------------------------------------------------------------- 1 | # Tuya IoT 快速上手 2 | 3 | 涂鸦云应用是涂鸦 IoT 平台提供的一种应用,通过涂鸦云应用,开发者可以快速实现设备远程控制、设备管理等功能。 4 | 5 | `quickStart` 演示一个简单的,跨平台、跨系统、支持多种连接的开关示例,通过涂鸦 APP、涂鸦云服务,可以对这个 LED 进行远程控制。 6 | 7 | ## 产品创建 8 | 9 | 参考[产品创建文档](https://developer.tuya.com/cn/docs/iot-device-dev/application-creation?id=Kbxw7ket3aujc),在 [Tuya IoT](https://iot.tuya.com) 平台上创建产品,并获取到创建产品的 PID 。 10 | 11 | 然后在 `quickStart` 中 PID 替换为您创建得到的 PID。 12 | 13 | ```c 14 | void setup() { 15 | ... 16 | TuyaIoT.begin("YOUR PID", PROJECT_VERSION); 17 | } 18 | ``` 19 | 20 | ## 确认 tuyaopen 授权码 21 | 22 | 通过该项目开发的产品需采用 TuyaOpen 专用授权码,使用其他授权码无法正常连接涂鸦云。 23 | 24 | 在代码中修改授权码的位置如下: 25 | 26 | ```c 27 | // Tuya license 28 | #define TUYA_DEVICE_UUID "uuidxxxxxxxxxxxxxxxx" 29 | #define TUYA_DEVICE_AUTHKEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 30 | ``` 31 | 32 | 如果没有 tuyaopen 的授权码,可以通过下面的方式进行获取: 33 | 34 | + 购买已烧录 TuyaOpen 授权码模块。该授权码已经在出厂时烧录在对应模组中,且不会丢失。tuyaopen 在启动时通过 `TuyaIoT.readBoardLicense(&license);` 接口读取授权码。请确认当前设备是否为烧录了 tuyaopen 授权码。 35 | 36 | + 如当前模组未烧录 TuyaOpen 授权码,可通过 [TuyaIoT 平台](https://platform.tuya.com/purchase/index?type=6)购买 tuyaopen 授权码。 37 | 38 | + 通过下面的活动获取免费 TuyaOpen 授权码 39 | 40 | 为了让开发者们可以自由体验 Tuyaopen Framework,现在只要在 GitHub 上给 Tuyaopen Framework 开发框架仓库加 star,包括: 41 | 42 | + [tuyaopen](https://github.com/tuya/tuyaopen) 43 | + [arduino-tuyaopen](https://github.com/tuya/arduino-tuyaopen) 44 | + [luanode-tuyaopen](https://github.com/tuya/luanode-tuyaopen) 45 | 46 | 凭 github 账号和截图,发送邮件至 `chenyisong@tuya.com` 或 加入 QQ 群 `796221529` 向群主免费领取一个 TuyaOpen Framework 专用授权码。限量 500 个,先到先得,送完即止,赶紧扫码加群来领👇: 47 | 48 | ![qq_qrcode](https://github.com/tuya/tuyaopen/blob/master/docs/images/zh/qq_qrcode.png?raw=true) 49 | -------------------------------------------------------------------------------- /libraries/TuyaIoT/examples/quickStart/appConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "PROJECT_VERSION": "1.0.0" 3 | } -------------------------------------------------------------------------------- /libraries/TuyaIoT/examples/weatherGet/appConfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "PROJECT_VERSION": "1.0.0" 3 | } -------------------------------------------------------------------------------- /libraries/TuyaIoT/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For TuyaIoT 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | TuyaIoT KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | begin KEYWORD2 16 | write KEYWORD2 17 | remove KEYWORD2 18 | 19 | setLicense KEYWORD2 20 | setLogLevel KEYWORD2 21 | setEventCallback KEYWORD2 22 | 23 | getEventId KEYWORD2 24 | getEventDpNum KEYWORD2 25 | getEventDpId KEYWORD2 26 | readBool KEYWORD2 27 | readValue KEYWORD2 28 | readEnum KEYWORD2 29 | readString KEYWORD2 30 | readBitmap KEYWORD2 31 | readRaw KEYWORD2 32 | 33 | ####################################### 34 | # Constants (LITERAL1) 35 | ####################################### 36 | 37 | -------------------------------------------------------------------------------- /libraries/TuyaIoT/library.properties: -------------------------------------------------------------------------------- 1 | name=TuyaIoT 2 | version=0.0.3 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence=Tuya IoT Cloud connection library 6 | paragraph=This library provides a simple way to connect to Tuya IoT Cloud. 7 | category=Communication 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/TuyaIoT/src/ArduinoTuyaIoTClient.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARDUINO_TUYA_IOT_CLIENT_H__ 2 | #define __ARDUINO_TUYA_IOT_CLIENT_H__ 3 | 4 | /****************************************************************************** 5 | * INCLUDE 6 | ******************************************************************************/ 7 | #include 8 | 9 | #include "tuya_iot.h" 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /****************************************************************************** 16 | * CONSTANTS 17 | ******************************************************************************/ 18 | 19 | /****************************************************************************** 20 | * TYPEDEF 21 | ******************************************************************************/ 22 | void app_iot_event_register_cb(void (*callback)(tuya_event_msg_t* event)); 23 | 24 | void app_iot_license_set(const char *uuid, const char *authKey); 25 | 26 | void app_iot_task_start(const char *pid, const char *version); 27 | 28 | /****************************************************************************** 29 | * EXTERN DECLARATION 30 | ******************************************************************************/ 31 | extern tuya_iot_client_t ArduinoIoTClient; 32 | 33 | #ifdef __cplusplus 34 | } 35 | #endif 36 | 37 | #endif // __ARDUINO_TUYA_IOT_CLIENT_H__ 38 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/SimpleWiFiServer/SimpleWiFiServer.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char* ssid = "********"; 4 | const char* password = "********"; 5 | 6 | WiFiServer server; 7 | 8 | void setup() 9 | { 10 | Serial.begin(115200); 11 | pinMode(5, OUTPUT); // set the LED pin mode 12 | 13 | delay(10); 14 | 15 | // We start by connecting to a WiFi network 16 | 17 | Serial.println(); 18 | Serial.println(); 19 | Serial.print("Connecting to "); 20 | Serial.println(ssid); 21 | 22 | WiFi.begin(ssid, password); 23 | 24 | while (WiFi.status() != WSS_GOT_IP) { 25 | delay(500); 26 | Serial.print("."); 27 | } 28 | 29 | Serial.println(""); 30 | Serial.println("WiFi connected."); 31 | Serial.println("IP address: "); 32 | Serial.println(WiFi.localIP()); 33 | server.begin(80); 34 | 35 | } 36 | 37 | void loop(){ 38 | WiFiClient client = server.available(); // listen for incoming clients 39 | 40 | if (client) { // if you get a client, 41 | Serial.println("New Client."); // print a message out the serial port 42 | String currentLine = ""; // make a String to hold incoming data from the client 43 | while (client.connected()) { // loop while the client's connected 44 | if (client.available()) { // if there's bytes to read from the client, 45 | char c = client.read(); // read a byte, then 46 | Serial.write(c); // print it out the serial monitor 47 | if (c == '\n') { // if the byte is a newline character 48 | // if the current line is blank, you got two newline characters in a row. 49 | // that's the end of the client HTTP request, so send a response: 50 | if (currentLine.length() == 0) { 51 | // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) 52 | // and a content-type so the client knows what's coming, then a blank line: 53 | client.println("HTTP/1.1 200 OK"); 54 | client.println("Content-type:text/html"); 55 | client.println(); 56 | 57 | // the content of the HTTP response follows the header: 58 | client.print("Click here to turn the LED on pin 5 on.
"); 59 | client.print("Click here to turn the LED on pin 5 off.
"); 60 | 61 | // The HTTP response ends with another blank line: 62 | client.println(); 63 | // break out of the while loop: 64 | break; 65 | } else { // if you got a newline, then clear currentLine: 66 | currentLine = ""; 67 | } 68 | } else if (c != '\r') { // if you got anything else but a carriage return character, 69 | currentLine += c; // add it to the end of the currentLine 70 | } 71 | 72 | // Check to see if the client request was "GET /H" or "GET /L": 73 | if (currentLine.endsWith("GET /H")) { 74 | digitalWrite(5, HIGH); // GET /H turns the LED on 75 | } 76 | if (currentLine.endsWith("GET /L")) { 77 | digitalWrite(5, LOW); // GET /L turns the LED off 78 | } 79 | } 80 | } 81 | // close the connection: 82 | client.stop(); 83 | Serial.println("Client Disconnected."); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Set these to your desired credentials. 6 | const char *ssid = "your_name"; 7 | const char *password = "your_passwd"; 8 | 9 | WiFiServer server; 10 | 11 | void setup() { 12 | pinMode(LED_BUILTIN, OUTPUT); 13 | 14 | Serial.begin(115200); 15 | Serial.println(); 16 | Serial.println("Configuring access point..."); 17 | 18 | // You can remove the password parameter if you want the AP to be open. 19 | // a valid password must have more than 7 characters 20 | if (!WiFi.softAP(ssid, password)) { 21 | Serial.println("Soft AP creation failed."); 22 | while(1); 23 | } 24 | IPAddress myIP = WiFi.softAPIP(); 25 | Serial.print("AP IP address: "); 26 | Serial.println(myIP); 27 | server.begin(80); 28 | Serial.println("Server started"); 29 | } 30 | 31 | void loop() { 32 | WiFiClient client = server.available(); // listen for incoming clients 33 | if (client) { // if you get a client, 34 | Serial.println("New Client."); // print a message out the serial port 35 | String currentLine = ""; // make a String to hold incoming data from the client 36 | while (client.connected()) { // loop while the client's connected 37 | if (client.available()) { // if there's bytes to read from the client, 38 | char c = client.read(); // read a byte, then 39 | Serial.write(c); // print it out the serial monitor 40 | if (c == '\n') { // if the byte is a newline character 41 | 42 | // if the current line is blank, you got two newline characters in a row. 43 | // that's the end of the client HTTP request, so send a response: 44 | if (currentLine.length() == 0) { 45 | // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) 46 | // and a content-type so the client knows what's coming, then a blank line: 47 | client.println("HTTP/1.1 200 OK"); 48 | client.println("Content-type:text/html"); 49 | client.println(); 50 | 51 | // the content of the HTTP response follows the header: 52 | client.print("Click here to turn ON the LED.
"); 53 | client.print("Click here to turn OFF the LED.
"); 54 | 55 | // The HTTP response ends with another blank line: 56 | client.println(); 57 | // break out of the while loop: 58 | break; 59 | } else { // if you got a newline, then clear currentLine: 60 | currentLine = ""; 61 | } 62 | } else if (c != '\r') { // if you got anything else but a carriage return character, 63 | currentLine += c; // add it to the end of the currentLine 64 | } 65 | 66 | // Check to see if the client request was "GET /H" or "GET /L": 67 | if (currentLine.endsWith("GET /H")) { 68 | digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on 69 | } 70 | if (currentLine.endsWith("GET /L")) { 71 | digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off 72 | } 73 | } 74 | } 75 | // close the connection: 76 | client.stop(); 77 | Serial.println("Client Disconnected."); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiClient/WiFiClient.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const char* ssid = "********"; // Change this to your WiFi SSID 4 | const char* password = "********"; // Change this to your WiFi password 5 | 6 | const char* host = "api.thingspeak.com"; // This should not be changed 7 | const int httpPort = 80; // This should not be changed 8 | const String channelID = "2005329"; // Change this to your channel ID 9 | const String writeApiKey = "V6YOTILH9I7D51F9"; // Change this to your Write API key 10 | const String readApiKey = "34W6LGLIFXD56MPM"; // Change this to your Read API key 11 | 12 | // The default example accepts one data filed named "field1" 13 | // For your own server you can ofcourse create more of them. 14 | int field1 = 0; 15 | 16 | int numberOfResults = 3; // Number of results to be read 17 | int fieldNumber = 1; // Field number which will be read out 18 | 19 | void setup() 20 | { 21 | Serial.begin(115200); 22 | while(!Serial){delay(100);} 23 | 24 | // We start by connecting to a WiFi network 25 | 26 | Serial.println(); 27 | Serial.println("******************************************************"); 28 | Serial.print("Connecting to "); 29 | Serial.println(ssid); 30 | 31 | WiFi.begin(ssid, password); 32 | 33 | while (WiFi.status() != WSS_GOT_IP) { 34 | delay(500); 35 | Serial.print("."); 36 | } 37 | 38 | Serial.println(""); 39 | Serial.println("WiFi connected"); 40 | Serial.println("IP address: "); 41 | Serial.println(WiFi.localIP()); 42 | } 43 | 44 | void readResponse(WiFiClient *client){ 45 | unsigned long timeout = millis(); 46 | while(client->available() == 0){ 47 | if(millis() - timeout > 5000){ 48 | Serial.println(">>> Client Timeout !"); 49 | client->stop(); 50 | return; 51 | } 52 | } 53 | 54 | // Read all the lines of the reply from server and print them to Serial 55 | while(client->available()) { 56 | String line = client->readStringUntil('\r'); 57 | Serial.print(line); 58 | } 59 | 60 | // Serial.printf("\nClosing connection\n\n"); 61 | } 62 | 63 | void loop(){ 64 | WiFiClient client; 65 | String footer = String(" HTTP/1.1\r\n") + "Host: " + String(host) + "\r\n" + "Connection: close\r\n\r\n"; 66 | 67 | // WRITE -------------------------------------------------------------------------------------------- 68 | if (!client.connect(host, httpPort)) { 69 | return; 70 | } 71 | 72 | client.print("GET /update?api_key=" + writeApiKey + "&field1=" + field1 + footer); 73 | readResponse(&client); 74 | 75 | // READ -------------------------------------------------------------------------------------------- 76 | 77 | String readRequest = "GET /channels/" + channelID + "/fields/" + fieldNumber + ".json?results=" + numberOfResults + " HTTP/1.1\r\n" + 78 | "Host: " + host + "\r\n" + 79 | "Connection: close\r\n\r\n"; 80 | 81 | if (!client.connect(host, httpPort)) { 82 | return; 83 | } 84 | 85 | client.print(readRequest); 86 | readResponse(&client); 87 | 88 | // ------------------------------------------------------------------------------------------------- 89 | 90 | ++field1; 91 | delay(10000); 92 | } -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch sends a message to a TCP server 3 | * 4 | */ 5 | 6 | #include 7 | 8 | void setup() 9 | { 10 | Serial.begin(115200); 11 | delay(10); 12 | 13 | // We start by connecting to a WiFi network 14 | 15 | WiFi.begin("********","********"); 16 | Serial.println(); 17 | Serial.println(); 18 | Serial.print("Waiting for WiFi... "); 19 | 20 | while(WiFi.status() != WSS_GOT_IP) { 21 | Serial.print("."); 22 | delay(500); 23 | } 24 | 25 | Serial.println(""); 26 | Serial.println("WiFi connected"); 27 | Serial.println("IP address: "); 28 | Serial.println(WiFi.localIP()); 29 | 30 | delay(500); 31 | } 32 | 33 | 34 | void loop() 35 | { 36 | // const uint16_t port = 80; 37 | // const char * host = "192.168.1.1"; // ip or dns 38 | const uint16_t port = 1337; 39 | const char * host = "192.168.15.37"; // ip or dns 40 | 41 | Serial.print("Connecting to "); 42 | Serial.println(host); 43 | 44 | // Use WiFiClient class to create TCP connections 45 | WiFiClient client; 46 | 47 | if (!client.connect(host, port)) { 48 | Serial.println("Connection failed."); 49 | Serial.println("Waiting 5 seconds before retrying..."); 50 | delay(5000); 51 | return; 52 | } 53 | 54 | // This will send a request to the server 55 | //uncomment this line to send an arbitrary string to the server 56 | //client.print("Send this data to the server"); 57 | //uncomment this line to send a basic document request to the server 58 | client.print("GET /index.html HTTP/1.1\n\n"); 59 | 60 | int maxloops = 0; 61 | 62 | //wait for the server's reply to become available 63 | while (!client.available() && maxloops < 1000) 64 | { 65 | maxloops++; 66 | delay(1); //delay 1 msec 67 | } 68 | if (client.available() > 0) 69 | { 70 | //read back one line from the server 71 | String line = client.readStringUntil('\r'); 72 | Serial.println(line); 73 | } 74 | else 75 | { 76 | Serial.println("client.available() timed out "); 77 | } 78 | 79 | Serial.println("Closing connection."); 80 | client.stop(); 81 | 82 | Serial.println("Waiting 5 seconds before restarting..."); 83 | delay(5000); 84 | } 85 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiClientConnect/WiFiClientConnect.ino: -------------------------------------------------------------------------------- 1 | /* Wi-Fi STA Connect and Disconnect Example 2 | 3 | This example code is in the Public Domain (or CC0 licensed, at your option.) 4 | 5 | Unless required by applicable law or agreed to in writing, this 6 | software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 7 | CONDITIONS OF ANY KIND, either express or implied. 8 | 9 | */ 10 | #include 11 | 12 | const char* ssid = "*******"; 13 | const char* password = "*******"; 14 | 15 | int btnGPIO = 8; 16 | int btnState = false; 17 | 18 | void setup() 19 | { 20 | Serial.begin(115200); 21 | delay(10); 22 | 23 | // Set GPIO0 Boot button as input 24 | pinMode(btnGPIO, INPUT); 25 | 26 | // We start by connecting to a WiFi network 27 | // To debug, please enable Core Debug Level to Verbose 28 | 29 | Serial.println(); 30 | Serial.print("[WiFi] Connecting to "); 31 | Serial.println(ssid); 32 | 33 | WiFi.begin(ssid, password); 34 | // Auto reconnect is set true as default 35 | // To set auto connect off, use the following function 36 | // WiFi.setAutoReconnect(false); 37 | 38 | // Will try for about 10 seconds (20x 500ms) 39 | int tryDelay = 500; 40 | int numberOfTries = 20; 41 | 42 | // Wait for the WiFi event 43 | while (true) { 44 | 45 | switch(WiFi.status()) { 46 | case WSS_NO_AP_FOUND: 47 | Serial.println("[WiFi] SSID not found"); 48 | break; 49 | case WSS_CONN_FAIL: 50 | Serial.print("[WiFi] Failed - WiFi not connected! Reason: "); 51 | return; 52 | break; 53 | case WSS_CONN_SUCCESS: 54 | Serial.println("[WiFi] Scan is completed"); 55 | break; 56 | case WSS_GOT_IP: 57 | Serial.println("[WiFi] WiFi is connected!"); 58 | Serial.print("[WiFi] IP address: "); 59 | Serial.println(WiFi.localIP()); 60 | return; 61 | break; 62 | default: 63 | Serial.print("[WiFi] WiFi Status: "); 64 | Serial.println(WiFi.status()); 65 | break; 66 | } 67 | delay(tryDelay); 68 | 69 | if(numberOfTries <= 0){ 70 | Serial.print("[WiFi] Failed to connect to WiFi!"); 71 | // Use disconnect function to force stop trying to connect 72 | WiFi.disconnect(); 73 | return; 74 | } else { 75 | numberOfTries--; 76 | } 77 | } 78 | } 79 | 80 | void loop() 81 | { 82 | // Read the button state 83 | btnState = digitalRead(btnGPIO); 84 | if (btnState == LOW) { 85 | // Disconnect from WiFi 86 | Serial.println("[WiFi] Disconnecting from WiFi!"); 87 | // This function will disconnect and turn off the WiFi (NVS WiFi data is kept) 88 | if(WiFi.disconnect(true, false)){ 89 | Serial.println("[WiFi] Disconnected from WiFi!"); 90 | } 91 | delay(1000); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiClientStaticIP/WiFiClientStaticIP.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Example of connection using Static IP 3 | by Evandro Luis Copercini 4 | Public domain - 2017 5 | */ 6 | 7 | #include 8 | 9 | const char* ssid = "********"; 10 | const char* password = "********"; 11 | const char* host = "example.com"; 12 | const char* url = "/index.html"; 13 | 14 | IPAddress local_IP(192, 168, 15, 69); 15 | IPAddress gateway(192, 168, 15, 1); 16 | IPAddress subnet(255, 255, 255, 0); 17 | IPAddress primaryDNS(8, 8, 8, 8); //optional 18 | 19 | void setup() 20 | { 21 | Serial.begin(115200); 22 | 23 | if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) { 24 | Serial.println("STA Failed to configure"); 25 | } 26 | 27 | Serial.print("Connecting to "); 28 | Serial.println(ssid); 29 | 30 | WiFi.begin(ssid, password); 31 | 32 | while (WiFi.status() != WSS_GOT_IP) { 33 | delay(500); 34 | Serial.print("."); 35 | } 36 | 37 | Serial.println(""); 38 | Serial.println("WiFi connected!"); 39 | Serial.print("IP address: "); 40 | Serial.println(WiFi.localIP()); 41 | Serial.print("Mac Address: "); 42 | Serial.println(WiFi.macAddress()); 43 | Serial.print("Subnet Mask: "); 44 | Serial.println(WiFi.subnetMask()); 45 | Serial.print("Gateway IP: "); 46 | Serial.println(WiFi.gatewayIP()); 47 | Serial.print("DNS: "); 48 | Serial.println(WiFi.dnsIP()); 49 | } 50 | 51 | void loop() 52 | { 53 | delay(5000); 54 | 55 | Serial.print("connecting to "); 56 | Serial.println(host); 57 | 58 | // Use WiFiClient class to create TCP connections 59 | WiFiClient client; 60 | const int httpPort = 80; 61 | if (!client.connect(host, httpPort)) { 62 | Serial.println("connection failed"); 63 | return; 64 | } 65 | 66 | Serial.print("Requesting URL: "); 67 | Serial.println(url); 68 | 69 | // This will send the request to the server 70 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 71 | "Host: " + host + "\r\n" + 72 | "Connection: close\r\n\r\n"); 73 | unsigned long timeout = millis(); 74 | while (client.available() == 0) { 75 | if (millis() - timeout > 5000) { 76 | Serial.println(">>> Client Timeout !"); 77 | client.stop(); 78 | return; 79 | } 80 | } 81 | 82 | // Read all the lines of the reply from server and print them to Serial 83 | while (client.available()) { 84 | String line = client.readStringUntil('\r'); 85 | Serial.print(line); 86 | } 87 | 88 | Serial.println(); 89 | Serial.println("closing connection"); 90 | } 91 | 92 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiMulti/WiFiMulti.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch tries to connect to the best AP based on a given list 3 | * 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | WiFiMulti wifiMulti; 10 | 11 | void setup() 12 | { 13 | Serial.begin(115200); 14 | delay(10); 15 | 16 | wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1"); 17 | wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); 18 | wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); 19 | 20 | Serial.println("Connecting Wifi..."); 21 | if(wifiMulti.run() == WSS_GOT_IP) { 22 | Serial.println(""); 23 | Serial.println("WiFi connected"); 24 | Serial.println("IP address: "); 25 | Serial.println(WiFi.localIP()); 26 | } 27 | } 28 | 29 | void loop() 30 | { 31 | if(wifiMulti.run() != WSS_GOT_IP) { 32 | Serial.println("WiFi not connected!"); 33 | delay(1000); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiScan/WiFiScan.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch demonstrates how to scan WiFi networks. 3 | * The API is based on the Arduino WiFi Shield library, but has significant changes as newer WiFi functions are supported. 4 | * E.g. the return value of `encryptionType()` different because more modern encryption is supported. 5 | */ 6 | #include "WiFi.h" 7 | void setup() 8 | { 9 | Serial.begin(115200); 10 | 11 | // Set WiFi to station mode and disconnect from an AP if it was previously connected. 12 | WiFi.mode(WIFI_STA); 13 | WiFi.disconnect(); 14 | delay(100); 15 | 16 | Serial.println("Setup done"); 17 | 18 | } 19 | 20 | void loop() 21 | { 22 | Serial.println("Scan start"); 23 | 24 | // WiFi.scanNetworks will return the number of networks found. 25 | int n = WiFi.scanNetworks(); 26 | 27 | Serial.println("Scan done"); 28 | if (n == 0) { 29 | Serial.println("no networks found"); 30 | } else { 31 | Serial.print(n); 32 | Serial.println(" networks found"); 33 | Serial.println("Nr | SSID | RSSI | CH | Encryption"); 34 | for (int i = 0; i < n; ++i) { 35 | // Print SSID and RSSI for each network found 36 | Serial.print(i + 1); 37 | Serial.print(" | \t\t"); 38 | Serial.print(WiFi.SSID(i)); 39 | Serial.print("\t\t | \t"); 40 | Serial.print( WiFi.RSSI(i)); 41 | Serial.print("\t\t | \t"); 42 | Serial.print( WiFi.channel(i)); 43 | Serial.print("\t\t | \t"); 44 | switch (WiFi.encryptionType(i)) 45 | { 46 | case WAAM_OPEN: 47 | Serial.print("open"); 48 | break; 49 | case WAAM_WEP: 50 | Serial.print("WEP"); 51 | break; 52 | case WAAM_WPA_PSK: 53 | Serial.print("WPA PSK"); 54 | break; 55 | case WAAM_WPA2_PSK: 56 | Serial.print("WPA2 PSK"); 57 | break; 58 | case WAAM_WPA_WPA2_PSK: 59 | Serial.print("WPA + WPA2"); 60 | break; 61 | case WAAM_WPA_WPA3_SAE: 62 | Serial.print("WPA3"); 63 | break; 64 | default: 65 | Serial.print("unknown"); 66 | } 67 | Serial.println(); 68 | delay(10); 69 | } 70 | } 71 | Serial.println(""); 72 | // Delete the scan result to free memory for code below. 73 | WiFi.scanDelete(); 74 | 75 | // Wait a bit before scanning again. 76 | delay(5000); 77 | } 78 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiScanDualAntenna/WiFiScanDualAntenna.ino: -------------------------------------------------------------------------------- 1 | #include "WiFi.h" 2 | #define GPIO_ANT1 2 3 | #define GPIO_ANT2 25 4 | void setup() 5 | { 6 | bool err = false; 7 | Serial.begin(115200); 8 | 9 | WiFi.mode(WIFI_STA); 10 | err = WiFi.setDualAntennaConfig(GPIO_ANT1, GPIO_ANT2, WIFI_RX_ANT_AUTO, WIFI_TX_ANT_AUTO); 11 | 12 | if(err == false) { 13 | Serial.println("Dual Antenna configuration failed!"); 14 | } else { 15 | Serial.println("Dual Antenna configuration successfully done!"); 16 | } 17 | 18 | WiFi.disconnect(); 19 | delay(100); 20 | 21 | Serial.println("Setup done"); 22 | } 23 | 24 | void loop() 25 | { 26 | Serial.println("scan start"); 27 | 28 | // WiFi.scanNetworks will return the number of networks found 29 | int n = WiFi.scanNetworks(); 30 | Serial.println("scan done"); 31 | if (n == 0) { 32 | Serial.println("no networks found"); 33 | } else { 34 | Serial.print(n); 35 | Serial.println(" networks found"); 36 | for (int i = 0; i < n; ++i) { 37 | // Print SSID and RSSI for each network found 38 | Serial.print(i + 1); 39 | Serial.print(": "); 40 | Serial.print(WiFi.SSID(i)); 41 | Serial.print(" ("); 42 | Serial.print(WiFi.RSSI(i)); 43 | Serial.print(")"); 44 | Serial.println((WiFi.encryptionType(i) == WAAM_OPEN)?" ":"*"); 45 | delay(10); 46 | } 47 | } 48 | Serial.println(""); 49 | 50 | // Wait a bit before scanning again 51 | delay(5000); 52 | } 53 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tal_api.h" 4 | 5 | WiFiMulti wifiMulti; 6 | 7 | 8 | #define MAX_SRV_CLIENTS 1 9 | const char* ssid = "********"; 10 | const char* password = "********"; 11 | 12 | WiFiServer server; 13 | WiFiClient serverClients[MAX_SRV_CLIENTS]; 14 | 15 | void setup() { 16 | Serial.begin(115200); 17 | Serial.println("\nConnecting"); 18 | 19 | wifiMulti.addAP(ssid,password); 20 | wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); 21 | wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); 22 | 23 | Serial.println("Connecting Wifi "); 24 | for (int loops = 10; loops > 0; loops--) { 25 | if (wifiMulti.run() == WSS_GOT_IP) { 26 | Serial.println(""); 27 | Serial.print("WiFi connected "); 28 | Serial.print("IP address: "); 29 | Serial.println(WiFi.localIP()); 30 | break; 31 | } 32 | else { 33 | Serial.println(loops); 34 | delay(1000); 35 | } 36 | } 37 | if (wifiMulti.run() != WSS_GOT_IP) { 38 | Serial.println("WiFi connect failed"); 39 | delay(1000); 40 | } 41 | 42 | //start UART and the server 43 | server.begin(23); 44 | server.setNoDelay(true); 45 | 46 | Serial.print("Ready! Use 'telnet "); 47 | Serial.print(WiFi.localIP()); 48 | Serial.println(" 23' to connect"); 49 | } 50 | 51 | void loop() { 52 | uint8_t i; 53 | if (wifiMulti.run() == WSS_GOT_IP) { 54 | //check if there are any new clients 55 | if (server.hasClient()){ 56 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 57 | //find free/disconnected spot 58 | if (!serverClients[i] || !serverClients[i].connected()){ 59 | if(serverClients[i]) serverClients[i].stop(); 60 | serverClients[i] = server.available(); 61 | if (!serverClients[i]) Serial.println("available broken"); 62 | Serial.print("New client: "); 63 | Serial.print(i); Serial.print(' '); 64 | Serial.println(serverClients[i].remoteIP()); 65 | break; 66 | } 67 | } 68 | if (i >= MAX_SRV_CLIENTS) { 69 | //no free/disconnected spot so reject 70 | server.available().stop(); 71 | } 72 | } 73 | //check clients for data 74 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 75 | if (serverClients[i] && serverClients[i].connected()){ 76 | if(serverClients[i].available()){ 77 | //get data from the telnet client and push it to the UART 78 | while(serverClients[i].available()) Serial.write(serverClients[i].read()); 79 | } 80 | } 81 | else { 82 | if (serverClients[i]) { 83 | serverClients[i].stop(); 84 | } 85 | } 86 | } 87 | //check UART for data 88 | if(Serial.available()){ 89 | size_t len = Serial.available(); 90 | uint8_t *sbuf = (uint8_t *)tal_malloc(len); 91 | Serial.readBytes(sbuf, len); 92 | //push UART data to all connected telnet clients 93 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 94 | if (serverClients[i] && serverClients[i].connected()){ 95 | serverClients[i].write(sbuf, len); 96 | delay(1); 97 | } 98 | } 99 | tal_free(sbuf); 100 | } 101 | } 102 | else { 103 | Serial.println("WiFi not connected!"); 104 | for(i = 0; i < MAX_SRV_CLIENTS; i++) { 105 | if (serverClients[i]) serverClients[i].stop(); 106 | } 107 | delay(1000); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // WiFi network name and password: 5 | const char * networkName = "********"; 6 | const char * networkPswd = "********"; 7 | 8 | const int udpPort = 4556; 9 | 10 | #define UDP_TX_MAX_SIZE 1024 11 | 12 | // buffers for receiving and sending data 13 | char packetBuffer[UDP_TX_MAX_SIZE + 1]; // buffer to hold incoming packet, 14 | char ReplyBuffer[] = "acknowledged\r\n"; 15 | //Are we currently connected? 16 | boolean connected = false; 17 | 18 | //The udp library class 19 | WiFiUDP udp; 20 | 21 | void setup(){ 22 | // Initialize hardware serial: 23 | Serial.begin(115200); 24 | 25 | //Connect to the WiFi network 26 | Serial.println(); 27 | Serial.println(); 28 | Serial.print("Connecting to "); 29 | Serial.println(networkName); 30 | 31 | WiFi.begin(networkName,networkPswd); 32 | while (WiFi.status() != WSS_GOT_IP) { 33 | delay(500); 34 | Serial.print("."); 35 | } 36 | 37 | Serial.println(""); 38 | Serial.println("WiFi connected."); 39 | Serial.println("IP address: "); 40 | Serial.println(WiFi.localIP()); 41 | connected = true; 42 | udp.begin(WiFi.localIP(),udpPort); 43 | 44 | } 45 | 46 | void loop(){ 47 | //only send data when connected 48 | int packetSize = udp.parsePacket(); 49 | if (packetSize) { 50 | Serial.print("packet size: "); 51 | Serial.println(packetSize); 52 | Serial.print("remoteIP: "); 53 | Serial.println(udp.remoteIP().toString()); 54 | Serial.print("remotePort: "); 55 | Serial.println(udp.remotePort()); 56 | 57 | // read the packet into packetBufffer 58 | int n = udp.read(packetBuffer, UDP_TX_MAX_SIZE); 59 | packetBuffer[n] = 0; 60 | Serial.println("Contents:"); 61 | Serial.println(packetBuffer); 62 | 63 | // send a reply, to the IP address and port that sent us the packet we received 64 | udp.beginPacket(udp.remoteIP(), udp.remotePort()); 65 | udp.write((const uint8_t*)ReplyBuffer, 14); 66 | udp.endPacket(); 67 | } 68 | //Wait for 1 second 69 | delay(1000); 70 | } 71 | 72 | -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiUDPClient/udp_server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | 4 | try : 5 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 7 | except socket.error, msg : 8 | print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 9 | sys.exit() 10 | 11 | try: 12 | s.bind(('', 3333)) 13 | except socket.error , msg: 14 | print 'Bind failed. Error: ' + str(msg[0]) + ': ' + msg[1] 15 | sys.exit() 16 | 17 | print 'Server listening' 18 | 19 | while 1: 20 | d = s.recvfrom(1024) 21 | data = d[0] 22 | 23 | if not data: 24 | break 25 | 26 | print data.strip() 27 | 28 | s.close() -------------------------------------------------------------------------------- /libraries/WiFi/examples/WiFiUDPClient/udp_server.rb: -------------------------------------------------------------------------------- 1 | require 'socket' 2 | include Socket::Constants 3 | 4 | udp_socket = UDPSocket.new(AF_INET) 5 | 6 | #bind 7 | udp_socket.bind("", 3333) 8 | puts 'Server listening' 9 | 10 | while true do 11 | message, sender = udp_socket.recvfrom(1024) 12 | puts message 13 | end -------------------------------------------------------------------------------- /libraries/WiFi/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For WiFi 3 | ####################################### 4 | 5 | ####################################### 6 | # Library (KEYWORD3) 7 | ####################################### 8 | 9 | WiFi KEYWORD3 10 | 11 | ####################################### 12 | # Datatypes (KEYWORD1) 13 | ####################################### 14 | 15 | WiFi KEYWORD1 16 | WiFiClient KEYWORD1 17 | WiFiServer KEYWORD1 18 | WiFiUDP KEYWORD1 19 | WiFiClientSecure KEYWORD1 20 | 21 | ####################################### 22 | # Methods and Functions (KEYWORD2) 23 | ####################################### 24 | 25 | status KEYWORD2 26 | mode KEYWORD2 27 | connect KEYWORD2 28 | write KEYWORD2 29 | available KEYWORD2 30 | config KEYWORD2 31 | setDNS KEYWORD2 32 | read KEYWORD2 33 | flush KEYWORD2 34 | stop KEYWORD2 35 | connected KEYWORD2 36 | begin KEYWORD2 37 | beginMulticast KEYWORD2 38 | disconnect KEYWORD2 39 | macAddress KEYWORD2 40 | localIP KEYWORD2 41 | subnetMask KEYWORD2 42 | gatewayIP KEYWORD2 43 | SSID KEYWORD2 44 | psk KEYWORD2 45 | BSSID KEYWORD2 46 | RSSI KEYWORD2 47 | encryptionType KEYWORD2 48 | beginPacket KEYWORD2 49 | beginPacketMulticast KEYWORD2 50 | endPacket KEYWORD2 51 | parsePacket KEYWORD2 52 | destinationIP KEYWORD2 53 | remoteIP KEYWORD2 54 | remotePort KEYWORD2 55 | softAP KEYWORD2 56 | softAPIP KEYWORD2 57 | softAPmacAddress KEYWORD2 58 | softAPConfig KEYWORD2 59 | printDiag KEYWORD2 60 | hostByName KEYWORD2 61 | scanNetworks KEYWORD2 62 | 63 | ####################################### 64 | # Constants (LITERAL1) 65 | ####################################### 66 | WIFI_AP LITERAL1 67 | WIFI_STA LITERAL1 68 | WIFI_AP_STA LITERAL1 69 | -------------------------------------------------------------------------------- /libraries/WiFi/library.properties: -------------------------------------------------------------------------------- 1 | name=WiFi 2 | version=1.0.0 3 | author=BK 4 | sentence=Enables network connection (local and Internet) built-in WiFi. 5 | paragraph=With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi. The shield can connect either to open or encrypted networks. The IP address can be assigned statically or through a DHCP. The library can also manage DNS. 6 | category=Communication 7 | url= 8 | architectures=* 9 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFi.cpp: -------------------------------------------------------------------------------- 1 | #include "WiFi.h" 2 | 3 | extern "C" { 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | } 12 | 13 | void WiFiClass::printDiag(Print& p) 14 | { 15 | const char* modes[] = { "NULL", "SNIFFER", "STA", "AP", "STA+AP" }; 16 | 17 | WF_WK_MD_E mode; 18 | tkl_wifi_get_work_mode(&mode); 19 | 20 | uint8_t primaryChan; 21 | tkl_wifi_get_cur_channel(&primaryChan); 22 | 23 | p.print("Mode: "); 24 | p.println(modes[mode]); 25 | 26 | p.print("Channel: "); 27 | p.println(primaryChan); 28 | 29 | } 30 | 31 | void WiFiClass::enableProv(bool status) 32 | { 33 | prov_enable = status; 34 | } 35 | 36 | bool WiFiClass::isProvEnabled() 37 | { 38 | return prov_enable; 39 | } 40 | 41 | WiFiClass WiFi; -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFi.h: -------------------------------------------------------------------------------- 1 | #ifndef WiFi_h 2 | #define WiFi_h 3 | 4 | #include 5 | #include "api/Print.h" 6 | 7 | #include "WiFiType.h" 8 | #include "WiFiSTA.h" 9 | #include "WiFiAP.h" 10 | #include "WiFiScan.h" 11 | #include "WiFiGeneric.h" 12 | 13 | #include "WiFiClient.h" 14 | #include "WiFiServer.h" 15 | #include "WiFiUdp.h" 16 | 17 | class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanClass, public WiFiAPClass 18 | { 19 | private: 20 | bool prov_enable; 21 | public: 22 | WiFiClass() 23 | { 24 | prov_enable = false; 25 | } 26 | 27 | using WiFiGenericClass::channel; 28 | 29 | using WiFiSTAClass::SSID; 30 | using WiFiSTAClass::RSSI; 31 | using WiFiSTAClass::BSSID; 32 | using WiFiSTAClass::BSSIDstr; 33 | 34 | using WiFiScanClass::SSID; 35 | // using WiFiScanClass::encryptionType; 36 | using WiFiScanClass::RSSI; 37 | using WiFiScanClass::BSSID; 38 | using WiFiScanClass::BSSIDstr; 39 | using WiFiScanClass::channel; 40 | public: 41 | void printDiag(Print& dest); 42 | friend class WiFiClient; 43 | friend class WiFiServer; 44 | friend class WiFiUDP; 45 | void enableProv(bool status); 46 | bool isProvEnabled(); 47 | }; 48 | 49 | extern WiFiClass WiFi; 50 | 51 | #endif -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiAP.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFIAP_H_ 2 | #define WIFIAP_H_ 3 | 4 | #include "WiFiType.h" 5 | #include "WiFiGeneric.h" 6 | 7 | #define WLAN_DEFAULT_IP "192.168.176.1" 8 | #define WLAN_DEFAULT_GW "192.168.176.1" 9 | #define WLAN_DEFAULT_MASK "255.255.255.0" 10 | 11 | class WiFiAPClass 12 | { 13 | public: 14 | bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4, bool ftm_responder = false); 15 | bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0); 16 | bool softAPdisconnect(bool wifioff = false); 17 | 18 | uint8_t softAPgetStationNum(); 19 | IPAddress softAPIP(); 20 | 21 | IPAddress softAPBroadcastIP(); 22 | IPAddress softAPNetworkID(); 23 | IPAddress softAPSubnetMask(); 24 | uint8_t softAPSubnetCIDR(); 25 | 26 | const char * softAPgetHostname(); 27 | bool softAPsetHostname(const char * hostname); 28 | 29 | uint8_t* softAPmacAddress(uint8_t* mac); 30 | String softAPmacAddress(void); 31 | String softAPSSID(void) const; 32 | 33 | private: 34 | WF_AP_CFG_IF_S ap_cfg_info ; 35 | WF_STA_INFO_S * sta_info ; 36 | uint32_t sta_num; 37 | }; 38 | 39 | #endif /* WIFIAP_H_*/ 40 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiClient.h: -------------------------------------------------------------------------------- 1 | #ifndef _WIFICLIENT_H_ 2 | #define _WIFICLIENT_H_ 3 | 4 | #include 5 | #include "api/Client.h" 6 | #include 7 | 8 | class WiFiClientSocketHandle; 9 | class WiFiClientRxBuffer; 10 | 11 | class LwIPClient : public Client 12 | { 13 | public: 14 | virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0; 15 | virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0; 16 | virtual int setTimeout(uint32_t seconds) = 0; 17 | }; 18 | 19 | class WiFiClient : public LwIPClient 20 | { 21 | protected: 22 | std::shared_ptr clientSocketHandle; 23 | std::shared_ptr _rxBuffer; 24 | bool _connected; 25 | int _timeout; 26 | 27 | public: 28 | WiFiClient *next; 29 | WiFiClient(); 30 | WiFiClient(int fd); 31 | ~WiFiClient(); 32 | int connect(IPAddress ip, uint16_t port); 33 | int connect(IPAddress ip, uint16_t port, int32_t timeout_ms); 34 | int connect(const char *host, uint16_t port); 35 | int connect(const char *host, uint16_t port, int32_t timeout_ms); 36 | size_t write(uint8_t data); 37 | size_t write(const uint8_t *buf, size_t size); 38 | size_t write_P(PGM_P buf, size_t size); 39 | size_t write(Stream &stream); 40 | int available(); 41 | int read(); 42 | int read(uint8_t *buf, size_t size); 43 | int peek(); 44 | void flush(); 45 | void stop(); 46 | uint8_t connected(); 47 | 48 | operator bool() 49 | { 50 | return connected(); 51 | } 52 | WiFiClient & operator=(const WiFiClient &other); 53 | bool operator==(const bool value) 54 | { 55 | return bool() == value; 56 | } 57 | bool operator!=(const bool value) 58 | { 59 | return bool() != value; 60 | } 61 | bool operator==(const WiFiClient&); 62 | bool operator!=(const WiFiClient& rhs) 63 | { 64 | return !this->operator==(rhs); 65 | }; 66 | 67 | virtual int fd() const; 68 | 69 | int setSocketOption(int option, char* value, size_t len); 70 | int setSocketOption(int level, int option, const void* value, size_t len); 71 | int setOption(int option, int *value); 72 | int getOption(int option, int *value); 73 | int setTimeout(uint32_t seconds); 74 | int setNoDelay(bool nodelay); 75 | bool getNoDelay(); 76 | 77 | IPAddress remoteIP() const; 78 | IPAddress remoteIP(int fd) const; 79 | uint16_t remotePort() const; 80 | uint16_t remotePort(int fd) const; 81 | IPAddress localIP() const; 82 | IPAddress localIP(int fd) const; 83 | uint16_t localPort() const; 84 | uint16_t localPort(int fd) const; 85 | 86 | //friend class WiFiServer; 87 | using Print::write; 88 | }; 89 | 90 | #endif /* _WIFICLIENT_H_ */ 91 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiMulti.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFICLIENTMULTI_H_ 2 | #define WIFICLIENTMULTI_H_ 3 | 4 | #include "WiFi.h" 5 | #include 6 | 7 | typedef struct { 8 | char * ssid; 9 | char * passphrase; 10 | } WifiAPlist_t; 11 | 12 | class WiFiMulti 13 | { 14 | public: 15 | WiFiMulti(); 16 | ~WiFiMulti(); 17 | 18 | bool addAP(const char* ssid, const char *passphrase = NULL); 19 | uint8_t run(uint32_t connectTimeout=5000); 20 | 21 | private: 22 | std::vector APlist; 23 | }; 24 | 25 | #endif /* WIFICLIENTMULTI_H_ */ 26 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiSTA.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFISTA_H_ 2 | #define WIFISTA_H_ 3 | 4 | #include "WiFiType.h" 5 | #include "WiFiGeneric.h" 6 | 7 | typedef enum { 8 | WPA2_AUTH_TLS = 0, 9 | WPA2_AUTH_PEAP = 1, 10 | WPA2_AUTH_TTLS = 2 11 | } wpa2_auth_method_t; 12 | 13 | class WiFiSTAClass 14 | { 15 | public: 16 | 17 | WF_STATION_STAT_E begin(const char* wpa2_ssid, wpa2_auth_method_t method, const char* wpa2_identity=NULL, const char* wpa2_username=NULL, const char *wpa2_password=NULL, const char* ca_pem=NULL, const char* client_crt=NULL, const char* client_key=NULL, int32_t channel=0, const uint8_t* bssid=0, bool connect=true); 18 | WF_STATION_STAT_E begin(const String& wpa2_ssid, wpa2_auth_method_t method, const String& wpa2_identity = (const char*)NULL, const String& wpa2_username = (const char*)NULL, const String& wpa2_password = (const char*)NULL, const String& ca_pem = (const char*)NULL, const String& client_crt = (const char*)NULL, const String& client_key = (const char*)NULL, int32_t channel=0, const uint8_t* bssid=0, bool connect=true) { 19 | return begin(wpa2_ssid.c_str(), method, wpa2_identity.c_str(), wpa2_username.c_str(), wpa2_password.c_str(), ca_pem.c_str(), client_crt.c_str(), client_key.c_str(), channel, bssid, connect); 20 | } 21 | WF_STATION_STAT_E begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); 22 | WF_STATION_STAT_E begin(const String& ssid, const String& passphrase = (const char*)NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true) { 23 | return begin(ssid.c_str(), passphrase.c_str(), channel, bssid, connect); 24 | } 25 | WF_STATION_STAT_E begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true); 26 | WF_STATION_STAT_E begin(); 27 | 28 | bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000,IPAddress dns2 = (uint32_t)0x00000000); 29 | 30 | bool reconnect(); 31 | bool disconnect(bool wifioff = false, bool eraseap = false); 32 | bool eraseAP(void); 33 | 34 | bool isConnected(); 35 | 36 | bool setAutoConnect(bool autoConnect); 37 | bool getAutoConnect(); 38 | 39 | bool setAutoReconnect(bool autoReconnect); 40 | bool getAutoReconnect(); 41 | 42 | uint8_t waitForConnectResult(unsigned long timeoutLength = 60000); 43 | 44 | 45 | IPAddress localIP(); 46 | 47 | uint8_t * macAddress(uint8_t* mac); 48 | String macAddress(); 49 | 50 | IPAddress subnetMask(); 51 | IPAddress gatewayIP(); 52 | IPAddress dnsIP(uint8_t dns_no = 0); 53 | 54 | IPAddress broadcastIP(); 55 | IPAddress networkID(); 56 | uint8_t subnetCIDR(); 57 | 58 | // STA WiFi info 59 | static WF_STATION_STAT_E status(); 60 | String SSID() const; 61 | String psk() const; 62 | 63 | uint8_t * BSSID(); 64 | String BSSIDstr(); 65 | 66 | int8_t RSSI(); 67 | 68 | static void _setStatus(WF_STATION_STAT_E status); 69 | 70 | protected: 71 | static bool _useStaticIp; 72 | static bool _autoReconnect; 73 | }; 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiScan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "WiFi.h" 3 | #include "WiFiGeneric.h" 4 | #include "WiFiScan.h" 5 | #include "tal_log.h" 6 | 7 | extern "C" { 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "lwip/err.h" 16 | } 17 | 18 | int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel, const char * ssid, const uint8_t * bssid) 19 | { 20 | ap_info = NULL; 21 | ap_num = 0; 22 | tkl_wifi_scan_ap(NULL,&ap_info,&ap_num); 23 | return ap_num; 24 | } 25 | 26 | void WiFiScanClass::scanDelete() 27 | { 28 | if( ap_info != NULL) 29 | tkl_wifi_release_ap(ap_info); 30 | ap_num = 0; 31 | PR_INFO("scanDelete Done!\n"); 32 | } 33 | 34 | void * WiFiScanClass::_getScanInfoByIndex(uint32_t i) 35 | { 36 | if (i >= (int)ap_num || !ap_info) 37 | { 38 | return 0; 39 | } 40 | return (AP_IF_S*)(ap_info + i); 41 | } 42 | 43 | bool WiFiScanClass::getNetworkInfo(uint32_t i, String &ssid, uint8_t &encType, int32_t &rssi, uint8_t* &bssid, int32_t &channel) 44 | { 45 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 46 | if(!it) { 47 | return false; 48 | } 49 | ssid = (const char*) it->ssid; 50 | encType = it->security; 51 | rssi = it->rssi; 52 | bssid = it->bssid; 53 | channel = it->channel; 54 | return true; 55 | } 56 | 57 | String WiFiScanClass::SSID(uint8_t i) 58 | { 59 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 60 | if(!it) { 61 | return String(); 62 | } 63 | return String(reinterpret_cast(it->ssid)); 64 | } 65 | 66 | WF_AP_AUTH_MODE_E WiFiScanClass::encryptionType(uint8_t i) 67 | { 68 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 69 | if(!it) { 70 | return WAAM_OPEN; 71 | } 72 | return (WF_AP_AUTH_MODE_E)it->security; 73 | } 74 | 75 | int32_t WiFiScanClass::RSSI(uint8_t i) 76 | { 77 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 78 | if(!it) { 79 | return 0; 80 | } 81 | return (int32_t)it->rssi; 82 | } 83 | 84 | uint8_t * WiFiScanClass::BSSID(uint8_t i) 85 | { 86 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 87 | if(!it) { 88 | return 0; 89 | } 90 | return it->bssid; 91 | } 92 | 93 | String WiFiScanClass::BSSIDstr(uint8_t i) 94 | { 95 | char mac[18] = { 0 }; 96 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 97 | if(!it) { 98 | return String(); 99 | } 100 | sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); 101 | return String(mac); 102 | } 103 | 104 | int32_t WiFiScanClass::channel(uint8_t i) 105 | { 106 | AP_IF_S* it = reinterpret_cast(_getScanInfoByIndex(i)); 107 | if(!it) { 108 | return 0; 109 | } 110 | return it->channel; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiScan.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFISCAN_H_ 2 | #define WIFISCAN_H_ 3 | 4 | #include "WiFiType.h" 5 | #include "WiFiGeneric.h" 6 | 7 | class WiFiScanClass 8 | { 9 | public: 10 | int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0, const char * ssid=nullptr, const uint8_t * bssid=nullptr); 11 | void scanDelete(); 12 | bool getNetworkInfo(uint32_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel); 13 | 14 | String SSID(uint8_t networkItem); 15 | WF_AP_AUTH_MODE_E encryptionType(uint8_t networkItem); 16 | int32_t RSSI(uint8_t networkItem); 17 | uint8_t * BSSID(uint8_t networkItem); 18 | String BSSIDstr(uint8_t networkItem); 19 | int32_t channel(uint8_t networkItem); 20 | void * getScanInfoByIndex(uint32_t i) { return _getScanInfoByIndex(i); }; 21 | 22 | protected: 23 | void * _getScanInfoByIndex(uint32_t i); 24 | 25 | private: 26 | AP_IF_S * ap_info; 27 | uint32_t ap_num; 28 | }; 29 | 30 | 31 | #endif /* WIFISCAN_H_ */ 32 | -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiServer.h: -------------------------------------------------------------------------------- 1 | #ifndef _WIFISERVER_H_ 2 | #define _WIFISERVER_H_ 3 | 4 | #include "api/Server.h" 5 | #include "WiFiClient.h" 6 | #include "api/IPAddress.h" 7 | #include "tal_log.h" 8 | class WiFiServer : public Server { 9 | private: 10 | int sockfd; 11 | int _accepted_sockfd = -1; 12 | IPAddress _addr; 13 | uint16_t _port; 14 | uint8_t _max_clients; 15 | bool _listening; 16 | bool _noDelay = false; 17 | 18 | public: 19 | void listenOnLocalhost(){} 20 | 21 | // _addr(INADDR_ANY) is the same as _addr() ==> 0.0.0.0 22 | WiFiServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) { 23 | PR_INFO("WiFiServer::WiFiServer(port=%d, ...)\r\n", port); 24 | } 25 | WiFiServer(const IPAddress& addr, uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_addr(addr),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false) { 26 | PR_INFO("WiFiServer::WiFiServer(addr=%s, port=%d, ...)\r\n", addr.toString().c_str(), port); 27 | } 28 | ~WiFiServer(){ end();} 29 | WiFiClient available(); 30 | WiFiClient accept(){return available();} 31 | void begin( ){ begin(0); }; 32 | void begin(uint16_t port=0); 33 | void begin(uint16_t port, int reuse_enable); 34 | void setNoDelay(bool nodelay); 35 | bool getNoDelay(); 36 | bool hasClient(); 37 | size_t write(const uint8_t *data, size_t len); 38 | size_t write(uint8_t data){ 39 | return write(&data, 1); 40 | } 41 | using Print::write; 42 | 43 | void end(); 44 | void close(); 45 | void stop(); 46 | operator bool(){return _listening;} 47 | int setTimeout(uint32_t seconds); 48 | void stopAll(); 49 | }; 50 | 51 | #endif -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiType.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFITYPE_H_ 2 | #define WIFITYPE_H_ 3 | 4 | #include "tkl_wifi.h" 5 | 6 | #define WIFI_SCAN_RUNNING (-1) 7 | #define WIFI_SCAN_FAILED (-2) 8 | 9 | #define WiFiMode_t WF_WK_MD_E 10 | #define WIFI_OFF WWM_POWERDOWN 11 | #define WIFI_STA WWM_STATION 12 | #define WIFI_AP WWM_SOFTAP 13 | #define WIFI_AP_STA WWM_STATIONAP 14 | 15 | #define WiFiEvent_t arduino_event_id_t 16 | #define WiFiEventInfo_t arduino_event_info_t 17 | #define WiFiEventId_t wifi_event_id_t 18 | 19 | #endif -------------------------------------------------------------------------------- /libraries/WiFi/src/WiFiUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets. 3 | * 4 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 5 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 6 | * might not happen often in practice, but in larger network topologies, a UDP 7 | * packet can be received out of sequence. 8 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 9 | * aware of it. Again, this may not be a concern in practice on small local networks. 10 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 11 | * 12 | * MIT License: 13 | * Copyright (c) 2008 Bjoern Hartmann 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to deal 16 | * in the Software without restriction, including without limitation the rights 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | * copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | * THE SOFTWARE. 31 | * 32 | * bjoern@cs.stanford.edu 12/30/2008 33 | */ 34 | 35 | #ifndef _WIFIUDP_H_ 36 | #define _WIFIUDP_H_ 37 | 38 | #include "api/Udp.h" 39 | #include 40 | 41 | using namespace arduino; 42 | class WiFiUDP : public UDP { 43 | private: 44 | int udp_server; 45 | IPAddress multicast_ip; 46 | IPAddress remote_ip; 47 | uint16_t server_port; 48 | uint16_t remote_port; 49 | char * tx_buffer; 50 | size_t tx_buffer_len; 51 | cbuf * rx_buffer; 52 | public: 53 | WiFiUDP(); 54 | ~WiFiUDP(); 55 | uint8_t begin(IPAddress a, uint16_t p); 56 | uint8_t begin(uint16_t p); 57 | uint8_t beginMulticast(IPAddress a, uint16_t p); 58 | void stop(); 59 | int beginMulticastPacket(); 60 | int beginPacket(); 61 | int beginPacket(IPAddress ip, uint16_t port); 62 | int beginPacket(const char *host, uint16_t port); 63 | int endPacket(); 64 | size_t write(uint8_t); 65 | size_t write(const uint8_t *buffer, size_t size); 66 | int parsePacket(); 67 | int available(); 68 | int read(); 69 | int read(unsigned char* buffer, size_t len); 70 | int read(char* buffer, size_t len); 71 | int peek(); 72 | void flush(); 73 | IPAddress remoteIP(); 74 | uint16_t remotePort(); 75 | }; 76 | 77 | #endif /* _WIFIUDP_H_ */ -------------------------------------------------------------------------------- /libraries/Wire/examples/masterWriter/masterWriter.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void setup() { 5 | Serial.begin(115200); // start serial for output 6 | Wire.begin(); // join i2c bus (address optional for master) 7 | } 8 | 9 | byte x = 0; 10 | 11 | void loop() { 12 | Serial.println("Sending: " + String(x)); 13 | Wire.beginTransmission(8); // transmit to device #8 14 | Wire.write(x); // sends one byte 15 | Wire.endTransmission(); // stop transmitting 16 | 17 | x++; 18 | delay(500); 19 | } 20 | -------------------------------------------------------------------------------- /libraries/Wire/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Wire 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | begin KEYWORD2 14 | setClock KEYWORD2 15 | beginTransmission KEYWORD2 16 | endTransmission KEYWORD2 17 | requestFrom KEYWORD2 18 | available KEYWORD2 19 | send KEYWORD2 20 | receive KEYWORD2 21 | onReceive KEYWORD2 22 | onRequest KEYWORD2 23 | setSDA KEYWORD2 24 | setSCL KEYWORD2 25 | 26 | ####################################### 27 | # Instances (KEYWORD2) 28 | ####################################### 29 | 30 | Wire KEYWORD2 31 | 32 | ####################################### 33 | # Constants (LITERAL1) 34 | ####################################### 35 | -------------------------------------------------------------------------------- /libraries/Wire/library.properties: -------------------------------------------------------------------------------- 1 | name=Wire 2 | version=0.0.1 3 | author=Tuya 4 | maintainer=Tuya 5 | sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus (IIC or I2C). 6 | paragraph= 7 | category=Signal Input/Output 8 | url=https://github.com/tuya/arduino-tuyaopen 9 | architectures=* 10 | -------------------------------------------------------------------------------- /libraries/Wire/src/Wire.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "api/HardwareI2C.h" 5 | 6 | #ifndef WIRE_BUFFER_SIZE 7 | #define WIRE_BUFFER_SIZE 128 8 | #endif 9 | 10 | class TwoWire : public HardwareI2C { 11 | protected: 12 | uint8_t _isBegin = 0; 13 | 14 | uint8_t _i2cPort = -1; 15 | int _clkHz; 16 | pin_size_t _sda = PIN_I2C0_SDA; 17 | pin_size_t _scl = PIN_I2C0_SCL; 18 | 19 | size_t _bufferSize; 20 | uint8_t *_rxBuffer; 21 | size_t _rxIndex; 22 | size_t _rxLength; 23 | 24 | uint8_t *_txBuffer; 25 | size_t _txLength; 26 | uint16_t _txAddress; 27 | 28 | uint32_t _timeOutMillis; 29 | public: 30 | TwoWire(uint8_t port); 31 | ~TwoWire(); 32 | 33 | void begin() override; 34 | void begin(uint8_t address) override; 35 | void end() override; 36 | 37 | // Select IO pins to use. Call before ::begin() 38 | bool setSDA(pin_size_t sda); 39 | bool setSCL(pin_size_t scl); 40 | void setClock(uint32_t freq) override; 41 | 42 | void beginTransmission(uint8_t address) override; 43 | uint8_t endTransmission(bool stopBit) override; 44 | uint8_t endTransmission(void) override; 45 | 46 | // Not support slave mode 47 | size_t requestFrom(uint8_t address, size_t len, bool stopBit) override; 48 | size_t requestFrom(uint8_t address, size_t len) override; 49 | 50 | void onReceive(void(*)(int)) override; 51 | void onRequest(void(*)(void)) override; 52 | 53 | size_t write(uint8_t) override; 54 | size_t write(const uint8_t *, size_t) override; 55 | int available() override; 56 | int read() override; 57 | int peek() override; 58 | void flush() override; 59 | 60 | size_t setBufferSize(size_t bSize); 61 | void freeWireBuffer(); 62 | }; 63 | 64 | extern TwoWire Wire; 65 | -------------------------------------------------------------------------------- /platform.txt: -------------------------------------------------------------------------------- 1 | name=Tuya 2 | version=1.1.6 3 | 4 | # 5 | ## Arduino build 6 | # 7 | ## prebuild 8 | recipe.hooks.prebuild.1.pattern="{runtime.tools.env-python.path}/python3" -I "{runtime.platform.path}/tools/prebuild.py" --chip "{build.chip}" --sketch-path "{sketch_path}" --build-path "{build.path}" 9 | recipe.hooks.prebuild.1.pattern.windows="{runtime.tools.env-python.path}/python.exe" -I "{runtime.platform.path}/tools/prebuild.py" --chip "{build.chip}" --sketch-path "{sketch_path}" --build-path "{build.path}" 10 | 11 | ## Compile c files 12 | recipe.c.o.pattern="{compiler.cmd.c}" {compiler.flags.c} {appBuild.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" {compiler.includes} {includes} {source_file} -o {object_file} 13 | 14 | ## Compile cpp files 15 | recipe.cpp.o.pattern="{compiler.cmd.cpp}" {compiler.flags.cpp} {appBuild.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" {compiler.includes} {includes} {source_file} -o {object_file} 16 | 17 | ## Compile S files 18 | recipe.S.o.pattern="{compiler.cmd.S}" {compiler.flags.S} {appBuild.flags} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DBOARD_NAME="{build.board}" {compiler.includes} {includes} {source_file} -o {object_file} 19 | 20 | ## ar 21 | archive_file_path={build.path}/{archive_file} 22 | recipe.ar.pattern="{compiler.cmd.ar}" {compiler.flags.ar} "{archive_file_path}" "{object_file}" 23 | 24 | ## combine 25 | recipe.c.combine.pattern="{compiler.cmd.ld}" "{compiler.flags.ld}" {object_files} -Wl,--start-group {archive_file_path} {compiler.flags.libs} -Wl,--end-group {compiler.flags.ld_scripts} 26 | 27 | ## objcopy 28 | recipe.objcopy.bin.1.pattern="{runtime.tools.env-python.path}/python3" -I "{runtime.platform.path}/tools/objcopy.py" --compiler-path "{compiler.path}" --chip "{build.chip}" --vendor-path "{vendor.path}" --build-path "{build.path}" --sketch-name "{build.project_name}" 29 | recipe.objcopy.bin.1.pattern.windows="{runtime.tools.env-python.path}/python.exe" -I "{runtime.platform.path}/tools/objcopy.py" --compiler-path "{compiler.path}" --chip "{build.chip}" --vendor-path "{vendor.path}" --build-path "{build.path}" --sketch-name "{build.project_name}" 30 | 31 | ## upload 32 | tools.tyutool.upload.protocol=serial 33 | tools.tyutool.upload.pattern={runtime.tools.tyutool.path}/tyutool_cli -n write -d {build.chip} -p {upload.port.address} -b {upload.speed} -s 0x000000 -f {build.path}/{build.project_name}_QIO.bin --tqdm 34 | tools.tyutool.upload.pattern.windows={runtime.tools.tyutool.path}/tyutool_cli.exe -n write -d {build.chip} -p {upload.port.address} -b {upload.speed} -s 0x000000 -f {build.path}/{build.project_name}_QIO.bin --tqdm 35 | 36 | ## export compiled binary 37 | recipe.hooks.savehex.postsavehex.1.pattern="{runtime.tools.env-python.path}/python3" -I "{runtime.platform.path}/tools/export_binary.py" --build-path {build.path} --sketch-path {sketch_path} 38 | recipe.hooks.savehex.postsavehex.1.pattern.windows="{runtime.tools.env-python.path}/python.exe" -I "{runtime.platform.path}/tools/export_binary.py" --build-path {build.path} --sketch-path {sketch_path} 39 | -------------------------------------------------------------------------------- /programmers.txt: -------------------------------------------------------------------------------- 1 | tyutool.name=tyutool 2 | tyutool.communication=serial 3 | tyutool.protocol=serial 4 | tyutool.program.protocol=serial 5 | tyutool.program.tool=tyutool 6 | tyutool.program.tool.default=tyutool 7 | tyutool.program.extra_params= 8 | tyutool.extra_params= -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | # Tools directory for the arduino-tuyaopen 2 | 3 | ## prebuild.py 4 | 5 | 解析应用固件版本号。 6 | -------------------------------------------------------------------------------- /tools/export_binary.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import sys 6 | import argparse 7 | import logging 8 | import shutil 9 | 10 | toolspath = os.path.dirname(os.path.realpath(__file__)) 11 | toolspath = os.path.normpath(toolspath) 12 | 13 | sys.path.insert(0, os.path.join(toolspath)) 14 | import fileutils 15 | 16 | FORMAT = '[%(levelname)-8s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 17 | logging.basicConfig( 18 | level=logging.INFO, 19 | format=FORMAT, 20 | datefmt='%H:%M:%S' 21 | ) 22 | 23 | parser = argparse.ArgumentParser() 24 | parser.add_argument('--build-path', type=str, help='Build path') 25 | parser.add_argument('--sketch-path', type=str, help='Sketch path') 26 | 27 | args = parser.parse_args() 28 | buildPath = args.build_path 29 | sketchPath = args.sketch_path 30 | 31 | # Normalize file paths 32 | buildPath = os.path.normpath(buildPath) 33 | sketchPath = os.path.normpath(sketchPath) 34 | 35 | buildOutputPath = os.path.join(buildPath, "tuyaTmp", "output") 36 | sketchOutputPath = os.path.join(sketchPath, "tuyaOutput") 37 | 38 | # Check if the build output path exists 39 | if not os.path.exists(buildOutputPath): 40 | logging.error("Build output path does not exist: " + buildOutputPath) 41 | sys.exit(1) 42 | 43 | fileutils.delete_if_exists(sketchOutputPath) 44 | 45 | # Copy the binary dir to the sketch folder 46 | shutil.copytree(buildOutputPath, sketchOutputPath) 47 | 48 | sys.exit(0) 49 | -------------------------------------------------------------------------------- /tools/fileutils/__init__.py: -------------------------------------------------------------------------------- 1 | # fileutils/__init__.py 2 | 3 | from .file_operations import delete_if_exists, copy_with_overwrite 4 | -------------------------------------------------------------------------------- /tools/fileutils/file_operations.py: -------------------------------------------------------------------------------- 1 | # fileutils/file_operations.py 2 | 3 | import os 4 | import shutil 5 | import logging 6 | 7 | def delete_if_exists(path): 8 | if not os.path.exists(path): 9 | logging.debug(f"{path} does not exist") 10 | return 11 | 12 | if os.path.isfile(path): 13 | os.remove(path) 14 | logging.debug(f"Deleted file {path}") 15 | elif os.path.isdir(path): 16 | shutil.rmtree(path) 17 | logging.debug(f"Deleted directory {path}") 18 | else: 19 | logging.warning(f"Unknown file type {path}") 20 | 21 | def copy_with_overwrite(source, target): 22 | if not os.path.exists(source): 23 | logging.error(f"Source file {source} does not exist") 24 | return False 25 | 26 | if os.path.exists(target): 27 | delete_if_exists(target) 28 | 29 | if os.path.isdir(source): 30 | shutil.copytree(source, target) 31 | elif os.path.isfile(source): 32 | shutil.copy2(source, target) 33 | else: 34 | logging.error(f"Unknown file type {source}") 35 | return False 36 | 37 | -------------------------------------------------------------------------------- /tools/objcopy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | import os 6 | import shutil 7 | import logging 8 | import argparse 9 | import platform 10 | import subprocess 11 | import json 12 | 13 | toolspath = os.path.dirname(os.path.realpath(__file__)) 14 | toolspath = os.path.normpath(toolspath) 15 | 16 | sys.path.insert(0, os.path.join(toolspath)) 17 | import sketch_version 18 | import fileutils 19 | import packager 20 | 21 | def log_config(level=logging.INFO): 22 | FORMAT = '[%(levelname)-8s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 23 | logging.basicConfig( 24 | level=level, 25 | format=FORMAT, 26 | datefmt='%H:%M:%S' 27 | ) 28 | 29 | def parse_args(): 30 | parser = argparse.ArgumentParser() 31 | parser.add_argument("--chip", type=str, required=True, help="chip type") 32 | parser.add_argument("--compiler-path", type=str, required=True, help="path to compiler tool path") 33 | parser.add_argument("--vendor-path", type=str, required=True, help="path to chip vendor") 34 | parser.add_argument("--build-path", type=str, required=True, help="path to build") 35 | parser.add_argument("--sketch-name", type=str, required=True, help="sketch name") 36 | parser.add_argument("-v", "--verbose", action='store_true', help="verbose output") 37 | return parser.parse_args() 38 | 39 | if __name__ == "__main__": 40 | args = parse_args() 41 | if args.verbose: 42 | log_config(logging.DEBUG) 43 | else: 44 | log_config() 45 | 46 | logging.info(f"Platform: {platform.system()}") 47 | 48 | compiler_path = os.path.normpath(args.compiler_path) 49 | vendor_path = os.path.normpath(args.vendor_path) 50 | build_path = os.path.normpath(args.build_path) 51 | 52 | logging.debug(f"Chip type: {args.chip}") 53 | logging.debug(f"Sketch name: {args.sketch_name}") 54 | logging.debug(f"Compiler_path: {compiler_path}") 55 | logging.debug(f"Vendor path: {vendor_path}") 56 | logging.debug(f"Build path: {build_path}") 57 | 58 | sketch_version_file = os.path.join(build_path, 'tuyaTmp', 'appConfig', 'appConfig.json') 59 | sketch_version = sketch_version.sketch_version_parse(sketch_version_file) 60 | output_path = os.path.join(build_path, 'tuyaTmp', 'output', sketch_version) 61 | logging.debug(f"Sketch version: {sketch_version}") 62 | 63 | fileutils.delete_if_exists(output_path) 64 | os.makedirs(output_path) 65 | if not os.path.exists(output_path): 66 | logging.error(f"Failed to create output path: {output_path}") 67 | sys.exit(1) 68 | 69 | # copy .elf, .map file to output path 70 | elf_file = os.path.join(build_path, f"{args.sketch_name}.elf") 71 | map_file = os.path.join(build_path, f"{args.sketch_name}.map") 72 | shutil.copy2(elf_file, output_path) 73 | shutil.copy2(map_file, output_path) 74 | 75 | tools_path = os.path.join(vendor_path, 'packager-tools') 76 | packager = packager.packager_class(args.chip, compiler_path, tools_path, output_path, args.sketch_name, sketch_version) 77 | 78 | if not packager.elf_to_binary(): 79 | sys.exit(1) 80 | logging.debug("elf_to_binary success") 81 | 82 | if not packager.get_qio_binary(): 83 | sys.exit(1) 84 | logging.debug("get_qio_binary success") 85 | 86 | arduino_upload_file = os.path.join(build_path, f"{args.sketch_name}_QIO.bin") 87 | if not packager.get_arduino_upload_binary(os.path.join(output_path, arduino_upload_file)): 88 | logging.error("get_arduino_upload_binary failed") 89 | sys.exit(1) 90 | logging.debug("get_arduino_upload_binary success") 91 | 92 | sys.exit(0) 93 | -------------------------------------------------------------------------------- /tools/packager/__init__.py: -------------------------------------------------------------------------------- 1 | # packager/__init__.py 2 | 3 | from .packager import * 4 | -------------------------------------------------------------------------------- /tools/packager/chip_info.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | # packager/chip_info.py 3 | import logging 4 | 5 | class chip_info_class: 6 | def __init__(self, chip, compiler_path, tools_path, output_path, sketch_name, sketch_version): 7 | self.chip = chip 8 | self.compiler_path = compiler_path 9 | self.tools_path = tools_path 10 | self.output_path = output_path 11 | self.sketch_name = sketch_name 12 | self.sketch_version = sketch_version 13 | self.bin_file = None 14 | self.bin_file_QIO = None 15 | self.bin_file_UA = None 16 | self.bin_file_UG = None 17 | 18 | def print_info(self): 19 | logging.debug(f"chip: {self.chip}") 20 | logging.debug(f"compiler_path: {self.compiler_path}") 21 | logging.debug(f"tools_path: {self.tools_path}") 22 | logging.debug(f"output_path: {self.output_path}") 23 | logging.debug(f"sketch_name: {self.sketch_name}") 24 | logging.debug(f"sketch_version: {self.sketch_version}") 25 | logging.debug(f"bin_file: {self.bin_file}") 26 | logging.debug(f"bin_file_QIO: {self.bin_file_QIO}") 27 | logging.debug(f"bin_file_UA: {self.bin_file_UA}") 28 | logging.debug(f"bin_file_UG: {self.bin_file_UG}") 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /tools/packager/packager_esp32.py: -------------------------------------------------------------------------------- 1 | import os 2 | import logging 3 | import shutil 4 | import platform 5 | 6 | from .chip_info import * 7 | 8 | OFFSET_START = 0x0 9 | OFFSET_BOOTLOADER = 0x1000 10 | OFFSET_PARTITIONS = 0x8000 11 | OFFSET_OTA_DATA_INITIAL = 0xd000 12 | OFFSET_APPLICATION = 0x10000 13 | OFFSET_END = 0x190000 14 | 15 | def esp32_image_gen(chip_info): 16 | bin_path_bootloader = os.path.join(chip_info.tools_path, "bootloader.bin") 17 | bin_path_partitions_table = os.path.join(chip_info.tools_path, "partition-table.bin") 18 | bin_path_ota_data_init = os.path.join(chip_info.tools_path, "ota_data_initial.bin") 19 | bin_path_app = os.path.join(chip_info.output_path, f"{chip_info.sketch_name}_app.bin") 20 | 21 | # output bin 22 | chip_info.bin_file_QIO = os.path.join(chip_info.output_path, f"{chip_info.sketch_name}_QIO_{chip_info.sketch_version}.bin") 23 | 24 | if not os.path.exists(bin_path_bootloader): 25 | logging.error(f"{bin_path_bootloader} not found") 26 | return False 27 | 28 | if not os.path.exists(bin_path_partitions_table): 29 | logging.error(f"{bin_path_partitions_table} not found") 30 | return False 31 | 32 | if not os.path.exists(bin_path_ota_data_init): 33 | logging.error(f"{bin_path_ota_data_init} not found") 34 | return False 35 | 36 | if not os.path.exists(bin_path_app): 37 | logging.error(f"{bin_path_app} not found") 38 | return False 39 | 40 | bin_files_in = [ 41 | ("bootloader", OFFSET_BOOTLOADER, bin_path_bootloader), 42 | ('partitions', OFFSET_PARTITIONS, bin_path_partitions_table), 43 | ('ota_data_initial', OFFSET_OTA_DATA_INITIAL, bin_path_ota_data_init), 44 | ('application', OFFSET_APPLICATION, bin_path_app) 45 | ] 46 | 47 | with open(chip_info.bin_file_QIO, 'wb') as bin_out: 48 | cur_offset = OFFSET_START 49 | for name, offset, bin_in in bin_files_in: 50 | bin_out.write(b'\xff' * (offset - cur_offset)) 51 | cur_offset = offset 52 | with open(bin_in, 'rb') as bin_in: 53 | data = bin_in.read() 54 | bin_out.write(data) 55 | cur_offset += len(data) 56 | offset = OFFSET_END 57 | bin_out.write(b'\xff' * (offset - cur_offset)) 58 | logging.debug(f"ESP32 QIO binary: {chip_info.bin_file_QIO}") 59 | logging.debug("package success.") 60 | 61 | # Copy bin_path_app 62 | chip_info.bin_file_UA = os.path.join(chip_info.output_path, f"{chip_info.sketch_name}_UA_{chip_info.sketch_version}.bin") 63 | chip_info.bin_file_UG = os.path.join(chip_info.output_path, f"{chip_info.sketch_name}_UG_{chip_info.sketch_version}.bin") 64 | shutil.copy2(bin_path_app, chip_info.bin_file_UA) 65 | shutil.copy2(bin_path_app, chip_info.bin_file_UG) 66 | 67 | # remove bin_path_app 68 | os.remove(bin_path_app) 69 | 70 | return True 71 | 72 | def get_qio_binary_esp32(chip_info): 73 | logging.debug(f"platform system: {platform.system()}") 74 | 75 | if False == esp32_image_gen(chip_info): 76 | logging.error("esp32_image_gen failed") 77 | return False 78 | 79 | return True 80 | 81 | -------------------------------------------------------------------------------- /tools/packager/packager_ln882h.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | # packager/packager.py 3 | 4 | import os 5 | import logging 6 | import subprocess 7 | import json 8 | import platform 9 | 10 | from .chip_info import * 11 | 12 | def merge_bin_files(file1_path, file2_path, output_file_path): 13 | try: 14 | with open(file1_path, 'rb') as file1: 15 | content1 = file1.read() 16 | 17 | with open(file2_path, 'rb') as file2: 18 | content2 = file2.read() 19 | 20 | merged_content = content1 + content2 21 | 22 | with open(output_file_path, 'wb') as output_file: 23 | output_file.write(merged_content) 24 | 25 | except Exception as e: 26 | print(f"error: {e}") 27 | 28 | def get_qio_binary_ln882h(chip_info): 29 | if platform.system() == 'Windows': 30 | ota_tool = os.path.join(chip_info.tools_path, 'windows', 'xz_tool.exe') 31 | elif platform.system() == 'Linux': 32 | ota_tool = os.path.join(chip_info.tools_path, 'linux', 'xz_tool') 33 | elif platform.system() == 'Darwin': 34 | mac_arch = platform.machine() 35 | logging.info(f"MAC machine is: {mac_arch}") 36 | ota_tool = os.path.join(chip_info.tools_path, 'mac', mac_arch, 'xz_tool') 37 | else: 38 | logging.error(f"Unsupported platform: {platform.system()}") 39 | return False 40 | 41 | ln882h_bootloader = os.path.join(chip_info.tools_path, "ln882h_bootloader.bin") 42 | 43 | logging.debug(f"ota_tool: {ota_tool}") 44 | logging.debug(f"ln882h_bootloader: {ln882h_bootloader}") 45 | 46 | if not os.path.exists(ota_tool): 47 | logging.error("ota_tool not exists") 48 | return False 49 | 50 | if not os.path.exists(ln882h_bootloader): 51 | logging.error("ln882h_bootloader not find") 52 | return False 53 | 54 | os.chdir(chip_info.output_path) 55 | 56 | logging.info(f"chip_info.bin_file: {chip_info.bin_file}") 57 | 58 | chip_info.bin_file_QIO = os.path.join(chip_info.output_path, f"{chip_info.sketch_name}_QIO_{chip_info.sketch_version}.bin") 59 | logging.info(f"QIO binary file: {chip_info.bin_file_QIO}") 60 | 61 | merge_bin_files(ln882h_bootloader, chip_info.bin_file, chip_info.bin_file_QIO) 62 | 63 | ota_command = [ 64 | ota_tool, 65 | '-i', 66 | chip_info.bin_file, 67 | '-v', 68 | chip_info.sketch_version 69 | ] 70 | 71 | logging.info("ota_command: " + ' '.join(ota_command)) 72 | result = subprocess.run(ota_command) 73 | if result.returncode != 0: 74 | logging.error("ota_command failed") 75 | return False 76 | 77 | return True 78 | -------------------------------------------------------------------------------- /tools/prebuild.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | import os 6 | import platform 7 | import logging 8 | import argparse 9 | 10 | toolspath = os.path.dirname(os.path.realpath(__file__)) 11 | toolspath = os.path.normpath(toolspath) 12 | 13 | sys.path.insert(0, os.path.join(toolspath)) 14 | import sketch_version 15 | 16 | sketch_config_json_name = 'appConfig.json' 17 | 18 | def log_config(level=logging.INFO): 19 | FORMAT = '[%(levelname)-8s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 20 | logging.basicConfig( 21 | level=level, 22 | format=FORMAT, 23 | datefmt='%H:%M:%S' 24 | ) 25 | 26 | def parse_args(): 27 | parser = argparse.ArgumentParser() 28 | parser.add_argument("--sketch-path", type=str, required=True, help="path to sketch") 29 | parser.add_argument("--build-path", type=str, required=True, help="path to build") 30 | parser.add_argument("--chip", type=str, required=True, help="chip type") 31 | parser.add_argument("-v", "--verbose", action='store_true', help="verbose output") 32 | return parser.parse_args() 33 | 34 | if __name__ == "__main__": 35 | args = parse_args() 36 | if args.verbose: 37 | log_config(logging.DEBUG) 38 | else: 39 | log_config() 40 | 41 | logging.debug(f"platform system: {platform.system()}") 42 | 43 | if platform.system() == "Darwin" and args.chip == "t2": 44 | logging.error("t2 not support Darwin") 45 | sys.exit(1) 46 | 47 | logging.debug(f"Toolspath: {toolspath}") 48 | 49 | # Copy sketch/appConfig.json -> /tmp/.../tuyaTmp/appConfig/appConfig.json 50 | app_config_json = os.path.join(args.sketch_path, sketch_config_json_name) 51 | app_config_dir = os.path.join(args.build_path, "tuyaTmp", 'appConfig') 52 | if not sketch_version.sketch_version_write(app_config_json, app_config_dir): 53 | sys.exit(1) 54 | 55 | sys.exit(0) 56 | -------------------------------------------------------------------------------- /tools/sketch_version/__init__.py: -------------------------------------------------------------------------------- 1 | # sketch_version/__init__.py 2 | 3 | from .sketch_version import sketch_version_write, sketch_version_parse 4 | -------------------------------------------------------------------------------- /tools/sketch_version/sketch_version.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | # sketch_version/sketch_version_parse.py 3 | 4 | import json 5 | import logging 6 | import re 7 | import os 8 | import shutil 9 | 10 | defaultVension="1.0.0" 11 | 12 | def sketch_version_parse(filePath): 13 | if not os.path.exists(filePath): 14 | return defaultVension 15 | 16 | try: 17 | with open(filePath, 'r') as file: 18 | data = json.load(file) 19 | version = data.get("PROJECT_VERSION", defaultVension) 20 | 21 | # Validate the version format 22 | if not re.match(r'^\d+\.\d+\.\d+$', version): 23 | raise ValueError(f"Incorrect version format, using default version {defaultVension}") 24 | except (json.JSONDecodeError, KeyError, ValueError) as e: 25 | logging.error(f"Parsing error: {e}") 26 | version = defaultVension 27 | 28 | return version 29 | 30 | def sketch_version_write(jsonFile, outputDir): 31 | # Check if the output directory exists, if not, create it 32 | if not os.path.exists(outputDir): 33 | try: 34 | os.makedirs(outputDir) 35 | logging.debug(f"Created directory {outputDir}") 36 | except OSError as e: 37 | logging.error(f"Failed to create directory {outputDir}: {e}") 38 | return False 39 | 40 | output_json_file = os.path.join(outputDir, 'appConfig.json') 41 | output_version_file = os.path.join(outputDir, 'appBuildDefine.txt') 42 | 43 | # Copy the appConfig.json file to the output directory 44 | if os.path.exists(jsonFile): 45 | try: 46 | shutil.copy2(jsonFile, output_json_file) 47 | logging.debug(f"File {jsonFile} has been copied to {output_json_file}") 48 | except IOError: 49 | logging.error(f"Failed to copy {jsonFile} to {output_json_file}") 50 | return False 51 | 52 | version = sketch_version_parse(output_json_file) 53 | 54 | # Prepare the version string with the required format and a newline at the end 55 | versionStr = '-DPROJECT_VERSION=\\\"' + version + '\\\"\n' 56 | 57 | # Write the version number to the file 58 | try: 59 | with open(output_version_file, "w") as text_file: 60 | text_file.write(versionStr) 61 | logging.debug(f"Version number {versionStr} has been written to {output_version_file}") 62 | except IOError: 63 | logging.error(f"Failed to write to file {output_version_file}") 64 | return False 65 | 66 | return True -------------------------------------------------------------------------------- /variants/XH_WB5E/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) 14 | #define pin1 (1u) 15 | #define pin2 (2u) 16 | #define pin3 (3u) 17 | #define pin4 (4u) 18 | #define pin5 (5u) 19 | #define pin6 (6u) 20 | #define pin7 (7u) 21 | #define pin8 (8u) 22 | #define pin9 (9u) 23 | #define pin12 (12u) 24 | #define pin13 (13u) 25 | #define pin14 (14u) 26 | #define pin15 (15u) 27 | #define pin16 (16u) 28 | #define pin17 (17u) 29 | #define pin18 (18u) 30 | #define pin19 (19u) 31 | #define pin20 (20u) 32 | #define pin21 (21u) 33 | #define pin22 (22u) 34 | #define pin23 (23u) 35 | #define pin24 (24u) 36 | #define pin25 (25u) 37 | #define pin26 (26u) 38 | #define pin27 (27u) 39 | #define pin28 (28u) 40 | #define pin29 (29u) 41 | #define pin30 (30u) 42 | #define pin31 (31u) 43 | #define pin32 (32u) 44 | #define pin33 (33u) 45 | #define pin34 (34u) 46 | #define pin35 (35u) 47 | #define pin36 (36u) 48 | #define pin37 (37u) 49 | #define pin38 (38u) 50 | #define pin39 (39u) 51 | #define pin40 (40u) 52 | #define pin41 (41u) 53 | #define pin42 (42u) 54 | #define pin43 (43u) 55 | #define pin44 (44u) 56 | #define pin45 (45u) 57 | #define pin46 (46u) 58 | #define pin47 (47u) 59 | 60 | #define LED_BUILTIN 9 61 | #define BUTTON_BUILTIN 7 62 | 63 | static const uint8_t A0 = (25u); 64 | static const uint8_t A1 = (24u); 65 | static const uint8_t A2 = (28u); 66 | static const uint8_t A3 = (13u); 67 | static const uint8_t A4 = (12u); 68 | 69 | static const uint8_t D0 = (18u); 70 | static const uint8_t D1 = (24u); 71 | static const uint8_t D2 = (32u); 72 | static const uint8_t D3 = (34u); 73 | static const uint8_t D4 = (36u); 74 | 75 | // uart 76 | static const uint8_t defaultSerial = 0; 77 | 78 | // adc 79 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 80 | 81 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 82 | uint8_t adcPinToChannel(uint8_t pin); 83 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 84 | 85 | // pwm 86 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 87 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 88 | 89 | // i2c 90 | #define PIN_I2C0_SCL (20u) 91 | #define PIN_I2C0_SDA (22u) 92 | 93 | // SPI 94 | #define SPI_DEFAULT_CLOCK (8000000u) 95 | 96 | // #define PIN_SPI0_SCK (34u) 97 | #define PIN_SPI0_CS (34u) 98 | #define PIN_SPI0_MISO (36u) 99 | #define PIN_SPI0_MOSI (35u) 100 | 101 | #define PIN_SPI1_SCK (2u) 102 | #define PIN_SPI1_CS (3u) 103 | #define PIN_SPI1_MISO (5u) 104 | #define PIN_SPI1_MOSI (4u) 105 | 106 | #if defined(__cplusplus) && !defined(c_plusplus) 107 | } 108 | #endif // __cplusplus 109 | 110 | #endif /* __PINS_ARDUINO_H__ */ 111 | -------------------------------------------------------------------------------- /variants/XH_WB5E/project_elf_src.c: -------------------------------------------------------------------------------- 1 | volatile const char build_version[] = __DATE__ " " __TIME__; 2 | -------------------------------------------------------------------------------- /variants/XH_WB5E/t5_hw_port.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "pins_arduino.h" 4 | 5 | static TUYA_ADC_BASE_CFG_T sgAdcConfig[TUYA_ADC_NUM_MAX] = { 6 | {.ch_list.bits.ch_0=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin26 7 | {.ch_list.bits.ch_1=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin24 8 | {.ch_list.bits.ch_2=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin23 9 | {.ch_list.bits.ch_3=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin28 10 | {.ch_list.bits.ch_4=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin22 11 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 12 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 13 | }; 14 | 15 | // duty 5000 = 50% 16 | static TUYA_PWM_BASE_CFG_T sgPwmConfig[TUYA_PWM_NUM_MAX] = { 17 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin18 18 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin24 19 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin32 20 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin34 21 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin36 22 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000} // none 23 | }; 24 | 25 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin) 26 | { 27 | switch (pin) { 28 | case 26: 29 | return sgAdcConfig[0]; 30 | case 24: 31 | return sgAdcConfig[1]; 32 | case 23: 33 | return sgAdcConfig[2]; 34 | case 28: 35 | return sgAdcConfig[3]; 36 | case 22: 37 | return sgAdcConfig[4]; 38 | default: 39 | return sgAdcConfig[5]; 40 | } 41 | } 42 | 43 | uint8_t adcPinToChannel(uint8_t pin) 44 | { 45 | switch (pin) { 46 | case 25: 47 | return 0; 48 | case 24: 49 | return 1; 50 | case 23: 51 | return 2; 52 | case 28: 53 | return 3; 54 | case 22: 55 | return 4; 56 | default: 57 | return TUYA_ADC_INVALID_VALUE; 58 | } 59 | } 60 | 61 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin) 62 | { 63 | // ADC only one channel 64 | return TUYA_ADC_NUM_0; 65 | } 66 | 67 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin) 68 | { 69 | switch (pin) { 70 | case 18: 71 | return TUYA_PWM_NUM_0; 72 | case 24: 73 | return TUYA_PWM_NUM_1; 74 | case 32: 75 | return TUYA_PWM_NUM_2; 76 | case 34: 77 | return TUYA_PWM_NUM_3; 78 | case 36: 79 | return TUYA_PWM_NUM_4; 80 | default: 81 | return TUYA_PWM_NUM_MAX; 82 | } 83 | } 84 | 85 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin) 86 | { 87 | switch (pin) { 88 | case 6: 89 | return sgPwmConfig[0]; 90 | case 7: 91 | return sgPwmConfig[1]; 92 | case 8: 93 | return sgPwmConfig[2]; 94 | case 24: 95 | return sgPwmConfig[3]; 96 | case 26: 97 | return sgPwmConfig[4]; 98 | default: 99 | return sgPwmConfig[5]; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /variants/esp32/esp32_hw_port.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "pins_arduino.h" 4 | 5 | static TUYA_ADC_BASE_CFG_T sgAdcConfig[TUYA_ADC_NUM_MAX] = { 6 | {.ch_list.bits.ch_0=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin14 7 | {.ch_list.bits.ch_1=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin15 8 | {.ch_list.bits.ch_2=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin20 9 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 10 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 11 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 12 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 13 | }; 14 | 15 | // duty 5000 = 50% 16 | static TUYA_PWM_BASE_CFG_T sgPwmConfig[TUYA_PWM_NUM_MAX] = { 17 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin19 18 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // none 19 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // none 20 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // none 21 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // none 22 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // none 23 | }; 24 | 25 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin) 26 | { 27 | switch (pin) { 28 | case 14: 29 | return sgAdcConfig[0]; 30 | case 15: 31 | return sgAdcConfig[1]; 32 | case 20: 33 | return sgAdcConfig[2]; 34 | default: 35 | return sgAdcConfig[5]; 36 | } 37 | } 38 | 39 | uint8_t adcPinToChannel(uint8_t pin) 40 | { 41 | switch (pin) { 42 | case 14: 43 | return 0; 44 | case 15: 45 | return 1; 46 | case 20: 47 | return 2; 48 | default: 49 | return TUYA_ADC_INVALID_VALUE; 50 | } 51 | } 52 | 53 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin) 54 | { 55 | // ADC only one channel 56 | return TUYA_ADC_NUM_0; 57 | } 58 | 59 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin) 60 | { 61 | switch (pin) { 62 | case 19: 63 | return TUYA_PWM_NUM_0; 64 | default: 65 | return TUYA_PWM_NUM_MAX; 66 | } 67 | } 68 | 69 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin) 70 | { 71 | switch (pin) { 72 | case 19: 73 | return sgPwmConfig[0]; 74 | default: 75 | return sgPwmConfig[5]; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /variants/esp32/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) 14 | #define pin1 (1u) 15 | #define pin2 (2u) 16 | #define pin3 (3u) 17 | #define pin4 (4u) 18 | #define pin5 (5u) 19 | #define pin6 (6u) 20 | #define pin7 (7u) 21 | #define pin8 (8u) 22 | #define pin9 (9u) 23 | #define pin10 (10u) 24 | #define pin11 (11u) 25 | #define pin12 (12u) 26 | #define pin13 (13u) 27 | #define pin14 (14u) 28 | #define pin15 (15u) 29 | #define pin16 (16u) 30 | #define pin17 (17u) 31 | #define pin18 (18u) 32 | #define pin19 (19u) 33 | #define pin20 (20u) 34 | #define pin21 (21u) 35 | #define pin22 (22u) 36 | #define pin23 (23u) 37 | #define pin25 (25u) 38 | #define pin26 (26u) 39 | #define pin27 (27u) 40 | #define pin28 (28u) 41 | #define pin29 (29u) 42 | #define pin30 (30u) 43 | #define pin31 (31u) 44 | #define pin32 (32u) 45 | #define pin33 (33u) 46 | #define pin34 (34u) 47 | #define pin35 (35u) 48 | #define pin36 (36u) 49 | #define pin37 (37u) 50 | #define pin38 (38u) 51 | #define pin39 (39u) 52 | 53 | #define LED_BUILTIN pin33 54 | #define BUTTON_BUILTIN pin35 55 | 56 | static const uint8_t A0 = (14u); 57 | static const uint8_t A1 = (15u); 58 | static const uint8_t A2 = (20u); 59 | 60 | static const uint8_t D0 = (19u); 61 | 62 | // uart 63 | static const uint8_t defaultSerial = 0; 64 | 65 | // adc 66 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 67 | 68 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 69 | uint8_t adcPinToChannel(uint8_t pin); 70 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 71 | 72 | // pwm 73 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 74 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 75 | 76 | // i2c 77 | #define PIN_I2C0_SCL (22) 78 | #define PIN_I2C0_SDA (21) 79 | 80 | // SPI 81 | #define SPI_DEFAULT_CLOCK (8000000u) 82 | 83 | #define PIN_SPI0_MISO (19) 84 | #define PIN_SPI0_MOSI (23) 85 | #define PIN_SPI0_SCK (18) 86 | #define PIN_SPI0_CS (5) 87 | 88 | #if defined(__cplusplus) && !defined(c_plusplus) 89 | } 90 | #endif // __cplusplus 91 | 92 | #endif /* __PINS_ARDUINO_H__ */ 93 | -------------------------------------------------------------------------------- /variants/ln882h/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) // PB9 14 | #define pin1 (1u) // PB8 15 | #define pin6 (6u) // PA11 16 | #define pin7 (7u) // PA10 17 | #define pin8 (8u) // PA7 18 | #define pin9 (9u) // PB5 19 | #define pin10 (10u) // PA3 20 | #define pin11 (11u) // PA2 21 | #define pin14 (14u) // PA5 22 | #define pin15 (15u) // PB7 23 | #define pin16 (16u) // PA6 24 | #define pin17 (17u) // PB6 25 | #define pin20 (20u) // PA4 26 | #define pin21 (21u) // PA9 27 | #define pin22 (22u) // PA0 28 | #define pin23 (23u) // PA1 29 | #define pin24 (24u) // PB3 30 | #define pin26 (26u) // PA12 31 | #define pin28 (28u) // PB4 32 | 33 | #define LED_BUILTIN 26 34 | #define BUTTON_BUILTIN 7 35 | 36 | static const uint8_t A0 = (23u); 37 | static const uint8_t A1 = (22u); 38 | static const uint8_t A2 = (24u); 39 | static const uint8_t A3 = (26u); 40 | static const uint8_t A4 = (28u); 41 | 42 | static const uint8_t D0 = (6u); 43 | static const uint8_t D1 = (7u); 44 | static const uint8_t D2 = (8u); 45 | static const uint8_t D3 = (24u); 46 | static const uint8_t D4 = (26u); 47 | 48 | // uart 49 | static const uint8_t defaultSerial = 0; 50 | 51 | // adc 52 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 53 | 54 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 55 | uint8_t adcPinToChannel(uint8_t pin); 56 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 57 | 58 | // pwm 59 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 60 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 61 | 62 | // i2c 63 | #define PIN_I2C0_SCL (20) 64 | #define PIN_I2C0_SDA (22) 65 | 66 | // SPI 67 | #define SPI_DEFAULT_CLOCK (8000000u) 68 | 69 | #define PIN_SPI0_MISO (17) 70 | #define PIN_SPI0_MOSI (16) 71 | #define PIN_SPI0_SCK (14) 72 | #define PIN_SPI0_CS (15) 73 | 74 | #if defined(__cplusplus) && !defined(c_plusplus) 75 | } 76 | #endif // __cplusplus 77 | 78 | #endif /* __PINS_ARDUINO_H__ */ 79 | -------------------------------------------------------------------------------- /variants/t2/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) 14 | #define pin1 (1u) 15 | #define pin6 (6u) 16 | #define pin7 (7u) 17 | #define pin8 (8u) 18 | #define pin9 (9u) 19 | #define pin14 (14u) 20 | #define pin15 (15u) 21 | #define pin16 (16u) 22 | #define pin17 (17u) 23 | #define pin20 (20u) 24 | #define pin22 (22u) 25 | #define pin23 (23u) 26 | #define pin24 (24u) 27 | #define pin26 (26u) 28 | #define pin28 (28u) 29 | 30 | #define LED_BUILTIN 26 31 | #define BUTTON_BUILTIN 7 32 | 33 | static const uint8_t A0 = (23u); 34 | static const uint8_t A1 = (22u); 35 | static const uint8_t A2 = (24u); 36 | static const uint8_t A3 = (26u); 37 | static const uint8_t A4 = (28u); 38 | 39 | static const uint8_t D0 = (6u); 40 | static const uint8_t D1 = (7u); 41 | static const uint8_t D2 = (8u); 42 | static const uint8_t D3 = (24u); 43 | static const uint8_t D4 = (26u); 44 | 45 | // uart 46 | static const uint8_t defaultSerial = 0; 47 | 48 | // adc 49 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 50 | 51 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 52 | uint8_t adcPinToChannel(uint8_t pin); 53 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 54 | 55 | // pwm 56 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 57 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 58 | 59 | // i2c 60 | #define PIN_I2C0_SCL (20) 61 | #define PIN_I2C0_SDA (22) 62 | 63 | // SPI 64 | #define SPI_DEFAULT_CLOCK (8000000u) 65 | 66 | #define PIN_SPI0_MISO (17) 67 | #define PIN_SPI0_MOSI (16) 68 | #define PIN_SPI0_SCK (14) 69 | #define PIN_SPI0_CS (15) 70 | 71 | #if defined(__cplusplus) && !defined(c_plusplus) 72 | } 73 | #endif // __cplusplus 74 | 75 | #endif /* __PINS_ARDUINO_H__ */ 76 | -------------------------------------------------------------------------------- /variants/t2/t2_hw_port.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "pins_arduino.h" 4 | 5 | static TUYA_ADC_BASE_CFG_T sgAdcConfig[TUYA_ADC_NUM_MAX] = { 6 | {.ch_list.bits.ch_0=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin26 7 | {.ch_list.bits.ch_1=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin24 8 | {.ch_list.bits.ch_2=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin23 9 | {.ch_list.bits.ch_3=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin28 10 | {.ch_list.bits.ch_4=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin22 11 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 12 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 13 | }; 14 | 15 | // duty 5000 = 50% 16 | // cycle T2 not use 17 | static TUYA_PWM_BASE_CFG_T sgPwmConfig[TUYA_PWM_NUM_MAX] = { 18 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin6 19 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin7 20 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin8 21 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin24 22 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin26 23 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000} // none 24 | }; 25 | 26 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin) 27 | { 28 | switch (pin) { 29 | case 26: 30 | return sgAdcConfig[0]; 31 | case 24: 32 | return sgAdcConfig[1]; 33 | case 23: 34 | return sgAdcConfig[2]; 35 | case 28: 36 | return sgAdcConfig[3]; 37 | case 22: 38 | return sgAdcConfig[4]; 39 | default: 40 | return sgAdcConfig[5]; 41 | } 42 | } 43 | 44 | uint8_t adcPinToChannel(uint8_t pin) 45 | { 46 | switch (pin) { 47 | case 26: 48 | return 0; 49 | case 24: 50 | return 1; 51 | case 23: 52 | return 2; 53 | case 28: 54 | return 3; 55 | case 22: 56 | return 4; 57 | default: 58 | return TUYA_ADC_INVALID_VALUE; 59 | } 60 | } 61 | 62 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin) 63 | { 64 | // ADC only one channel 65 | return TUYA_ADC_NUM_0; 66 | } 67 | 68 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin) 69 | { 70 | switch (pin) { 71 | case 6: 72 | return TUYA_PWM_NUM_0; 73 | case 7: 74 | return TUYA_PWM_NUM_1; 75 | case 8: 76 | return TUYA_PWM_NUM_2; 77 | case 24: 78 | return TUYA_PWM_NUM_4; 79 | case 26: 80 | return TUYA_PWM_NUM_5; 81 | default: 82 | return TUYA_PWM_NUM_MAX; 83 | } 84 | } 85 | 86 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin) 87 | { 88 | switch (pin) { 89 | case 6: 90 | return sgPwmConfig[0]; 91 | case 7: 92 | return sgPwmConfig[1]; 93 | case 8: 94 | return sgPwmConfig[2]; 95 | case 24: 96 | return sgPwmConfig[3]; 97 | case 26: 98 | return sgPwmConfig[4]; 99 | default: 100 | return sgPwmConfig[5]; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /variants/t3/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) 14 | #define pin1 (1u) 15 | #define pin10 (10u) 16 | #define pin11 (11u) 17 | #define pin12 (12u) 18 | #define pin13 (13u) 19 | #define pin14 (14u) 20 | #define pin15 (15u) 21 | #define pin16 (16u) 22 | #define pin17 (17u) 23 | #define pin18 (18u) 24 | #define pin19 (19u) 25 | #define pin24 (24u) 26 | #define pin25 (25u) 27 | #define pin28 (28u) 28 | #define pin32 (32u) 29 | #define pin34 (34u) 30 | #define pin36 (36u) 31 | #define pin41 (41u) 32 | #define pin40 (40u) 33 | #define pin48 (48u) 34 | 35 | #define LED_BUILTIN 36 36 | #define BUTTON_BUILTIN 32 37 | 38 | static const uint8_t A0 = (25u); 39 | static const uint8_t A1 = (24u); 40 | static const uint8_t A2 = (28u); 41 | static const uint8_t A3 = (12u); 42 | static const uint8_t A4 = (13u); 43 | 44 | static const uint8_t D0 = (18u); 45 | static const uint8_t D1 = (24u); 46 | static const uint8_t D2 = (32u); 47 | static const uint8_t D3 = (34u); 48 | static const uint8_t D4 = (36u); 49 | 50 | // uart 51 | static const uint8_t defaultSerial = 0; 52 | 53 | // adc 54 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 55 | 56 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 57 | uint8_t adcPinToChannel(uint8_t pin); 58 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 59 | 60 | // i2c 61 | #define PIN_I2C0_SCL (40) 62 | #define PIN_I2C0_SDA (41) 63 | 64 | // pwm 65 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 66 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 67 | 68 | // SPI 69 | #define SPI_DEFAULT_CLOCK (8000000u) 70 | 71 | #define PIN_SPI0_MISO (17) 72 | #define PIN_SPI0_MOSI (16) 73 | #define PIN_SPI0_SCK (14) 74 | #define PIN_SPI0_CS (15) 75 | 76 | #if defined(__cplusplus) && !defined(c_plusplus) 77 | } 78 | #endif // __cplusplus 79 | 80 | #endif /* __PINS_ARDUINO_H__ */ 81 | -------------------------------------------------------------------------------- /variants/t3/project_elf_src.c: -------------------------------------------------------------------------------- 1 | volatile const char build_version[] = __DATE__ " " __TIME__; 2 | -------------------------------------------------------------------------------- /variants/t5/pins_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef __PINS_ARDUINO_H__ 2 | #define __PINS_ARDUINO_H__ 3 | 4 | #include 5 | 6 | #include "tuya_cloud_types.h" 7 | 8 | #if defined(__cplusplus) && !defined(c_plusplus) 9 | extern "C" { 10 | #endif // __cplusplus 11 | 12 | // Number of pins defined in PinDescription array 13 | #define pin0 (0u) 14 | #define pin1 (1u) 15 | #define pin2 (2u) 16 | #define pin3 (3u) 17 | #define pin4 (4u) 18 | #define pin5 (5u) 19 | #define pin6 (6u) 20 | #define pin7 (7u) 21 | #define pin8 (8u) 22 | #define pin9 (9u) 23 | #define pin12 (12u) 24 | #define pin13 (13u) 25 | #define pin14 (14u) 26 | #define pin15 (15u) 27 | #define pin16 (16u) 28 | #define pin17 (17u) 29 | #define pin18 (18u) 30 | #define pin19 (19u) 31 | #define pin20 (20u) 32 | #define pin21 (21u) 33 | #define pin22 (22u) 34 | #define pin23 (23u) 35 | #define pin24 (24u) 36 | #define pin25 (25u) 37 | #define pin26 (26u) 38 | #define pin27 (27u) 39 | #define pin28 (28u) 40 | #define pin29 (29u) 41 | #define pin30 (30u) 42 | #define pin31 (31u) 43 | #define pin32 (32u) 44 | #define pin33 (33u) 45 | #define pin34 (34u) 46 | #define pin35 (35u) 47 | #define pin36 (36u) 48 | #define pin37 (37u) 49 | #define pin38 (38u) 50 | #define pin39 (39u) 51 | #define pin40 (40u) 52 | #define pin41 (41u) 53 | #define pin42 (42u) 54 | #define pin43 (43u) 55 | #define pin44 (44u) 56 | #define pin45 (45u) 57 | #define pin46 (46u) 58 | #define pin47 (47u) 59 | 60 | #define LED_BUILTIN 9 61 | #define BUTTON_BUILTIN 7 62 | 63 | static const uint8_t A0 = (25u); 64 | static const uint8_t A1 = (24u); 65 | static const uint8_t A2 = (28u); 66 | static const uint8_t A3 = (13u); 67 | static const uint8_t A4 = (12u); 68 | 69 | static const uint8_t D0 = (18u); 70 | static const uint8_t D1 = (24u); 71 | static const uint8_t D2 = (32u); 72 | static const uint8_t D3 = (34u); 73 | static const uint8_t D4 = (36u); 74 | 75 | // uart 76 | static const uint8_t defaultSerial = 0; 77 | 78 | // adc 79 | #define TUYA_ADC_INVALID_VALUE (0xFFu) 80 | 81 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin); 82 | uint8_t adcPinToChannel(uint8_t pin); 83 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin); 84 | 85 | // pwm 86 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin); 87 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin); 88 | 89 | // i2c 90 | #define PIN_I2C0_SCL (20u) 91 | #define PIN_I2C0_SDA (22u) 92 | 93 | // SPI 94 | #define SPI_DEFAULT_CLOCK (8000000u) 95 | 96 | // #define PIN_SPI0_SCK (34u) 97 | #define PIN_SPI0_CS (34u) 98 | #define PIN_SPI0_MISO (36u) 99 | #define PIN_SPI0_MOSI (35u) 100 | 101 | #define PIN_SPI1_SCK (2u) 102 | #define PIN_SPI1_CS (3u) 103 | #define PIN_SPI1_MISO (5u) 104 | #define PIN_SPI1_MOSI (4u) 105 | 106 | #if defined(__cplusplus) && !defined(c_plusplus) 107 | } 108 | #endif // __cplusplus 109 | 110 | #endif /* __PINS_ARDUINO_H__ */ 111 | -------------------------------------------------------------------------------- /variants/t5/project_elf_src.c: -------------------------------------------------------------------------------- 1 | volatile const char build_version[] = __DATE__ " " __TIME__; 2 | -------------------------------------------------------------------------------- /variants/t5/t5_hw_port.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "pins_arduino.h" 4 | 5 | static TUYA_ADC_BASE_CFG_T sgAdcConfig[TUYA_ADC_NUM_MAX] = { 6 | {.ch_list.bits.ch_0=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin26 7 | {.ch_list.bits.ch_1=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin24 8 | {.ch_list.bits.ch_2=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin23 9 | {.ch_list.bits.ch_3=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin28 10 | {.ch_list.bits.ch_4=1, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // pin22 11 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 12 | {.ch_list.data=0, .ch_nums=1, .width=12, .mode=TUYA_ADC_CONTINUOUS, .type=TUYA_ADC_INNER_SAMPLE_VOL, .conv_cnt = 1}, // not support 13 | }; 14 | 15 | // duty 5000 = 50% 16 | static TUYA_PWM_BASE_CFG_T sgPwmConfig[TUYA_PWM_NUM_MAX] = { 17 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin18 18 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin24 19 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin32 20 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin34 21 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000}, // pin36 22 | {.polarity=TUYA_PWM_NEGATIVE, .count_mode=TUYA_PWM_CNT_UP, .duty=5000, .cycle=10000, .frequency = 1000} // none 23 | }; 24 | 25 | TUYA_ADC_BASE_CFG_T adcCfgGet(uint8_t pin) 26 | { 27 | switch (pin) { 28 | case 26: 29 | return sgAdcConfig[0]; 30 | case 24: 31 | return sgAdcConfig[1]; 32 | case 23: 33 | return sgAdcConfig[2]; 34 | case 28: 35 | return sgAdcConfig[3]; 36 | case 22: 37 | return sgAdcConfig[4]; 38 | default: 39 | return sgAdcConfig[5]; 40 | } 41 | } 42 | 43 | uint8_t adcPinToChannel(uint8_t pin) 44 | { 45 | switch (pin) { 46 | case 25: 47 | return 0; 48 | case 24: 49 | return 1; 50 | case 23: 51 | return 2; 52 | case 28: 53 | return 3; 54 | case 22: 55 | return 4; 56 | default: 57 | return TUYA_ADC_INVALID_VALUE; 58 | } 59 | } 60 | 61 | TUYA_ADC_NUM_E adcPinToNum(uint8_t pin) 62 | { 63 | // ADC only one channel 64 | return TUYA_ADC_NUM_0; 65 | } 66 | 67 | TUYA_PWM_NUM_E pwmPinToNum(uint8_t pin) 68 | { 69 | switch (pin) { 70 | case 18: 71 | return TUYA_PWM_NUM_0; 72 | case 24: 73 | return TUYA_PWM_NUM_1; 74 | case 32: 75 | return TUYA_PWM_NUM_2; 76 | case 34: 77 | return TUYA_PWM_NUM_3; 78 | case 36: 79 | return TUYA_PWM_NUM_4; 80 | default: 81 | return TUYA_PWM_NUM_MAX; 82 | } 83 | } 84 | 85 | TUYA_PWM_BASE_CFG_T pwmCfgGet(uint8_t pin) 86 | { 87 | switch (pin) { 88 | case 6: 89 | return sgPwmConfig[0]; 90 | case 7: 91 | return sgPwmConfig[1]; 92 | case 8: 93 | return sgPwmConfig[2]; 94 | case 24: 95 | return sgPwmConfig[3]; 96 | case 26: 97 | return sgPwmConfig[4]; 98 | default: 99 | return sgPwmConfig[5]; 100 | } 101 | } 102 | --------------------------------------------------------------------------------