hmi.c 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-26 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <control.h>
  14. #include <rtdevice.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "stdarg.h"
  19. #include "string.h"
  20. #include "sht30_collect.h"
  21. #include "beep.h"
  22. #include "easyflash.h"
  23. #include "sound.h"
  24. #include "ymodem.h"
  25. #include <shell.h>
  26. #include <msh.h> /* msh_exec:屏幕 debug1 调试页控制台命令执行 */
  27. #include "dtu_uart2.h"
  28. #include <fal.h>
  29. #include <spi_flash_sfud.h> /* debug1 页外挂Flash信息(与 sys_monitor.c 相同方式读取) */
  30. #include "sys_monitor.h"
  31. #include "param_mirror.h"
  32. #define DBG_ENABLE
  33. #define DBG_COLOR
  34. #define DBG_TAG "hmi"
  35. #define DBG_LEVEL DBG_LOG
  36. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  37. #define HMI_THREAD_PRIORITY 5 //线程优先级
  38. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  39. #define HMI_THREAD_TIMESLICE 10 //时间片
  40. #define CONTROL_MOTOR_PRIORITY 18
  41. #define CONTROL_MOTOR_STACK_SIZE 2048
  42. #define CONTROL_MOTOR_TIMESLICE 5
  43. static char msg_pool[512];
  44. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  45. /* 串口原始包缓冲(与解析缓冲 rx_buffer 分离:帧组装时两者不能是同一块内存) */
  46. static char rx_pkt[HMI_UART_RXBUFFER_SIZE + 1];
  47. void list_mem(void);
  48. int mb_master_samlpe();
  49. /**/
  50. Device_Status_typedef device_status;
  51. System_stats_t system_status;
  52. time_t time(time_t *t);
  53. /* 串口接收消息结构*/
  54. struct rx_msg
  55. {
  56. rt_device_t dev;
  57. rt_size_t size;
  58. };
  59. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  60. ////
  61. static rt_thread_t hmi1 = RT_NULL;
  62. static rt_thread_t ctr_motor = RT_NULL;
  63. rt_device_t serial; /*串口设备句柄*/
  64. static upgrade_state_t current_state = STATE_IDLE;
  65. uint32_t total_file_size = 0;
  66. static uint32_t bytes_received = 0;
  67. static uint8_t recv_buffer[RECV_BUFFER_SIZE];
  68. static uint32_t recv_write_pos = 0;
  69. /* 消息队列控制块 */
  70. static struct rt_messagequeue rx_mq;//接收数据消息队列
  71. static void HMI_Decode(void);
  72. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  73. static int HMI_uart4_init(void)
  74. {
  75. serial = rt_device_find(UART_NAME);
  76. /*修改串口配置参数*/
  77. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  78. config.data_bits = DATA_BITS_8; //数据位8
  79. config.stop_bits = STOP_BITS_1; //停止位1
  80. config.bufsz = 512; //缓冲区128字节
  81. config.parity = PARITY_NONE; //无奇偶校验
  82. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  83. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  84. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  85. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  86. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  87. rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
  88. return RT_EOK;
  89. }
  90. /*串口屏写数据*/
  91. /* ==================== UART4(屏幕)发送:互斥 / 升级状态 / 批量 / 去重 ====================
  92. * 背景:workSta 页每秒被 getFullData 刷新一次,原实现每条属性 = 一次 rt_device_write
  93. * + 一次加解锁,115200bps 下每秒 60+ 次写、约 2KB 数据,既占串口带宽也占 CPU,
  94. * 还会把"屏幕数据"挤在日志里。2026-10-10 做如下优化(HMI_WRITE 签名不变,调用方无感):
  95. * ① 批量发送:HMI_BATCH_BEGIN()/END() 之间累积成整块再一次写入(缓冲满自动 flush);
  96. * ② 重复过滤:同一调用点(用格式化串地址唯一标识)输出内容与上次相同则不发送;
  97. * 过滤表为直接映射表,槽位冲突时按"未命中"处理——只会多发、绝不会漏发;
  98. * ③ 过滤表定期(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位,并在收到屏幕 Swversion 帧
  99. * (屏幕重启/换页握手)时立即复位,保证屏幕重启后最多 HMI_DEDUPE_RESYNC_SEC 秒
  100. * 即恢复全量刷新,不会长期停在旧值。
  101. * 维护提示:新增刷新项时无需关心批量/去重,直接用 HMI_WRITE 即可。 */
  102. /* ---------- 发送互斥与升级状态 ---------- */
  103. static struct rt_mutex hmi_write_mutex;
  104. static rt_bool_t hmi_mutex_inited = RT_FALSE;
  105. static volatile rt_tick_t s_hmi_upgrade_tick = 0; /* 0=不在升级;非0=最近一次升级数据时间 */
  106. static rt_bool_t hmi_write_lock(void)
  107. {
  108. /* 中断上下文不加锁(互斥锁不允许在中断中获取) */
  109. if (rt_interrupt_get_nest() != 0)
  110. {
  111. return RT_FALSE;
  112. }
  113. if (!hmi_mutex_inited)
  114. {
  115. rt_base_t level = rt_hw_interrupt_disable();
  116. if (!hmi_mutex_inited)
  117. {
  118. if (rt_mutex_init(&hmi_write_mutex, "hmi_mtx", RT_IPC_FLAG_FIFO) == RT_EOK)
  119. {
  120. hmi_mutex_inited = RT_TRUE;
  121. }
  122. }
  123. rt_hw_interrupt_enable(level);
  124. }
  125. if (hmi_mutex_inited)
  126. {
  127. rt_mutex_take(&hmi_write_mutex, RT_WAITING_FOREVER);
  128. return RT_TRUE;
  129. }
  130. return RT_FALSE;
  131. }
  132. static void hmi_write_unlock(void)
  133. {
  134. if (hmi_mutex_inited)
  135. {
  136. rt_mutex_release(&hmi_write_mutex);
  137. }
  138. }
  139. void Hmi_Upgrade_Begin(void)
  140. {
  141. s_hmi_upgrade_tick = rt_tick_get();
  142. LOG_I("HMI upgrade begin");
  143. }
  144. void Hmi_Upgrade_End(void)
  145. {
  146. s_hmi_upgrade_tick = 0;
  147. LOG_I("HMI upgrade end");
  148. }
  149. /* 升级传输期间返回真:屏蔽普通属性刷新,避免混入固件数据流;
  150. * 60s 内没有新的升级数据自动解除(防止升级中断后屏幕刷新永久失效)。 */
  151. static rt_bool_t hmi_upgrade_busy(void)
  152. {
  153. rt_tick_t t = s_hmi_upgrade_tick;
  154. if (t == 0)
  155. {
  156. return RT_FALSE;
  157. }
  158. if ((rt_tick_get() - t) > (60 * RT_TICK_PER_SECOND))
  159. {
  160. s_hmi_upgrade_tick = 0;
  161. LOG_W("HMI upgrade timeout, auto clear busy flag");
  162. return RT_FALSE;
  163. }
  164. return RT_TRUE;
  165. }
  166. /* ---------- ② 重复过滤(CRC32 直接映射表) ---------- */
  167. #define HMI_DEDUPE_EN 1 /* 置 0 可整体关闭重复过滤(调试用) */
  168. #define HMI_DEDUPE_ENTRIES 96 /* 槽位数;冲突只影响压缩率,不影响正确性 */
  169. #define HMI_DEDUPE_RESYNC_SEC 10 /* 定期全量刷新周期(s) */
  170. typedef struct {
  171. const char *key; /* 格式化串地址:每个调用点唯一 */
  172. rt_uint32_t crc; /* 上次发送内容的 CRC32 */
  173. rt_uint16_t len; /* 上次发送内容长度 */
  174. } hmi_dedupe_item_t;
  175. static hmi_dedupe_item_t hmi_dedupe_tab[HMI_DEDUPE_ENTRIES];
  176. static rt_uint16_t hmi_dedupe_tick_cnt = 0;
  177. static rt_uint32_t hmi_crc32(const char *s, rt_uint16_t len)
  178. {
  179. rt_uint32_t crc = 0xFFFFFFFFu;
  180. rt_uint16_t i;
  181. rt_uint8_t k;
  182. for (i = 0; i < len; i++)
  183. {
  184. crc ^= (rt_uint8_t)s[i];
  185. for (k = 0; k < 8; k++)
  186. {
  187. crc = (crc >> 1) ^ (0xEDB88320u & (rt_uint32_t)(-(rt_int32_t)(crc & 1u)));
  188. }
  189. }
  190. return ~crc;
  191. }
  192. static rt_uint16_t hmi_dedupe_index(const char *key)
  193. {
  194. return (rt_uint16_t)(((rt_uint32_t)key >> 2) % HMI_DEDUPE_ENTRIES);
  195. }
  196. /* 返回 RT_TRUE:本次内容与上次完全相同,可跳过发送 */
  197. static rt_bool_t hmi_dedupe_skip(const char *key, const char *line, rt_uint16_t len)
  198. {
  199. rt_uint16_t idx = hmi_dedupe_index(key);
  200. hmi_dedupe_item_t *it = &hmi_dedupe_tab[idx];
  201. rt_uint32_t crc = hmi_crc32(line, len);
  202. if (it->key == key && it->len == len && it->crc == crc)
  203. {
  204. return RT_TRUE;
  205. }
  206. /* 写入顺序:crc/len 先写、key 最后写;读取时先比 key——
  207. * 这样并发时"命中"的条目其 crc/len 一定是完整的 */
  208. it->crc = crc;
  209. it->len = len;
  210. it->key = key;
  211. return RT_FALSE;
  212. }
  213. static void hmi_dedupe_reset(void)
  214. {
  215. rt_memset(hmi_dedupe_tab, 0, sizeof(hmi_dedupe_tab)); /* key=NULL,任何真实调用点都不会命中 */
  216. hmi_dedupe_tick_cnt = 0;
  217. }
  218. /* 由 Ctr_Mortor_entry 每 100ms 调用一次:到点复位过滤表(等价于下一次刷新全量重发) */
  219. static void hmi_dedupe_tick(void)
  220. {
  221. if (++hmi_dedupe_tick_cnt >= (HMI_DEDUPE_RESYNC_SEC * 10))
  222. {
  223. hmi_dedupe_reset();
  224. }
  225. }
  226. /* ---------- ① 批量发送 ---------- */
  227. #define HMI_BATCH_BUF_SIZE 1024 /* 累积缓冲;满则自动 flush,不丢指令 */
  228. static char hmi_batch_buf[HMI_BATCH_BUF_SIZE];
  229. static rt_uint16_t hmi_batch_len = 0;
  230. static volatile rt_bool_t hmi_batch_on = RT_FALSE;
  231. static rt_thread_t hmi_batch_owner = RT_NULL; /* 批量会话所属线程 */
  232. #define HMI_BATCH_BEGIN() Hmi_Batch_Begin()
  233. #define HMI_BATCH_END() Hmi_Batch_End()
  234. void Hmi_Batch_Begin(void)
  235. {
  236. hmi_batch_len = 0;
  237. hmi_batch_owner = rt_thread_self();
  238. hmi_batch_on = RT_TRUE;
  239. }
  240. void Hmi_Batch_End(void)
  241. {
  242. hmi_batch_on = RT_FALSE;
  243. hmi_batch_owner = RT_NULL;
  244. if (hmi_batch_len > 0)
  245. {
  246. rt_bool_t locked = hmi_write_lock();
  247. rt_device_write(serial, 0, hmi_batch_buf, hmi_batch_len);
  248. if (locked)
  249. {
  250. hmi_write_unlock();
  251. }
  252. system_status.uart4_tx_bytes += hmi_batch_len;
  253. hmi_batch_len = 0;
  254. }
  255. }
  256. /* 访问批量缓冲只允许批量会话的属主线程(见 HMI_WRITE):
  257. * 其它线程即便在批量期间调用 HMI_WRITE 也直接立即发送,
  258. * 避免两线程同时往同一缓冲追加导致内容错乱。 */
  259. static rt_bool_t hmi_batch_owned(void)
  260. {
  261. return (hmi_batch_on && hmi_batch_owner == rt_thread_self()) ? RT_TRUE : RT_FALSE;
  262. }
  263. /* 单条立即发送(批量会话外的默认行为,与改造前一致) */
  264. static void hmi_send_now(const char *data, rt_uint16_t len)
  265. {
  266. rt_bool_t locked = hmi_write_lock();
  267. rt_device_write(serial, 0, data, len);
  268. if (locked)
  269. {
  270. hmi_write_unlock();
  271. }
  272. system_status.uart4_tx_bytes += len;
  273. }
  274. void HMI_WRITE(char *fmt, ...)
  275. {
  276. va_list ap;
  277. char str[256 + 3]; /* 预留 3 字节结束符 0xFF 0xFF 0xFF */
  278. rt_size_t str_length;
  279. if (serial == NULL || fmt == NULL) {
  280. return;
  281. }
  282. /* 升级传输期间屏蔽普通业务指令 */
  283. if (hmi_upgrade_busy()) {
  284. return;
  285. }
  286. va_start(ap, fmt);
  287. str_length = rt_vsnprintf(str, sizeof(str) - 3, fmt, ap);
  288. va_end(ap);
  289. if (str_length == 0 || str_length >= sizeof(str) - 3) {
  290. return;
  291. }
  292. #if HMI_DEDUPE_EN
  293. /* ② 同一调用点内容未变化则不重发 */
  294. if (hmi_dedupe_skip(fmt, str, (rt_uint16_t)str_length))
  295. {
  296. return;
  297. }
  298. #endif
  299. /* 结束符与指令合并为一次写入,减少写入次数、避免发送被拆散 */
  300. str[str_length] = (char)0xFF;
  301. str[str_length + 1] = (char)0xFF;
  302. str[str_length + 2] = (char)0xFF;
  303. if (hmi_batch_owned())
  304. {
  305. rt_uint16_t need = (rt_uint16_t)(str_length + 3);
  306. if ((rt_uint32_t)hmi_batch_len + need > sizeof(hmi_batch_buf))
  307. {
  308. Hmi_Batch_End(); /* 缓冲不足先落地一批 */
  309. Hmi_Batch_Begin(); /* 继续留在批量会话内(重新取回属主) */
  310. }
  311. if ((rt_uint32_t)hmi_batch_len + need <= sizeof(hmi_batch_buf))
  312. {
  313. rt_memcpy(hmi_batch_buf + hmi_batch_len, str, need);
  314. hmi_batch_len += need;
  315. }
  316. else
  317. {
  318. /* 单条本身就超过批量缓冲(异常情况):直接发,宁可多一次写也不丢指令 */
  319. hmi_send_now(str, need);
  320. }
  321. return;
  322. }
  323. hmi_send_now(str, (rt_uint16_t)(str_length + 3));
  324. }
  325. /* 接收数据回调函数 */
  326. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  327. {
  328. struct rx_msg msg;
  329. rt_err_t result;
  330. msg.dev = dev;
  331. msg.size = size;
  332. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  333. if ( result == -RT_EFULL)
  334. {
  335. /* 队列满:本条的"唤醒消息"丢弃即可——数据仍在串口 FIFO 中,
  336. * 下一帧到达时消费者会一并读取。原实现在中断回调里 RESET 整个队列,
  337. * 会把已入队的唤醒消息全部吞掉,多帧突发时反而丢更多帧。 */
  338. LOG_I("hmi message queue full, drop this wakeup!\n");
  339. }
  340. return result;
  341. }
  342. /* UART4(屏幕口)接收回调重新注册。
  343. * 注意:rt_device_close() 会清空 rx_indicate,设备被 close/open 后(如串口环回自检、
  344. * 屏幕升级流程)必须重新注册,否则屏幕数据无法被 Hmi_thread_entry 读取。 */
  345. void hmi_uart4_reinit_rx(void)
  346. {
  347. if (serial != RT_NULL)
  348. {
  349. rt_device_set_rx_indicate(serial, uart_input);
  350. LOG_I("hmi uart4 rx_indicate re-registered");
  351. }
  352. }
  353. // 预计算转换表
  354. static const int8_t hex_to_byte[256] = {
  355. [0 ... 255] = -1,
  356. ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
  357. ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
  358. ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
  359. ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
  360. };
  361. /**
  362. * @brief 快速HEX字符串转二进制
  363. * @param hex HEX字符串
  364. * @param bin 输出缓冲区
  365. * @param max_len 最大输出长度
  366. * @return 转换的字节数,<0表示错误
  367. */
  368. static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
  369. {
  370. if (!hex || !bin || max_len <= 0) {
  371. return -1;
  372. }
  373. int hex_len = strlen(hex);
  374. if (hex_len % 2 != 0) {
  375. return -1; // HEX长度必须是偶数
  376. }
  377. int bin_len = hex_len / 2;
  378. if (bin_len > max_len) {
  379. return -1; // 输出缓冲区太小
  380. }
  381. for (int i = 0; i < bin_len; i++) {
  382. int high = hex_to_byte[(unsigned char)hex[i * 2]];
  383. int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
  384. if (high < 0 || low < 0) {
  385. return -1; // 无效HEX字符
  386. }
  387. bin[i] = (high << 4) | low;
  388. }
  389. return bin_len;
  390. }
  391. // 发送原始数据到HMI
  392. static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
  393. {
  394. rt_size_t sent;
  395. rt_bool_t locked;
  396. if (!serial || !data || size == 0) return -1;
  397. /* 与 HMI_WRITE 共用同一把锁,避免升级大包数据与业务指令互相穿插 */
  398. locked = hmi_write_lock();
  399. sent = rt_device_write(serial, 0, data, size);
  400. if (locked)
  401. {
  402. hmi_write_unlock();
  403. }
  404. return (sent == size) ? 0 : -1;
  405. }
  406. // 最简洁版本 - 每4096字节直接清空
  407. void process_hex_payload(const char* hex_str, int req_id) {
  408. int data_len;
  409. int progress;
  410. uint8_t temp[JSON_CHUNK_SIZE];
  411. char progress_json[256];
  412. if (!hex_str) return;
  413. current_state = STATE_RECEIVING;
  414. s_hmi_upgrade_tick = rt_tick_get(); /* 刷新"升级活动中"时间戳 */
  415. // 转换HEX
  416. data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
  417. if (data_len <= 0) return;
  418. // 添加到缓冲区
  419. if (recv_write_pos + data_len > sizeof(recv_buffer)) {
  420. LOG_E("HMI升级缓冲区溢出(recv=%u,new=%d), 丢弃本包", (unsigned)recv_write_pos, data_len);
  421. return;
  422. }
  423. memcpy(recv_buffer + recv_write_pos, temp, data_len);
  424. recv_write_pos += data_len;
  425. bytes_received += data_len;
  426. LOG_I("收到 %d 字节,缓冲: %u/%u",
  427. data_len, (unsigned)recv_write_pos, (unsigned)sizeof(recv_buffer));
  428. /* 每满 4096 字节发送一块。
  429. * 修复:原实现发送后直接清空缓冲,若积攒超过 4096 字节(分块长度不固定时)
  430. * 会把超出的部分丢掉导致屏幕固件损坏;这里改为发送后把剩余数据前移保留。 */
  431. if (recv_write_pos >= 4096) {
  432. LOG_I("发送 4096 字节到HMI...");
  433. /* 失败时重试一次,仍失败才丢弃本块 */
  434. if (send_raw_to_hmi(recv_buffer, 4096) != 0 &&
  435. send_raw_to_hmi(recv_buffer, 4096) != 0)
  436. {
  437. LOG_E("发送 4096 字节到HMI失败(已重试), 丢弃本块(请检查屏幕连接)");
  438. recv_write_pos = 0;
  439. }
  440. else
  441. {
  442. recv_write_pos -= 4096;
  443. if (recv_write_pos > 0) {
  444. memmove(recv_buffer, recv_buffer + 4096, recv_write_pos);
  445. }
  446. }
  447. return;
  448. }
  449. // 检查完成
  450. if (bytes_received >= total_file_size && total_file_size > 0) {
  451. if (recv_write_pos > 0) {
  452. LOG_I("发送最后 %u 字节", (unsigned)recv_write_pos);
  453. send_raw_to_hmi(recv_buffer, recv_write_pos);
  454. recv_write_pos = 0; // 清空
  455. }
  456. current_state = STATE_COMPLETED;
  457. Hmi_Upgrade_End();
  458. LOG_I("升级完成!");
  459. return;
  460. }
  461. // 发送进度
  462. progress = total_file_size > 0 ?
  463. (int)(bytes_received * 100 / total_file_size) : 0;
  464. rt_snprintf(progress_json, sizeof(progress_json),
  465. "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
  466. "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
  467. (unsigned)bytes_received, (unsigned)total_file_size, progress, req_id);
  468. DTU_Write("%s", progress_json); /* 修复:进度串必须作为参数而不是格式串 */
  469. }
  470. /* ==================== UART4(屏幕)接收:帧组装 ====================
  471. * 屏幕每条指令以 "\r\n" 结尾(见原 Hmi_thread_entry 注释),而串口按 DMA 包分段:
  472. * ① 一条指令可能被拆到两次 read —— 旧实现靠 strstr 前缀匹配,残段会误命中别的
  473. * 分支(2026-10-08 的 SwversionV4/V43 误切版本类型即此问题);
  474. * ② 一次 read 可能含多条指令 —— 旧实现是 if/else 链,只处理第一条匹配的分支,
  475. * 例如 "getFullData\r\n" 与 "putTest.switchHeat=1\r\n" 同包时,按键会被丢掉。
  476. * 现在:整包先喂入组装缓冲,按 '\n' 切出完整帧逐条交给 HMI_Decode(帧内容保留
  477. * "\r\n",与旧行为一致);残段留待下次 read 拼接;超长无结束符的帧丢弃并告警。
  478. */
  479. /* 单帧上限:常规屏幕指令都很短;惟 debug1 "dbg:" 帧会送回整个文本框内容
  480. * (含中文日志时按 UTF-8 3 字节/字计算,120 字符最坏约 380B + 前缀/结束符),
  481. * 因此由 256 提升到 512(接收缓冲 rx_buffer 同为 512,配套一致)。 */
  482. #define HMI_FRAME_MAX 512
  483. static char hmi_frame[HMI_FRAME_MAX + 1];
  484. static rt_uint16_t hmi_frame_len = 0;
  485. /* 处理一帧完整指令;返回 RT_TRUE 表示 HMI 线程应退出(屏幕升级完成) */
  486. static rt_bool_t Hmi_Frame_Handle(void)
  487. {
  488. rt_uint16_t copy_len;
  489. if (hmi_frame_len == 0)
  490. {
  491. return RT_FALSE;
  492. }
  493. hmi_frame[hmi_frame_len] = '\0';
  494. /* 屏幕升级应答:单字节 0x05 */
  495. if (hmi_frame_len == 1 && (rt_uint8_t)hmi_frame[0] == HMI_ACK_BYTE)
  496. {
  497. LOG_W("屏幕响应信号!");
  498. hmi_frame_len = 0;
  499. if (current_state == STATE_COMPLETED)
  500. {
  501. Hmi_Upgrade_End();
  502. return RT_TRUE; /* 升级完成,Hmi_thread_entry 收到后退出线程 */
  503. }
  504. /* RTU 可以发送升级文件了 */
  505. char *upgrade_hmi = makeJsonRpc2_0("upgradehmi", NULL, 1);
  506. if (upgrade_hmi)
  507. {
  508. DTU_Write("%s",upgrade_hmi);
  509. free(upgrade_hmi);
  510. }
  511. return RT_FALSE;
  512. }
  513. /* 交给解析入口(rx_buffer 为解析缓冲,HMI_Decode 内部会清空它) */
  514. copy_len = (hmi_frame_len < HMI_UART_RXBUFFER_SIZE) ? hmi_frame_len : HMI_UART_RXBUFFER_SIZE;
  515. rt_memcpy(rx_buffer, hmi_frame, copy_len);
  516. rx_buffer[copy_len] = '\0';
  517. hmi_frame_len = 0;
  518. HMI_Decode();
  519. return RT_FALSE;
  520. }
  521. /* 喂入串口收到的原始数据;返回 RT_TRUE 表示升级完成、HMI 线程应退出 */
  522. static rt_bool_t Hmi_Rx_Feed(const char *data, rt_uint16_t len)
  523. {
  524. rt_uint16_t i;
  525. for (i = 0; i < len; i++)
  526. {
  527. char c = data[i];
  528. /* 升级应答 0x05:单字节,不参与帧组装 */
  529. if (hmi_frame_len == 0 && (rt_uint8_t)c == HMI_ACK_BYTE)
  530. {
  531. hmi_frame[0] = c;
  532. hmi_frame_len = 1;
  533. if (Hmi_Frame_Handle())
  534. {
  535. return RT_TRUE;
  536. }
  537. continue;
  538. }
  539. if (hmi_frame_len >= HMI_FRAME_MAX)
  540. {
  541. LOG_W("hmi frame too long(%d), drop!", hmi_frame_len);
  542. hmi_frame_len = 0;
  543. }
  544. hmi_frame[hmi_frame_len++] = c;
  545. if (c == '\n')
  546. {
  547. if (Hmi_Frame_Handle())
  548. {
  549. return RT_TRUE;
  550. }
  551. }
  552. }
  553. return RT_FALSE;
  554. }
  555. /*入口函数*/
  556. static void Hmi_thread_entry(void *parameter)
  557. {
  558. struct rx_msg msg;
  559. rt_err_t result;
  560. rt_uint32_t rx_length;
  561. if(device_status.dtype==3){
  562. device_status.collt_box_sta = 0;
  563. device_status.run_sta = 0;
  564. }
  565. device_status.camsta = 1;
  566. while(1)
  567. {
  568. rt_memset(&msg, 0, sizeof(msg));
  569. /* 从消息队列中读取消息*/
  570. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  571. if (result == RT_EOK)
  572. {
  573. /* 读到本次 DMA 收到的原始数据(rx_pkt),按帧组装后再解析 */
  574. rx_length = rt_device_read(msg.dev, 0, rx_pkt, HMI_UART_RXBUFFER_SIZE);
  575. if (rx_length > 0)
  576. {
  577. system_status.uart4_rx_bytes += rx_length;
  578. rx_pkt[rx_length] = '\0';
  579. /* 屏幕每帧原始数据:改为 DBG_DBG 级日志(原为无条件 rt_kprintf,
  580. * 每秒刷新时刷屏且占串口时间),需要排查时把 DBG_LEVEL 调到 DBG_DBG */
  581. LOG_D("hmi rx(%u): %s", (unsigned)rx_length, rx_pkt);
  582. if (Hmi_Rx_Feed(rx_pkt, (rt_uint16_t)rx_length))
  583. {
  584. hmi1 = RT_NULL; /* 线程即将退出,句柄置空以便后续按需重建 */
  585. return;
  586. }
  587. }
  588. }
  589. /*********************************************************************************************************************/
  590. //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  591. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  592. }
  593. }
  594. /*******************************************************************************************/
  595. void Start_timer_Uart2(void)
  596. {
  597. device_status.mock_uart2_timer_flag = 1;
  598. }
  599. void Stop_timer_Uart2(void)
  600. {
  601. device_status.mock_uart2_timer_flag = 0;
  602. }
  603. /* ==================== 屏幕测试加热:定时自动关闭 ====================
  604. * 需求:屏幕测试页打开加热(putTest.switchHeat=1)后,10 分钟自动关闭,避免忘记关导致干烧。
  605. * 实现:一次性软定时器;只在"本次加热由屏幕测试打开"时生效——手动关闭(switchHeat=0)
  606. * 或定时到点都会撤销;若定时到点时设备正处于工作流程中(work_status==1),
  607. * 加热由工作循环(calorstat)托管,则本定时器不干预,仅撤销自己。
  608. * 维护提示:如需改自动关闭时长,只改 HMI_TEST_HEAT_TIMEOUT 一处。 */
  609. #define HMI_TEST_HEAT_TIMEOUT (10 * 60) /* 秒 */
  610. static rt_timer_t s_test_heat_timer = RT_NULL;
  611. static volatile rt_bool_t s_test_heat_on = RT_FALSE;
  612. static void hmi_test_heat_timeout(void *parameter)
  613. {
  614. if (!s_test_heat_on)
  615. {
  616. return;
  617. }
  618. s_test_heat_on = RT_FALSE;
  619. if (device_status.work_status == 1)
  620. {
  621. LOG_I("test heat timeout ignored (work in progress)");
  622. return;
  623. }
  624. ctr_Heat(OFF);
  625. device_status.heat_status = 0;
  626. HMI_WRITE("sysTest.switchHeat.val=0");
  627. HMI_WRITE("workSta.heatStatus.val=0");
  628. LOG_W("屏幕测试加热已自动关闭(超时 %d 秒)", HMI_TEST_HEAT_TIMEOUT);
  629. }
  630. static void hmi_test_heat_start(void)
  631. {
  632. if (s_test_heat_timer == RT_NULL)
  633. {
  634. s_test_heat_timer = rt_timer_create("t_testheat", hmi_test_heat_timeout, RT_NULL,
  635. HMI_TEST_HEAT_TIMEOUT * RT_TICK_PER_SECOND,
  636. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  637. if (s_test_heat_timer == RT_NULL)
  638. {
  639. LOG_E("rt_timer_create t_testheat failed!");
  640. return;
  641. }
  642. }
  643. s_test_heat_on = RT_TRUE;
  644. rt_timer_stop(s_test_heat_timer);
  645. rt_timer_start(s_test_heat_timer); /* 每次打开都重新计时 */
  646. }
  647. static void hmi_test_heat_stop(void)
  648. {
  649. s_test_heat_on = RT_FALSE;
  650. if (s_test_heat_timer != RT_NULL)
  651. {
  652. rt_timer_stop(s_test_heat_timer);
  653. }
  654. }
  655. /***************************** 解析从屏幕收到的数据 begin **********************************/
  656. /* ==================== 屏幕命令严格匹配(2026-10-08 修复) ====================
  657. * 屏幕命令以 \r 结尾(见 Hmi_thread_entry 中说明),但串口按包分段:
  658. * 一条 "SwversionV43" 可能被拆成 "SwversionV4" + "3" 两段先后到达。
  659. * 原实现用 strstr() 前缀包含匹配,前段 "SwversionV4" 会误命中"水稻"分支,
  660. * 把 version_type 错误切换(并误报 RTU、重复写 Flash、随后完整帧又切回,
  661. * 配置来回翻转,日志表现为 SwversionV4/save/SwversionV43/save 连环)。
  662. * 本函数要求:命令字完整相等,且其后紧跟 \r(完整帧结束符,兼容 \r\n);
  663. * 分包残段没有 \r,会被拒绝,等屏幕后续完整帧(心跳重发)正常处理。
  664. * 用法:替换存在"短命令是长命令前缀"关系的分支(SwversionV1/V4)。 */
  665. static rt_bool_t hmi_cmd_match(const char *buf, const char *cmd)
  666. {
  667. size_t n = rt_strlen(cmd);
  668. if (rt_strncmp(buf, cmd, n) != 0)
  669. {
  670. return RT_FALSE;
  671. }
  672. return (buf[n] == '\r') ? RT_TRUE : RT_FALSE;
  673. }
  674. /* ==================== 屏幕整页刷新:getFullData / Swversion 帧触发 ====================
  675. * 本函数原为 HMI_Decode 中 if/else 链的第一个分支(约 240 行),抽出为独立函数,
  676. * 便于定位、维护与文档化;调用处用 HMI_BATCH_BEGIN/END 包裹,整页 60+ 条属性
  677. * 合并为 1~2 次串口写(见文件头部 HMI_WRITE 处的发送优化说明)。
  678. * 参数 ptm:调用方已取好的当前 RTC 时间,避免重复 time()/gmtime_r()。 */
  679. static void hmi_refresh_pages(const struct tm *ptm)
  680. {
  681. const struct tm tm = *ptm;
  682. if(device_status.refresh_num==5)
  683. {
  684. device_status.refresh_num = 0;
  685. char *readinfo = makeJson_ReadInfo();
  686. DTU_Write("%s",readinfo);
  687. rt_free(readinfo);
  688. list_mem();
  689. /* 2026-10-15 修复:不得覆盖云端 read 触发的待补发上报标志(hmi_fresh==2)。
  690. * 原实现无条件置 1,会把 read→getinfo→sync→补发上报 链路上的待办请求吃掉:
  691. * 本屏幕刷新周期(refresh_num==5)恰好落在 RTU 应答 synchronization 的
  692. * 1.5~2.2s 窗口内时,==2 判定失效 → 补发被吞 → read 无应答、RTU 等待超时
  693. * (设备实测 2026-10-15:read 后数百 ms 内即被覆盖)。 */
  694. if(device_status.hmi_fresh!=2)
  695. {
  696. device_status.hmi_fresh = 1;
  697. }
  698. rt_thread_delay(100);
  699. }
  700. //在解析getinfo中清除超时0
  701. if(device_status.refresh_timeout==15)
  702. {
  703. rt_sprintf(device_status.sim,"1");
  704. rt_sprintf(device_status.net,"1");
  705. rt_sprintf(device_status.csq,"0");
  706. device_status.camsta = 1;
  707. device_status.link_sta=0;
  708. device_status.refresh_timeout=0;
  709. HMI_WRITE("menu.warn.txt=\"%s\"","[RTU/4G模块]通信失败,检查连接/固件!");
  710. }
  711. device_status.refresh_num++;
  712. device_status.refresh_timeout++;
  713. if(1)
  714. {
  715. HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
  716. if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
  717. {
  718. HMI_WRITE("uploadSet.plt.val=%d",RESET);
  719. }
  720. else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
  721. HMI_WRITE("uploadSet.plt.val=%d",SET);
  722. }
  723. //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
  724. HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
  725. //更新固件版本号
  726. HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
  727. //更新固件编译日期
  728. HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
  729. //
  730. HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
  731. HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
  732. //更新dtu/rtu版本
  733. HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
  734. //更新时间
  735. HMI_WRITE("menu.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  736. //HMI_WRITE("workSta.rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  737. HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
  738. HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
  739. HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
  740. //更新电压
  741. HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
  742. //更新电流
  743. HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
  744. //更新温湿度数据
  745. HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
  746. HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
  747. //更新电池状态
  748. HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
  749. //更新上仓门状态
  750. HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
  751. //更新下仓门状态
  752. HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
  753. //更新工作状态
  754. HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
  755. //更新温控状态
  756. HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
  757. //更新雨控状态
  758. HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
  759. //更新光控状态
  760. HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
  761. //更新有线连接状态
  762. HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
  763. //更新CSQ
  764. HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
  765. HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
  766. //更新收集时间
  767. HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
  768. //更新加热时间
  769. HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
  770. HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
  771. //
  772. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  773. //更新加热时间和温度
  774. HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
  775. HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
  776. //更新加热状态
  777. HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
  778. //测试模式加热状态
  779. HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
  780. //更新密码
  781. HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
  782. //更新时间设置中的参数
  783. //更新定时模式
  784. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  785. //更新光控定时时长
  786. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  787. //更新时控开始
  788. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  789. //更新时控结束
  790. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  791. //SIM卡状态
  792. HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
  793. //ID
  794. HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
  795. HMI_WRITE("menu.deviceId.txt=\"%s\"",device_status.deviceId);
  796. //HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
  797. //数据上传间隔
  798. HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
  799. //语音音量
  800. HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
  801. //震动时间
  802. HMI_WRITE("cangWei.h0.val=%d",device_status.shake_sec);
  803. HMI_WRITE("menu.version.txt=\"%s\"",device_status.driver);
  804. HMI_WRITE("menu.cpu_usage.val=%d",system_status.cpu_usage);
  805. //更新熄屏时间
  806. if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
  807. HMI_WRITE("thsp=%d",device_status.thsp);
  808. HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
  809. HMI_WRITE("menu.mn.txt=\"%s\"",device_status.cam_model);
  810. }
  811. if(atoi(device_status.gps)==1){
  812. HMI_WRITE("menu.gps_state.txt=\"定位成功:(GPS+BD)%d/%d\"",device_status.gps_inuse,device_status.gps_view);
  813. }else if(atoi(device_status.gps)==2){
  814. HMI_WRITE("menu.gps_state.txt=\"定位成功:(基站)\"");
  815. }
  816. if(device_status.sensor_485_sta==1)
  817. {
  818. HMI_WRITE("menu.sensor_state.txt=\"传感器已连接:(%s lux-%s hpa)\"",device_status.lux,device_status.ap);
  819. }
  820. else
  821. {
  822. HMI_WRITE("menu.sensor_state.txt=\"传感器未连接\"");
  823. }
  824. //更新联网状态
  825. if(atoi(device_status.net)==1)
  826. {
  827. HMI_WRITE("menu.net.val=1");
  828. HMI_WRITE("workSta.net.val=1");
  829. }
  830. else {
  831. HMI_WRITE("workSta.net.val=0");
  832. HMI_WRITE("menu.net.val=0");
  833. }
  834. if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
  835. {
  836. HMI_WRITE("menu.net.val=1");
  837. HMI_WRITE("workSta.net.val=2");
  838. }
  839. /**********************************************************************/
  840. }
  841. /* ==================== 屏幕 workSta 页:拍照/清扫状态 + 工作过程显示 ====================
  842. * 随整页刷新每秒执行一次(getFullData 帧触发):
  843. * 拍照状态 workSta.photoSta / 清扫状态 workSta.clearSta:0-停止 1-进行中(双态按钮,写 .val)
  844. * t1 剩余加热时间(s) t2 收集倒计时(s) t3 仓门开/关动作倒计时(s)
  845. * t4 工作进度(%) t5 拍照结束剩余(s) t6 清扫结束剩余(s) (文本控件,写 .txt)
  846. * 数据来源:control.c 各控制流程实时写入 device_status.work_*、photo_left、clear_left;
  847. * t3/t5 的倒计时源在 Ctr_Mortor_entry 中随仓门电机/拍照时序刷新;
  848. * t4 由 Work_Progress_Get() 按"已用时间/预计总时长"换算(见 control.c)。
  849. * 数值显示规则(2026-10-10 调整):t1~t6 数值为 0 时下发**空字符串**(屏幕该栏显示空白,
  850. * 而不是"0"),非 0 才下发数值;t4 非 0 时带 % 号(显示形如 "45%")。
  851. * 下发条件:HMI_WORK_PROC_VT(vt) 当前为 (vt)!=6 —— 除吸虫塔外的测报灯页面都下发,
  852. * 孢子仪(dtype!=3)在前一行条件已被挡掉。若想只给特定页面下发,
  853. * 把该宏改成例如 (vt)==41(仅 4.0 海康)即可。
  854. * 若 HMI 工程里控件名/类型与此不一致,只需改本函数内的 HMI_WRITE 目标名。 */
  855. #define HMI_WORK_PROC_VT(vt) ((vt) != 6) /* 需要显示工作过程的版本类型 */
  856. /* t1~t6 数值朗读:为 0 时下发空字符串(屏幕该栏显示空白),非 0 下发数值;
  857. * percent=RT_TRUE 时数值后带 % 号(t4 进度显示形如 "45%")。
  858. * 实现要点:每个控件只保留**一个** HMI_WRITE 调用点(数值先格式化成字符串、统一走 %s)。
  859. * 因为 HMI_WRITE 的重复过滤按调用点记录"上次内容"——只有单调用点时,
  860. * "上次内容"才恒等于"屏幕当前显示"。若拆成 空串/数值 两个调用点,两个缓存互相独立:
  861. * 从"有值(如 1)"切回 0 时会命中空串调用点的旧缓存而被跳过,屏幕会残留上一个数字
  862. * (要等到过滤表 10s 周期复位才纠正)。 */
  863. static void hmi_write_num_or_blank(char *fmt, uint16_t v, rt_bool_t percent)
  864. {
  865. char num[8];
  866. if (v == 0)
  867. {
  868. num[0] = '\0'; /* 0 → 空串,屏幕显示空白 */
  869. }
  870. else if (percent)
  871. {
  872. rt_snprintf(num, sizeof(num), "%u%%", v);
  873. }
  874. else
  875. {
  876. rt_snprintf(num, sizeof(num), "%u", v);
  877. }
  878. HMI_WRITE(fmt, num);
  879. }
  880. static void hmi_refresh_work_proc(void)
  881. {
  882. if(device_status.dtype != 3 || !HMI_WORK_PROC_VT(device_status.version_type))
  883. {
  884. return;
  885. }
  886. /* 拍照状态:device_status.photo_ws(0-停止 1-拍照中) */
  887. HMI_WRITE("workSta.photoSta.val=%d", (device_status.photo_ws != 0) ? 1 : 0);
  888. /* 清扫状态:device_status.clear_status(0-停止 1-清扫中,2-转仓动作) */
  889. HMI_WRITE("workSta.clearSta.val=%d", (device_status.clear_status != 0) ? 1 : 0);
  890. /* t1~t6:工作过程实时数据(文本控件;数值为 0 时下发空串,该栏显示空白) */
  891. hmi_write_num_or_blank("workSta.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
  892. hmi_write_num_or_blank("workSta.t2.txt=\"%s\"", device_status.work_clt_left, RT_FALSE); /* 收集倒计时(s) */
  893. hmi_write_num_or_blank("workSta.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
  894. hmi_write_num_or_blank("workSta.t4.txt=\"%s\"", Work_Progress_Get(), RT_TRUE); /* 工作进度(%) */
  895. hmi_write_num_or_blank("workSta.t5.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
  896. hmi_write_num_or_blank("workSta.t6.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
  897. }
  898. /* ==================== 屏幕 debug1 调试页(2026-10-13 新增) ====================
  899. * 对应陶晶驰(USART HMI)工程中的调试页 debug1,功能与协议如下:
  900. * ① 信息区(t0 RT-Thread版本 / t1 内核 / t2 BSP型号 / t13 运行时长 / t5 BL版本 /
  901. * t3 固件版本 / t4 编译时间 / t11+j0 CPU占用 / t12+j1 RAM占用 / t10 外挂Flash):
  902. * 屏幕侧在该页定时器(或进入页面)下发 "getDebugData",固件整批刷新(HMI_WRITE 带重复过滤,
  903. * 未变化的项不会重复占用串口带宽);
  904. * ② 按钮区 b0~b9(屏幕弹起事件下发文本命令,以 "\r\n" 结束):
  905. * reboot 重启(沿用既有分支 rt_hw_cpu_reset)
  906. * bootloader 启动BL:置 stop_in_bootloader 并重启进入 BootLoader(沿用既有分支)
  907. * startapp 启动APP:清除 stop_in_bootloader 并重启(下次直接启动 APP)
  908. * dbg_on 打开调试:把 RT-Thread 控制台输出镜像到 debug1 底部文本框
  909. * dbg_off 关闭调试:恢复原控制台设备
  910. * dbg_clr 清空固件侧控制台缓存与屏幕文本框
  911. * dbg_erase 擦除参数:恢复出厂默认参数(按供电类型走交流/电池两套默认值)
  912. * dbg_restore 恢复固件:置 restore_pending + stop_in_bootloader 并重启进 BL,
  913. * 由 BL 执行 df_area→app 恢复(BL 侧支持见交付说明;未支持时不会误动作)
  914. * dbg:<命令> 把 <命令> 作为 msh 命令在本机执行,输出回传文本框(b4 发送按钮)
  915. * ③ 调试控制台实现:注册只写"捕获设备" dbgcap,打开调试时 rt_console_set_device("dbgcap"),
  916. * 此后 rt_kprintf / finsh / msh 的输出全部经其 write 回调缓存并推送屏幕文本框
  917. * (换行按 TJC 官方约定用 "\r" 两字符转义下发,不能用 \n);同时经"转发设备"
  918. * DBG1_CHAIN_DEV(默认 uart1) 原样转发,保证本机调试口日志不停流。命令执行放在
  919. * 独立线程 dbg_exec_thread,避免 top 之类的长命令把屏幕刷新线程卡死。
  920. * 注意(2026-10-13 修复):转发设备只 find 句柄、不自行 open/close,并在每次
  921. * 控制台切换后由 dbg_chain_fixup() 补回接收模式位(详见该函数注释)——
  922. * 否则 rt_serial_open 的 open_flag 覆盖会洗掉 INT_RX,导致本机口无法输入。
  923. * ④ 屏幕文本框收发约定("单框收发"设计,与截图的底部 msh>> 框一致):
  924. * - 固件每次下发的内容尾部都会附加一行提示符 "msh >> "(宏 DBG1_PROMPT);
  925. * - 用户点框→键盘输入→按[发送](b4);b4 弹起事件发送:printh 64 62 67 3A("dbg:")
  926. * + prints "debug1.<框名>.txt",0 + printh 0d 0a;
  927. * - 固件收到 "dbg:"+整个框内容后,取**最后一个 ">>" 之后**的文本作为命令执行
  928. * (无 ">>" 时按纯命令处理,兼容"独立输入框"的接法),执行结果回填框内。
  929. * ⑤ 推送节流与页面联动:输出推送带最小间隔节流;且仅在"该页有心跳"(最近
  930. * DBG1_PAGE_ALIVE_TICK 内收到过 getDebugData)时才推送——离开调试页后自动停推,
  931. * 返回调试页后自动恢复(缓存不丢,回页即见最新内容)。
  932. * 注意:打开调试期间本机 UART1 调试口日志经转发保留;控制台设备在"关闭调试"后恢复原样。
  933. * 维护提示:屏幕控件名若变化,仅需改 DBG1_CON_OBJ 宏及下方各 HMI_WRITE 中的控件名。 */
  934. #define DBG1_CON_OBJ "msh" /* 底部调试文本框对象名(改名只需改这一处) */
  935. #define DBG1_PROMPT "msh >> " /* 文本框尾部提示符(兼作命令提取锚点) */
  936. #define DBG1_CHAIN_DEV "uart1" /* 日志转发设备(本机调试口);置 "" 可关闭转发 */
  937. #define DBG1_CAP_DEV_NAME "dbgcap" /* 控制台捕获设备名(仅固件内部使用) */
  938. #define DBG1_CON_RAW_MAX 200 /* 行缓存上限(约文本框一屏可见字符数,可调) */
  939. #define DBG1_CON_ESC_MAX 220 /* 单次下发转义后上限(受 HMI_WRITE 内部 256B 缓冲限制) */
  940. #define DBG1_CON_PUSH_MAX 120 /* 单次下发取缓存尾部长度(转义前)。受"b4 发送会回传整框"
  941. * 约束:中文按 UTF-8 3B/字,120 字符最坏 360B,
  942. * 加提示符与用户输入仍小于单帧上限 HMI_FRAME_MAX(512) */
  943. #define DBG1_CMD_MAX 100 /* 屏幕控制台单条命令最大长度 */
  944. #define DBG1_EXEC_PRIO 22 /* 调试命令执行线程优先级(低于屏幕线程) */
  945. #define DBG1_EXEC_STACK_SIZE 3072 /* 调试命令执行线程栈 */
  946. #define DBG1_FLUSH_MIN_TICK 30 /* 输出推送最小间隔(tick,10ms/tick;纯节流不丢行) */
  947. #define DBG1_PAGE_ALIVE_TICK 500 /* 调试页心跳存活窗口(5s):超过则暂停推送,离开页面不再占带宽 */
  948. #define DBG1_BSP_NAME "STM32F103RE" /* t2 BSP型号显示(按实际板卡调整) */
  949. #define DBG1_CORE_NAME "Cortex-M3" /* t1 内核显示(如需改成内核版本号改这里) */
  950. #define DBG1_PART_OBJ "t14" /* 分区表文本框对象名(屏幕侧新增控件;改名只改这一处) */
  951. #define DBG1_PART_MAX 200 /* 分区表下发字符串上限(单条 HMI_WRITE 上限约 250B,留余量) */
  952. static char dbg_con_raw[DBG1_CON_RAW_MAX + 2]; /* 控制台行缓存(屏幕文本框内容源) */
  953. static rt_uint16_t dbg_con_len = 0;
  954. static rt_bool_t dbg_mirror_on = RT_FALSE; /* 控制台是否已镜像到屏幕(打开调试) */
  955. static char dbg_saved_console[RT_NAME_MAX + 1]; /* 打开调试前的控制台设备名 */
  956. static struct rt_device dbg_con_dev; /* 只写捕获设备(dbgcap) */
  957. static rt_bool_t dbg_con_dev_ready = RT_FALSE;
  958. static rt_device_t dbg_chain_dev = RT_NULL; /* 日志转发设备句柄(只 find,不持有引用;见 dbg_chain_fixup) */
  959. static volatile rt_tick_t s_dbg1_page_tick = 0; /* 最近一次收到 getDebugData 的时刻(0=从未) */
  960. static struct rt_thread dbg_exec_thread;
  961. static rt_uint8_t dbg_exec_stack[DBG1_EXEC_STACK_SIZE];
  962. static struct rt_semaphore dbg_exec_sem;
  963. static char dbg_cmd_buf[DBG1_CMD_MAX + 1];
  964. static volatile rt_uint8_t dbg_exec_busy = 0;
  965. static rt_bool_t dbg_exec_inited = RT_FALSE;
  966. static rt_bool_t dbg_exec_ready = RT_FALSE;
  967. /* ---------- 控制台行缓存与屏幕推送 ---------- */
  968. static void dbg_con_append(const char *s, rt_uint16_t n)
  969. {
  970. if (n == 0)
  971. {
  972. return;
  973. }
  974. /* 缓存满:丢弃最旧内容(优先从行边界截断),保留最新输出 */
  975. if ((rt_uint32_t)dbg_con_len + n > DBG1_CON_RAW_MAX)
  976. {
  977. rt_uint16_t cut = (dbg_con_len > (DBG1_CON_RAW_MAX / 3)) ? (dbg_con_len - DBG1_CON_RAW_MAX / 3) : 0;
  978. while (cut < dbg_con_len && dbg_con_raw[cut] != '\n')
  979. {
  980. cut++;
  981. }
  982. cut = (cut < dbg_con_len) ? (cut + 1) : dbg_con_len;
  983. if (cut > 0)
  984. {
  985. rt_memmove(dbg_con_raw, dbg_con_raw + cut, dbg_con_len - cut);
  986. dbg_con_len -= cut;
  987. }
  988. }
  989. if (n > DBG1_CON_RAW_MAX - dbg_con_len) /* 单次超长:只保留尾部 */
  990. {
  991. s += (n - (DBG1_CON_RAW_MAX - dbg_con_len));
  992. n = DBG1_CON_RAW_MAX - dbg_con_len;
  993. }
  994. rt_memcpy(dbg_con_raw + dbg_con_len, s, n);
  995. dbg_con_len += n;
  996. dbg_con_raw[dbg_con_len] = '\0';
  997. }
  998. static void dbg_con_puts(const char *s)
  999. {
  1000. dbg_con_append(s, (rt_uint16_t)rt_strlen(s));
  1001. }
  1002. /* 把行缓存转义后推送到屏幕文本框。
  1003. * TJC 字符串转义(官方 wiki 约定):换行 -> "\r"(\ 和 r 两个字符,不要用 \n);双引号 -> \"。
  1004. * 下发内容 = [缓存尾部] + 换行 + 提示符 DBG1_PROMPT —— 提示符固定收尾,用户直接在框尾
  1005. * 输入命令后按[发送],固件即可按"最后一个 >> 之后"提取命令(见 dbg_exec_submit)。
  1006. * force=RT_FALSE 时按 DBG1_FLUSH_MIN_TICK 做最小间隔节流(只影响推送时机,不丢内容);
  1007. * 页面心跳消失(离开调试页)超过 DBG1_PAGE_ALIVE_TICK 时暂存不推,回页后自动恢复。 */
  1008. static void dbg_con_flush(rt_bool_t force)
  1009. {
  1010. static rt_tick_t s_last_flush = 0;
  1011. char esc[DBG1_CON_ESC_MAX + 1];
  1012. rt_uint16_t i, n = 0, raw_len = dbg_con_len;
  1013. const char *p = dbg_con_raw;
  1014. if (raw_len == 0 || rt_interrupt_get_nest() != 0) /* 中断上下文不推送,等下次 */
  1015. {
  1016. return;
  1017. }
  1018. /* 页面联动:从未收到心跳或心跳已停(离开调试页)时暂不推送 */
  1019. if (s_dbg1_page_tick == 0 ||
  1020. (rt_tick_get() - s_dbg1_page_tick) > DBG1_PAGE_ALIVE_TICK)
  1021. {
  1022. return;
  1023. }
  1024. if (force == RT_FALSE && (rt_tick_get() - s_last_flush) < DBG1_FLUSH_MIN_TICK)
  1025. {
  1026. return;
  1027. }
  1028. s_last_flush = rt_tick_get();
  1029. if (raw_len > DBG1_CON_PUSH_MAX) /* 超长只保留尾部(屏幕一屏可见有限) */
  1030. {
  1031. p += (raw_len - DBG1_CON_PUSH_MAX);
  1032. raw_len = DBG1_CON_PUSH_MAX;
  1033. }
  1034. for (i = 0; i < raw_len && n < DBG1_CON_ESC_MAX - 16; i++)
  1035. {
  1036. char c = p[i];
  1037. if (c == '\n')
  1038. {
  1039. if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = 'r'; }
  1040. }
  1041. else if (c == '"')
  1042. {
  1043. if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = '"'; }
  1044. }
  1045. else
  1046. {
  1047. esc[n++] = c;
  1048. }
  1049. }
  1050. if (n > 0 && esc[n - 1] == '\\') /* 截断落在转义符中间时丢弃半个转义 */
  1051. {
  1052. n--;
  1053. }
  1054. /* 追加换行 + 提示符(预留空间已在循环上限扣除) */
  1055. esc[n++] = '\\';
  1056. esc[n++] = 'r';
  1057. for (i = 0; DBG1_PROMPT[i] != '\0' && n < DBG1_CON_ESC_MAX; i++)
  1058. {
  1059. esc[n++] = DBG1_PROMPT[i];
  1060. }
  1061. esc[n] = '\0';
  1062. HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"%s\"", esc);
  1063. }
  1064. /* ---------- 控制台捕获设备(只写,替代 uart1 作为 rt_console_set_device 目标) ---------- */
  1065. static rt_err_t dbg_con_dev_open(rt_device_t dev, rt_uint16_t oflag)
  1066. {
  1067. (void)dev; (void)oflag;
  1068. return RT_EOK;
  1069. }
  1070. static rt_err_t dbg_con_dev_close(rt_device_t dev)
  1071. {
  1072. (void)dev;
  1073. return RT_EOK;
  1074. }
  1075. static rt_size_t dbg_con_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  1076. {
  1077. (void)dev; (void)pos; (void)buffer; (void)size;
  1078. return 0;
  1079. }
  1080. /* 捕获 rt_kprintf/finsh/msh 的全部输出:① 转发到 DBG1_CHAIN_DEV(本机调试口,保持日志不停流);
  1081. * ② 过滤 ANSI 转义与编辑控制字符后按行入缓存,行尾或节流点推送屏幕(页面心跳存在时)。 */
  1082. static rt_size_t dbg_con_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  1083. {
  1084. static rt_uint8_t s_esc = 0; /* ANSI 转义过滤状态机:0-普通 1-已见ESC 2-ESC[ 序列 */
  1085. const char *p = (const char *)buffer;
  1086. rt_size_t i;
  1087. (void)dev; (void)pos;
  1088. /* ① 本机调试口转发(best effort:设备未打开则静默跳过) */
  1089. if (dbg_chain_dev != RT_NULL)
  1090. {
  1091. rt_device_write(dbg_chain_dev, 0, buffer, size);
  1092. }
  1093. for (i = 0; i < size; i++)
  1094. {
  1095. char c = p[i];
  1096. if (s_esc == 0)
  1097. {
  1098. if ((rt_uint8_t)c == 0x1B) { s_esc = 1; continue; }
  1099. }
  1100. else if (s_esc == 1)
  1101. {
  1102. s_esc = (c == '[') ? 2 : 0;
  1103. continue;
  1104. }
  1105. else
  1106. {
  1107. if ((rt_uint8_t)c >= 0x40 && (rt_uint8_t)c <= 0x7E) s_esc = 0;
  1108. continue;
  1109. }
  1110. if (c == '\n')
  1111. {
  1112. dbg_con_append("\n", 1);
  1113. if (dbg_mirror_on || dbg_exec_busy) dbg_con_flush(RT_FALSE);
  1114. }
  1115. else if (c == '\t')
  1116. {
  1117. dbg_con_append(" ", 1);
  1118. }
  1119. else if (c == '\r' || (rt_uint8_t)c < 0x20 || (rt_uint8_t)c == 0x7F)
  1120. {
  1121. /* \r 与其它控制字符(含 finsh 编辑用的 \b)不显示 */
  1122. }
  1123. else
  1124. {
  1125. dbg_con_append(&c, 1);
  1126. }
  1127. }
  1128. return size;
  1129. }
  1130. static void dbg_con_dev_init(void)
  1131. {
  1132. if (dbg_con_dev_ready)
  1133. {
  1134. return;
  1135. }
  1136. rt_memset(&dbg_con_dev, 0, sizeof(dbg_con_dev));
  1137. dbg_con_dev.type = RT_Device_Class_Char;
  1138. dbg_con_dev.rx_indicate = RT_NULL;
  1139. dbg_con_dev.tx_complete = RT_NULL;
  1140. dbg_con_dev.init = RT_NULL;
  1141. dbg_con_dev.open = dbg_con_dev_open;
  1142. dbg_con_dev.close = dbg_con_dev_close;
  1143. dbg_con_dev.read = dbg_con_dev_read;
  1144. dbg_con_dev.write = dbg_con_dev_write;
  1145. dbg_con_dev.control = RT_NULL;
  1146. if (rt_device_register(&dbg_con_dev, DBG1_CAP_DEV_NAME, RT_DEVICE_FLAG_STREAM) == RT_EOK)
  1147. {
  1148. dbg_con_dev_ready = RT_TRUE;
  1149. }
  1150. }
  1151. /* ---------- 本机调试口"读路径保护"(2026-10-13 修复:打开调试后串口无法输入) ----------
  1152. * 现象:点"打开调试"后,PC 上的调试串口(本机口 uart1)敲键盘无任何反应。
  1153. * 根因(已读内核源码取证):
  1154. * RT-Thread 4.0.2 的 rt_serial_open() 用 `dev->open_flag = oflag & 0xff` 无条件覆盖
  1155. * 设备标志位(components/drivers/serial/serial.c:593),而
  1156. * RT_DEVICE_FLAG_INT_RX(0x100) 高于 0xff —— 任何一次"不带 INT_RX 的 open"
  1157. * (如 rt_console_set_device() 内部的 OFLAG_RDWR|STREAM)都会把接收模式位洗掉。
  1158. * 而接收路径 rt_serial_read() 按该位选模式(serial.c:803):位丢失后
  1159. * tshell 的 read() 掉进 _serial_poll_rx 轮询分支(stm32_getc 直读 RDR 寄存器),
  1160. * 字符却早已被 RX 中断搬进 fifo —— fifo 里的输入永远读不出来,即"无法输入"。
  1161. * 处理:uart1 的 fifo/RX 中断本身不受影响(引用计数从未归零,device.c 的 close
  1162. * 只在 ref 减到 0 时才真正关闭设备),**唯一要做的就是把接收模式位补回 open_flag**:
  1163. * ① dbg_chain_fixup():记录 uart1 原始接收模式位(INT_RX/DMA_RX)并补回;
  1164. * ② 每次控制台切换(打开/关闭调试、屏幕命令的临时切换)之后都调用一次;
  1165. * ③ 调试页心跳(每秒 getDebugData)也调用 —— 即使其它代码误改,1 秒内自愈。
  1166. * 另:不再自行 open/close 转发设备(旧实现的 open 正是肇事者,close 还会错配引用
  1167. * 计数),转发只需 rt_device_write 时设备已被 finsh/console 持有引用即可。 */
  1168. static rt_uint16_t dbg_chain_rx_flag = 0; /* 本机口的接收模式位(INT_RX/DMA_RX),首次读取后固定 */
  1169. static void dbg_chain_fixup(void)
  1170. {
  1171. struct rt_serial_device *ser;
  1172. if (DBG1_CHAIN_DEV[0] == '\0') /* 置 "" 关闭转发(编译期常量) */
  1173. {
  1174. return;
  1175. }
  1176. if (dbg_chain_dev == RT_NULL)
  1177. {
  1178. dbg_chain_dev = rt_device_find(DBG1_CHAIN_DEV);
  1179. if (dbg_chain_dev == RT_NULL)
  1180. {
  1181. return;
  1182. }
  1183. }
  1184. ser = (struct rt_serial_device *)dbg_chain_dev;
  1185. if (dbg_chain_rx_flag == 0)
  1186. {
  1187. /* 首次:从当前 open_flag 读接收模式(uart1 无 DMA 配置,默认 INT_RX) */
  1188. dbg_chain_rx_flag = dbg_chain_dev->open_flag &
  1189. (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
  1190. if (dbg_chain_rx_flag == 0)
  1191. {
  1192. dbg_chain_rx_flag = RT_DEVICE_FLAG_INT_RX;
  1193. }
  1194. }
  1195. if (ser->serial_rx == RT_NULL)
  1196. {
  1197. /* 接收 fifo 不存在(设备曾被整体关闭过)→ 重新 open 重建 fifo 并重开 RX 中断。
  1198. * open 走"首次打开"分支,会 malloc fifo 并执行 SET_INT(见 serial.c:596-615),
  1199. * 且 open 的 oflag 已带接收位,open_flag 自动正确。 */
  1200. rt_device_open(dbg_chain_dev, RT_DEVICE_OFLAG_RDWR | dbg_chain_rx_flag | RT_DEVICE_FLAG_STREAM);
  1201. return;
  1202. }
  1203. /* 补回接收模式位(仅设备框架层标志,不动硬件;fifo 数据原样可读) */
  1204. dbg_chain_dev->open_flag |= dbg_chain_rx_flag;
  1205. }
  1206. /* ---------- uart4(屏幕口)接收模式位保护 ----------
  1207. * 与 uart1 同一机理(见上方 dbg_chain_fixup 注释):uart4 由 HMI_uart4_init() 以
  1208. * RT_DEVICE_FLAG_DMA_RX 打开;rt_console_set_device("uart4")/finsh_set_device("uart4")
  1209. * 等路径内部的 open 会按 `open_flag = oflag & 0xff` 把 DMA_RX 位(0x400)洗掉
  1210. * (serial.c:593),rt_serial_read() 随即掉进轮询读分支(serial.c:803)——
  1211. * 屏幕数据读不出来,现象为"屏幕点不动",且屏幕自身无法再下发恢复指令。
  1212. * 处理:Debug_log_ON/OFF(含屏幕 DebugEnable/DebugDisable 指令)切换后立即补回;
  1213. * 电机线程循环每 100ms 再补一次(自愈)。 */
  1214. static rt_uint16_t hmi_uart4_rx_flag = 0; /* uart4 接收模式位,首次读取后固定 */
  1215. static void hmi_uart4_fixup_rx(void)
  1216. {
  1217. if (serial == RT_NULL)
  1218. {
  1219. return;
  1220. }
  1221. if (hmi_uart4_rx_flag == 0)
  1222. {
  1223. /* 首次:读当前 open_flag;若已被洗掉按实际打开方式(DMA_RX)兜底 */
  1224. hmi_uart4_rx_flag = serial->open_flag &
  1225. (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
  1226. if (hmi_uart4_rx_flag == 0)
  1227. {
  1228. hmi_uart4_rx_flag = RT_DEVICE_FLAG_DMA_RX;
  1229. }
  1230. }
  1231. /* 仅补框架层标志位,不动硬件(DMA 仍在搬运,fifo 数据原样可读) */
  1232. serial->open_flag |= hmi_uart4_rx_flag;
  1233. }
  1234. static void dbg_console_save(void)
  1235. {
  1236. rt_device_t cur = rt_console_get_device();
  1237. /* 已处于捕获设备(如执行线程临时切换期间被并发调用)时不覆盖记录,
  1238. * 否则"关闭调试"会恢复到 dbgcap 上,永远回不到真实控制台 */
  1239. if (cur != RT_NULL && rt_strcmp(cur->parent.name, DBG1_CAP_DEV_NAME) == 0)
  1240. {
  1241. return;
  1242. }
  1243. if (cur != RT_NULL) rt_strncpy(dbg_saved_console, cur->parent.name, RT_NAME_MAX);
  1244. else rt_strncpy(dbg_saved_console, "uart1", RT_NAME_MAX);
  1245. dbg_saved_console[RT_NAME_MAX] = '\0';
  1246. }
  1247. static void dbg_console_restore(void)
  1248. {
  1249. if (dbg_saved_console[0] != '\0') rt_console_set_device(dbg_saved_console);
  1250. else rt_console_set_device("uart1");
  1251. }
  1252. /* ---------- 屏幕控制台:命令执行(独立线程,防长命令卡死屏幕线程) ---------- */
  1253. static rt_bool_t dbg_cmd_is_blocking(const char *cmd)
  1254. {
  1255. while (*cmd == ' ' || *cmd == '\t')
  1256. {
  1257. cmd++;
  1258. }
  1259. /* top/Msh_DisplaySysMonitor 为持续刷新命令,在屏幕控制台里无法按键退出,直接拒绝 */
  1260. if (rt_strncmp(cmd, "top", 3) == 0 && (cmd[3] == '\0' || cmd[3] == ' '))
  1261. {
  1262. return RT_TRUE;
  1263. }
  1264. if (rt_strncmp(cmd, "Msh_DisplaySysMonitor", 21) == 0)
  1265. {
  1266. return RT_TRUE;
  1267. }
  1268. return RT_FALSE;
  1269. }
  1270. static void dbg_exec_run(void)
  1271. {
  1272. rt_bool_t switched = RT_FALSE;
  1273. char cmd[DBG1_CMD_MAX + 1];
  1274. rt_memcpy(cmd, dbg_cmd_buf, DBG1_CMD_MAX + 1);
  1275. cmd[DBG1_CMD_MAX] = '\0';
  1276. if (dbg_cmd_is_blocking(cmd))
  1277. {
  1278. dbg_con_puts("(该命令为持续刷新命令,不支持屏幕控制台执行)\n");
  1279. dbg_con_flush(RT_TRUE);
  1280. return;
  1281. }
  1282. /* 未开镜像时临时把控制台切到捕获设备,保证 msh 输出可被捕获 */
  1283. if (dbg_mirror_on == RT_FALSE)
  1284. {
  1285. dbg_con_dev_init();
  1286. if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
  1287. {
  1288. dbg_con_puts("(捕获设备未就绪,命令未执行)\n");
  1289. dbg_con_flush(RT_TRUE);
  1290. return;
  1291. }
  1292. dbg_console_save();
  1293. rt_console_set_device(DBG1_CAP_DEV_NAME); /* 转发只需设备引用,无需自行 open */
  1294. switched = RT_TRUE;
  1295. }
  1296. dbg_con_puts("msh> ");
  1297. dbg_con_puts(cmd);
  1298. dbg_con_append("\n", 1);
  1299. dbg_con_flush(RT_FALSE);
  1300. msh_exec(cmd, rt_strlen(cmd)); /* 输出经控制台进入捕获设备 -> 行缓存 -> 屏幕 */
  1301. if (switched)
  1302. {
  1303. dbg_console_restore(); /* 恢复控制台;其内部 open 会洗掉接收位 */
  1304. dbg_chain_fixup(); /* 补回本机口接收模式位,保 tshell 输入 */
  1305. }
  1306. dbg_con_flush(RT_TRUE);
  1307. }
  1308. static void dbg_exec_thread_entry(void *parameter)
  1309. {
  1310. (void)parameter;
  1311. while (1)
  1312. {
  1313. if (rt_sem_take(&dbg_exec_sem, RT_WAITING_FOREVER) != RT_EOK)
  1314. {
  1315. continue;
  1316. }
  1317. dbg_exec_busy = 1;
  1318. dbg_exec_run();
  1319. dbg_exec_busy = 0;
  1320. }
  1321. }
  1322. static void dbg_exec_init(void)
  1323. {
  1324. if (dbg_exec_inited)
  1325. {
  1326. return;
  1327. }
  1328. dbg_exec_inited = RT_TRUE;
  1329. rt_sem_init(&dbg_exec_sem, "dbgsem", 0, RT_IPC_FLAG_FIFO);
  1330. if (rt_thread_init(&dbg_exec_thread, "dbgexec", dbg_exec_thread_entry, RT_NULL,
  1331. dbg_exec_stack, sizeof(dbg_exec_stack), DBG1_EXEC_PRIO, 10) == RT_EOK)
  1332. {
  1333. rt_thread_startup(&dbg_exec_thread);
  1334. dbg_exec_ready = RT_TRUE;
  1335. }
  1336. }
  1337. /* 从 "dbg:" 之后的载荷中提取命令并提交执行线程。
  1338. * 载荷 = 屏幕文本框的整个内容("单框收发"设计),提取规则:
  1339. * 取**最后一个 ">>"** 之后的文本作为命令(框尾固定有提示符 "msh >> ",用户在其后输入);
  1340. * 载荷中不含 ">>" 时按"纯命令"处理(兼容另接独立命令输入框的用法);
  1341. * 再裁剪:跳过前导空白、截至行尾/上限、去尾部空白。 */
  1342. static void dbg_exec_submit(const char *payload)
  1343. {
  1344. const char *cmd = payload;
  1345. const char *p = payload;
  1346. const char *last = RT_NULL;
  1347. rt_uint16_t n = 0;
  1348. dbg_exec_init();
  1349. while ((p = rt_strstr(p, ">>")) != RT_NULL) /* 定位最后一个 ">>" */
  1350. {
  1351. last = p;
  1352. p += 2;
  1353. }
  1354. if (last != RT_NULL)
  1355. {
  1356. cmd = last + 2;
  1357. }
  1358. while (*cmd == ' ' || *cmd == '\t') /* 跳过提示符后的空白 */
  1359. {
  1360. cmd++;
  1361. }
  1362. while (cmd[n] != '\0' && cmd[n] != '\r' && cmd[n] != '\n' && n < DBG1_CMD_MAX)
  1363. {
  1364. dbg_cmd_buf[n] = cmd[n];
  1365. n++;
  1366. }
  1367. while (n > 0 && (dbg_cmd_buf[n - 1] == ' ' || dbg_cmd_buf[n - 1] == '\t'))
  1368. {
  1369. n--;
  1370. }
  1371. dbg_cmd_buf[n] = '\0';
  1372. if (n == 0)
  1373. {
  1374. return; /* 空命令(只发了提示符)不处理 */
  1375. }
  1376. if (dbg_exec_ready == RT_FALSE)
  1377. {
  1378. dbg_con_puts("(调试执行线程创建失败)\n");
  1379. dbg_con_flush(RT_TRUE);
  1380. return;
  1381. }
  1382. if (dbg_exec_busy)
  1383. {
  1384. dbg_con_puts("(上一条命令仍在执行,请稍候)\n");
  1385. dbg_con_flush(RT_TRUE);
  1386. return;
  1387. }
  1388. rt_sem_release(&dbg_exec_sem);
  1389. }
  1390. /* ---------- debug1 按钮处理 ---------- */
  1391. static void dbg_console_on(void)
  1392. {
  1393. dbg_con_dev_init();
  1394. if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
  1395. {
  1396. dbg_con_puts("(捕获设备注册失败,无法打开屏幕调试)\n");
  1397. dbg_con_flush(RT_TRUE);
  1398. return;
  1399. }
  1400. if (dbg_mirror_on == RT_FALSE)
  1401. {
  1402. dbg_console_save(); /* 记录当前控制台(uart1) */
  1403. rt_console_set_device(DBG1_CAP_DEV_NAME); /* 输出切到捕获设备;日志仍转发本机口 */
  1404. dbg_chain_fixup(); /* 保住本机口接收模式位,PC 输入不受影响 */
  1405. dbg_mirror_on = RT_TRUE;
  1406. }
  1407. dbg_con_puts("--- 屏幕调试已打开:日志同步到本框;在提示符后输入命令按[发送] ---\n");
  1408. dbg_con_flush(RT_TRUE);
  1409. }
  1410. static void dbg_console_off(void)
  1411. {
  1412. if (dbg_mirror_on)
  1413. {
  1414. dbg_console_restore(); /* 恢复控制台:内部 open 会洗掉接收模式位 */
  1415. dbg_mirror_on = RT_FALSE;
  1416. dbg_chain_fixup(); /* 必须补回,否则恢复后本机口无法输入 */
  1417. }
  1418. dbg_con_puts("--- 屏幕调试已关闭(控制台已恢复) ---\n");
  1419. dbg_con_flush(RT_TRUE);
  1420. }
  1421. static void dbg_con_clear(void)
  1422. {
  1423. dbg_con_len = 0;
  1424. dbg_con_raw[0] = '\0';
  1425. /* 复位重复过滤表:否则本调用点缓存仍是上次发出的内容,
  1426. * 二次点击"清空"(期间文本框已被日志刷新)会被去重逻辑跳过 */
  1427. hmi_dedupe_reset();
  1428. HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"" DBG1_PROMPT "\""); /* 清空后仅保留提示符便于继续输入 */
  1429. }
  1430. static void dbg_erase_params(void)
  1431. {
  1432. LOG_I("debug1: 擦除参数(恢复出厂默认)");
  1433. if (device_status.Power_Type == 1) ResetParamDefault_DC(); /* 电池供电默认值 */
  1434. else ResetParamDefault(); /* 交流供电默认值 */
  1435. dbg_con_puts("--- 参数已恢复出厂默认值 ---\n");
  1436. dbg_con_flush(RT_TRUE);
  1437. }
  1438. static void dbg_start_app(void)
  1439. {
  1440. LOG_I("debug1: 启动APP(清除BL停留标志), 重启");
  1441. device_status.stop_in_bootloader = 0;
  1442. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
  1443. sizeof(device_status.stop_in_bootloader));
  1444. rt_thread_mdelay(200);
  1445. rt_hw_cpu_reset();
  1446. }
  1447. static void dbg_restore_fw(void)
  1448. {
  1449. uint8_t pending = 1;
  1450. LOG_I("debug1: 恢复固件(置标志并重启进BL)");
  1451. /* APP 正在 app 分区运行,不能自擦运行分区;置标志重启进 BL 恢复。
  1452. * restore_pending:供 BL 读取(BL 支持后自动执行 df_area→app 恢复)。 */
  1453. device_status.stop_in_bootloader = 1;
  1454. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
  1455. sizeof(device_status.stop_in_bootloader));
  1456. ef_set_env_blob("restore_pending", &pending, sizeof(pending));
  1457. rt_thread_mdelay(200);
  1458. rt_hw_cpu_reset();
  1459. }
  1460. /* debug1 页指令分发:返回 RT_TRUE 表示指令属于 debug1 并已处理。
  1461. * 注意:调用点必须放在 HMI_Decode 的 if/else 链最前 —— "dbg:<命令>" 的
  1462. * 命令内容里可能包含 reboot/clear 等既有分支的关键字,不能让它们先匹配。
  1463. * 另:能收到本页按钮指令即证明用户停留在调试页,匹配成功时顺带刷新页面心跳
  1464. * (s_dbg1_page_tick),使按钮在未配 1 秒心跳定时器时也能独立工作。 */
  1465. static rt_bool_t hmi_debug1_handle(const char *frame)
  1466. {
  1467. if (rt_strncmp(frame, "dbg:", 4) == 0) /* b4 发送:执行控制台命令 */
  1468. {
  1469. s_dbg1_page_tick = rt_tick_get();
  1470. dbg_exec_submit(frame + 4);
  1471. return RT_TRUE;
  1472. }
  1473. if (strstr(frame, "dbg_on") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_on(); return RT_TRUE; }
  1474. if (strstr(frame, "dbg_off") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_off(); return RT_TRUE; }
  1475. if (strstr(frame, "dbg_clr") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_con_clear(); return RT_TRUE; }
  1476. if (strstr(frame, "dbg_erase") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_erase_params(); return RT_TRUE; }
  1477. if (strstr(frame, "dbg_restore") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_restore_fw(); return RT_TRUE; }
  1478. if (strstr(frame, "startapp") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_start_app(); return RT_TRUE; }
  1479. return RT_FALSE;
  1480. }
  1481. /* ---------- debug1 信息区整批刷新("getDebugData" 触发) ---------- */
  1482. /* JEDEC 厂商 ID → 名称(取值与 SFUD 内置 mf_table 一致,仅供显示用)。
  1483. * 2026-10-13 新增:芯片走 SFDP 识别路径时 SFUD 不填 chip.name(见 t10 注释),
  1484. * 用厂商名兜底可读性最好。 */
  1485. static const char *dbg1_flash_vendor(uint8_t mf_id)
  1486. {
  1487. static const struct { uint8_t id; const char *name; } tbl[] = {
  1488. {0x01, "Cypress"}, {0x04, "Fujitsu"}, {0x1C, "EON"}, {0x1F, "Atmel"},
  1489. {0x20, "Micron"}, {0x37, "AMIC"}, {0x62, "Sanyo"}, {0x89, "Intel"},
  1490. {0x8C, "ESMT"}, {0xA1, "Fudan"}, {0xAD, "Hyundai"}, {0xBF, "SST"},
  1491. {0xC2, "Micronix"}, {0xC8, "GigaDevice"}, {0xD5, "ISSI"}, {0xEF, "Winbond"},
  1492. };
  1493. rt_size_t i;
  1494. for (i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++)
  1495. {
  1496. if (tbl[i].id == mf_id)
  1497. {
  1498. return tbl[i].name;
  1499. }
  1500. }
  1501. return RT_NULL;
  1502. }
  1503. /* 组装 FAL 分区表文本(t14 下发用):每行 "分区名 所属Flash 起址K+大小K",
  1504. * 行间用 TJC 换行转义 "\r"(两个字符)。例:
  1505. * bl onchip_flash 0K+200K / app onchip_flash 200K+312K
  1506. * easyflash MYFLASH16 0K+3072K / fm_area ... / df_area ...
  1507. * 超出 cap 时按整行截断(宁少一行,不给屏幕发半行)。 */
  1508. static void dbg1_build_part_txt(char *out, rt_uint16_t cap)
  1509. {
  1510. size_t part_num = 0;
  1511. const struct fal_partition *tbl = fal_get_partition_table(&part_num);
  1512. rt_uint16_t n = 0;
  1513. size_t i;
  1514. out[0] = '\0';
  1515. if (tbl == RT_NULL || part_num == 0)
  1516. {
  1517. rt_snprintf(out, cap, "%s", "分区表不可用");
  1518. return;
  1519. }
  1520. for (i = 0; i < part_num; i++)
  1521. {
  1522. char line[56];
  1523. int ln = rt_snprintf(line, sizeof(line), "%s %s %luK+%luK",
  1524. tbl[i].name, tbl[i].flash_name,
  1525. (unsigned long)(tbl[i].offset / 1024),
  1526. (unsigned long)(tbl[i].len / 1024));
  1527. if (ln <= 0)
  1528. {
  1529. continue;
  1530. }
  1531. if (ln > (int)sizeof(line) - 1) /* rt_snprintf 返回"应有长度",先夹取再参与判界/拷贝 */
  1532. {
  1533. ln = (int)sizeof(line) - 1;
  1534. }
  1535. if ((rt_uint32_t)n + (rt_uint32_t)ln + 2 + 1 > cap) /* +2 换行 +1 结束符 */
  1536. {
  1537. break;
  1538. }
  1539. if (n > 0)
  1540. {
  1541. out[n++] = '\\';
  1542. out[n++] = 'r';
  1543. }
  1544. rt_memcpy(out + n, line, ln);
  1545. n += (rt_uint16_t)ln;
  1546. out[n] = '\0';
  1547. }
  1548. }
  1549. static void hmi_refresh_debug1(void)
  1550. {
  1551. char buf[48];
  1552. rt_uint32_t total = 0, used = 0, max_used = 0, sec, pct = 0;
  1553. sfud_flash_t nor;
  1554. sys_monitor_refresh(); /* CPU 利用率 / 运行时长(非阻塞,任意线程可用) */
  1555. /* t0 RT-Thread版本 / t1 内核 / t2 BSP型号 */
  1556. rt_snprintf(buf, sizeof(buf), "%d.%d.%d", (int)RT_VERSION, (int)RT_SUBVERSION, (int)RT_REVISION);
  1557. HMI_WRITE("debug1.t0.txt=\"%s\"", buf);
  1558. HMI_WRITE("debug1.t1.txt=\"%s\"", DBG1_CORE_NAME);
  1559. HMI_WRITE("debug1.t2.txt=\"%s\"", DBG1_BSP_NAME);
  1560. /* t13 运行时长:总小时:分:秒(如 72:05:11,小时持续累加、不跨天回绕)。
  1561. * 2026-10-13 修正:① 秒位原为累计秒(屏幕显示形如 2:03:7397),改为 sec%60;
  1562. * ② 时位去掉 %86400 掩码——去掉"天"字段后若保留掩码,24h 处会静默回绕、
  1563. * 长跑设备无法区分"已运行 2 天还是 26 天"。 */
  1564. sec = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
  1565. rt_snprintf(buf, sizeof(buf), "%lu:%02lu:%02lu",
  1566. (unsigned long)(sec / 3600),
  1567. (unsigned long)((sec % 3600) / 60),
  1568. (unsigned long)(sec % 60));
  1569. HMI_WRITE("debug1.t13.txt=\"%s\"", buf);
  1570. /* 版本区:t5 BL版本 / t3 固件版本 / t4 编译时间 */
  1571. HMI_WRITE("debug1.t5.txt=\"%s\"", device_status.driver_bl);
  1572. HMI_WRITE("debug1.t3.txt=\"%s\"", device_status.driver);
  1573. HMI_WRITE("debug1.t4.txt=\"%s\"", device_status.driver_compile);
  1574. /* CPU:t11 文本 + j0 进度条(0-100) */
  1575. HMI_WRITE("debug1.t11.txt=\"%d%%\"", system_status.cpu_usage);
  1576. HMI_WRITE("debug1.j0.val=%d", system_status.cpu_usage);
  1577. /* RAM:t12 文本 + j1 进度条(0-100) */
  1578. rt_memory_info(&total, &used, &max_used);
  1579. if (total > 0)
  1580. {
  1581. pct = (rt_uint32_t)used * 100 / total;
  1582. rt_snprintf(buf, sizeof(buf), "%lu%%", (unsigned long)pct);
  1583. HMI_WRITE("debug1.t12.txt=\"%s\"", buf);
  1584. HMI_WRITE("debug1.j1.val=%d", (int)pct);
  1585. }
  1586. else
  1587. {
  1588. HMI_WRITE("debug1.t12.txt=\"%s\"", "N/A");
  1589. HMI_WRITE("debug1.j1.val=%d", 0);
  1590. }
  1591. /* t10 外挂Flash(SFUD 探测结果)。
  1592. * 2026-10-13 修复:芯片支持 JESD216(SFDP) 时,SFUD 走 SFDP 识别路径并**显式置
  1593. * chip.name = NULL**(sfud.c:279,SFDP 不含型号名;实测本机 16MB 闪存即此路径),
  1594. * 原实现直接 %s 打印会输出 "(NULL)"。现按三级兜底保证内容有效:
  1595. * ① 型号表命中 → "W25Q128 16MB"(原样保留);
  1596. * ② 型号为空 → "Winbond EF4018 16MB"(厂商名 + JEDEC ID + 容量,信息全部实读自芯片);
  1597. * ③ 厂商也未知 → "JEDEC EF4018 16MB"。 */
  1598. nor = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME);
  1599. if (nor != RT_NULL)
  1600. {
  1601. const char *vendor = dbg1_flash_vendor(nor->chip.mf_id);
  1602. unsigned cap_mb = (unsigned)(nor->chip.capacity / (1024 * 1024));
  1603. if (nor->chip.name != RT_NULL && nor->chip.name[0] != '\0')
  1604. {
  1605. rt_snprintf(buf, sizeof(buf), "%s %uMB", nor->chip.name, cap_mb);
  1606. }
  1607. else if (vendor != RT_NULL)
  1608. {
  1609. rt_snprintf(buf, sizeof(buf), "%s %02X%02X%02X %uMB", vendor,
  1610. (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
  1611. (unsigned)nor->chip.capacity_id, cap_mb);
  1612. }
  1613. else
  1614. {
  1615. rt_snprintf(buf, sizeof(buf), "JEDEC %02X%02X%02X %uMB",
  1616. (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
  1617. (unsigned)nor->chip.capacity_id, cap_mb);
  1618. }
  1619. }
  1620. else
  1621. {
  1622. rt_snprintf(buf, sizeof(buf), "未检测到");
  1623. }
  1624. HMI_WRITE("debug1.t10.txt=\"%s\"", buf);
  1625. /* t14 分区表详情(FAL 分区表:分区名 所属Flash 起址K+大小K,多行)。
  1626. * 内容静态,配合发送去重仅首次(及换页/10s 复位后)实际占用串口带宽。 */
  1627. {
  1628. char part[DBG1_PART_MAX + 2];
  1629. dbg1_build_part_txt(part, sizeof(part));
  1630. HMI_WRITE("debug1." DBG1_PART_OBJ ".txt=\"%s\"", part);
  1631. }
  1632. }
  1633. /* 屏幕版本握手串变化检测。
  1634. * 说明:屏幕心跳里 SwversionV** 可能与 getFullData 一样每秒都发,
  1635. * 若每次都复位重复过滤表会让去重完全失效;因此只在"版本串发生变化"时复位
  1636. * (换型号/换页面工程才会变)。屏幕重启但版本不变的情况由 HMI_DEDUPE_RESYNC_SEC
  1637. * 周期复位兜底(最坏 10s 后恢复全量刷新)。
  1638. * 2026-10-15:版本串变化还用于触发版本类型切换(hmi_switch_version),
  1639. * 见 HMI_Decode 的 SwversionV 分支。 */
  1640. static char s_last_hmi_ver[16] = {0};
  1641. static rt_bool_t hmi_ver_changed(const char *frame)
  1642. {
  1643. char cur[16];
  1644. int i = 0;
  1645. while (frame[i] != '\0' && frame[i] != '\r' && frame[i] != '\n' && i < (int)sizeof(cur) - 1)
  1646. {
  1647. cur[i] = frame[i];
  1648. i++;
  1649. }
  1650. cur[i] = '\0';
  1651. if (rt_strcmp(cur, s_last_hmi_ver) != 0)
  1652. {
  1653. rt_strncpy(s_last_hmi_ver, cur, sizeof(s_last_hmi_ver) - 1);
  1654. s_last_hmi_ver[sizeof(s_last_hmi_ver) - 1] = '\0';
  1655. LOG_I("hmi version frame changed: %s", s_last_hmi_ver);
  1656. return RT_TRUE;
  1657. }
  1658. return RT_FALSE;
  1659. }
  1660. /* ==================== 屏幕版本串 → 版本类型切换(2026-10-15) ====================
  1661. * 触发:HMI_Decode 的 SwversionV 分支中,hmi_ver_changed() 检测到屏幕版本握手串
  1662. * 发生变化时调用一次——即连接/更换不同版本的屏幕工程时,按串切换
  1663. * dtype/version_type/driver 并写 EF(MA_save/param_mirror_sync)。
  1664. * 历史:本逻辑原为 HMI_Decode 内一段 else-if 链(V12/V11/V1/V43/V42/V41/V4/V6/
  1665. * V30/V7),自 SwversionV 整页刷新分支加入后所有版本帧都被其先命中,
  1666. * 原链永远不可达(死代码);现迁移为本函数并改由版本串变化事件触发,
  1667. * 逐分支语义与原链一致(分包防误切仍用 hmi_cmd_match 严格匹配)。 */
  1668. static void hmi_switch_version(const char *frame)
  1669. {
  1670. //高空-带拍照
  1671. if (strstr(frame,"SwversionV12")!=NULL)
  1672. {
  1673. device_status.dtype = 3;
  1674. if(device_status.version_type != 12)
  1675. {
  1676. device_status.version_type = 12;
  1677. sprintf(device_status.driver, "%s",VERSION_GK);
  1678. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1679. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1680. MA_save();
  1681. param_mirror_sync();
  1682. }
  1683. else {
  1684. }
  1685. }
  1686. //高空
  1687. else if (strstr(frame,"SwversionV11")!=NULL)
  1688. {
  1689. device_status.dtype = 3;
  1690. if(device_status.version_type != 11)
  1691. {
  1692. device_status.version_type = 11;
  1693. sprintf(device_status.driver, "%s",VERSION_GK);
  1694. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1695. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1696. MA_save();
  1697. param_mirror_sync();
  1698. }
  1699. else {
  1700. }
  1701. return;
  1702. }
  1703. //1.0
  1704. /* 注意:必须用严格匹配——"SwversionV1" 是 "SwversionV11/12" 的前缀,
  1705. * 串口分包时残段会误切到本分支(同 V4 的问题,见 hmi_cmd_match 说明) */
  1706. else if (hmi_cmd_match(frame,"SwversionV1"))
  1707. {
  1708. if(device_status.version_type != 1)
  1709. {
  1710. device_status.dtype = 3;
  1711. device_status.version_type = 1;
  1712. sprintf(device_status.driver, "%s",VERSION_ZP);
  1713. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1714. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1715. MA_save();
  1716. param_mirror_sync();
  1717. }
  1718. else {
  1719. }
  1720. return;
  1721. }
  1722. //中端款
  1723. else if (strstr(frame,"SwversionV43")!=NULL) {
  1724. device_status.dtype = 3;
  1725. if(device_status.version_type != 43)
  1726. {
  1727. device_status.dtype = 3;
  1728. device_status.version_type = 43;
  1729. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1730. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1731. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1732. MA_save();
  1733. param_mirror_sync();
  1734. }
  1735. return;
  1736. }
  1737. //低端款
  1738. else if (strstr(frame,"SwversionV42")!=NULL) {
  1739. device_status.dtype = 3;
  1740. if(device_status.version_type != 42)
  1741. {
  1742. device_status.dtype = 3;
  1743. device_status.version_type = 42;
  1744. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1745. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1746. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1747. MA_save();
  1748. param_mirror_sync();
  1749. }
  1750. return;
  1751. }
  1752. //4.0
  1753. else if (strstr(frame,"SwversionV41")!=NULL) {
  1754. device_status.dtype = 3;
  1755. if(device_status.version_type != 41)
  1756. {
  1757. device_status.dtype = 3;
  1758. device_status.version_type = 41;
  1759. sprintf(device_status.driver, "%s",VERSION_HK);
  1760. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1761. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
  1762. DTU_Write("SwversionV41");
  1763. MA_save();
  1764. param_mirror_sync();
  1765. }
  1766. else {
  1767. }
  1768. return;
  1769. }
  1770. //水稻
  1771. /* 注意:必须用严格匹配——"SwversionV4" 是 "SwversionV41/42/43" 的前缀。
  1772. * 实测(2026-10-08):串口分包时 "SwversionV43" 的前段 "SwversionV4" 会误切到
  1773. * 本分支(version_type 43→4、误报 RTU、写 Flash),随后完整帧又切回 43,
  1774. * 造成配置来回翻转。见 hmi_cmd_match 说明。 */
  1775. else if (hmi_cmd_match(frame,"SwversionV4"))
  1776. {
  1777. device_status.dtype = 3;
  1778. if(device_status.version_type != 4)
  1779. {
  1780. device_status.version_type = 4;
  1781. sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  1782. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1783. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1784. MA_save();
  1785. param_mirror_sync();
  1786. }
  1787. else {
  1788. }
  1789. return;
  1790. }
  1791. //吸虫塔
  1792. else if (strstr(frame,"SwversionV6")!=NULL) {
  1793. device_status.dtype = 3;
  1794. if(device_status.version_type != 6)
  1795. {
  1796. device_status.version_type = 6;
  1797. sprintf(device_status.driver, "%s",VERSION_XCT);
  1798. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1799. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
  1800. MA_save();
  1801. param_mirror_sync();
  1802. }
  1803. }
  1804. else if((strstr(frame,"SwversionV7")!=NULL)) {
  1805. if(device_status.dtype!=7)
  1806. {
  1807. LOG_D("设置版本类型:7");
  1808. device_status.dtype = 7;
  1809. device_status.version_type = 7;
  1810. ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //设备类型
  1811. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  1812. DTU_Write("SwversionV7");
  1813. MA_save();
  1814. param_mirror_sync();
  1815. }
  1816. }
  1817. }
  1818. static void HMI_Decode(void)
  1819. {
  1820. char tmp[128];
  1821. time_t now = time(RT_NULL);
  1822. static struct tm tm;
  1823. gmtime_r(&now, &tm);
  1824. char *json_temp;
  1825. char *var=NULL;
  1826. if(device_status.refresh_num==0) device_status.refresh_num = 5;
  1827. /* ---- 屏幕 debug1 调试页:心跳刷新(getDebugData)+ 按钮/控制台指令 ----
  1828. * 必须放在 if/else 链最前:dbg:<命令> 的内容里可能包含 reboot/clear 等
  1829. * 既有分支的关键字,让它们先匹配会导致误动作。 */
  1830. if (strstr(rx_buffer, "getDebugData") != RT_NULL)
  1831. {
  1832. s_dbg1_page_tick = rt_tick_get(); /* 调试页心跳:离开该页(超时)后暂停日志推送 */
  1833. dbg_chain_fixup(); /* 自愈:本机口接收位若被其它路径改动,1 秒内自动恢复 */
  1834. HMI_BATCH_BEGIN();
  1835. hmi_refresh_debug1();
  1836. dbg_con_flush(RT_FALSE); /* 回页/持续在页:把最新控制台内容补推一次(带去重) */
  1837. HMI_BATCH_END();
  1838. }
  1839. else if (hmi_debug1_handle(rx_buffer))
  1840. {
  1841. /* debug1 指令已处理(dbg:/dbg_on/dbg_off/dbg_clr/dbg_erase/dbg_restore/startapp),
  1842. * 页面心跳已在处理函数内随之刷新。 */
  1843. }
  1844. else if(strstr(rx_buffer,"SwversionV") != NULL || strstr(rx_buffer,"getFullData") != NULL)
  1845. {
  1846. /* 屏幕版本握手串变化(换型号/换页面工程) → 复位重复过滤表 + 按串切换版本类型,
  1847. * 并立即整页全量刷新 */
  1848. if (strstr(rx_buffer,"SwversionV") != NULL && hmi_ver_changed(rx_buffer))
  1849. {
  1850. hmi_dedupe_reset();
  1851. /* 2026-10-15:版本串变化后按字符设置版本类型(见 hmi_switch_version 说明) */
  1852. hmi_switch_version(rx_buffer);
  1853. }
  1854. HMI_BATCH_BEGIN();
  1855. hmi_refresh_pages(&tm);
  1856. hmi_refresh_work_proc(); /* 拍照/清扫状态 + t1~t6 工作过程显示 */
  1857. HMI_BATCH_END();
  1858. }
  1859. else if (strstr(rx_buffer,"getClockData")!=NULL) {
  1860. //更新定时模式
  1861. HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
  1862. //更新光控定时时长
  1863. HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
  1864. //更新时控开始
  1865. HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
  1866. //更新时控结束
  1867. HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
  1868. }
  1869. else if (strstr(rx_buffer,"button")!=NULL) {
  1870. LOG_D("button!");
  1871. DTU_Write("button");
  1872. }
  1873. //拍照
  1874. else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
  1875. #if 1
  1876. device_status.test_flag = 1;
  1877. device_status.photo_ws = 0;
  1878. rt_thread_delay(500);
  1879. device_status.photo_ws = 1;
  1880. char *photo = makeJson_Warn("takephoto","control");
  1881. DTU_Write("%s",photo);
  1882. rt_free(photo);
  1883. #else
  1884. Hk_Take_io();
  1885. #endif
  1886. }
  1887. else if(strstr(rx_buffer,"putTest.switchPhoto=0")!=NULL) {
  1888. device_status.photo_ws = 0;
  1889. rt_thread_delay(500);
  1890. LOG_I("停止拍照!");
  1891. }
  1892. else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
  1893. #if 1
  1894. device_status.photo_ws = 0;
  1895. rt_thread_delay(500);
  1896. device_status.photo_ws = 1;
  1897. char *photo = makeJson_Warn("takephoto","control");
  1898. DTU_Write("%s",photo);
  1899. rt_free(photo);
  1900. #else
  1901. Hk_Take_io();
  1902. #endif
  1903. }
  1904. //转盘复位
  1905. else if (strstr(rx_buffer,"turn_init")!=NULL) {
  1906. LOG_I("转仓复位!");
  1907. ctr_turn(0,0);
  1908. }
  1909. //转盘
  1910. else if (strstr(rx_buffer,"turn")!=NULL) {
  1911. LOG_I("转仓!");
  1912. ctr_turn(90,0);
  1913. }
  1914. //打开电磁阀
  1915. else if (strstr(rx_buffer,"putTest.switchDrop=1")!=NULL) {
  1916. LOG_I("开始滴液!");
  1917. #if Color_EN == 1
  1918. device_status.run_sta = 5;
  1919. #else
  1920. ctr_shake(ON, device_status.shake_sec);
  1921. #endif
  1922. }
  1923. //关闭电磁阀
  1924. else if (strstr(rx_buffer,"putTest.switchDrop=0")!=NULL) {
  1925. LOG_I("停止滴液!");
  1926. ctr_shake(OFF, 0);
  1927. }
  1928. //打开补光
  1929. else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
  1930. LOG_I("打开补光!!");
  1931. ctr_led(ON);
  1932. }
  1933. //关闭补光
  1934. else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
  1935. LOG_I("关闭补光!!!");
  1936. ctr_led(OFF);
  1937. }
  1938. //关闭加热
  1939. else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
  1940. device_status.heat_status = 0;
  1941. ctr_Heat(OFF);
  1942. hmi_test_heat_stop(); /* 手动关闭:撤销屏幕测试加热的自动关闭定时 */
  1943. LOG_I("Close Heat!");
  1944. }
  1945. //打开加热(屏幕测试加热:HMI_TEST_HEAT_TIMEOUT 秒后自动关闭)
  1946. else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
  1947. device_status.heat_status = 1;
  1948. ctr_Heat(ON);
  1949. hmi_test_heat_start();
  1950. LOG_I("Open Heat! (auto off in %d s)", HMI_TEST_HEAT_TIMEOUT);
  1951. }
  1952. //打开制冷
  1953. else if(strstr(rx_buffer,"putTest.switchCold=1")!=NULL) {
  1954. ctr_Cold(ON);
  1955. LOG_I("Open Cold!");
  1956. }
  1957. //关闭制冷
  1958. else if(strstr(rx_buffer,"putTest.switchCold=0")!=NULL) {
  1959. ctr_Cold(OFF);
  1960. LOG_I("Close Cold!");
  1961. }
  1962. //打开采集
  1963. else if(strstr(rx_buffer,"putTest.switchCollect=1")!=NULL){
  1964. ctr_Collect_Fan(ON);
  1965. LOG_I("采集风机已打开!");
  1966. }
  1967. //关闭采集
  1968. else if(strstr(rx_buffer,"putTest.switchCollect=0")!=NULL){
  1969. ctr_Collect_Fan(OFF);
  1970. LOG_I("采集风机已关闭!");
  1971. }
  1972. //打开散热
  1973. else if(strstr(rx_buffer,"putTest.switchCool=1")!=NULL){
  1974. ctr_Cool_Fan(ON);
  1975. LOG_I("散热风机已打开!");
  1976. }
  1977. //关闭散热
  1978. else if(strstr(rx_buffer,"putTest.switchCool=0")!=NULL){
  1979. ctr_Cool_Fan(OFF);
  1980. LOG_I("散热风机已关闭!");
  1981. }
  1982. //开始对焦拍照
  1983. else if(strstr(rx_buffer,"putTest.switchAutopic=1")!=NULL){
  1984. device_status.autotakepic=1;
  1985. rt_kprintf("开始对焦拍照!\n");
  1986. }
  1987. //停止对焦拍照
  1988. else if(strstr(rx_buffer,"putTest.switchAutopic=0")!=NULL){
  1989. device_status.autotakepic=0;
  1990. }
  1991. //粘附测试-开
  1992. else if(strstr(rx_buffer,"putTest.switchFsl=1")!=NULL){
  1993. rt_kprintf("粘附测试开!\n");
  1994. device_status.fsl_sw = 1;
  1995. }
  1996. //粘附测试-关
  1997. else if(strstr(rx_buffer,"putTest.switchFsl=0")!=NULL){
  1998. rt_kprintf("粘附测试关!\n");
  1999. pwm_off(2, 0);
  2000. pwm_off(3,0);
  2001. }
  2002. else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
  2003. {
  2004. LOG_I("开始一键测试!!!");
  2005. device_status.auto_test_sw=1;
  2006. }
  2007. /* 修复(2026-10-10):原条件误写成 switchAutotest=1,与上一个分支完全重复、
  2008. * 永远不可达,导致屏幕上的"停止一键测试"无效;按日志本意改为 =0。 */
  2009. else if (strstr(rx_buffer,"putTest.switchAutotest=0")!=NULL)
  2010. {
  2011. LOG_I("停止一键测试!!!");
  2012. device_status.auto_test_sw=0;
  2013. }
  2014. //清扫
  2015. else if (strstr(rx_buffer,"clear")!=NULL) {
  2016. rt_kprintf("\n清扫!\n");
  2017. if(device_status.version_type==4|| //水稻
  2018. device_status.version_type==41|| //4.0
  2019. device_status.version_type==42|| //低端款
  2020. device_status.version_type==43|| //中端款
  2021. device_status.version_type==12) //高空带拍照
  2022. {
  2023. device_status.clear_status=1;
  2024. }
  2025. else if (device_status.version_type==1) {
  2026. device_status.clear_status=2;
  2027. }
  2028. else if (device_status.version_type==6) {
  2029. ctr_JD_Clear(ON);
  2030. }
  2031. }
  2032. //震动
  2033. else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
  2034. LOG_I("震动!");
  2035. if(device_status.version_type==6)
  2036. {
  2037. Motor_shake(ON, device_status.shake_sec);
  2038. }
  2039. else{
  2040. ctr_shake(ON,device_status.shake_sec);
  2041. }
  2042. }
  2043. //打开补光
  2044. else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
  2045. rt_kprintf("\n打开补光!!!\n");
  2046. ctr_led(ON);
  2047. }
  2048. //关闭补光
  2049. else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
  2050. rt_kprintf("\n关闭补光!!!\n");
  2051. ctr_led(OFF);
  2052. }
  2053. //打开灯管
  2054. else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
  2055. {
  2056. device_status.test_flag=1;
  2057. if(Read_Rain_Voltage()<250)
  2058. device_status.rain_status = 0;
  2059. rt_kprintf("Open Lamp!\n");
  2060. ctr_lamp(ON,1);
  2061. #if LAMP_EN == 1
  2062. ctr_lamp(ON,2);
  2063. ctr_lamp(ON,3);
  2064. #endif
  2065. }
  2066. //关闭灯管
  2067. else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
  2068. {
  2069. device_status.test_flag=0;
  2070. rt_kprintf("Close Lamp1!\n");
  2071. ctr_lamp(OFF,1);
  2072. #if LAMP_EN == 1
  2073. ctr_lamp(OFF,2);
  2074. ctr_lamp(OFF,3);
  2075. #endif
  2076. }
  2077. /* 说明(2026-10-10):此处原有第二组 "putTest.switchHeat=0/1" 分支,与上方的
  2078. * 同名分支条件完全相同、永远不可达(死代码),已删除;加热的开关动作统一由
  2079. * 上方第一组分支处理(含屏幕测试加热 10 分钟自动关闭,见 hmi_test_heat_start())。
  2080. * 同时修复了一处同类笔误:下方"停止一键测试"分支原条件误写成 switchAutotest=1
  2081. * (与上一个分支重复、不可达),已改为 =0,使屏幕上的停止按钮生效。 */
  2082. //打开上仓门
  2083. else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
  2084. if(device_status.dtype==7)
  2085. {
  2086. ctr_lamp(ON, 1);
  2087. ctr_Motor_Driver(1, 0,1000000,500);
  2088. LOG_I("开始步进上行!");
  2089. }
  2090. else
  2091. {
  2092. rt_kprintf("Open UP!\n");
  2093. ctr_cangmen_up(OFF);
  2094. device_status.ctr_up_motor = 1;
  2095. }
  2096. }
  2097. //关闭上仓门
  2098. else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
  2099. if(device_status.dtype==7)
  2100. {
  2101. LOG_I("停止步进上行!");
  2102. ctr_lamp(OFF, 1);
  2103. ctr_Motor_Driver(0, 0,0,0);
  2104. }
  2105. else
  2106. {
  2107. rt_kprintf("CLOSE UP!\n");
  2108. ctr_cangmen_up(OFF);
  2109. device_status.ctr_up_motor = 2;
  2110. }
  2111. }
  2112. //打开下仓门
  2113. else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
  2114. if(device_status.dtype==7)
  2115. {
  2116. LOG_I("开始步进下行!");
  2117. ctr_lamp(ON, 1);
  2118. ctr_Motor_Driver(1, 1,1000000,500);
  2119. }
  2120. else
  2121. {
  2122. ctr_cangmen_down(OFF);
  2123. device_status.ctr_down_motor = 1;
  2124. rt_kprintf("Open down!\n");
  2125. }
  2126. }
  2127. //关闭下仓门
  2128. else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
  2129. if(device_status.dtype==7)
  2130. {
  2131. ctr_lamp(OFF, 1);
  2132. ctr_Motor_Driver(0, 0,0,0);
  2133. rt_kprintf("停止步进下行!\n");
  2134. }
  2135. else
  2136. {
  2137. ctr_cangmen_down(OFF);
  2138. device_status.ctr_down_motor = 2;
  2139. rt_kprintf("CLOSE down!\n");
  2140. }
  2141. }
  2142. else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
  2143. if(device_status.collt_box_sta==0){
  2144. device_status.collt_box_sta = 111;
  2145. }else{
  2146. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2147. }
  2148. LOG_I("排虫测试!");
  2149. }
  2150. //排虫
  2151. else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
  2152. if(device_status.run_sta == 0){
  2153. device_status.run_sta = 111;
  2154. }else{
  2155. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2156. }
  2157. LOG_I("开始排虫!");
  2158. device_status.handle_bugger = 0;
  2159. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2160. param_mirror_sync();
  2161. }
  2162. else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
  2163. if(device_status.run_sta == 0){
  2164. device_status.run_sta = 112;
  2165. }else{
  2166. HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
  2167. }
  2168. LOG_I("开始收虫!");
  2169. device_status.handle_bugger = 1;
  2170. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  2171. param_mirror_sync();
  2172. }
  2173. //设置设备时间
  2174. else if (strstr(rx_buffer,"stamp=")!=NULL)
  2175. {
  2176. var = strstr(rx_buffer,"=");
  2177. var ++;
  2178. LOG_I("设置时间:%s",var);
  2179. static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
  2180. strncpy(year_s, var, 4);
  2181. strncpy(month_s,var+4,2);
  2182. strncpy(day_s,var+6,2);
  2183. strncpy(hour_s,var+8,2);
  2184. strncpy(min_s,var+10,2);
  2185. strncpy(sec_s,var+12,2);
  2186. if(atoi(year_s)>=2024&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
  2187. {
  2188. if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
  2189. {
  2190. LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
  2191. if(atoi(hour_s)>0&&atoi(hour_s)<=24&&atoi(min_s)>=0&&atoi(min_s)<=59&&atoi(sec_s)>=0&&atoi(sec_s)<=59)
  2192. {
  2193. LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
  2194. set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
  2195. MA_save();
  2196. }
  2197. //上传最新数据(立即触发一次上报更新RTU/平台时间;原为 num_updata=2 延时2秒上报)
  2198. dtu_request_report();
  2199. }
  2200. else {
  2201. LOG_E("set_date failed!");
  2202. }
  2203. }
  2204. }
  2205. //设备编码设置
  2206. else if (strstr(rx_buffer,"deviceId=")!=NULL)
  2207. {
  2208. char value_str[20];
  2209. memset(value_str,0,sizeof(value_str));
  2210. LOG_I("设备编码 长度:%d",strlen(rx_buffer));
  2211. if(strlen(rx_buffer)!=19){
  2212. LOG_E("编码长度错误!");
  2213. return;
  2214. }
  2215. var = strstr(rx_buffer,"=");
  2216. var ++;
  2217. strncpy(value_str,var,8); //设备编码为16位
  2218. if(strstr(value_str,"00000000")!=RT_NULL){
  2219. char *config_reset = makeJson_Config_Reset();
  2220. //发送json到RTU
  2221. DTU_Write("%s",config_reset);
  2222. rt_free(config_reset);
  2223. //恢复RTU的默认配置参数
  2224. }else{
  2225. rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
  2226. ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
  2227. LOG_I("解析到设备编码:%s",device_status.deviceId);
  2228. char *device_id = makeJson_Config_BDH(device_status.deviceId);
  2229. //发送json到RTU
  2230. DTU_Write("%s",device_id);
  2231. rt_free(device_id);
  2232. }
  2233. }
  2234. //定时设置
  2235. #if DZ_260630_1 == 0
  2236. else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
  2237. char value_str[10];
  2238. memset(value_str,0,sizeof(value_str));
  2239. if(device_status.dtype==7)
  2240. {
  2241. var = strstr(rx_buffer,"=");
  2242. var ++;
  2243. strncpy(value_str,var,2);
  2244. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2245. {
  2246. device_status.collecting_sh1 = atoi(value_str);
  2247. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
  2248. }
  2249. else {
  2250. LOG_E("Set collecting_sh1 Failed!");
  2251. }
  2252. memset(value_str,0,sizeof(value_str));
  2253. strncpy(value_str,var+2,2);
  2254. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2255. {
  2256. device_status.collecting_eh1 = atoi(value_str);
  2257. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
  2258. }
  2259. else {
  2260. LOG_E("Set collecting_eh1 Failed!");
  2261. }
  2262. memset(value_str,0,sizeof(value_str));
  2263. strncpy(value_str,var+4,2);
  2264. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2265. {
  2266. device_status.collecting_sh2 = atoi(value_str);
  2267. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
  2268. }
  2269. else {
  2270. LOG_E("Set collecting_sh2 Failed!");
  2271. }
  2272. memset(value_str,0,sizeof(value_str));
  2273. strncpy(value_str,var+6,2);
  2274. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2275. {
  2276. device_status.collecting_eh2 = atoi(value_str);
  2277. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
  2278. }
  2279. else {
  2280. LOG_E("Set collecting_eh2 Failed!");
  2281. }
  2282. memset(value_str,0,sizeof(value_str));
  2283. strncpy(value_str,var+8,2);
  2284. if(atoi(value_str)>=0&&atoi(value_str)<=23)
  2285. {
  2286. device_status.shake_sec = atoi(value_str);
  2287. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  2288. }
  2289. else {
  2290. LOG_E("Set shake_sec Failed!");
  2291. }
  2292. memset(value_str,0,sizeof(value_str));
  2293. strncpy(value_str,var+10,2);
  2294. if(atoi(value_str)>0&&atoi(value_str)<=24)
  2295. {
  2296. device_status.cul_time = atoi(value_str);
  2297. ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
  2298. }
  2299. else {
  2300. LOG_E("Set cul_time Failed!");
  2301. }
  2302. MA_save();
  2303. param_mirror_sync();
  2304. LOG_I("参数已保存:%d-%d-%d-%d-%d-%d",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2,device_status.shake_sec,device_status.cul_time);
  2305. return;
  2306. }
  2307. LOG_I("时间设置!");
  2308. var=strstr(rx_buffer,"=");
  2309. var++;
  2310. strncpy(value_str,var,2);
  2311. if(atoi(value_str) > 0 && atoi(value_str) <60)
  2312. {
  2313. if(device_status.version_type!=6)
  2314. {
  2315. /* 注:LAMP_EN==1(三灯管)时工作时长不再由 sunBar 决定
  2316. * (灯管按 LAMP_ROTATE_SEC 每小时轮换),此处值仅做记录 */
  2317. device_status.sunBar=atoi(value_str);
  2318. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  2319. }
  2320. else {
  2321. device_status.shake_sec = atoi(value_str);
  2322. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
  2323. }
  2324. // time_t now;
  2325. // time(&now);
  2326. // static struct tm nowstamp;
  2327. // nowstamp =*localtime(&now);
  2328. // nowstamp.tm_hour = nowstamp.tm_hour - 8;
  2329. // static rt_uint32_t nowstamp_num = 0;
  2330. // nowstamp_num = mktime(&nowstamp);
  2331. // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
  2332. }
  2333. else {
  2334. LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
  2335. }
  2336. memset(value_str,0,sizeof(value_str));
  2337. //时控开始时间
  2338. strncpy(value_str,var+2,2);
  2339. if(atoi(value_str) >= 0 && atoi(value_str) < 24)
  2340. {
  2341. #if 1
  2342. device_status.startBar=atoi(value_str);
  2343. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  2344. #endif
  2345. }
  2346. else {
  2347. LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
  2348. }
  2349. memset(value_str,0,sizeof(value_str));
  2350. //
  2351. strncpy(value_str,var+4,2);
  2352. if(atoi(value_str) >= 0&& atoi(value_str) < 24)
  2353. {
  2354. #if 1
  2355. device_status.stopBar=atoi(value_str);
  2356. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  2357. #endif
  2358. }
  2359. else {
  2360. LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
  2361. }
  2362. memset(value_str,0,sizeof(value_str));
  2363. //
  2364. strncpy(value_str,var+6,2);
  2365. if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
  2366. {
  2367. device_status.timeSwitch=atoi(value_str);
  2368. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2369. }
  2370. else {
  2371. if(device_status.version_type==6)
  2372. {
  2373. device_status.timeSwitch=1;
  2374. device_status.dattim=atoi(value_str);
  2375. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2376. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  2377. }
  2378. else {
  2379. LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
  2380. }
  2381. }
  2382. //2022/06/23,add:
  2383. rt_snprintf(tmp, sizeof(tmp), "屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp); /* 原 rt_sprintf 无长度上限,改带界变体 */
  2384. json_temp = makeJson_Warn(tmp, "修改工作时间");
  2385. DTU_Write("%s",json_temp);
  2386. rt_free(json_temp);
  2387. rt_thread_delay(2000);
  2388. MA_save();
  2389. param_mirror_sync();
  2390. /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
  2391. rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
  2392. }
  2393. #endif
  2394. else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
  2395. static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
  2396. static char value_temp[5];
  2397. LOG_I("系统设置!");
  2398. if(device_status.dtype==7)
  2399. {
  2400. LOG_I("系统设置,孢子仪!");
  2401. var = strstr(rx_buffer,"=");
  2402. var ++;
  2403. memset(value_temp,0,sizeof(value_temp));
  2404. strncpy(value_temp,var,2);
  2405. if(atoi(value_temp)==0 || atoi(value_temp) == 1)
  2406. {
  2407. device_status.cold_sw = atoi(value_temp); //制冷开关
  2408. ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
  2409. }
  2410. else {
  2411. LOG_E("制冷开关参数设置错误!");
  2412. }
  2413. memset(value_temp,0,sizeof(value_temp));
  2414. strncpy(value_temp,var+2,2);
  2415. if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
  2416. {
  2417. device_status.vol = atoi(value_temp); //音量
  2418. }
  2419. else {
  2420. LOG_E("音量参数设置错误!");
  2421. }
  2422. memset(value_temp,0,sizeof(value_temp));
  2423. strncpy(value_temp,var+4,2);
  2424. if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
  2425. {
  2426. device_status.heatvalue = atoi(value_temp); //设定的培养温度
  2427. ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2428. }
  2429. else {
  2430. LOG_E("培养温度参数设置失败!");
  2431. }
  2432. memset(value_temp,0,sizeof(value_temp));
  2433. strncpy(value_temp,var+6,2);
  2434. if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
  2435. {
  2436. device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
  2437. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2438. }
  2439. else {
  2440. LOG_E("数据上传间隔参数设置失败!");
  2441. }
  2442. memset(value_temp,0,sizeof(value_temp));
  2443. strncpy(value_temp,var+8,2);
  2444. if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
  2445. {
  2446. device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
  2447. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  2448. }
  2449. else {
  2450. LOG_E("数据息屏间隔参数设置失败!");
  2451. }
  2452. LOG_I("制冷开关:%d",device_status.cold_sw);
  2453. LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
  2454. MA_save();
  2455. param_mirror_sync();
  2456. return;
  2457. }
  2458. var = strstr(rx_buffer,"=");
  2459. var ++;
  2460. strncpy(clt_time_str,var,2);
  2461. if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
  2462. {
  2463. device_status.clt_time = atoi(clt_time_str); //收集时间
  2464. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  2465. }
  2466. strncpy(heattime_str,var+2,2);
  2467. if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
  2468. {
  2469. rt_kprintf("加热时间:%d\n",atoi(heattime_str));
  2470. device_status.heattime = atoi(heattime_str); //加热时间
  2471. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  2472. }
  2473. strncpy(heatvale_str,var+4,3);
  2474. if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
  2475. {
  2476. device_status.heatvalue=atoi(heatvale_str);
  2477. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  2478. }
  2479. strncpy(vol_str,var+7,3);
  2480. if(atoi(vol_str)>0&&atoi(vol_str)<8)
  2481. {
  2482. device_status.vol = atoi(vol_str); //音量
  2483. DTU_Write("volume=");
  2484. }
  2485. strncpy(thsp_str,var+10,4);
  2486. if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
  2487. {
  2488. device_status.thsp = atoi(thsp_str); //熄屏时间
  2489. }
  2490. else {
  2491. rt_kprintf("参数设置失败!!!\n");
  2492. }
  2493. rt_thread_delay(1000);
  2494. MA_save();
  2495. param_mirror_sync();
  2496. /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
  2497. rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
  2498. }
  2499. else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
  2500. /* 原实现 char rtc_h[2] + strncpy(…,2) 无结束符:两位数值(如 20)时
  2501. * atoi() 会越界读栈(未定义行为)。数组加 1 字节并补 '\0'。 */
  2502. char rtc_h[3];
  2503. var = strstr(rx_buffer,"=");
  2504. var++;
  2505. strncpy(rtc_h,var,2);
  2506. rtc_h[2] = '\0';
  2507. if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
  2508. {
  2509. set_time(atoi(rtc_h), 99, 99);
  2510. }
  2511. }
  2512. else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
  2513. char rtc_m[3];
  2514. var = strstr(rx_buffer,"=");
  2515. var++;
  2516. strncpy(rtc_m,var,2);
  2517. rtc_m[2] = '\0';
  2518. if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
  2519. {
  2520. set_time(99, atoi(rtc_m), 99);
  2521. }
  2522. }
  2523. else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
  2524. char rtc_s[3];
  2525. var = strstr(rx_buffer,"=");
  2526. var++;
  2527. strncpy(rtc_s,var,2);
  2528. rtc_s[2] = '\0';
  2529. if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
  2530. {
  2531. set_time(99, 99, atoi(rtc_s));
  2532. }
  2533. }
  2534. else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
  2535. {
  2536. static char dattim_str[3];
  2537. rt_kprintf("上传间隔设置!\n");
  2538. var = strstr(rx_buffer,"=");
  2539. var ++;
  2540. strncpy(dattim_str,var,2);
  2541. if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
  2542. {
  2543. device_status.dattim =atoi(dattim_str); //数据上传间隔
  2544. ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
  2545. MA_save();
  2546. param_mirror_sync();
  2547. }
  2548. else {
  2549. rt_kprintf("设置失败!!!\n");
  2550. }
  2551. /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
  2552. rt_kprintf("数据上传设置:%d\n",device_status.dattim);
  2553. }
  2554. //光控测试
  2555. else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
  2556. {
  2557. rt_kprintf("光控测试!!!");
  2558. ctr_lamp(ON,1);
  2559. device_status.sun_test = 1;
  2560. }
  2561. else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
  2562. {
  2563. static char shakesec_str[4];
  2564. LOG_I("震动时间设置!");
  2565. var = strstr(rx_buffer,"=");
  2566. var ++;
  2567. strncpy(shakesec_str,var,2);
  2568. if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
  2569. {
  2570. device_status.shake_sec = atoi(shakesec_str); //2026-06-01:修复*10导致震动很久
  2571. }
  2572. else {
  2573. LOG_E("设置失败!!!");
  2574. }
  2575. /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
  2576. LOG_D("震动时间设置:%d",device_status.shake_sec);
  2577. }
  2578. //恢复出厂设置电池
  2579. else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
  2580. {
  2581. ResetParamDefault_DC();
  2582. rt_thread_delay(1000);
  2583. //2022/06/23,add:
  2584. rt_snprintf(tmp, sizeof(tmp), "屏幕操作:系统重置:电池供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2585. json_temp = makeJson_Warn(tmp, "系统重置");
  2586. DTU_Write("%s",json_temp);
  2587. rt_free(json_temp);
  2588. }
  2589. //恢复出厂设置-交流
  2590. else if (strstr(rx_buffer,"resetDefault")!=NULL)
  2591. {
  2592. ResetParamDefault();
  2593. rt_thread_delay(1000);
  2594. //2022/06/23,add:
  2595. rt_snprintf(tmp, sizeof(tmp), "屏幕操作:系统重置:交流供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2596. json_temp = makeJson_Warn(tmp, "系统重置");
  2597. DTU_Write("%s",json_temp);
  2598. rt_free(json_temp);
  2599. }
  2600. else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
  2601. Debug_log_ON();
  2602. }
  2603. else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
  2604. Debug_log_OFF();
  2605. }
  2606. /* 屏幕版本类型切换(SwversionV12/V11/V1/V43/V42/V41/V4/V6/V30/V7)已迁移:
  2607. * 原链位于本 else-if 阶梯内,因上方“SwversionV 整页刷新”分支会先命中所有
  2608. * 版本帧而永远不可达(死代码);现改由“版本串变化检测”分支在
  2609. * hmi_ver_changed() 为真时调用 hmi_switch_version() 完成切换(2026-10-15)。 */
  2610. else if (strstr(rx_buffer,"putTest.switchFAN_PU=1")!=NULL) {
  2611. ctr_Fan_PUT(ON);
  2612. }
  2613. else if (strstr(rx_buffer,"putTest.switchFAN_PU=0")!=NULL) {
  2614. ctr_Fan_PUT(OFF);
  2615. }
  2616. else if (strstr(rx_buffer,"putTest.switchFAN_IN=1")!=NULL) {
  2617. ctr_Fan_IN(ON);
  2618. }
  2619. else if (strstr(rx_buffer,"putTest.switchFAN_IN=0")!=NULL) {
  2620. ctr_Fan_IN(OFF);
  2621. }
  2622. else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
  2623. {
  2624. HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
  2625. hmi_write_num_or_blank("sysTest.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
  2626. hmi_write_num_or_blank("sysTest.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
  2627. hmi_write_num_or_blank("sysTest.t0.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
  2628. hmi_write_num_or_blank("sysTest.t5.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
  2629. }
  2630. //立即上传
  2631. else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
  2632. {
  2633. UpLoad_Data();
  2634. }
  2635. else if (strstr(rx_buffer,"checkupdate")!=NULL)
  2636. {
  2637. time_t now = time(RT_NULL);
  2638. static struct tm tm;
  2639. gmtime_r(&now, &tm);
  2640. HMI_WRITE("hardcheck.tip_rtc.txt=\"%04d-%02d-%02d %02d:%02d:%02d\"",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2641. HMI_WRITE("hardcheck.p0.pic=%d",70);
  2642. //RTC时间判断,如果当前年份小于2020,提示需要设置时间
  2643. if(tm.tm_year+1900<2020)
  2644. {
  2645. rt_kprintf("时间未设置:%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2646. HMI_WRITE("hardcheck.tip_rtc.txt=%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  2647. }
  2648. }
  2649. //进入bootloader模式
  2650. else if (strstr(rx_buffer,"bootloader")!=NULL) {
  2651. device_status.stop_in_bootloader = 1;
  2652. ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
  2653. rt_hw_cpu_reset();
  2654. }
  2655. //软件重启
  2656. else if (strstr(rx_buffer,"reboot")!=NULL) {
  2657. rt_hw_cpu_reset();
  2658. }
  2659. //屏幕上触发远程升级
  2660. else if (strstr(rx_buffer,"hmi_update")!=NULL) {
  2661. DTU_Write("hmi_update");
  2662. }else if (strstr(rx_buffer,"checkcamera")!=NULL){
  2663. DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
  2664. //校准屏幕的RTC
  2665. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  2666. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  2667. HMI_WRITE("rtc2=%d",tm.tm_mday);
  2668. HMI_WRITE("rtc3=%d",tm.tm_hour);
  2669. HMI_WRITE("rtc4=%d",tm.tm_min);
  2670. HMI_WRITE("rtc5=%d",tm.tm_sec);
  2671. //开启超时定时器
  2672. Start_timer_Uart2();
  2673. }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
  2674. DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
  2675. //开启超时定时器
  2676. Start_timer_Uart2();
  2677. }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
  2678. else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
  2679. const char *prefix ="SaveCamParam:";
  2680. size_t prefix_len = strlen(prefix);
  2681. const char *start_pos = strstr(rx_buffer, prefix);
  2682. if(start_pos!=NULL)
  2683. {
  2684. // 移动到前缀之后的位置
  2685. start_pos += prefix_len;
  2686. /* 修复:原实现直传 makeJson_Cam 返回值,每次保存相机参数泄漏一份 cJSON 缓冲 */
  2687. char *cam_json = makeJson_Cam("SaveParam",start_pos);
  2688. if (cam_json != RT_NULL)
  2689. {
  2690. DTU_Write("%s",cam_json);
  2691. rt_free(cam_json);
  2692. }
  2693. }
  2694. //开启超时定时器
  2695. Start_timer_Uart2();
  2696. }else if (strstr(rx_buffer,"GrabOn")!=NULL){
  2697. DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
  2698. //开启超时定时器
  2699. Start_timer_Uart2();
  2700. }else if (strstr(rx_buffer,"GrabOff")!=NULL){
  2701. DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
  2702. //开启超时定时器
  2703. Start_timer_Uart2();
  2704. }
  2705. else if(strstr(rx_buffer,"SelfCheck")!=NULL)
  2706. {
  2707. //校准屏幕的RTC
  2708. HMI_WRITE("rtc0=%d",tm.tm_year+1900);
  2709. HMI_WRITE("rtc1=%d",tm.tm_mon+1);
  2710. HMI_WRITE("rtc2=%d",tm.tm_mday);
  2711. HMI_WRITE("rtc3=%d",tm.tm_hour);
  2712. HMI_WRITE("rtc4=%d",tm.tm_min);
  2713. HMI_WRITE("rtc5=%d",tm.tm_sec);
  2714. device_status.selfcheck_ws = 2;
  2715. LOG_W("开始自检:%d",device_status.selfcheck_ws);
  2716. }
  2717. memset(rx_buffer,0,sizeof(rx_buffer));
  2718. }
  2719. //入口函数-电机控制及
  2720. static void Ctr_Mortor_entry(void* parameter)
  2721. {
  2722. rt_uint16_t num_up_open = 200; //20s
  2723. rt_uint16_t num_up_close = 200; //
  2724. rt_uint16_t num_down_open = 200; //
  2725. rt_uint16_t num_down_close = 200; //
  2726. rt_int16_t photo_led_sec = 900; //55S
  2727. rt_uint8_t num_tg_open = 200; //20S
  2728. rt_uint8_t num_tg_init = 200;
  2729. static rt_uint16_t mock_uart_timer2_cnt = 0;
  2730. while(1)
  2731. {
  2732. /* uart4 接收位自愈:任何路径误洗(控制台切换等)后 100ms 内自动补回,
  2733. * 保证屏幕始终可接收(屏幕失灵时无法靠屏幕指令恢复) */
  2734. hmi_uart4_fixup_rx();
  2735. #if 0
  2736. time_t time_reset12V;
  2737. time(&time_reset12V);
  2738. static struct tm alarm_reset12V;
  2739. alarm_reset12V =*localtime(&time_reset12V);
  2740. if((alarm_reset12V.tm_hour)==8&&(alarm_reset12V.tm_min==18)&&(alarm_reset12V.tm_sec==00))
  2741. {
  2742. char *rep = makeJsonRpc2_0("reboot",NULL, 1);
  2743. if(rep){
  2744. DTU_Write("%s",rep);
  2745. free(rep);
  2746. }
  2747. rt_thread_delay(1000);
  2748. }
  2749. #endif
  2750. if(device_status.mock_uart2_timer_flag==1)
  2751. {
  2752. mock_uart_timer2_cnt++;
  2753. if(mock_uart_timer2_cnt>=200){
  2754. device_status.mock_uart2_timer_flag = 0;
  2755. HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
  2756. LOG_I("t_uart2_callback");
  2757. }
  2758. }else{
  2759. mock_uart_timer2_cnt = 0;
  2760. }
  2761. if(device_status.ctr_up_motor==1)
  2762. {
  2763. ctr_cangmen_up(OPEN);
  2764. num_up_open--;
  2765. if (num_up_open == 0)
  2766. {
  2767. ctr_cangmen_up(OFF);
  2768. num_up_open = 200;
  2769. system_status.upper_door_open_count +=1;
  2770. }
  2771. num_up_close = 200;
  2772. }
  2773. else if(device_status.ctr_up_motor==2)
  2774. {
  2775. ctr_cangmen_up(CLOSE);
  2776. num_up_close --;
  2777. if (num_up_close == 0)
  2778. {
  2779. ctr_cangmen_up(OFF);
  2780. num_up_close= 200;
  2781. system_status.upper_door_close_count +=1;
  2782. }
  2783. num_up_open = 200;
  2784. }
  2785. if (device_status.ctr_down_motor == 1)
  2786. {
  2787. ctr_cangmen_down(OPEN);
  2788. num_down_open--;
  2789. if (num_down_open == 0)
  2790. {
  2791. ctr_cangmen_down(OFF);
  2792. num_down_open = 200;
  2793. system_status.lower_door_open_count +=1;
  2794. }
  2795. num_down_close = 200;
  2796. }
  2797. else if (device_status.ctr_down_motor == 2)
  2798. {
  2799. ctr_cangmen_down(CLOSE);
  2800. num_down_close--;
  2801. if (num_down_close == 0)
  2802. {
  2803. ctr_cangmen_down(OFF);
  2804. num_down_close = 200;
  2805. system_status.lower_door_close_count += 1;
  2806. }
  2807. num_down_open = 200;
  2808. }
  2809. /* ---- 屏幕 workSta.t3:当前正在动作的那路仓门电机剩余倒计时(秒) ----
  2810. * 仓门电机每次动作上限 20s(对应 num_*_open/close 从 200 递减到 0),
  2811. * 无动作时清 0;开门/关门方向按 ctr_*_motor(1-开 2-关) 区分。 */
  2812. if(device_status.ctr_up_motor == 1) device_status.work_door_left = (uint16_t)(num_up_open / 10);
  2813. else if(device_status.ctr_up_motor == 2) device_status.work_door_left = (uint16_t)(num_up_close / 10);
  2814. else if(device_status.ctr_down_motor == 1) device_status.work_door_left = (uint16_t)(num_down_open / 10);
  2815. else if(device_status.ctr_down_motor == 2) device_status.work_door_left = (uint16_t)(num_down_close / 10);
  2816. else device_status.work_door_left = 0;
  2817. if(device_status.sun_test==1)
  2818. {
  2819. SunTest();
  2820. }
  2821. //拍照
  2822. if(device_status.photo_ws==1)
  2823. {
  2824. struct timeval tv;
  2825. gettimeofday(&tv, NULL);
  2826. device_status.photo_stamp = tv.tv_sec-8*3600;
  2827. if(photo_led_sec==900)
  2828. ctr_led(ON);
  2829. //打开补光灯
  2830. if(photo_led_sec==880)
  2831. {
  2832. system_status.photo_taken_count+=1;
  2833. LOG_I("测试拍照!");
  2834. #if 1
  2835. if(device_status.version_type!=41){
  2836. char *takephotojson = makeJson_takephoto();
  2837. DTU_Write("%s",takephotojson);
  2838. rt_free(takephotojson);
  2839. }
  2840. #endif
  2841. rt_thread_delay(5000); //延时
  2842. }
  2843. //15S后执行拍照
  2844. else if (photo_led_sec==750)
  2845. {
  2846. DTU_Write("takephoto.mp3");
  2847. //延时
  2848. rt_thread_delay(1000);
  2849. LOG_I("测试拍照!");
  2850. Hk_Take_io();
  2851. if(device_status.version_type==41){
  2852. char *takephotojson = makeJson_takephoto();
  2853. DTU_Write("%s",takephotojson);
  2854. rt_free(takephotojson);
  2855. }
  2856. }
  2857. photo_led_sec--;
  2858. /* 屏幕 workSta.t5:拍照结束剩余时间(秒)。整个拍照流程 900→300(100ms 计数)=60s,
  2859. * 到 300 关闭补光灯并结束拍照 */
  2860. device_status.photo_left = (photo_led_sec > 300) ? (uint16_t)((photo_led_sec - 300) / 10) : 0;
  2861. if(photo_led_sec==300) //
  2862. {
  2863. ctr_led(OFF);
  2864. photo_led_sec = 900;
  2865. device_status.photo_ws = 0;
  2866. device_status.photo_left = 0;
  2867. if(device_status.test_flag==0){
  2868. device_status.clear_status = 1; //20260522
  2869. }else{
  2870. device_status.test_flag = 0;
  2871. }
  2872. HMI_WRITE("sysTest.switchPhoto.val=0");
  2873. HMI_WRITE("sysTest.switchFill.val=0");
  2874. }
  2875. }else{
  2876. photo_led_sec = 900;
  2877. device_status.photo_left = 0;
  2878. }
  2879. if(device_status.collt_box_sta==111){
  2880. ctr_Fan_IN(ON); //推杆电机收缩(供电打开,复位)
  2881. num_tg_init--;
  2882. if(num_tg_init%10==0)
  2883. HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
  2884. if(num_tg_init==0){
  2885. HMI_WRITE("sysTest.result.txt=\"排虫复位完成\"");
  2886. ctr_Fan_PUT(ON);
  2887. num_tg_init = 200;
  2888. device_status.collt_box_sta=112;
  2889. }
  2890. }else if(device_status.collt_box_sta==112){
  2891. num_tg_open--;
  2892. if(num_tg_open%10==0)
  2893. HMI_WRITE("sysTest.result.txt=\"开始执行排虫:%d\"",num_tg_open/10);
  2894. if(num_tg_open==0){
  2895. HMI_WRITE("sysTest.result.txt=\"排虫复位中\"");
  2896. ctr_Fan_PUT(OFF);
  2897. num_tg_open = 200;
  2898. device_status.collt_box_sta=113;
  2899. }
  2900. }else if(device_status.collt_box_sta==113){
  2901. num_tg_init--;
  2902. if(num_tg_init%10==0)
  2903. HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
  2904. if(num_tg_init==0){
  2905. HMI_WRITE("sysTest.result.txt=\"排虫机执行已完成!\"");
  2906. ctr_Fan_PUT(OFF);
  2907. ctr_Fan_IN(OFF);
  2908. num_tg_init = 200;
  2909. device_status.collt_box_sta=0;
  2910. }
  2911. }
  2912. if(device_status.run_sta==111){ //排虫-收缩
  2913. ctr_Fan_IN(ON); //推杆电机收缩(供电打开,复位)
  2914. ctr_Fan_PUT(OFF);
  2915. num_tg_init--;
  2916. if(num_tg_init%10==0)
  2917. HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
  2918. if(num_tg_init==0){
  2919. HMI_WRITE("sysTest.result.txt=\"排虫复位完成\"");
  2920. ctr_Fan_IN(OFF);
  2921. num_tg_init = 200;
  2922. device_status.run_sta=0;
  2923. }
  2924. }else if(device_status.run_sta==112){ //收虫-推出
  2925. ctr_Fan_IN(ON);
  2926. ctr_Fan_PUT(ON);
  2927. num_tg_open--;
  2928. if(num_tg_open%10==0)
  2929. HMI_WRITE("sysTest.result.txt=\"开始执行收虫:%d\"",num_tg_open/10);
  2930. if(num_tg_open==0){
  2931. HMI_WRITE("sysTest.result.txt=\"收虫中...\"");
  2932. ctr_Fan_PUT(OFF);
  2933. ctr_Fan_IN(OFF);
  2934. num_tg_open = 200;
  2935. device_status.run_sta=0;
  2936. }
  2937. }
  2938. /* 屏幕刷新重复过滤表:到点(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位一次,
  2939. * 保证屏幕重启/换页后最迟该周期内恢复全量刷新(详见 HMI_WRITE 处说明) */
  2940. hmi_dedupe_tick();
  2941. rt_thread_delay(100);
  2942. }
  2943. }
  2944. /*屏幕写数据*/
  2945. int Hmi_Fresh(void)
  2946. {
  2947. //初始化串口4
  2948. rt_err_t ret = RT_EOK;
  2949. /* 初始化消息队列 */
  2950. rt_mq_init(&rx_mq, "rx_mq",
  2951. msg_pool, /* 存放消息的缓冲区 */
  2952. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  2953. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  2954. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  2955. /* 设置接收回调函数 */
  2956. rt_device_set_rx_indicate(serial, uart_input);
  2957. /* 创建线程*/
  2958. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  2959. Hmi_thread_entry,
  2960. RT_NULL,
  2961. HMI_THREAD_STACK_SIZE,
  2962. HMI_THREAD_PRIORITY,
  2963. HMI_THREAD_TIMESLICE);
  2964. /*部分控制的线程*/
  2965. ctr_motor = rt_thread_create("Ctr motor",
  2966. Ctr_Mortor_entry,
  2967. RT_NULL,
  2968. CONTROL_MOTOR_STACK_SIZE,
  2969. CONTROL_MOTOR_PRIORITY,
  2970. CONTROL_MOTOR_TIMESLICE);
  2971. if(ctr_motor != RT_NULL)
  2972. {
  2973. rt_thread_startup(ctr_motor);
  2974. }
  2975. /* 如果获得线程控制块,启动这个线程 */
  2976. if (hmi1 != RT_NULL)
  2977. {
  2978. rt_thread_startup(hmi1);
  2979. }
  2980. else
  2981. {
  2982. ret = RT_ERROR;
  2983. }
  2984. return ret;
  2985. }
  2986. void Check_ParamConfig(void)
  2987. {
  2988. /* 2026-10-29:开机参数加载耗时打点起点。终点在下方"参数加载完成"处,
  2989. * 不能放在函数末尾——末尾还有等待联网循环(最长3分钟)与 dtu_data_play()。 */
  2990. rt_tick_t load_t0 = rt_tick_get();
  2991. time_t now = time(RT_NULL);
  2992. static struct tm tm;
  2993. gmtime_r(&now, &tm);
  2994. if(easyflash_init()==EF_NO_ERR) //初始化成功
  2995. {
  2996. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  2997. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  2998. /* 2026-10-29:原实现每次开机都写一条 reboot_time —— EasyFlash 是追加式 KV,
  2999. * 每次写占一条记录并推动扇区轮换/GC,属持续写放大。开机次数对精度要求不高,
  3000. * 改为每 10 次落盘一次(落盘值 1,11,21…,断电最多倒退 9 次计数)。 */
  3001. device_status.rebootcnt++;
  3002. if ((device_status.rebootcnt % 10) == 1)
  3003. {
  3004. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  3005. }
  3006. LOG_I("rebootcnt:%d",device_status.rebootcnt);
  3007. LOG_I("iSave:%d",device_status.iSave);
  3008. }
  3009. else {
  3010. LOG_E("easyflash init ERRPR!!!");
  3011. }
  3012. /* If iSave was lost (EF has no record of it, value stays 0), check the
  3013. * mirror before deciding to apply defaults. A valid mirror means the
  3014. * device was configured before — recover from there instead of blowing
  3015. * all params back to factory defaults. */
  3016. if (device_status.iSave != FLASH_FLAG)
  3017. {
  3018. param_payload_t boot_mirror;
  3019. if (param_mirror_load(&boot_mirror) == RT_EOK)
  3020. {
  3021. LOG_W("iSave lost but mirror is valid (seq=%u), recovering",
  3022. boot_mirror.write_seq);
  3023. mirror_to_device_status(&boot_mirror);
  3024. /* iSave is now FLASH_FLAG; set iFlag=0 to match the normal
  3025. * "load user config" path semantics. */
  3026. device_status.iFlag = 0;
  3027. }
  3028. }
  3029. //保存默认配置
  3030. if(device_status.iSave!=FLASH_FLAG)
  3031. {
  3032. device_status.iSave=FLASH_FLAG;
  3033. device_status.iFlag = 1;
  3034. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  3035. device_status.dtype = 3;
  3036. device_status.version_type = 4; //4.0
  3037. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  3038. device_status.clt_time = 6; //收集时间
  3039. device_status.heattime = 8; //加热时间
  3040. device_status.heatvalue=100;//加热温度
  3041. device_status.thsp = 300; //默认熄屏时间5分钟
  3042. device_status.sunBar = 9; //光控定时时长
  3043. device_status.startBar = 20; //定时开始时间
  3044. device_status.stopBar = 0; //定时结束时间
  3045. device_status.tph = 70; //高温保护阈值
  3046. device_status.tpl = 5; //低温保护阈值
  3047. device_status.shake_ws = 1; //震动开关,默认开
  3048. device_status.shake_sec = 1; //震动时间,s
  3049. device_status.vol = 7; //音量
  3050. device_status.timeSwitch = 0; //工作模式:光控
  3051. device_status.passWord = 8888; //屏幕密码
  3052. device_status.dattim = 10; //数据上传间隔
  3053. device_status.ds = 1; //设备状态
  3054. device_status.heat_sw =0;
  3055. device_status.cul_time = 1;
  3056. device_status.run_sta = 0;
  3057. device_status.handle_bugger = 0; //默认排虫
  3058. sprintf(device_status.driver, "%s",VERSION_HK);
  3059. sprintf(device_status.deviceId,"0");
  3060. rt_sprintf(device_status.tempture, "-15");
  3061. rt_sprintf(device_status.humidity, "31");
  3062. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  3063. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  3064. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3065. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3066. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3067. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3068. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3069. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3070. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3071. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3072. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3073. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3074. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3075. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3076. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3077. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3078. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3079. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  3080. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  3081. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  3082. ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
  3083. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3084. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3085. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  3086. LOG_I("Default paramconf is:");
  3087. LOG_I("stime:%d",device_status.iSave);
  3088. LOG_I("stime:%d",device_status.clt_time);
  3089. LOG_I("startBar:%d",device_status.startBar);
  3090. LOG_I("stopBar:%d",device_status.stopBar);
  3091. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  3092. LOG_I("sunBar:%d",device_status.sunBar);
  3093. LOG_I("jtime:%d",device_status.heattime);
  3094. LOG_I("jheat:%d",device_status.heatvalue);
  3095. LOG_I("thsp:%d", device_status.thsp);
  3096. LOG_I("passwd:%d",device_status.passWord);
  3097. /* Seed the mirror with the same defaults we just wrote to EF.
  3098. * After this point, all EF writes below also sync to the mirror. */
  3099. param_mirror_sync();
  3100. }//从easyflash读取参数配置
  3101. else if (device_status.iSave==FLASH_FLAG) {
  3102. /* Try to recover from the mirror first. If the mirror has a valid
  3103. * copy, prefer it over single-key EF lookups: a single dropped EF
  3104. * entry no longer causes a stale default to surface. Fields that
  3105. * are NOT in the mirror (imei, iccid, deviceId, json_w_*, driver_bl,
  3106. * platform, env_t/h) are still fetched from EF below. */
  3107. device_status.iFlag = 0;
  3108. param_payload_t mirror;
  3109. if (param_mirror_load(&mirror) == RT_EOK)
  3110. {
  3111. LOG_I("param mirror loaded (seq=%u)", mirror.write_seq);
  3112. mirror_to_device_status(&mirror);
  3113. }
  3114. else
  3115. {
  3116. LOG_W("param mirror unavailable, falling back to EF single-key");
  3117. }
  3118. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  3119. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  3120. ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
  3121. ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
  3122. ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
  3123. ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
  3124. ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
  3125. ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
  3126. ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
  3127. ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
  3128. ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
  3129. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  3130. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  3131. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  3132. ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
  3133. ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
  3134. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  3135. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  3136. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  3137. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  3138. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  3139. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  3140. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  3141. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  3142. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  3143. ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
  3144. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  3145. ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
  3146. ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
  3147. device_status.light_over_tim = device_status.sunBar * 3600; //光控计数
  3148. device_status.work_done = 0;
  3149. if(device_status.dattim==0 ||
  3150. device_status.thsp==0||
  3151. device_status.sunBar==0||
  3152. device_status.tph==0){
  3153. device_status.iFlag = 1;
  3154. }
  3155. //丢参数时的异常处理
  3156. if(device_status.dattim==0) device_status.dattim = 10;
  3157. if(device_status.thsp==0) device_status.thsp = 300;
  3158. if(device_status.sunBar==0) device_status.sunBar = 4;
  3159. if(device_status.tph==0) device_status.tph = 70;
  3160. if(device_status.clt_time==0) device_status.clt_time = 10;
  3161. if(device_status.heattime==0) device_status.heattime = 10;
  3162. if(device_status.heatvalue==0) device_status.heatvalue = 100;
  3163. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  3164. if(device_status.dtype!=7)
  3165. {
  3166. device_status.dtype = 3;
  3167. }
  3168. else {
  3169. device_status.version_type = 7;
  3170. }
  3171. if(device_status.version_type==0)
  3172. {
  3173. app_partion_backup(); //固件备份至df_area
  3174. device_status.version_type = 41;
  3175. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  3176. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
  3177. }
  3178. else if(device_status.version_type==1)
  3179. {
  3180. rt_sprintf(device_status.driver, "%s",VERSION_ZP);
  3181. }
  3182. else if (device_status.version_type==11)
  3183. {
  3184. rt_sprintf(device_status.driver, "%s",VERSION_GK);
  3185. }
  3186. else if (device_status.version_type==4||device_status.version_type==42||device_status.version_type==43)
  3187. {
  3188. rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
  3189. }
  3190. else if (device_status.version_type==41) {
  3191. rt_sprintf(device_status.driver, "%s",VERSION_HK);
  3192. }
  3193. else if (device_status.version_type==6)
  3194. {
  3195. //打开相机供电;
  3196. ctr_Camera_Power(ON);
  3197. rt_sprintf(device_status.driver, "%s",VERSION_XCT);
  3198. }
  3199. else if (device_status.version_type==7) {
  3200. rt_sprintf(device_status.driver, "%s",VERSION_BZY);
  3201. //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
  3202. if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
  3203. {
  3204. device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
  3205. }
  3206. }
  3207. /*定制三色光源*/
  3208. #if LAMP_EN == 1
  3209. /* 三灯管模式下工作时长由 Lamp_Cycle(每只灯管轮换 LAMP_ROTATE_SEC)决定,
  3210. * sunBar 不再限制实际工作时间,仅按参数记录,不再强制改写为3 */
  3211. // device_status.timeSwitch = 1;
  3212. // device_status.startBar = 20;
  3213. // device_status.stopBar = 23;
  3214. #endif
  3215. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  3216. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  3217. LOG_I("开机次数:%d",device_status.rebootcnt);
  3218. LOG_I("收集时间:%d",device_status.clt_time);
  3219. LOG_I("定时开始时间:%d",device_status.startBar);
  3220. LOG_I("定时结束时间:%d",device_status.stopBar);
  3221. LOG_I("timeSwitch:%d",device_status.timeSwitch);
  3222. LOG_I("光控定时时长:%d",device_status.sunBar);
  3223. LOG_I("收集时间:%d",device_status.heattime);
  3224. LOG_I("加热温度:%d",device_status.heatvalue);
  3225. LOG_I("震动开关:%d",device_status.shake_ws);
  3226. LOG_I("震动时间:%d",device_status.shake_sec);
  3227. LOG_I("高温保护:%d",device_status.tph);
  3228. LOG_I("低温保护:%d",device_status.tpl);
  3229. LOG_I("thsp:%d", device_status.thsp);
  3230. LOG_I("密码:%d",device_status.passWord);
  3231. LOG_I("数据上传间隔:%d",device_status.dattim);
  3232. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  3233. LOG_I("PowerType:%d",device_status.Power_Type);
  3234. LOG_I("deviceId:%s",device_status.deviceId);
  3235. LOG_I("handle_bugger:%d",device_status.handle_bugger);
  3236. LOG_W("heat_sw加热开关:%d",device_status.heat_sw);
  3237. }
  3238. /* ===== 开机 Flash 参数加载耗时打点(2026-10-29 新增)=====
  3239. * 终点必须落在"参数读取/默认值写入"结束处:下方还有等待联网循环(最长 3 分钟)
  3240. * 与 dtu_data_play(),若放到函数末尾,测到的将是联网等待时间而非参数加载时间。
  3241. * 口径(本机 RT_TICK_PER_SECOND=1000,1 tick = 1ms):
  3242. * boot_param_ms = rt_tick 起点(板级初始化) → 参数加载完成,含前段全部 INIT_EXPORT,
  3243. * 即上报字段 bootlt(对"开机慢"最直观);
  3244. * param_load_ms = 进入本函数 → 参数加载完成,即 Flash 读取/默认值写入自身耗时。 */
  3245. {
  3246. rt_tick_t load_t1 = rt_tick_get();
  3247. ef_boot_diag_t efdiag;
  3248. #if RT_TICK_PER_SECOND == 1000
  3249. device_status.boot_param_ms = (rt_uint32_t)load_t1;
  3250. device_status.param_load_ms = (rt_uint32_t)(load_t1 - load_t0);
  3251. #else
  3252. device_status.boot_param_ms = (rt_uint32_t)(((rt_uint64_t)load_t1 * 1000UL) / RT_TICK_PER_SECOND);
  3253. device_status.param_load_ms = (rt_uint32_t)(((rt_uint64_t)(load_t1 - load_t0) * 1000UL) / RT_TICK_PER_SECOND);
  3254. #endif
  3255. /* EasyFlash 重建诊断(2026-10-29):把原先"静默格式化扇区"的行为变成可观测指标。
  3256. * 扇区头非法 → ef_env.c 的 check_sec_hdr_cb() 会当场擦 4KB 并重写头,是开机
  3257. * 参数加载慢的直接原因(详见 doc/readme.md 同日条目)。 */
  3258. ef_env_get_boot_diag(&efdiag);
  3259. device_status.ef_fmt_cnt = (efdiag.format_cnt > 0xFFFFU) ? 0xFFFFU : (uint16_t)efdiag.format_cnt;
  3260. device_status.ef_fmt_fail_cnt = (efdiag.format_fail_cnt > 0xFFFFU) ? 0xFFFFU : (uint16_t)efdiag.format_fail_cnt;
  3261. LOG_I("开机参数加载耗时: boot=%lu ms(含前段初始化), load=%lu ms, EF重建扇区:%u 失败:%u",
  3262. (unsigned long)device_status.boot_param_ms,
  3263. (unsigned long)device_status.param_load_ms,
  3264. (unsigned)device_status.ef_fmt_cnt,
  3265. (unsigned)device_status.ef_fmt_fail_cnt);
  3266. if (device_status.boot_param_ms > BOOT_PARAM_SLOW_MS)
  3267. {
  3268. LOG_W("开机参数加载超时(%lu ms > %lu ms),待上报预警",
  3269. (unsigned long)device_status.boot_param_ms,
  3270. (unsigned long)BOOT_PARAM_SLOW_MS);
  3271. }
  3272. if (device_status.ef_fmt_fail_cnt != 0)
  3273. {
  3274. LOG_W("EF 扇区擦写失败(%u 个),擦不干净会在每次开机重复格式化,待上报预警",
  3275. (unsigned)device_status.ef_fmt_fail_cnt);
  3276. }
  3277. }
  3278. {
  3279. device_status.update_flag = 0;
  3280. //初始化联网状态
  3281. rt_sprintf(device_status.net,"1");
  3282. rt_sprintf(device_status.csq,"0");
  3283. rt_sprintf(device_status.sim,"1");
  3284. ctr_router(ON);
  3285. Hmi_Fresh();
  3286. #if Color_EN == 1
  3287. Control_clear();
  3288. Control_heat();
  3289. #endif
  3290. if(device_status.dtype!=7)
  3291. {
  3292. Control_clear();
  3293. Control_heat();
  3294. }
  3295. dtu_data_play();
  3296. sht30_collect();
  3297. #if SENSOR_EN==1
  3298. mb_master_samlpe();
  3299. #endif
  3300. #if ROBOT_EN == 0
  3301. uint16_t connect_num=0;
  3302. //等待联网,
  3303. while(atoi(device_status.net)==1)
  3304. {
  3305. if(device_status.dtype==7)
  3306. HMI_WRITE("sysTest.auto_flag.txt=\"0\"");
  3307. char *readinfo = makeJson_ReadInfo();
  3308. DTU_Write("%s",readinfo);
  3309. rt_free(readinfo);
  3310. rt_thread_delay(1000);
  3311. connect_num++;
  3312. LOG_I("联网中......%d",connect_num);
  3313. //超时时间为3分钟
  3314. if(connect_num==180)
  3315. {
  3316. device_status.timeSwitch = 0;
  3317. LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  3318. #if 0
  3319. set_date(2020, 1, 1);
  3320. set_time(0, 0, 0);
  3321. #endif
  3322. break;
  3323. }
  3324. if(tm.tm_year>=2021)
  3325. break;
  3326. }
  3327. connect_num = 0;
  3328. while(atoi(device_status.net)==0)
  3329. {
  3330. char *readinfo = makeJson_ReadInfo();
  3331. DTU_Write("%s",readinfo);
  3332. rt_free(readinfo);
  3333. rt_thread_delay(2000);
  3334. connect_num++;
  3335. if(connect_num>5) break;
  3336. }
  3337. if(device_status.version_type==6) device_status.timeSwitch = 1;
  3338. {
  3339. /* 修复:原实现直传 makeJson_Warn 返回值,每次开机泄漏一份 cJSON 缓冲 */
  3340. char *turnon_json = makeJson_Warn("turnon", "device_stm32");
  3341. if (turnon_json != RT_NULL)
  3342. {
  3343. DTU_Write("%s",turnon_json);
  3344. rt_free(turnon_json);
  3345. }
  3346. }
  3347. rt_thread_delay(1000);
  3348. dtu_send_data_start();
  3349. #endif
  3350. }
  3351. }
  3352. /* device_status_to_mirror / mirror_to_device_status / param_mirror_sync
  3353. * have been moved to param_mirror.c so any translation unit can call
  3354. * param_mirror_sync() without needing internal hmi.c linkage. */
  3355. void ResetParamDefault(void)
  3356. {
  3357. rt_sprintf(device_status.tempture,"%d",-15);
  3358. device_status.ds = 1; //设备状态开机
  3359. device_status.thsp = 300;
  3360. device_status.passWord = 8888;
  3361. device_status.dattim = 10;
  3362. device_status.collecting_sh1 = 9;
  3363. device_status.collecting_eh1 = 10;
  3364. device_status.collecting_sh2 = 14;
  3365. device_status.collecting_eh2 = 16;
  3366. device_status.Power_Type = 0;
  3367. device_status.heat_sw = 1;
  3368. ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
  3369. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3370. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3371. if(device_status.dtype==3)
  3372. {
  3373. device_status.clt_time = 6; //收集时间
  3374. #if LAMP_EN == 0
  3375. device_status.startBar = 20; //定时开始时间
  3376. device_status.stopBar = 5; //定时结束时间
  3377. #endif
  3378. device_status.heattime = 8; //加热时间
  3379. device_status.shake_ws = 1; //针对开关
  3380. device_status.shake_sec = 1; //震动时间1-100ms
  3381. #if LAMP_EN == 1
  3382. device_status.sunBar = 3; //光控定时时长
  3383. #else
  3384. device_status.sunBar = 8;
  3385. #endif
  3386. device_status.tph = 70; //高温保护阈值
  3387. device_status.tpl = 5; //低温保护阈值
  3388. device_status.timeSwitch = 0; //定时模式:光控
  3389. device_status.heatvalue=100; //加热温度
  3390. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3391. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3392. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3393. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3394. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3395. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3396. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3397. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3398. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3399. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3400. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3401. device_status.handle_bugger = 0;
  3402. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3403. }
  3404. else if(device_status.dtype==7)
  3405. {
  3406. device_status.run_sta = 0;
  3407. device_status.heatvalue = 25; //培养温度
  3408. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  3409. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  3410. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  3411. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  3412. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  3413. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3414. }
  3415. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3416. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3417. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3418. param_mirror_sync();
  3419. HMI_WRITE("play %d,%d,%d",1,5,0);
  3420. DTU_Write("reset.mp3");
  3421. rt_thread_delay(1000);
  3422. }
  3423. void ResetParamDefault_DC(void)
  3424. {
  3425. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  3426. rt_sprintf(device_status.tempture,"%d",-15);
  3427. device_status.clt_time = 6; //收集时间
  3428. device_status.dattim = 10; //数据上传间隔
  3429. #if LAMP_EN == 0
  3430. device_status.startBar = 20; //定时开始时间
  3431. device_status.stopBar = 0; //定时结束时间
  3432. #endif
  3433. device_status.heattime = 8; //加热时间
  3434. device_status.shake_ws = 1; //针对开关
  3435. device_status.shake_sec = 1; //震动时间1-100ms
  3436. #if LAMP_EN == 1
  3437. device_status.sunBar = 3; //光控定时时长
  3438. #else
  3439. device_status.sunBar = 4;
  3440. #endif
  3441. device_status.tph = 70; //高温保护阈值
  3442. device_status.tpl = 5; //低温保护阈值
  3443. device_status.timeSwitch = 0; //定时模式
  3444. device_status.heatvalue=100;
  3445. device_status.ds = 1; //设备状态开机
  3446. device_status.thsp = 300;
  3447. device_status.passWord = 8888;
  3448. device_status.timeSwitch = 0;
  3449. device_status.Power_Type = 1;
  3450. device_status.handle_bugger = 0;
  3451. device_status.heat_sw = 0;
  3452. ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //上传时间
  3453. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  3454. ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
  3455. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  3456. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  3457. ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
  3458. ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
  3459. ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
  3460. ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
  3461. ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
  3462. ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
  3463. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  3464. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  3465. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  3466. ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
  3467. ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
  3468. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  3469. ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
  3470. param_mirror_sync();
  3471. DTU_Write("reset.mp3");
  3472. rt_thread_delay(1000);
  3473. }
  3474. //固件备份
  3475. void app_partion_backup(void)
  3476. {
  3477. rt_kprintf("固件恢复!!!\n");
  3478. const char *dst_part_name = "df_area";
  3479. const char *src_part_name = "app";
  3480. const struct fal_partition *dst_part;
  3481. const struct fal_partition *src_part;
  3482. dst_part = fal_partition_find(dst_part_name);
  3483. src_part = fal_partition_find(src_part_name);
  3484. if (dst_part == RT_NULL || src_part == RT_NULL)
  3485. {
  3486. if (dst_part == RT_NULL)
  3487. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  3488. if (src_part == RT_NULL)
  3489. rt_kprintf("%s partition is not exist!\n", src_part_name);
  3490. }
  3491. else
  3492. {
  3493. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  3494. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  3495. {
  3496. int clone_pos = 0;
  3497. int clone_len = 0, clone_tol_len;
  3498. rt_uint8_t *buf = rt_malloc(4096);
  3499. if (dst_part->len < src_part->len)
  3500. clone_tol_len = dst_part->len;
  3501. else
  3502. clone_tol_len = src_part->len;
  3503. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  3504. {
  3505. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  3506. if (clone_len < 0)
  3507. {
  3508. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  3509. break;
  3510. }
  3511. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  3512. {
  3513. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  3514. break;
  3515. }
  3516. HMI_WRITE("#");
  3517. rt_kprintf("#");
  3518. clone_pos += clone_len;
  3519. }
  3520. if (clone_pos >= clone_tol_len)
  3521. {
  3522. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  3523. }
  3524. else
  3525. {
  3526. rt_kprintf("Clone partition failed!");
  3527. }
  3528. if (buf)
  3529. rt_free(buf);
  3530. }
  3531. }
  3532. }
  3533. int delete_hmi_thread(void)
  3534. {
  3535. int32_t try_cnt;
  3536. LOG_W("delete_hmi_thread.");
  3537. /* 安全删除:两线程平时阻塞在收消息/延时上,若恰好处于 IPC 等待则稍后重试,
  3538. * 避免 rt_thread_delete 造成 IPC 等待链表野指针(见 control.c 说明)。
  3539. * 删除成功后 thread_delete_safe 会把句柄置空,便于后续按需重建。 */
  3540. for (try_cnt = 0; try_cnt < 30; try_cnt++)
  3541. {
  3542. if (thread_delete_safe(&ctr_motor) == 0 && thread_delete_safe(&hmi1) == 0)
  3543. {
  3544. break;
  3545. }
  3546. rt_thread_mdelay(50);
  3547. }
  3548. return 0;
  3549. }
  3550. void Debug_log_ON(void)
  3551. {
  3552. hmi_uart4_fixup_rx(); /* 先记录 uart4 原始接收位(切换前) */
  3553. rt_console_set_device("uart4");
  3554. finsh_set_device("uart4");
  3555. /* 上述 open 会把 uart4 的 DMA_RX 接收位洗掉(serial.c:593),
  3556. * 不补回则屏幕接收失效("屏幕点不动")——立即补回 */
  3557. hmi_uart4_fixup_rx();
  3558. }
  3559. void Debug_log_OFF(void)
  3560. {
  3561. rt_console_set_device("uart1");
  3562. finsh_set_device("uart1");
  3563. /* 同族加固(2026-10-13):rt_console_set_device 恢复时的 open 会按
  3564. * `open_flag = oflag & 0xff` 洗掉 uart1 的 INT_RX 位,而 finsh 在
  3565. * "uart1 已是 shell 设备"时直接 return 不补位 → 控制台切回后本机口无法输入。
  3566. * 统一由 dbg_chain_fixup() 把接收模式位补回(详见该函数注释)。 */
  3567. dbg_chain_fixup();
  3568. /* uart4(屏幕口)接收位同样在调试打开/关闭链路中被洗过,这里一并补回 */
  3569. hmi_uart4_fixup_rx();
  3570. /* 线程已存在时不再重复创建(原实现每次调用都会新建线程且不回收,造成线程泄漏) */
  3571. if (hmi1 == RT_NULL)
  3572. {
  3573. rt_mq_init(&rx_mq, "rx_mq",
  3574. msg_pool, /* 存放消息的缓冲区 */
  3575. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  3576. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  3577. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  3578. rt_device_set_rx_indicate(serial, uart_input);
  3579. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  3580. Hmi_thread_entry,
  3581. RT_NULL,
  3582. HMI_THREAD_STACK_SIZE,
  3583. HMI_THREAD_PRIORITY,
  3584. HMI_THREAD_TIMESLICE);
  3585. if (hmi1 != RT_NULL)
  3586. {
  3587. rt_thread_startup(hmi1);
  3588. }
  3589. }
  3590. }
  3591. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  3592. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  3593. //
  3594. INIT_BOARD_EXPORT(HMI_uart4_init);