yc_terminal.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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\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. lora_set_power_level(1); // 打开lora电源
  465. // 恢复串口i功能
  466. uart_sleep_out_config();
  467. beep_blink(1000, 1);
  468. Machine_info.left_current_Quick_refresh_time = 5;
  469. vTaskDelay(1000 / portTICK_PERIOD_MS);
  470. // printf("开机刷屏\r\n");
  471. // if(xQueueSend(screen_queue,&Machine_info,portMAX_DELAY) != true)
  472. // {
  473. // printf("left send fail\r\n");
  474. // }
  475. }
  476. else if (Machine_info.power_status == 1)
  477. {
  478. printf("aready power on\r\n");
  479. }
  480. }
  481. #endif
  482. break;
  483. }
  484. else
  485. {
  486. printf("start power off\r\n");
  487. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  488. #if 1 // 电源按键
  489. // adc_oneshot_del_unit(adc1_handle);
  490. gpio_reset_pin(4);
  491. int ext_wakeup_pin_0 = 4;
  492. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  493. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  494. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  495. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  496. // No need to keep that power domain explicitly, unlike EXT1.
  497. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  498. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  499. gpio_reset_pin(2);
  500. const int ext_wakeup_pin_1 = 2;
  501. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  502. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  503. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  504. #endif
  505. esp_deep_sleep_start();
  506. }
  507. }
  508. #endif
  509. printf("deep Wake up from ext0\n");
  510. }
  511. else if (reson == ESP_SLEEP_WAKEUP_ULP)
  512. {
  513. printf("wakeup_result = %ld\r\n", ulp_wakeup_result);
  514. // int key = find_key_value(ulp_wakeup_result);
  515. // printf("key = %d\r\n",key);
  516. #if 1
  517. // reson = is_wake_up_reson(); //返回唤醒的原因
  518. /* ULP Risc-V read and detected a temperature above the limit */
  519. if (reson == ESP_SLEEP_WAKEUP_ULP)
  520. {
  521. if (Machine_info.power_status == 0)
  522. {
  523. ESP_LOGW(LOG_TAG,"left key press power off\r\n");
  524. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  525. #if 1
  526. font_into_sleep();
  527. #include "EPD.h"
  528. epd_sleep(SCREEN_LEFT);
  529. epd_sleep(SCREEN_RIGHT);
  530. // gpio_hold_en(PIN_L_CS);
  531. // gpio_hold_en(PIN_R_CS);
  532. // gpio_set_level(PIN_L_CS,1);
  533. // gpio_set_level(PIN_R_CS,1);
  534. // gpio_reset_pin(46);
  535. // uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  536. // ESP_ERROR_CHECK(uart_wait_tx_done(UART_NUM_1,portMAX_DELAY));
  537. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
  538. // gpio_set_level(LORA_TXD_PIN, 0);
  539. // gpio_set_level(LORA_RXD_PIN, 0);
  540. // uart_sleep_in_config();
  541. uart_driver_delete(UART_NUM_1);
  542. gpio_reset_pin(LORA_TXD_PIN);
  543. gpio_reset_pin(LORA_RXD_PIN);
  544. gpio_config_t io_conf = {};
  545. io_conf.pin_bit_mask = (1 << LORA_TXD_PIN);
  546. io_conf.mode = GPIO_MODE_OUTPUT;
  547. io_conf.pull_up_en = false;
  548. gpio_config(&io_conf);
  549. io_conf.pin_bit_mask = (1 << LORA_RXD_PIN);
  550. io_conf.mode = GPIO_MODE_OUTPUT;
  551. io_conf.pull_up_en = false;
  552. gpio_config(&io_conf);
  553. gpio_set_level(LORA_TXD_PIN, 0);
  554. gpio_set_level(LORA_RXD_PIN, 0);
  555. gpio_hold_en(LORA_TXD_PIN);
  556. gpio_hold_en(LORA_RXD_PIN);
  557. // 电源按键
  558. // adc_oneshot_del_unit(adc1_handle);
  559. gpio_reset_pin(4);
  560. int ext_wakeup_pin_0 = 4;
  561. // printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  562. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  563. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  564. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  565. // No need to keep that power domain explicitly, unlike EXT1.
  566. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  567. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  568. // 充电按键
  569. gpio_reset_pin(2);
  570. const int ext_wakeup_pin_1 = 2;
  571. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  572. printf("Enabling EXT1 wakeup on pins GPIO%d\r\n", ext_wakeup_pin_1);
  573. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  574. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  575. ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
  576. #endif
  577. printf("esp_deep_sleep_start\r\n");
  578. esp_deep_sleep_start();
  579. }
  580. else
  581. {
  582. int btn = left_adc_wake_btn_send();
  583. ESP_LOGE(LOG_TAG, "btn = %d", btn);
  584. Machine_info.left_state = btn;
  585. beep_blink(30, 1);
  586. #if 1
  587. set_screen_dis_info_and_send_queue(true, true, false, false, 100);
  588. #endif
  589. while (1)
  590. {
  591. vTaskDelay(20 / portTICK_PERIOD_MS);
  592. }
  593. }
  594. }
  595. #endif
  596. }
  597. #if 0
  598. // info_init();
  599. // board_init();
  600. #else
  601. #if USER_DEEP_SLEEP_ENABLE
  602. int reson = is_wake_up_reson(); // 返回唤醒的原因
  603. printf(" info_init reson = %d\r\n", reson);
  604. // idf.py 设置分区
  605. spiffs_init();
  606. /* not a wakeup from ULP, load the firmware */
  607. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER))
  608. {
  609. #if 1
  610. // uint8_t yc_name[6] = {0xd2,0xcf,0xb3,0xb2,0x00,0x00};
  611. Machine_info_t default_info = {
  612. .left_display_mode = 0,
  613. //.left_state =1,
  614. .eflagID = 0xFF,
  615. .rssi = 0x64,
  616. .refresh_cycle = 2,
  617. .batt_precent = 100,
  618. };
  619. spiffs_write(&default_info);
  620. #endif
  621. spiffs_read(&Machine_info);
  622. }
  623. else
  624. {
  625. }
  626. #endif
  627. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  628. lora_init();
  629. #endif
  630. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  631. adc1_init();
  632. #endif
  633. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  634. button_init(adc1_handle); // 左侧按键
  635. power_button_init(adc1_handle); // 右侧电源按键
  636. Machine_info.batt_precent = read_battery_voltage();
  637. Machine_info.last_batt_precent = Machine_info.batt_precent;
  638. #endif
  639. #if USER_NOT_SLEEP_ENABLE || USER_LIGHT_SLEEP_ENABLE || USER_DEEP_SLEEP_ENABLE
  640. // timer_init();
  641. #endif
  642. //////////////////////////////////////////////////////////////////////////////////////////////////
  643. #endif
  644. xTaskCreate(button_task, "button_task", 5 * 2048, NULL, configMAX_PRIORITIES, NULL);
  645. // xTaskCreate( right_screen_task, "right_screen_task",15*1024, NULL, configMAX_PRIORITIES - 2, NULL);
  646. xTaskCreate(screen_task, "screen_task", 40 * 1024, NULL, configMAX_PRIORITIES - 2, NULL);
  647. printf("=================================UPDATE OK===========================\r\n");
  648. #if 1
  649. xTaskCreate(business_logic_task, "business_logic_task", 25 * 1024, NULL, YC_TASK_NONE, NULL);
  650. // xTaskCreate( unpack_task, "unpack_task", 5*1024, NULL, YC_TASK_UNPACK, NULL);
  651. #if !USER_QIXIN //
  652. xTaskCreate(lora_task, "lora_task", 5 * 1024, NULL, configMAX_PRIORITIES, NULL);
  653. #endif
  654. #if 0
  655. xTaskCreate(gui_task, "gui_task", 4096 * 2, NULL, 5, NULL);
  656. #endif
  657. #if 1 // 任务处理函数
  658. xTaskCreate(read_deal_data_callback_handler, "read_deal_data_task", 1024 * 8, NULL, configMAX_PRIORITIES, NULL);
  659. #endif
  660. // 添加定时发送当前是否有链表数据
  661. // xTaskCreate(Sendlist_task_callback_handler, "Sendlist_task", 1024*8, NULL, configMAX_PRIORITIES, NULL);
  662. #endif
  663. // 打印系统信息
  664. print_systenInfo();
  665. }
  666. static void screen_task(void *arg)
  667. {
  668. // Machine_info.rssi = 100; // 没有获取到真数据,默认100
  669. int reson = is_wake_up_reson(); // 返回唤醒的原因
  670. ESP_LOGW(LOG_TAG, "screen_task %d", reson);
  671. if ((reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER) && (reson != ESP_SLEEP_WAKEUP_EXT0))
  672. {
  673. Paint_leftScreen(Machine_info.power_status, false);
  674. Paint_rightScreen(Machine_info.power_status, false);
  675. }
  676. else if (reson == ESP_SLEEP_WAKEUP_EXT0)
  677. {
  678. ESP_LOGW(LOG_TAG, "按键从深睡唤醒,开机刷新屏慕");
  679. Paint_leftScreen(Machine_info.power_status, false);
  680. Paint_rightScreen(Machine_info.power_status, false);
  681. }
  682. Screen_dis_t screen_rev_info;
  683. screen_rev_info.is_left = false;
  684. screen_rev_info.is_into_sleep = true;
  685. while (1)
  686. {
  687. if (xQueueReceive(screen_queue, &screen_rev_info, (TickType_t)portMAX_DELAY))
  688. {
  689. 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",
  690. screen_rev_info.is_change_power ? "change power" : "not change power", screen_rev_info.is_dont_dis ? "dont dis" : "dis");
  691. if (screen_rev_info.is_dont_dis)
  692. {
  693. ESP_LOGE(LOG_TAG, "goto -> dont_dis");
  694. goto dont_dis;
  695. }
  696. font_exit_sleep();
  697. is_epd_ok_cplt = false;
  698. if (screen_rev_info.is_left) // 刷左屏
  699. {
  700. if (screen_rev_info.is_change_power)
  701. {
  702. ESP_LOGE(LOG_TAG, "power_status:%s", Machine_info.power_status ? "now[poweron],after[poweroff]" : "now[poweroff],after[poweron]");
  703. Machine_info.power_status = !Machine_info.power_status; // 关机
  704. }
  705. if (Machine_info.left_current_Quick_refresh_time >= Machine_info.left_max_Quick_refresh_time)
  706. {
  707. Machine_info.left_current_Quick_refresh_time = 0;
  708. Paint_leftScreen(Machine_info.power_status, false);
  709. }
  710. else
  711. {
  712. Machine_info.left_current_Quick_refresh_time++;
  713. Paint_leftScreen(Machine_info.power_status, true);
  714. }
  715. }
  716. else // 刷右屏
  717. {
  718. if (Machine_info.right_current_Quick_refresh_time == 0)
  719. {
  720. Machine_info.right_current_Quick_refresh_time = 1;
  721. Paint_rightScreen(Machine_info.power_status, true);
  722. }
  723. else
  724. {
  725. Paint_rightScreen(Machine_info.power_status, false);
  726. }
  727. }
  728. is_epd_ok_cplt = true;
  729. dont_dis:
  730. // ESP_LOGE(LOG_TAG, "%d %d %d", Machine_info.power_status, Machine_info.paired, screen_rev_info.is_into_sleep);
  731. if ((Machine_info.power_status == 1) /*&& (Machine_info.paired == 1) */ && screen_rev_info.is_into_sleep)
  732. {
  733. ESP_LOGW(LOG_TAG, "%dms sleep %s", screen_rev_info.sleep_ms, screen_rev_info.is_left ? "left" : "right");
  734. sleep_timer_start(screen_rev_info.sleep_ms); // 进入睡眠
  735. }
  736. if (Machine_info.power_status == 0) // 展示说明书都是先左后右,等右屏刷完再休眠
  737. {
  738. ESP_LOGW(LOG_TAG, "now is power off");
  739. sleep_timer_start(1000); // 进入睡眠
  740. }
  741. // ESP_LOGW(LOG_TAG, "screen task over");
  742. }
  743. }
  744. }
  745. static void button_task(void *arg)
  746. {
  747. // update_last_button_info(Machine_info.current_button.button_info);//初始化上个按键为运行,用作paint0703计时
  748. uint8_t button_info;
  749. while (1)
  750. {
  751. if (xQueueReceive(button_Data_queue, &button_info, (TickType_t)portMAX_DELAY))
  752. {
  753. #if 1
  754. ESP_LOGI(LOG_TAG, "btn_flag[%d][%d][%d][%d][%d][%d],button_info = [%d]%s ", Machine_info.btn_dis_flag[0],
  755. Machine_info.btn_dis_flag[1], Machine_info.btn_dis_flag[2], Machine_info.btn_dis_flag[3], Machine_info.btn_dis_flag[4],
  756. Machine_info.btn_dis_flag[5], button_info, Machine_info.power_status ? "poweron" : "poweroff");
  757. 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]",
  758. Machine_info.batt_precent,
  759. Machine_info.cid,
  760. Machine_info.last_button.button_info,
  761. Machine_info.lora_new_channel,
  762. Machine_info.eflagID,
  763. Machine_info.Duration_time,
  764. Machine_info.rssi,
  765. Machine_info.paired ? "paired" : "not pair");
  766. // print_lora_set_info();
  767. #endif
  768. if (button_info < 0x12) // 左屏慕按键
  769. {
  770. // vTaskDelay(200/ portTICK_PERIOD_MS);
  771. // print_lora(); // 按键查询lora配置
  772. if (Machine_info.btn_dis_flag[button_info - 1] == false)
  773. {
  774. printf("btn is close\n");
  775. button_info = STATE_OPERATION; // 按键关闭,默认运行
  776. if ((Machine_info.power_status == 1) && (Machine_info.paired == 1))
  777. {
  778. set_screen_dis_info_and_send_queue(false, false, false, true, 100);
  779. }
  780. }
  781. Machine_info.current_button.button_info = button_info;
  782. // 判断当前的按键状态 设置为当前状态后 再次按键不处理
  783. printf("last btn = %d ,curr btn = %d\n", Machine_info.last_button.button_info, Machine_info.current_button.button_info);
  784. if (Machine_info.last_button.button_info != Machine_info.current_button.button_info)
  785. {
  786. if (Machine_info.paired == 1)
  787. {
  788. // printf("paired add data to list chanl = 0x%02x\r\n",Machine_info.lora_new_channel);
  789. getRtcTime(&Machine_info); // 获取当前时间
  790. // 更新当前时间
  791. Machine_info.current_button.button_info = button_info;
  792. Machine_info.current_button.Year = Machine_info.year;
  793. Machine_info.current_button.Month = Machine_info.month;
  794. Machine_info.current_button.Day = Machine_info.day;
  795. Machine_info.current_button.Hour = Machine_info.hour;
  796. Machine_info.current_button.Minute = Machine_info.min;
  797. Machine_info.current_button.Second = Machine_info.sec;
  798. long long current_Duration_time = calculate_minutes_difference(
  799. Machine_info.last_button.Year,
  800. Machine_info.last_button.Month,
  801. Machine_info.last_button.Day,
  802. Machine_info.last_button.Hour,
  803. Machine_info.last_button.Minute,
  804. Machine_info.last_button.Second,
  805. Machine_info.current_button.Year,
  806. Machine_info.current_button.Month,
  807. Machine_info.current_button.Day,
  808. Machine_info.current_button.Hour,
  809. Machine_info.current_button.Minute,
  810. Machine_info.current_button.Second);
  811. Machine_info.Duration_time = current_Duration_time; // 持续时长
  812. printf("Machine_info.Duration_time = %ld\r\n", Machine_info.Duration_time);
  813. reset_btn_last_time();
  814. #if 0
  815. printf("list before\r\n");
  816. printList(Send_list);
  817. if(Machine_info.Duration_time == 0)
  818. {
  819. deleteNode_head(Send_list);
  820. }
  821. printf("list after\r\n");
  822. printList(Send_list);
  823. #endif
  824. set_status_heights();
  825. // 更新上次的状态
  826. update_last_button_info(Machine_info.last_button.button_info);
  827. }
  828. else
  829. {
  830. printf("not add data to list\r\n");
  831. }
  832. Machine_info.last_button.button_info = Machine_info.current_button.button_info;
  833. // Machine_info.left_state = button_info;
  834. Machine_info.left_state = Machine_info.current_button.button_info;
  835. #if 1
  836. set_screen_dis_info_and_send_queue(true, true, false, false, 100);
  837. #endif
  838. beep_blink(30, 1);
  839. // printList(Send_list);
  840. // printList(list_head);
  841. }
  842. else // 和上个按键重复,唤醒原因为ulp唤醒,不进入休眠
  843. {
  844. printf("repeat ,goto sleep !\r\n");
  845. // sleep_timer_start(100); // 开始进入倒计时休眠
  846. set_screen_dis_info_and_send_queue(true, false, false, true, 100);
  847. }
  848. }
  849. else // 右屏幕按键触发
  850. {
  851. if (button_info == POWER_ON_PRESS_VALUE) // power 开机时短按
  852. {
  853. beep_blink(30, 1);
  854. if ((Machine_info.power_status == 1) && (Machine_info.paired == 1))
  855. {
  856. // 判断当前开机 是否配对 继续执行时间片操作
  857. // sleep_timer_start(100); // 开始进入倒计时休眠
  858. set_screen_dis_info_and_send_queue(false, false, false, true, 1000);
  859. }
  860. }
  861. // if (button_info == POWER_OFF_PRESS_VALUE) // power 关机时短按
  862. // {
  863. // sleep_timer_start(100); // 开始进入倒计时休眠
  864. // }
  865. if (button_info == POWER_ON_INTO_STATUS_CHANGE_VALUE) // power 长按触发
  866. {
  867. extern bool is_sleep;
  868. is_sleep = false; // 强制不休眠
  869. beep_blink(1000, 1);
  870. if (Machine_info.power_status == 1) // 开机状态
  871. {
  872. printf("poweron->poweroff\r\n");
  873. lora_set_power_level(0); // 关机关闭lora防止lora消息刷屏
  874. // Machine_info.power_status = 0;
  875. // screen_dis_info.is_change_power = true;
  876. xQueueReset(screen_queue);
  877. set_screen_dis_info_and_send_queue(false, true, true, false, 5000); // 关机
  878. set_screen_dis_info_and_send_queue(true, false, false, false, 5000);
  879. }
  880. else if (Machine_info.power_status == 0) // 关机后还未进入休眠直接长按开机
  881. {
  882. printf("button power on\r\n");
  883. Machine_info.power_status = 1;
  884. gpio_hold_dis(LORA_TXD_PIN);
  885. gpio_hold_dis(LORA_RXD_PIN);
  886. gpio_hold_dis(PIN_L_CS);
  887. gpio_hold_dis(PIN_R_CS);
  888. set_screen_dis_info_and_send_queue(false, true, false, false, 100);
  889. set_screen_dis_info_and_send_queue(true, false, false, false, 100);
  890. lora_set_power_level(1);
  891. // 恢复串口i功能
  892. uart_sleep_out_config();
  893. font_exit_sleep();
  894. }
  895. }
  896. if (button_info == POWER_ON_INTO_DIS_RIGHT)
  897. {
  898. xQueueReset(screen_queue);
  899. Machine_info.left_current_Quick_refresh_time = 5;
  900. set_screen_dis_info_and_send_queue(false, true, false, false, 100);
  901. set_screen_dis_info_and_send_queue(true, false, false, false, 100);
  902. }
  903. if (button_info == POWER_ON_INTO_OTA_VALUE) // 进入OTA模式
  904. {
  905. beep_blink(50, 3);
  906. printf("into ota mode\r\n");
  907. iot_button_stop();
  908. esp_ble_ota();
  909. }
  910. if (button_info == POWER_ON_INTO_RESET_VALUE) // 进入配网模式
  911. {
  912. is_change_chanel_ok = false;
  913. printf("into reset mode\r\n");
  914. font_exit_sleep();
  915. lora_set_power_level(1);
  916. uart_sleep_out_config();
  917. beep_blink(2000, 1);
  918. extern void sleep_timer_stop();
  919. extern void Already_send_timer_stop();
  920. sleep_timer_stop();
  921. Already_send_timer_stop();
  922. vTaskDelay(300 / portTICK_PERIOD_MS);
  923. reset_default(true, 1);
  924. reset_lora(LORA_CHANENL);
  925. }
  926. }
  927. }
  928. }
  929. }
  930. static void business_logic_task(void *arg)
  931. {
  932. // YC_DATA_T tmp;
  933. int len = 0;
  934. bool is_refresh = false;
  935. for (;;)
  936. {
  937. if (xQueueReceive(yc_data_queue, &len, (TickType_t)portMAX_DELAY))
  938. {
  939. #if 1
  940. is_refresh = subcontract(&yc_data);
  941. if (is_refresh)
  942. {
  943. screen_display();
  944. }
  945. else
  946. {
  947. printf("not display fresh\r\n");
  948. }
  949. #endif
  950. free(yc_data.data); // 释放内存
  951. yc_data.data = NULL;
  952. yc_data.len = 0;
  953. yc_data.index = 0;
  954. }
  955. }
  956. }
  957. static void unpack_task(void *arg)
  958. {
  959. LORA_DATA_T tmp_data;
  960. extern esp_timer_handle_t lora_timer;
  961. for (;;)
  962. {
  963. if (xQueueReceive(lora_data_queue, &tmp_data, (TickType_t)portMAX_DELAY))
  964. {
  965. if (!esp_timer_is_active(lora_timer))
  966. {
  967. lora_timer_start();
  968. }
  969. else
  970. {
  971. lora_timer_restart();
  972. }
  973. yc_data.len += tmp_data.data_len;
  974. memcpy(yc_data.data + yc_data.index, tmp_data.data_buf, tmp_data.data_len);
  975. yc_data.index += tmp_data.data_len;
  976. }
  977. }
  978. }
  979. static void lora_task(void *arg)
  980. {
  981. lora_event_task(arg);
  982. }
  983. // /*********************************************************************************
  984. // * function : Sendlist_task_callback_handler
  985. // * Description : 发送处理链表函数
  986. // * Input :
  987. // * Output :
  988. // * Author : 祁鑫 Data : 2023 9.12
  989. // **********************************************************************************/
  990. // void Sendlist_task_callback_handler()
  991. // {
  992. // int length = 0;
  993. // uint8_t* result = (uint8_t*) malloc(buffer_size+1);
  994. // TickType_t xLastWakeTime;
  995. // #if 0
  996. // const TickType_t xFrequency = 400/10; // 定时通知的间隔
  997. // #else
  998. // const TickType_t xFrequency = 500/10; // 定时通知的间隔
  999. // #endif
  1000. // // 初始化xLastWakeTime
  1001. // xLastWakeTime = xTaskGetTickCount();
  1002. // int receive_times = 0;
  1003. // int user_size = 0;
  1004. // int result_length = 0;
  1005. // //int result_length = 0;
  1006. // static int result_index = 0;
  1007. // int result_data_len = 0;
  1008. // while (1) {
  1009. // #if 0
  1010. // //定时通知数据处理任务有新数据可用
  1011. // vTaskDelayUntil(&xLastWakeTime, xFrequency);
  1012. // #endif
  1013. // if(xQueueReceive(Send_Data_queue, &length, portMAX_DELAY) == pdPASS)
  1014. // {
  1015. // //int len = countNodes(Send_list); /* returns the number of nodes in the list */
  1016. // Node *list = Send_list; //发送数据链表
  1017. // int len = countNodes(Send_list); /* returns the number of nodes in the list */
  1018. // if(len!=0)
  1019. // {
  1020. // printf("current wait send num data=%d\r\n",len);
  1021. // #if 1
  1022. // //int len = countNodes(list); /* returns the number of nodes in the list */
  1023. // while(len)
  1024. // {
  1025. // //int busy = get_lora_busy_pin();
  1026. // //printf("busy = %d\r\n",busy);
  1027. // printf("send times\r\n");
  1028. // //if()
  1029. // lora_send_data((char *)list->data,list->len);
  1030. // list=list->next;
  1031. // len--;
  1032. // }
  1033. // //Send_list = deleteList(Send_list);
  1034. // free(list);
  1035. // #endif
  1036. // }
  1037. // #if 0
  1038. // for (int i = 0; i < len; i++)
  1039. // {
  1040. // printf("%02x",result[i]);
  1041. // }
  1042. // #endif
  1043. // }
  1044. // }
  1045. // }
  1046. /*********************************************************************************
  1047. * function : uart_task_callback_handler
  1048. * Description : 串口0函数
  1049. * Input :
  1050. * Output :
  1051. * Author : 祁鑫 Data : 2023 9.12
  1052. **********************************************************************************/
  1053. void uart_task_callback_handler()
  1054. {
  1055. // #include "driver/uart.h"
  1056. // uint8_t dtmp[200]= {0};
  1057. // while (1)
  1058. // {
  1059. // printf("uart 0 rev = ");
  1060. // int len = uart_read_bytes(UART_NUM_0, dtmp, 1024,200/portTICK_PERIOD_MS);
  1061. // if(len)
  1062. // {
  1063. // for(int i = 0;i<len;i++)
  1064. // printf("%02X",dtmp[i]);
  1065. // printf("\r\n");
  1066. // }
  1067. // printf("\r\n");
  1068. // vTaskDelay(1000 / portTICK_PERIOD_MS);
  1069. // }
  1070. }
  1071. /*********************************************************************************
  1072. * function : read_deal_data_callback_handler
  1073. * Description : 定时处理函数
  1074. * Input :
  1075. * Output :
  1076. * Author : 祁鑫 Data : 2023 9.12
  1077. **********************************************************************************/
  1078. void read_deal_data_callback_handler()
  1079. {
  1080. #if 0
  1081. int length = 0;
  1082. uint8_t* result = (uint8_t*) malloc(buffer_size+1);
  1083. TickType_t xLastWakeTime;
  1084. #if 1
  1085. const TickType_t xFrequency = 100/10; // 定时通知的间隔
  1086. #else
  1087. const TickType_t xFrequency = 500; // 定时通知的间隔
  1088. #endif
  1089. // 初始化xLastWakeTime
  1090. xLastWakeTime = xTaskGetTickCount();
  1091. int receive_times = 0;
  1092. int user_size = 0;
  1093. int result_length = 0;
  1094. //int result_length = 0;
  1095. static int result_index = 0;
  1096. int result_data_len = 0;
  1097. #include "y_ringbuf.h"
  1098. extern struct RINGBUF_st;
  1099. extern RINGBUF_st *lora_ringbuf;
  1100. while (1) {
  1101. // 定时通知数据处理任务有新数据可用
  1102. vTaskDelayUntil(&xLastWakeTime, xFrequency);
  1103. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  1104. #if USER_OTA
  1105. if(user_size>=0)
  1106. #endif
  1107. if(user_size>0)
  1108. {
  1109. #if 0
  1110. for (int i = 0; i < len; i++)
  1111. {
  1112. printf("%02x",result[i]);
  1113. }
  1114. #endif
  1115. printf("times comming length=%d\r\n",user_size);
  1116. if(user_size == 36)
  1117. {
  1118. y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1119. }
  1120. #if 1
  1121. int len = y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1122. yc_data.len = user_size;
  1123. yc_data.data = malloc(sizeof(uint8_t)*user_size);//分配内存
  1124. memcpy(yc_data.data,result,user_size);
  1125. //lora_send_queue_callback(result,len); //发送消息处理函数
  1126. if(xQueueSend(yc_data_queue,&len,0) != true)
  1127. {
  1128. ESP_LOGE(LOG_TAG,"yc_data_queue send is fail");
  1129. }
  1130. #endif
  1131. #if USER_OTA
  1132. if((user_size>1024)||(user_size == 0) || (((user_size<1024))&&(is_ota ==true)) )
  1133. if(xQueueSend(ota_queue,&user_size,0) != true)
  1134. {
  1135. ESP_LOGE(LOG_TAG,"ota_queue send is fail");
  1136. }
  1137. #endif
  1138. }
  1139. }
  1140. #else
  1141. int length = 0;
  1142. uint8_t *result = (uint8_t *)malloc(buffer_size + 1);
  1143. int user_size = 0;
  1144. while (1)
  1145. {
  1146. if (xQueueReceive(lora_dealhandle, &length, portMAX_DELAY) == pdPASS)
  1147. {
  1148. // printf("deal data\r\n");
  1149. user_size = y_ringbuf_get_used_size(lora_ringbuf);
  1150. if (user_size > 0)
  1151. {
  1152. ESP_LOGI(LOG_TAG, "-->lora len = %d\r\n", user_size);
  1153. #if 0
  1154. if(user_size == 36)
  1155. {
  1156. y_ringbuf_read_clear(lora_ringbuf, result, user_size); //读取并清除数据
  1157. #if 1
  1158. for (int i = 0; i < user_size; i++)
  1159. {
  1160. printf("%02x",result[i]);
  1161. }
  1162. #endif
  1163. }else
  1164. #endif
  1165. {
  1166. #if 1
  1167. int len = y_ringbuf_read_clear(lora_ringbuf, result, user_size); // 读取并清除数据
  1168. #if 0
  1169. for (int i = 0; i < user_size; i++)
  1170. {
  1171. printf("%02x",result[i]);
  1172. }
  1173. printf("\r\n");
  1174. #endif
  1175. ESP_LOG_BUFFER_HEX("from gateway lora", result, user_size);
  1176. yc_data.len = user_size;
  1177. yc_data.data = malloc(sizeof(uint8_t) * user_size); // 分配内存
  1178. memcpy(yc_data.data, result, user_size);
  1179. // lora_send_queue_callback(result,len); //发送消息处理函数
  1180. if (xQueueSend(yc_data_queue, &len, (TickType_t)portMAX_DELAY) != true)
  1181. {
  1182. ESP_LOGE(LOG_TAG, "yc_data_queue send is fail");
  1183. }
  1184. #endif
  1185. }
  1186. }
  1187. }
  1188. }
  1189. #endif
  1190. }