yc_terminal.c 53 KB

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