yc_terminal.c 60 KB

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