yc_terminal.c 50 KB

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