yc_terminal.c 50 KB

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