Tuya Sandwich Evaluation Kit -Wi-Fi Arduino-Lantern Implementation

Last Updated on2020-02-13 11:33:2762

Overview

This document mainly introduces the application code part of the RGB light demo of the Tuya Sandwich Development Board, about environment building, SDK porting, downloading and debugging. Please refer to the following article.

《Tuya Sandwich Development Board Development Environment》

《Tuya Sandwich Development Board Quick Start》

《MCU-SDK Migration》

Terms

Steps

  • Step 1: Download verification

    1. ST Nucleo, long press button 1 for network configuration, the black button next to the indicator light is the reset port of the development board, and USB is the program download port.

      image-20191205205848397

    2. Tuya sandwich lighting board

    3. Tuya Sandwich Wi-Fi board

    4. The connection method is as follows: (from top to bottom: graffiti sandwich lighting board, ST Nucleo, Tuya Sandwich Wi-Fi board)

    Note: In order to prevent the development board from constantly restarting after the download is completed due to insufficient USB power supply of the computer, it is recommended to use an independent power board to supply power, or remove the RGB board during download and connect it after download is complete.

  • Step 2: Program implementation

    Developers need to focus on three functions: setup (), loop () and dp_download_handle ()

    1. The setup () function is used to perform some initialization content, only executed once
    void setup () {
      pinMode (PC13, INPUT); // Initialization of distribution network
      mySerial.begin (9600); // Software serial port initialization
      mySerial.println ("myserial init successful!");
      Serial.begin (115200); // PA3 RX PA2 TX
      Serial.println ("serial init successful! \ R \ n");
      light_data_int (); // Initialization of RGB cold and warm lights
      IIC_Init (); // IIC initialization is used to drive RGB lights
      delay (10);
      rgb_init (0, 0, 0);
      wifi_protocol_init (); // Protocol serial port initialization function
      timer_init (); // Timer initialization
      Hue_shade_start (& a);
    }
    
    1. The loop () function will loop through the contents of the function body
    void loop () {
      if (init_flag == 0) {
        time_cnt ++;
        if (time_cnt% 6000 == 0) {
          time_cnt = 0;
          cnt ++;
        }
        wifi_stat_led (& cnt); // WI-FI status processing
      }
      wifi_uart_service (); // WI-FI serial port processing service
      myserialEvent (); // Serial port receiving processing
      key_scan (); // Reset network button detection
    }
    
    
    / ************************************************* ****************************
      Function Name: dp_download_handle
      Function description: dp release processing function
      Input parameters: dpid: DP
               value: dp data buffer address
               length: dp data length
      Return parameter: Success return: SUCCESS / Failure return: ERRO
      Instructions for use: This function cannot be modified by the user
    ********************************************** *************************** /
    unsigned char dp_download_handle (unsigned char dpid, const unsigned char value [], unsigned short length)
    {
      / *********************************
        Current function processing can be issued / reportable data call
        The specific function needs to implement the issued data processing
        To complete the application, the processing result needs to be fed back to the APP.Otherwise, the APP will consider that the delivery fails.
      *********************************** /
      unsigned char ret;
      printf ("dpid =% d \ r \ n", dpid);
      switch (dpid)
      {
        case DPID_SWITCH_LED:
          // Switch processing function
          ret = dp_download_switch_led_handle (value, length);
          break;
        case DPID_WORK_MODE:
          // pattern processing function
          ret = dp_download_work_mode_handle (value, length);
          break;
        case DPID_BRIGHT_VALUE:
          // White light brightness processing function
          ret = dp_download_bright_value_handle (value, length);
          break;
        case DPID_TEMP_VALUE:
          // White light color temperature processing function
          ret = dp_download_temp_value_handle (value, length);
          break;
        case DPID_COLOUR_DATA:
          // Color light color processing function
          ret = dp_download_colour_data_handle (value, length);
          break;
        case DPID_CONTROL_DATA:
          // Adjust processing function
          ret = dp_download_control_data_handle (value, length);
          break;
    
        default:
          break;
      }
      return ret;
    }
    
    1. dp_download_handle () is mainly used to process DP data issued by the cloud, developers need to implement the corresponding functions according to the corresponding DP release
    / ************************************************* ****************************
      Function Name: dp_download_handle
      Function description: dp release processing function
      Input parameters: dpid: DP
               value: dp data buffer address
               length: dp data length
      Return parameter: Success return: SUCCESS / Failure return: ERRO
      Instructions for use: This function cannot be modified by the user
    ********************************************** *************************** /
    unsigned char dp_download_handle (unsigned char dpid, const unsigned char value [], unsigned short length)
    {
      / *********************************
        Current function processing can be issued / reportable data call
        The specific function needs to implement the issued data processing
        To complete the application, the processing result needs to be fed back to the APP.Otherwise, the APP will consider that the delivery fails.
      *********************************** /
      unsigned char ret;
      printf ("dpid =% d \ r \ n", dpid);
      switch (dpid)
      {
        case DPID_SWITCH_LED:
          // Switch processing function
          ret = dp_download_switch_led_handle (value, length);
          break;
        case DPID_WORK_MODE:
          // pattern processing function
          ret = dp_download_work_mode_handle (value, length);
          break;
        case DPID_BRIGHT_VALUE:
          // White light brightness processing function
          ret = dp_download_bright_value_handle (value, length);
          break;
        case DPID_TEMP_VALUE:
          // White light color temperature processing function
          ret = dp_download_temp_value_handle (value, length);
          break;
        case DPID_COLOUR_DATA:
          // Color light color processing function
          ret = dp_download_colour_data_handle (value, length);
          break;
        case DPID_CONTROL_DATA:
          // Adjust processing function
          ret = dp_download_control_data_handle (value, length);
          break;
    
        default:
          break;
      }
      return ret;
    }
    
    Panel RGB Data Processing Functions dp_download_colour_data_handle ()
    Panel cold light data processing function dp_download_bright_value_handle ()
    Panel warm light data processing function dp_download_temp_value_handle ()

    The color data sent from the cloud is in HSV format. We need to convert it to RGB format and modify the RGB data to get the correct RGB value for display.

    Several important APIs for RGB dimming:

    HSV to RGB interface hsv2rgb ();
    RGB data correction interface tuya_light_gamma_adjust ();
    RGB color setting interface rgb_init ();

    Specific implementation details can be found in Demo

  • Step 3: Arduino-API instructions

    As the Tuya Sandwich development board is programmed using Arduino standard API, Arduino-API developers can refer to the following URL:

    《Arduino-API Instructions》

    ArduinoIDE also provides Demo to demonstrate the use of related APIs