yc_terminal.c 48 KB

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