一、断电记忆
1、原理
断电记忆的原理是将设备在断电前的各个工作状态写入到空闲的flash闪存中,在下次上电时从flash中读取数据,让设备工作在断电前的工作状态。
TLSR8253芯片flash布局如下:
0x040000 - 0x060000为未使用区域,可将设备状态写入该区域内。
2、实现
(1)写状态到flash
/***********************************************************
* Function: write_massage_status_to_flash
* Input: none
* Output: none
* Return: none
* Notice: 将按摩仪状态写到 flash 中
***********************************************************/
void write_massage_status_to_flash(void)
{Flash_Write_Buff[0] = massage_state.on_off;Flash_Write_Buff[1] = massage_state.pattern;Flash_Write_Buff[2] = massage_state.gear;Flash_Write_Buff[3] = massage_state.heat;flash_write_page(FLASH_ADDR, FLASH_BUFF_LEN, (unsigned char *)Flash_Write_Buff);return;
}
(2)从flash读状态
/**********************************************************************
* Function: read_massage_status_to_flash
* Input: none
* Output: none
* Return: none
* Notice: 从 flash 中读取按摩仪断电前状态,存到按摩仪状态结构体内
**********************************************************************/
void read_massage_status_to_flash(void)
{flash_read_page(FLASH_ADDR, FLASH_BUFF_LEN, (unsigned char *)Flash_Read_Buff);//将从 flash 读取到的数据,存放到结构体中massage_state.on_off = Flash_Read_Buff[0];massage_state.pattern = Flash_Read_Buff[1];massage_state.gear = Flash_Read_Buff[2];massage_state.heat = Flash_Read_Buff[3];return;
}
(3)恢复出厂设置
/***********************************************************
* Function: erase_massage_flash
* Input: none
* Output: none
* Return: none
* Notice: 恢复出厂设置
***********************************************************/
void erase_massage_flash(void)
{massage_state.on_off = OFF;massage_state.pattern = relieve;massage_state.gear = first_gear;massage_state.heat = off_heat;Flash_Write_Buff[0] = OFF;Flash_Write_Buff[1] = relieve;Flash_Write_Buff[2] = first_gear;Flash_Write_Buff[3] = off_heat;flash_write_page(FLASH_ADDR, FLASH_BUFF_LEN, (unsigned char *)Flash_Write_Buff);return;
}
二、云端控制
单点蓝牙一个DP点的所有信息存放在一个数组中。以功能模式为例,其DPID为104,对应数组为:
unsigned char mode_buf[] = {0x68, 0x04, 0x01, 0x00}; //{DP_ID, DP_type, DP_len, DP_data}
tuya_ble_dp_data_report(mode_buf, 4); //数据上报函数
上报单个DP点数据时调用tuya_ble_dp_data_report(uint8_t *p_data,uint32_t len)函数即将数据上报到云端。
APP下发控制命令也是以数组的形式存放在dp_data_array[255+3]数组中,编写DP数据下发处理函数,将dp_data_array[255+3]作为输入参数放在tuya_ble_demo.c中的tuya_cb_handler(tuya_ble_cb_evt_param_t* event)函数中即可实现APP指令下发。
2、实现代码
void app_dp_handle(uint8_t *dp_data)
{printf("dp_data:%d %d %d %d\r\n", dp_data[0], dp_data[1], dp_data[2], dp_data[3]); switch (dp_data[0]) {case 0x66:if (dp_data[3] == strong_heat) {massage_state.heat = strong_heat;} else {massage_state.heat = off_heat;}printf("dp_data[3]:%d massage_state.heat:%d\r\n", dp_data[3], massage_state.heat);if (!app_flag) {switching_heat(massage_state.heat);}break;case 0x67:printf("dp_data[3]:%d \r\n", dp_data[3]);switch (dp_data[3]) {case first_gear:hhhhhhhhhmassage_state.gear = first_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case second_gear:massage_state.gear = second_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case third_gear:massage_state.gear = third_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case fourth_gear:massage_state.gear = fourth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case fifth_gear:massage_state.gear = fifth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case sixth_gear:massage_state.gear = sixth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case seventh_gear:massage_state.gear = seventh_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case eighth_gear:massage_state.gear = eighth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case ninth_gear:massage_state.gear = ninth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case tenth_gear:massage_state.gear = tenth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case eleventh_gear:massage_state.gear = eleventh_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case twelfth_gear:massage_state.gear = twelfth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case thirteenth_gear:massage_state.gear = thirteenth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case fourteenth_gear:massage_state.gear = fourteenth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case fifteenth_gear:massage_state.gear = fifteenth_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;case max_gear:massage_state.gear = max_gear;if (!app_flag) {switching_gear(massage_state.gear);}break;default:break;}break;case 0x68:if (dp_data[3] == relieve) {h'h'h'h'h'h'h'h'hmassage_state.pattern = relieve;} else if (dp_data[3] == vitality) {massage_state.pattern = vitality;} else if (dp_data[3] == hammering) {massage_state.pattern = hammering;} else if (dp_data[3] == scraping_therapy) {massage_state.pattern = scraping_therapy;} else {massage_state.pattern = intelligent;}break;case 0x69:if (dp_data[3] == ON) {massage_state.on_off = ON;rs2255_init();voice_prompt_init();pattern_pin_init();app_flag = 0;} else {massage_state.on_off = OFF;power_off_init();app_flag = 1;}break;default:break;}
}
戳我:更多物联网应用在这里!