yc_terminal.c 51 KB

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