TuyaOSBluetooth FrameworkProduct DevelopmentBluetooth Mesh Lighting (PHY6222)

Bluetooth Mesh Lighting (PHY6222)

Last Updated on : 2023-03-31 02:46:18download

This topic describes the features provided by the Bluetooth Mesh Lighting Product Development Kit (PHY6222).

Release

Release Description
1.0 (applies to TuyaOS v3.5.0) Implement the basic features for a lighting product.

Description

  • Device pairing.
  • The LED blinks every 500 ms for 6 minutes to indicate the pairing state.
  • Repeating the on/off cycle five times can reset the device. If no operation is performed within six minutes, the device restores to the previous network state.
  • The white and color light is adjusted by PWM.
  • The standard protocol allows for group-based sync of lighting scenes.
  • Music sync.
  • On/Off gradient (800 ms).
  • Incremental storage.
  • OTA updates.

app_common.c includes examples of using components. V1.0 only supports the PWM driver. You can add the required driver as needed. To adjust the maximum and minimum brightness and power scale, right-click your project and choose Config Project in Tuya Wind IDE.

Application logic

app_common.c

Architecture

Bluetooth Mesh Lighting (PHY6222)

By default, logging is disabled in app_common.c and other components. To enable logging, modify the settings in tal_log.h. The serial port baud rate for authorization defaults to 9600 and can be set to 115200 for log printing. If the transmission rate is slow due to a large volume of log output, the device might restart. In the production stage, set ENABLE_APP_LOG to 0 for authorization.

#define ENABLE_APP_LOG      1

ty_light_save_user_flash.c

The data is stored incrementally in two blocks alternately. It is 4-byte aligned and contains a 4-byte cyclic redundancy check (CRC) value. You can store up to three IDs by default. To add an ID, edit the macro ID_NUM_MAX and include the ID in the struct LIGHT_FLASH_SAVE_ID_E.

To enable log printing, set the macro ENABLE_FLASH_RW_LOG to 1.

#define ENABLE_FLASH_RW_LOG      1

Architecture

Bluetooth Mesh Lighting (PHY6222)

Macro definition

/*ty_light_save_user_flash.h*/
#define LIGHT_CTRL_DATA_FLASH_ADDR1     0x7D000    // 0x45000 for Telink and Phyplus
#define LIGHT_CTRL_DATA_FLASH_ADDR2     0x7E000    // 0x46000 for Telink and Phyplus
#define ID_NUM_MAX                       3   // Currently, the system can store data for up to three IDs.
#define FLASH_WRITE_DATA_MAX_LEN        100 // Up to 100 bytes can be stored at a time, with 4-byte alignment.

/*ty_light_save_user_flash.c*/
#define FLASH_WRITE_SIZE            4000 // The size of a block is 4 KB, with 96 bytes reserved.

Structs

/*ty_light_save_user_flash.h*/
typedef enum
 {
    ID_RESET_CNT_DATA = 0,
    ID_LIGHT_CTRL_DATA,
    ID_LIGHT_MEM_TYPE_DATA,
    ID_MAX,
}LIGHT_FLASH_SAVE_ID_E; // Three IDs are used for: reset, light data, and scene information. The NV component stores and maintains the scene data.

/*ty_light_save_user_flash.c*/
typedef struct
{
    UINT8_T data_id;   // ID
    UINT32_T offset;   // ID data storage address
} DATA_ID_INFO_T;      // ID cache

typedef struct
{
    UINT8_T head; //0x5A
    UINT8_T data_id;
    UINT16_T size;
    UINT32_T data_crc; //CRC check
} DATA_RW_INFO_T;

typedef struct
{
    UINT32_T read;
    UINT32_T write;
} DATA_FLASH_RW_OFFSET_T; // Read/write address cache

Global variables

DATA_ID_INFO_T data_id_offset[ID_NUM_MAX]; // ID data cache
DATA_FLASH_RW_OFFSET_T data_flash_rw_offset; // read/write address cache

STATIC UINT8_T total_id_num; //Total number of IDs
STATIC UINT32_T flash_rw_original_addr = LIGHT_CTRL_DATA_FLASH_ADDR1 + FLASH_HEAD_SIZE;// Initial storage address

Block diagram

  • OPERATE_RET ty_light_save_flash_init(VOID_T)

    Initialize storage: Initialize the memory address of each data storage ID.

    Bluetooth Mesh Lighting (PHY6222)
  • OPERATE_RET ty_light_save_flash_data_write(UINT8_T data_id, UINT8_T *p_data, UINT16_T data_len)

    Write data incrementally: Write the data, read the comparison, and write the header information.

    Bluetooth Mesh Lighting (PHY6222)
  • OPERATE_RET ty_light_save_flash_data_read(UINT8_T data_id, UINT8_T *p_data, UINT16_T read_len, UINT16_T *out_len);

    Read data.

    Bluetooth Mesh Lighting (PHY6222)

    out_len indicates the length of the returned data, which allows for changes in data length when needed.

  • OPERATE_RET ty_light_save_flash_data_delete(UINT8_T data_id)

    Delete data: Delete the ID of the cache and write header data as 0.

    Bluetooth Mesh Lighting (PHY6222)
  • OPERATE_RET ty_light_save_flash_recover_default(VOID_T)

    Erase the storage data and reset to default state.

    Bluetooth Mesh Lighting (PHY6222)

Dependent components

tbl_light_control

  • Component description: basic light control.

  • Project file: ..\apps\tuyaos-mesh-light-demo-phy6222\app.components\tbl_light_control\README.md

tbl_light_tools

  • Component description: algorithm tool.

  • Project file: ..\apps\tuyaos-mesh-light-demo-phy6222\app.components\tbl_light_tools\README.md

tdd_light_pwm

  • Component description: PWM driver.

  • Project file: ..\apps\tuyaos-mesh-light-demo-phy6222\app.components\tdd_light_pwm\README.md

tdl_light_driver_management

  • Component description: driver management.

  • Project file: ..\apps\tuyaos-mesh-light-demo-phy6222\app.components\tdl_light_driver_management\README.md

tfm_light_gradual

  • Component description: gradient lighting effect.

  • Project file: ..\apps\tuyaos-mesh-light-demo-phy6222\app.components\tfm_light_gradual\README.md

tbl_light_scene

  • Function definition: standard lighting scenes.

    Implement simultaneous lighting patterns such as for building facade lighting, multi-holder ceiling lights, and ambient lights. This component is adapted to the standard lighting scene protocol. It can parse the scene data and transmit the control parameters for simultaneous scene transitions.

    This component can automatically parse and execute scene data and retrieve information about the currently running scene.

  • Dependencies:

    • High dependence on tal_sw_timer to create, start, and stop a timer.

      • Create: OPERATE_RET tal_sw_timer_create(TAL_TIMER_CB func, VOID_T *arg, TIMER_ID *timer_id)
      • Start: OPERATE_RET tal_sw_timer_start(TIMER_ID timer_id, TIME_MS time_ms, TIMER_TYPE timer_type)
      • Stop: OPERATE_RET tal_sw_timer_stop(TIMER_ID timer_id)
    • Dependence on tal_system to generate random numbers.

      INT_T tal_system_get_random(UINT32_T range)
      
    • Dependence on tal_bluetooth_mesh_device to send sync signals.

      OPERATE_RET tal_mesh_data_send(USHORT_T src_addr, USHORT_T dst_addr, UINT_T opcode, UCHAR_T *data, USHORT_T data_len);
      
    • Dependence on functions in tfm_light_gradual. For more information about the functions, see tfm_light_gradual.

      • OPERATE_RET tfm_light_gradual_ctrl_calc_rgbcw(UINT8_T *ctrl_data)

      • OPERATE_RET tfm_light_gradual_ctrl_start(UINT8_T *gradual_param)

  • Design details

    • Architecture:

      Bluetooth Mesh Lighting (PHY6222)

      For the gradient light pattern, the light value is set every 10 ms (LIGHT_SHADE_CYCLE), with a maximum value of 20,000 (CTRL_CAL_VALUE_RANGE). You can call tbl_light_scene_variable_init(LIGHT_SHADE_CYCLE, CTRL_CAL_VALUE_RANGE); to set the initial values for these two parameters.

      To implement transmission sync, you need to set the time to live (TTL) and calculate the delay time. TTL is used to limit the number of times a message can be relayed.

    • Function calls:

      Bluetooth Mesh Lighting (PHY6222)
    • Block diagram

      Bluetooth Mesh Lighting (PHY6222)
  • Scenarios

    • Initialization:

      The application layer manages the scene data. When powering on the device, assign the default scene data to the scene component.

          #define LIGHT_SHADE_CYCLE           10// light shade cycle (unit:ms)
          #define CTRL_CAL_VALUE_RANGE        20000
      
          tbl_light_scene_variable_init(LIGHT_SHADE_CYCLE, CTRL_CAL_VALUE_RANGE);
          OPERATE_RET op_ret = OPRT_OK;
          TBL_LIGHT_CTRL_SCENE_DATA_T sence_data;
          UINT8_T scene_data_temp[14] = {0x01,0x0b,0x0b,0x02,0x00,0x00,0x03,0xe8,0x03,0xe8,0x00,0x00,0x00,0x00};
          sence_data.data_len = 14;
          memcpy(sence_data.data, scene_data_temp, 14);
          op_ret = tbl_light_scene_data_set(&sence_data);// Set the initial scene data on power on.
      
    • General control:

      Call tbl_light_scene_start_with_first_uint to run a scene from its first unit. If this function is not called, the system will run the unit next to the last stopped unit.

          OPERATE_RET op_ret = OPRT_OK;
          TBL_LIGHT_CTRL_SCENE_DATA_T sence_data;
          UINT8_T scene_data_temp[14] = {0x01,0x0b,0x0b,0x02,0x00,0x00,0x03,0xe8,0x03,0xe8,0x00,0x00,0x00,0x00};
          sence_data.data_len = 14;
          memcpy(sence_data.data, scene_data_temp, 14);
      
          op_ret = tbl_light_scene_data_set(&sence_data);// Set scene data.
          if(op_ret != OPRT_OK){
              return op_ret;
          }
          tbl_light_scene_start_with_first_uint(TRUE);
      
          op_ret = tbl_light_scene_ctrl_start();
          return op_ret;
      

Flashing and authorization

In Tuya Wind IDE, right-click on your project and choose Config Project to change the parameters shown in the following figure. Then, generate the macro definitions in app_config.h.

#define PRODUCTKEY "key3rsd3" // Firmware key. If IS_FIRMWARE_KEY is set to 0, populate this parameter with the PID.
#define MESH_CATEGORY 0x1015   // The capability value for lighting product. The last digit represents the channel of the light. For example, 1014 represents a 4-channel light, that is cool white and colored light (RGBC).
#define IS_FIRMWARE_KEY 1      // Specifies whether to use this parameter as the firmware key.
#define NEED_PUBLISH_ADDR 0    // Specifies whether to assign PUBLISH_ADDR when pairing. Typically, set this parameter to 1 for local linkage.
Bluetooth Mesh Lighting (PHY6222) Bluetooth Mesh Lighting (PHY6222) Bluetooth Mesh Lighting (PHY6222)

For more information about firmware flashing, see ..\software\TuyaOS\docs.

Things to note

  • Device network status is indicated after the protocol stack is initialized. If you want the light to come on during this period, configure the function tuya_init_first.
  • The app compresses the scene and music data before sending it. The component then decompresses the received data to set the value. Likewise, before the scene data is reported, it is first compressed.
  • ty_light_save_user_flash is used to maintain light data. Since scene data is rarely modified, it is maintained using the NV component.
  • The white light does not come with complementary PWM, which needs to be implemented by you.

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.