yc_terminal.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/queue.h"
  7. #include "freertos/semphr.h"
  8. #include "esp_log.h"
  9. #include "esp_system.h"
  10. #include "freertos/event_groups.h"
  11. #include "esp_timer.h"
  12. #include "esp_err.h"
  13. #include "user_config.h"
  14. #include "LED.h"
  15. #include "list.h"
  16. #include "freertos/timers.h"
  17. #include "user_sleep.h"
  18. #include "esp_sleep.h"
  19. #include "esp_sleep.h"
  20. #include "user_time.h"
  21. #include "driver/uart.h"
  22. #include "driver/rtc_io.h"
  23. char user_device_id[50] = {0};
  24. RTC_FAST_ATTR unsigned char power = 0;
  25. #define MAX_RETRY_ACK 3 // 最大重传次数停止发送数据
  26. int retry_times = 0;
  27. bool powerOn_flag = false;
  28. static const char *LOG_TAG = "user_main";
  29. extern Node *Send_list; // 发送数据链表
  30. YC_DATA_T yc_data;
  31. // ListNode *list_head = NULL;
  32. adc_oneshot_unit_handle_t adc1_handle;
  33. QueueHandle_t screen_queue;
  34. QueueHandle_t lora_data_queue;
  35. QueueHandle_t yc_data_queue;
  36. QueueHandle_t button_Data_queue;
  37. EventGroupHandle_t screen_event;
  38. extern QueueHandle_t lora_receiveQueue; // lora底层数据上报
  39. extern QueueHandle_t lora_dealhandle; // 开始处理逻辑的数据
  40. QueueHandle_t sleep_queue;
  41. QueueHandle_t wakeup_queue;
  42. QueueHandle_t Send_Data_queue; // 发送链表任务
  43. SemaphoreHandle_t button_semaphore; // 刷新屏幕时都得加上按键互斥锁
  44. // SemaphoreHandle_t screen_semaphore; //刷新屏幕时都得加上互斥锁
  45. #if !HARDWARE_SPI
  46. struct EPD_INFO_SET left_screen = {};
  47. struct EPD_INFO_SET right_screen = {};
  48. #endif
  49. extern LORA_DATA_T lora_data;
  50. extern TERMINAL_INFO_T terminal_info;
  51. #include "y_ringbuf.h"
  52. extern struct RINGBUF_st;
  53. extern RINGBUF_st *lora_ringbuf;
  54. // static void board_init(void);
  55. // static void info_init(void);
  56. // static void left_screen_task(void* arg);
  57. // static void right_screen_task(void* arg);
  58. static void screen_task(void *arg);
  59. static void unpack_task(void *arg);
  60. static void lora_task(void *arg);
  61. static void button_task(void *arg);
  62. static void business_logic_task(void *arg);
  63. static void gui_task(void *pvParameter);
  64. void read_deal_data_callback_handler();
  65. // void Sendlist_task_callback_handler();
  66. void uart_task_callback_handler();
  67. /*********************************************************************************
  68. * function : user_nvs_init
  69. * Description : 添加NVS相关 获取出厂设备ID等相关参数
  70. * Input :
  71. * Output :
  72. * Author : Data : 2023 11.08
  73. **********************************************************************************/
  74. void user_nvs_init()
  75. {
  76. #include "nvs_flash.h"
  77. #include "nvs.h"
  78. nvs_handle_t my_handle;
  79. esp_err_t ret;
  80. /* Initialize NVS. */
  81. ret = nvs_flash_init();
  82. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  83. {
  84. ESP_ERROR_CHECK(nvs_flash_erase());
  85. ret = nvs_flash_init();
  86. printf("nvs_flash_init ok\r\n");
  87. }
  88. ret = ESP_OK;
  89. if ((ret = nvs_flash_init_partition("nvs")) != ESP_OK)
  90. {
  91. printf("nvs init fail\r\n");
  92. }
  93. else
  94. {
  95. printf("nvs init ok\r\n");
  96. }
  97. ret = nvs_open_from_partition("nvs", "user_config", NVS_READONLY, &my_handle);
  98. if (ret != ESP_OK)
  99. {
  100. printf("nvs_open failed with\r\n");
  101. }
  102. size_t len = 0;
  103. char cid[50] = {0};
  104. // size_t required_size;
  105. // nvs_get_str(my_handle, "server_name", NULL, &required_size);
  106. // char* server_name = malloc(required_size);
  107. // nvs_get_str(my_handle, "server_name", server_name, &required_size);
  108. ret = nvs_get_str(my_handle, "deviceid", NULL, &len);
  109. char *result = malloc(len);
  110. nvs_get_str(my_handle, "deviceid", result, &len);
  111. switch (ret)
  112. {
  113. case ESP_OK:
  114. // printf("Read string from NVS: %s\r\n", cid);
  115. break;
  116. case ESP_ERR_NVS_NOT_FOUND:
  117. printf("Value not found in NVS");
  118. break;
  119. default:
  120. printf("Error (%d) reading from NVS", ret);
  121. }
  122. memcpy(user_device_id, cid, len);
  123. memcpy(Machine_info.cid, cid, len);
  124. #if 1
  125. strcpy((char *)Machine_info.cid, result);
  126. printf("device id %s,result = %s,len = %d\r\n", cid, result, len);
  127. #else
  128. strcpy((char *)Machine_info.cid, "1tpmQwHNS");
  129. #endif
  130. printf("Machine_info cid %s\r\n", Machine_info.cid);
  131. nvs_close(my_handle);
  132. free(result);
  133. }
  134. extern void app_init();
  135. Machine_info_t default_info = {
  136. .left_display_mode = 0,
  137. .left_state = 1,
  138. .paired = 0, // 未配网
  139. .power_status = 0, // 关机
  140. // .is_setting = 0, //设置模式
  141. .eflagID = 0xFF,
  142. .left_max_Quick_refresh_time = 5, // 最大快刷次数
  143. .left_current_Quick_refresh_time = 0, // 当前已经快刷的次数 当前快刷的次数大于设置 慢刷一次
  144. .lora_factory_channel = LORA_CHANENL,
  145. .right_max_Quick_refresh_time = 5, // 最大快刷次数
  146. .right_current_Quick_refresh_time = 0, // 当前已经快刷的次数 当前快刷的次数大于设置 慢刷一次
  147. .current_button.button_info = 0x01, // 初始化为1(运行)
  148. .last_button.button_info = 0x01,
  149. .terminal_name = {0xD2, 0xCF, 0xB3, 0xB2, 0xD6, 0xD5, 0xB6, 0xCB, 0x00, 0x00}, // 蚁巢终端
  150. .terminal_number = {0xD2, 0xCF, 0xB3, 0xB2, 0xB1, 0xE0, 0xBA, 0xC5, 0x00, 0x00}, // 蚁巢编号
  151. .station_name = {0xB9, 0xA4, 0xD5, 0xBE, 0xC3, 0xFB, 0xB3, 0xC6, 0x00, 0x00}, // 工站名称
  152. .station_number = {0xB9, 0xA4, 0xD5, 0xBE, 0xB1, 0xE0, 0xBA, 0xC5, 0x00, 0x00}, // 工站编号
  153. .rssi = 0x64,
  154. .refresh_cycle = 2,
  155. .batt_precent = 100,
  156. .btn_operation = {0xd4, 0xcb, 0xd0, 0xd0, 0x00, 0x00},
  157. .btn_breakDown_info = {0xb9, 0xca, 0xd5, 0xcf, 0x00, 0x00},
  158. .btn_shutDown_info = {0xcd, 0xa3, 0xbb, 0xfa, 0x00, 0x00},
  159. .btn_safeKeep_info = {0xb7, 0xe2, 0xb4, 0xe6, 0x00, 0x00},
  160. .btn_upKeep_info = {0xb1, 0xa3, 0xd1, 0xf8, 0x00, 0x00},
  161. .btn_waitMaterials_info = {0xb4, 0xfd, 0xc1, 0xcf, 0x00, 0x00},
  162. .btn_dis_flag = {true, true, true, true, true, true},
  163. .checkIn_set[0] = {
  164. .other_name = {0xC9, 0xFA, 0xB2, 0xFA, 0x00, 0x00},
  165. .checkIn_close = false,
  166. },
  167. .checkIn_set[1] = {
  168. .other_name = {0xCE, 0xAC, 0xD0, 0xDE, 0x00, 0x00},
  169. .checkIn_close = false,
  170. },
  171. .checkIn_set[2] = {
  172. .other_name = {0xD1, 0xB2, 0xBC, 0xEC, 0x00, 0x00},
  173. .checkIn_close = false,
  174. },
  175. .checkIn_set[3] = {
  176. .other_name = {0xB1, 0xA3, 0xD1, 0xF8, 0x00, 0x00},
  177. .checkIn_close = false,
  178. },
  179. .person[0] = {
  180. .Charge_close = false,
  181. .other_name = {0xb9, 0xdc, 0x20, 0x20, 0xc0, 0xed, 0x20, 0x20, 0xd4, 0xb1, 0x3a, 0x00, 0x00, 0x00},
  182. },
  183. .person[1] = {
  184. .Charge_close = false,
  185. .other_name = {0xc9, 0xfa, 0xb2, 0xfa, 0xd4, 0xf0, 0xc8, 0xce, 0xc8, 0xcb, 0x3a, 0x00, 0x00, 0x00},
  186. },
  187. .person[2] = {
  188. .Charge_close = false,
  189. .other_name = {0xce, 0xac, 0xd0, 0xde, 0xd4, 0xf0, 0xc8, 0xce, 0xc8, 0xcb, 0x3a, 0x00, 0x00, 0x00},
  190. },
  191. .person[3] = {
  192. .Charge_close = false,
  193. .other_name = {0xb1, 0xa3, 0xd1, 0xf8, 0xd4, 0xf0, 0xc8, 0xce, 0xc8, 0xcb, 0x3a, 0x00, 0x00, 0x00},
  194. },
  195. .person[4] = {
  196. .Charge_close = false,
  197. .other_name = {0xd1, 0xb2, 0xbc, 0xec, 0xd4, 0xf0, 0xc8, 0xce, 0xc8, 0xcb, 0x3a, 0x00, 0x00, 0x00},
  198. },
  199. };
  200. #define PRINT_SPIFFS 0
  201. #if PRINT_SPIFFS
  202. void printHexData(const void *ptr, size_t size)
  203. {
  204. const unsigned char *p = (const unsigned char *)ptr;
  205. for (size_t i = 0; i < size; ++i)
  206. {
  207. printf("%02x ", p[i]);
  208. if (i % 16 == 0)
  209. {
  210. printf("\r\n");
  211. }
  212. }
  213. // printf("\n");
  214. }
  215. #endif
  216. void app_init()
  217. {
  218. #if 0
  219. gpio_reset_pin(4);
  220. gpio_config_t io_conf = {};
  221. io_conf.pin_bit_mask = (1<<4);
  222. io_conf.mode = GPIO_MODE_INPUT;
  223. io_conf.pull_up_en = false;
  224. gpio_config(&io_conf);
  225. while(1)
  226. {
  227. printf("power gpio is %s\r\n",gpio_get_level(4)?"high":"low");
  228. vTaskDelay(100 / portTICK_PERIOD_MS);
  229. }
  230. #endif
  231. // 设置蜂鸣器声音
  232. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  233. beep_init();
  234. #endif
  235. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  236. font_init();
  237. #endif
  238. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  239. epd_init();
  240. #endif
  241. user_sleep_into(); // 休眠定时器初始化
  242. // charge_init();
  243. // decection_charging_init();
  244. screen_queue = xQueueCreate(25, sizeof(Screen_dis_t));
  245. lora_data_queue = xQueueCreate(20, sizeof(LORA_DATA_T));
  246. yc_data_queue = xQueueCreate(20, sizeof(int));
  247. button_Data_queue = xQueueCreate(10, sizeof(uint8_t));
  248. // screen_semaphore = xSemaphoreCreateMutex();
  249. screen_event = xEventGroupCreate();
  250. // button_semaphore = xSemaphoreCreateMutex();
  251. #if 0
  252. lora_receiveQueue = xQueueCreate(100, sizeof(int));
  253. #else
  254. lora_dealhandle = xQueueCreate(100, sizeof(int));
  255. #endif
  256. sleep_queue = xQueueCreate(10, sizeof(uint8_t));
  257. // Send_Data_queue= xQueueCreate(10, sizeof(uint8_t)); //发送缓存数据任务
  258. if (sleep_queue != NULL)
  259. {
  260. // 创建队列成功,myQueue 包含有效的队列句柄
  261. // 在这里可以继续使用该队列
  262. }
  263. else
  264. {
  265. // 创建队列失败,myQueue 是 NULL
  266. // 需要处理创建失败的情况
  267. printf("---------------create fail-----------------\r\n");
  268. }
  269. wakeup_queue = xQueueCreate(10, sizeof(uint8_t));
  270. if (wakeup_queue != NULL)
  271. {
  272. // 创建队列成功,myQueue 包含有效的队列句柄
  273. // 在这里可以继续使用该队列
  274. }
  275. else
  276. {
  277. // 创建队列失败,myQueue 是 NULL
  278. // 需要处理创建失败的情况
  279. printf("---------------create fail-----------------\r\n");
  280. }
  281. extern unsigned char *last_paint_buf_left;
  282. extern unsigned char *last_paint_buf_right;
  283. extern uint32_t display_size;
  284. #if 0
  285. extern unsigned char *tmp_paint_buf;
  286. tmp_paint_buf = heap_caps_malloc(display_size,MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM); // 开辟 结构体句柄 所需要的空间
  287. if(tmp_paint_buf!=NULL)
  288. {
  289. //printf("tmp_paint_buf malloc success\r\n");
  290. }else
  291. {
  292. printf("tmp_paint_buf malloc fail\r\n");
  293. heap_caps_free(tmp_paint_buf);
  294. }
  295. #endif
  296. #if 1
  297. last_paint_buf_left = heap_caps_malloc(display_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); // 开辟所需要的空间
  298. if (last_paint_buf_left != NULL)
  299. {
  300. // printf("last_paint_buf_left malloc success\r\n");
  301. }
  302. else
  303. {
  304. printf("last_paint_buf_left malloc fail\r\n");
  305. heap_caps_free(last_paint_buf_left);
  306. }
  307. last_paint_buf_right = heap_caps_malloc(display_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); // 开辟所需要的空间
  308. if (last_paint_buf_right != NULL)
  309. {
  310. // printf("last_paint_buf_right malloc success\r\n");
  311. }
  312. else
  313. {
  314. printf("last_paint_buf_right malloc fail\r\n");
  315. heap_caps_free(last_paint_buf_right);
  316. }
  317. #endif
  318. int reson = esp_sleep_get_wakeup_cause(); // 获取当前唤醒原因
  319. if (reson == ESP_SLEEP_WAKEUP_EXT1)
  320. {
  321. adc1_init();
  322. // extern void dis_right_instructions();
  323. // dis_right_instructions();
  324. screen_dis_info.is_left = false;
  325. screen_dis_info.is_into_sleep = true;
  326. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  327. {
  328. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  329. }
  330. int power_key = 0;
  331. int charge_key = 0;
  332. int value_count = 0;
  333. charge_key = gpio_get_level(2);
  334. printf("charge_key is %s\r\n", !charge_key ? "charge in" : "charge out");
  335. printf("charge_key is %s\r\n", !charge_key ? "charge in" : "charge out");
  336. printf("charge_key is %s\r\n", !charge_key ? "charge in" : "charge out");
  337. printf("charge_key is %s\r\n", !charge_key ? "charge in" : "charge out");
  338. Machine_info.batt_precent = read_battery_voltage();
  339. Machine_info.last_batt_precent = Machine_info.batt_precent;
  340. screen_dis_info.is_left = false;
  341. while (1)
  342. {
  343. // value++;
  344. charge_key = gpio_get_level(2);
  345. if (!charge_key) // 充电中
  346. {
  347. Machine_info.batt_precent = read_battery_voltage();
  348. if (Machine_info.batt_precent < 96)
  349. {
  350. user_compare_power_off(Machine_info.last_batt_precent, Machine_info.batt_precent);
  351. printf("charge_key is %s,batt=%d,last batt=%d\r\n", !charge_key ? "charge in" : "charge out", Machine_info.batt_precent, Machine_info.last_batt_precent);
  352. value_count++;
  353. if (value_count > 60)
  354. {
  355. Machine_info.last_batt_precent = Machine_info.batt_precent;
  356. value_count = 0;
  357. }
  358. }
  359. }
  360. else // 未充电
  361. {
  362. printf("charge_key is %s\r\n", !charge_key ? "charge in" : "charge out");
  363. // extern void dis_right_instructions();
  364. // dis_right_instructions();
  365. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  366. {
  367. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  368. }
  369. printf("start power off\r\n");
  370. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  371. #if 1 // 电源按键
  372. // adc_oneshot_del_unit(adc1_handle);
  373. gpio_reset_pin(4);
  374. int ext_wakeup_pin_0 = 4;
  375. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  376. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  377. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  378. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  379. // No need to keep that power domain explicitly, unlike EXT1.
  380. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  381. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  382. gpio_reset_pin(2);
  383. const int ext_wakeup_pin_1 = 2;
  384. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  385. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  386. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  387. #endif
  388. esp_deep_sleep_start();
  389. break;
  390. }
  391. vTaskDelay(1000 / portTICK_PERIOD_MS);
  392. }
  393. }
  394. // xTaskCreate( left_screen_task, "left_screen_task", 25*1024, NULL, configMAX_PRIORITIES - 1, NULL);
  395. // idf.py 设置分区
  396. spiffs_init();
  397. extern uint32_t ulp_wakeup_result;
  398. if (
  399. (reson != ESP_SLEEP_WAKEUP_EXT0) &&
  400. (reson != ESP_SLEEP_WAKEUP_ULP) &&
  401. (reson != ESP_SLEEP_WAKEUP_TIMER))
  402. {
  403. #if PRINT_SPIFFS
  404. printHexData(&default_info, sizeof(Machine_info_t));
  405. // spiffs_write(&default_info);
  406. #endif
  407. spiffs_read_powerOn(&Machine_info);
  408. // 重新初始化开机后默认关机
  409. Machine_info.power_status = 0;
  410. user_nvs_init();
  411. }
  412. if (reson == ESP_SLEEP_WAKEUP_EXT0)
  413. {
  414. #if 0
  415. adc1_init();
  416. int value = 0;
  417. while(1)
  418. {
  419. value++;
  420. adc_read_left_key_pin(adc1_handle);
  421. vTaskDelay(10 / portTICK_PERIOD_MS);
  422. if(value>10)
  423. {
  424. break;
  425. }
  426. }
  427. #else
  428. int power_key = 0;
  429. adc1_init();
  430. while (1)
  431. {
  432. // value++;
  433. power_key = gpio_get_level(4);
  434. vTaskDelay(4000 / portTICK_PERIOD_MS);
  435. if (0 == gpio_get_level(4))
  436. {
  437. if (read_battery_voltage() < 10) // 判断电压小于10% 不让开机
  438. {
  439. printf("start power off\r\n");
  440. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  441. #if 1 // 电源按键
  442. // adc_oneshot_del_unit(adc1_handle);
  443. gpio_reset_pin(4);
  444. int ext_wakeup_pin_0 = 4;
  445. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  446. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  447. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  448. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  449. // No need to keep that power domain explicitly, unlike EXT1.
  450. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  451. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  452. gpio_reset_pin(2);
  453. const int ext_wakeup_pin_1 = 2;
  454. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  455. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  456. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  457. #endif
  458. esp_deep_sleep_start();
  459. }
  460. adc_oneshot_del_unit(adc1_handle); // 删除adc使用
  461. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  462. #if 1 // USER_DEEP_SLEEP_ENABLE
  463. reson = is_wake_up_reson(); // 返回唤醒的原因
  464. ESP_LOGW(LOG_TAG, "power on- reson = %d\r\n", reson);
  465. /* ULP Risc-V read and detected a temperature above the limit */
  466. if (reson == ESP_SLEEP_WAKEUP_EXT0)
  467. {
  468. if (Machine_info.power_status == 0)
  469. {
  470. Machine_info.power_status = 1; // 开机
  471. lora_set_power_level(1); // 打开lora电源
  472. // 恢复串口i功能
  473. uart_sleep_out_config();
  474. beep_blink(1000, 1);
  475. Machine_info.left_current_Quick_refresh_time = 5;
  476. vTaskDelay(1000 / portTICK_PERIOD_MS);
  477. // printf("开机刷屏\r\n");
  478. // if(xQueueSend(screen_queue,&Machine_info,portMAX_DELAY) != true)
  479. // {
  480. // printf("left send fail\r\n");
  481. // }
  482. }
  483. else if (Machine_info.power_status == 1)
  484. {
  485. printf("aready power on\r\n");
  486. }
  487. }
  488. #endif
  489. break;
  490. }
  491. else
  492. {
  493. printf("start power off\r\n");
  494. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  495. #if 1 // 电源按键
  496. // adc_oneshot_del_unit(adc1_handle);
  497. gpio_reset_pin(4);
  498. int ext_wakeup_pin_0 = 4;
  499. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  500. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  501. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  502. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  503. // No need to keep that power domain explicitly, unlike EXT1.
  504. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  505. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  506. gpio_reset_pin(2);
  507. const int ext_wakeup_pin_1 = 2;
  508. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  509. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  510. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  511. #endif
  512. esp_deep_sleep_start();
  513. }
  514. }
  515. #endif
  516. printf("deep Wake up from ext0\n");
  517. }
  518. else if (reson == ESP_SLEEP_WAKEUP_ULP)
  519. {
  520. printf("wakeup_result = %ld\r\n", ulp_wakeup_result);
  521. // int key = find_key_value(ulp_wakeup_result);
  522. // printf("key = %d\r\n",key);
  523. #if 1
  524. // reson = is_wake_up_reson(); //返回唤醒的原因
  525. /* ULP Risc-V read and detected a temperature above the limit */
  526. if (reson == ESP_SLEEP_WAKEUP_ULP)
  527. {
  528. if (Machine_info.power_status == 0)
  529. {
  530. printf("left key press power off\r\n");
  531. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  532. #if 1
  533. font_into_sleep();
  534. #include "EPD.h"
  535. epd_sleep(SCREEN_LEFT);
  536. epd_sleep(SCREEN_RIGHT);
  537. // gpio_hold_en(PIN_L_CS);
  538. // gpio_hold_en(PIN_R_CS);
  539. // gpio_set_level(PIN_L_CS,1);
  540. // gpio_set_level(PIN_R_CS,1);
  541. // gpio_reset_pin(46);
  542. // uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  543. // ESP_ERROR_CHECK(uart_wait_tx_done(UART_NUM_1,portMAX_DELAY));
  544. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
  545. // gpio_set_level(LORA_TXD_PIN, 0);
  546. // gpio_set_level(LORA_RXD_PIN, 0);
  547. // uart_sleep_in_config();
  548. uart_driver_delete(UART_NUM_1);
  549. gpio_reset_pin(LORA_TXD_PIN);
  550. gpio_reset_pin(LORA_RXD_PIN);
  551. gpio_config_t io_conf = {};
  552. io_conf.pin_bit_mask = (1 << LORA_TXD_PIN);
  553. io_conf.mode = GPIO_MODE_OUTPUT;
  554. io_conf.pull_up_en = false;
  555. gpio_config(&io_conf);
  556. io_conf.pin_bit_mask = (1 << LORA_RXD_PIN);
  557. io_conf.mode = GPIO_MODE_OUTPUT;
  558. io_conf.pull_up_en = false;
  559. gpio_config(&io_conf);
  560. gpio_set_level(LORA_TXD_PIN, 0);
  561. gpio_set_level(LORA_RXD_PIN, 0);
  562. gpio_hold_en(LORA_TXD_PIN);
  563. gpio_hold_en(LORA_RXD_PIN);
  564. // 电源按键
  565. // adc_oneshot_del_unit(adc1_handle);
  566. gpio_reset_pin(4);
  567. int ext_wakeup_pin_0 = 4;
  568. // printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  569. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  570. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  571. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  572. // No need to keep that power domain explicitly, unlike EXT1.
  573. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  574. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  575. // 充电按键
  576. gpio_reset_pin(2);
  577. const int ext_wakeup_pin_1 = 2;
  578. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  579. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  580. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  581. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  582. ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
  583. #endif
  584. esp_deep_sleep_start();
  585. }
  586. else
  587. {
  588. int btn = left_adc_wake_btn_send();
  589. ESP_LOGE(LOG_TAG, "btn = %d", btn);
  590. Machine_info.left_state = btn;
  591. beep_blink(30, 1);
  592. #if 1
  593. screen_dis_info.is_left = true;
  594. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  595. {
  596. printf("left send fail\r\n");
  597. }
  598. #endif
  599. while (1)
  600. {
  601. vTaskDelay(20 / portTICK_PERIOD_MS);
  602. }
  603. }
  604. }
  605. #endif
  606. }
  607. #if 0
  608. // info_init();
  609. // board_init();
  610. #else
  611. #if USER_DEEP_SLEEP_ENABLE
  612. int reson = is_wake_up_reson(); // 返回唤醒的原因
  613. printf(" info_init reson = %d\r\n", reson);
  614. // idf.py 设置分区
  615. spiffs_init();
  616. /* not a wakeup from ULP, load the firmware */
  617. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER))
  618. {
  619. #if 1
  620. // uint8_t yc_name[6] = {0xd2,0xcf,0xb3,0xb2,0x00,0x00};
  621. Machine_info_t default_info = {
  622. .left_display_mode = 0,
  623. //.left_state =1,
  624. .eflagID = 0xFF,
  625. .rssi = 0x64,
  626. .refresh_cycle = 2,
  627. .batt_precent = 100,
  628. };
  629. spiffs_write(&default_info);
  630. #endif
  631. spiffs_read(&Machine_info);
  632. }
  633. else
  634. {
  635. }
  636. #endif
  637. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  638. lora_init();
  639. #endif
  640. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  641. adc1_init();
  642. #endif
  643. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  644. button_init(adc1_handle); // 左侧按键
  645. power_button_init(adc1_handle); // 右侧电源按键
  646. Machine_info.batt_precent = read_battery_voltage();
  647. Machine_info.last_batt_precent = Machine_info.batt_precent;
  648. #endif
  649. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  650. // timer_init();
  651. #endif
  652. //////////////////////////////////////////////////////////////////////////////////////////////////
  653. #endif
  654. xTaskCreate(button_task, "button_task", 5 * 2048, NULL, configMAX_PRIORITIES, NULL);
  655. // xTaskCreate( right_screen_task, "right_screen_task",15*1024, NULL, configMAX_PRIORITIES - 2, NULL);
  656. xTaskCreate(screen_task, "screen_task", 40 * 1024, NULL, configMAX_PRIORITIES - 2, NULL);
  657. printf("=================================UPDATE OK===========================\r\n");
  658. #if 1
  659. xTaskCreate(business_logic_task, "business_logic_task", 25 * 1024, NULL, YC_TASK_NONE, NULL);
  660. // xTaskCreate( unpack_task, "unpack_task", 5*1024, NULL, YC_TASK_UNPACK, NULL);
  661. #if !USER_QIXIN //
  662. xTaskCreate(lora_task, "lora_task", 5 * 1024, NULL, configMAX_PRIORITIES, NULL);
  663. #endif
  664. #if 0
  665. xTaskCreate(gui_task, "gui_task", 4096 * 2, NULL, 5, NULL);
  666. #endif
  667. #if 1 // 任务处理函数
  668. xTaskCreate(read_deal_data_callback_handler, "read_deal_data_task", 1024 * 8, NULL, configMAX_PRIORITIES, NULL);
  669. #endif
  670. // 添加定时发送当前是否有链表数据
  671. // xTaskCreate(Sendlist_task_callback_handler, "Sendlist_task", 1024*8, NULL, configMAX_PRIORITIES, NULL);
  672. #endif
  673. // 打印系统信息
  674. print_systenInfo();
  675. }
  676. static void screen_task(void *arg)
  677. {
  678. Machine_info.rssi = 100; // 没有获取到真数据,默认100
  679. int reson = is_wake_up_reson(); // 返回唤醒的原因
  680. ESP_LOGW(LOG_TAG, "screen_task %d", reson);
  681. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER) && (reson != ESP_SLEEP_WAKEUP_EXT0))
  682. {
  683. // Paint_leftScreen_main_powerOn();
  684. // Paint_rightScreen_main_powerON();
  685. Paint_leftScreen(Machine_info.power_status, false);
  686. Paint_rightScreen(Machine_info.power_status, false);
  687. }
  688. else if (reson == ESP_SLEEP_WAKEUP_EXT0)
  689. {
  690. // 按键从深睡唤醒,开机刷新屏慕
  691. ESP_LOGW(LOG_TAG, "按键从深睡唤醒,开机刷新屏慕");
  692. // Paint_leftScreen_main_slow(&Machine_info);
  693. // Paint_rightScreen_main_slow(&Machine_info);
  694. Paint_leftScreen(Machine_info.power_status, false);
  695. Paint_rightScreen(Machine_info.power_status, false);
  696. }
  697. Screen_dis_t screen_rev_info;
  698. screen_rev_info.is_left = false;
  699. screen_rev_info.is_into_sleep = true;
  700. Machine_info.left_current_Quick_refresh_time = 1; // 开机慢刷
  701. while (1)
  702. {
  703. if (xQueueReceive(screen_queue, &screen_rev_info, (TickType_t)portMAX_DELAY))
  704. {
  705. font_exit_sleep();
  706. ESP_LOGI(LOG_TAG, "%s , %s", screen_rev_info.is_left ? "左屏刷新" : "右屏刷新", screen_rev_info.is_into_sleep ? "进入睡眠" : "不进睡眠");
  707. if (screen_rev_info.is_left) // 刷左屏
  708. {
  709. if (Machine_info.left_current_Quick_refresh_time >= Machine_info.left_max_Quick_refresh_time)
  710. {
  711. Machine_info.left_current_Quick_refresh_time = 0;
  712. Paint_leftScreen(Machine_info.power_status, false);
  713. }
  714. else
  715. {
  716. Machine_info.left_current_Quick_refresh_time++;
  717. Paint_leftScreen(Machine_info.power_status, true);
  718. }
  719. }
  720. else // 刷右屏
  721. {
  722. if (Machine_info.right_current_Quick_refresh_time == 0)
  723. {
  724. Machine_info.right_current_Quick_refresh_time = 1;
  725. Paint_rightScreen(Machine_info.power_status, true);
  726. }
  727. else
  728. {
  729. Paint_rightScreen(Machine_info.power_status, false);
  730. }
  731. }
  732. if ((Machine_info.power_status == 1) && (Machine_info.paired == 1) && screen_rev_info.is_into_sleep)
  733. {
  734. ESP_LOGW(LOG_TAG, "刷屏进入休眠 %s", screen_rev_info.is_left ? "左屏刷新" : "右屏刷新");
  735. sleep_timer_start(100); // 进入睡眠
  736. }
  737. // if(Machine_info.power_status == 0 )//展示说明书都是先左后右,等右屏刷完再休眠
  738. // {
  739. // printf("=>screen deep sleep\r\n");
  740. // // uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  741. // // #if 1 //电源按键
  742. // // gpio_reset_pin(4);
  743. // // int ext_wakeup_pin_0 = 4;
  744. // // printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  745. // // ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  746. // // // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  747. // // // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  748. // // // No need to keep that power domain explicitly, unlike EXT1.
  749. // // ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  750. // // ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  751. // // esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  752. // // ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup());
  753. // // #endif
  754. // // esp_deep_sleep_start();
  755. // }
  756. }
  757. }
  758. }
  759. static void button_task(void *arg)
  760. {
  761. // update_last_button_info(Machine_info.current_button.button_info);//初始化上个按键为运行,用作paint0703计时
  762. uint8_t button_info;
  763. while (1)
  764. {
  765. if (xQueueReceive(button_Data_queue, &button_info, (TickType_t)portMAX_DELAY))
  766. {
  767. #if 1
  768. ESP_LOGI(LOG_TAG, "btn_flag[%d][%d][%d][%d][%d][%d],button_info = [%d]%s ", Machine_info.btn_dis_flag[0],
  769. Machine_info.btn_dis_flag[1], Machine_info.btn_dis_flag[2], Machine_info.btn_dis_flag[3], Machine_info.btn_dis_flag[4],
  770. Machine_info.btn_dis_flag[5], button_info, Machine_info.power_status ? "开机" : "关机");
  771. ESP_LOGI(LOG_TAG, "batt_precent[%d]\ncid[%s]\nlast_button[%d]\nlora_new_channel[%d]\neflagID[%d]\nDuration_time[%ld]rssi[%d]paired[%s]",
  772. Machine_info.batt_precent,
  773. Machine_info.cid,
  774. Machine_info.last_button.button_info,
  775. Machine_info.lora_new_channel,
  776. Machine_info.eflagID,
  777. Machine_info.Duration_time,
  778. Machine_info.rssi,
  779. Machine_info.paired ? "已配网" : "未配网");
  780. #endif
  781. if (button_info < 0x12) // 左屏慕按键
  782. {
  783. // vTaskDelay(200/ portTICK_PERIOD_MS);
  784. // print_lora();//按键查询lora配置
  785. if (Machine_info.btn_dis_flag[button_info - 1] == false)
  786. {
  787. printf("按键关闭,默认运行\n");
  788. button_info = STATE_OPERATION; // 按键关闭,默认运行
  789. if ((Machine_info.power_status == 1) && (Machine_info.paired == 1))
  790. {
  791. sleep_timer_start(100); // 进入睡眠
  792. }
  793. }
  794. Machine_info.current_button.button_info = button_info;
  795. // 判断当前的按键状态 设置为当前状态后 再次按键不处理
  796. printf("last btn = %d ,curr btn = %d\n", Machine_info.last_button.button_info, Machine_info.current_button.button_info);
  797. if (Machine_info.last_button.button_info != Machine_info.current_button.button_info)
  798. {
  799. if (Machine_info.paired == 1)
  800. {
  801. // printf("paired add data to list chanl = 0x%02x\r\n",Machine_info.lora_new_channel);
  802. getRtcTime(&Machine_info); // 获取当前时间
  803. // 更新当前时间
  804. Machine_info.current_button.button_info = button_info;
  805. Machine_info.current_button.Year = Machine_info.year;
  806. Machine_info.current_button.Month = Machine_info.month;
  807. Machine_info.current_button.Day = Machine_info.day;
  808. Machine_info.current_button.Hour = Machine_info.hour;
  809. Machine_info.current_button.Minute = Machine_info.min;
  810. Machine_info.current_button.Second = Machine_info.sec;
  811. long long current_Duration_time = calculate_minutes_difference(
  812. Machine_info.last_button.Year,
  813. Machine_info.last_button.Month,
  814. Machine_info.last_button.Day,
  815. Machine_info.last_button.Hour,
  816. Machine_info.last_button.Minute,
  817. Machine_info.last_button.Second,
  818. Machine_info.current_button.Year,
  819. Machine_info.current_button.Month,
  820. Machine_info.current_button.Day,
  821. Machine_info.current_button.Hour,
  822. Machine_info.current_button.Minute,
  823. Machine_info.current_button.Second);
  824. Machine_info.Duration_time = current_Duration_time; // 持续时长
  825. printf("Machine_info.Duration_time = %ld\r\n", Machine_info.Duration_time);
  826. reset_btn_last_time();
  827. #if 0
  828. printf("list before\r\n");
  829. printList(Send_list);
  830. if(Machine_info.Duration_time == 0)
  831. {
  832. deleteNode_head(Send_list);
  833. }
  834. printf("list after\r\n");
  835. printList(Send_list);
  836. #endif
  837. set_status_heights();
  838. Machine_info.Duration_time = 0;
  839. // 更新上次的状态
  840. update_last_button_info(Machine_info.last_button.button_info);
  841. }
  842. else
  843. {
  844. printf("not add data to list\r\n");
  845. }
  846. Machine_info.last_button.button_info = Machine_info.current_button.button_info;
  847. // Machine_info.left_state = button_info;
  848. Machine_info.left_state = Machine_info.current_button.button_info;
  849. #if 1
  850. screen_dis_info.is_left = true;
  851. screen_dis_info.is_into_sleep = true;
  852. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  853. {
  854. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  855. }
  856. #endif
  857. beep_blink(30, 1);
  858. // printList(Send_list);
  859. // printList(list_head);
  860. }
  861. else // 和上个按键重复,唤醒原因为ulp唤醒,不进入休眠
  862. {
  863. printf("和上个按键重复,唤醒原因为ulp唤醒,不进入休眠\r\n");
  864. sleep_timer_start(100); // 开始进入倒计时休眠
  865. }
  866. }
  867. else // 右屏幕按键触发
  868. {
  869. if (button_info == POWER_ON_PRESS_VALUE) // power 开机时短按
  870. {
  871. beep_blink(30, 1);
  872. if ((Machine_info.power_status == 1) && (Machine_info.paired == 1))
  873. {
  874. // 判断当前开机 是否配对 继续执行时间片操作
  875. sleep_timer_start(100); // 开始进入倒计时休眠
  876. }
  877. }
  878. if (button_info == POWER_OFF_PRESS_VALUE) // power 关机时短按
  879. {
  880. sleep_timer_start(100); // 开始进入倒计时休眠
  881. }
  882. if (button_info == POWER_ON_INTO_STATUS_CHANGE_VALUE) // power 长按触发
  883. {
  884. beep_blink(1000, 1);
  885. if (Machine_info.power_status == 1) // 开机状态
  886. {
  887. Machine_info.power_status = 0;
  888. printf("poweron->poweroff\r\n");
  889. }
  890. else if (Machine_info.power_status == 0)
  891. {
  892. printf("power on\r\n");
  893. Machine_info.power_status = 1;
  894. lora_set_power_level(1);
  895. font_exit_sleep();
  896. gpio_hold_dis(LORA_TXD_PIN);
  897. gpio_hold_dis(LORA_RXD_PIN);
  898. gpio_hold_dis(PIN_L_CS);
  899. gpio_hold_dis(PIN_R_CS);
  900. // 恢复串口i功能
  901. uart_sleep_out_config();
  902. }
  903. screen_dis_info.is_left = true;
  904. screen_dis_info.is_into_sleep = false;
  905. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  906. {
  907. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  908. }
  909. screen_dis_info.is_left = false;
  910. screen_dis_info.is_into_sleep = true;
  911. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  912. {
  913. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  914. }
  915. }
  916. if (button_info == POWER_ON_INTO_DIS_RIGHT)
  917. {
  918. screen_dis_info.is_left = true;
  919. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  920. {
  921. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  922. }
  923. screen_dis_info.is_left = false;
  924. screen_dis_info.is_into_sleep = true;
  925. Machine_info.left_current_Quick_refresh_time = 5;
  926. if (xQueueSend(screen_queue, &screen_dis_info, portMAX_DELAY) != true)
  927. {
  928. ESP_LOGE(LOG_TAG, "err:screen queue send fail");
  929. }
  930. }
  931. if (button_info == POWER_ON_INTO_OTA_VALUE) // 进入OTA模式
  932. {
  933. beep_blink(50, 3);
  934. printf("into ota mode\r\n");
  935. #include "iot_button.h"
  936. iot_button_stop();
  937. esp_ble_ota();
  938. }
  939. if (button_info == POWER_ON_INTO_RESET_VALUE) // 进入配网模式
  940. {
  941. beep_blink(2000, 1);
  942. printf("into reset mode\r\n");
  943. font_exit_sleep();
  944. lora_set_power_level(1);
  945. uart_sleep_out_config();
  946. #include "user_sleep.h"
  947. // #include "esp_sleep.h"
  948. extern void sleep_timer_stop();
  949. extern void Already_send_timer_stop();
  950. sleep_timer_stop();
  951. Already_send_timer_stop();
  952. // //修改信道前修改设备ID 及 设备ID
  953. // dymatic_change_device_id(0x00000001);
  954. // dymatic_change_dst_device_id(0xFFFFFFFF);
  955. // //切换信道
  956. // dymatic_change_chanel(Machine_info.lora_factory_channel); //切换信道
  957. // // spiffs_read(&Machine_info);
  958. vTaskDelay(300 / portTICK_PERIOD_MS);
  959. reset_default(true, 1);
  960. reset_lora(LORA_CHANENL);
  961. vTaskDelay(300 / portTICK_PERIOD_MS);
  962. }
  963. }
  964. }
  965. }
  966. }
  967. static void business_logic_task(void *arg)
  968. {
  969. // YC_DATA_T tmp;
  970. int len = 0;
  971. bool is_refresh = false;
  972. for (;;)
  973. {
  974. if (xQueueReceive(yc_data_queue, &len, (TickType_t)portMAX_DELAY))
  975. {
  976. #if 1
  977. is_refresh = subcontract(&yc_data);
  978. if (is_refresh)
  979. {
  980. screen_display();
  981. }
  982. else
  983. {
  984. printf("not display fresh\r\n");
  985. }
  986. #endif
  987. free(yc_data.data); // 释放内存
  988. yc_data.data = NULL;
  989. yc_data.len = 0;
  990. yc_data.index = 0;
  991. }
  992. }
  993. }
  994. static void unpack_task(void *arg)
  995. {
  996. LORA_DATA_T tmp_data;
  997. extern esp_timer_handle_t lora_timer;
  998. for (;;)
  999. {
  1000. if (xQueueReceive(lora_data_queue, &tmp_data, (TickType_t)portMAX_DELAY))
  1001. {
  1002. if (!esp_timer_is_active(lora_timer))
  1003. {
  1004. lora_timer_start();
  1005. }
  1006. else
  1007. {
  1008. lora_timer_restart();
  1009. }
  1010. yc_data.len += tmp_data.data_len;
  1011. memcpy(yc_data.data + yc_data.index, tmp_data.data_buf, tmp_data.data_len);
  1012. yc_data.index += tmp_data.data_len;
  1013. }
  1014. }
  1015. }
  1016. static void lora_task(void *arg)
  1017. {
  1018. lora_event_task(arg);
  1019. }
  1020. // /*********************************************************************************
  1021. // * function : Sendlist_task_callback_handler
  1022. // * Description : 发送处理链表函数
  1023. // * Input :
  1024. // * Output :
  1025. // * Author : 祁鑫 Data : 2023 9.12
  1026. // **********************************************************************************/
  1027. // void Sendlist_task_callback_handler()
  1028. // {
  1029. // int length = 0;
  1030. // uint8_t* result = (uint8_t*) malloc(buffer_size+1);
  1031. // TickType_t xLastWakeTime;
  1032. // #if 0
  1033. // const TickType_t xFrequency = 400/10; // 定时通知的间隔
  1034. // #else
  1035. // const TickType_t xFrequency = 500/10; // 定时通知的间隔
  1036. // #endif
  1037. // // 初始化xLastWakeTime
  1038. // xLastWakeTime = xTaskGetTickCount();
  1039. // int receive_times = 0;
  1040. // int user_size = 0;
  1041. // int result_length = 0;
  1042. // //int result_length = 0;
  1043. // static int result_index = 0;
  1044. // int result_data_len = 0;
  1045. // while (1) {
  1046. // #if 0
  1047. // //定时通知数据处理任务有新数据可用
  1048. // vTaskDelayUntil(&xLastWakeTime, xFrequency);
  1049. // #endif
  1050. // if(xQueueReceive(Send_Data_queue, &length, portMAX_DELAY) == pdPASS)
  1051. // {
  1052. // //int len = countNodes(Send_list); /* returns the number of nodes in the list */
  1053. // Node *list = Send_list; //发送数据链表
  1054. // int len = countNodes(Send_list); /* returns the number of nodes in the list */
  1055. // if(len!=0)
  1056. // {
  1057. // printf("current wait send num data=%d\r\n",len);
  1058. // #if 1
  1059. // //int len = countNodes(list); /* returns the number of nodes in the list */
  1060. // while(len)
  1061. // {
  1062. // //int busy = get_lora_busy_pin();
  1063. // //printf("busy = %d\r\n",busy);
  1064. // printf("send times\r\n");
  1065. // //if()
  1066. // lora_send_data((char *)list->data,list->len);
  1067. // list=list->next;
  1068. // len--;
  1069. // }
  1070. // //Send_list = deleteList(Send_list);
  1071. // free(list);
  1072. // #endif
  1073. // }
  1074. // #if 0
  1075. // for (int i = 0; i < len; i++)
  1076. // {
  1077. // printf("%02x",result[i]);
  1078. // }
  1079. // #endif
  1080. // }
  1081. // }
  1082. // }
  1083. /*********************************************************************************
  1084. * function : uart_task_callback_handler
  1085. * Description : 串口0函数
  1086. * Input :
  1087. * Output :
  1088. * Author : 祁鑫 Data : 2023 9.12
  1089. **********************************************************************************/
  1090. void uart_task_callback_handler()
  1091. {
  1092. // #include "driver/uart.h"
  1093. // uint8_t dtmp[200]= {0};
  1094. // while (1)
  1095. // {
  1096. // printf("uart 0 rev = ");
  1097. // int len = uart_read_bytes(UART_NUM_0, dtmp, 1024,200/portTICK_PERIOD_MS);
  1098. // if(len)
  1099. // {
  1100. // for(int i = 0;i<len;i++)
  1101. // printf("%02X",dtmp[i]);
  1102. // printf("\r\n");
  1103. // }
  1104. // printf("\r\n");
  1105. // vTaskDelay(1000 / portTICK_PERIOD_MS);
  1106. // }
  1107. }
  1108. /*********************************************************************************
  1109. * function : read_deal_data_callback_handler
  1110. * Description : 定时处理函数
  1111. * Input :
  1112. * Output :
  1113. * Author : 祁鑫 Data : 2023 9.12
  1114. **********************************************************************************/
  1115. void read_deal_data_callback_handler()
  1116. {
  1117. #if 0
  1118. int length = 0;
  1119. uint8_t* result = (uint8_t*) malloc(buffer_size+1);
  1120. TickType_t xLastWakeTime;
  1121. #if 1
  1122. const TickType_t xFrequency = 100/10; // 定时通知的间隔
  1123. #else
  1124. const TickType_t xFrequency = 500; // 定时通知的间隔
  1125. #endif
  1126. // 初始化xLastWakeTime
  1127. xLastWakeTime = xTaskGetTickCount();
  1128. int receive_times = 0;
  1129. int user_size = 0;
  1130. int result_length = 0;
  1131. //int result_length = 0;
  1132. static int result_index = 0;
  1133. int result_data_len = 0;
  1134. #include "y_ringbuf.h"
  1135. extern struct RINGBUF_st;
  1136. extern RINGBUF_st *lora_ringbuf;
  1137. while (1) {
  1138. // 定时通知数据处理任务有新数据可用
  1139. vTaskDelayUntil(&xLastWakeTime, xFrequency);
  1140. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  1141. #if USER_OTA
  1142. if(user_size>=0)
  1143. #endif
  1144. if(user_size>0)
  1145. {
  1146. #if 0
  1147. for (int i = 0; i < len; i++)
  1148. {
  1149. printf("%02x",result[i]);
  1150. }
  1151. #endif
  1152. printf("times comming length=%d\r\n",user_size);
  1153. if(user_size == 36)
  1154. {
  1155. y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1156. }
  1157. #if 1
  1158. int len = y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1159. yc_data.len = user_size;
  1160. yc_data.data = malloc(sizeof(uint8_t)*user_size);//分配内存
  1161. memcpy(yc_data.data,result,user_size);
  1162. //lora_send_queue_callback(result,len); //发送消息处理函数
  1163. if(xQueueSend(yc_data_queue,&len,0) != true)
  1164. {
  1165. ESP_LOGE(LOG_TAG,"yc_data_queue send is fail");
  1166. }
  1167. #endif
  1168. #if USER_OTA
  1169. if((user_size>1024)||(user_size == 0) || (((user_size<1024))&&(is_ota ==true)) )
  1170. if(xQueueSend(ota_queue,&user_size,0) != true)
  1171. {
  1172. ESP_LOGE(LOG_TAG,"ota_queue send is fail");
  1173. }
  1174. #endif
  1175. }
  1176. }
  1177. #else
  1178. int length = 0;
  1179. uint8_t *result = (uint8_t *)malloc(buffer_size + 1);
  1180. int user_size = 0;
  1181. while (1)
  1182. {
  1183. if (xQueueReceive(lora_dealhandle, &length, portMAX_DELAY) == pdPASS)
  1184. {
  1185. if (rssi != 0)
  1186. {
  1187. Machine_info.rssi = rssi;
  1188. }
  1189. // printf("deal data\r\n");
  1190. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  1191. if (user_size > 0)
  1192. {
  1193. printf("lora 数据长度 = %d\r\n", user_size);
  1194. #if 0
  1195. if(user_size == 36)
  1196. {
  1197. y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1198. #if 1
  1199. for (int i = 0; i < user_size; i++)
  1200. {
  1201. printf("%02x",result[i]);
  1202. }
  1203. #endif
  1204. }else
  1205. #endif
  1206. {
  1207. #if 1
  1208. int len = y_ringbuf_read_clear(lora_ringbuf, result, user_size); // 读取并清除数据
  1209. #if 0
  1210. for (int i = 0; i < user_size; i++)
  1211. {
  1212. printf("%02x",result[i]);
  1213. }
  1214. printf("\r\n");
  1215. #endif
  1216. ESP_LOG_BUFFER_HEX("接收的lora", result, user_size);
  1217. yc_data.len = user_size;
  1218. yc_data.data = malloc(sizeof(uint8_t) * user_size); // 分配内存
  1219. memcpy(yc_data.data, result, user_size);
  1220. // lora_send_queue_callback(result,len); //发送消息处理函数
  1221. if (xQueueSend(yc_data_queue, &len, (TickType_t)portMAX_DELAY) != true)
  1222. {
  1223. ESP_LOGE(LOG_TAG, "yc_data_queue send is fail");
  1224. }
  1225. #endif
  1226. }
  1227. }
  1228. }
  1229. }
  1230. #endif
  1231. }