wanghechen il y a 1 an
Parent
commit
10243010e6

+ 77 - 80
components/Decection/Decection.c

@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include "Decection.h"
 
-
 #include "esp_log.h"
 #include "esp_adc/adc_oneshot.h"
 #include "esp_adc/adc_cali.h"
@@ -12,16 +11,15 @@
 #include "esp_sleep.h"
 const static char *TAG = "Decection";
 
-//#include "LED.h"
-
+// #include "LED.h"
 
 static int adc_raw[2][10];
 static int voltage[2][10];
 
-#define USER_KEY_CHANNEL  ADC_CHANNEL_7
+#define USER_KEY_CHANNEL ADC_CHANNEL_7
 
-#define USER_ADC_CHANNEL  ADC_CHANNEL_2
-#define USER_ADC_UNIT   ADC_UNIT_1
+#define USER_ADC_CHANNEL ADC_CHANNEL_2
+#define USER_ADC_UNIT ADC_UNIT_1
 static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
 static void adc_calibration_deinit(adc_cali_handle_t handle);
 
@@ -39,75 +37,77 @@ int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle)
     bool do_calibration;
 
     adc_oneshot_chan_cfg_t config = {
-    .bitwidth = ADC_BITWIDTH_DEFAULT,
-    .atten = ADC_ATTEN_DB_11,
+        .bitwidth = ADC_BITWIDTH_DEFAULT,
+        .atten = ADC_ATTEN_DB_11,
     };
 
     do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_11, &adc2_cali_handle);
     //-------------ADC Config---------------//
     adc_oneshot_config_channel(adc_handle, USER_ADC_CHANNEL, &config);
-    //adc_ll_set_power_manage(ADC_POWER_BY_FSM);
+    // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
     adc_oneshot_read(adc_handle, USER_ADC_CHANNEL, &adc_raw[0][0]);
-    //adc_ll_set_power_manage(ADC_POWER_SW_OFF);
+    // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
 
     ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, adc_raw[0][0]);
 
-    if (do_calibration) {
+    if (do_calibration)
+    {
         ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
         ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, voltage[0][0]);
         adc_calibration_deinit(adc2_cali_handle);
-    }   
+    }
     int batt_percent;
 
-    //电量范围映射
-    float result_voltage = (float)(voltage[0][0]) / 1000.0/*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
+    // 电量范围映射
+    float result_voltage = (float)(voltage[0][0]) / 1000.0 /*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
     float battery_percentage = ((result_voltage - 1.8) / (2.2 - 1.8)) * 100.0;
     // 限制电量范围在0%到100%之间
-    if (battery_percentage <= 0) {
-    battery_percentage = 0;
-    } else if (battery_percentage >= 100) {
-    battery_percentage = 100;
+    if (battery_percentage <= 0)
+    {
+        battery_percentage = 0;
+    }
+    else if (battery_percentage >= 100)
+    {
+        battery_percentage = 100;
     }
 
-    batt_percent = (int )battery_percentage; 
+    batt_percent = (int)battery_percentage;
 
-    #if 0
+#if 0
         ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
-    #else
-        // printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage);
-    #endif
+#else
+    // printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage);
+#endif
     return batt_percent;
 }
 
-
-
-
 int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle)
 {
     bool do_calibration;
 
     adc_oneshot_chan_cfg_t config = {
-    .bitwidth = ADC_BITWIDTH_DEFAULT,
-    .atten = ADC_ATTEN_DB_0,
+        .bitwidth = ADC_BITWIDTH_DEFAULT,
+        .atten = ADC_ATTEN_DB_0,
     };
 
     do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_0, &adc2_cali_handle);
     //-------------ADC Config---------------//
     ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, USER_KEY_CHANNEL, &config));
-    //adc_ll_set_power_manage(ADC_POWER_BY_FSM);
+    // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
     ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, USER_KEY_CHANNEL, &adc_raw[0][0]));
-    //adc_ll_set_power_manage(ADC_POWER_SW_OFF);
+    // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
 
     ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, adc_raw[0][0]);
 
-    if (do_calibration) {
+    if (do_calibration)
+    {
         ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
         ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, voltage[0][0]);
         adc_calibration_deinit(adc2_cali_handle);
-    }   
+    }
     int batt_percent;
 
-    #if 0
+#if 0
     //电量范围映射
     float result_voltage = (float)(voltage[0][0]) / 1000.0/*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
     float battery_percentage = ((result_voltage - 1.5) / (2.2 - 1.5)) * 100.0;
@@ -118,27 +118,24 @@ int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle)
     battery_percentage = 100;
     }
 
-    batt_percent = (int )battery_percentage; 
+    batt_percent = (int )battery_percentage;
 
-    #if 0
+#if 0
         ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
-    #else
+#else
         printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage);
-    #endif
-    #else
-
+#endif
+#else
 
-     printf("left key voltage:%d\r\n",voltage[0][0]);
+    printf("left key voltage:%d\r\n", voltage[0][0]);
 
     batt_percent = voltage[0][0];
 
-
-    #endif
+#endif
     return batt_percent;
 }
 extern adc_oneshot_unit_handle_t adc1_handle;
 
-
 int read_battery_voltage()
 {
     // printf("读电量\n");
@@ -154,7 +151,8 @@ static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_ha
     esp_err_t ret = ESP_FAIL;
     bool calibrated = false;
 
-    if (!calibrated) {
+    if (!calibrated)
+    {
         ESP_LOGD(TAG, "calibration scheme version is %s", "Curve Fitting");
         adc_cali_curve_fitting_config_t cali_config = {
             .unit_id = unit,
@@ -162,17 +160,23 @@ static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_ha
             .bitwidth = ADC_BITWIDTH_DEFAULT,
         };
         ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
-        if (ret == ESP_OK) {
+        if (ret == ESP_OK)
+        {
             calibrated = true;
         }
     }
 
     *out_handle = handle;
-    if (ret == ESP_OK) {
+    if (ret == ESP_OK)
+    {
         ESP_LOGD(TAG, "Calibration Success");
-    } else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
+    }
+    else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated)
+    {
         ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
-    } else {
+    }
+    else
+    {
         ESP_LOGE(TAG, "Invalid arg or no memory");
     }
 
@@ -185,73 +189,66 @@ static void adc_calibration_deinit(adc_cali_handle_t handle)
     ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
 }
 
-
- //返回充电中状态
+// 返回充电中状态
 int decection_state_0(void)
 {
     return gpio_get_level(2);
 }
 
-//返回充满状态
+// 返回充满状态
 int decection_state_1(void)
 {
 
-    return 0;//gpio_get_level(38);
+    return 0; // gpio_get_level(38);
 }
 
-
-
-
-
-
-
-
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "freertos/queue.h"
 #include "SPIFFS.h"
-extern RTC_FAST_ATTR Machine_info_t   Machine_info;
-static void IRAM_ATTR gpio_isr_handler(void* arg)
+extern RTC_FAST_ATTR Machine_info_t Machine_info;
+static void IRAM_ATTR gpio_isr_handler(void *arg)
 {
-    uint32_t gpio_num = (uint32_t) arg;
-    switch(gpio_num)
+    uint32_t gpio_num = (uint32_t)arg;
+    switch (gpio_num)
     {
 
-      case  2:
+    case 2:
 
-           // beep_blink(100,3);
-            //printf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
+        // beep_blink(100,3);
+        // printf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
         break;
 
-        case 38:
+    case 38:
 
-           // beep_blink(100,4);//rintf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
+        // beep_blink(100,4);//rintf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
         break;
 
-        default :break;
+    default:
+        break;
     }
 }
 
 void decection_charging_init(void)
 {
-    //zero-initialize the config structure.
+    // zero-initialize the config structure.
     gpio_config_t io_conf = {};
-    //interrupt of rising edge
-    io_conf.intr_type = GPIO_INTR_NEGEDGE;  //下降沿
-    //bit mask of the pins
-    io_conf.pin_bit_mask =((1ULL<<2) | (1ULL<<38));
-    //set as input mode
+    // interrupt of rising edge
+    io_conf.intr_type = GPIO_INTR_NEGEDGE; // 下降沿
+    // bit mask of the pins
+    io_conf.pin_bit_mask = ((1ULL << 2) | (1ULL << 38));
+    // set as input mode
     io_conf.mode = GPIO_MODE_INPUT;
-    io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 禁用上拉电阻
+    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;     // 禁用上拉电阻
     io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 禁用下拉电阻
-    //configure GPIO with the given settings
+    // configure GPIO with the given settings
     gpio_config(&io_conf);
 
     ESP_LOGW(TAG, "decection_charging_init");
-   //install gpio isr service
+    // install gpio isr service
     gpio_install_isr_service(ESP_INTR_FLAG_EDGE);
-    gpio_isr_handler_add(2, gpio_isr_handler, (void*) 2);
-    gpio_isr_handler_add(38, gpio_isr_handler, (void*) 38);
+    gpio_isr_handler_add(2, gpio_isr_handler, (void *)2);
+    gpio_isr_handler_add(38, gpio_isr_handler, (void *)38);
 }
 // void decection_charging_deinit(void)
 // {

Fichier diff supprimé car celui-ci est trop grand
+ 707 - 836
components/EPD/EPD.c


Fichier diff supprimé car celui-ci est trop grand
+ 853 - 654
components/EPD/GUI_Paint.c


Fichier diff supprimé car celui-ci est trop grand
+ 91312 - 8435
components/EPD/image.c


Fichier diff supprimé car celui-ci est trop grand
+ 495 - 305
components/EPD/qr_encode.c


+ 120 - 138
components/FONT_LIB/FONT_LIB.c

@@ -10,150 +10,138 @@
 
 static const char *LOG_TAG = "FONTLIB";
 
-
 static void task_delay_ms(uint32_t ms_count)
 {
-    vTaskDelay(ms_count / portTICK_PERIOD_MS);
+	vTaskDelay(ms_count / portTICK_PERIOD_MS);
 }
 
 void gt_font_pin_init()
 {
-    gpio_config_t fontLib_pin_cfg = {};
-
-    fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
-    fontLib_pin_cfg.mode = GPIO_MODE_OUTPUT;
-    fontLib_pin_cfg.pin_bit_mask =FONT_OUTPUT_PIN_SEL;
-    fontLib_pin_cfg.pull_down_en = 0;
-    fontLib_pin_cfg.pull_up_en = 0;
-    gpio_config(&fontLib_pin_cfg);
-
-    fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
-    fontLib_pin_cfg.mode = GPIO_MODE_INPUT;
-    fontLib_pin_cfg.pin_bit_mask =FONT_INPUT_PIN_SEL;
-    fontLib_pin_cfg.pull_down_en = 0;
-    fontLib_pin_cfg.pull_up_en = 1;
-    gpio_config(&fontLib_pin_cfg);    
+	gpio_config_t fontLib_pin_cfg = {};
+
+	fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
+	fontLib_pin_cfg.mode = GPIO_MODE_OUTPUT;
+	fontLib_pin_cfg.pin_bit_mask = FONT_OUTPUT_PIN_SEL;
+	fontLib_pin_cfg.pull_down_en = 0;
+	fontLib_pin_cfg.pull_up_en = 0;
+	gpio_config(&fontLib_pin_cfg);
+
+	fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
+	fontLib_pin_cfg.mode = GPIO_MODE_INPUT;
+	fontLib_pin_cfg.pin_bit_mask = FONT_INPUT_PIN_SEL;
+	fontLib_pin_cfg.pull_down_en = 0;
+	fontLib_pin_cfg.pull_up_en = 1;
+	gpio_config(&fontLib_pin_cfg);
 }
 
 void font_init()
 {
-    gt_font_pin_init();
+	gt_font_pin_init();
 
-    FONT_CLK_1;
+	FONT_CLK_1;
 	FONT_SDA_1;
 	FONT_CS_HIGHT;
 
-	font_exit_sleep(); //退出睡眠
+	font_exit_sleep(); // 退出睡眠
 
-	//task_delay_ms(500); 
+	// task_delay_ms(500);
 
 #if 1
-    uint8_t id = GT_Font_Init();
-    while(id==0)
-    {
-	  	ESP_LOGE(LOG_TAG,"font fail");
+	uint8_t id = GT_Font_Init();
+	while (id == 0)
+	{
+		ESP_LOGE(LOG_TAG, "font fail");
 		task_delay_ms(500);
-      	id = GT_Font_Init();
-    }
+		id = GT_Font_Init();
+	}
 #endif
-	ESP_LOGI(LOG_TAG,"font OK");
-
+	ESP_LOGI(LOG_TAG, "font OK");
 }
 void font_spi_write(unsigned char value)
 {
-  int i=0,data=value;   
-
-  gpio_set_level(FONT_CLK_PIN,LOW_LEVEL);
-   
-  for(i=0;i<8;i++)
-  {
-    if(data&0x0080)
-    {
-       FONT_SDA_1;
-    }
-      
-    else
-    {
-       FONT_SDA_0;
-    }
-    //ets_delay_us(1);
-    gpio_set_level(FONT_CLK_PIN,HIGH_LEVEL);
-    //ets_delay_us(1);
-    gpio_set_level(FONT_CLK_PIN,LOW_LEVEL);
-    data=((data<<1)&0xff);
-  }
-     
-}
+	int i = 0, data = value;
 
+	gpio_set_level(FONT_CLK_PIN, LOW_LEVEL);
 
-void Send_Byte(unsigned char out)
-{	
-	unsigned char i=0;
-
-	for(i=0;i<8;i++)
+	for (i = 0; i < 8; i++)
 	{
-	  FONT_CLK_0;  
-	  if(((out<<i)&0x80)==0)
-		  FONT_SDA_0;   
-	  else
-		  FONT_SDA_1;
-	  FONT_CLK_1;
-  }
+		if (data & 0x0080)
+		{
+			FONT_SDA_1;
+		}
+
+		else
+		{
+			FONT_SDA_0;
+		}
+		// ets_delay_us(1);
+		gpio_set_level(FONT_CLK_PIN, HIGH_LEVEL);
+		// ets_delay_us(1);
+		gpio_set_level(FONT_CLK_PIN, LOW_LEVEL);
+		data = ((data << 1) & 0xff);
+	}
 }
 
+void Send_Byte(unsigned char out)
+{
+	unsigned char i = 0;
 
+	for (i = 0; i < 8; i++)
+	{
+		FONT_CLK_0;
+		if (((out << i) & 0x80) == 0)
+			FONT_SDA_0;
+		else
+			FONT_SDA_1;
+		FONT_CLK_1;
+	}
+}
 
 /*******************************************************************************/
 // Get data sub-pro (STM8,STM32等双向口)    SPI接收点阵数据的算法               /
 /*******************************************************************************/
-unsigned char Get_Byte(void)         
+unsigned char Get_Byte(void)
 {
 	unsigned char i;
-	unsigned char read_dat= 0,MISO = 0;
+	unsigned char read_dat = 0, MISO = 0;
 
 	FONT_CLK_1;
-	for(i=0;i<8;i++)
+	for (i = 0; i < 8; i++)
 	{
 		FONT_CLK_0;
-        MISO = FONT_RD_SDA;
-		read_dat = read_dat<<1;
-		if(MISO)				
-			read_dat|=0x01;
+		MISO = FONT_RD_SDA;
+		read_dat = read_dat << 1;
+		if (MISO)
+			read_dat |= 0x01;
 		else
-			read_dat&=0xfe;
+			read_dat &= 0xfe;
 		FONT_CLK_1;
 	}
-	return(read_dat);
+	return (read_dat);
 }
 
-
-
-
-
-unsigned char gt_read_data(unsigned char* sendbuf , unsigned char sendlen , unsigned char* receivebuf, unsigned int receivelen)
+unsigned char gt_read_data(unsigned char *sendbuf, unsigned char sendlen, unsigned char *receivebuf, unsigned int receivelen)
 {
 
-    unsigned int i;
-    FONT_CS_LOW;
-    for(i = 0; i < sendlen;i++)
-    {
-        Send_Byte(sendbuf[i] );
-    }
-    for(i = 0; i < receivelen;i++)
-    {
-        receivebuf[i] = Get_Byte();
-    }
-
-    FONT_CS_HIGHT;
-    return 1;
-}
-
+	unsigned int i;
+	FONT_CS_LOW;
+	for (i = 0; i < sendlen; i++)
+	{
+		Send_Byte(sendbuf[i]);
+	}
+	for (i = 0; i < receivelen; i++)
+	{
+		receivebuf[i] = Get_Byte();
+	}
 
+	FONT_CS_HIGHT;
+	return 1;
+}
 
 /*******************************************************************************/
 //                    Send address sub-pro (STM8,STM32,51)                                     /
 /*******************************************************************************/
-void SPI_Address(unsigned char AddH,unsigned char AddM,unsigned char AddL) 
+void SPI_Address(unsigned char AddH, unsigned char AddM, unsigned char AddL)
 {
 	Send_Byte(AddH);
 	Send_Byte(AddM);
@@ -162,39 +150,40 @@ void SPI_Address(unsigned char AddH,unsigned char AddM,unsigned char AddL)
 /*******************************************************************************/
 //                    Get N bytes sub-pro  (STM8,STM32,51)                                    //
 /*******************************************************************************/
-//客户自己实现,从address地址读取len个字节的数据并存入到DZ_Data数组当中
-unsigned char r_dat_bat(unsigned long address,unsigned long DataLen,unsigned char *pBuff)
+// 客户自己实现,从address地址读取len个字节的数据并存入到DZ_Data数组当中
+unsigned char r_dat_bat(unsigned long address, unsigned long DataLen, unsigned char *pBuff)
 {
-    unsigned long i;
-    unsigned char addrHigh;
-    unsigned char addrMid;
-    unsigned char addrLow;
-    addrHigh=address>>16;
-    addrMid=address>>8;
-    addrLow=(unsigned char)address;
-  
-	FONT_CS_LOW;          //片选选中字库芯片
-	Send_Byte(0x03);	//普通读取首先送0X03,然后发送地址高八位addrHigh,中八位addrMid,低八位addrLow。
-	SPI_Address(addrHigh,addrMid,addrLow);	
-	for(i=0;i<DataLen;i++)
-	*(pBuff+i)=Get_Byte();
+	unsigned long i;
+	unsigned char addrHigh;
+	unsigned char addrMid;
+	unsigned char addrLow;
+	addrHigh = address >> 16;
+	addrMid = address >> 8;
+	addrLow = (unsigned char)address;
+
+	FONT_CS_LOW;	 // 片选选中字库芯片
+	Send_Byte(0x03); // 普通读取首先送0X03,然后发送地址高八位addrHigh,中八位addrMid,低八位addrLow。
+	SPI_Address(addrHigh, addrMid, addrLow);
+	for (i = 0; i < DataLen; i++)
+		*(pBuff + i) = Get_Byte();
 	FONT_CS_HIGHT;
 	return 0;
 }
 
-//客户自己实现,从address地址读取一个字节的数据并返回该数据
-unsigned char r_dat(unsigned long address){
+// 客户自己实现,从address地址读取一个字节的数据并返回该数据
+unsigned char r_dat(unsigned long address)
+{
 	unsigned char buff;
 	unsigned char addrHigh;
 	unsigned char addrMid;
 	unsigned char addrLow;
-	addrHigh=address>>16;
-	addrMid=address>>8;
-	addrLow=(unsigned char)address;
-	
+	addrHigh = address >> 16;
+	addrMid = address >> 8;
+	addrLow = (unsigned char)address;
+
 	FONT_CS_LOW;
 	Send_Byte(0x03);
-	SPI_Address(addrHigh,addrMid,addrLow);
+	SPI_Address(addrHigh, addrMid, addrLow);
 	buff = Get_Byte();
 	FONT_CS_HIGHT;
 	return buff;
@@ -205,23 +194,22 @@ unsigned char r_dat(unsigned long address){
 根据说明文件或头文件是否需要, 没有就不需要实现
 ******************************************************/
 unsigned char CheckID(unsigned char CMD, unsigned long address,
-	unsigned long byte_long,unsigned char *p_arr)
+					  unsigned long byte_long, unsigned char *p_arr)
 {
 	unsigned long j;
 	FONT_CS_LOW;
 	Send_Byte(CMD);
-	Send_Byte((unsigned char)((address)>>16)); 
-	Send_Byte((unsigned char)((address)>>8));   
-	Send_Byte((unsigned char)address); 
-	for(j=0;j<byte_long;j++)
+	Send_Byte((unsigned char)((address) >> 16));
+	Send_Byte((unsigned char)((address) >> 8));
+	Send_Byte((unsigned char)address);
+	for (j = 0; j < byte_long; j++)
 	{
-			p_arr[j]=Get_Byte();
+		p_arr[j] = Get_Byte();
 	}
 	FONT_CS_HIGHT;
 	return 1;
 }
 
-
 void font_into_sleep()
 {
 
@@ -229,11 +217,9 @@ void font_into_sleep()
 	FONT_CS_LOW;
 	Send_Byte(0xB9);
 	FONT_CS_HIGHT;
-	//return 1;
-
+	// return 1;
 }
 
-
 void font_exit_sleep()
 {
 
@@ -241,20 +227,18 @@ void font_exit_sleep()
 	FONT_CS_LOW;
 	Send_Byte(0xAB);
 	FONT_CS_HIGHT;
-
 }
 
-
 void font_soft_rst()
 {
-		FONT_CS_LOW;
-		Send_Byte(0x66);
-		FONT_CS_HIGHT;
-		task_delay_ms(10);
-		FONT_CS_LOW;
-		Send_Byte(0x99);
-		FONT_CS_HIGHT;
-		task_delay_ms(10);
+	FONT_CS_LOW;
+	Send_Byte(0x66);
+	FONT_CS_HIGHT;
+	task_delay_ms(10);
+	FONT_CS_LOW;
+	Send_Byte(0x99);
+	FONT_CS_HIGHT;
+	task_delay_ms(10);
 }
 // unsigned char test_ASCII_GetData(unsigned char asc,unsigned long ascii_kind,unsigned char *DZ_Data)
 // {
@@ -266,7 +250,6 @@ void font_soft_rst()
 // 	Send_Byte(0x78);
 //     FONT_CS_HIGHT;
 
-
 //     FONT_CS_LOW;
 // 	Send_Byte(0x56);
 //     FONT_CS_HIGHT;
@@ -281,4 +264,3 @@ void font_soft_rst()
 //     FONT_CS_HIGHT;
 
 // }
-

+ 32 - 45
components/LED/LED.c

@@ -2,16 +2,13 @@
 #include "LED.h"
 #include "esp_log.h"
 
-
 static const char *LOG_TAG = "LED";
 
-//uint8_t led[6]={0x04,0x20,0x08,0x10,0x02,0x40 };
-
-//uint8_t led[6]={0x20,0x02,0x04,0x08,0x10,0x40 };
-//dailiao baoyang tunxing fengcun  guzhang tingji 
-uint8_t led[6]={0x04,0x40,0x20,0x10,0x08,0x02 };
-
+// uint8_t led[6]={0x04,0x20,0x08,0x10,0x02,0x40 };
 
+// uint8_t led[6]={0x20,0x02,0x04,0x08,0x10,0x40 };
+// dailiao baoyang tunxing fengcun  guzhang tingji
+uint8_t led[6] = {0x04, 0x40, 0x20, 0x10, 0x08, 0x02};
 
 #include <stdio.h>
 
@@ -21,32 +18,25 @@ static void example_ledc_init(void)
 {
     // Prepare and then apply the LEDC PWM timer configuration
     ledc_timer_config_t ledc_timer = {
-        .speed_mode       = LEDC_MODE,
-        .timer_num        = LEDC_TIMER,
-        .duty_resolution  = LEDC_DUTY_RES,
-        .freq_hz          = LEDC_FREQUENCY,  // Set output frequency at 5 kHz
-        .clk_cfg          = LEDC_AUTO_CLK
-    };
+        .speed_mode = LEDC_MODE,
+        .timer_num = LEDC_TIMER,
+        .duty_resolution = LEDC_DUTY_RES,
+        .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
+        .clk_cfg = LEDC_AUTO_CLK};
     ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
 
     // Prepare and then apply the LEDC PWM channel configuration
     ledc_channel_config_t ledc_channel = {
-        .speed_mode     = LEDC_MODE,
-        .channel        = LEDC_CHANNEL,
-        .timer_sel      = LEDC_TIMER,
-        .intr_type      = LEDC_INTR_DISABLE,
-        .gpio_num       = LEDC_OUTPUT_IO,
-        .duty           = 0, // Set duty to 0%
-        .hpoint         = 0
-    };
+        .speed_mode = LEDC_MODE,
+        .channel = LEDC_CHANNEL,
+        .timer_sel = LEDC_TIMER,
+        .intr_type = LEDC_INTR_DISABLE,
+        .gpio_num = LEDC_OUTPUT_IO,
+        .duty = 0, // Set duty to 0%
+        .hpoint = 0};
     ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
 }
 
-
-
-
-
-
 void beep_init(void)
 {
 
@@ -58,31 +48,31 @@ void beep_init(void)
     ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
 }
 
-static void  _74HC595_WriteByte(unsigned char Byte)
+static void _74HC595_WriteByte(unsigned char Byte)
 {
 
     unsigned char i;
-    for(i=0;i<8;i++)
-    {   
+    for (i = 0; i < 8; i++)
+    {
         uint8_t bit = (Byte >> (i)) & 0x01;
-        gpio_set_level(LED_DATA_PIN,bit);
-        LED_LCLK_0 ;
-        LED_LCLK_1 ; 
+        gpio_set_level(LED_DATA_PIN, bit);
+        LED_LCLK_0;
+        LED_LCLK_1;
     }
-      LED_SCLK_0;  
-      LED_SCLK_1;    
+    LED_SCLK_0;
+    LED_SCLK_1;
 }
 
-
-void led_set(uint8_t led_index,uint8_t led_status)
+void led_set(uint8_t led_index, uint8_t led_status)
 {
 
-    ESP_LOGI(LOG_TAG,"led_set  %s",led_status ? "open":"close");
+    ESP_LOGI(LOG_TAG, "led_set  %s", led_status ? "open" : "close");
 
-    if(led_status)
+    if (led_status)
     {
         _74HC595_WriteByte(led[led_index]);
-    }else
+    }
+    else
     {
         _74HC595_WriteByte(0x00);
     }
@@ -96,7 +86,6 @@ void beep_open()
     ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
 }
 
-
 void beep_close()
 {
     ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
@@ -104,17 +93,15 @@ void beep_close()
     ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
 }
 
-
-void beep_blink(uint16_t ms,uint16_t count)
+void beep_blink(uint16_t ms, uint16_t count)
 {
 
-    for(int i =0;i<count;i++)
+    for (int i = 0; i < count; i++)
     {
-        //printf("beep\r\n");
+        // printf("beep\r\n");
         beep_open();
         vTaskDelay(ms / portTICK_PERIOD_MS);
         beep_close();
         vTaskDelay(ms / portTICK_PERIOD_MS);
     }
- 
 }

Fichier diff supprimé car celui-ci est trop grand
+ 1327 - 1588
components/LORA/LORA.c


+ 83 - 82
components/USER_SPIFFS/SPIFFS.c

@@ -12,101 +12,102 @@ static const char *TAG = "user_spiffs";
 
 #include "esp_attr.h"
 
-RTC_FAST_ATTR  Machine_info_t   Machine_info;
-RTC_FAST_ATTR Node *Send_list = NULL;  //发送数据链表
-
-
-
-
+RTC_FAST_ATTR Machine_info_t Machine_info;
+RTC_FAST_ATTR Node *Send_list = NULL; // 发送数据链表
 
 void spiffs_init(void)
 {
     esp_vfs_spiffs_conf_t conf = {
-      .base_path = "/spiffs",
-      .partition_label = NULL,
-      .max_files = 5,
-      .format_if_mount_failed = true
-    };
+        .base_path = "/spiffs",
+        .partition_label = NULL,
+        .max_files = 5,
+        .format_if_mount_failed = true};
 
     // Use settings defined above to initialize and mount SPIFFS filesystem.
     // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
     esp_err_t ret = esp_vfs_spiffs_register(&conf);
 
-    if (ret != ESP_OK) {
-        if (ret == ESP_FAIL) {
+    if (ret != ESP_OK)
+    {
+        if (ret == ESP_FAIL)
+        {
             ESP_LOGE(TAG, "Failed to mount or format filesystem");
-        } else if (ret == ESP_ERR_NOT_FOUND) {
+        }
+        else if (ret == ESP_ERR_NOT_FOUND)
+        {
             ESP_LOGE(TAG, "Failed to find SPIFFS partition");
-        } else {
+        }
+        else
+        {
             ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
         }
         return;
     }
 
-
     size_t total = 0, used = 0;
     ret = esp_spiffs_info(conf.partition_label, &total, &used);
-    if (ret != ESP_OK) {
+    if (ret != ESP_OK)
+    {
         ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
         esp_spiffs_format(conf.partition_label);
         return;
-    } else {
+    }
+    else
+    {
         ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
     }
 
-
     // Check consistency of reported partiton size info.
-    if (used > total) {
+    if (used > total)
+    {
         ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
         ret = esp_spiffs_check(conf.partition_label);
         // Could be also used to mend broken files, to clean unreferenced pages, etc.
         // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
-        if (ret != ESP_OK) {
+        if (ret != ESP_OK)
+        {
             ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
             return;
-        } else {
+        }
+        else
+        {
             ESP_LOGI(TAG, "SPIFFS_check() successful");
         }
     }
 }
 
-
-void spiffs_write(Machine_info_t* info)
+void spiffs_write(Machine_info_t *info)
 {
     ESP_LOGI(TAG, "File written");
-    FILE* f = fopen("/spiffs/yc_data.bin", "w");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/yc_data.bin", "w");
+    if (f == NULL)
+    {
         ESP_LOGE(TAG, "Failed to open file for writing");
         return;
     }
-    fwrite(info,sizeof(Machine_info_t),1,f);
+    fwrite(info, sizeof(Machine_info_t), 1, f);
     fclose(f);
 }
 
-
-
-void spiffs_read(Machine_info_t* info)
+void spiffs_read(Machine_info_t *info)
 {
     ESP_LOGI(TAG, "Reading file");
-    FILE* f = fopen("/spiffs/yc_data.bin", "r");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/yc_data.bin", "r");
+    if (f == NULL)
+    {
         ESP_LOGE(TAG, "Failed to open file for reading");
         return;
     }
-    fread(info,sizeof(Machine_info_t),1,f);
+    fread(info, sizeof(Machine_info_t), 1, f);
     fclose(f);
 }
 
-
-
-
-
-
-void left_spiffs_write(uint8_t *buffer,unsigned int size)
+void left_spiffs_write(uint8_t *buffer, unsigned int size)
 {
     // ESP_LOGI(TAG, "left File written");
-    FILE* f = fopen("/spiffs/left_display.bin", "w");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/left_display.bin", "w");
+    if (f == NULL)
+    {
         printf("Failed to open file for writing");
         return;
     }
@@ -114,11 +115,13 @@ void left_spiffs_write(uint8_t *buffer,unsigned int size)
     size_t bytes_written = 0;
     size_t bytes_to_write = size;
 
-    while (bytes_written < size) {
+    while (bytes_written < size)
+    {
         size_t write_result = fwrite(buffer + bytes_written, 1, bytes_to_write, f);
 
-        if (write_result < 0) {
-           printf("Error writing to file: left_display.bin\r\n");
+        if (write_result < 0)
+        {
+            printf("Error writing to file: left_display.bin\r\n");
             fclose(f);
             return;
         }
@@ -127,18 +130,15 @@ void left_spiffs_write(uint8_t *buffer,unsigned int size)
         bytes_to_write -= write_result;
     }
 
-
-
     fclose(f);
 }
 
-
-
-void left_spiffs_read(uint8_t *buffer, unsigned int  size)
+void left_spiffs_read(uint8_t *buffer, unsigned int size)
 {
     ESP_LOGI(TAG, "Reading left file");
-    FILE* f = fopen("/spiffs/left_display.bin", "r");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/left_display.bin", "r");
+    if (f == NULL)
+    {
         printf("Failed to open file for reading");
         return;
     }
@@ -146,11 +146,13 @@ void left_spiffs_read(uint8_t *buffer, unsigned int  size)
     size_t bytes_read = 0;
     size_t bytes_to_read = size;
 
-    while (bytes_read < size) {
+    while (bytes_read < size)
+    {
         size_t read_result = fread(buffer + bytes_read, 1, bytes_to_read, f);
 
-        if (read_result < 0) {
-            printf( "Error reading from file: left_display.bin\r\n");
+        if (read_result < 0)
+        {
+            printf("Error reading from file: left_display.bin\r\n");
             fclose(f);
             return;
         }
@@ -158,7 +160,8 @@ void left_spiffs_read(uint8_t *buffer, unsigned int  size)
         bytes_read += read_result;
         bytes_to_read -= read_result;
 
-        if (read_result == 0) {
+        if (read_result == 0)
+        {
             // 文件已经读取完毕
             break;
         }
@@ -166,16 +169,12 @@ void left_spiffs_read(uint8_t *buffer, unsigned int  size)
     fclose(f);
 }
 
-
-
-
-
-
-void right_spiffs_write(uint8_t *buffer,unsigned int  size)
+void right_spiffs_write(uint8_t *buffer, unsigned int size)
 {
     ESP_LOGI(TAG, "right File written");
-    FILE* f = fopen("/spiffs/right_display.bin", "w");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/right_display.bin", "w");
+    if (f == NULL)
+    {
         printf("Failed to open file for writing\r\n");
         return;
     }
@@ -183,11 +182,13 @@ void right_spiffs_write(uint8_t *buffer,unsigned int  size)
     size_t bytes_written = 0;
     size_t bytes_to_write = size;
 
-    while (bytes_written < size) {
+    while (bytes_written < size)
+    {
         size_t write_result = fwrite(buffer + bytes_written, 1, bytes_to_write, f);
 
-        if (write_result < 0) {
-           printf("Error writing to file: left_display.bin\r\n");
+        if (write_result < 0)
+        {
+            printf("Error writing to file: left_display.bin\r\n");
             fclose(f);
             return;
         }
@@ -195,29 +196,29 @@ void right_spiffs_write(uint8_t *buffer,unsigned int  size)
         bytes_written += write_result;
         bytes_to_write -= write_result;
     }
-    
 
     fclose(f);
 }
 
-
-
-void right_spiffs_read(uint8_t *buffer,unsigned int  size)
+void right_spiffs_read(uint8_t *buffer, unsigned int size)
 {
     ESP_LOGI(TAG, "Reading right file");
-    FILE* f = fopen("/spiffs/right_display.bin", "r");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/right_display.bin", "r");
+    if (f == NULL)
+    {
         printf("Failed to open file for reading");
         return;
     }
     size_t bytes_read = 0;
     size_t bytes_to_read = size;
 
-    while (bytes_read < size) {
+    while (bytes_read < size)
+    {
         size_t read_result = fread(buffer + bytes_read, 1, bytes_to_read, f);
 
-        if (read_result < 0) {
-            printf( "Error reading from file: right_display.bin\r\n");
+        if (read_result < 0)
+        {
+            printf("Error reading from file: right_display.bin\r\n");
             fclose(f);
             return;
         }
@@ -225,7 +226,8 @@ void right_spiffs_read(uint8_t *buffer,unsigned int  size)
         bytes_read += read_result;
         bytes_to_read -= read_result;
 
-        if (read_result == 0) {
+        if (read_result == 0)
+        {
             // 文件已经读取完毕
             break;
         }
@@ -233,18 +235,17 @@ void right_spiffs_read(uint8_t *buffer,unsigned int  size)
     fclose(f);
 }
 
-
-
-size_t spiffs_read_powerOn(Machine_info_t* info)
+size_t spiffs_read_powerOn(Machine_info_t *info)
 {
     size_t ret;
     ESP_LOGI(TAG, "Reading powerOn file");
-    FILE* f = fopen("/spiffs/yc_data.bin", "r");
-    if (f == NULL) {
+    FILE *f = fopen("/spiffs/yc_data.bin", "r");
+    if (f == NULL)
+    {
         printf("Failed to open file for reading");
         return 0;
     }
-    ret = fread(info,sizeof(Machine_info_t),1,f);//返回的不一定是读取的字节数
+    ret = fread(info, sizeof(Machine_info_t), 1, f); // 返回的不一定是读取的字节数
     fclose(f);
     // ESP_LOGW(TAG,"ret  = %d",ret);
     // ESP_LOG_BUFFER_HEX(TAG,info,sizeof(Machine_info_t));

+ 65 - 85
components/USER_SPIFFS/include/SPIFFS.h

@@ -4,40 +4,37 @@
 #include "list.h"
 #include "stdbool.h"
 
-#define PERSON_MAX_NAME     10
-#define PERSON_NAME_MAX_LEN  10
+#define PERSON_MAX_NAME 10
+#define PERSON_NAME_MAX_LEN 10
 
 typedef enum
 {
-      Administrator                     = 0,               //管理员
-      product_person                    = 1,               //生产责任人
-      repair_person                     = 2,               //维修责任人
-      Maintenance_person                = 3,               //保养责任人
-      check_person                      = 4,               //巡检责任人
+    Administrator = 0,      // 管理员
+    product_person = 1,     // 生产责任人
+    repair_person = 2,      // 维修责任人
+    Maintenance_person = 3, // 保养责任人
+    check_person = 4,       // 巡检责任人
 
-}Person_type_t;
+} Person_type_t;
 
 typedef struct
 {
-    uint16_t   person_num;  //序号
-    uint8_t person_name[PERSON_NAME_MAX_LEN]; //人员名称
-}Person_Name_t;
+    uint16_t person_num;                      // 序号
+    uint8_t person_name[PERSON_NAME_MAX_LEN]; // 人员名称
+} Person_Name_t;
 ///////////////////////////////////////////
 typedef struct
 {
-    uint8_t    person_type;  //人员类型
-    Person_Name_t  person_name[PERSON_MAX_NAME];
+    uint8_t person_type; // 人员类型
+    Person_Name_t person_name[PERSON_MAX_NAME];
     char string_name[100];
-    bool Charge_close;//为真关闭责任人显示
-    uint8_t other_name[16];//右屏第二部分责任人显示
-}Person_t;
-
-
-
+    bool Charge_close;      // 为真关闭责任人显示
+    uint8_t other_name[16]; // 右屏第二部分责任人显示
+} Person_t;
 
 typedef struct
 {
-    uint8_t button_info;  //当前按键状态
+    uint8_t button_info; // 当前按键状态
 
     uint16_t Year;
     uint8_t Month;
@@ -45,22 +42,21 @@ typedef struct
     uint8_t Hour;
     uint8_t Minute;
     uint8_t Second;
-    
-    uint32_t time_min;
-}Button_Time_t;
 
+    uint32_t time_min;
+} Button_Time_t;
 
 typedef struct
 {
-    bool checkIn_close;//为真关闭打卡
-    uint32_t number;//只加不减,用于左屏图标数据
-    uint8_t real_number;//真实签到人数,用于右屏
-    uint8_t other_name[10];//四类打卡类型   限制四个字
-}CheckIn_Setting_t;
+    bool checkIn_close;     // 为真关闭打卡
+    uint32_t number;        // 只加不减,用于左屏图标数据
+    uint8_t real_number;    // 真实签到人数,用于右屏
+    uint8_t other_name[10]; // 四类打卡类型   限制四个字
+} CheckIn_Setting_t;
 
 typedef struct
-{    
-    //所有要保存的数据
+{
+    // 所有要保存的数据
 
     char terminal_name[20];
     char terminal_number[20];
@@ -73,37 +69,33 @@ typedef struct
     char btn_shutDown_info[6];
     char btn_safeKeep_info[6];
     char btn_waitMaterials_info[6];
-    //保存按键信息以及是否按键显示显示
-    //0表示关闭,1表示开启
+    // 保存按键信息以及是否按键显示显示
+    // 0表示关闭,1表示开启
     bool btn_dis_flag[6];
 
     uint8_t lora_factory_channel;
     uint8_t lora_new_channel;
-    uint8_t eflagID;         //本机组内编号
-    unsigned char cid[20];   //设备ID  CID flash中获取的ID
-    uint8_t paired;          //是否配网
-    uint8_t power_status;    //当前系统的状态  开机或者关机
+    uint8_t eflagID;       // 本机组内编号
+    unsigned char cid[20]; // 设备ID  CID flash中获取的ID
+    uint8_t paired;        // 是否配网
+    uint8_t power_status;  // 当前系统的状态  开机或者关机
     // uint8_t is_setting;      //设置当前是否为设置模式
 
-
     Node *Send_list;
-    uint16_t msg_id;        //添加消息发送设备ID 唯一性分配
-
-    Button_Time_t  last_button;         //上次按键状态
-    Button_Time_t  current_button;      //当前按键状态
-    uint32_t Duration_time;             //持续的时间
+    uint16_t msg_id; // 添加消息发送设备ID 唯一性分配
 
-    uint8_t left_max_Quick_refresh_time;     //最大快刷次数
-    uint8_t left_current_Quick_refresh_time; //当前已经快刷的次数  当前快刷的次数大于设置 慢刷一次
+    Button_Time_t last_button;    // 上次按键状态
+    Button_Time_t current_button; // 当前按键状态
+    uint32_t Duration_time;       // 持续的时间
 
-    uint8_t right_max_Quick_refresh_time;     //最大快刷次数
-    uint8_t right_current_Quick_refresh_time; //当前已经快刷的次数  当前快刷的次数大于设置 慢刷一次
+    uint8_t left_max_Quick_refresh_time;     // 最大快刷次数
+    uint8_t left_current_Quick_refresh_time; // 当前已经快刷的次数  当前快刷的次数大于设置 慢刷一次
 
+    uint8_t right_max_Quick_refresh_time;     // 最大快刷次数
+    uint8_t right_current_Quick_refresh_time; // 当前已经快刷的次数  当前快刷的次数大于设置 慢刷一次
 
-
-    
-    int batt_precent;       //当前显示的电量
-    int last_batt_precent;  //记录上次显示的电量
+    int batt_precent;      // 当前显示的电量
+    int last_batt_precent; // 记录上次显示的电量
 
     uint16_t year;
     uint8_t month;
@@ -114,31 +106,28 @@ typedef struct
 
     uint8_t rssi;
 
-    uint8_t left_state;//左屏的模式  --> Machine_state_t
-    uint8_t left_display_mode;//cmd 0x07 
-    uint8_t right_display_mode;//cmd 0x08
+    uint8_t left_state;         // 左屏的模式  --> Machine_state_t
+    uint8_t left_display_mode;  // cmd 0x07
+    uint8_t right_display_mode; // cmd 0x08
 
 #if 0
     uint8_t personnel_check_in[4];//存签到人数。
     uint8_t person_in_charge_name[4][12];//责任人名称
 #else
-    CheckIn_Setting_t checkIn_set[4];//签到相关
+    CheckIn_Setting_t checkIn_set[4]; // 签到相关
 #endif
 
     uint8_t mac_addr[6];
     // uint8_t p_name[32];//未使用
     uint8_t refresh_cycle;
-  
-    uint8_t announcement[120];  //公告
-    uint8_t systemMessage[120];  //系统消息
-    Person_t  person[5];//
-
-    uint8_t gateway_mac[6];  //网关的mac地址
 
+    uint8_t announcement[120];  // 公告
+    uint8_t systemMessage[120]; // 系统消息
+    Person_t person[5];         //
 
-  
+    uint8_t gateway_mac[6]; // 网关的mac地址
 
-    //保存右屏图表信息
+    // 保存右屏图表信息
     uint32_t num_goodProducts[8];
     uint32_t num_badProducts[8];
 
@@ -157,37 +146,28 @@ typedef struct
 
     uint8_t wait_send_rssi_bat;
 
-    bool is_charge;      //判断是否充电
-    bool is_charge_full; //判断是否充满
+    bool is_charge;      // 判断是否充电
+    bool is_charge_full; // 判断是否充满
 
-    char timestamp[20];   //当前时间戳
-    int time_offset;      //时区偏移
+    char timestamp[20]; // 当前时间戳
+    int time_offset;    // 时区偏移
 
-}Machine_info_t;//所有要保存的数据
+} Machine_info_t; // 所有要保存的数据
 
 extern Machine_info_t Machine_info;
-extern  Node *Send_list;  //发送数据链表
-
+extern Node *Send_list; // 发送数据链表
 
 void spiffs_init(void);
-void spiffs_write(Machine_info_t* info);
-void spiffs_read(Machine_info_t* info);
-
-
-
-
-void left_spiffs_write(uint8_t *buffer,unsigned int  size);
-void left_spiffs_read(uint8_t *buffer,unsigned int  size);
-
-
-void right_spiffs_write(uint8_t *buffer,unsigned int  size);
-
-void right_spiffs_read(uint8_t *buffer,unsigned int  size);
-
+void spiffs_write(Machine_info_t *info);
+void spiffs_read(Machine_info_t *info);
 
-size_t spiffs_read_powerOn(Machine_info_t* info);
+void left_spiffs_write(uint8_t *buffer, unsigned int size);
+void left_spiffs_read(uint8_t *buffer, unsigned int size);
 
+void right_spiffs_write(uint8_t *buffer, unsigned int size);
 
+void right_spiffs_read(uint8_t *buffer, unsigned int size);
 
+size_t spiffs_read_powerOn(Machine_info_t *info);
 
-#endif/*_SPIFFS_H_*/
+#endif /*_SPIFFS_H_*/

+ 213 - 297
components/button/user_button.c

@@ -9,7 +9,6 @@
 #include "freertos/semphr.h"
 #include "freertos/timers.h"
 
-
 #include "../EPD//include/EPD.h"
 #include <stdlib.h>
 #include <time.h>
@@ -18,107 +17,84 @@
 
 static const char *LOG_TAG = "user button";
 
-static struct timeval btn_last_time;//左屏超过30s,不发送按键lora信息
+static struct timeval btn_last_time; // 左屏超过30s,不发送按键lora信息
 
 extern QueueHandle_t button_Data_queue;
 extern SemaphoreHandle_t button_semaphore;
 
-
 static button_handle_t user_button_handle[KEY_NUM] = {0};
 
 static button_handle_t user_powerbutton_handle[3] = {0};
 TimerHandle_t btn_timer_handle;
 RTC_FAST_ATTR static bool is_setting = false;
-RTC_FAST_ATTR static bool is_reset_net = false;//配网
-RTC_FAST_ATTR static bool is_send_lora  = false;
+RTC_FAST_ATTR static bool is_reset_net = false; // 配网
+RTC_FAST_ATTR static bool is_send_lora = false;
 
-uint16_t adc_value[][2]=
-{
-    {BAOYANG_MIN_ADC,BAOYANG_MAX_ADC},
-    {FENGCUN_MIN_ADC,FENGCUN_MAX_ADC},
-    {GUZHUANG_MIN_ADC,GUZHANG_MAX_ADC},
-    {DAILIAO_MIN_ADC,DAILIAO_MAX_ADC},
-    {TINGJI_MIN_ADC,TINGJI_MAX_ADC},
-    {YUNXING_MIN_ADC,YUNXING_MAX_ADC},
+uint16_t adc_value[][2] =
+    {
+        {BAOYANG_MIN_ADC, BAOYANG_MAX_ADC},
+        {FENGCUN_MIN_ADC, FENGCUN_MAX_ADC},
+        {GUZHUANG_MIN_ADC, GUZHANG_MAX_ADC},
+        {DAILIAO_MIN_ADC, DAILIAO_MAX_ADC},
+        {TINGJI_MIN_ADC, TINGJI_MAX_ADC},
+        {YUNXING_MIN_ADC, YUNXING_MAX_ADC},
 };
 
+static void press_up_cb(void *arg, void *usr_data);
+static void long_press_up_cb(void *arg, void *usr_data);
 
-static void press_up_cb(void *arg,void *usr_data);
-static void long_press_up_cb(void *arg,void *usr_data);
-
-
-
-uint16_t power_adc_value[][2]=
-{
-    {0,100},
-    {1000,1900},
-    {2000,3063},
+uint16_t power_adc_value[][2] =
+    {
+        {0, 100},
+        {1000, 1900},
+        {2000, 3063},
 };
 
-
-int  find_key_value(int value)
+int find_key_value(int value)
 {
     int key = 0xff;
 
-    for(int i  =0 ;i<6;i++)
+    for (int i = 0; i < 6; i++)
     {
-        if( 
-            (adc_value[i][1]>=value)&&
-            (adc_value[i][0]<=value)
-          )
-          {
+        if (
+            (adc_value[i][1] >= value) &&
+            (adc_value[i][0] <= value))
+        {
 
             key = i;
-            printf("key vaule %d\r\n",i);
-             
-               break;
-          }
+            printf("key vaule %d\r\n", i);
 
+            break;
+        }
     }
 
-
-      return key;
-
+    return key;
 }
 
+static void user_button_single_click_cb(void *arg, void *usr_data);
 
+static void power_long_press_start_cb(void *arg, void *usr_data);
+static void power_long_press_hold_cb(void *arg, void *usr_data);
+static void power_long_press_up_cb(void *arg, void *usr_data);
 
+static void power_single_press_cb(void *arg, void *usr_data);
 
+static void user_button_single_UP_cb(void *arg, void *usr_data);
 
+static void power_button_multi_press_cb(void *arg, void *usr_data);
 
-static void user_button_single_click_cb(void *arg,void *usr_data);
-
-static void power_long_press_start_cb(void *arg,void *usr_data);
-static void power_long_press_hold_cb(void *arg,void *usr_data);
-static void power_long_press_up_cb(void *arg,void *usr_data);
-
-
-static void power_single_press_cb(void *arg,void *usr_data);
-
-
-static void user_button_single_UP_cb(void *arg,void *usr_data);
-
-
-
-static void power_button_multi_press_cb(void *arg,void *usr_data);
-
-
-
-static void power_left_single_press_cb(void *arg,void *usr_data)
+static void power_left_single_press_cb(void *arg, void *usr_data)
 {
-    printf("%s\r\n",__FUNCTION__);
+    printf("%s\r\n", __FUNCTION__);
 }
 
-static void power_right_single_press_cb(void *arg,void *usr_data)
+static void power_right_single_press_cb(void *arg, void *usr_data)
 {
-    printf("%s\r\n",__FUNCTION__);
+    printf("%s\r\n", __FUNCTION__);
 }
 
-
 int button_idx = 0;
 
-
-
 int power_button_idx = 0;
 
 int power_button_mult_time = 0;
@@ -126,20 +102,19 @@ int power_button_mult_time = 0;
 void btn_timer_Callback(TimerHandle_t xTimer)
 {
     ESP_LOGW(LOG_TAG, "btn_timer_Callback");
-    if(is_setting)
+    if (is_setting)
     {
-        is_setting = false; 
+        is_setting = false;
     }
     else if (is_reset_net)
     {
-        is_reset_net = false; 
+        is_reset_net = false;
     }
-    
 
     is_setting = 0;
-    //xTimerStop(btn_timer_handle,0);
+    // xTimerStop(btn_timer_handle,0);
 }
-static void get_rtc_time(struct tm* tmp)
+static void get_rtc_time(struct tm *tmp)
 {
     struct timeval tv;
     gettimeofday(&tv, NULL);
@@ -159,17 +134,17 @@ bool is_btn_timeout(void)
 {
     struct timeval now_time;
     gettimeofday(&now_time, NULL);
-    ESP_LOGD(LOG_TAG,"is_send_lora= %s now_time =  %lld,last =  %lld,\n",is_send_lora?"ture":"false",now_time.tv_sec, btn_last_time.tv_sec);
+    ESP_LOGD(LOG_TAG, "is_send_lora= %s now_time =  %lld,last =  %lld,\n", is_send_lora ? "ture" : "false", now_time.tv_sec, btn_last_time.tv_sec);
 
-    struct tm* tmp = localtime(&now_time.tv_sec);
+    struct tm *tmp = localtime(&now_time.tv_sec);
     // ESP_LOGI(LOG_TAG,"时间: %04d-%02d-%02d %02d:%02d:%02d\n",
     // tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
     // tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
-    if(((now_time.tv_sec - btn_last_time.tv_sec) > 30)&&(is_send_lora))
+    if (((now_time.tv_sec - btn_last_time.tv_sec) > 30) && (is_send_lora))
     {
-        ESP_LOGW(LOG_TAG,"发送-----现在按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
-        tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
-        tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+        ESP_LOGW(LOG_TAG, "发送-----现在按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
+                 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
+                 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
         is_send_lora = false;
         return true;
     }
@@ -177,62 +152,59 @@ bool is_btn_timeout(void)
 }
 void reset_btn_last_time(void)
 {
-    if(!is_send_lora)
+    if (!is_send_lora)
     {
         is_send_lora = true;
     }
     gettimeofday(&btn_last_time, NULL);
-    struct tm* tmp = localtime(&btn_last_time.tv_sec);
+    struct tm *tmp = localtime(&btn_last_time.tv_sec);
     // printf("btn_last_time: %lld\n", btn_last_time.tv_sec);
-    ESP_LOGW(LOG_TAG,"最后按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
-        tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
-        tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+    ESP_LOGW(LOG_TAG, "最后按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
+             tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
+             tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 }
 void button_init(adc_oneshot_unit_handle_t adc_handle)
 {
     int i;
     printf("button_init\n");
 
-    #if 1
-	button_config_t adc_btn_cfg = {
-	    .type = BUTTON_TYPE_ADC,
-	    .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
-	    .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
-	    .adc_button_config = {
-	        .adc_channel = 7,
+#if 1
+    button_config_t adc_btn_cfg = {
+        .type = BUTTON_TYPE_ADC,
+        .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
+        .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
+        .adc_button_config = {
+            .adc_channel = 7,
             .adc_handle = &adc_handle,
-	    },
-	};
-    //左边6个按键初始化
-    for(i = 0;i < KEY_NUM ;i++)
+        },
+    };
+    // 左边6个按键初始化
+    for (i = 0; i < KEY_NUM; i++)
     {
         adc_btn_cfg.adc_button_config.button_index = i;
-        adc_btn_cfg.adc_button_config.min = adc_value[i][0] ;
-        adc_btn_cfg.adc_button_config.max = adc_value[i][1] ;
+        adc_btn_cfg.adc_button_config.min = adc_value[i][0];
+        adc_btn_cfg.adc_button_config.max = adc_value[i][1];
         user_button_handle[i] = iot_button_create(&adc_btn_cfg);
-        if(NULL == user_button_handle[i]) {
-            printf( "Button create failed");
+        if (NULL == user_button_handle[i])
+        {
+            printf("Button create failed");
         }
         button_idx = i;
         // iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_DOWN, user_button_single_click_cb,button_idx);
-        iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb,button_idx);//松手才发按键消息
-        
+        iot_button_register_cb(user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb, button_idx); // 松手才发按键消息
+
         // iot_button_register_cb( user_button_handle[i], BUTTON_LONG_PRESS_UP, long_press_up_cb,button_idx);
-        //iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb,button_idx);
+        // iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb,button_idx);
     }
 
-    #endif
-//电源按键
+#endif
+    // 电源按键
     btn_timer_handle = xTimerCreate(
-    "btn_timer_handle",             
-    pdMS_TO_TICKS(10*1000),    
-    pdFALSE,                
-    0,                     
-    btn_timer_Callback         
-    );
-
-
-
+        "btn_timer_handle",
+        pdMS_TO_TICKS(10 * 1000),
+        pdFALSE,
+        0,
+        btn_timer_Callback);
 
 #if 0
     //右边3个按键  目前只是用一个按键 POWER
@@ -258,13 +230,13 @@ void button_init(adc_oneshot_unit_handle_t adc_handle)
         button_idx = i;
         
         iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
-        #if 0
+#if 0
         iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
-        #else
+#else
         iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
         iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
         iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
-        #endif
+#endif
 
 
 
@@ -292,112 +264,83 @@ void button_init(adc_oneshot_unit_handle_t adc_handle)
     }
 #else
 
-
-
-  button_config_t power_btn_cfg = {
-            .type = BUTTON_TYPE_GPIO,
-            .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
-            .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
-            .gpio_button_config = {
-                .gpio_num = 4,
-                .active_level = 0,
-            },
-        };
-
-
-
-
-
-
-        for( i = 0;i < 1 ;i++)
+    button_config_t power_btn_cfg = {
+        .type = BUTTON_TYPE_GPIO,
+        .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
+        .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
+        .gpio_button_config = {
+            .gpio_num = 4,
+            .active_level = 0,
+        },
+    };
+
+    for (i = 0; i < 1; i++)
     {
-       //power_btn_cfg.adc_button_config.button_index = i;
-       //power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
-       //power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
-
+        // power_btn_cfg.adc_button_config.button_index = i;
+        // power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
+        // power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
 
         user_powerbutton_handle[i] = iot_button_create(&power_btn_cfg);
-        if(NULL == user_powerbutton_handle[i]) {
-            printf( "Button create failed");
+        if (NULL == user_powerbutton_handle[i])
+        {
+            printf("Button create failed");
         }
         button_idx = i;
-        
-        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
-        #if 0
-        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
-        #else
-        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
-        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
-        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
-        #endif
-
-
-
-
 
+        iot_button_register_cb(user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb, button_idx);
+#if 0
+        iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
+#else
+        iot_button_register_cb(user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb, button_idx);
+        iot_button_register_cb(user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb, button_idx);
+        iot_button_register_cb(user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb, button_idx);
+#endif
 
-        //注册按键多次按键回调
+        // 注册按键多次按键回调
         button_event_config_t btn_cfg;
         btn_cfg.event = BUTTON_MULTIPLE_CLICK;
         // btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_OTA;
         // power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
         // iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
         btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_RESET;
-        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
+        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks;
         iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
 
-
         btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_DIS_RIGHT;
-        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
+        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks;
         iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
 
         btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_BLE_OTA_MODE;
-        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
-        iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);            
-
+        power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks;
+        iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
 
         // //注册按键长按按键回调
         // button_event_config_t btn_long_press_cfg;
         // btn_cfg.event = BUTTON_LONG_PRESS_START;
     }
 
-
-
-
-
 #endif
-
-
-
 }
 void button_deinit(void)
 {
     printf("button_deinit\n");
-    for(int i=0;i<KEY_NUM;i++)
+    for (int i = 0; i < KEY_NUM; i++)
     {
         iot_button_delete(user_button_handle[i]);
     }
-    //iot_button_delete(user_powerbutton_handle[0]);
+    // iot_button_delete(user_powerbutton_handle[0]);
 }
 
-
 void power_button_init(adc_oneshot_unit_handle_t adc_handle)
 {
-
-
 }
 
-
-
-
-
 void power_button_deinit(void)
 {
-     for(int i = 0;i < 3;i++)
-     iot_button_delete(user_powerbutton_handle[i]);
+    for (int i = 0; i < 3; i++)
+        iot_button_delete(user_powerbutton_handle[i]);
 }
 
-
 uint8_t button_info = 0;
 
 #include "SPIFFS.h"
@@ -422,154 +365,147 @@ static void power_long_press_cb(void *arg,void *usr_data)
 }
 iot_button_get_long_press_hold_cnt((button_handle_t)arg)
 #else
-static void press_up_cb(void *arg,void *usr_data)
+static void press_up_cb(void *arg, void *usr_data)
 {
-    ESP_LOGE(LOG_TAG,"BUTTON_PRESS_UP %d",(int)usr_data);
+    ESP_LOGE(LOG_TAG, "BUTTON_PRESS_UP %d", (int)usr_data);
 }
-static void long_press_up_cb(void *arg,void *usr_data)
+static void long_press_up_cb(void *arg, void *usr_data)
 {
-    ESP_LOGE(LOG_TAG,"BUTTON_LONG_PRESS_UP %d",(int)usr_data);
+    ESP_LOGE(LOG_TAG, "BUTTON_LONG_PRESS_UP %d", (int)usr_data);
 }
-static void power_long_press_start_cb(void *arg,void *usr_data)
+static void power_long_press_start_cb(void *arg, void *usr_data)
 {
     // printf("power_long_press_start_cb\n");
 }
-static void power_long_press_hold_cb(void *arg,void *usr_data)
+static void power_long_press_hold_cb(void *arg, void *usr_data)
 {
-    //设置的长按开始时间后开始计算hold_cnt  长按开始时间设置为1s,cnt计算时间间隔(SERIAL_TICKS)也设置为1s
+    // 设置的长按开始时间后开始计算hold_cnt  长按开始时间设置为1s,cnt计算时间间隔(SERIAL_TICKS)也设置为1s
     uint16_t hold_cnt = iot_button_get_long_press_hold_cnt((button_handle_t)arg);
-    ESP_LOGE(LOG_TAG," BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
+    ESP_LOGE(LOG_TAG, " BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
 
     switch ((int)hold_cnt)
     {
-    case 2://设置模式,长按3s进入
-        if(is_setting && is_reset_net)
+    case 2: // 设置模式,长按3s进入
+        if (is_setting && is_reset_net)
         {
-            ESP_LOGE(LOG_TAG,"err:OTA 和 重新配网 同时");
+            ESP_LOGE(LOG_TAG, "err:OTA 和 重新配网 同时");
             return;
         }
-        if(is_setting)
+        if (is_setting)
         {
             button_info = POWER_ON_INTO_OTA_VALUE;
-            if(xQueueSend(button_Data_queue,&button_info,0) != true)
+            if (xQueueSend(button_Data_queue, &button_info, 0) != true)
             {
-                ESP_LOGE(LOG_TAG,"queue send is fail");
+                ESP_LOGE(LOG_TAG, "queue send is fail");
                 return;
             }
         }
 
-        if(is_reset_net)
+        if (is_reset_net)
         {
             button_info = POWER_ON_INTO_RESET_VALUE;
-            if(xQueueSend(button_Data_queue,&button_info,0) != true)
+            if (xQueueSend(button_Data_queue, &button_info, 0) != true)
             {
-                ESP_LOGE(LOG_TAG,"queue send is fail");
+                ESP_LOGE(LOG_TAG, "queue send is fail");
                 return;
             }
         }
-    break;
-    case 4://5s开关机
+        break;
+    case 4: // 5s开关机
         button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
-        if(xQueueSend(button_Data_queue,&button_info,0) != true)
+        if (xQueueSend(button_Data_queue, &button_info, 0) != true)
         {
-            ESP_LOGE(LOG_TAG,"queue send is fail");
+            ESP_LOGE(LOG_TAG, "queue send is fail");
             return;
         }
-    break;
+        break;
     default:
-        //printf(" BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
-    break;
+        // printf(" BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
+        break;
     }
 }
-static void power_long_press_up_cb(void *arg,void *usr_data)
+static void power_long_press_up_cb(void *arg, void *usr_data)
 {
     // printf("power_long_press_up_cb\n");
 }
 #endif
 
-
-
 #include "esp_sleep.h"
 
-static void power_single_press_cb(void *arg,void *usr_data)
+static void power_single_press_cb(void *arg, void *usr_data)
 {
 
-      switch ((int)usr_data)
-      {
-          case 0:
-          printf("power sigle press\r\n");
-
-                  if(Machine_info.power_status == 1)  //开机
-                {
-
-                        button_info = POWER_ON_PRESS_VALUE;
-
-                        if(xQueueSend(button_Data_queue,&button_info,0) != true)
-                    {
-                        ESP_LOGE(LOG_TAG,"queue send is fail");
-                        return;
-                    }
+    switch ((int)usr_data)
+    {
+    case 0:
+        printf("power sigle press\r\n");
 
-                    
-                }else 
-                {
-                    button_info = POWER_OFF_PRESS_VALUE;  //关机状态下单击按键
-                if(xQueueSend(button_Data_queue,&button_info,0) != true)
-                    {
-                        ESP_LOGE(LOG_TAG,"queue send is fail");
-                        return;
-                    }
-                }
-   
-          break;
+        if (Machine_info.power_status == 1) // 开机
+        {
 
+            button_info = POWER_ON_PRESS_VALUE;
 
+            if (xQueueSend(button_Data_queue, &button_info, 0) != true)
+            {
+                ESP_LOGE(LOG_TAG, "queue send is fail");
+                return;
+            }
+        }
+        else
+        {
+            button_info = POWER_OFF_PRESS_VALUE; // 关机状态下单击按键
+            if (xQueueSend(button_Data_queue, &button_info, 0) != true)
+            {
+                ESP_LOGE(LOG_TAG, "queue send is fail");
+                return;
+            }
+        }
 
+        break;
 
-          case 1:
-          printf("power left single press\r\n");
+    case 1:
+        printf("power left single press\r\n");
 
-          break;
+        break;
 
-          case 2:
-          printf("power right single press\r\n");
+    case 2:
+        printf("power right single press\r\n");
 
-          break;
-      }
+        break;
+    }
 }
 
-
-static void user_button_single_click_cb(void *arg,void *usr_data)
+static void user_button_single_click_cb(void *arg, void *usr_data)
 {
     is_setting = false;
     is_reset_net = false;
-    ESP_LOGW(LOG_TAG," single_click_cb[%d]", iot_button_get_ticks_time((button_handle_t)arg));
+    ESP_LOGW(LOG_TAG, " single_click_cb[%d]", iot_button_get_ticks_time((button_handle_t)arg));
 
     switch ((int)usr_data)
     {
     case BAOYANG_KEY:
-            button_info = 6;
-            ESP_LOGD(LOG_TAG,"bao yang");
+        button_info = 6;
+        ESP_LOGD(LOG_TAG, "bao yang");
         break;
     case FENGCUN_KEY:
-            button_info = 5;
-            ESP_LOGD(LOG_TAG,"feng cun");
+        button_info = 5;
+        ESP_LOGD(LOG_TAG, "feng cun");
         break;
     case GUZHUANG_KEY:
-            button_info = 4;
-            ESP_LOGD(LOG_TAG,"gu zhang");
+        button_info = 4;
+        ESP_LOGD(LOG_TAG, "gu zhang");
         break;
     case DAILIAO_KEY:
-            button_info = 3;
-            ESP_LOGD(LOG_TAG,"dai liao");
+        button_info = 3;
+        ESP_LOGD(LOG_TAG, "dai liao");
         break;
     case TINGJI_KEY:
-            button_info = 2;
-            ESP_LOGD(LOG_TAG,"ting ji");
+        button_info = 2;
+        ESP_LOGD(LOG_TAG, "ting ji");
         break;
     case YUNXING_KEY:
-            button_info = 1;
-            ESP_LOGD(LOG_TAG,"yun xing");
+        button_info = 1;
+        ESP_LOGD(LOG_TAG, "yun xing");
         break;
     default:
         break;
@@ -583,84 +519,64 @@ static void user_button_single_click_cb(void *arg,void *usr_data)
     }
 
     xSemaphoreGive(button_semaphore);
-#endif   
+#endif
 
-    if(Machine_info.power_status == 0)  //关机
+    if (Machine_info.power_status == 0) // 关机
     {
         printf("power off not refresh\r\n");
         return;
     }
 
-    
-    if(xQueueSend(button_Data_queue,&button_info,0) != true)
+    if (xQueueSend(button_Data_queue, &button_info, 0) != true)
     {
-        ESP_LOGE(LOG_TAG,"queue send is fail");
+        ESP_LOGE(LOG_TAG, "queue send is fail");
         return;
     }
-
-    
 }
 
-
-
-static void power_button_multi_press_cb(void *arg,void *usr_data)
+static void power_button_multi_press_cb(void *arg, void *usr_data)
 {
-    if(Machine_info.power_status == 0)  //关机
+    if (Machine_info.power_status == 0) // 关机
     {
         printf("关机状态\r\n");
         return;
     }
     switch ((int)usr_data)
     {
-        case POWER_KEY_PRSSS_BLE_OTA_MODE:
-        if(!is_setting && !is_reset_net) //当前非配置模式
+    case POWER_KEY_PRSSS_BLE_OTA_MODE:
+        if (!is_setting && !is_reset_net) // 当前非配置模式
         {
             is_setting = true;
             xTimerStart(btn_timer_handle, 0);
             printf("短按2次\r\n");
         }
         break;
-        case POWER_KEY_PRSSS_DIS_RIGHT:
-            printf("短按3次\r\n");
-            send_button_key_queue((uint8_t)POWER_ON_INTO_DIS_RIGHT);
+    case POWER_KEY_PRSSS_DIS_RIGHT:
+        printf("短按3次\r\n");
+        send_button_key_queue((uint8_t)POWER_ON_INTO_DIS_RIGHT);
         break;
-        case  POWER_KEY_PRSSS_COUNT_RESET:
-            if(!is_setting && !is_reset_net)
-            {
-                is_reset_net = true;
-                xTimerStart(btn_timer_handle, 0);
-                printf("短按4次\r\n");
-            }
+    case POWER_KEY_PRSSS_COUNT_RESET:
+        if (!is_setting && !is_reset_net)
+        {
+            is_reset_net = true;
+            xTimerStart(btn_timer_handle, 0);
+            printf("短按4次\r\n");
+        }
         break;
     }
 }
 
-
-
-
-
-
-static void user_button_single_UP_cb(void *arg,void *usr_data)
+static void user_button_single_UP_cb(void *arg, void *usr_data)
 {
-
-
-    
 }
 
 void send_button_key_queue(uint8_t value)
 {
 
-         button_info = value;
-         if(xQueueSend(button_Data_queue,&button_info,0) != true)
-        {
-            ESP_LOGE(LOG_TAG,"queue send is fail");
-            return;
-        }
+    button_info = value;
+    if (xQueueSend(button_Data_queue, &button_info, 0) != true)
+    {
+        ESP_LOGE(LOG_TAG, "queue send is fail");
+        return;
+    }
 }
-
-
-
-
-
-
-

+ 207 - 131
components/esp_ble_ota/src/nimble_ota.c

@@ -2,7 +2,7 @@
  * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  *
  * SPDX-License-Identifier: Apache-2.0
-*/
+ */
 
 #include "esp_log.h"
 #include "nvs_flash.h"
@@ -20,34 +20,56 @@
 
 bool is_conn = false;
 
-
 #ifdef CONFIG_PRE_ENC_OTA
-#define SECTOR_END_ID                       2
-#define ENC_HEADER                          512
+#define SECTOR_END_ID 2
+#define ENC_HEADER 512
 esp_decrypt_handle_t decrypt_handle_cmp;
 #endif
 
-#define BUF_LENGTH                          4096
-#define OTA_IDX_NB                          4
-#define CMD_ACK_LENGTH                      20
+#define BUF_LENGTH 4096
+#define OTA_IDX_NB 4
+#define CMD_ACK_LENGTH 20
 
-#define character_declaration_uuid          BLE_ATT_UUID_CHARACTERISTIC
-#define character_client_config_uuid        BLE_ATT_UUID_CHARACTERISTIC
+#define character_declaration_uuid BLE_ATT_UUID_CHARACTERISTIC
+#define character_client_config_uuid BLE_ATT_UUID_CHARACTERISTIC
 
-#define GATT_SVR_SVC_ALERT_UUID             0x1811
-#define BLE_OTA_SERVICE_UUID                0x8018
+#define GATT_SVR_SVC_ALERT_UUID 0x1811
+#define BLE_OTA_SERVICE_UUID 0x8018
 
-#define RECV_FW_UUID                        0x8020
-#define OTA_BAR_UUID                        0x8021
-#define COMMAND_UUID                        0x8022
-#define CUSTOMER_UUID                       0x8023
+#define RECV_FW_UUID 0x8020
+#define OTA_BAR_UUID 0x8021
+#define COMMAND_UUID 0x8022
+#define CUSTOMER_UUID 0x8023
 
 #if CONFIG_EXAMPLE_EXTENDED_ADV
 static uint8_t ext_adv_pattern[] = {
-    0x02, 0x01, 0x06,
-    0x03, 0x03, 0xab, 0xcd,
-    0x03, 0x03, 0x18, 0x11,
-    0x0f, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'o', 't', 'a', '-', 'e', 'x', 't',
+    0x02,
+    0x01,
+    0x06,
+    0x03,
+    0x03,
+    0xab,
+    0xcd,
+    0x03,
+    0x03,
+    0x18,
+    0x11,
+    0x0f,
+    0X09,
+    'n',
+    'i',
+    'm',
+    'b',
+    'l',
+    'e',
+    '-',
+    'o',
+    't',
+    'a',
+    '-',
+    'e',
+    'x',
+    't',
 };
 #endif
 
@@ -64,8 +86,7 @@ static uint32_t ota_total_len = 0;
 static uint32_t ota_block_size = BUF_LENGTH;
 
 esp_ble_ota_callback_funs_t ota_cb_fun_t = {
-    .recv_fw_cb = NULL
-};
+    .recv_fw_cb = NULL};
 
 #ifndef CONFIG_OTA_WITH_PROTOCOMM
 esp_ble_ota_notification_check_t ota_notification = {
@@ -97,7 +118,7 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
 #endif
 
 /*----------------------------------------------------
- * Common api's 
+ * Common api's
  *----------------------------------------------------*/
 
 void ble_store_config_init(void);
@@ -119,7 +140,8 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
 
     pargs.data_in_len = om->om_len - 3;
 
-    if (SLIST_NEXT(om, om_next) != NULL) {
+    if (SLIST_NEXT(om, om_next) != NULL)
+    {
         struct os_mbuf *temp2 = SLIST_NEXT(om, om_next);
         pargs.data_in_len += temp2->om_len;
     }
@@ -127,12 +149,14 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
     pargs.data_in = (const char *)malloc(pargs.data_in_len * sizeof(char *));
     err = os_mbuf_copydata(om, 3, pargs.data_in_len, pargs.data_in);
 
-    if (om->om_data[2] == 0xff) {
+    if (om->om_data[2] == 0xff)
+    {
         pargs.data_in_len -= SECTOR_END_ID;
     }
 
     err = esp_encrypted_img_decrypt_data(decrypt_handle_cmp, &pargs);
-    if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED) {
+    if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED)
+    {
         return;
     }
 #endif
@@ -140,22 +164,25 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
     uint8_t cmd_ack[CMD_ACK_LENGTH] = {0x03, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00
-                                      };
+                                       0x00, 0x00, 0x00, 0x00, 0x00};
     uint16_t crc16;
 
-    if ((om->om_data[0] + (om->om_data[1] * 256)) != cur_sector) {
+    if ((om->om_data[0] + (om->om_data[1] * 256)) != cur_sector)
+    {
         // sector error
-        if ((om->om_data[0] == 0xff) && (om->om_data[1] == 0xff)) {
+        if ((om->om_data[0] == 0xff) && (om->om_data[1] == 0xff))
+        {
             // last sector
             ESP_LOGD(TAG, "Last sector");
-        } else {
+        }
+        else
+        {
             // sector error
             ESP_LOGE(TAG, "%s - sector index error, cur: %" PRIu32 ", recv: %d", __func__,
                      cur_sector, (om->om_data[0] + (om->om_data[1] * 256)));
             cmd_ack[0] = om->om_data[0];
             cmd_ack[1] = om->om_data[1];
-            cmd_ack[2] = 0x02; //sector index error
+            cmd_ack[2] = 0x02; // sector index error
             cmd_ack[3] = 0x00;
             cmd_ack[4] = cur_sector & 0xff;
             cmd_ack[5] = (cur_sector & 0xff00) >> 8;
@@ -168,11 +195,15 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
         }
     }
 
-    if (om->om_data[2] != cur_packet) { // packet seq error
-        if (om->om_data[2] == 0xff) { // last packet
+    if (om->om_data[2] != cur_packet)
+    { // packet seq error
+        if (om->om_data[2] == 0xff)
+        { // last packet
             ESP_LOGD(TAG, "last packet");
             goto write_ota_data;
-        } else { // packet seq error
+        }
+        else
+        { // packet seq error
             ESP_LOGE(TAG, "%s - packet index error, cur: %" PRIu32 ", recv: %d", __func__,
                      cur_packet, om->om_data[2]);
         }
@@ -180,7 +211,8 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
 
 write_ota_data:
 #ifdef CONFIG_PRE_ENC_OTA
-    if(pargs.data_out_len > 0) {
+    if (pargs.data_out_len > 0)
+    {
         memcpy(fw_buf + fw_buf_offset, pargs.data_out, pargs.data_out_len);
 
         free(pargs.data_out);
@@ -194,7 +226,8 @@ write_ota_data:
     memcpy(fw_buf + fw_buf_offset, om->om_data + 3, om->om_len - 3);
     fw_buf_offset += om->om_len - 3;
 
-    if (SLIST_NEXT(om, om_next) != NULL) {
+    if (SLIST_NEXT(om, om_next) != NULL)
+    {
         struct os_mbuf *temp = SLIST_NEXT(om, om_next);
 
         memcpy(fw_buf + fw_buf_offset, temp->om_data, temp->om_len);
@@ -206,21 +239,27 @@ write_ota_data:
     ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector,
              fw_buf_offset, om->om_len - 3);
 #endif
-    if (om->om_data[2] == 0xff) {
+    if (om->om_data[2] == 0xff)
+    {
         cur_packet = 0;
         cur_sector++;
         ESP_LOGD(TAG, "DEBUG: recv %" PRIu32 " sector", cur_sector);
         goto sector_end;
-    } else {
+    }
+    else
+    {
         ESP_LOGD(TAG, "DEBUG: wait next packet");
         cur_packet++;
     }
     return;
 
 sector_end:
-    if (fw_buf_offset < ota_block_size) {
+    if (fw_buf_offset < ota_block_size)
+    {
         esp_ble_ota_recv_fw_handler(fw_buf, fw_buf_offset);
-    } else {
+    }
+    else
+    {
         esp_ble_ota_recv_fw_handler(fw_buf, ota_block_size);
     }
     fw_buf_offset = 0;
@@ -228,7 +267,7 @@ sector_end:
 
     cmd_ack[0] = om->om_data[0];
     cmd_ack[1] = om->om_data[1];
-    cmd_ack[2] = 0x00; //success
+    cmd_ack[2] = 0x00; // success
     cmd_ack[3] = 0x00;
     crc16 = crc16_ccitt(cmd_ack, 18);
     cmd_ack[18] = crc16 & 0xff;
@@ -244,13 +283,18 @@ static uint16_t crc16_ccitt(const unsigned char *buf, int len)
     uint16_t crc16 = 0;
     int32_t i;
 
-    while (len--) {
+    while (len--)
+    {
         crc16 ^= *buf++ << 8;
 
-        for (i = 0; i < 8; i++) {
-            if (crc16 & 0x8000) {
+        for (i = 0; i < 8; i++)
+        {
+            if (crc16 & 0x8000)
+            {
                 crc16 = (crc16 << 1) ^ 0x1021;
-            } else {
+            }
+            else
+            {
                 crc16 = crc16 << 1;
             }
         }
@@ -272,7 +316,8 @@ unsigned int esp_ble_ota_get_fw_length(void)
 static esp_err_t
 esp_ble_ota_recv_fw_handler(uint8_t *buf, uint32_t length)
 {
-    if (ota_cb_fun_t.recv_fw_cb) {
+    if (ota_cb_fun_t.recv_fw_cb)
+    {
         ota_cb_fun_t.recv_fw_cb(buf, length);
     }
 
@@ -286,7 +331,7 @@ esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback)
     return ESP_OK;
 }
 #else
-esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback, 
+esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback,
                                             esp_decrypt_handle_t esp_decrypt_handle)
 {
     decrypt_handle_cmp = esp_decrypt_handle;
@@ -300,15 +345,13 @@ esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback,
  *----------------------------------------------------*/
 
 #ifdef CONFIG_OTA_WITH_PROTOCOMM
-void
-esp_ble_ota_set_sizes(size_t file_size, size_t block_size)
+void esp_ble_ota_set_sizes(size_t file_size, size_t block_size)
 {
     ota_total_len = file_size;
     ota_block_size = block_size;
 }
 
-void
-esp_ble_ota_finish(void)
+void esp_ble_ota_finish(void)
 {
     ESP_LOGI(TAG, "Received OTA end command");
     start_ota = false;
@@ -318,8 +361,7 @@ esp_ble_ota_finish(void)
     fw_buf = NULL;
 }
 
-void
-esp_ble_ota_write(uint8_t *file, size_t length)
+void esp_ble_ota_write(uint8_t *file, size_t length)
 {
     struct os_mbuf *om = ble_hs_mbuf_from_flat(file, length);
     esp_ble_ota_write_chr(om);
@@ -336,12 +378,12 @@ ble_ota_start_write_chr(struct os_mbuf *om)
     uint8_t cmd_ack[CMD_ACK_LENGTH] = {0x03, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0x00,
-                                       0x00, 0x00, 0x00, 0x00, 0x00
-                                      };
+                                       0x00, 0x00, 0x00, 0x00, 0x00};
     uint16_t crc16;
 
     esp_ble_ota_char_t ota_char = find_ota_char_and_desr_by_handle(attribute_handle);
-    if ((om->om_data[0] == 0x01) && (om->om_data[1] == 0x00)) {
+    if ((om->om_data[0] == 0x01) && (om->om_data[1] == 0x00))
+    {
         start_ota = true;
 
         ota_total_len = (om->om_data[2]) + (om->om_data[3] * 256) +
@@ -355,12 +397,15 @@ ble_ota_start_write_chr(struct os_mbuf *om)
         fw_buf = (uint8_t *)malloc(ota_block_size * sizeof(uint8_t));
 #else
 
- fw_buf = (uint8_t *)heap_caps_malloc(ota_block_size * sizeof(uint8_t),MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM);
+        fw_buf = (uint8_t *)heap_caps_malloc(ota_block_size * sizeof(uint8_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
 
 #endif
-        if (fw_buf == NULL) {
+        if (fw_buf == NULL)
+        {
             ESP_LOGE(TAG, "%s -  malloc fail", __func__);
-        } else {
+        }
+        else
+        {
             memset(fw_buf, 0x0, ota_block_size);
         }
         cmd_ack[2] = 0x01;
@@ -369,11 +414,14 @@ ble_ota_start_write_chr(struct os_mbuf *om)
         cmd_ack[18] = crc16 & 0xff;
         cmd_ack[19] = (crc16 & 0xff00) >> 8;
         esp_ble_ota_notification_data(connection_handle, attribute_handle, cmd_ack, ota_char);
-    } else if ((om->om_data[0] == 0x02) && (om->om_data[1] == 0x00)) {
+    }
+    else if ((om->om_data[0] == 0x02) && (om->om_data[1] == 0x00))
+    {
         printf("\nCMD_CHAR -> 0 : %d, 1 : %d", om->om_data[0],
                om->om_data[1]);
 #ifdef CONFIG_PRE_ENC_OTA
-        if (esp_encrypted_img_decrypt_end(decrypt_handle_cmp) != ESP_OK) {
+        if (esp_encrypted_img_decrypt_end(decrypt_handle_cmp) != ESP_OK)
+        {
             ESP_LOGI(TAG, "Decryption end failed");
         }
 #endif
@@ -396,7 +444,8 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
 {
     char buf[BLE_UUID_STR_LEN];
 
-    switch (ctxt->op) {
+    switch (ctxt->op)
+    {
     case BLE_GATT_REGISTER_OP_SVC:
         ESP_LOGD(TAG, "registered service %s with handle=%d\n",
                  ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
@@ -405,7 +454,7 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
 
     case BLE_GATT_REGISTER_OP_CHR:
         ESP_LOGD(TAG, "registering characteristic %s with "
-                 "def_handle=%d val_handle=%d\n",
+                      "def_handle=%d val_handle=%d\n",
                  ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
                  ctxt->chr.def_handle,
                  ctxt->chr.val_handle);
@@ -432,7 +481,8 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
 
     attribute_handle = attr_handle;
 
-    switch (ctxt->op) {
+    switch (ctxt->op)
+    {
     case BLE_GATT_ACCESS_OP_READ_CHR:
         ota_char = find_ota_char_and_desr_by_handle(attr_handle);
         ESP_LOGI(TAG, "client read, ota_char: %d", ota_char);
@@ -443,14 +493,19 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
         ota_char = find_ota_char_and_desr_by_handle(attr_handle);
         ESP_LOGD(TAG, "client write; len = %d", ctxt->om->om_len);
 
-        if (ota_char == RECV_FW_CHAR) {
-            if (start_ota) {
+        if (ota_char == RECV_FW_CHAR)
+        {
+            if (start_ota)
+            {
                 esp_ble_ota_write_chr(ctxt->om);
-
-            } else {
+            }
+            else
+            {
                 ESP_LOGE(TAG, "%s -  don't receive the start cmd", __func__);
             }
-        } else if (ota_char == CMD_CHAR) {
+        }
+        else if (ota_char == CMD_CHAR)
+        {
             ble_ota_start_write_chr(ctxt->om);
         }
         break;
@@ -466,36 +521,38 @@ static const struct ble_gatt_svc_def ota_gatt_db[] = {
         /* OTA Service Declaration */
         .type = BLE_GATT_SVC_TYPE_PRIMARY,
         .uuid = BLE_UUID16_DECLARE(BLE_OTA_SERVICE_UUID),
-        .characteristics = (struct ble_gatt_chr_def[])
-        {
+        .characteristics = (struct ble_gatt_chr_def[]){
             {
                 /* Receive Firmware Characteristic */
                 .uuid = BLE_UUID16_DECLARE(RECV_FW_UUID),
                 .access_cb = ble_ota_gatt_handler,
                 .val_handle = &receive_fw_val,
                 .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
-            }, {
+            },
+            {
                 /* OTA Characteristic */
                 .uuid = BLE_UUID16_DECLARE(OTA_BAR_UUID),
                 .access_cb = ble_ota_gatt_handler,
                 .val_handle = &ota_status_val,
                 .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_READ,
-            }, {
+            },
+            {
                 /* Command Characteristic */
                 .uuid = BLE_UUID16_DECLARE(COMMAND_UUID),
                 .access_cb = ble_ota_gatt_handler,
                 .val_handle = &command_val,
                 .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
-            }, {
+            },
+            {
                 /* Customer characteristic */
                 .uuid = BLE_UUID16_DECLARE(CUSTOMER_UUID),
                 .access_cb = ble_ota_gatt_handler,
                 .val_handle = &custom_val,
                 .flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
-            }, {
+            },
+            {
                 0, /* No more characteristics in this service */
-            }
-        },
+            }},
     },
     {
         0, /* No more services */
@@ -507,9 +564,12 @@ find_ota_char_and_desr_by_handle(uint16_t handle)
 {
     esp_ble_ota_char_t ret = INVALID_CHAR;
 
-    for (int i = 0; i < OTA_IDX_NB ; i++) {
-        if (handle == ota_handle_table[i]) {
-            switch (i) {
+    for (int i = 0; i < OTA_IDX_NB; i++)
+    {
+        if (handle == ota_handle_table[i])
+        {
+            switch (i)
+            {
             case RECV_FW_CHAR_VAL_IDX:
                 ret = RECV_FW_CHAR;
                 break;
@@ -540,24 +600,29 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
     int rc;
     txom = ble_hs_mbuf_from_flat(cmd_ack, CMD_ACK_LENGTH);
 
-    switch (ota_char) {
+    switch (ota_char)
+    {
     case RECV_FW_CHAR:
-        if (ota_notification.recv_fw_ntf_enable) {
+        if (ota_notification.recv_fw_ntf_enable)
+        {
             notify_enable = true;
         }
         break;
     case OTA_STATUS_CHAR:
-        if (ota_notification.process_bar_ntf_enable) {
+        if (ota_notification.process_bar_ntf_enable)
+        {
             notify_enable = true;
         }
         break;
     case CMD_CHAR:
-        if (ota_notification.command_ntf_enable) {
+        if (ota_notification.command_ntf_enable)
+        {
             notify_enable = true;
         }
         break;
     case CUS_CHAR:
-        if (ota_notification.customer_ntf_enable) {
+        if (ota_notification.customer_ntf_enable)
+        {
             notify_enable = true;
         }
         break;
@@ -565,11 +630,15 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
         break;
     }
 
-    if (notify_enable) {
+    if (notify_enable)
+    {
         rc = ble_gattc_notify_custom(conn_handle, attr_handle, txom);
-        if (rc == 0) {
+        if (rc == 0)
+        {
             ESP_LOGD(TAG, "Notification sent, attr_handle = %d", attr_handle);
-        } else {
+        }
+        else
+        {
             ESP_LOGE(TAG, "Error in sending notification, rc = %d", rc);
         }
         return rc;
@@ -618,7 +687,7 @@ esp_ble_ota_print_conn_desc(struct ble_gap_conn_desc *desc)
              desc->peer_id_addr.type);
     print_addr(desc->peer_id_addr.val);
     ESP_LOGI(TAG, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
-             "encrypted=%d authenticated=%d bonded=%d\n",
+                  "encrypted=%d authenticated=%d bonded=%d\n",
              desc->conn_itvl, desc->conn_latency,
              desc->supervision_timeout,
              desc->sec_state.encrypted,
@@ -641,11 +710,11 @@ esp_ble_ota_ext_advertise(void)
     int rc;
 
     /* First check if any instance is already active */
-    if(ble_gap_adv_active())
+    if (ble_gap_adv_active())
         return;
 
     /* use defaults for non-set params */
-    memset (&params, 0, sizeof(params));
+    memset(&params, 0, sizeof(params));
 
     /* enable connectable advertising */
     params.connectable = 1;
@@ -655,7 +724,7 @@ esp_ble_ota_ext_advertise(void)
 
     params.primary_phy = BLE_HCI_LE_PHY_1M;
     params.secondary_phy = BLE_HCI_LE_PHY_2M;
-    //params.tx_power = 127;
+    // params.tx_power = 127;
     params.sid = 1;
 
     params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
@@ -664,7 +733,7 @@ esp_ble_ota_ext_advertise(void)
     /* configure instance 0 */
     rc = ble_gap_ext_adv_configure(instance, &params, NULL,
                                    esp_ble_ota_gap_event, NULL);
-    assert (rc == 0);
+    assert(rc == 0);
     /* in this case only scan response is allowed */
 
     /* get mbuf for scan rsp data */
@@ -676,11 +745,11 @@ esp_ble_ota_ext_advertise(void)
     assert(rc == 0);
 
     rc = ble_gap_ext_adv_set_data(instance, data);
-    assert (rc == 0);
+    assert(rc == 0);
 
     /* start advertising */
     rc = ble_gap_ext_adv_start(instance, 0, 0);
-    assert (rc == 0);
+    assert(rc == 0);
 }
 #else
 static void
@@ -720,14 +789,14 @@ esp_ble_ota_advertise(void)
     fields.name_len = strlen(name);
     fields.name_is_complete = 1;
 
-    fields.uuids16 = (ble_uuid16_t[]) {
-        BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
-    };
+    fields.uuids16 = (ble_uuid16_t[]){
+        BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)};
     fields.num_uuids16 = 1;
     fields.uuids16_is_complete = 1;
 
     rc = ble_gap_adv_set_fields(&fields);
-    if (rc != 0) {
+    if (rc != 0)
+    {
         ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc);
         return;
     }
@@ -738,7 +807,8 @@ esp_ble_ota_advertise(void)
     adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
     rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
                            &adv_params, esp_ble_ota_gap_event, NULL);
-    if (rc != 0) {
+    if (rc != 0)
+    {
         ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc);
         return;
     }
@@ -767,20 +837,24 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
     int rc;
     esp_ble_ota_char_t ota_char;
 
-    switch (event->type) {
+    switch (event->type)
+    {
     case BLE_GAP_EVENT_CONNECT:
 
         /* A new connection was established or a connection attempt failed. */
         ESP_LOGI(TAG, "connection %s; status=%d ",
                  event->connect.status == 0 ? "established" : "failed",
                  event->connect.status);
-        if (event->connect.status == 0) {
+        if (event->connect.status == 0)
+        {
             is_conn = true;
             rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
             assert(rc == 0);
             esp_ble_ota_print_conn_desc(&desc);
             connection_handle = event->connect.conn_handle;
-        } else {
+        }
+        else
+        {
             /* Connection failed; resume advertising. */
 #if CONFIG_EXAMPLE_EXTENDED_ADV
             esp_ble_ota_ext_advertise();
@@ -799,7 +873,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
         /* Connection terminated; resume advertising. */
 #if CONFIG_EXAMPLE_EXTENDED_ADV
         esp_ble_ota_ext_advertise();
-#else       
+#else
         esp_ble_ota_advertise();
 #endif
         return 0;
@@ -837,7 +911,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
         ESP_LOGI(TAG, "client subscribe ble_gap_event, ota_char: %d", ota_char);
 
         ESP_LOGI(TAG, "subscribe event; conn_handle=%d attr_handle=%d "
-                 "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
+                      "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
                  event->subscribe.conn_handle,
                  event->subscribe.attr_handle,
                  event->subscribe.reason,
@@ -846,7 +920,8 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
                  event->subscribe.prev_indicate,
                  event->subscribe.cur_indicate);
 
-        switch (ota_char) {
+        switch (ota_char)
+        {
         case RECV_FW_CHAR:
             ota_notification.recv_fw_ntf_enable = true;
             break;
@@ -895,8 +970,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
     return 0;
 }
 
-int
-ble_ota_gatt_svr_init(void)
+int ble_ota_gatt_svr_init(void)
 {
     int rc;
 
@@ -904,12 +978,14 @@ ble_ota_gatt_svr_init(void)
     ble_svc_gatt_init();
 
     rc = ble_gatts_count_cfg(ota_gatt_db);
-    if (rc != 0) {
+    if (rc != 0)
+    {
         return rc;
     }
 
     rc = ble_gatts_add_svcs(ota_gatt_db);
-    if (rc != 0) {
+    if (rc != 0)
+    {
         return rc;
     }
 
@@ -932,7 +1008,8 @@ esp_ble_ota_on_sync(void)
 
     /* Figure out address to use while advertising (no privacy for now) */
     rc = ble_hs_id_infer_auto(0, &own_addr_type);
-    if (rc != 0) {
+    if (rc != 0)
+    {
         ESP_LOGE(TAG, "error determining address type; rc=%d\n", rc);
         return;
     }
@@ -946,10 +1023,9 @@ esp_ble_ota_on_sync(void)
     /* Begin advertising. */
 #if CONFIG_EXAMPLE_EXTENDED_ADV
     esp_ble_ota_ext_advertise();
-#else       
+#else
     esp_ble_ota_advertise();
 #endif
-
 }
 
 void esp_ble_ota_host_task(void *param)
@@ -967,7 +1043,8 @@ esp_ble_ota_host_init(void)
     int rc;
 
 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
-    if (esp_nimble_init() != 0) {
+    if (esp_nimble_init() != 0)
+    {
         ESP_LOGE(TAG, "nimble host init failed\n");
         return ESP_FAIL;
     }
@@ -990,25 +1067,24 @@ esp_ble_ota_host_init(void)
     rc = ble_ota_gatt_svr_init();
     assert(rc == 0);
 
-            #include "esp_mac.h"
-            char* device_name = (char*)malloc(sizeof("YC-OTA#")+4);
-            memcpy(device_name,"YC-OTA#",sizeof("YC-OTA#"));
-            
-
-            uint8_t macAddr[7];
-            char mac_5[4];
-            char mac_6[4];
-            esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
-            ESP_LOG_BUFFER_HEX(TAG,&macAddr,6);
-            itoa(macAddr[4],mac_5,16);
-            itoa(macAddr[5],mac_6,16);
-
-            strcat(device_name,mac_5);
-            strcat(device_name,mac_6);
-            ESP_LOGW(TAG,"ble name[%s]",device_name);
+#include "esp_mac.h"
+    char *device_name = (char *)malloc(sizeof("YC-OTA#") + 4);
+    memcpy(device_name, "YC-OTA#", sizeof("YC-OTA#"));
+
+    uint8_t macAddr[7];
+    char mac_5[4];
+    char mac_6[4];
+    esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
+    ESP_LOG_BUFFER_HEX(TAG, &macAddr, 6);
+    itoa(macAddr[4], mac_5, 16);
+    itoa(macAddr[5], mac_6, 16);
+
+    strcat(device_name, mac_5);
+    strcat(device_name, mac_6);
+    ESP_LOGW(TAG, "ble name[%s]", device_name);
     /* Set the default device name. */
     rc = ble_svc_gap_device_name_set(device_name);
-                free(device_name);
+    free(device_name);
     assert(rc == 0);
 
     /* XXX Need to have template for store */

+ 90 - 71
main/esp_ble_ota.c

@@ -21,16 +21,16 @@ TimerHandle_t ota_timer_handle;
 bool is_adv = false;
 void ota_timer_Callback(TimerHandle_t xTimer);
 void esp_ble_ota_delete(void);
-uint8_t ota_timeout_data[3] = {0x65,0x72,0x72};//err
+uint8_t ota_timeout_data[3] = {0x65, 0x72, 0x72}; // err
 
-#define OTA_RINGBUF_SIZE                    8192
-#define OTA_TASK_SIZE                       8192
+#define OTA_RINGBUF_SIZE 8192
+#define OTA_TASK_SIZE 8192
 
 static const char *TAG = "ESP_BLE_OTA";
 
 #if CONFIG_EXAMPLE_USE_PRE_ENC_OTA
 extern const char rsa_private_pem_start[] asm("_binary_private_pem_start");
-extern const char rsa_private_pem_end[]   asm("_binary_private_pem_end");
+extern const char rsa_private_pem_end[] asm("_binary_private_pem_end");
 esp_decrypt_handle_t decrypt_handle;
 #endif
 
@@ -41,8 +41,7 @@ esp_decrypt_handle_t decrypt_handle;
 
 #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
 static const char sec2_salt[] = {
-    0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4
-};
+    0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4};
 
 static const char sec2_verifier[] = {
     0x7c, 0x7c, 0x85, 0x47, 0x65, 0x08, 0x94, 0x6d, 0xd6, 0x36, 0xaf, 0x37, 0xd7, 0xe8, 0x91, 0x43,
@@ -68,8 +67,7 @@ static const char sec2_verifier[] = {
     0xec, 0xf0, 0x10, 0x95, 0x34, 0x33, 0x6b, 0xcb, 0x3e, 0x84, 0x0f, 0xb9, 0xd8, 0x5f, 0xb8, 0xa0,
     0xb8, 0x55, 0x53, 0x3e, 0x70, 0xf7, 0x18, 0xf5, 0xce, 0x7b, 0x4e, 0xbf, 0x27, 0xce, 0xce, 0xa8,
     0xb3, 0xbe, 0x40, 0xc5, 0xc5, 0x32, 0x29, 0x3e, 0x71, 0x64, 0x9e, 0xde, 0x8c, 0xf6, 0x75, 0xa1,
-    0xe6, 0xf6, 0x53, 0xc8, 0x31, 0xa8, 0x78, 0xde, 0x50, 0x40, 0xf7, 0x62, 0xde, 0x36, 0xb2, 0xba
-};
+    0xe6, 0xf6, 0x53, 0xc8, 0x31, 0xa8, 0x78, 0xde, 0x50, 0x40, 0xf7, 0x62, 0xde, 0x36, 0xb2, 0xba};
 #endif
 
 static esp_err_t
@@ -108,8 +106,10 @@ static void
 event_handler(void *arg, esp_event_base_t event_base,
               int32_t event_id, void *event_data)
 {
-    if (event_base == ESP_BLE_OTA_EVENT) {
-        switch (event_id) {
+    if (event_base == ESP_BLE_OTA_EVENT)
+    {
+        switch (event_id)
+        {
         case OTA_FILE_RCV:
             ESP_LOGD(TAG, "File received in appln layer :");
             ota_recv_fw_cb(event_data, 4096);
@@ -117,8 +117,11 @@ event_handler(void *arg, esp_event_base_t event_base,
         default:
             break;
         }
-    } else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT) {
-        switch (event_id) {
+    }
+    else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT)
+    {
+        switch (event_id)
+        {
         case PROTOCOMM_TRANSPORT_BLE_CONNECTED:
             ESP_LOGI(TAG, "BLE transport: Connected!");
             break;
@@ -142,11 +145,11 @@ get_device_service_name(char *service_name, size_t size)
 
 static RingbufHandle_t s_ringbuf = NULL;
 
-bool
-ble_ota_ringbuf_init(uint32_t ringbuf_size)
+bool ble_ota_ringbuf_init(uint32_t ringbuf_size)
 {
     s_ringbuf = xRingbufferCreate(ringbuf_size, RINGBUF_TYPE_BYTEBUF);
-    if (s_ringbuf == NULL) {
+    if (s_ringbuf == NULL)
+    {
         return false;
     }
 
@@ -157,23 +160,24 @@ size_t
 write_to_ringbuf(const uint8_t *data, size_t size)
 {
     BaseType_t done = xRingbufferSend(s_ringbuf, (void *)data, size, (TickType_t)portMAX_DELAY);
-    if (done) {
+    if (done)
+    {
         return size;
-    } else {
+    }
+    else
+    {
         return 0;
     }
 }
 
-void
-ota_task(void *arg)
+void ota_task(void *arg)
 {
-	ota_timer_handle = xTimerCreate(
-	    "ota_timer_handle",             
-	    pdMS_TO_TICKS(20*1000),    
-	    pdFALSE,                
-	    0,                     
-	    ota_timer_Callback         
-	);
+    ota_timer_handle = xTimerCreate(
+        "ota_timer_handle",
+        pdMS_TO_TICKS(20 * 1000),
+        pdFALSE,
+        0,
+        ota_timer_Callback);
     xTimerStart(ota_timer_handle, 0);
 
     esp_partition_t *partition_ptr = NULL;
@@ -187,93 +191,106 @@ ota_task(void *arg)
     ESP_LOGI(TAG, "ota_task start");
     // search ota partition
     partition_ptr = (esp_partition_t *)esp_ota_get_boot_partition();
-    if (partition_ptr == NULL) {
+    if (partition_ptr == NULL)
+    {
         ESP_LOGE(TAG, "boot partition NULL!\r\n");
         goto OTA_ERROR;
     }
-    if (partition_ptr->type != ESP_PARTITION_TYPE_APP) {
+    if (partition_ptr->type != ESP_PARTITION_TYPE_APP)
+    {
         ESP_LOGE(TAG, "esp_current_partition->type != ESP_PARTITION_TYPE_APP\r\n");
         goto OTA_ERROR;
     }
 
-    if (partition_ptr->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY) {
+    if (partition_ptr->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY)
+    {
         partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
-    } else {
+    }
+    else
+    {
         next_partition = esp_ota_get_next_update_partition(partition_ptr);
-        if (next_partition) {
+        if (next_partition)
+        {
             partition.subtype = next_partition->subtype;
-        } else {
+        }
+        else
+        {
             partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
         }
     }
     partition.type = ESP_PARTITION_TYPE_APP;
 
     partition_ptr = (esp_partition_t *)esp_partition_find_first(partition.type, partition.subtype, NULL);
-    if (partition_ptr == NULL) {
+    if (partition_ptr == NULL)
+    {
         ESP_LOGE(TAG, "partition NULL!\r\n");
         goto OTA_ERROR;
     }
 
     memcpy(&partition, partition_ptr, sizeof(esp_partition_t));
-    if (esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &out_handle) != ESP_OK) {
+    if (esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &out_handle) != ESP_OK)
+    {
         ESP_LOGE(TAG, "esp_ota_begin failed!\r\n");
         goto OTA_ERROR;
     }
 
     ESP_LOGE(TAG, "wait for data from ringbuf! fw_len = %u", esp_ble_ota_get_fw_length());
     /*deal with all receive packet*/
-    for (;;) {
+    for (;;)
+    {
         data = (uint8_t *)xRingbufferReceive(s_ringbuf, &item_size, (TickType_t)portMAX_DELAY);
-        
-        if((item_size == 3) )
-        {   
+
+        if ((item_size == 3))
+        {
             extern bool is_conn;
-            ESP_LOGI(TAG,"is_conn = %s",is_conn?"true":"false");
+            ESP_LOGI(TAG, "is_conn = %s", is_conn ? "true" : "false");
             // ESP_LOGI(TAG," [%02x] [%02x]   --- [%02x] [%02x]",data[0],data[1],ota_timeout_data[0],ota_timeout_data[1]);
-            if(!is_conn && (data[0] == 0x65)&& (data[1] == 0x72)&&(data[2] == 0x72))//
+            if (!is_conn && (data[0] == 0x65) && (data[1] == 0x72) && (data[2] == 0x72)) //
             {
                 goto OTA_ERROR;
             }
             else
             {
-                vRingbufferReturnItem(s_ringbuf, (void *)data);//
+                vRingbufferReturnItem(s_ringbuf, (void *)data); //
                 continue;
             }
         }
 
-        ESP_LOGI(TAG, "recv: %u, recv_total:%"PRIu32"\n", item_size, recv_len + item_size);
-        if (item_size != 0) {
-            if (esp_ota_write(out_handle, (const void *)data, item_size) != ESP_OK) {
+        ESP_LOGI(TAG, "recv: %u, recv_total:%" PRIu32 "\n", item_size, recv_len + item_size);
+        if (item_size != 0)
+        {
+            if (esp_ota_write(out_handle, (const void *)data, item_size) != ESP_OK)
+            {
                 ESP_LOGE(TAG, "esp_ota_write failed!\r\n");
                 goto OTA_ERROR;
             }
             recv_len += item_size;
             vRingbufferReturnItem(s_ringbuf, (void *)data);
 
-            if (recv_len >= esp_ble_ota_get_fw_length()) {
+            if (recv_len >= esp_ble_ota_get_fw_length())
+            {
                 break;
             }
         }
-
     }
 
-    if (esp_ota_end(out_handle) != ESP_OK) {
+    if (esp_ota_end(out_handle) != ESP_OK)
+    {
         ESP_LOGE(TAG, "esp_ota_end failed!\r\n");
         goto OTA_ERROR;
     }
 
-    if (esp_ota_set_boot_partition(&partition) != ESP_OK) {
+    if (esp_ota_set_boot_partition(&partition) != ESP_OK)
+    {
         ESP_LOGE(TAG, "esp_ota_set_boot_partition failed!\r\n");
         goto OTA_ERROR;
     }
 
-
     // extern Machine_info_t   Machine_info;
     // spiffs_write(&Machine_info);
 
-
-    extern void reset_default(bool is_dis,uint8_t power_status);
-    reset_default(false,0);
+    extern void reset_default(bool is_dis, uint8_t power_status);
+    reset_default(false, 0);
     ESP_LOGE(TAG, "存数据 esp_restart");
     vTaskDelay(500 / portTICK_PERIOD_MS);
     esp_restart();
@@ -282,14 +299,13 @@ OTA_ERROR:
     ESP_LOGE(TAG, "OTA failed");
     esp_ble_ota_delete();
     vTaskDelete(NULL);
-    #include "iot_button.h"
+#include "iot_button.h"
     iot_button_resume();
     extern void sleep_timer_start(int ms);
-    sleep_timer_start(100); //开始进入倒计时休眠 
+    sleep_timer_start(100); // 开始进入倒计时休眠
 }
 
-void
-ota_recv_fw_cb(uint8_t *buf, uint32_t length)
+void ota_recv_fw_cb(uint8_t *buf, uint32_t length)
 {
     write_to_ringbuf(buf, length);
 }
@@ -301,10 +317,9 @@ ota_task_init(void)
     return;
 }
 
-void
-esp_ble_ota(void)
+void esp_ble_ota(void)
 {
-    if(is_adv)
+    if (is_adv)
     {
         ESP_LOGI(TAG, " is adv ,return");
         return;
@@ -314,13 +329,15 @@ esp_ble_ota(void)
 
     // Initialize NVS
     ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
+    {
         ESP_ERROR_CHECK(nvs_flash_erase());
         ret = nvs_flash_init();
     }
     ESP_ERROR_CHECK(ret);
 
-    if (!ble_ota_ringbuf_init(OTA_RINGBUF_SIZE)) {
+    if (!ble_ota_ringbuf_init(OTA_RINGBUF_SIZE))
+    {
         ESP_LOGE(TAG, "%s init ringbuf fail", __func__);
         return;
     }
@@ -340,8 +357,7 @@ esp_ble_ota(void)
     esp_ble_ota_config_t config = {
         .scheme = esp_ble_ota_scheme_ble,
 
-        .scheme_event_handler = ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
-    };
+        .scheme_event_handler = ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM};
 
     /* Initialize ota manager with the
      * configuration parameters set above */
@@ -377,23 +393,26 @@ esp_ble_ota(void)
 #endif
     const char *service_key = NULL;
 
-    ESP_ERROR_CHECK(esp_ble_ota_start(security, (const void *) sec_params, service_name, service_key));
+    ESP_ERROR_CHECK(esp_ble_ota_start(security, (const void *)sec_params, service_name, service_key));
 #else
     ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
 
     ret = esp_bt_controller_init(&bt_cfg);
-    if (ret) {
+    if (ret)
+    {
         ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
         return;
     }
 
     ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
-    if (ret) {
+    if (ret)
+    {
         ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
         return;
     }
 
-    if (esp_ble_ota_host_init() != ESP_OK) {
+    if (esp_ble_ota_host_init() != ESP_OK)
+    {
         ESP_LOGE(TAG, "%s initialoze ble host fail: %s\n", __func__, esp_err_to_name(ret));
         return;
     }
@@ -403,7 +422,8 @@ esp_ble_ota(void)
     cfg.rsa_pub_key = rsa_private_pem_start;
     cfg.rsa_pub_key_len = rsa_private_pem_end - rsa_private_pem_start;
     decrypt_handle = esp_encrypted_img_decrypt_start(&cfg);
-    if (!decrypt_handle) {
+    if (!decrypt_handle)
+    {
         ESP_LOGE(TAG, "OTA upgrade failed");
         vTaskDelete(NULL);
     }
@@ -418,16 +438,15 @@ esp_ble_ota(void)
     ota_task_init();
 }
 
-
 void ota_timer_Callback(TimerHandle_t xTimer)
 {
     extern bool is_conn;
-    if(!is_conn)
+    if (!is_conn)
     {
         ESP_LOGI(TAG, "ota_timer_Callback");
         xRingbufferSend(s_ringbuf, (void *)ota_timeout_data, 3, (TickType_t)portMAX_DELAY);
     }
-    xTimerDelete(ota_timer_handle,0);
+    xTimerDelete(ota_timer_handle, 0);
 }
 #include "nimble/nimble_port.h"
 void esp_ble_ota_delete(void)

Fichier diff supprimé car celui-ci est trop grand
+ 422 - 498
main/ulp_riscv_adc_example_main.c


+ 34 - 40
main/user_config.h

@@ -12,52 +12,48 @@
 #include "SPIFFS.h"
 #include "Decection.h"
 
-
 typedef enum
 {
     YC_TASK_NONE = 0,
-    
+
     YC_TASK_OTHER,
     YC_TASK_BUTTON,
     YC_TASK_LORA,
     YC_TASK_UNPACK,
     YC_TASK_LOGIC,
-    
+
     YC_TASK_RIGHT_SCREEN,
     YC_TASK_LEFT_SCREEN,
 
-    YC_TASK_DEAL, //数据处理
+    YC_TASK_DEAL, // 数据处理
 
     YC_TASK_END,
 
-}YC_TASK_FUNC;
+} YC_TASK_FUNC;
 #define LEFT_SCREEN_BIT 1
 #define RIGHT_SCREEN_BIT 2
 
+#define SCREEN_WIDTH 648
+#define SCREEN_HEIGHT 480
 
-#define SCREEN_WIDTH                648
-#define SCREEN_HEIGHT               480
-
-
-#define IS_FEB_MONTH                ((Machine_info.day==29)&&(Machine_info.month==2)&&\
-                                        (!((Machine_info.year/4==0)||(Machine_info.year/2000==0))))
+#define IS_FEB_MONTH ((Machine_info.day == 29) && (Machine_info.month == 2) && \
+                      (!((Machine_info.year / 4 == 0) || (Machine_info.year / 2000 == 0))))
 
-#define IS_LEAP_YEAR                ((Machine_info.day==30)&&(Machine_info.month==2)&&\
-                                        ((Machine_info.year/4==0)||(Machine_info.year/2000==0)))
+#define IS_LEAP_YEAR ((Machine_info.day == 30) && (Machine_info.month == 2) && \
+                      ((Machine_info.year / 4 == 0) || (Machine_info.year / 2000 == 0)))
 
+#define IS_SMALL_MONTH ((Machine_info.day == 31) && ((Machine_info.month == 4) || \
+                                                     (Machine_info.month == 6) || \
+                                                     (Machine_info.month == 9) || \
+                                                     (Machine_info.month == 11)))
 
-#define IS_SMALL_MONTH              ((Machine_info.day==31) &&   ((Machine_info.month==4)||\
-                                                                    (Machine_info.month==6)||\
-                                                                    (Machine_info.month==9)||\
-                                                                    (Machine_info.month==11)))
-
-#define IS_BIG_MONTH                ((Machine_info.day==32) && ((Machine_info.month==1) ||\
-                                                                (Machine_info.month==3) ||\
-                                                                (Machine_info.month==5) ||\
-                                                                (Machine_info.month==7) ||\
-                                                                (Machine_info.month==8) ||\
-                                                                (Machine_info.month==10) ||\
-                                                                (Machine_info.month==12 )))
+#define IS_BIG_MONTH ((Machine_info.day == 32) && ((Machine_info.month == 1) ||  \
+                                                   (Machine_info.month == 3) ||  \
+                                                   (Machine_info.month == 5) ||  \
+                                                   (Machine_info.month == 7) ||  \
+                                                   (Machine_info.month == 8) ||  \
+                                                   (Machine_info.month == 10) || \
+                                                   (Machine_info.month == 12)))
 
 extern FONT_TYPE_T ascii_type_5x7;
 extern FONT_TYPE_T ascii_type_7x8;
@@ -81,22 +77,21 @@ extern struct EPD_INFO_SET right_screen;
 
 extern adc_oneshot_unit_handle_t adc1_handle;
 
-//extern Machine_info_t Machine_info;
-
+// extern Machine_info_t Machine_info;
 
 typedef enum
 {
-    STATE_NONE                  = 0,        //机器状态
-    
-    STATE_OPERATION             = 1,        //运行
-    STATE_SHUT_DOWN             = 2,        //停机
-    STATE_BREAKDOWN             = 3,        //故障
-    STATE_SAFEKEEP              = 4,        //封存
-    STATE_UPKEEP                = 5,        //保养
-    STATE_WAIT_MATERIALS        = 6,        //待料
+    STATE_NONE = 0, // 机器状态
+
+    STATE_OPERATION = 1,      // 运行
+    STATE_SHUT_DOWN = 2,      // 停机
+    STATE_BREAKDOWN = 3,      // 故障
+    STATE_SAFEKEEP = 4,       // 封存
+    STATE_UPKEEP = 5,         // 保养
+    STATE_WAIT_MATERIALS = 6, // 待料
 
     STATE_END,
-}Machine_state_t;
+} Machine_state_t;
 
 // typedef struct
 // {
@@ -104,12 +99,11 @@ typedef enum
 //     bool is_left;
 // }Screen_dis_t;
 
-
 extern int last_batt_precent;
 
 void esp_ble_ota(void);
-void reset_default(bool is_dis,uint8_t power_status);
+void reset_default(bool is_dis, uint8_t power_status);
 
-void Paint_leftScreen(bool is_poweron,bool is_test);
-void Paint_rightScreen(bool is_poweron,bool is_test);
+void Paint_leftScreen(bool is_poweron, bool is_test);
+void Paint_rightScreen(bool is_poweron, bool is_test);
 #endif

+ 252 - 293
main/user_sleep.c

@@ -15,7 +15,7 @@
 #include "soc/rtc.h"
 #include "nvs_flash.h"
 #include "nvs.h"
-#include"EPD.h"
+#include "EPD.h"
 #include "FONT_LIB.h"
 #include "esp_timer.h"
 #include "freertos/timers.h"
@@ -25,30 +25,24 @@
 
 #include "LORA.h"
 
+// 定时器选择
+#define TIMER_CHOICE 1 // 0:ESP  //1 FREERTOS
 
-//定时器选择
-#define   TIMER_CHOICE    1  //0:ESP  //1 FREERTOS
+#if TIMER_CHOICE
 
-
-#if     TIMER_CHOICE
-
-        TimerHandle_t sleep_timer;         //进入休眠定时器
-        TimerHandle_t already_send_timer;  //可以发送数据的定时器
+TimerHandle_t sleep_timer;        // 进入休眠定时器
+TimerHandle_t already_send_timer; // 可以发送数据的定时器
 #else
-        esp_timer_handle_t sleep_timer;         //开启睡眠定时器
-        esp_timer_handle_t already_send_timer;  //可以发送数据的定时器
+esp_timer_handle_t sleep_timer;        // 开启睡眠定时器
+esp_timer_handle_t already_send_timer; // 可以发送数据的定时器
 #endif
 
-
 static RTC_DATA_ATTR struct timeval sleep_enter_time;
 
+RTC_DATA_ATTR uint8_t _wakeup_reson = 0; // 内存上次唤醒的原因
 
-RTC_DATA_ATTR uint8_t _wakeup_reson = 0;  //内存上次唤醒的原因
-
-
-int64_t sleep_before_us = 0;  //开始进入睡眠时间
-int64_t sleep_afterr_us = 0;  //唤醒时间
-
+int64_t sleep_before_us = 0; // 开始进入睡眠时间
+int64_t sleep_afterr_us = 0; // 唤醒时间
 
 #define DEEP_SLEEP 0
 
@@ -74,217 +68,201 @@ extern QueueHandle_t wakeup_queue;
 extern void light_sleep_task(void *args);
 extern void wakeup_sleep_task(void *args);
 
-
 bool is_sleep = false;
 
-
-
-
 /*********************************************************************************
-* function   :   sleep_timer_callback     
-* Description :  进入睡眠回调函数
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
-void sleep_timer_callback(void* arg)
+ * function   :   sleep_timer_callback
+ * Description :  进入睡眠回调函数
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
+void sleep_timer_callback(void *arg)
 {
-    #if 0
+#if 0
     send_sleep_Queue();
-    #else
-    //printf("fall into sleep\r\n");
-    #include "ulp_riscv.h"
-    #include "esp_sleep.h"
-	#include "driver/uart.h"
-    //printf("error =%d\r\n",err);
-    sleep_before_us= esp_timer_get_time();
-     uart_wait_tx_idle_polling(0);
-     //ulp_riscv_timer_resume();
+#else
+// printf("fall into sleep\r\n");
+#include "ulp_riscv.h"
+#include "esp_sleep.h"
+#include "driver/uart.h"
+    // printf("error =%d\r\n",err);
+    sleep_before_us = esp_timer_get_time();
+    uart_wait_tx_idle_polling(0);
+    // ulp_riscv_timer_resume();
 
     is_sleep = true;
 
-// #if LORA_SLEEP_ENABLE
-//     //rtc_gpio_hold_dis(LORA_POWER_PIN);
-//     lora_set_power_level(0);
-//     //rtc_gpio_hold_en(LORA_POWER_PIN);
-// #endif
-//     esp_light_sleep_start();
-    #endif
+    // #if LORA_SLEEP_ENABLE
+    //     //rtc_gpio_hold_dis(LORA_POWER_PIN);
+    //     lora_set_power_level(0);
+    //     //rtc_gpio_hold_en(LORA_POWER_PIN);
+    // #endif
+    //     esp_light_sleep_start();
+#endif
 }
 
-
 /*********************************************************************************
-* function   :   sleep_timer_start     
-* Description :  倒计时开始进入睡眠
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
+ * function   :   sleep_timer_start
+ * Description :  倒计时开始进入睡眠
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
 void sleep_timer_start(int ms)
 {
-#if     TIMER_CHOICE
+#if TIMER_CHOICE
 
-        xTimerStop(sleep_timer, 0);   //停止定时器
-        // 在这里可以改变定时器的超时时间
-        // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
-        xTimerChangePeriod(sleep_timer, pdMS_TO_TICKS(ms), 0);
+    xTimerStop(sleep_timer, 0); // 停止定时器
+    // 在这里可以改变定时器的超时时间
+    // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
+    xTimerChangePeriod(sleep_timer, pdMS_TO_TICKS(ms), 0);
 
-        xTimerStart(sleep_timer, 0); //开始定时器
- //xTimerStart(sleep_Timerout, 0);
+    xTimerStart(sleep_timer, 0); // 开始定时器
+    // xTimerStart(sleep_Timerout, 0);
 
 #else
 
-    #if 0
+#if 0
     ESP_ERROR_CHECK(esp_timer_start_once(sleep_timer, ms*1000));//500ms
-    #else
+#else
 
-    esp_timer_start_once(sleep_timer, ms*1000);//500ms
-    #endif
-#endif    
+    esp_timer_start_once(sleep_timer, ms * 1000); // 500ms
+#endif
+#endif
 }
 
 /*********************************************************************************
-* function   :   sleep_timer_stop     
-* Description :  停止进入睡眠
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
+ * function   :   sleep_timer_stop
+ * Description :  停止进入睡眠
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
 void sleep_timer_stop(void)
 {
 
-#if     TIMER_CHOICE
+#if TIMER_CHOICE
 
-
-     xTimerStop(sleep_timer, 0);   //停止定时器
+    xTimerStop(sleep_timer, 0); // 停止定时器
 #else
-            #if 0
+#if 0
             ESP_ERROR_CHECK(esp_timer_stop(sleep_timer));
-            #else
-                esp_timer_stop(sleep_timer);
-            #endif
+#else
+    esp_timer_stop(sleep_timer);
+#endif
 #endif
 }
 
-
-
-
 //
 
 /*********************************************************************************
-* function   :   Already_send_timer_callback     
-* Description :  定到达指定时间准备发送数据或者接收数据
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
-void Already_send_timer_callback(void* arg)
+ * function   :   Already_send_timer_callback
+ * Description :  定到达指定时间准备发送数据或者接收数据
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
+void Already_send_timer_callback(void *arg)
 {
-#if     TIMER_CHOICE
+#if TIMER_CHOICE
 
     //   printf("Already_send_timer_callback\r\n");
 
-       extern void send_can_I_receive();
+    extern void send_can_I_receive();
 
-        if(Machine_info.eflagID != 0xff)
-        {
-                send_can_I_receive(); //发送可以接受的命令
-
-                if(Send_list!=NULL)
-                {
-                    vTaskDelay(50 / portTICK_PERIOD_MS);
-                    lora_send_data((char *)Send_list->data,Send_list->len);
-                    printf("->发送一条消息给网关\r\n");
-                }else
-                {
-                    printf("list not have data\r\n");
-                }
+    if (Machine_info.eflagID != 0xff)
+    {
+        send_can_I_receive(); // 发送可以接受的命令
 
+        if (Send_list != NULL)
+        {
+            vTaskDelay(50 / portTICK_PERIOD_MS);
+            lora_send_data((char *)Send_list->data, Send_list->len);
+            printf("->发送一条消息给网关\r\n");
         }
-    //uart_wait_tx_idle_polling(UART_NUM_1);
-    //example_register_timer_wakeup();
-    //esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*Machine_info.eflagID);  //配置当前休眠的唤醒时间
-    //printf("sleep time = %d\r\n",(TIMER_WAKEUP_TIME_US*Machine_info.eflagID)/1000);
-
-
-    #if 1
-       sleep_timer_start(1000);  //倒计时进入睡眠
-    #endif   
+        else
+        {
+            printf("list not have data\r\n");
+        }
+    }
+    // uart_wait_tx_idle_polling(UART_NUM_1);
+    // example_register_timer_wakeup();
+    // esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*Machine_info.eflagID);  //配置当前休眠的唤醒时间
+    // printf("sleep time = %d\r\n",(TIMER_WAKEUP_TIME_US*Machine_info.eflagID)/1000);
 
+#if 1
+    sleep_timer_start(1000); // 倒计时进入睡眠
+#endif
 
 #else
 
-    #if 0
+#if 0
     send_sleep_Queue();
-    #else
+#else
     printf("Already_send_timer_callback\r\n");
 
-       extern void send_can_I_receive();
-       send_can_I_receive(); //发送可以接受的命令
-       uart_wait_tx_idle_polling(UART_NUM_1);
+    extern void send_can_I_receive();
+    send_can_I_receive(); // 发送可以接受的命令
+    uart_wait_tx_idle_polling(UART_NUM_1);
 
-       sleep_timer_start(600);  //进入睡眠
+    sleep_timer_start(600); // 进入睡眠
 
-    // #include "ulp_riscv.h"
-    // #include "esp_sleep.h"
-	// #include "driver/uart.h"
-    // //printf("error =%d\r\n",err);
-    //  uart_wait_tx_idle_polling(0);
-    //  //ulp_riscv_timer_resume();
-    // esp_light_sleep_start();
-    #endif
+// #include "ulp_riscv.h"
+// #include "esp_sleep.h"
+// #include "driver/uart.h"
+// //printf("error =%d\r\n",err);
+//  uart_wait_tx_idle_polling(0);
+//  //ulp_riscv_timer_resume();
+// esp_light_sleep_start();
+#endif
 
- #endif   
+#endif
 }
 
-
-
 /*********************************************************************************
-* function   :   Already_send_timer_start     
-* Description :  定到达指定时间准备发送数据或者接收数据
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
+ * function   :   Already_send_timer_start
+ * Description :  定到达指定时间准备发送数据或者接收数据
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
 void Already_send_timer_start(int ms)
 {
-    #if     TIMER_CHOICE
-        xTimerStop(already_send_timer, 0);   //停止定时器
-        // 在这里可以改变定时器的超时时间
-        // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
-        xTimerChangePeriod(already_send_timer, pdMS_TO_TICKS(ms), 0);
-        xTimerStart(already_send_timer, 0); //开始定时器
-    #else
-    ESP_ERROR_CHECK(esp_timer_start_once(already_send_timer, ms*1000));//500ms
-    #endif
+#if TIMER_CHOICE
+    xTimerStop(already_send_timer, 0); // 停止定时器
+    // 在这里可以改变定时器的超时时间
+    // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
+    xTimerChangePeriod(already_send_timer, pdMS_TO_TICKS(ms), 0);
+    xTimerStart(already_send_timer, 0); // 开始定时器
+#else
+    ESP_ERROR_CHECK(esp_timer_start_once(already_send_timer, ms * 1000)); // 500ms
+#endif
 }
 
 /*********************************************************************************
-* function   :   Already_send_timer_stop     
-* Description :  停止定时器
-* Input       :  
-* Output      :  
-* Author      :  祁鑫                  Data : 2023 10.18
-**********************************************************************************/	
+ * function   :   Already_send_timer_stop
+ * Description :  停止定时器
+ * Input       :
+ * Output      :
+ * Author      :  祁鑫                  Data : 2023 10.18
+ **********************************************************************************/
 void Already_send_timer_stop(void)
 {
 
-    #if     TIMER_CHOICE
-     xTimerStop(already_send_timer, 0);   //停止定时器
-    #else
+#if TIMER_CHOICE
+    xTimerStop(already_send_timer, 0); // 停止定时器
+#else
     ESP_ERROR_CHECK(esp_timer_stop(already_send_timer));
 
-    #endif
+#endif
 }
 
-
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 /*定时器唤醒*/
 #include "esp_check.h"
 #include "esp_sleep.h"
 
-
-
 static const char *TAG = "timer_wakeup";
 
 esp_err_t example_register_timer_wakeup(void)
@@ -296,60 +274,71 @@ esp_err_t example_register_timer_wakeup(void)
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 #endif
 
-
 void user_sleep_into()
 {
 
-    #if DEEP_SLEEP
+#if DEEP_SLEEP
     struct timeval now;
     gettimeofday(&now, NULL);
     int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
 
-    switch (esp_sleep_get_wakeup_cause()) {
+    switch (esp_sleep_get_wakeup_cause())
+    {
 #if CONFIG_EXAMPLE_EXT0_WAKEUP
-        case ESP_SLEEP_WAKEUP_EXT0: {
-            printf("Wake up from ext0\n");
-            break;
-        }
+    case ESP_SLEEP_WAKEUP_EXT0:
+    {
+        printf("Wake up from ext0\n");
+        break;
+    }
 #endif // CONFIG_EXAMPLE_EXT0_WAKEUP
 #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
-        case ESP_SLEEP_WAKEUP_EXT1: {
-            uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
-            if (wakeup_pin_mask != 0) {
-                int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
-                printf("Wake up from GPIO %d\n", pin);
-            } else {
-                printf("Wake up from GPIO\n");
-            }
-            break;
+    case ESP_SLEEP_WAKEUP_EXT1:
+    {
+        uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
+        if (wakeup_pin_mask != 0)
+        {
+            int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
+            printf("Wake up from GPIO %d\n", pin);
         }
+        else
+        {
+            printf("Wake up from GPIO\n");
+        }
+        break;
+    }
 #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
-        case ESP_SLEEP_WAKEUP_GPIO: {
-            uint64_t wakeup_pin_mask = esp_sleep_get_gpio_wakeup_status();
-            if (wakeup_pin_mask != 0) {
-                int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
-                printf("Wake up from GPIO %d\n", pin);
-            } else {
-                printf("Wake up from GPIO\n");
-            }
-            break;
+    case ESP_SLEEP_WAKEUP_GPIO:
+    {
+        uint64_t wakeup_pin_mask = esp_sleep_get_gpio_wakeup_status();
+        if (wakeup_pin_mask != 0)
+        {
+            int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
+            printf("Wake up from GPIO %d\n", pin);
         }
-#endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
-        case ESP_SLEEP_WAKEUP_TIMER: {
-            printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
-            break;
+        else
+        {
+            printf("Wake up from GPIO\n");
         }
+        break;
+    }
+#endif // SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
+    case ESP_SLEEP_WAKEUP_TIMER:
+    {
+        printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
+        break;
+    }
 #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
-        case ESP_SLEEP_WAKEUP_TOUCHPAD: {
-            printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
-            break;
-        }
+    case ESP_SLEEP_WAKEUP_TOUCHPAD:
+    {
+        printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
+        break;
+    }
 #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
 
-        case ESP_SLEEP_WAKEUP_UNDEFINED:
-        default:
-            printf("Not a deep sleep reset\n");
+    case ESP_SLEEP_WAKEUP_UNDEFINED:
+    default:
+        printf("Not a deep sleep reset\n");
     }
 
     vTaskDelay(1000 / portTICK_PERIOD_MS);
@@ -358,15 +347,13 @@ void user_sleep_into()
     printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
     ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
 
-
-
     printf("Entering deep sleep\n");
     // get deep sleep enter time
     gettimeofday(&sleep_enter_time, NULL);
 
     font_into_sleep();
 
-    #if 0 
+#if 0 
     int i = 60;
 
     printf("not start deep display\r\n");
@@ -382,15 +369,13 @@ void user_sleep_into()
     {
          vTaskDelay(1000 / portTICK_PERIOD_MS);
     }
-    #else
+#else
     epd_sleep(SCREEN_LEFT);
     epd_sleep(SCREEN_RIGHT);
-    #endif
+#endif
     // enter deep sleep
     esp_deep_sleep_start();
-    #else
-
-
+#else
 
 #if 0
     //进入睡眠timer
@@ -411,54 +396,39 @@ void user_sleep_into()
     ESP_ERROR_CHECK(esp_timer_create(&send_timer_args, &already_send_timer));
 #else
 
- #if 1
-     //创建
-     sleep_timer = xTimerCreate(
-         "sleep_timer",        // 定时器名称(可以为NULL)
-         pdMS_TO_TICKS(800),         // 定时器超时时间(以毫秒为单位)
-         pdFALSE,                    // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
-         0,                          // 定时器ID(可以为0)
-         sleep_timer_callback  // 定时器回调函数
-     );
-
-
-
-
-    //创建
-     already_send_timer = xTimerCreate(
-         "already_send_timer",        // 定时器名称(可以为NULL)
-         pdMS_TO_TICKS(800),         // 定时器超时时间(以毫秒为单位)
-         pdFALSE,                    // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
-         0,                          // 定时器ID(可以为0)
-         Already_send_timer_callback  // 定时器回调函数
-     );
-
- //xTimerStart(sleep_Timerout, 0);
- #else
- sleep_timer_start(5000);
-
-
- #endif
-
-
+#if 1
+    // 创建
+    sleep_timer = xTimerCreate(
+        "sleep_timer",       // 定时器名称(可以为NULL)
+        pdMS_TO_TICKS(800),  // 定时器超时时间(以毫秒为单位)
+        pdFALSE,             // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
+        0,                   // 定时器ID(可以为0)
+        sleep_timer_callback // 定时器回调函数
+    );
+
+    // 创建
+    already_send_timer = xTimerCreate(
+        "already_send_timer",       // 定时器名称(可以为NULL)
+        pdMS_TO_TICKS(800),         // 定时器超时时间(以毫秒为单位)
+        pdFALSE,                    // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
+        0,                          // 定时器ID(可以为0)
+        Already_send_timer_callback // 定时器回调函数
+    );
+
+// xTimerStart(sleep_Timerout, 0);
+#else
+    sleep_timer_start(5000);
 
 #endif
 
+#endif
 
-
-
-
-   #endif
-
+#endif
 }
 
-
-
-
-
-int is_wake_up_reson()  //返回唤醒的原因
+int is_wake_up_reson() // 返回唤醒的原因
 {
-    _wakeup_reson  = esp_sleep_get_wakeup_cause();
+    _wakeup_reson = esp_sleep_get_wakeup_cause();
     // /* not a wakeup from ULP, load the firmware */
     // if ((cause != ESP_SLEEP_WAKEUP_ULP) && (cause != ESP_SLEEP_WAKEUP_TIMER)) {
     //     printf("Not a ULP-RISC-V wakeup (cause = %d), initializing it! \n", cause);
@@ -472,92 +442,81 @@ int is_wake_up_reson()  //返回唤醒的原因
 int left_adc_wake_btn_send()
 {
 
-
-uint16_t adc_value[][2]=
-{
-    {BAOYANG_MIN_ADC,BAOYANG_MAX_ADC},
-    {FENGCUN_MIN_ADC,FENGCUN_MAX_ADC},
-    {GUZHUANG_MIN_ADC,GUZHANG_MAX_ADC},
-    {DAILIAO_MIN_ADC,DAILIAO_MAX_ADC},
-    {TINGJI_MIN_ADC,TINGJI_MAX_ADC},
-    {YUNXING_MIN_ADC,YUNXING_MAX_ADC},
-};
-
-
-     //获取adc值 返回左界面索引值
-    #include "user_button.h"
+    uint16_t adc_value[][2] =
+        {
+            {BAOYANG_MIN_ADC, BAOYANG_MAX_ADC},
+            {FENGCUN_MIN_ADC, FENGCUN_MAX_ADC},
+            {GUZHUANG_MIN_ADC, GUZHANG_MAX_ADC},
+            {DAILIAO_MIN_ADC, DAILIAO_MAX_ADC},
+            {TINGJI_MIN_ADC, TINGJI_MAX_ADC},
+            {YUNXING_MIN_ADC, YUNXING_MAX_ADC},
+        };
+
+    // 获取adc值 返回左界面索引值
+#include "user_button.h"
 
     int result = 0xff;
 
     int button_info = 0xfB;
 
-     //extern uint16_t adc_value[][2];
+    // extern uint16_t adc_value[][2];
 
-        #include "ulp/example_config.h"
-        // extern int32_t wakeup_result;
-        // int result_adc = wakeup_result;
-        #if 1
-        int result_adc = ulp_wakeup_result;
+#include "ulp/example_config.h"
+// extern int32_t wakeup_result;
+// int result_adc = wakeup_result;
+#if 1
+    int result_adc = ulp_wakeup_result;
 
-        #endif
+#endif
 
-        int mid_result  = result_adc ;//(int)(((float)(result_adc/4095.0))*1100);
+    int mid_result = result_adc; //(int)(((float)(result_adc/4095.0))*1100);
 
-        printf("mid adc = %d\r\n",mid_result);
-        for(int i = 0;i<6;i++)
+    printf("mid adc = %d\r\n", mid_result);
+    for (int i = 0; i < 6; i++)
+    {
+        if ((adc_value[i][0] <= mid_result) && (adc_value[i][1] >= mid_result))
         {
-            if( ( adc_value[i][0]<=mid_result) &&   ( adc_value[i][1]>=mid_result)   )
-            {
-                    result =  i;
-                    break;
-            }
+            result = i;
+            break;
         }
+    }
 
-
-
-
-
-         switch ((int)result)
+    switch ((int)result)
     {
     case BAOYANG_KEY:
-            button_info = 6;
-            //ESP_LOGD(LOG_TAG,"bao yang");
+        button_info = 6;
+        // ESP_LOGD(LOG_TAG,"bao yang");
         break;
     case FENGCUN_KEY:
-            button_info = 5;
-            //ESP_LOGD(LOG_TAG,"feng cun");
+        button_info = 5;
+        // ESP_LOGD(LOG_TAG,"feng cun");
         break;
     case GUZHUANG_KEY:
-            button_info = 4;
-            //ESP_LOGD(LOG_TAG,"gu zhang");
+        button_info = 4;
+        // ESP_LOGD(LOG_TAG,"gu zhang");
         break;
     case DAILIAO_KEY:
-            button_info = 3;
-            //ESP_LOGD(LOG_TAG,"dai liao");
+        button_info = 3;
+        // ESP_LOGD(LOG_TAG,"dai liao");
         break;
     case TINGJI_KEY:
-            button_info = 2;
-            //ESP_LOGD(LOG_TAG,"ting ji");
+        button_info = 2;
+        // ESP_LOGD(LOG_TAG,"ting ji");
         break;
     case YUNXING_KEY:
-            button_info = 1;
-            //ESP_LOGD(LOG_TAG,"yun xing");
+        button_info = 1;
+        // ESP_LOGD(LOG_TAG,"yun xing");
         break;
     default:
         break;
     }
 
-
     ulp_wakeup_result = 3300;
 
-
-    if(button_info>6)
+    if (button_info > 6)
     {
         return 0xff;
     }
-       
-
 
     return button_info;
-             
 }

+ 12 - 22
main/user_sleep.h

@@ -1,27 +1,20 @@
 #ifndef __USER_SLEEP_H__
 #define __USER_SLEEP_H__
 
+#define TIMER_WAKEUP_TIME_S 30
+#define TIMER_WAKEUP_TIME_US (TIMER_WAKEUP_TIME_S * 1000 * 1000) // 定义的唤醒的时间
+#define TIMER_CAN_SEND_TIME (200)                                // 定义可以唤醒lora工作的时间单位ms
+#define LORA_POWER_TIME (2000)
 
-#define TIMER_WAKEUP_TIME_S      30
-#define TIMER_WAKEUP_TIME_US    (TIMER_WAKEUP_TIME_S * 1000 * 1000)     //定义的唤醒的时间  
-#define TIMER_CAN_SEND_TIME     (200)                 //定义可以唤醒lora工作的时间单位ms
-#define LORA_POWER_TIME         (2000)
-
-
-//睡眠关闭电源使能
-
-#define LORA_SLEEP_ENABLE 1         //使能LORA关闭电源
-#define FONT_SLEEP_ENABLE 1         //使能字库关闭电源
-#define LCD_SLEEP_ENABLE  0         //使能LCD关闭电源
-
-#define USER_LIGHT_SLEEP_ENABLE  1   //使能深度睡眠
-#define USER_DEEP_SLEEP_ENABLE   0   //使能深度睡眠
-#define USER_NOT_SLEEP_ENABLE    0   //使能禁止休眠模式
-
-
-
+// 睡眠关闭电源使能
 
+#define LORA_SLEEP_ENABLE 1 // 使能LORA关闭电源
+#define FONT_SLEEP_ENABLE 1 // 使能字库关闭电源
+#define LCD_SLEEP_ENABLE 0  // 使能LCD关闭电源
 
+#define USER_LIGHT_SLEEP_ENABLE 1 // 使能深度睡眠
+#define USER_DEEP_SLEEP_ENABLE 0  // 使能深度睡眠
+#define USER_NOT_SLEEP_ENABLE 0   // 使能禁止休眠模式
 
 void user_sleep_into();
 
@@ -30,9 +23,7 @@ esp_err_t example_register_timer_wakeup(void);
 
 // extern  uint8_t _wakeup_reson;  //内存上次唤醒的原因
 
-int is_wake_up_reson();  //返回唤醒的原因
-
-
+int is_wake_up_reson(); // 返回唤醒的原因
 
 void sleep_timer_start(int ms);
 void sleep_timer_stop(void);
@@ -40,7 +31,6 @@ void sleep_timer_stop(void);
 void Already_send_timer_start(int ms);
 void Already_send_timer_stop(void);
 
-
 int left_adc_wake_btn_send();
 
 #endif

+ 16 - 62
main/user_time.c

@@ -5,39 +5,25 @@
 #include <string.h>
 #include <ctype.h> // 包含ctype.h头文件以使用isxdigit函数
 
-
 void user_time_init()
 {
-
 }
 
-
-
 void user_time_handler()
 {
-
-    
 }
 
-
-
-
-
-
-
-
-
-
 #include <stdio.h>
 #include <time.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "driver/rtc_io.h"
 
-void setRtcTime(unsigned int year, unsigned int month, unsigned int day, 
-                unsigned int hour, unsigned int minute, unsigned int second) {
+void setRtcTime(unsigned int year, unsigned int month, unsigned int day,
+                unsigned int hour, unsigned int minute, unsigned int second)
+{
     struct tm tm_info = {0};
-    tm_info.tm_year = year - 1900;  // 年份需要减去1900
+    tm_info.tm_year = year - 1900; // 年份需要减去1900
     tm_info.tm_mon = month - 1;    // 月份从0开始,需要减去1
     tm_info.tm_mday = day;
     tm_info.tm_hour = hour;
@@ -51,13 +37,14 @@ void setRtcTime(unsigned int year, unsigned int month, unsigned int day,
     settimeofday(&tv, NULL);
 }
 
-void getRtcTime(Machine_info_t *info) {
+void getRtcTime(Machine_info_t *info)
+{
     // 获取 RTC 时间
     struct timeval tv;
     gettimeofday(&tv, NULL);
 
     // 打印 RTC 时间戳
-    //printf("RTC Timestamp: %lld\n", tv.tv_sec);
+    // printf("RTC Timestamp: %lld\n", tv.tv_sec);
 
     // 使用 localtime 函数将时间戳转换为本地时间
     struct tm *tm_info = localtime(&tv.tv_sec);
@@ -67,7 +54,6 @@ void getRtcTime(Machine_info_t *info) {
            tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday,
            tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
 
-
     info->year = tm_info->tm_year + 1900;
     info->month = tm_info->tm_mon + 1;
     info->day = tm_info->tm_mday;
@@ -75,44 +61,27 @@ void getRtcTime(Machine_info_t *info) {
     info->min = tm_info->tm_min;
     info->sec = tm_info->tm_sec;
 
-
-    printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",\
-    esp_get_free_heap_size(),esp_get_free_internal_heap_size(),esp_get_minimum_free_heap_size());
-
+    printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",
+           esp_get_free_heap_size(), esp_get_free_internal_heap_size(), esp_get_minimum_free_heap_size());
 }
 
-
-
 bool is_sync_time(Machine_info_t *info)
 {
     getRtcTime(info);
 
-    if((info->year ==1997) &&(info->month ==1)  &&(info->day ==1))
+    if ((info->year == 1997) && (info->month == 1) && (info->day == 1))
     {
         return false;
     }
 
-        return true;
-} 
-
-
-
-
-
+    return true;
+}
 
 // // 设置 RTC 时间戳为 2023-10-30 12:34:56
 // setRtcTime(2023, 10, 30, 12, 34, 56);
 // // 获取并打印 RTC 时间
 // getRtcTime();
 
-
-
-
-
-
-
-
-
 // // 将13位时间戳转换为格式化时间字符串,加上时区偏移
 // void timestampToStr(const char* timestamp_str, char* output_buffer, int size, int timezone_offset) {
 //     long long int timestamp = atoll(timestamp_str); // 将字符串转换为 long long int 类型的时间戳
@@ -123,7 +92,6 @@ bool is_sync_time(Machine_info_t *info)
 //     //strftime(output_buffer, buffer_size, "%Y-%m-%d %H:%M:%S", tm_info); // 将时间格式化为字符串
 // }
 
-
 // //函数用于从字符串中提取时间戳数字部分
 // char* extractTimestamp(const char* input_str) {
 //     const char* time_str = strstr(input_str, "\"time\":");
@@ -143,9 +111,6 @@ bool is_sync_time(Machine_info_t *info)
 //     }
 // }
 
-
-
-
 #if 0
 void timestamp_to_local_time(const char *timestamp, int *year, int *month, int *day, int *hour, int *minute, int *second) {
     long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds
@@ -164,9 +129,8 @@ void timestamp_to_local_time(const char *timestamp, int *year, int *month, int *
 }
 #else
 
-
-
-void timestamp_to_local_time(const char *timestamp, int offset, int *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second) {
+void timestamp_to_local_time(const char *timestamp, int offset, int *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second)
+{
     long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds
 
     struct tm *time_info;
@@ -186,14 +150,9 @@ void timestamp_to_local_time(const char *timestamp, int offset, int *year, uint8
     *second = time_info->tm_sec;
 }
 
-
-
-
-
-
-
 long long calculate_minutes_difference(int year1, int month1, int day1, int hour1, int minute1, int second1,
-                                       int year2, int month2, int day2, int hour2, int minute2, int second2) {
+                                       int year2, int month2, int day2, int hour2, int minute2, int second2)
+{
     struct tm timeinfo1 = {0};
     struct tm timeinfo2 = {0};
 
@@ -218,9 +177,4 @@ long long calculate_minutes_difference(int year1, int month1, int day1, int hour
     return difference / 60; // Convert seconds difference to minutes
 }
 
-
-
-
-
-
 #endif

+ 1 - 6
main/user_time.h

@@ -4,7 +4,6 @@
 #include "SPIFFS.h"
 #include <stdbool.h>
 
-
 void user_time_init();
 void user_time_handler();
 
@@ -13,23 +12,19 @@ void user_time_handler();
 #include <unistd.h>
 #include <sys/time.h>
 
-
-void setRtcTime(unsigned int year, unsigned int month, unsigned int day, 
+void setRtcTime(unsigned int year, unsigned int month, unsigned int day,
                 unsigned int hour, unsigned int minute, unsigned int second);
 
 void getRtcTime(Machine_info_t *info);
 
-
 bool is_sync_time(Machine_info_t *info);
 
 long long calculate_minutes_difference(int year1, int month1, int day1, int hour1, int minute1, int second1,
                                        int year2, int month2, int day2, int hour2, int minute2, int second2);
 
-
 // void timestampToStr(const char* timestamp_str, char* output_buffer, int size, int timezone_offset);
 // char* extractTimestamp(const char* input_str);
 
-
 #if 0
 void timestamp_to_local_time(const char *timestamp, int *year, int *month, int *day, int *hour, int *minute, int *second);
 #else

Fichier diff supprimé car celui-ci est trop grand
+ 1214 - 1275
main/yc_paint.c


Fichier diff supprimé car celui-ci est trop grand
+ 1864 - 2119
main/yc_protocol.c


+ 74 - 99
main/yc_protocol.h

@@ -1,7 +1,6 @@
 #ifndef _YC_PROTOCOL_H_
 #define _YC_PROTOCOL_H_
 
-
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
@@ -16,23 +15,18 @@
 
 extern LORA_DATA_T lora_data;
 
-//睡眠时间
-#define SLEEP_TIMES    3000  //3s
-//工作时间
-#define WORK_TIMES     100   //100ms
+// 睡眠时间
+#define SLEEP_TIMES 3000 // 3s
+// 工作时间
+#define WORK_TIMES 100 // 100ms
 
 typedef unsigned char u8;
 
-
 #define DEVOP_TEST 0
 
-#define VERSION_X    3
-#define VERSION_Y   2
-#define VERSION_Z   1
-
-
-
-
+#define VERSION_X 3
+#define VERSION_Y 2
+#define VERSION_Z 1
 
 // //------right char 3---------
 // extern uint32_t num_goodProducts[8];
@@ -54,18 +48,18 @@ typedef unsigned char u8;
 
 typedef enum
 {
-   KEY_CURRENT_STATUS_CMD  = 0, 
-   KEY_NEW_STATUS_CMD      = 1,
+    KEY_CURRENT_STATUS_CMD = 0,
+    KEY_NEW_STATUS_CMD = 1,
 };
 
 typedef struct
 {
     bool is_into_sleep;
     bool is_left;
-}Screen_dis_t;
+} Screen_dis_t;
 extern Screen_dis_t screen_dis_info;
 
-typedef enum 
+typedef enum
 {
     PROTOCOL_NONE = 0x00,
 
@@ -88,28 +82,28 @@ typedef enum
     PROTOCOL_CAPACITY_STATISTICS = 0x11,
     PROTOCOL_RESPONSIBLE_PERSON_SYNCHRONIZE_INFO = 0x12,
     PROTOCOL_STATUS_LED = 0x13,
-    PROTOCOL_LOG_LED =0x14,
+    PROTOCOL_LOG_LED = 0x14,
     PROTOCOL_BATTERY_TEMPERATURE = 0x15,
 
     PROTOCOL_STATION_NAME = 0X16,
     PROTOCOL_STATION_NUMBER = 0X17,
-    PROTOCOL_RESPONSIBLE_PERSON_SETUP= 0X18,
+    PROTOCOL_RESPONSIBLE_PERSON_SETUP = 0X18,
     PROTOCOL_SYSTEM_BULLETIN = 0X19,
     PROTOCOL_HARDWARE_UPDATE = 0x27,
 
-    PROTOCOL_HARDWARE_GWPAIRED = 0x80,  //硬件配网
+    PROTOCOL_HARDWARE_GWPAIRED = 0x80, // 硬件配网
 
-    PROTOCOL_HARDWARE_UNGWPAIRED = 0x81,  //硬件取消配网
+    PROTOCOL_HARDWARE_UNGWPAIRED = 0x81, // 硬件取消配网
 
-    PROTOCOL_RSPONSIBLE_ACK = 0x99,      //ACK
+    PROTOCOL_RSPONSIBLE_ACK = 0x99, // ACK
 
     PROTOCOL_END,
-}COMMUNICATION_PROTOCOL_T;
-
+} COMMUNICATION_PROTOCOL_T;
 
-typedef struct {
+typedef struct
+{
     uint8_t time_synchronization[8];
-    uint8_t terminal_name[32];//unicode
+    uint8_t terminal_name[32]; // unicode
     uint8_t terminal_number[16];
     uint8_t station_name[32];
     uint8_t station_number[16];
@@ -122,7 +116,7 @@ typedef struct {
     uint8_t rsponsible_person_delete[32];
     uint8_t production_punch[16];
     uint8_t personnel_check_in[32];
-    uint8_t status_setting[10];//别名最多2个汉字
+    uint8_t status_setting[10]; // 别名最多2个汉字
     uint8_t status_duration[32];
     uint8_t update_and_duration[32];
     uint8_t announcement[120];
@@ -132,17 +126,17 @@ typedef struct {
     uint8_t states_led;
     uint8_t logo_led;
     uint8_t batt_temperature[2];
-}TERMINAL_INFO_T;
+} TERMINAL_INFO_T;
 
 typedef enum
 {
-    CHARGE_administrator                =0,                //
-    CHARGE_product                    = 1,               //生产责任人
-    CHARGE_repair                     = 2,               //维修责任人
-    CHARGE_maintenance                  = 3,               //保养责任人
-    CHARGE_check                     = 4,               //巡检责任人
+    CHARGE_administrator = 0, //
+    CHARGE_product = 1,       // 生产责任人
+    CHARGE_repair = 2,        // 维修责任人
+    CHARGE_maintenance = 3,   // 保养责任人
+    CHARGE_check = 4,         // 巡检责任人
     CHARGE_END,
-}PERSON_CHARGE_TYPE_T;
+} PERSON_CHARGE_TYPE_T;
 
 #if 0
 typedef struct
@@ -155,19 +149,18 @@ typedef struct
 #endif
 typedef enum
 {
-    checkIn_product                    = 0,               //生产
-    checkIn_repair                     = 1,               //维修
-    checkIn_maintenance                = 2,               //保养
-    checkIn_check                      = 3,               //巡检
-}PERSON_CHECK_IN_TYPE_T;
+    checkIn_product = 0,     // 生产
+    checkIn_repair = 1,      // 维修
+    checkIn_maintenance = 2, // 保养
+    checkIn_check = 3,       // 巡检
+} PERSON_CHECK_IN_TYPE_T;
 
 typedef struct
 {
-    PERSON_CHARGE_TYPE_T        person_type;  //人员类型
-    uint8_t                     person_serial_number[17];
-    PAINT_TIME                  cur_time;
-}CHECK_IN_T;
-
+    PERSON_CHARGE_TYPE_T person_type; // 人员类型
+    uint8_t person_serial_number[17];
+    PAINT_TIME cur_time;
+} CHECK_IN_T;
 
 #if 1
 
@@ -200,18 +193,11 @@ typedef struct
 // // extern Person_Name_t;
 // // extern Person_type_t;
 
-
-
-
 void selectionSort(int arr[], int n, int indices[]);
-int  person_get_num_is_exist(uint8_t type,uint16_t num);
-int  person_get_num(uint8_t type);
-void person_add(uint8_t type,uint8_t num,char* in,char *out);
-void person_del(uint8_t type,uint8_t num,char* in,char *out);
-
-
-
-
+int person_get_num_is_exist(uint8_t type, uint16_t num);
+int person_get_num(uint8_t type);
+void person_add(uint8_t type, uint8_t num, char *in, char *out);
+void person_del(uint8_t type, uint8_t num, char *in, char *out);
 
 #endif
 
@@ -229,43 +215,41 @@ typedef struct
 
     uint8_t time[20];
 
-}Current_Time_t;
+} Current_Time_t;
 typedef struct
 {
     uint8_t cmd;
     uint8_t len;
-    uint8_t mac_addr[20];   //16进制mac地址 中间: 分割  0x3A
-
-}Mac_t;
+    uint8_t mac_addr[20]; // 16进制mac地址 中间: 分割  0x3A
 
+} Mac_t;
 
 typedef struct
 {
     uint8_t cmd;
     uint8_t len;
-    uint8_t client_id[20];  //设备id
+    uint8_t client_id[20]; // 设备id
 
-}Client_t;
+} Client_t;
 
 typedef struct
 {
     uint8_t cmd;
     uint8_t len;
-    uint8_t product[20];  //产品型号设备
+    uint8_t product[20]; // 产品型号设备
 
-}Product_Device_t;
+} Product_Device_t;
 typedef struct
 {
     uint8_t head[2];
     uint8_t CRC;
-    Mac_t  mac ;                //0x03
-    Client_t client;            //0x04
-    Product_Device_t  product;  //0x05
+    Mac_t mac;                // 0x03
+    Client_t client;          // 0x04
+    Product_Device_t product; // 0x05
 
-    Current_Time_t time;        //0x61
-
-}Qrcode_protocol_t;
+    Current_Time_t time; // 0x61
 
+} Qrcode_protocol_t;
 
 typedef struct
 {
@@ -274,15 +258,12 @@ typedef struct
     // uint8_t data[1024];
     int len;
     int index;
-}YC_DATA_T;
-
-
-#define USE_DATA_LEN_INDEX              15
-#define USE_CMD_LEN_INDEX               12
-#define USELESS_DATA_LEN                17
-#define DATA_START_LEN              (USELESS_DATA_LEN)
-
+} YC_DATA_T;
 
+#define USE_DATA_LEN_INDEX 15
+#define USE_CMD_LEN_INDEX 12
+#define USELESS_DATA_LEN 17
+#define DATA_START_LEN (USELESS_DATA_LEN)
 
 void timer_init(void);
 void lora_timer_start(void);
@@ -292,36 +273,31 @@ void adc1_init(void);
 void print_systenInfo(void);
 void yc_timer_restart(void);
 // void periodic_timer_callback(void* arg);
-void print_yc_data(const LORA_DATA_T* buf);
-void business_logic_func(const LORA_DATA_T* buf,uint8_t cmd_index,int msg);
+void print_yc_data(const LORA_DATA_T *buf);
+void business_logic_func(const LORA_DATA_T *buf, uint8_t cmd_index, int msg);
 void screen_display(void);
-bool filtered_data(const LORA_DATA_T* buf);
+bool filtered_data(const LORA_DATA_T *buf);
 
-void uincode2gbk(char *str,uint16_t len,char *out_buffer);
-void qrcode_protocol_create(char *result,Machine_info_t info);
-bool subcontract(YC_DATA_T* data);
+void uincode2gbk(char *str, uint16_t len, char *out_buffer);
+void qrcode_protocol_create(char *result, Machine_info_t info);
+bool subcontract(YC_DATA_T *data);
 
 void send_rssi(void);
 void send_status(void);
-void send_ACK(char cmd,bool flag);
+void send_ACK(char cmd, bool flag);
 
+void loro_gwpair_ack(int destaddr, int sourceaddr, int cmd, int st, char *strd);
 
-void loro_gwpair_ack(int destaddr,int sourceaddr,int cmd,int st,char *strd);
-
-
-//typedef unsigned char u8;
-int findByteSequence(const u8* array, int arraySize, const u8* sequence, int sequenceSize, int** positions);
-void  analysis_protocol(char *result,int len);
-
-
-int reply_ack_func(int cmd,int msg_id,bool is_send,uint8_t *result);
+// typedef unsigned char u8;
+int findByteSequence(const u8 *array, int arraySize, const u8 *sequence, int sequenceSize, int **positions);
+void analysis_protocol(char *result, int len);
 
+int reply_ack_func(int cmd, int msg_id, bool is_send, uint8_t *result);
 
 void send_can_I_receive();
 
 void send_pair_ack();
 
-
 void terminal_send_data(void);
 void update_last_button_info(uint8_t btn);
 void f_send_version(void);
@@ -330,11 +306,10 @@ void f_send_lora_rssi(int rssi);
 void f_send_battary_vaule(int battry);
 void f_send_reply_status(int status);
 
-
 void set_status_heights(void);
-void user_compare(int last,int now);
+void user_compare(int last, int now);
 
-void user_compare_power_off(int last,int now);//这个函数用来比较关机状态时电量变化 刷右屏//
+void user_compare_power_off(int last, int now); // 这个函数用来比较关机状态时电量变化 刷右屏//
 
 void right_screen_send();
 
@@ -343,10 +318,10 @@ void right_screen_send();
 //     uint16_t number;               // 节点值
 //     uint8_t type;
 //     char* name;
-    
+
 //     struct ListNode *next; // 指向下一节点的引用
 // };
 // typedef struct ListNode ListNode;
 // ListNode *newListHead(void);
 // void printList(ListNode *head);
-#endif/*_YC_PROTOCOL_H_*/
+#endif /*_YC_PROTOCOL_H_*/

Fichier diff supprimé car celui-ci est trop grand
+ 723 - 883
main/yc_terminal.c