yc_terminal.c 47 KB

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