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