| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831 |
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-26 Administrator the first version
- */
- /* [2026-10-15] 日志配置必须放在本文件所有 include 之前: spi_flash_sfud.h ->
- * sfud_cfg.h 会抢先 #include <rtdbg.h>(该头带 include guard) 并以 DBG_LVL=DBG_INFO
- * 钉死本文件的日志等级 —— 放在后面会使 LOG_D 被整体裁剪
- * (现象: 屏幕接收原始数据等 LOG_D 打印消失)。此处直接定义 DBG_SECTION_NAME 而非
- * DBG_TAG, 避免 sfud_cfg.h 的 DBG_TAG 重定义("SFUD")影响本文件日志节名。 */
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_SECTION_NAME "hmi"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h>
- #include <rtthread.h>
- #include "hmi.h"
- #include <board.h>
- #include <control.h>
- #include <rtdevice.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stdarg.h"
- #include "string.h"
- #include "sht30_collect.h"
- #include "beep.h"
- #include "easyflash.h"
- #include "sound.h"
- #include "ymodem.h"
- #include <shell.h>
- #include <msh.h> /* msh_exec:屏幕 debug1 调试页控制台命令执行 */
- #include "dtu_uart2.h"
- #include <fal.h>
- #include <spi_flash_sfud.h> /* debug1 页外挂Flash信息(与 sys_monitor.c 相同方式读取) */
- #include "selfcheck.h"
- #include <user_mb_app.h>
- #include "sys_monitor.h"
- #include "param_backup.h"
- #define HMI_THREAD_PRIORITY 5 //线程优先级
- #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
- #define HMI_THREAD_TIMESLICE 10 //时间片
- #define CONTROL_MOTOR_PRIORITY 18
- #define CONTROL_MOTOR_STACK_SIZE 2048
- #define CONTROL_MOTOR_TIMESLICE 5
- static char msg_pool[512];
- static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
- static char rx_pkt[HMI_UART_RXBUFFER_SIZE + 1];
- int mb_master_samlpe();
- /**/
- /* device_status 已收敛为全工程唯一定义(在 main.c), 此处不再重复定义, 见 control.h 的 extern 声明 */
- /* system_status 已收敛为全工程唯一定义(在 sys_monitor.c), 见 sys_monitor.h 的 extern 声明 */
- extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
- time_t time(time_t *t);
- /* 串口接收消息结构*/
- struct rx_msg
- {
- rt_device_t dev;
- rt_size_t size;
- };
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
- ////
- static rt_thread_t hmi1 = RT_NULL;
- static rt_thread_t ctr_motor = RT_NULL;
- rt_device_t serial; /*串口设备句柄*/
- rt_timer_t timer_uart2 = RT_NULL;
- static upgrade_state_t current_state = STATE_IDLE;
- uint32_t total_file_size = 0;
- static uint32_t bytes_received = 0;
- static uint8_t recv_buffer[RECV_BUFFER_SIZE];
- static uint32_t recv_write_pos = 0;
- /* 消息队列控制块 */
- static struct rt_messagequeue rx_mq;//接收数据消息队列
- static void HMI_Decode(void);
- static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
- /* P2-14 把开机等联网搬进独立线程时新增的两个函数, 定义在文件后段
- (Check_ParamConfig 之后), 但 net_wait_start() 在前面的 Check_ParamConfig 里
- 就被调用 -> 必须先有前向声明, 否则报 implicit declaration。 */
- static void net_wait_thread_entry(void);
- /* 开机"联网检测线程"(net_wait)是否仍在运行。
- net_wait 在等网期间自己每秒发一帧 read 去刷新网络状态;
- refresh_hmi_Component() 每 5 个节拍也会发一帧 read —— 两者同时发就是重复上报,
- RTU 会连续收到两倍 read 帧。用本标志让 HMI 侧在 net_wait 退出前让路。 */
- static volatile uint8_t net_wait_running = 0;
- #ifdef RT_USING_SERIAL_V2
- static int HMI_uart4_init(void)
- {
- serial = rt_device_find(UART_NAME);
- /*修改串口配置参数*/
- config.baud_rate = BAUD_RATE_115200; //修改波特率9600
- config.data_bits = DATA_BITS_8; //数据位8
- config.stop_bits = STOP_BITS_1; //停止位1
- #ifdef RT_USING_SERIAL_V2
- config.rx_bufsz = 512;
- config.tx_bufsz = 1024;
- #else
- config.bufsz = 512; //缓冲区128字节
- #endif
- config.parity = PARITY_NONE; //无奇偶校验
- /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
- rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
- // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
- //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
- /* 以 DMA 接收及轮询发送模式打开串口设备 */
- #ifdef RT_USING_SERIAL_V2
- rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
- #else
- rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
- #endif
- if(uart_res!=RT_EOK)
- {
- LOG_E("%s打开失败!",UART_NAME);
- }else{
- LOG_I("%s Open Success!",UART_NAME);
- }
- return RT_EOK;
- }
- #endif
- /* ==================== UART4(屏幕)发送:互斥 / 升级闸门 / 批量 / 去重 ====================
- * 来源: master 分支 2026-10-10 发送优化(HMI_WRITE 签名不变, 调用方无感):
- * ① 批量发送:HMI_BATCH_BEGIN()/END() 之间累积成整块再一次写入(缓冲满自动 flush);
- * 整页刷新 60+ 条属性由 60+ 次写合并为 1~3 次写。
- * ② 重复过滤:同一调用点(用格式化串地址唯一标识)输出内容与上次相同则不发送;
- * 直接映射表, 槽位冲突按"未命中"处理——只会多发、绝不会漏发。
- * ③ 过滤表每 HMI_DEDUPE_RESYNC_SEC 秒整体复位, 并在 Swversion 握手串变化
- * (换型号/换页面/屏幕重启)时立即复位, 保证屏幕不长期停在旧值。
- * ④ 升级闸门: 屏幕升级传输期间屏蔽普通刷新指令(见 Hmi_Upgrade_Begin/End),
- * 避免业务数据混入升级固件流; 60s 无新升级数据自动解除。
- * 平台差异(apm32f1): 保留 g_hmi_port_closed 早退(OTA 前 uart4 已 close,
- * 继续写会解引用已释放的 tx_fifo 而 HardFault, 见 hmi.h 注释)。
- * 维护提示:新增刷新项直接用 HMI_WRITE 即可, 无需关心批量/去重。 */
- /* ---------- 发送互斥与升级状态 ---------- */
- static struct rt_mutex hmi_write_mutex;
- static rt_bool_t hmi_mutex_inited = RT_FALSE;
- static volatile rt_tick_t s_hmi_upgrade_tick = 0; /* 0=不在升级; 非0=最近一次升级数据时间 */
- /* 串口4(HMI屏)在 OTA 升级前被关闭, 置 1 后所有发送路径直接 return。
- 为什么必须有这个标志而不能只靠句柄判空:
- rt_device_close() 真正关掉设备时, serial_v2 的 rt_serial_rx/tx_disable()
- 会 rt_free 掉 rx/tx fifo 并把 serial->serial_rx / serial_tx 置 NULL,
- 但 dev 指针本身依然非空、依然能被 rt_device_write() 调到;
- 而 rt_serial_write() 会直接 `serial->serial_tx->activated` 解引用 →
- 空指针, 整机 HardFault。所以必须靠这个显式标志拦住。 */
- rt_uint8_t g_hmi_port_closed = 0;
- static rt_bool_t hmi_write_lock(void)
- {
- /* 中断上下文不加锁(互斥锁不允许在中断中获取) */
- if (rt_interrupt_get_nest() != 0)
- {
- return RT_FALSE;
- }
- if (!hmi_mutex_inited)
- {
- rt_base_t level = rt_hw_interrupt_disable();
- if (!hmi_mutex_inited)
- {
- if (rt_mutex_init(&hmi_write_mutex, "hmi_mtx", RT_IPC_FLAG_FIFO) == RT_EOK)
- {
- hmi_mutex_inited = RT_TRUE;
- }
- }
- rt_hw_interrupt_enable(level);
- }
- if (hmi_mutex_inited)
- {
- rt_mutex_take(&hmi_write_mutex, RT_WAITING_FOREVER);
- return RT_TRUE;
- }
- return RT_FALSE;
- }
- static void hmi_write_unlock(void)
- {
- if (hmi_mutex_inited)
- {
- rt_mutex_release(&hmi_write_mutex);
- }
- }
- void Hmi_Upgrade_Begin(void)
- {
- s_hmi_upgrade_tick = rt_tick_get();
- LOG_I("HMI upgrade begin");
- }
- void Hmi_Upgrade_End(void)
- {
- s_hmi_upgrade_tick = 0;
- LOG_I("HMI upgrade end");
- }
- /* 升级传输期间返回真:屏蔽普通属性刷新,避免混入固件数据流;
- * 60s 内没有新的升级数据自动解除(防止升级中断后屏幕刷新永久失效)。 */
- static rt_bool_t hmi_upgrade_busy(void)
- {
- rt_tick_t t = s_hmi_upgrade_tick;
- if (t == 0)
- {
- return RT_FALSE;
- }
- if ((rt_tick_get() - t) > (60 * RT_TICK_PER_SECOND))
- {
- s_hmi_upgrade_tick = 0;
- LOG_W("HMI upgrade timeout, auto clear busy flag");
- return RT_FALSE;
- }
- return RT_TRUE;
- }
- /* ---------- 重复过滤(CRC32 直接映射表) ---------- */
- #define HMI_DEDUPE_EN 1 /* 置 0 可整体关闭重复过滤(调试用) */
- #define HMI_DEDUPE_ENTRIES 96 /* 槽位数; 冲突只影响压缩率, 不影响正确性 */
- #define HMI_DEDUPE_RESYNC_SEC 10 /* 定期全量刷新周期(s) */
- typedef struct {
- const char *key; /* 格式化串地址:每个调用点唯一 */
- rt_uint32_t crc; /* 上次发送内容的 CRC32 */
- rt_uint16_t len; /* 上次发送内容长度 */
- } hmi_dedupe_item_t;
- static hmi_dedupe_item_t hmi_dedupe_tab[HMI_DEDUPE_ENTRIES];
- static rt_uint16_t hmi_dedupe_tick_cnt = 0;
- static rt_uint32_t hmi_crc32(const char *s, rt_uint16_t len)
- {
- rt_uint32_t crc = 0xFFFFFFFFu;
- rt_uint16_t i;
- rt_uint8_t k;
- for (i = 0; i < len; i++)
- {
- crc ^= (rt_uint8_t)s[i];
- for (k = 0; k < 8; k++)
- {
- crc = (crc >> 1) ^ (0xEDB88320u & (rt_uint32_t)(-(rt_int32_t)(crc & 1u)));
- }
- }
- return ~crc;
- }
- static rt_uint16_t hmi_dedupe_index(const char *key)
- {
- return (rt_uint16_t)(((rt_uint32_t)key >> 2) % HMI_DEDUPE_ENTRIES);
- }
- /* 返回 RT_TRUE:本次内容与上次完全相同,可跳过发送 */
- static rt_bool_t hmi_dedupe_skip(const char *key, const char *line, rt_uint16_t len)
- {
- rt_uint16_t idx = hmi_dedupe_index(key);
- hmi_dedupe_item_t *it = &hmi_dedupe_tab[idx];
- rt_uint32_t crc = hmi_crc32(line, len);
- if (it->key == key && it->len == len && it->crc == crc)
- {
- return RT_TRUE;
- }
- /* 写入顺序:crc/len 先写、key 最后写; 读取时先比 key——
- * 这样并发时"命中"的条目其 crc/len 一定是完整的 */
- it->crc = crc;
- it->len = len;
- it->key = key;
- return RT_FALSE;
- }
- static void hmi_dedupe_reset(void)
- {
- rt_memset(hmi_dedupe_tab, 0, sizeof(hmi_dedupe_tab)); /* key=NULL, 任何真实调用点都不会命中 */
- hmi_dedupe_tick_cnt = 0;
- }
- /* 由 Ctr_Mortor_entry 每 100ms 调用一次:到点复位过滤表(等价于下一次刷新全量重发) */
- static void hmi_dedupe_tick(void)
- {
- if (++hmi_dedupe_tick_cnt >= (HMI_DEDUPE_RESYNC_SEC * 10))
- {
- hmi_dedupe_reset();
- }
- }
- /* ---------- ① 批量发送 ---------- */
- #define HMI_BATCH_BUF_SIZE 1024 /* 累积缓冲; 满则自动 flush, 不丢指令 */
- static char hmi_batch_buf[HMI_BATCH_BUF_SIZE];
- static rt_uint16_t hmi_batch_len = 0;
- static volatile rt_bool_t hmi_batch_on = RT_FALSE;
- static rt_thread_t hmi_batch_owner = RT_NULL; /* 批量会话所属线程 */
- #define HMI_BATCH_BEGIN() Hmi_Batch_Begin()
- #define HMI_BATCH_END() Hmi_Batch_End()
- /* ---------- 完整写入(nonblocking TX 的环形缓冲保护,2026-10-15) ----------
- * uart4 以 TX_NON_BLOCKING 打开(见 HMI_uart4_init):数据先进 1024B 的 TX
- * 环形缓冲,rt_device_write 只接受能容纳的部分并立即返回,**超出的静默丢弃**。
- * 2.48 时代逐条发送(约30B)+ 每条 mdelay(1) 自然节流,缓冲几乎不积压;
- * 2.49 引入批量发送后单次 flush 可达 1024B:第 1 批写满缓冲并启动 DMA
- * (1024B@115200 ≈ 89ms 才发完),同一刷新内的第 2/3 批到达时缓冲仍满 →
- * **整批丢失**。现象:屏幕后段控件(menu.va_net / menu.cam_sta 等)收不到
- * 指令;且发送去重表已记录"值未变不再重发" → 该控件永久不更新
- * (每 10s 全量轮的重发又落回被丢批次)。
- * 本函数分块写入并检查实际写入量,未写完时让出等待 TX 缓冲释放后重试;
- * 中断上下文不等待(保持 HMI_WRITE 的中断可调用性),仅尽力发送。
- * 返回实际写入字节数。 */
- #define HMI_TX_RETRY_MAX 400 /* 最长等待约 400ms:覆盖升级块 4KB@115200≈356ms;普通刷新远小于此 */
- static rt_uint16_t hmi_write_all(const void *data, rt_uint16_t len)
- {
- const char *pbuf = (const char *)data;
- rt_uint16_t sent = 0;
- rt_uint16_t retry = 0;
- while (sent < len)
- {
- rt_size_t n = rt_device_write(serial, 0, pbuf + sent, len - sent);
- sent += (rt_uint16_t)n;
- if (sent >= len)
- {
- break;
- }
- /* 中断上下文不能等待:放弃剩余(正常业务发送都走线程上下文) */
- if (rt_interrupt_get_nest() != 0)
- {
- break;
- }
- if (++retry > HMI_TX_RETRY_MAX)
- {
- LOG_W("hmi tx wait timeout, dropped %u bytes", (unsigned)(len - sent));
- break;
- }
- rt_thread_mdelay(1);
- }
- system_status.uart4_tx_bytes += sent;
- return sent;
- }
- void Hmi_Batch_Begin(void)
- {
- hmi_batch_len = 0;
- hmi_batch_owner = rt_thread_self();
- hmi_batch_on = RT_TRUE;
- }
- void Hmi_Batch_End(void)
- {
- hmi_batch_on = RT_FALSE;
- hmi_batch_owner = RT_NULL;
- if (hmi_batch_len > 0)
- {
- if (!g_hmi_port_closed && serial != RT_NULL)
- {
- rt_bool_t locked = hmi_write_lock();
- hmi_write_all(hmi_batch_buf, hmi_batch_len); /* 完整写入: TX 缓冲满时等待重试, 不丢数据 */
- if (locked)
- {
- hmi_write_unlock();
- }
- }
- hmi_batch_len = 0;
- }
- }
- /* 访问批量缓冲只允许批量会话的属主线程(见 HMI_WRITE):
- * 其它线程即便在批量期间调用 HMI_WRITE 也直接立即发送,
- * 避免两线程同时往同一缓冲追加导致内容错乱。 */
- static rt_bool_t hmi_batch_owned(void)
- {
- return (hmi_batch_on && hmi_batch_owner == rt_thread_self()) ? RT_TRUE : RT_FALSE;
- }
- /* 单条立即发送(批量会话外的默认行为) */
- static void hmi_send_now(const char *data, rt_uint16_t len)
- {
- rt_bool_t locked = hmi_write_lock();
- hmi_write_all(data, len); /* 完整写入: TX 缓冲满时等待重试, 不丢数据 */
- if (locked)
- {
- hmi_write_unlock();
- }
- }
- void HMI_WRITE(char *fmt, ...)
- {
- va_list ap;
- char str[256 + 3]; /* 预留 3 字节结束符 0xFF 0xFF 0xFF */
- rt_size_t str_length;
- /* OTA 期间串口4已关闭(见 control.c 的 uart_stop_for_ota)必须最先返回:
- 不能只靠 serial 判空——rt_device_close 释放的是 serial_v2 内部 fifo,
- 设备指针本身仍非空, 继续写会解引用已释放的 tx_fifo 而 HardFault。 */
- if (g_hmi_port_closed || serial == RT_NULL || fmt == NULL) {
- return;
- }
- /* 升级传输期间屏蔽普通业务指令 */
- if (hmi_upgrade_busy()) {
- return;
- }
- va_start(ap, fmt);
- str_length = rt_vsnprintf(str, sizeof(str) - 3, fmt, ap);
- va_end(ap);
- if (str_length == 0 || str_length >= sizeof(str) - 3) {
- if (str_length > 0)
- {
- LOG_W("HMI_WRITE: frame too long(%d), dropped", (int)str_length);
- }
- return;
- }
- #if HMI_DEDUPE_EN
- /* ② 同一调用点内容未变化则不重发 */
- if (hmi_dedupe_skip(fmt, str, (rt_uint16_t)str_length))
- {
- return;
- }
- #endif
- /* 结束符与指令合并为一次写入, 减少写入次数、避免发送被拆散 */
- str[str_length] = (char)0xFF;
- str[str_length + 1] = (char)0xFF;
- str[str_length + 2] = (char)0xFF;
- if (hmi_batch_owned())
- {
- rt_uint16_t need = (rt_uint16_t)(str_length + 3);
- if ((rt_uint32_t)hmi_batch_len + need > sizeof(hmi_batch_buf))
- {
- Hmi_Batch_End(); /* 缓冲不足先落地一批 */
- Hmi_Batch_Begin(); /* 继续留在批量会话内(重新取回属主) */
- }
- if ((rt_uint32_t)hmi_batch_len + need <= sizeof(hmi_batch_buf))
- {
- rt_memcpy(hmi_batch_buf + hmi_batch_len, str, need);
- hmi_batch_len += need;
- }
- else
- {
- /* 单条本身就超过批量缓冲(异常情况): 直接发, 宁可多一次写也不丢指令 */
- hmi_send_now(str, need);
- }
- return;
- }
- hmi_send_now(str, (rt_uint16_t)(str_length + 3));
- }
- /* 接收数据回调函数 */
- static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
- {
- struct rx_msg msg;
- rt_err_t result;
- msg.dev = dev;
- msg.size = size;
- result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
- if ( result == -RT_EFULL)
- {
- /* 消息队列满 */
- LOG_E("hmi message queue full!\n");
- rt_mq_control(&rx_mq, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
- }
- return result;
- }
- // 预计算转换表
- static const int8_t hex_to_byte[256] = {
- [0 ... 255] = -1,
- ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
- ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
- ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
- ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
- };
- /**
- * @brief 快速HEX字符串转二进制
- * @param hex HEX字符串
- * @param bin 输出缓冲区
- * @param max_len 最大输出长度
- * @return 转换的字节数,<0表示错误
- */
- static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
- {
- if (!hex || !bin || max_len <= 0) {
- return -1;
- }
- int hex_len = strlen(hex);
- if (hex_len % 2 != 0) {
- return -1; // HEX长度必须是偶数
- }
- int bin_len = hex_len / 2;
- if (bin_len > max_len) {
- return -1; // 输出缓冲区太小
- }
- for (int i = 0; i < bin_len; i++) {
- int high = hex_to_byte[(unsigned char)hex[i * 2]];
- int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
- if (high < 0 || low < 0) {
- return -1; // 无效HEX字符
- }
- bin[i] = (high << 4) | low;
- }
- return bin_len;
- }
- // 发送原始数据到HMI
- static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
- {
- if (g_hmi_port_closed || !serial || !data || size == 0) return -1;
- /* 4KB 块远大于 1024B TX 环形缓冲:必须完整写入(等待重试),
- 原实现单次写只进 1KB、返回 -1,升级数据静默丢失 3/4 */
- return (hmi_write_all(data, (rt_uint16_t)size) == (rt_uint16_t)size) ? 0 : -1;
- }
- // 最简洁版本 - 每4096字节直接清空
- void process_hex_payload(const char* hex_str, int req_id) {
- if (!hex_str) return;
- if (current_state != STATE_RECEIVING)
- {
- /* 新一轮屏幕升级开始: 复位重复过滤表, 升级结束后屏幕控件恢复全量刷新 */
- hmi_dedupe_reset();
- /* ★ 原来只复位去重表: 第二轮升级时 bytes_received 仍是上一轮的值,
- 首包就满足 >= total_file_size -> 立刻判"升级完成"并退出 HMI 线程。 */
- bytes_received = 0;
- recv_write_pos = 0;
- }
- /* 升级传输期间屏蔽普通刷新指令(见 HMI_WRITE 升级闸门) */
- Hmi_Upgrade_Begin();
- current_state = STATE_RECEIVING;
- // 转换HEX
- uint8_t temp[JSON_CHUNK_SIZE];
- int data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
- if (data_len <= 0) return;
- // 添加到缓冲区
- if (recv_write_pos + data_len > sizeof(recv_buffer)) {
- LOG_E("缓冲区溢出");
- return;
- }
- memcpy(recv_buffer + recv_write_pos, temp, data_len);
- recv_write_pos += data_len;
- bytes_received += data_len;
- LOG_I("收到 %d 字节,缓冲: %u/%u",
- data_len, recv_write_pos, sizeof(recv_buffer));
- // 每4096字节发送一次
- if (recv_write_pos >= 4096) {
- LOG_I("发送 4096 字节到HMI...");
- if (send_raw_to_hmi(recv_buffer, 4096) != 0) {
- LOG_E("发送失败");
- return;
- }
- LOG_I("发送成功");
- /* ★ 原来发送后直接 recv_write_pos = 0: 本块解码长度不是 512 整数倍时,
- 超出的尾部(最多 511 字节)被整块丢掉 -> 屏幕收到的固件流缺字节。
- 发完 4096 字节后把剩余字节前移保留。 */
- if (recv_write_pos > 4096) {
- memmove(recv_buffer, recv_buffer + 4096, recv_write_pos - 4096);
- recv_write_pos -= 4096;
- } else {
- recv_write_pos = 0;
- }
- /* 不再 return: 文件长度正好是 4096 整数倍时, 最后一包之后不会再有新数据,
- 原来在这里返回会让升级永远停在 RECEIVING。继续走下面的完成判断。 */
- }
- // 检查完成
- if (bytes_received >= total_file_size && total_file_size > 0) {
- if (recv_write_pos > 0) {
- LOG_I("发送最后 %u 字节", recv_write_pos);
- send_raw_to_hmi(recv_buffer, recv_write_pos);
- recv_write_pos = 0; // 清空
- }
- current_state = STATE_COMPLETED;
- Hmi_Upgrade_End();
- LOG_I("升级完成!");
- return;
- }
- // 发送进度
- int progress = total_file_size > 0 ?
- (bytes_received * 100 / total_file_size) : 0;
- char progress_json[256];
- rt_snprintf(progress_json, sizeof(progress_json),
- "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
- "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
- bytes_received, total_file_size, progress, req_id);
- /* ★ progress_json 是栈上数组(不能交给 dtu_write_json 释放), 但要加 "%s" 防止被当格式串 */
- DTU_Write("%s", progress_json);
- }
- /* ==================== UART4(屏幕)接收:帧组装 ====================
- * 屏幕每条指令以 "\r\n" 结尾, 而串口按 DMA 包分段:
- * ① 一条指令可能被拆到两次 read —— 旧实现靠 strstr 前缀匹配, 残段会误命中别的
- * 分支(2026-10-08 的 SwversionV4/V43 误切版本类型即此问题, 见 hmi_cmd_match);
- * ② 一次 read 可能含多条指令 —— 旧实现是 if/else 链, 只处理第一条匹配的分支,
- * 例如 "getFullData\r\n" 与 "putTest.switchHeat=1\r\n" 同包时, 按键会被丢掉。
- * 现在: 整包先喂入组装缓冲, 按 '\n' 切出完整帧逐条交给 HMI_Decode(帧内容保留
- * "\r\n", 与旧行为一致); 残段留待下次 read 拼接; 超长无结束符的帧丢弃并告警。 */
- #define HMI_FRAME_MAX 512
- static char hmi_frame[HMI_FRAME_MAX + 1];
- static rt_uint16_t hmi_frame_len = 0;
- /* 处理一帧完整指令; 返回 RT_TRUE 表示 HMI 线程应退出(屏幕升级完成) */
- static rt_bool_t Hmi_Frame_Handle(void)
- {
- rt_uint16_t copy_len;
- if (hmi_frame_len == 0)
- {
- return RT_FALSE;
- }
- hmi_frame[hmi_frame_len] = '\0';
- /* 屏幕升级应答: 单字节 0x05 */
- if (hmi_frame_len == 1 && (rt_uint8_t)hmi_frame[0] == HMI_ACK_BYTE)
- {
- LOG_W("屏幕响应信号!");
- hmi_frame_len = 0;
- if (current_state == STATE_COMPLETED)
- {
- Hmi_Upgrade_End();
- return RT_TRUE; /* 升级完成, Hmi_thread_entry 收到后退出线程 */
- }
- /* RTU 可以发送升级文件了 */
- dtu_write_json(makeJsonRpc2_0("upgradehmi",NULL,1));
- return RT_FALSE;
- }
- /* 交给解析入口(rx_buffer 为解析缓冲, HMI_Decode 内部会清空它) */
- copy_len = (hmi_frame_len < HMI_UART_RXBUFFER_SIZE) ? hmi_frame_len : HMI_UART_RXBUFFER_SIZE;
- rt_memcpy(rx_buffer, hmi_frame, copy_len);
- rx_buffer[copy_len] = '\0';
- hmi_frame_len = 0;
- HMI_Decode();
- return RT_FALSE;
- }
- /* 喂入串口收到的原始数据; 返回 RT_TRUE 表示升级完成、HMI 线程应退出 */
- static rt_bool_t Hmi_Rx_Feed(const char *data, rt_uint16_t len)
- {
- rt_uint16_t i;
- for (i = 0; i < len; i++)
- {
- char c = data[i];
- /* 升级应答 0x05: 单字节, 不参与帧组装 */
- if (hmi_frame_len == 0 && (rt_uint8_t)c == HMI_ACK_BYTE)
- {
- hmi_frame[0] = c;
- hmi_frame_len = 1;
- if (Hmi_Frame_Handle())
- {
- return RT_TRUE;
- }
- continue;
- }
- if (hmi_frame_len >= HMI_FRAME_MAX)
- {
- LOG_W("hmi frame too long(%d), drop!", hmi_frame_len);
- hmi_frame_len = 0;
- }
- hmi_frame[hmi_frame_len++] = c;
- if (c == '\n')
- {
- if (Hmi_Frame_Handle())
- {
- return RT_TRUE;
- }
- }
- }
- return RT_FALSE;
- }
- /*入口函数*/
- static void Hmi_thread_entry(void *parameter)
- {
- struct rx_msg msg;
- rt_err_t result;
- rt_uint32_t rx_length;
- if(device_status.dtype==3){
- device_status.collt_box_sta = 0;
- device_status.run_sta = 0;
- }
- device_status.upan_sta = 1;
- device_status.camsta = 1;
- while(1)
- {
- rt_memset(&msg, 0, sizeof(msg));
- /* 从消息队列中读取消息*/
- result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
- if (result == RT_EOK)
- {
- /* 读到本次 DMA 收到的原始数据(rx_pkt), 按帧组装后再解析 */
- rx_length = rt_device_read(msg.dev, 0, rx_pkt, HMI_UART_RXBUFFER_SIZE);
- if (rx_length > 0)
- {
- system_status.uart4_rx_bytes += rx_length;
- rx_pkt[rx_length] = '\0';
- /* 屏幕每帧原始数据打印: 保持 LOG_I 可见级(2.48 行为)。
- * master 减噪版曾降为 LOG_D —— 控制台调屏/排查依赖本行直接可见, 勿降级 */
- LOG_I("hmi rx(%u): %s", (unsigned)rx_length, rx_pkt);
- if (Hmi_Rx_Feed(rx_pkt, (rt_uint16_t)rx_length))
- {
- return; /* 屏幕升级完成, 线程退出 */
- }
- }
- }
- /*********************************************************************************************************************/
- //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);
- //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
- }
- }
- /*******************************************************************************************/
- void Start_timer_Uart2(void)
- {
- device_status.mock_uart2_timer_flag = 1;
- }
- void Stop_timer_Uart2(void)
- {
- device_status.mock_uart2_timer_flag = 0;
- }
- static void refresh_hmi_Component(struct tm tm)
- {
- if(device_status.refresh_num>=5)
- {
- device_status.refresh_num = 0;
- /* ★ 开机联网检测线程(net_wait)还没退出时, 它自己每秒都在发 read 刷状态:
- 这里再发就是重复上报。等它退出(联网成功/超时)后由本函数接管刷新。
- 顺带补上 makeJson_ReadInfo() 的空指针判断(原实现会把 NULL 送进 DTU_Write)。 */
- if (net_wait_running == 1)
- {
- char *readinfo = makeJson_ReadInfo();
- if (readinfo != RT_NULL)
- {
- DTU_Write("%s", readinfo);
- rt_free(readinfo);
- }
- }
- device_status.hmi_fresh = 1;
- rt_thread_delay(100);
- }
- //在解析getinfo中清除超时0
- if(device_status.refresh_timeout==15)
- {
- rt_sprintf(device_status.sim,"1");
- rt_sprintf(device_status.net,"1");
- rt_sprintf(device_status.csq,"0");
- device_status.camsta = 1;
- device_status.link_sta=0;
- device_status.refresh_timeout=0;
- HMI_WRITE("menu.warn.txt=\"%s\"","[RTU/4G模块]通信失败,检查连接/固件!");
- }
- device_status.refresh_num++;
- device_status.refresh_timeout++;
- if(device_status.version_type!=6)
- {
- HMI_WRITE("uploadSet.iccid.txt=\"%s\"",device_status.iccid);
- if(strstr(device_status.mqttcfg1,"8.136.98.49")!=NULL)
- {
- HMI_WRITE("uploadSet.plt.val=%d",RESET);
- }
- else if (strstr(device_status.mqttcfg1,"120.27.222.26")!=NULL) {
- HMI_WRITE("uploadSet.plt.val=%d",SET);
- }
- //HMI_WRITE("mqttcfg.mqtt1Message.txt=\"%s\"",device_status.mqttcfg1);
- HMI_WRITE("uploadSet.t0.txt=\"%s\"",device_status.mqttcfg1);
- //更新固件版本号
- HMI_WRITE("debug.t3.txt=\"%s\"",device_status.driver);
- //更新固件编译日期
- HMI_WRITE("debug.t4.txt=\"%s\"",device_status.driver_compile);
- //
- HMI_WRITE("debug.t5.txt=\"%s\"",device_status.driver_bl);
- HMI_WRITE("debug.t7.txt=\"%s\"",device_status.driver_compile);
- //更新dtu/rtu版本
- HMI_WRITE("debug.t9.txt=\"%s\"",device_status.driver_4g);
- //更新时间
- 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);
- 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);
- HMI_WRITE("set.t0.txt=\"%d\"",tm.tm_hour);
- HMI_WRITE("set.t1.txt=\"%d\"",tm.tm_min);
- HMI_WRITE("set.t2.txt=\"%d\"",tm.tm_sec);
- //更新电压
- HMI_WRITE("workSta.vbat.txt=\"%s\"",device_status.voltage);
- //更新电流
- HMI_WRITE("workSta.power.txt=\"%s\"",device_status.power);
- //更新温湿度数据
- HMI_WRITE("workSta.envHum.val=%d",atoi(device_status.humidity));
- HMI_WRITE("workSta.envTem.val=%d",atoi(device_status.tempture));
- //更新电池状态
- HMI_WRITE("workSta.batStatus.val=%d",device_status.bat_status);
- //更新上仓门状态
- HMI_WRITE("workSta.upStatus.val=%d",device_status.up_status);
- //更新下仓门状态
- HMI_WRITE("workSta.downStatus.val=%d",device_status.down_status);
- //更新工作状态
- HMI_WRITE("workSta.workStatus.val=%d",device_status.work_status);
- //更新温控状态
- HMI_WRITE("workSta.tempStatus.val=%d",device_status.temp_status);
- //更新雨控状态
- HMI_WRITE("workSta.rainStatus.val=%d",device_status.rain_status);
- //更新光控状态
- HMI_WRITE("workSta.sunStatus.val=%d",device_status.sun_status);
- //更新收集时间
- HMI_WRITE("sysSet.stime.val=%d",device_status.clt_time);
- //更新加热时间
- HMI_WRITE("workSta.heatTime.val=%d",device_status.heattime);
- HMI_WRITE("sysSet.jtime.val=%d",device_status.heattime);
- //
- HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
- //更新加热时间和温度
- HMI_WRITE("workSta.heatTem.val=%d",device_status.heattem);
- HMI_WRITE("sysSet.jheat.val=%d",device_status.heatvalue);
- //更新加热状态
- HMI_WRITE("workSta.heatStatus.val=%d",device_status.heat_status);
- //更新密码
- HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
- //更新工作进度
- //HMI_WRITE("menu.workProgress.val=%d",);
- //更新定时模式
- HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
- //更新光控定时时长
- HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
- //更新时控开始
- HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
- //更新时控结束
- HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
- //SIM卡状态
- HMI_WRITE("workSta.bt0.val=%d",atoi(device_status.sim));
- //ID
- HMI_WRITE("menu.equipid.txt=\"%s\"",device_status.imei);
- HMI_WRITE("menu.deviceId.txt=\"%s\"",device_status.deviceId);
- //HMI_WRITE("workSta.equipid.txt=\"%s\"",device_status.imei);
- //数据上传间隔
- HMI_WRITE("uploadSet.datt.val=%d",device_status.dattim);
- //语音音量
- HMI_WRITE("sysSet.vol.val=%d",device_status.vol);
- //更新熄屏时间
- if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
- HMI_WRITE("thsp=%d",device_status.thsp);
- if(strstr(device_status.link_ip,".")!=RT_NULL){
- HMI_WRITE("menu.t0.txt=\"%s\"",device_status.link_ip);
- }
- #if MATCH_DEVICE == 1
- HMI_WRITE("menu.mode.val=%d",device_status.work_mode);
- HMI_WRITE("menu.upan.val=%d",device_status.upan_sta);
- HMI_WRITE("menu.alarm_tg.txt=\"%s\"",device_status.alarm_tg);
- //更新有线连接状态
- HMI_WRITE("menu.va_net.val=%d",0);
- #else
- HMI_WRITE("menu.va_net.val=%d",device_status.link_sta);
- #endif
- }
- HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
- HMI_WRITE("menu.version.txt=\"%s\"",device_status.driver);
- if(atoi(device_status.gps)==1){
- HMI_WRITE("menu.gps_state.txt=\"定位成功:(GPS+BD)%d/%d\"",device_status.gps_inuse,device_status.gps_view);
- }else if(atoi(device_status.gps)==2){
- HMI_WRITE("menu.gps_state.txt=\"定位成功:(基站)\"");
- }
- //更新联网状态
- if(atoi(device_status.net)==1)
- {
- HMI_WRITE("menu.net.val=1");
- HMI_WRITE("workSta.net.val=1");
- }
- else {
- #if MATCH_DEVICE == 0
- HMI_WRITE("workSta.net.val=0");
- HMI_WRITE("menu.net.val=0");
- //更新CSQ
- HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
- HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
- HMI_WRITE("workSta.csq_value.txt=\"%s\"",device_status.csq);
- #else
- HMI_WRITE("workSta.net.val=1");
- HMI_WRITE("menu.net.val=1");
- HMI_WRITE("menu.va_csq.val=0");
- #endif
- }
- if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
- {
- HMI_WRITE("menu.net.val=1");
- HMI_WRITE("workSta.net.val=2");
- }
- /**********************************************************************/
- }
- /***************************** 解析从屏幕收到的数据 begin **********************************/
- /* ==================== 屏幕测试加热:定时自动关闭 ====================
- * 需求:屏幕测试页打开加热(putTest.switchHeat=1)后,10 分钟自动关闭,避免忘记关导致干烧。
- * 实现:一次性软定时器;只在"本次加热由屏幕测试打开"时生效——手动关闭(switchHeat=0)
- * 或定时到点都会撤销;若定时到点时设备正处于工作流程中(work_status==1),
- * 加热由工作循环(calorstat)托管,则本定时器不干预,仅撤销自己。
- * 维护提示:如需改自动关闭时长,只改 HMI_TEST_HEAT_TIMEOUT 一处。 */
- #define HMI_TEST_HEAT_TIMEOUT (10 * 60) /* 秒 */
- static rt_timer_t s_test_heat_timer = RT_NULL;
- static volatile rt_bool_t s_test_heat_on = RT_FALSE;
- static void hmi_test_heat_timeout(void *parameter)
- {
- if (!s_test_heat_on)
- {
- return;
- }
- s_test_heat_on = RT_FALSE;
- if (device_status.work_status == 1)
- {
- LOG_I("test heat timeout ignored (work in progress)");
- return;
- }
- ctr_Heat(OFF);
- device_status.heat_status = 0;
- HMI_WRITE("sysTest.switchHeat.val=0");
- HMI_WRITE("workSta.heatStatus.val=0");
- LOG_W("屏幕测试加热已自动关闭(超时 %d 秒)", HMI_TEST_HEAT_TIMEOUT);
- }
- static void hmi_test_heat_start(void)
- {
- if (s_test_heat_timer == RT_NULL)
- {
- s_test_heat_timer = rt_timer_create("t_testheat", hmi_test_heat_timeout, RT_NULL,
- HMI_TEST_HEAT_TIMEOUT * RT_TICK_PER_SECOND,
- RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
- if (s_test_heat_timer == RT_NULL)
- {
- LOG_E("rt_timer_create t_testheat failed!");
- return;
- }
- }
- s_test_heat_on = RT_TRUE;
- rt_timer_stop(s_test_heat_timer);
- rt_timer_start(s_test_heat_timer); /* 每次打开都重新计时 */
- }
- static void hmi_test_heat_stop(void)
- {
- s_test_heat_on = RT_FALSE;
- if (s_test_heat_timer != RT_NULL)
- {
- rt_timer_stop(s_test_heat_timer);
- }
- }
- /* ==================== workSta 工作过程刷新(屏幕 t1~t6 + 拍照/清扫状态) ====================
- * 数据来源:control.c 各控制流程实时写入 device_status.work_*、photo_left、clear_left;
- * hmi.c Ctr_Mortor_entry 写入 work_door_left / photo_left;progress 由
- * Work_Progress_Get() 按时间加权计算。
- * 下发条件:HMI_WORK_PROC_VT(vt) 当前为 (vt)!=6 —— 除吸虫塔外的页面都下发,
- * 孢子仪(dtype!=3)在前一行条件已被挡掉。 */
- #define HMI_WORK_PROC_VT(vt) ((vt) != 6) /* 需要显示工作过程的版本类型 */
- /* t1~t6 数值朗读:为 0 时下发空字符串(屏幕该栏显示空白),非 0 下发数值;
- * percent=RT_TRUE 时数值后带 % 号(t4 进度显示形如 "45%")。
- * 实现要点:每个控件只保留**一个** HMI_WRITE 调用点(数值先格式化成字符串、统一走 %s)。
- * 因为 HMI_WRITE 的重复过滤按调用点记录"上次内容"——只有单调用点时,
- * "上次内容"才恒等于"屏幕当前显示"。若拆成 空串/数值 两个调用点,两个缓存互相独立:
- * 从"有值(如 1)"切回 0 时会命中空串调用点的旧缓存而被跳过,屏幕会残留上一个数字
- * (要等到过滤表 10s 周期复位才纠正)。 */
- static void hmi_write_num_or_blank(char *fmt, uint16_t v, rt_bool_t percent)
- {
- char num[8];
- if (v == 0)
- {
- num[0] = '\0'; /* 0 → 空串,屏幕显示空白 */
- }
- else if (percent)
- {
- rt_snprintf(num, sizeof(num), "%u%%", v);
- }
- else
- {
- rt_snprintf(num, sizeof(num), "%u", v);
- }
- HMI_WRITE(fmt, num);
- }
- static void hmi_refresh_work_proc(void)
- {
- if(device_status.dtype != 3)
- {
- return;
- }
- /* 拍照状态:device_status.photo_ws(0-停止 1-拍照中) */
- HMI_WRITE("workSta.photoSta.val=%d", (device_status.photo_ws != 0) ? 1 : 0);
- /* 清扫状态:device_status.clear_status(0-停止 1-清扫中,2-转仓动作) */
- HMI_WRITE("workSta.clearSta.val=%d", (device_status.clear_status != 0) ? 1 : 0);
- /* t1~t6:工作过程实时数据(文本控件;数值为 0 时下发空串,该栏显示空白) */
- hmi_write_num_or_blank("workSta.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
- hmi_write_num_or_blank("workSta.t2.txt=\"%s\"", device_status.work_clt_left, RT_FALSE); /* 收集倒计时(s) */
- hmi_write_num_or_blank("workSta.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
- hmi_write_num_or_blank("workSta.t4.txt=\"%s\"", Work_Progress_Get(), RT_TRUE); /* 工作进度(%) */
- hmi_write_num_or_blank("workSta.t5.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
- hmi_write_num_or_blank("workSta.t6.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
- }
- /* ==================== 屏幕命令严格匹配(2026-10-08 修复) ====================
- * 屏幕命令以 \r 结尾, 但串口按包分段: 一条 "SwversionV43" 可能被拆成
- * "SwversionV4" + "3" 两段先后到达。原实现用 strstr() 前缀包含匹配,
- * 前段 "SwversionV4" 会误命中"水稻"分支, 把 version_type 错误切换(并误报 RTU、
- * 重复写 Flash、随后完整帧又切回, 配置来回翻转)。
- * 本函数要求: 命令字完整相等, 且其后紧跟 \r(完整帧结束符, 兼容 \r\n);
- * 分包残段没有 \r, 会被拒绝, 等屏幕后续完整帧(心跳重发)正常处理。
- * 用法: 替换存在"短命令是长命令前缀"关系的分支(SwversionV1/V4)。 */
- static rt_bool_t hmi_cmd_match(const char *buf, const char *cmd)
- {
- size_t n = rt_strlen(cmd);
- if (rt_strncmp(buf, cmd, n) != 0)
- {
- return RT_FALSE;
- }
- return (buf[n] == '\r') ? RT_TRUE : RT_FALSE;
- }
- /* ==================== 屏幕 debug1 调试页(2026-10-13 新增) ====================
- * 对应陶晶驰(USART HMI)工程中的调试页 debug1,功能与协议如下:
- * ① 信息区(t0 RT-Thread版本 / t1 内核 / t2 BSP型号 / t13 运行时长 / t5 BL版本 /
- * t3 固件版本 / t4 编译时间 / t11+j0 CPU占用 / t12+j1 RAM占用 / t10 外挂Flash):
- * 屏幕侧在该页定时器(或进入页面)下发 "getDebugData",固件整批刷新(HMI_WRITE 带重复过滤,
- * 未变化的项不会重复占用串口带宽);
- * ② 按钮区 b0~b9(屏幕弹起事件下发文本命令,以 "\r\n" 结束):
- * reboot 重启(沿用既有分支 rt_hw_cpu_reset)
- * bootloader 启动BL:置 stop_in_bootloader 并重启进入 BootLoader(沿用既有分支)
- * startapp 启动APP:清除 stop_in_bootloader 并重启(下次直接启动 APP)
- * dbg_on 打开调试:把 RT-Thread 控制台输出镜像到 debug1 底部文本框
- * dbg_off 关闭调试:恢复原控制台设备
- * dbg_clr 清空固件侧控制台缓存与屏幕文本框
- * dbg_erase 擦除参数:恢复出厂默认参数(按供电类型走交流/电池两套默认值)
- * dbg_restore 恢复固件:置 restore_pending + stop_in_bootloader 并重启进 BL,
- * 由 BL 执行 df_area→app 恢复(BL 侧支持见交付说明;未支持时不会误动作)
- * dbg:<命令> 把 <命令> 作为 msh 命令在本机执行,输出回传文本框(b4 发送按钮)
- * ③ 调试控制台实现:注册只写"捕获设备" dbgcap,打开调试时 rt_console_set_device("dbgcap"),
- * 此后 rt_kprintf / finsh / msh 的输出全部经其 write 回调缓存并推送屏幕文本框
- * (换行按 TJC 官方约定用 "\r" 两字符转义下发,不能用 \n);同时经"转发设备"
- * DBG1_CHAIN_DEV(默认 uart1) 原样转发,保证本机调试口日志不停流。命令执行放在
- * 独立线程 dbg_exec_thread,避免 top 之类的长命令把屏幕刷新线程卡死。
- * 注意:转发设备只 find 句柄、不自行 open/close。(本工程 apm32f1 用 serial_v2:
- * open 已打开设备直接返回、标志位只增不覆盖,不存在 v1 的接收位被洗问题,
- * 故未移植 dbg_chain_fixup / hmi_uart4_fixup_rx)
- * ④ 屏幕文本框收发约定("单框收发"设计,与截图的底部 msh>> 框一致):
- * - 固件每次下发的内容尾部都会附加一行提示符 "msh >> "(宏 DBG1_PROMPT);
- * - 用户点框→键盘输入→按[发送](b4);b4 弹起事件发送:printh 64 62 67 3A("dbg:")
- * + prints "debug1.<框名>.txt",0 + printh 0d 0a;
- * - 固件收到 "dbg:"+整个框内容后,取**最后一个 ">>" 之后**的文本作为命令执行
- * (无 ">>" 时按纯命令处理,兼容"独立输入框"的接法),执行结果回填框内。
- * ⑤ 推送节流与页面联动:输出推送带最小间隔节流;且仅在"该页有心跳"(最近
- * DBG1_PAGE_ALIVE_TICK 内收到过 getDebugData)时才推送——离开调试页后自动停推,
- * 返回调试页后自动恢复(缓存不丢,回页即见最新内容)。
- * 注意:打开调试期间本机 UART1 调试口日志经转发保留;控制台设备在"关闭调试"后恢复原样。
- * 维护提示:屏幕控件名若变化,仅需改 DBG1_CON_OBJ 宏及下方各 HMI_WRITE 中的控件名。 */
- #define DBG1_CON_OBJ "msh" /* 底部调试文本框对象名(改名只需改这一处) */
- #define DBG1_PROMPT "msh >> " /* 文本框尾部提示符(兼作命令提取锚点) */
- #define DBG1_CHAIN_DEV "uart1" /* 日志转发设备(本机调试口);置 "" 可关闭转发 */
- #define DBG1_CAP_DEV_NAME "dbgcap" /* 控制台捕获设备名(仅固件内部使用) */
- #define DBG1_CON_RAW_MAX 200 /* 行缓存上限(约文本框一屏可见字符数,可调) */
- #define DBG1_CON_ESC_MAX 220 /* 单次下发转义后上限(受 HMI_WRITE 内部 256B 缓冲限制) */
- #define DBG1_CON_PUSH_MAX 120 /* 单次下发取缓存尾部长度(转义前)。受"b4 发送会回传整框"
- * 约束:中文按 UTF-8 3B/字,120 字符最坏 360B,
- * 加提示符与用户输入仍小于单帧上限 HMI_FRAME_MAX(512) */
- #define DBG1_CMD_MAX 100 /* 屏幕控制台单条命令最大长度 */
- #define DBG1_EXEC_PRIO 22 /* 调试命令执行线程优先级(低于屏幕线程) */
- #define DBG1_EXEC_STACK_SIZE 3072 /* 调试命令执行线程栈 */
- #define DBG1_FLUSH_MIN_TICK 30 /* 输出推送最小间隔(tick,10ms/tick;纯节流不丢行) */
- #define DBG1_PAGE_ALIVE_TICK 500 /* 调试页心跳存活窗口(5s):超过则暂停推送,离开页面不再占带宽 */
- #define DBG1_BSP_NAME "APM32F103RET6" /* t2 BSP型号显示(按实际板卡) */
- #define DBG1_CORE_NAME "Cortex-M3" /* t1 内核显示(如需改成内核版本号改这里) */
- #define DBG1_PART_OBJ "t14" /* 分区表文本框对象名(屏幕侧新增控件;改名只改这一处) */
- #define DBG1_PART_MAX 200 /* 分区表下发字符串上限(单条 HMI_WRITE 上限约 250B,留余量) */
- static char dbg_con_raw[DBG1_CON_RAW_MAX + 2]; /* 控制台行缓存(屏幕文本框内容源) */
- static rt_uint16_t dbg_con_len = 0;
- static rt_bool_t dbg_mirror_on = RT_FALSE; /* 控制台是否已镜像到屏幕(打开调试) */
- static char dbg_saved_console[RT_NAME_MAX + 1]; /* 打开调试前的控制台设备名 */
- static struct rt_device dbg_con_dev; /* 只写捕获设备(dbgcap) */
- static rt_bool_t dbg_con_dev_ready = RT_FALSE;
- static rt_device_t dbg_chain_dev = RT_NULL; /* 日志转发设备句柄(只 find,不持有引用;见 dbg_chain_fixup) */
- static volatile rt_tick_t s_dbg1_page_tick = 0; /* 最近一次收到 getDebugData 的时刻(0=从未) */
- static struct rt_thread dbg_exec_thread;
- static rt_uint8_t dbg_exec_stack[DBG1_EXEC_STACK_SIZE];
- static struct rt_semaphore dbg_exec_sem;
- static char dbg_cmd_buf[DBG1_CMD_MAX + 1];
- static volatile rt_uint8_t dbg_exec_busy = 0;
- static rt_bool_t dbg_exec_inited = RT_FALSE;
- static rt_bool_t dbg_exec_ready = RT_FALSE;
- /* ---------- 控制台行缓存与屏幕推送 ---------- */
- static void dbg_con_append(const char *s, rt_uint16_t n)
- {
- if (n == 0)
- {
- return;
- }
- /* 缓存满:丢弃最旧内容(优先从行边界截断),保留最新输出 */
- if ((rt_uint32_t)dbg_con_len + n > DBG1_CON_RAW_MAX)
- {
- rt_uint16_t cut = (dbg_con_len > (DBG1_CON_RAW_MAX / 3)) ? (dbg_con_len - DBG1_CON_RAW_MAX / 3) : 0;
- while (cut < dbg_con_len && dbg_con_raw[cut] != '\n')
- {
- cut++;
- }
- cut = (cut < dbg_con_len) ? (cut + 1) : dbg_con_len;
- if (cut > 0)
- {
- rt_memmove(dbg_con_raw, dbg_con_raw + cut, dbg_con_len - cut);
- dbg_con_len -= cut;
- }
- }
- if (n > DBG1_CON_RAW_MAX - dbg_con_len) /* 单次超长:只保留尾部 */
- {
- s += (n - (DBG1_CON_RAW_MAX - dbg_con_len));
- n = DBG1_CON_RAW_MAX - dbg_con_len;
- }
- rt_memcpy(dbg_con_raw + dbg_con_len, s, n);
- dbg_con_len += n;
- dbg_con_raw[dbg_con_len] = '\0';
- }
- static void dbg_con_puts(const char *s)
- {
- dbg_con_append(s, (rt_uint16_t)rt_strlen(s));
- }
- /* 把行缓存转义后推送到屏幕文本框。
- * TJC 字符串转义(官方 wiki 约定):换行 -> "\r"(\ 和 r 两个字符,不要用 \n);双引号 -> \"。
- * 下发内容 = [缓存尾部] + 换行 + 提示符 DBG1_PROMPT —— 提示符固定收尾,用户直接在框尾
- * 输入命令后按[发送],固件即可按"最后一个 >> 之后"提取命令(见 dbg_exec_submit)。
- * force=RT_FALSE 时按 DBG1_FLUSH_MIN_TICK 做最小间隔节流(只影响推送时机,不丢内容);
- * 页面心跳消失(离开调试页)超过 DBG1_PAGE_ALIVE_TICK 时暂存不推,回页后自动恢复。 */
- static void dbg_con_flush(rt_bool_t force)
- {
- static rt_tick_t s_last_flush = 0;
- char esc[DBG1_CON_ESC_MAX + 1];
- rt_uint16_t i, n = 0, raw_len = dbg_con_len;
- const char *p = dbg_con_raw;
- if (raw_len == 0 || rt_interrupt_get_nest() != 0) /* 中断上下文不推送,等下次 */
- {
- return;
- }
- /* 页面联动:从未收到心跳或心跳已停(离开调试页)时暂不推送 */
- if (s_dbg1_page_tick == 0 ||
- (rt_tick_get() - s_dbg1_page_tick) > DBG1_PAGE_ALIVE_TICK)
- {
- return;
- }
- if (force == RT_FALSE && (rt_tick_get() - s_last_flush) < DBG1_FLUSH_MIN_TICK)
- {
- return;
- }
- s_last_flush = rt_tick_get();
- if (raw_len > DBG1_CON_PUSH_MAX) /* 超长只保留尾部(屏幕一屏可见有限) */
- {
- p += (raw_len - DBG1_CON_PUSH_MAX);
- raw_len = DBG1_CON_PUSH_MAX;
- }
- for (i = 0; i < raw_len && n < DBG1_CON_ESC_MAX - 16; i++)
- {
- char c = p[i];
- if (c == '\n')
- {
- if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = 'r'; }
- }
- else if (c == '"')
- {
- if (n < DBG1_CON_ESC_MAX - 16) { esc[n++] = '\\'; esc[n++] = '"'; }
- }
- else
- {
- esc[n++] = c;
- }
- }
- if (n > 0 && esc[n - 1] == '\\') /* 截断落在转义符中间时丢弃半个转义 */
- {
- n--;
- }
- /* 追加换行 + 提示符(预留空间已在循环上限扣除) */
- esc[n++] = '\\';
- esc[n++] = 'r';
- for (i = 0; DBG1_PROMPT[i] != '\0' && n < DBG1_CON_ESC_MAX; i++)
- {
- esc[n++] = DBG1_PROMPT[i];
- }
- esc[n] = '\0';
- HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"%s\"", esc);
- }
- /* ---------- 控制台捕获设备(只写,替代 uart1 作为 rt_console_set_device 目标) ---------- */
- static rt_err_t dbg_con_dev_open(rt_device_t dev, rt_uint16_t oflag)
- {
- (void)dev; (void)oflag;
- return RT_EOK;
- }
- static rt_err_t dbg_con_dev_close(rt_device_t dev)
- {
- (void)dev;
- return RT_EOK;
- }
- static rt_size_t dbg_con_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
- {
- (void)dev; (void)pos; (void)buffer; (void)size;
- return 0;
- }
- /* 捕获 rt_kprintf/finsh/msh 的全部输出:① 转发到 DBG1_CHAIN_DEV(本机调试口,保持日志不停流);
- * ② 过滤 ANSI 转义与编辑控制字符后按行入缓存,行尾或节流点推送屏幕(页面心跳存在时)。 */
- static rt_size_t dbg_con_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
- {
- static rt_uint8_t s_esc = 0; /* ANSI 转义过滤状态机:0-普通 1-已见ESC 2-ESC[ 序列 */
- const char *p = (const char *)buffer;
- rt_size_t i;
- (void)dev; (void)pos;
- /* ① 本机调试口转发(best effort:设备未打开则静默跳过) */
- if (dbg_chain_dev != RT_NULL)
- {
- rt_device_write(dbg_chain_dev, 0, buffer, size);
- }
- for (i = 0; i < size; i++)
- {
- char c = p[i];
- if (s_esc == 0)
- {
- if ((rt_uint8_t)c == 0x1B) { s_esc = 1; continue; }
- }
- else if (s_esc == 1)
- {
- s_esc = (c == '[') ? 2 : 0;
- continue;
- }
- else
- {
- if ((rt_uint8_t)c >= 0x40 && (rt_uint8_t)c <= 0x7E) s_esc = 0;
- continue;
- }
- if (c == '\n')
- {
- dbg_con_append("\n", 1);
- if (dbg_mirror_on || dbg_exec_busy) dbg_con_flush(RT_FALSE);
- }
- else if (c == '\t')
- {
- dbg_con_append(" ", 1);
- }
- else if (c == '\r' || (rt_uint8_t)c < 0x20 || (rt_uint8_t)c == 0x7F)
- {
- /* \r 与其它控制字符(含 finsh 编辑用的 \b)不显示 */
- }
- else
- {
- dbg_con_append(&c, 1);
- }
- }
- return size;
- }
- static void dbg_con_dev_init(void)
- {
- if (dbg_con_dev_ready)
- {
- return;
- }
- rt_memset(&dbg_con_dev, 0, sizeof(dbg_con_dev));
- dbg_con_dev.type = RT_Device_Class_Char;
- dbg_con_dev.rx_indicate = RT_NULL;
- dbg_con_dev.tx_complete = RT_NULL;
- dbg_con_dev.init = RT_NULL;
- dbg_con_dev.open = dbg_con_dev_open;
- dbg_con_dev.close = dbg_con_dev_close;
- dbg_con_dev.read = dbg_con_dev_read;
- dbg_con_dev.write = dbg_con_dev_write;
- dbg_con_dev.control = RT_NULL;
- if (rt_device_register(&dbg_con_dev, DBG1_CAP_DEV_NAME, RT_DEVICE_FLAG_STREAM) == RT_EOK)
- {
- dbg_con_dev_ready = RT_TRUE;
- }
- }
- /* ---------- (apm32f1) 本机口/屏幕口"接收位保护":无需移植 ----------
- * master(v1 serial) 的 dbg_chain_fixup()/hmi_uart4_fixup_rx() 用于修复
- * "rt_serial_open 用 open_flag=oflag&0xff 覆盖标志位、洗掉 INT_RX/DMA_RX 导致
- * 无法输入"的问题。本工程用 serial_v2 驱动:rt_serial_open 对已打开设备直接返回、
- * 标志位一律 |= 不覆盖(serial_v2.c),不存在该缺陷,故整段未移植。 */
- static void dbg_console_save(void)
- {
- rt_device_t cur = rt_console_get_device();
- /* 已处于捕获设备(如执行线程临时切换期间被并发调用)时不覆盖记录,
- * 否则"关闭调试"会恢复到 dbgcap 上,永远回不到真实控制台 */
- if (cur != RT_NULL && rt_strcmp(cur->parent.name, DBG1_CAP_DEV_NAME) == 0)
- {
- return;
- }
- if (cur != RT_NULL) rt_strncpy(dbg_saved_console, cur->parent.name, RT_NAME_MAX);
- else rt_strncpy(dbg_saved_console, "uart1", RT_NAME_MAX);
- dbg_saved_console[RT_NAME_MAX] = '\0';
- }
- static void dbg_console_restore(void)
- {
- if (dbg_saved_console[0] != '\0') rt_console_set_device(dbg_saved_console);
- else rt_console_set_device("uart1");
- }
- /* ---------- 屏幕控制台:命令执行(独立线程,防长命令卡死屏幕线程) ---------- */
- static rt_bool_t dbg_cmd_is_blocking(const char *cmd)
- {
- while (*cmd == ' ' || *cmd == '\t')
- {
- cmd++;
- }
- /* top/Msh_DisplaySysMonitor 为持续刷新命令,在屏幕控制台里无法按键退出,直接拒绝 */
- if (rt_strncmp(cmd, "top", 3) == 0 && (cmd[3] == '\0' || cmd[3] == ' '))
- {
- return RT_TRUE;
- }
- if (rt_strncmp(cmd, "Msh_DisplaySysMonitor", 21) == 0)
- {
- return RT_TRUE;
- }
- return RT_FALSE;
- }
- static void dbg_exec_run(void)
- {
- rt_bool_t switched = RT_FALSE;
- char cmd[DBG1_CMD_MAX + 1];
- rt_memcpy(cmd, dbg_cmd_buf, DBG1_CMD_MAX + 1);
- cmd[DBG1_CMD_MAX] = '\0';
- if (dbg_cmd_is_blocking(cmd))
- {
- dbg_con_puts("(该命令为持续刷新命令,不支持屏幕控制台执行)\n");
- dbg_con_flush(RT_TRUE);
- return;
- }
- /* 未开镜像时临时把控制台切到捕获设备,保证 msh 输出可被捕获 */
- if (dbg_mirror_on == RT_FALSE)
- {
- dbg_con_dev_init();
- if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
- {
- dbg_con_puts("(捕获设备未就绪,命令未执行)\n");
- dbg_con_flush(RT_TRUE);
- return;
- }
- dbg_console_save();
- rt_console_set_device(DBG1_CAP_DEV_NAME); /* 转发只需设备引用,无需自行 open */
- switched = RT_TRUE;
- }
- dbg_con_puts("msh> ");
- dbg_con_puts(cmd);
- dbg_con_append("\n", 1);
- dbg_con_flush(RT_FALSE);
- msh_exec(cmd, rt_strlen(cmd)); /* 输出经控制台进入捕获设备 -> 行缓存 -> 屏幕 */
- if (switched)
- {
- dbg_console_restore(); /* 恢复控制台;其内部 open 会洗掉接收位 */
- }
- dbg_con_flush(RT_TRUE);
- }
- static void dbg_exec_thread_entry(void *parameter)
- {
- (void)parameter;
- while (1)
- {
- if (rt_sem_take(&dbg_exec_sem, RT_WAITING_FOREVER) != RT_EOK)
- {
- continue;
- }
- dbg_exec_busy = 1;
- dbg_exec_run();
- dbg_exec_busy = 0;
- }
- }
- static void dbg_exec_init(void)
- {
- if (dbg_exec_inited)
- {
- return;
- }
- dbg_exec_inited = RT_TRUE;
- rt_sem_init(&dbg_exec_sem, "dbgsem", 0, RT_IPC_FLAG_FIFO);
- if (rt_thread_init(&dbg_exec_thread, "dbgexec", dbg_exec_thread_entry, RT_NULL,
- dbg_exec_stack, sizeof(dbg_exec_stack), DBG1_EXEC_PRIO, 10) == RT_EOK)
- {
- rt_thread_startup(&dbg_exec_thread);
- dbg_exec_ready = RT_TRUE;
- }
- }
- /* 从 "dbg:" 之后的载荷中提取命令并提交执行线程。
- * 载荷 = 屏幕文本框的整个内容("单框收发"设计),提取规则:
- * 取**最后一个 ">>"** 之后的文本作为命令(框尾固定有提示符 "msh >> ",用户在其后输入);
- * 载荷中不含 ">>" 时按"纯命令"处理(兼容另接独立命令输入框的用法);
- * 再裁剪:跳过前导空白、截至行尾/上限、去尾部空白。 */
- static void dbg_exec_submit(const char *payload)
- {
- const char *cmd = payload;
- const char *p = payload;
- const char *last = RT_NULL;
- rt_uint16_t n = 0;
- dbg_exec_init();
- while ((p = rt_strstr(p, ">>")) != RT_NULL) /* 定位最后一个 ">>" */
- {
- last = p;
- p += 2;
- }
- if (last != RT_NULL)
- {
- cmd = last + 2;
- }
- while (*cmd == ' ' || *cmd == '\t') /* 跳过提示符后的空白 */
- {
- cmd++;
- }
- while (cmd[n] != '\0' && cmd[n] != '\r' && cmd[n] != '\n' && n < DBG1_CMD_MAX)
- {
- dbg_cmd_buf[n] = cmd[n];
- n++;
- }
- while (n > 0 && (dbg_cmd_buf[n - 1] == ' ' || dbg_cmd_buf[n - 1] == '\t'))
- {
- n--;
- }
- dbg_cmd_buf[n] = '\0';
- if (n == 0)
- {
- return; /* 空命令(只发了提示符)不处理 */
- }
- if (dbg_exec_ready == RT_FALSE)
- {
- dbg_con_puts("(调试执行线程创建失败)\n");
- dbg_con_flush(RT_TRUE);
- return;
- }
- if (dbg_exec_busy)
- {
- dbg_con_puts("(上一条命令仍在执行,请稍候)\n");
- dbg_con_flush(RT_TRUE);
- return;
- }
- rt_sem_release(&dbg_exec_sem);
- }
- /* ---------- debug1 按钮处理 ---------- */
- static void dbg_console_on(void)
- {
- dbg_con_dev_init();
- if (rt_device_find(DBG1_CAP_DEV_NAME) == RT_NULL)
- {
- dbg_con_puts("(捕获设备注册失败,无法打开屏幕调试)\n");
- dbg_con_flush(RT_TRUE);
- return;
- }
- if (dbg_mirror_on == RT_FALSE)
- {
- dbg_console_save(); /* 记录当前控制台(uart1) */
- rt_console_set_device(DBG1_CAP_DEV_NAME); /* 输出切到捕获设备;日志仍转发本机口 */
- dbg_mirror_on = RT_TRUE;
- }
- dbg_con_puts("--- 屏幕调试已打开:日志同步到本框;在提示符后输入命令按[发送] ---\n");
- dbg_con_flush(RT_TRUE);
- }
- static void dbg_console_off(void)
- {
- if (dbg_mirror_on)
- {
- dbg_console_restore(); /* 恢复控制台:内部 open 会洗掉接收模式位 */
- dbg_mirror_on = RT_FALSE;
- }
- dbg_con_puts("--- 屏幕调试已关闭(控制台已恢复) ---\n");
- dbg_con_flush(RT_TRUE);
- }
- static void dbg_con_clear(void)
- {
- dbg_con_len = 0;
- dbg_con_raw[0] = '\0';
- /* 复位重复过滤表:否则本调用点缓存仍是上次发出的内容,
- * 二次点击"清空"(期间文本框已被日志刷新)会被去重逻辑跳过 */
- hmi_dedupe_reset();
- HMI_WRITE("debug1." DBG1_CON_OBJ ".txt=\"" DBG1_PROMPT "\""); /* 清空后仅保留提示符便于继续输入 */
- }
- static void dbg_erase_params(void)
- {
- LOG_I("debug1: 擦除参数(恢复出厂默认)");
- if (device_status.Power_Type == 1) ResetParamDefault_DC(); /* 电池供电默认值 */
- else ResetParamDefault(); /* 交流供电默认值 */
- dbg_con_puts("--- 参数已恢复出厂默认值 ---\n");
- dbg_con_flush(RT_TRUE);
- }
- static void dbg_start_app(void)
- {
- LOG_I("debug1: 启动APP(清除BL停留标志), 重启");
- device_status.stop_in_bootloader = 0;
- ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
- sizeof(device_status.stop_in_bootloader));
- rt_thread_mdelay(200);
- rt_hw_cpu_reset();
- }
- static void dbg_restore_fw(void)
- {
- uint8_t pending = 1;
- LOG_I("debug1: 恢复固件(置标志并重启进BL)");
- /* APP 正在 app 分区运行,不能自擦运行分区;置标志重启进 BL 恢复。
- * restore_pending:供 BL 读取(BL 支持后自动执行 df_area→app 恢复)。 */
- device_status.stop_in_bootloader = 1;
- ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader,
- sizeof(device_status.stop_in_bootloader));
- ef_set_env_blob("restore_pending", &pending, sizeof(pending));
- rt_thread_mdelay(200);
- rt_hw_cpu_reset();
- }
- /* debug1 页指令分发:返回 RT_TRUE 表示指令属于 debug1 并已处理。
- * 注意:调用点必须放在 HMI_Decode 的 if/else 链最前 —— "dbg:<命令>" 的
- * 命令内容里可能包含 reboot/clear 等既有分支的关键字,不能让它们先匹配。
- * 另:能收到本页按钮指令即证明用户停留在调试页,匹配成功时顺带刷新页面心跳
- * (s_dbg1_page_tick),使按钮在未配 1 秒心跳定时器时也能独立工作。 */
- static rt_bool_t hmi_debug1_handle(const char *frame)
- {
- if (rt_strncmp(frame, "dbg:", 4) == 0) /* b4 发送:执行控制台命令 */
- {
- s_dbg1_page_tick = rt_tick_get();
- dbg_exec_submit(frame + 4);
- return RT_TRUE;
- }
- if (strstr(frame, "dbg_on") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_on(); return RT_TRUE; }
- if (strstr(frame, "dbg_off") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_console_off(); return RT_TRUE; }
- if (strstr(frame, "dbg_clr") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_con_clear(); return RT_TRUE; }
- if (strstr(frame, "dbg_erase") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_erase_params(); return RT_TRUE; }
- if (strstr(frame, "dbg_restore") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_restore_fw(); return RT_TRUE; }
- if (strstr(frame, "startapp") != RT_NULL) { s_dbg1_page_tick = rt_tick_get(); dbg_start_app(); return RT_TRUE; }
- return RT_FALSE;
- }
- /* ---------- debug1 信息区整批刷新("getDebugData" 触发) ---------- */
- /* JEDEC 厂商 ID → 名称(取值与 SFUD 内置 mf_table 一致,仅供显示用)。
- * 2026-10-13 新增:芯片走 SFDP 识别路径时 SFUD 不填 chip.name(见 t10 注释),
- * 用厂商名兜底可读性最好。 */
- static const char *dbg1_flash_vendor(uint8_t mf_id)
- {
- static const struct { uint8_t id; const char *name; } tbl[] = {
- {0x01, "Cypress"}, {0x04, "Fujitsu"}, {0x1C, "EON"}, {0x1F, "Atmel"},
- {0x20, "Micron"}, {0x37, "AMIC"}, {0x62, "Sanyo"}, {0x89, "Intel"},
- {0x8C, "ESMT"}, {0xA1, "Fudan"}, {0xAD, "Hyundai"}, {0xBF, "SST"},
- {0xC2, "Micronix"}, {0xC8, "GigaDevice"}, {0xD5, "ISSI"}, {0xEF, "Winbond"},
- };
- rt_size_t i;
- for (i = 0; i < sizeof(tbl) / sizeof(tbl[0]); i++)
- {
- if (tbl[i].id == mf_id)
- {
- return tbl[i].name;
- }
- }
- return RT_NULL;
- }
- /* 组装 FAL 分区表文本(t14 下发用):每行 "分区名 所属Flash 起址K+大小K",
- * 行间用 TJC 换行转义 "\r"(两个字符)。例:
- * bl onchip_flash 0K+200K / app onchip_flash 200K+312K
- * easyflash MYFLASH16 0K+3072K / fm_area ... / df_area ...
- * 超出 cap 时按整行截断(宁少一行,不给屏幕发半行)。 */
- static void dbg1_build_part_txt(char *out, rt_uint16_t cap)
- {
- size_t part_num = 0;
- const struct fal_partition *tbl = fal_get_partition_table(&part_num);
- rt_uint16_t n = 0;
- size_t i;
- out[0] = '\0';
- if (tbl == RT_NULL || part_num == 0)
- {
- rt_snprintf(out, cap, "%s", "分区表不可用");
- return;
- }
- for (i = 0; i < part_num; i++)
- {
- char line[56];
- int ln = rt_snprintf(line, sizeof(line), "%s %s %luK+%luK",
- tbl[i].name, tbl[i].flash_name,
- (unsigned long)(tbl[i].offset / 1024),
- (unsigned long)(tbl[i].len / 1024));
- if (ln <= 0)
- {
- continue;
- }
- if (ln > (int)sizeof(line) - 1) /* rt_snprintf 返回"应有长度",先夹取再参与判界/拷贝 */
- {
- ln = (int)sizeof(line) - 1;
- }
- if ((rt_uint32_t)n + (rt_uint32_t)ln + 2 + 1 > cap) /* +2 换行 +1 结束符 */
- {
- break;
- }
- if (n > 0)
- {
- out[n++] = '\\';
- out[n++] = 'r';
- }
- rt_memcpy(out + n, line, ln);
- n += (rt_uint16_t)ln;
- out[n] = '\0';
- }
- }
- static void hmi_refresh_debug1(void)
- {
- char buf[48];
- rt_uint32_t total = 0, used = 0, max_used = 0, sec, pct = 0;
- sfud_flash_t nor;
- sys_monitor_refresh(); /* CPU 利用率 / 运行时长(非阻塞,任意线程可用) */
- /* t0 RT-Thread版本 / t1 内核 / t2 BSP型号 */
- rt_snprintf(buf, sizeof(buf), "%d.%d.%d", (int)RT_VERSION, (int)RT_SUBVERSION, (int)RT_REVISION);
- HMI_WRITE("debug1.t0.txt=\"%s\"", buf);
- HMI_WRITE("debug1.t1.txt=\"%s\"", DBG1_CORE_NAME);
- HMI_WRITE("debug1.t2.txt=\"%s\"", DBG1_BSP_NAME);
- /* t13 运行时长:总小时:分:秒(如 72:05:11,小时持续累加、不跨天回绕)。
- * 2026-10-13 修正:① 秒位原为累计秒(屏幕显示形如 2:03:7397),改为 sec%60;
- * ② 时位去掉 %86400 掩码——去掉"天"字段后若保留掩码,24h 处会静默回绕、
- * 长跑设备无法区分"已运行 2 天还是 26 天"。 */
- sec = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
- rt_snprintf(buf, sizeof(buf), "%lu:%02lu:%02lu",
- (unsigned long)(sec / 3600),
- (unsigned long)((sec % 3600) / 60),
- (unsigned long)(sec % 60));
- HMI_WRITE("debug1.t13.txt=\"%s\"", buf);
- /* 版本区:t5 BL版本 / t3 固件版本 / t4 编译时间 */
- HMI_WRITE("debug1.t5.txt=\"%s\"", device_status.driver_bl);
- HMI_WRITE("debug1.t3.txt=\"%s\"", device_status.driver);
- HMI_WRITE("debug1.t4.txt=\"%s\"", device_status.driver_compile);
- /* CPU:t11 文本 + j0 进度条(0-100) */
- HMI_WRITE("debug1.t11.txt=\"%d%%\"", system_status.cpu_usage);
- HMI_WRITE("debug1.j0.val=%d", system_status.cpu_usage);
- /* RAM:t12 文本 + j1 进度条(0-100) */
- rt_memory_info(&total, &used, &max_used);
- if (total > 0)
- {
- pct = (rt_uint32_t)used * 100 / total;
- rt_snprintf(buf, sizeof(buf), "%lu%%", (unsigned long)pct);
- HMI_WRITE("debug1.t12.txt=\"%s\"", buf);
- HMI_WRITE("debug1.j1.val=%d", (int)pct);
- }
- else
- {
- HMI_WRITE("debug1.t12.txt=\"%s\"", "N/A");
- HMI_WRITE("debug1.j1.val=%d", 0);
- }
- /* t10 外挂Flash(SFUD 探测结果)。
- * 2026-10-13 修复:芯片支持 JESD216(SFDP) 时,SFUD 走 SFDP 识别路径并**显式置
- * chip.name = NULL**(sfud.c:279,SFDP 不含型号名;实测本机 16MB 闪存即此路径),
- * 原实现直接 %s 打印会输出 "(NULL)"。现按三级兜底保证内容有效:
- * ① 型号表命中 → "W25Q128 16MB"(原样保留);
- * ② 型号为空 → "Winbond EF4018 16MB"(厂商名 + JEDEC ID + 容量,信息全部实读自芯片);
- * ③ 厂商也未知 → "JEDEC EF4018 16MB"。 */
- nor = rt_sfud_flash_find_by_dev_name(NOR_FLASH_DEV_NAME);
- if (nor != RT_NULL)
- {
- const char *vendor = dbg1_flash_vendor(nor->chip.mf_id);
- unsigned cap_mb = (unsigned)(nor->chip.capacity / (1024 * 1024));
- if (nor->chip.name != RT_NULL && nor->chip.name[0] != '\0')
- {
- rt_snprintf(buf, sizeof(buf), "%s %uMB", nor->chip.name, cap_mb);
- }
- else if (vendor != RT_NULL)
- {
- rt_snprintf(buf, sizeof(buf), "%s %02X%02X%02X %uMB", vendor,
- (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
- (unsigned)nor->chip.capacity_id, cap_mb);
- }
- else
- {
- rt_snprintf(buf, sizeof(buf), "JEDEC %02X%02X%02X %uMB",
- (unsigned)nor->chip.mf_id, (unsigned)nor->chip.type_id,
- (unsigned)nor->chip.capacity_id, cap_mb);
- }
- }
- else
- {
- rt_snprintf(buf, sizeof(buf), "未检测到");
- }
- HMI_WRITE("debug1.t10.txt=\"%s\"", buf);
- /* t14 分区表详情(FAL 分区表:分区名 所属Flash 起址K+大小K,多行)。
- * 内容静态,配合发送去重仅首次(及换页/10s 复位后)实际占用串口带宽。 */
- {
- char part[DBG1_PART_MAX + 2];
- dbg1_build_part_txt(part, sizeof(part));
- HMI_WRITE("debug1." DBG1_PART_OBJ ".txt=\"%s\"", part);
- }
- }
- /* 屏幕版本握手串变化检测。
- * 说明:屏幕心跳里 SwversionV** 可能与 getFullData 一样每秒都发,
- * 若每次都复位重复过滤表会让去重完全失效;因此只在"版本串发生变化"时复位
- * (换型号/换页面工程才会变)。屏幕重启但版本不变的情况由 HMI_DEDUPE_RESYNC_SEC
- * 周期复位兜底(最坏 10s 后恢复全量刷新)。
- * 2026-10-15:版本串变化还用于触发版本类型切换(hmi_switch_version),
- * 见 HMI_Decode 的 SwversionV 分支。 */
- static char s_last_hmi_ver[16] = {0};
- static rt_bool_t hmi_ver_changed(const char *frame)
- {
- char cur[16];
- int i = 0;
- while (frame[i] != '\0' && frame[i] != '\r' && frame[i] != '\n' && i < (int)sizeof(cur) - 1)
- {
- cur[i] = frame[i];
- i++;
- }
- cur[i] = '\0';
- if (rt_strcmp(cur, s_last_hmi_ver) != 0)
- {
- rt_strncpy(s_last_hmi_ver, cur, sizeof(s_last_hmi_ver) - 1);
- s_last_hmi_ver[sizeof(s_last_hmi_ver) - 1] = '\0';
- LOG_I("hmi version frame changed: %s", s_last_hmi_ver);
- return RT_TRUE;
- }
- return RT_FALSE;
- }
- /* ==================== 屏幕版本串 → 版本类型切换(2026-10-15) ====================
- * 触发:HMI_Decode 的 SwversionV 分支中,hmi_ver_changed() 检测到屏幕版本握手串
- * 发生变化时调用一次——即连接/更换不同版本的屏幕工程时,按串切换
- * dtype/version_type/driver 并写 EF(MA_save/param_mirror_sync)。
- * 历史:本逻辑原为 HMI_Decode 内一段 else-if 链(V12/V11/V1/V43/V42/V41/V4/V6/
- * V30/V7),自 SwversionV 整页刷新分支加入后所有版本帧都被其先命中,
- * 原链永远不可达(死代码);现迁移为本函数并改由版本串变化事件触发,
- * 逐分支语义与原链一致(分包防误切仍用 hmi_cmd_match 严格匹配)。 */
- static void hmi_switch_version(const char *frame)
- {
- //高空-带拍照
- if (strstr(frame,"SwversionV12")!=NULL)
- {
- device_status.dtype = 3;
- if(device_status.version_type != 12)
- {
- device_status.version_type = 12;
- sprintf(device_status.driver, "%s",VERSION_GK);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
- MA_save();
- param_mark_dirty();
- }
- else {
- }
- }
- //高空
- else if (strstr(frame,"SwversionV11")!=NULL)
- {
- device_status.dtype = 3;
- if(device_status.version_type != 11)
- {
- device_status.version_type = 11;
- sprintf(device_status.driver, "%s",VERSION_GK);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
- MA_save();
- param_mark_dirty();
- }
- else {
- }
- return;
- }
- //1.0
- /* 注意:必须用严格匹配——"SwversionV1" 是 "SwversionV11/12" 的前缀,
- * 串口分包时残段会误切到本分支(同 V4 的问题,见 hmi_cmd_match 说明) */
- else if (hmi_cmd_match(frame,"SwversionV1"))
- {
- if(device_status.version_type != 1)
- {
- device_status.dtype = 3;
- device_status.version_type = 1;
- sprintf(device_status.driver, "%s",VERSION_ZP);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
- MA_save();
- param_mark_dirty();
- }
- else {
- }
- return;
- }
- //中端款
- else if (strstr(frame,"SwversionV43")!=NULL) {
- device_status.dtype = 3;
- if(device_status.version_type != 43)
- {
- device_status.dtype = 3;
- device_status.version_type = 43;
- sprintf(device_status.driver, "%s",VERSION_HK_2000W);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
- MA_save();
- param_mark_dirty();
- }
- return;
- }
- //低端款
- else if (strstr(frame,"SwversionV42")!=NULL) {
- device_status.dtype = 3;
- if(device_status.version_type != 42)
- {
- device_status.dtype = 3;
- device_status.version_type = 42;
- sprintf(device_status.driver, "%s",VERSION_HK_2000W);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
- MA_save();
- param_mark_dirty();
- }
- return;
- }
- //4.0
- else if (strstr(frame,"SwversionV41")!=NULL) {
- device_status.dtype = 3;
- if(device_status.version_type != 41)
- {
- device_status.dtype = 3;
- device_status.version_type = 41;
- sprintf(device_status.driver, "%s",VERSION_HK);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型+
- DTU_Write("SwversionV41");
- MA_save();
- param_mark_dirty();
- }
- else {
- }
- return;
- }
- //水稻
- /* 注意:必须用严格匹配——"SwversionV4" 是 "SwversionV41/42/43" 的前缀。
- * 实测(2026-10-08):串口分包时 "SwversionV43" 的前段 "SwversionV4" 会误切到
- * 本分支(version_type 43→4、误报 RTU、写 Flash),随后完整帧又切回 43,
- * 造成配置来回翻转。见 hmi_cmd_match 说明。 */
- else if (hmi_cmd_match(frame,"SwversionV4"))
- {
- device_status.dtype = 3;
- if(device_status.version_type != 4)
- {
- device_status.version_type = 4;
- sprintf(device_status.driver, "%s",VERSION_HK_2000W);
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //版本类型
- MA_save();
- param_mark_dirty();
- }
- else {
- }
- return;
- }
- else if((strstr(frame,"SwversionV7")!=NULL)) {
- if(device_status.dtype!=7)
- {
- LOG_D("设置版本类型:7");
- device_status.dtype = 7;
- device_status.version_type = 7;
- ef_set_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype)); //设备类型
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- DTU_Write("SwversionV7");
- MA_save();
- param_mark_dirty();
- }
- }
- }
- static void HMI_Decode(void)
- {
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- static char *json_temp;
- static char *var;
- /* 屏幕页刷新与版本握手统一入口(SwversionV* / getFullData 两类帧):
- * ① SwversionV*:先检测并持久化版本类型(原独立 else-if 分支链并入本块,
- * 保证"先更新版本、再刷新页面",刷新内容即时反映新版本);
- * ② 版本串变化(换型号/换页面工程) → 复位重复过滤表,立即恢复全量刷新;
- * ③ getFullData / SwversionV* 均触发整页刷新:60+ 条属性合并为 1~3 次
- * 串口写(见 HMI_WRITE 发送优化);
- * ④ 处理完直接 return(沿用原版本分支的语义),不再落入下方指令链。
- * 注意:显式排除 "dbg:" 控制台发送帧——b4[发送]会回传整框文本,其中可能内嵌
- * 历史日志里的版本串(如本机口镜像的 hmi rx 行),若不排除会抢在 hmi_debug1_handle
- * 之前吞掉命令帧,导致命令不再执行。 */
- if(strstr(rx_buffer,"SwversionV") != NULL || strstr(rx_buffer,"getFullData") != NULL)
- {
- /* 屏幕版本握手串变化(换型号/换页面工程) → 复位重复过滤表 + 按串切换版本类型,
- * 并立即整页全量刷新 */
- if (strstr(rx_buffer,"SwversionV") != NULL && hmi_ver_changed(rx_buffer))
- {
- hmi_dedupe_reset();
- /* 2026-10-15:版本串变化后按字符设置版本类型(见 hmi_switch_version 说明) */
- hmi_switch_version(rx_buffer);
- }
- /* 整页刷新 60+ 条属性合并为 1~3 次串口写(见 HMI_WRITE 发送优化) */
- HMI_BATCH_BEGIN();
- refresh_hmi_Component(tm);
- hmi_refresh_work_proc(); /* 拍照/清扫状态 + t1~t6 工作过程显示 */
- HMI_BATCH_END();
- return; /* 握手/刷新帧处理完毕, 不再落入下方指令链 */
- }
- /* ---- 屏幕 debug1 调试页(心跳刷新 + 按钮/控制台指令)----
- * 必须放在 if/else 链最前:dbg:<命令> 的内容里可能包含 reboot/clear 等
- * 既有分支的关键字,让它们先匹配会导致误动作。 */
- if (strstr(rx_buffer, "getDebugData") != RT_NULL)
- {
- s_dbg1_page_tick = rt_tick_get(); /* 调试页心跳:离开该页(超时)后暂停日志推送 */
- HMI_BATCH_BEGIN();
- hmi_refresh_debug1();
- dbg_con_flush(RT_FALSE); /* 回页/持续在页:把最新控制台内容补推一次(带去重) */
- HMI_BATCH_END();
- }
- else if (hmi_debug1_handle(rx_buffer))
- {
- /* debug1 指令已处理(dbg:/dbg_on/dbg_off/dbg_clr/dbg_erase/dbg_restore/startapp),
- * 页面心跳已在处理函数内随之刷新。 */
- }
- else if (strstr(rx_buffer,"getClockData")!=NULL) {
- //更新定时模式
- HMI_WRITE("clockSet.timeSwitch.val=%d",device_status.timeSwitch);
- //更新光控定时时长
- HMI_WRITE("clockSet.sunBar.val=%d",device_status.sunBar);
- //更新时控开始
- HMI_WRITE("clockSet.startBar.val=%d",device_status.startBar);
- //更新时控结束
- HMI_WRITE("clockSet.stopBar.val=%d",device_status.stopBar);
- }
- else if (strstr(rx_buffer,"button")!=NULL) {
- LOG_D("button!");
- DTU_Write("button");
- }
- //拍照
- else if(strstr(rx_buffer,"putTest.switchPhoto=1")!=NULL) {
- #if MATCH_DEVICE == 1
- ctr_led(ON);
- //发送拍照指令
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- HMI_WRITE("sysTest.switchPhoto.val=0");
- #else
- device_status.test_flag=1;
- device_status.photo_ws = 1;
- #endif
- }
- else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
- #if MATCH_DEVICE == 1
- ctr_led(ON);
- //发送拍照指令
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- HMI_WRITE("sysTest.switchPhoto.val=0");
- #else
- device_status.photo_ws = 1;
- #endif
- }
- //打开补光
- else if (strstr(rx_buffer,"putTest.switchLed=1")!=NULL) {
- LOG_I("打开补光!!");
- ctr_led(ON);
- }
- //关闭补光
- else if (strstr(rx_buffer,"putTest.switchLed=0")!=NULL) {
- LOG_I("关闭补光!!!");
- ctr_led(OFF);
- }
- //关闭加热
- else if(strstr(rx_buffer,"putTest.switchHeat=0")!=NULL) {
- device_status.heat_status = 0;
- ctr_Heat(OFF);
- hmi_test_heat_stop(); /* 手动关闭:撤销自动关闭定时器 */
- LOG_I("Close Heat!");
- }
- //打开加热
- else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
- device_status.heat_status = 1;
- ctr_Heat(ON);
- hmi_test_heat_start(); /* 屏幕测试加热:10分钟自动关闭(防忘记关干烧) */
- LOG_I("Open Heat!");
- }
- else if (strstr(rx_buffer,"turnzp")!=NULL) {
- LOG_W("转仓!");
- device_status.clear_status=2;
- }
- //清扫
- else if (strstr(rx_buffer,"clear")!=NULL) {
- LOG_W("清扫!");
- device_status.clear_status=1;
- }
- //震动
- else if (strstr(rx_buffer,"putTest.switchShake=1")!=NULL) {
- LOG_I("震动!");
- if(device_status.version_type==6)
- {
- Motor_shake(ON, device_status.shake_sec);
- }
- else{
- ctr_shake(ON,device_status.shake_sec);
- ctr_router(ON);
- }
- }
- //打开补光
- else if (strstr(rx_buffer,"putTest.switchFill=1")!=NULL) {
- rt_kprintf("\n打开补光!!!\n");
- ctr_led(ON);
- }
- //关闭补光
- else if (strstr(rx_buffer,"putTest.switchFill=0")!=NULL) {
- rt_kprintf("\n关闭补光!!!\n");
- ctr_led(OFF);
- }
- //打开灯管
- else if (strstr(rx_buffer,"putTest.switchLamp=1")!=NULL)
- {
- device_status.test_flag=1;
- rt_kprintf("Open Lamp!\n");
- ctr_lamp(ON,1);
- device_status.rain_status = 0;
- }
- //关闭灯管
- else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
- {
- device_status.test_flag=0;
- rt_kprintf("Close Lamp1!\n");
- ctr_lamp(OFF,1);
- }
- //打开上仓门
- else if(strstr(rx_buffer,"putTest.switchUp=1")!=NULL){
- if(device_status.dtype==7)
- {
- ctr_lamp(ON, 1);
- ctr_Motor_Driver(1, 0,1000000,500);
- LOG_I("开始步进上行!");
- }
- else
- {
- rt_kprintf("Open UP!\n");
- ctr_cangmen_up(OFF);
- device_status.ctr_up_motor = 1;
- }
- }
- //关闭上仓门
- else if(strstr(rx_buffer,"putTest.switchUp=0")!=NULL){
- if(device_status.dtype==7)
- {
- LOG_I("停止步进上行!");
- ctr_lamp(OFF, 1);
- ctr_Motor_Driver(0, 0,0,0);
- }
- else
- {
- rt_kprintf("CLOSE UP!\n");
- ctr_cangmen_up(OFF);
- device_status.ctr_up_motor = 2;
- }
- }
- //打开下仓门
- else if(strstr(rx_buffer,"putTest.switchDown=1")!=NULL){
- if(device_status.dtype==7)
- {
- LOG_I("开始步进下行!");
- ctr_lamp(ON, 1);
- ctr_Motor_Driver(1, 1,1000000,500);
- }
- else
- {
- ctr_cangmen_down(OFF);
- device_status.ctr_down_motor = 1;
- rt_kprintf("Open down!\n");
- }
- }
- //关闭下仓门
- else if(strstr(rx_buffer,"putTest.switchDown=0")!=NULL){
- if(device_status.dtype==7)
- {
- ctr_lamp(OFF, 1);
- ctr_Motor_Driver(0, 0,0,0);
- rt_kprintf("停止步进下行!\n");
- }
- else
- {
- ctr_cangmen_down(OFF);
- device_status.ctr_down_motor = 2;
- rt_kprintf("CLOSE down!\n");
- }
- }
- else if(strstr(rx_buffer,"putTest.Testpaichong")!=NULL){
- if(device_status.collt_box_sta==0){
- device_status.collt_box_sta = 111;
- }else{
- HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
- }
- LOG_I("排虫测试!");
- }
- //排虫
- else if(strstr(rx_buffer,"putTest.paichong")!=NULL){
- if(device_status.run_sta == 0){
- device_status.run_sta = 111;
- }else{
- HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
- }
- LOG_I("开始排虫!");
- device_status.handle_bugger = 0;
- ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
- }
- else if(strstr(rx_buffer,"putTest.shouchong")!=NULL){
- if(device_status.run_sta == 0){
- device_status.run_sta = 112;
- }else{
- HMI_WRITE("sysTest.result.txt=\"请等待当前执行完成再进行操作\"");
- }
- LOG_I("开始收虫!");
- device_status.handle_bugger = 1;
- ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
- }
- //设置设备时间
- else if (strstr(rx_buffer,"stamp=")!=NULL)
- {
- var = strstr(rx_buffer,"=");
- var ++;
- LOG_I("设置时间:%s",var);
- static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
- strncpy(year_s, var, 4);
- strncpy(month_s,var+4,2);
- strncpy(day_s,var+6,2);
- strncpy(hour_s,var+8,2);
- strncpy(min_s,var+10,2);
- strncpy(sec_s,var+12,2);
- 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)
- {
- if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
- {
- LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
- /* ★ 原来是 >0: 合法的 00:xx:xx 永远设不进去(只改了日期)。且 24 点非法。 */
- 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)
- {
- LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
- set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
- MA_save();
- }
- //上传最新数据
- dtu_request_report(); //立即请求一次上报(上报后按 dattim 自动重排定时器), 更新RTU的时间
- }
- else {
- LOG_E("set_date failed!");
- }
- }
- }
- //设置闹钟时间
- else if (strstr(rx_buffer,"ALARM=")!=NULL)
- {
- char *var = strstr(rx_buffer,"=");
- if (var == NULL) {
- return;
- }
- var ++;
- // ===================== 修复:去掉末尾的 \r 或 换行 =====================
- // 遍历字符串,把 \r \n 全部替换成结束符
- for (int i = 0; var[i] != '\0'; i++) {
- if (var[i] == '\r' || var[i] == '\n') {
- var[i] = '\0';
- break;
- }
- }
- /* ★ 原来无条件写 var[6]: 载荷短于 6 位时读写越过字符串结尾,
- 帧被截断时 var+6 甚至越过 rx_buffer 末尾。先判长度再截断。 */
- if(strlen(var) > 6) {
- var[6] = '\0';
- }
- // 现在 var 只剩下干净的 6 位数字了
- int len = strlen(var);
- int is_valid = 1;
- // 1. 必须是 6 个字符(已经去掉\r)
- if (len != 6) {
- is_valid = 0;
- }
- // 2. 必须全部是数字 0-9
- else {
- for (int i = 0; i < 6; i++) {
- if (!(var[i] >= '0' && var[i] <= '9')) {
- is_valid = 0;
- break;
- }
- }
- }
- // 校验不通过:打印报错,不赋值
- if (!is_valid) {
- LOG_E("闹钟格式错误!必须是 6 位数字时间(如 150000),当前值:%s", var);
- return;
- }
- // ======================================================================
- // 格式正确才赋值
- LOG_I("设置闹钟时间:%s",var);
- rt_sprintf(device_status.alarm_tg, "%s", var);
- ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
- }
- //设备编码设置
- else if (strstr(rx_buffer,"deviceId=")!=NULL)
- {
- char value_str[20];
- memset(value_str,0,sizeof(value_str));
- LOG_I("设备编码 长度:%d",strlen(rx_buffer));
- if(strlen(rx_buffer)!=19){
- LOG_E("编码长度错误!");
- return;
- }
- var = strstr(rx_buffer,"=");
- var ++;
- strncpy(value_str,var,8); //设备编码为16位
- if(strstr(value_str,"00000000")!=RT_NULL){
- char *config_reset = makeJson_Config_Reset();
- //发送json到RTU
- DTU_Write("%s",config_reset);
- rt_free(config_reset);
- //恢复RTU的默认配置参数
- }else{
- rt_sprintf(device_status.deviceId,"10300101%s",value_str); //设备大类编码103-虫情 设备小类编码 001 生产商代码01
- ef_set_env_blob("deviceId", &device_status.deviceId, sizeof(device_status.deviceId));
- LOG_I("解析到设备编码:%s",device_status.deviceId);
- char *device_id = makeJson_Config_BDH(device_status.deviceId);
- //发送json到RTU
- DTU_Write("%s",device_id);
- rt_free(device_id);
- }
- }
- //定时设置
- else if (strstr(rx_buffer,"saveClockSet=")!=NULL) {
- char value_str[10];
- memset(value_str,0,sizeof(value_str));
- if(device_status.dtype==7)
- {
- var = strstr(rx_buffer,"=");
- var ++;
- strncpy(value_str,var,2);
- if(atoi(value_str)>=0&&atoi(value_str)<=23)
- {
- device_status.collecting_sh1 = atoi(value_str);
- ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1));
- }
- else {
- LOG_E("Set collecting_sh1 Failed!");
- }
- memset(value_str,0,sizeof(value_str));
- strncpy(value_str,var+2,2);
- if(atoi(value_str)>=0&&atoi(value_str)<=23)
- {
- device_status.collecting_eh1 = atoi(value_str);
- ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1));
- }
- else {
- LOG_E("Set collecting_eh1 Failed!");
- }
- memset(value_str,0,sizeof(value_str));
- strncpy(value_str,var+4,2);
- if(atoi(value_str)>=0&&atoi(value_str)<=23)
- {
- device_status.collecting_sh2 = atoi(value_str);
- ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2));
- }
- else {
- LOG_E("Set collecting_sh2 Failed!");
- }
- memset(value_str,0,sizeof(value_str));
- strncpy(value_str,var+6,2);
- if(atoi(value_str)>=0&&atoi(value_str)<=23)
- {
- device_status.collecting_eh2 = atoi(value_str);
- ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2));
- }
- else {
- LOG_E("Set collecting_eh2 Failed!");
- }
- memset(value_str,0,sizeof(value_str));
- strncpy(value_str,var+8,2);
- if(atoi(value_str)>=0&&atoi(value_str)<=23)
- {
- device_status.shake_sec = atoi(value_str);
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
- }
- else {
- LOG_E("Set shake_sec Failed!");
- }
- memset(value_str,0,sizeof(value_str));
- strncpy(value_str,var+10,2);
- if(atoi(value_str)>0&&atoi(value_str)<=24)
- {
- device_status.cul_time = atoi(value_str);
- ef_set_env_blob("cul_time", &device_status.cul_time, sizeof(device_status.cul_time));
- }
- else {
- LOG_E("Set cul_time Failed!");
- }
- MA_save();
- 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);
- return;
- }
- LOG_I("时间设置!");
- var=strstr(rx_buffer,"=");
- var++;
- strncpy(value_str,var,2);
- if(atoi(value_str) > 0 && atoi(value_str) <60)
- {
- if(device_status.version_type!=6)
- {
- device_status.sunBar=atoi(value_str);
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
- }
- else {
- device_status.shake_sec = atoi(value_str);
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec));
- }
- }
- else {
- LOG_E("参数设置异常(光控时长):%d!!!",atoi(value_str));
- }
- memset(value_str,0,sizeof(value_str));
- //时控开始时间
- strncpy(value_str,var+2,2);
- if(atoi(value_str) >= 0 && atoi(value_str) < 24)
- {
- #if 1
- device_status.startBar=atoi(value_str);
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
- #endif
- }
- else {
- LOG_E("参数设置异常(开始):%d!!!",atoi(value_str));
- }
- memset(value_str,0,sizeof(value_str));
- //
- strncpy(value_str,var+4,2);
- if(atoi(value_str) >= 0&& atoi(value_str) < 24)
- {
- #if 1
- device_status.stopBar=atoi(value_str);
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
- #endif
- }
- else {
- LOG_E("参数设置异常(结束):%d!!!",atoi(value_str));
- }
- memset(value_str,0,sizeof(value_str));
- //
- strncpy(value_str,var+6,2);
- if(atoi(value_str) >= 0 && atoi(value_str) <= 1)
- {
- device_status.timeSwitch=atoi(value_str);
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
- }
- else {
- if(device_status.version_type==6)
- {
- device_status.timeSwitch=1;
- device_status.dattim=atoi(value_str);
- ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
- }
- else {
- LOG_E("参数设置异常(模式):%d!!!",atoi(value_str));
- }
- }
- //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_clock)
- {
- char json_warn[192];
- rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp);
- json_temp = makeJson_Warn(json_warn, "修改工作时间");
- ef_set_env_blob("json_w_clock", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
- if(atoi(device_status.net)==0)
- DTU_Write("%s",json_temp);
- rt_free(json_temp);
- }
- rt_thread_delay(2000);
- MA_save();
- rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
- }
- else if (strstr(rx_buffer,"saveSystemSet")!=NULL) {
- static char clt_time_str[3],heattime_str[3],heatvale_str[4],vol_str[4],thsp_str[5];
- static char value_temp[5];
- LOG_I("系统设置!");
- if(device_status.dtype==7)
- {
- LOG_I("系统设置,孢子仪!");
- var = strstr(rx_buffer,"=");
- var ++;
- memset(value_temp,0,sizeof(value_temp));
- strncpy(value_temp,var,2);
- if(atoi(value_temp)==0 || atoi(value_temp) == 1)
- {
- device_status.cold_sw = atoi(value_temp); //制冷开关
- ef_set_env_blob("cold_sw", &device_status.cold_sw, sizeof(device_status.cold_sw));
- }
- else {
- LOG_E("制冷开关参数设置错误!");
- }
- memset(value_temp,0,sizeof(value_temp));
- strncpy(value_temp,var+2,2);
- if(atoi(value_temp)>=0 && atoi(value_temp) <= 7)
- {
- device_status.vol = atoi(value_temp); //音量
- }
- else {
- LOG_E("音量参数设置错误!");
- }
- memset(value_temp,0,sizeof(value_temp));
- strncpy(value_temp,var+4,2);
- if(atoi(value_temp)>=10 && atoi(value_temp) <= 50)
- {
- device_status.heatvalue = atoi(value_temp); //设定的培养温度
- ef_set_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue));
- }
- else {
- LOG_E("培养温度参数设置失败!");
- }
- memset(value_temp,0,sizeof(value_temp));
- strncpy(value_temp,var+6,2);
- if(atoi(value_temp)>=1 && atoi(value_temp) <= 60)
- {
- device_status.dattim = atoi(value_temp); //设定的数据上传间隔,单位:分钟
- ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
- }
- else {
- LOG_E("数据上传间隔参数设置失败!");
- }
- memset(value_temp,0,sizeof(value_temp));
- strncpy(value_temp,var+8,2);
- if(atoi(value_temp)>=1 && atoi(value_temp) <= 30)
- {
- device_status.thsp = atoi(value_temp); //设定的息屏间隔,单位:分钟
- ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
- }
- else {
- LOG_E("数据息屏间隔参数设置失败!");
- }
- LOG_I("制冷开关:%d",device_status.cold_sw);
- LOG_I("系统设置已保存:\n自动熄屏-%d\n音量-%d\n培养温度-%d\n数据上传间隔-%d",device_status.thsp,device_status.vol,device_status.heatvalue,device_status.dattim);
- MA_save();
- return;
- }
- var = strstr(rx_buffer,"=");
- var ++;
- strncpy(clt_time_str,var,2);
- if(atoi(clt_time_str)>0 && atoi(clt_time_str) <= 30)
- {
- device_status.clt_time = atoi(clt_time_str); //收集时间
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
- }
- strncpy(heattime_str,var+2,2);
- if(atoi(heattime_str)>0 && atoi(heattime_str) <= 30)
- {
- device_status.heattime = atoi(heattime_str); //加热时间
- LOG_I("设置加热时间:%d",atoi(heattime_str));
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
- }
- strncpy(heatvale_str,var+4,3);
- if(atoi(heatvale_str) > 0 && atoi(heatvale_str) <= 130)
- {
- device_status.heatvalue=atoi(heatvale_str);
- LOG_I("设置加热温度:%d",device_status.heatvalue);
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- }
- strncpy(vol_str,var+7,3);
- if(atoi(vol_str)>0&&atoi(vol_str)<8)
- {
- device_status.vol = atoi(vol_str); //音量
- LOG_I("设置音量:%d",device_status.vol);
- //DTU_Write("volume=");
- }
- strncpy(thsp_str,var+10,4);
- if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
- {
- device_status.thsp = atoi(thsp_str); //熄屏时间
- LOG_I("设置熄屏时间:%d",device_status.thsp);
- }
- else {
- LOG_E("参数设置失败!!!");
- }
- rt_thread_delay(1000);
- MA_save();
- //rt_kprintf("系统设置已保存:收集时间-%d,加热时间-%d,加热温度-%d,音量-%d,自动关屏-%d\n",device_status.clt_time,device_status.heattime,device_status.heatvalue,device_status.vol,device_status.thsp);
- }
- else if (strstr(rx_buffer,"rtc_h=")!=NULL) {
- /* ★ 原来是 char rtc_h[2] + strncpy(rtc_h,var,2): 拷贝满 2 字节不补 '\0',
- atoi() 会越界读栈; 而且小时上限写成 24(非法)。直接解析并做范围校验。 */
- int v;
- var = strstr(rx_buffer,"=");
- var++;
- v = atoi(var);
- if(v>=0 && v<=23)
- {
- set_time(v, 99, 99);
- }
- }
- else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
- int v;
- var = strstr(rx_buffer,"=");
- var++;
- v = atoi(var);
- if(v>=0 && v<=59)
- {
- set_time(99, v, 99);
- }
- }
- else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
- int v;
- var = strstr(rx_buffer,"=");
- var++;
- v = atoi(var);
- if(v>=0 && v<=59)
- {
- set_time(99, 99, v);
- }
- }
- else if(strstr(rx_buffer,"saveUploadSet=")!=NULL)
- {
- static char dattim_str[3];
- rt_kprintf("上传间隔设置!\n");
- var = strstr(rx_buffer,"=");
- var ++;
- strncpy(dattim_str,var,2);
- if(atoi(dattim_str)>=1&&atoi(dattim_str)<=120)
- {
- device_status.dattim =atoi(dattim_str); //数据上传间隔
- ef_set_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim));
- MA_save();
- }
- else {
- rt_kprintf("设置失败!!!\n");
- }
- rt_kprintf("数据上传设置:%d\n",device_status.dattim);
- }
- //光控测试
- else if(strstr(rx_buffer,"putTest.switchSun=1")!=NULL)
- {
- rt_kprintf("光控测试!!!");
- ctr_lamp(ON,1);
- device_status.sun_test = 1;
- }
- else if (strstr(rx_buffer,"setshake_sec=")!=NULL)
- {
- static char shakesec_str[4];
- LOG_I("震动时间设置!");
- var = strstr(rx_buffer,"=");
- var ++;
- strncpy(shakesec_str,var,2);
- if(atoi(shakesec_str)>=0&&atoi(shakesec_str)<=10)
- {
- device_status.shake_sec =atoi(shakesec_str); //2026-0608:取消*10
- }
- else {
- LOG_E("设置失败!!!");
- }
- LOG_D("震动时间设置:%d",device_status.shake_sec);
- }
- //恢复出厂设置电池
- else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
- {
- ResetParamDefault_DC();
- rt_thread_delay(1000);
- //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_reset)
- {
- char json_warn[192];
- rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:系统重置:电池供电,操作时间:%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);
- json_temp = makeJson_Warn(json_warn, "系统重置");
- ef_set_env_blob("json_w_reset", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
- if(atoi(device_status.net)==0)
- DTU_Write("%s",json_temp);
- rt_free(json_temp);
- }
- }
- //恢复出厂设置-交流
- else if (strstr(rx_buffer,"resetDefault")!=NULL)
- {
- ResetParamDefault();
- rt_thread_delay(1000);
- //2022/06/23,add: 操作记录改为局部缓冲(原 device_status.json_w_reset)
- {
- char json_warn[192];
- rt_snprintf(json_warn, sizeof(json_warn),"屏幕操作:系统重置:交流供电,操作时间:%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);
- json_temp = makeJson_Warn(json_warn, "系统重置");
- ef_set_env_blob("json_w_reset", json_warn, strlen(json_warn) + 1); //将修改屏幕参数操作记录保存到Flash中
- if(atoi(device_status.net)==0)
- DTU_Write("%s",json_temp);
- rt_free(json_temp);
- }
- }
- //获取SIM卡信息
- else if(strstr(rx_buffer,"getSim")!=NULL)
- {
- //http://sim.openluat.com/api/wechat/openapidoc
- DTU_Write("getsiminfo");
- }
- else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
- Debug_log_ON();
- }
- else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
- Debug_log_OFF();
- }
- /* Swversion 版本类型检测已并入本函数开头"页刷新/版本握手"统一入口块(先检测后刷新),原分支链移除 */
- else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
- {
- HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.power);
- HMI_WRITE("sysTest.heat_txt.txt=\"%s\"",device_status.heat_power);
- hmi_write_num_or_blank("sysTest.t1.txt=\"%s\"", device_status.work_heat_left, RT_FALSE); /* 剩余加热时间(s) */
- hmi_write_num_or_blank("sysTest.t3.txt=\"%s\"", device_status.work_door_left, RT_FALSE); /* 仓门开/关倒计时(s) */
- hmi_write_num_or_blank("sysTest.t0.txt=\"%s\"", device_status.photo_left, RT_FALSE); /* 拍照结束剩余(s) */
- hmi_write_num_or_blank("sysTest.t5.txt=\"%s\"", device_status.clear_left, RT_FALSE); /* 清扫结束剩余(s) */
- // HMI_WRITE("sysTest.switchHeat.val=%d",device_status.heat_status);
- }
- //立即上传
- else if(strstr(rx_buffer,"uploaddata")!=RT_NULL)
- {
- UpLoad_Data();
- }
- else if (strstr(rx_buffer,"checkupdate")!=NULL)
- {
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- 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);
- HMI_WRITE("hardcheck.p0.pic=%d",70);
- //RTC时间判断,如果当前年份小于2020,提示需要设置时间
- if(tm.tm_year+1900<2020)
- {
- 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);
- 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);
- }
- }
- //进入bootloader模式
- else if (strstr(rx_buffer,"bootloader")!=NULL) {
- device_status.stop_in_bootloader = 1;
- ef_set_env_blob("stop_in_bootloader", &device_status.stop_in_bootloader, sizeof(device_status.stop_in_bootloader));
- rt_hw_cpu_reset();
- }
- /* ★ "dtu_reboot" 里包含 "reboot", 原来 reboot 分支在前 -> 屏幕下发 dtu_reboot
- 会走整机复位, RTU 重启指令永远发不出去。dtu_reboot 必须先判。 */
- else if (strstr(rx_buffer,"dtu_reboot")!=NULL) {
- DTU_Write("{\"cmd\": \"dtu_reboot\"}");
- }
- //软件重启
- else if (strstr(rx_buffer,"reboot")!=NULL) {
- rt_hw_cpu_reset();
- }
- else if (strstr(rx_buffer,"saveLog_usb")!=NULL) {
- DTU_Write("{\"cmd\": \"saveLog_usb\"}");
- }
- //屏幕上触发远程升级
- else if (strstr(rx_buffer,"hmi_update")!=NULL) {
- }else if (strstr(rx_buffer,"SaveRecordParam")!=NULL){
- //"SaveRecordParam:10000-200"
- int param1, param2;
- // 从冒号后开始扫描
- if (sscanf(rx_buffer + strlen("SaveRecordParam:"), "%d-%d", ¶m1, ¶m2) == 2) {
- /* ★ 原 itoa(param1, temp_val_pic, 10) 写 char[10]: 10 位十进制(或负值 11 字节)
- 直接踩栈, itoa 不做容量检查。负数是非法参数, 直接丢弃。 */
- char temp_val_mqtt[12],temp_val_pic[12];
- if (param1 < 0 || param2 < 0) {
- LOG_E("Invalid record param:%d-%d", param1, param2);
- return;
- }
- rt_snprintf(temp_val_pic, sizeof(temp_val_pic), "%d", param1);
- rt_snprintf(temp_val_mqtt, sizeof(temp_val_mqtt), "%d", param2);
- /* ★ 原来 DTU_Write(write_record_param) 把 JSON 当格式串(内容含 '-' 等无所谓,
- 但一旦含 '%' 就会被 vsnprintf 当格式说明符) -> 统一走 dtu_write_json */
- dtu_write_json(makeJson_record_write(temp_val_mqtt,temp_val_pic));
- } else {
- LOG_E("Parse failed.\n");
- }
- }
- else if (strstr(rx_buffer,"ReadRecordParam")!=NULL){
- LOG_I("读离线存储参数");
- dtu_write_json(makeJson_record_read("data"));
- }
- else if (strstr(rx_buffer,"checkcamera")!=NULL){
- DTU_Write("{\"ctr_cam\":\"checkcamera\"}");
- //校准屏幕的RTC
- HMI_WRITE("rtc0=%d",tm.tm_year+1900);
- HMI_WRITE("rtc1=%d",tm.tm_mon+1);
- HMI_WRITE("rtc2=%d",tm.tm_mday);
- HMI_WRITE("rtc3=%d",tm.tm_hour);
- HMI_WRITE("rtc4=%d",tm.tm_min);
- HMI_WRITE("rtc5=%d",tm.tm_sec);
- //开启超时定时器
- Start_timer_Uart2();
- }else if (strstr(rx_buffer,"ReadCamParam")!=NULL){
- DTU_Write("{\"ctr_cam\":\"ReadParam\"}");
- //开启超时定时器
- Start_timer_Uart2();
- }//SaveCamParam:2070.00000-0.7-144-30-0-0-5472*5472
- else if (strstr(rx_buffer,"SaveCamParam")!=NULL){
- const char *prefix ="SaveCamParam:";
- size_t prefix_len = strlen(prefix);
- const char *start_pos = strstr(rx_buffer, prefix);
- if(start_pos!=NULL)
- {
- // 移动到前缀之后的位置
- start_pos += prefix_len;
- /* ★ makeJson_Cam 的返回值原来没人释放 -> 每次点"保存相机参数"漏一块堆。
- (start_pos 是 const char*, 与 makeJson_Cam(char*,char*) 的形参有 const 冲突,
- 这里做一次显式转换消除 -Wdiscarded-qualifiers) */
- dtu_write_json(makeJson_Cam("SaveParam", (char *)start_pos));
- }
- //开启超时定时器
- Start_timer_Uart2();
- }else if (strstr(rx_buffer,"GrabOn")!=NULL){
- DTU_Write("{\"ctr_cam\":\"GrabOn\"}");
- //开启超时定时器
- Start_timer_Uart2();
- }else if (strstr(rx_buffer,"GrabOff")!=NULL){
- DTU_Write("{\"ctr_cam\":\"GrabOff\"}");
- //开启超时定时器
- Start_timer_Uart2();
- }
- else if(strstr(rx_buffer,"SelfCheck")!=NULL)
- {
- //校准屏幕的RTC
- HMI_WRITE("rtc0=%d",tm.tm_year+1900);
- HMI_WRITE("rtc1=%d",tm.tm_mon+1);
- HMI_WRITE("rtc2=%d",tm.tm_mday);
- HMI_WRITE("rtc3=%d",tm.tm_hour);
- HMI_WRITE("rtc4=%d",tm.tm_min);
- HMI_WRITE("rtc5=%d",tm.tm_sec);
- if(device_status.selfcheck_ws==2||device_status.selfcheck_ws==3)
- {
- LOG_I("自检进行中!");
- }else {
- device_status.selfcheck_ws = 2;
- LOG_W("开始自检:%d",device_status.selfcheck_ws);
- }
- }else if(strstr(rx_buffer,"SwitchMode")!=NULL)
- {
- device_status.work_mode = !device_status.work_mode;
- ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
- LOG_W("修改工作模式:%d",device_status.work_mode);
- }else if(strstr(rx_buffer,"DeleteUimage")!=NULL){
- /* ★ 原来 DTU_Write(temp) 把 JSON 当格式串; 统一走 dtu_write_json("%s" + 判空 + 自动释放) */
- dtu_write_json(makeJsonRpc2_0("upan_delete_image", NULL, 1));
- }else if(strstr(rx_buffer,"MOVE_IMAGE")!=NULL){
- dtu_write_json(makeJsonRpc2_0("MOVE_IMAGE", NULL, 1));
- }else if(strstr(rx_buffer,"COPY_IMAGE")!=NULL){
- cJSON *params = cJSON_CreateObject(); // params 子对象
- if (params == RT_NULL) {
- LOG_E("COPY_IMAGE: create params failed (no memory)");
- } else {
- cJSON_AddNumberToObject(params, "st", device_status.startBar); // st:20
- cJSON_AddNumberToObject(params, "et", device_status.stopBar); // et:7
- dtu_write_json(makeJsonRpc2_0("COPY_IMAGE", params, 1));
- }
- }else if(strstr(rx_buffer,"UMOUNT_UPAN")!=NULL){
- dtu_write_json(makeJsonRpc2_0("UMOUNT_UPAN", NULL, 1));
- }
- /* 一屏指令处理完, 若有参数变更则由落盘线程合并写入备份分区 */
- param_mark_dirty();
- }
- //入口函数-电机控制及
- static void Ctr_Mortor_entry(void* parameter)
- {
- rt_uint16_t num_up_open = 200; //20s 上仓门打开时间
- rt_uint16_t num_up_close = 200; // 上仓门关闭时间
- rt_uint16_t num_down_open = 200; // 下仓门打开时间
- rt_uint16_t num_down_close = 200; // 下仓门关闭时间
- rt_int16_t photo_led_sec = 900; //55S
- static rt_uint16_t mock_uart2_timer_cnt = 0;
- static rt_uint16_t route_off_t = 0;
- device_status.route_off_t = 0;
- static uint8_t is_rain = 0;
- static uint8_t isnot_rain = 0;
- rt_pin_mode(CTR_UPC_LIMIT, PIN_MODE_INPUT);
- while(1)
- {
- #if 0
- time_t time_reset12V;
- time(&time_reset12V);
- static struct tm alarm_reset12V;
- alarm_reset12V =*localtime(&time_reset12V);
- if((alarm_reset12V.tm_hour)==8&&(alarm_reset12V.tm_min==18)&&(alarm_reset12V.tm_sec==00))
- {
- char *rep = makeJsonRpc2_0("reboot",NULL, 1);
- /* ★ 第四批: DTU_Write 是变参函数, 原来直接传 rep 等于"拿 JSON 当格式串"。
- 统一走 dtu_write_json(固定 "%s" + 判空 + 自动释放)。 */
- dtu_write_json(rep);
- rt_thread_delay(1000);
- }
- if(device_status.mock_uart2_timer_flag==1){
- mock_uart2_timer_cnt++;
- if(mock_uart2_timer_cnt>=200){
- mock_uart2_timer_cnt = 0;
- device_status.mock_uart2_timer_flag = 0;
- }
- }
- #endif
- if(device_status.ctr_up_motor==1)
- {
- ctr_cangmen_up(OPEN);
- num_up_open--;
- if (num_up_open == 0)
- {
- #if JINGJIKUAN == 0
- ctr_cangmen_up(OFF);
- #else
- device_status.ctr_up_motor = 0;
- #endif
- num_up_open = 200;
- system_status.upper_door_open_count +=1;
- }
- num_up_close = 200;
- }
- else if(device_status.ctr_up_motor==2)
- {
- ctr_cangmen_up(CLOSE);
- num_up_close --;
- if (num_up_close == 0)
- {
- #if JINGJIKUAN == 0
- ctr_cangmen_up(OFF);
- #else
- device_status.ctr_up_motor = 0;
- #endif
- num_up_close= 200;
- system_status.upper_door_close_count +=1;
- }
- num_up_open = 200;
- }
- if (device_status.ctr_down_motor == 1)
- {
- ctr_cangmen_down(OPEN);
- num_down_open--;
- #if TG_MODULE ==1
- if(rt_pin_read(CTR_UPC_LIMIT)==1){
- ctr_cangmen_down(OFF);
- Beep_Tip(2000);
- ctr_cangmen_down(OPEN);
- Beep_Tip(2000);
- num_down_open = 200;
- device_status.ctr_down_motor = 1;
- }
- #endif
- if (num_down_open == 0) //2025-07-07,增加限位检测
- {
- #if JINGJIKUAN == 0
- ctr_cangmen_down(OFF);
- #else
- device_status.ctr_down_motor = 0;
- #endif
- num_down_open = 200;
- system_status.lower_door_open_count +=1;
- }
- num_down_close = 200;
- }
- else if (device_status.ctr_down_motor == 2)
- {
- ctr_cangmen_down(CLOSE);
- num_down_close--;
- if (num_down_close == 0)
- {
- #if JINGJIKUAN == 0
- ctr_cangmen_down(OFF);
- #else
- device_status.ctr_down_motor = 0;
- #endif
- num_down_close = 200;
- system_status.lower_door_close_count += 1;
- }
- num_down_open = 200;
- }
- /* 屏幕 workSta.t3:仓门开(关)动作倒计时(s);无动作时清零 */
- if(device_status.ctr_up_motor == 1) device_status.work_door_left = (uint16_t)(num_up_open / 10);
- else if(device_status.ctr_up_motor == 2) device_status.work_door_left = (uint16_t)(num_up_close / 10);
- else if(device_status.ctr_down_motor == 1) device_status.work_door_left = (uint16_t)(num_down_open / 10);
- else if(device_status.ctr_down_motor == 2) device_status.work_door_left = (uint16_t)(num_down_close / 10);
- else device_status.work_door_left = 0;
- if(device_status.sun_test==1)
- {
- SunTest();
- }
- //拍照
- if(device_status.photo_ws==1)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- device_status.photo_stamp = tv.tv_sec-8*3600;
- if(photo_led_sec==900)
- ctr_led(ON);
- //打开补光灯
- if(photo_led_sec==800)
- {
- system_status.photo_taken_count+=1;
- LOG_I("测试拍照!");
- #if 1
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- #endif
- rt_thread_delay(5000); //延时
- }
- //15S后执行拍照
- else if (photo_led_sec==750)
- {
- //DTU_Write("takephoto.mp3");
- //延时
- rt_thread_delay(1000);
- LOG_I("测试拍照!");
- Hk_Take_io();
- }
- photo_led_sec--;
- /* 屏幕 workSta.t5:拍照结束剩余(s)。photo_led_sec 900->300 段倒计时 60s->0 */
- device_status.photo_left = (photo_led_sec > 300) ? (uint16_t)((photo_led_sec - 300) / 10) : 0;
- if(photo_led_sec==300) //
- {
- ctr_led(OFF);
- device_status.photo_ws = 0;
- photo_led_sec = 900;
- device_status.photo_left = 0;
- if(device_status.test_flag==0){
- device_status.clear_status = 1;
- }else{
- device_status.test_flag = 0;
- }
- HMI_WRITE("sysTest.switchPhoto.val=0");
- HMI_WRITE("sysTest.switchFill.val=0");
- }
- }else{
- photo_led_sec = 900;
- device_status.photo_left = 0;
- }
- if(device_status.selfcheck_ws==2)
- {
- selfcheck(1);
- }else if (device_status.selfcheck_ws==3) {
- selfcheck(0);
- }
- if(device_status.route_off_t!=0)
- {
- ctr_router(OFF);
- route_off_t = device_status.route_off_t * 60 * 10;
- device_status.route_off_t = 0;
- }
- if(route_off_t>0)
- {
- route_off_t--;
- if(route_off_t==1)
- {
- ctr_router(ON);
- route_off_t = 0;
- }
- }
- #if JINGJIKUAN == 1
- if(device_status.rain_status==1){
- isnot_rain = 0;
- if(is_rain==0){
- ctr_Collect_Fan(OFF); //触发雨控
- rt_thread_delay(2000);
- device_status.ctr_up_motor = 2; //关闭上仓门
- is_rain = 1;
- }
- }else{
- is_rain = 0;
- if(isnot_rain == 0){
- ctr_Collect_Fan(ON); //解除雨控
- isnot_rain = 1;
- }
- }
- #endif
- /* 屏幕刷新重复过滤表:到点(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位一次,
- 保证屏幕重启/换页后最迟该周期内恢复全量刷新(详见 HMI_WRITE 处说明) */
- hmi_dedupe_tick();
- rt_thread_delay(100);
- }
- }
- /*屏幕写数据*/
- int Hmi_Fresh(void)
- {
- //初始化串口4
- //HMI_uart4_init();
- rt_err_t ret = RT_EOK;
- /* 初始化消息队列 */
- rt_mq_init(&rx_mq, "rx_mq",
- msg_pool, /* 存放消息的缓冲区 */
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- /* 设置接收回调函数(★ 原来未判空: 屏幕串口设备名配错/未注册时
- rt_device_find() 返回 NULL, 这里直接解引用空设备句柄) */
- if (serial != RT_NULL)
- {
- rt_device_set_rx_indicate(serial, uart_input);
- }
- /* 创建线程*/
- hmi1 = rt_thread_create("Hmi_Fresh_thread",
- Hmi_thread_entry,
- RT_NULL,
- HMI_THREAD_STACK_SIZE,
- HMI_THREAD_PRIORITY,
- HMI_THREAD_TIMESLICE);
- /*部分控制的线程*/
- ctr_motor = rt_thread_create("Ctr motor",
- Ctr_Mortor_entry,
- RT_NULL,
- CONTROL_MOTOR_STACK_SIZE,
- CONTROL_MOTOR_PRIORITY,
- CONTROL_MOTOR_TIMESLICE);
- if(ctr_motor != RT_NULL)
- {
- rt_thread_startup(ctr_motor);
- }
- /* 如果获得线程控制块,启动这个线程 */
- if (hmi1 != RT_NULL)
- {
- rt_thread_startup(hmi1);
- }
- else
- {
- ret = RT_ERROR;
- }
- return ret;
- }
- void Check_ParamConfig(void)
- {
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- /* 双分区参数备份: 上电先把 param_a / param_b 里有效的那份读出来备用。
- ★ 必须是 static: param_persist_t 有 820 字节,而 main 线程栈只有 2048,
- 这个函数里还有第二个(param 自检用的 cur),两个放栈上再加调用栈必爆。 */
- static param_persist_t param_boot_snap;
- uint32_t param_boot_seq = 0;
- int param_boot_snap_ok = 0;
- int ef_ok = 0; /* EasyFlash 是否可用 */
- int up_flag_ef = 0; /* 通道①: EasyFlash 里的升级请求 */
- int up_flag_snap = 0; /* 通道②: 参数快照里的升级请求 */
- int up_flag_bkp = 0; /* 通道③: RTC 备份寄存器里的升级请求 */
- int up_request = 0; /* 三通道裁决结果 */
- /* ★ flash 健康检查门控(2026-09-24 加固)
- SPI flash 探测失败(虚焊/击穿/SPI 不通)时, 原链路会一路"假装正常":
- fal 建出合法分区 -> easyflash_init 返回成功 -> 第一次读就 sfud_read(NULL,...)
- 空指针解引用 -> hard fault, 而日志停在 ENV 打印之后, 跟栈溢出极难区分。
- 这里在开机最前面显式判 flash 可用性: 不 ready 就完全不碰 EasyFlash,
- 用 RAM 默认值降级启动, 设备照样能干活、能上报故障, 而不是变砖。 */
- if(!nor_flash0_is_ready())
- {
- LOG_E("!! SPI FLASH NOT READY - degraded boot, params live in RAM only");
- /* 三声短鸣: 现场靠声音就知道是存储故障, 不用接串口 */
- Beep_Tip(200);
- rt_thread_mdelay(300);
- Beep_Tip(200);
- rt_thread_mdelay(300);
- Beep_Tip(200);
- }
- else if(easyflash_init()==EF_NO_ERR) //初始化成功
- {
- ef_ok = 1;
- ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
- ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
- up_flag_ef = (device_status.update_flag != 0) ? 1 : 0;
- ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
- device_status.rebootcnt++;
- /* ★ 原代码在这里立即 ef_set_env_blob("reboot_time") 落 flash。
- 这是每次开机必然执行的一笔写,正好落在上电/复位最不稳的时刻:
- 写一半被复位,该条 env 下次启动会被判成 ENV_ERR_HDR 而永久丢失;
- 若这一笔又触发 GC,中断的搬移会让整个 sector 被 format,成片丢参数。
- 开机计数改为随参数快照一起延迟落盘(param_mark_dirty),
- 计数照样保存,但消掉了开机瞬间这个高危 flash 写窗口。 */
- LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
- }
- else {
- LOG_E("easyflash init ERRPR!!!");
- }
- if(param_backup_init()==RT_EOK)
- {
- param_boot_snap_ok = (param_backup_read(¶m_boot_snap, ¶m_boot_seq) == 0);
- LOG_I("[param] boot snapshot %s, seq=%d",
- param_boot_snap_ok ? "VALID" : "NONE", (int)param_boot_seq);
- }
- /* ★ iSave 是整机参数的唯一哨兵: 它一旦丢失, 下面这个分支会把全部参数
- 刷成出厂值(现场表现为"重启后参数全没")。这里先用备份分区抢救一次,
- 把"丢一条 iSave -> 全量回出厂"拦在前面。 */
- if(ef_ok && (device_status.iSave != FLASH_FLAG) && param_boot_snap_ok
- && (param_boot_snap.iSave == FLASH_FLAG))
- {
- LOG_W("[param] iSave lost in easyflash, recover from backup seq=%d", (int)param_boot_seq);
- param_unpack(¶m_boot_snap);
- param_write_back_to_easyflash(¶m_boot_snap);
- param_mark_dirty();
- }
- //保存默认配置
- if(device_status.iSave!=FLASH_FLAG)
- {
- device_status.iSave=FLASH_FLAG;
- device_status.iFlag = 1;
- /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
- device_status.dtype = 3;
- device_status.version_type = 4; //4.0
- device_status.Power_Type = 0; //0-交流供电;1-直流供电
- device_status.clt_time = 6; //收集时间
- device_status.heattime = 8; //加热时间
- device_status.heatvalue=100; //加热温度
- device_status.thsp = 300; //默认熄屏时间5分钟
- device_status.sunBar = 9; //光控定时时长
- device_status.startBar = 20; //定时开始时间
- device_status.stopBar = 0; //定时结束时间
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = 5; //低温保护阈值
- device_status.shake_ws = 1; //震动开关,默认开
- #if TG_MODULE == 0
- device_status.shake_sec = 1; //震动时间,s
- #else
- device_status.shake_sec = 2; //震动时间,s
- #endif
- device_status.vol = 7; //音量
- device_status.timeSwitch = 0; //工作模式:光控
- device_status.passWord = 8888; //屏幕密码
- device_status.dattim = 10; //数据上传间隔
- device_status.ds = 1; //设备状态
- /* ★ 原代码在这里无条件把 update_flag 清 0。
- 走到这个分支只说明 iSave 这条 env 丢了(EasyFlash 局部损坏),
- 并不代表升级请求是假的 —— 现场表现就是:
- 参数丢了 -> 升级标志被顺手抹掉 -> 触发升级后重启静静跳过升级。
- 这里改为保留已读到的升级请求, 交给下面的三通道统一裁决。 */
- device_status.update_flag = ((up_flag_ef != 0)
- || (param_boot_snap_ok && param_boot_snap.update_flag != 0)) ? 1 : 0;
- /*孢子仪*/
- device_status.collecting_sh1 = 9;
- device_status.collecting_eh1 = 10;
- device_status.collecting_sh2 = 14;
- device_status.collecting_eh2 = 16;
- device_status.motor_active = 0;
- device_status.heat_sw = 0;
- device_status.cul_time = 1;
- device_status.run_sta = 0;
- device_status.handle_bugger = 0; //默认排虫
- device_status.work_mode = 0;
- sprintf(device_status.driver, "%s",VERSION_HK);
- sprintf(device_status.deviceId,"0");
- rt_sprintf(device_status.tempture, "-15");
- rt_sprintf(device_status.humidity, "31");
- rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
- rt_sprintf(device_status.alarm_tg,"220000");
- /* ★ 以下 32 连写只在 EasyFlash 可用时执行。
- flash 坏了硬写只会拿到一堆 EF_WRITE_ERR, 还可能触发 EasyFlash 内部 GC
- (GC 中途失败会 format 掉整个 sector)。默认值已经写进 RAM 了,
- 设备用出厂参数降级运行即可。 (内部未重新缩进, 保持与原代码逐行对齐) */
- if (ef_ok)
- {
- ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
- ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
- ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
- ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
- ef_set_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1)); //上传平台
- ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
- ef_set_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active)); //制冷开关
- ef_set_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //加热开关
- ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
- ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
- ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
- ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
- ef_set_env_blob("work_mode", &device_status.work_mode, sizeof(device_status.work_mode));
- ef_set_env_blob("alarm_tg", &device_status.alarm_tg, sizeof(device_status.alarm_tg));
- } /* if (ef_ok) —— flash 不可用时整段跳过 */
- LOG_I("SOFTWARE VERSION:%s",device_status.driver);
- LOG_I("Default paramconf is:");
- LOG_I("stime:%d",device_status.iSave);
- LOG_I("stime:%d",device_status.clt_time);
- LOG_I("startBar:%d",device_status.startBar);
- LOG_I("stopBar:%d",device_status.stopBar);
- LOG_I("timeSwitch:%d",device_status.timeSwitch);
- LOG_I("sunBar:%d",device_status.sunBar);
- LOG_I("jtime:%d",device_status.heattime);
- LOG_I("jheat:%d",device_status.heatvalue);
- LOG_I("thsp:%d", device_status.thsp);
- LOG_I("passwd:%d",device_status.passWord);
- LOG_I("alarm_tg:%s",device_status.alarm_tg);
- }//从easyflash读取参数配置
- else if (device_status.iSave==FLASH_FLAG) {
- device_status.iFlag = 0;
- ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
- ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
- ef_get_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time), NULL); //收集时间
- ef_get_env_blob("startBar", &device_status.startBar,sizeof(device_status.startBar), NULL); //开始时间
- ef_get_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar), NULL); //结束时间
- ef_get_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch), NULL); //模式
- ef_get_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar), NULL); //光控时间
- ef_get_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime), NULL); //加热时间
- /* heatvalue 这一个字段承载了两种语义, 写入侧也分成了两个 key:
- 测报灯(dtype!=7): 加热温度 -> key "jheat" (hmi.c 系统设置页)
- 孢子仪(dtype==7): 培养温度 -> key "heatvalue" (hmi.c 孢子仪系统设置)
- 老代码开机一律读 jheat, 孢子仪设好的培养温度重启后会被 jheat 的值顶掉,
- 现场表现就是"温度设了没保存住"。这里按 dtype 分开读;
- 老设备 flash 里还没有 heatvalue 这个 key 时回退读 jheat, 避免读到 0。 */
- if (device_status.dtype == 7) {
- if (ef_get_env_blob("heatvalue", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL) == 0) {
- ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //兼容老固件
- }
- } else {
- ef_get_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue), NULL); //加热温度
- }
- ef_get_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws), NULL); //震动开关
- ef_get_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec), NULL); //震动时间
- ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
- ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
- ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
- ef_get_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp), NULL); //熄屏时间
- ef_get_env_blob("passWord",&device_status.passWord,sizeof(device_status.passWord),NULL); //密码
- ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
- ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
- ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
- ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
- ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
- ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
- ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
- /* json_w_clock/json_w_reset/json_w_platform 已无对应全局字段,开机不再回读 */
- ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
- ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
- ef_get_env_blob("platform",&device_status.mqttcfg1,sizeof(device_status.mqttcfg1),NULL); //平台地址
- /*孢子仪*/
- ef_get_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time),NULL); //培养时间
- ef_get_env_blob("motor_active",&device_status.motor_active,sizeof(device_status.motor_active),NULL); //电机
- ef_get_env_blob("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw),NULL); //加热开关
- /* 制冷开关: 孢子仪设置页(hmi.c 系统设置, dtype==7)只写了这个 key,
- 老代码开机却从来不读 —— 设置完一重启就丢。这里补上读取。
- flash 里没有该 key 时 ef_get_env_blob 返回 0 且不清零 buf, 保持原值即可。 */
- ef_get_env_blob("cold_sw",&device_status.cold_sw,sizeof(device_status.cold_sw),NULL); //制冷开关
- ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
- ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
- ef_get_env_blob("deviceId",&device_status.deviceId,sizeof(device_status.deviceId),NULL); //设备编码
- ef_get_env_blob("handle_bugger",&device_status.handle_bugger,sizeof(device_status.handle_bugger),NULL); //排虫
- ef_get_env_blob("work_mode",&device_status.work_mode,sizeof(device_status.work_mode),NULL); //工作模式
- ef_get_env_blob("alarm_tg",&device_status.alarm_tg,sizeof(device_status.alarm_tg),NULL); //闹钟 //光控计数
- device_status.work_done = 0;
- /* ===== 参数健康自检 + 双分区恢复 =====
- EasyFlash 读出来的结果先体检; 只要有关键项为 0(说明对应 env 已损坏/丢失),
- 就用备份分区的快照整包恢复, 并回写 EasyFlash 修好 flash 侧的坏条目。
- 这样"丢参数"只会被修复一次, 不会像原来那样每重启都丢一遍。 */
- {
- /* ★ 同 param_boot_snap: 820 字节必须走静态区, 不能放 main 线程栈上 */
- static param_persist_t cur;
- int perr;
- param_pack(&cur);
- perr = param_check_health(&cur);
- if(perr != 0)
- {
- LOG_E("[param] easyflash param BROKEN, err=0x%02x", perr);
- if(param_boot_snap_ok && (param_check_health(¶m_boot_snap) == 0))
- {
- uint32_t rc = device_status.rebootcnt; /* 保留本次开机累加 */
- param_unpack(¶m_boot_snap);
- if(rc > device_status.rebootcnt)
- {
- device_status.rebootcnt = rc;
- }
- LOG_W("[param] restored from backup seq=%d, write back to easyflash", (int)param_boot_seq);
- param_write_back_to_easyflash(¶m_boot_snap);
- }
- else
- {
- LOG_E("[param] no usable backup(seq=%d), fall back to factory patch", (int)param_boot_seq);
- }
- }
- else if(param_boot_snap_ok && (param_boot_snap.rebootcnt > device_status.rebootcnt))
- {
- /* 两侧都正常时, 开机次数取较大者, 防止 reboot_time 这条 env 单独丢失 */
- device_status.rebootcnt = param_boot_snap.rebootcnt;
- }
- /* 无论走哪条路, 都让快照侧保持最新 */
- param_mark_dirty();
- }
- //丢参数的处理
- if(device_status.sunBar==0) device_status.sunBar = 4;
- if(device_status.dattim==0) device_status.dattim = 10;
- if(device_status.thsp==0) device_status.thsp = 300;
- if(device_status.clt_time==0) device_status.clt_time = 10;
- if(device_status.heattime==0) device_status.heattime = 10;
- if(device_status.heatvalue==0) device_status.heatvalue = 100;
- if(device_status.tph==0){ //参数丢失
- device_status.tpl = 5;
- device_status.tph = 70;
- device_status.ds = 1;
- device_status.startBar = 20;
- device_status.stopBar = 0;
- }
- if(device_status.work_mode>1) device_status.work_mode = 0;
- /* ★ 上面这段补丁原来只改 RAM 不落盘, 所以每次重启都要重复丢、重复修。
- 现在补一个 dirty 标记, 修正后的值会进备份分区, 下次开机再回写 EasyFlash,
- 把 flash 侧的错误值彻底修掉。 */
- param_mark_dirty();
- /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
- if(device_status.dtype!=7)
- {
- device_status.dtype = 3;
- }
- else {
- device_status.version_type = 7;
- }
- if(device_status.version_type==0)
- {
- device_status.version_type = 4;
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type));
- }
- else if(device_status.version_type==1)
- {
- rt_sprintf(device_status.driver, "%s",VERSION_ZP);
- }
- else if (device_status.version_type==11)
- {
- rt_sprintf(device_status.driver, "%s",VERSION_GK);
- }
- else if (device_status.version_type==4||device_status.version_type==42||device_status.version_type==43)
- {
- rt_sprintf(device_status.driver, "%s",VERSION_HK_2000W);
- }
- else if (device_status.version_type==41) {
- rt_sprintf(device_status.driver, "%s",VERSION_HK);
- }
- LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
- LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
- LOG_I("开机次数:%d",device_status.rebootcnt);
- LOG_I("收集时间:%d",device_status.clt_time);
- LOG_I("定时开始时间:%d",device_status.startBar);
- LOG_I("定时结束时间:%d",device_status.stopBar);
- LOG_I("timeSwitch:%d",device_status.timeSwitch);
- LOG_I("光控定时时长:%d",device_status.sunBar);
- LOG_I("加热开关:%d",device_status.heat_sw);
- LOG_I("加热时间:%d",device_status.heattime);
- LOG_I("加热温度:%d",device_status.heatvalue);
- LOG_I("震动开关:%d",device_status.shake_ws);
- LOG_I("震动时间:%d",device_status.shake_sec);
- LOG_I("高温保护:%d",device_status.tph);
- LOG_I("低温保护:%d",device_status.tpl);
- LOG_I("thsp:%d", device_status.thsp);
- LOG_I("密码:%d",device_status.passWord);
- LOG_I("数据上传间隔:%d",device_status.dattim);
- LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
- LOG_I("上传平台:%s",device_status.mqttcfg1);
- LOG_I("孢子仪运行状态:%d",device_status.run_sta);
- LOG_I("孢子仪采集时间:[%d-%d,%d-%d]",device_status.collecting_sh1,device_status.collecting_eh1,device_status.collecting_sh2,device_status.collecting_eh2);
- LOG_I("PowerType:%d",device_status.Power_Type);
- LOG_I("deviceId:%s",device_status.deviceId);
- LOG_I("handle_bugger:%d",device_status.handle_bugger);
- LOG_I("alarm_tg:%s",device_status.alarm_tg);
- LOG_I("motor_active:%d",device_status.motor_active);
- }
- /* 启动参数延迟落盘线程: 之后所有 param_mark_dirty() 的变更,
- 会在静默 500ms 后合并成一次 4KB 擦写写入备份分区,
- 把"改一条参数擦一次 flash"变成"一批改动擦一次"。 */
- param_backup_start();
- {
- device_status.update_flag = 0;
- //初始化联网状态
- rt_sprintf(device_status.net,"1");
- rt_sprintf(device_status.csq,"0");
- rt_sprintf(device_status.sim,"1");
- /* Sys_Motor_Task() 已取消(同步 master):sys_monitor 后台线程删除,
- 统计改为 sys_monitor_refresh() 现场采集,见 sys_monitor.c。 */
- ctr_router(ON);
- #if JINGJIKUAN == 1
- ctr_Cold(ON); //球阀电机电源
- ctr_Collect_Fan(ON); //雨控解除
- #endif
- Hmi_Fresh();
- Control_clear();
- Control_Check(); //自检
- dtu_data_play();
- sht30_collect();
- #if SENSOR_EN==1
- mb_master_samlpe();
- #endif
- net_wait_thread_entry();
- dtu_send_data_start();
- Workstatus_check();
- Work_Loop_Task();
- }
- }
- /* ★ P2-14: 开机等联网的独立线程。
- 原来这两个 while 直接写在 main 线程(Check_ParamConfig)里, 最长阻塞 180s + 10s:
- while(atoi(net)==1) { ... rt_thread_delay(1000) ... } // 最多 180 次 = 180s
- while(atoi(net)==0) { ... rt_thread_delay(2000) ... } // 最多 6 次 = 12s
- 期间 main 线程后面的 dtu_send_data_start() / Workstatus_check() / Work_Loop_Task()
- 全部延后 —— 现场表现是"开机后近 3 分钟内不自检、不进工作循环、不上报"。
- 现在把等待搬到本线程, main 立刻往下走。
- ⚠️ 本线程里所有用到 tm 的地方都用本函数自己的时间快照, 不再借用
- Check_ParamConfig() 的 static struct tm(那里开机后还会被别处改写)。 */
- static void net_wait_thread_entry(void)
- {
- uint16_t connect_num = 0;
- time_t nnow = time(RT_NULL);
- static struct tm ntm;
- gmtime_r(&nnow, &ntm);
- /* 等待联网 */
- while (atoi(device_status.net) == 1)
- {
- char *readinfo = makeJson_ReadInfo();
- if (readinfo != RT_NULL)
- {
- DTU_Write("%s", readinfo);
- rt_free(readinfo);
- }
- rt_thread_delay(2000);
- connect_num++;
- LOG_I("联网中......%d", connect_num);
- /* 超时时间为 3 分钟 */
- #if MATCH_DEVICE == 1
- if (connect_num == 60) /* 180 */
- #else
- if (connect_num == 90) /* 180 */
- #endif
- {
- device_status.timeSwitch = 0;
- LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",
- ntm.tm_year + 1900, ntm.tm_mon + 1, ntm.tm_mday,
- ntm.tm_hour, ntm.tm_min, ntm.tm_sec);
- #if 0
- set_date(2020, 1, 1);
- set_time(0, 0, 0);
- #endif
- break;
- }
- }
- connect_num = 0;
- rt_thread_delay(2000);
- #if MATCH_DEVICE == 1
- device_status.timeSwitch = 1;
- #endif
- /* ★ 原来 makeJson_Warn() 的返回值从未释放 -> 每次开机都漏一块堆 */
- dtu_write_json(makeJson_Warn("turnon", "device_stm32"));
- rt_thread_delay(1000);
- /* 本线程退出(联网成功或 3 分钟超时): 交还 read 刷新职责给 refresh_hmi_Component() */
- net_wait_running = 1;
- LOG_I("net wait thread exit");
- }
- void ResetParamDefault(void)
- {
- #if MATCH_DEVICE == 1
- rt_sprintf(device_status.tempture,"%d",30);
- #else
- rt_sprintf(device_status.tempture,"%d",-15);
- #endif
- device_status.ds = 1; //设备状态开机
- device_status.thsp = 300;
- device_status.passWord = 8888;
- device_status.dattim = 10;
- device_status.collecting_sh1 = 9;
- device_status.collecting_eh1 = 10;
- device_status.collecting_sh2 = 14;
- device_status.collecting_eh2 = 16;
- device_status.Power_Type = 0;
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- if(device_status.dtype==3)
- {
- device_status.clt_time = 6; //收集时间
- device_status.startBar = 20; //定时开始时间
- device_status.stopBar = 5; //定时结束时间
- device_status.heattime = 8; //加热时间
- device_status.shake_ws = 1; //针对开关
- #if TG_MODULE == 0
- device_status.shake_sec = 1; //震动时间,s
- #else
- device_status.shake_sec = 2; //震动时间,s
- #endif
- device_status.sunBar = 8;
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = -5; //低温保护阈值
- device_status.timeSwitch = 0; //定时模式:光控
- device_status.heatvalue=100; //加热温度
- device_status.heat_sw = 1;
- ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
- device_status.handle_bugger = 0;
- ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
- }
- else if(device_status.dtype==7)
- {
- device_status.run_sta = 0;
- device_status.heatvalue = 25; //培养温度
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
- ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
- ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
- ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
- ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- }
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
- ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
- /* 恢复出厂是一次 24 连写, 最容易被打断导致只写一半。
- 这里同步落一份整包快照到备份分区, 保证出厂值也有可信副本。
- 用 force 版本跳过节流, 保证立刻生效。 */
- param_mark_dirty();
- param_flush_force();
- HMI_WRITE("play %d,%d,%d",1,5,0);
- DTU_Write("reset.mp3");
- rt_thread_delay(1000);
- }
- void ResetParamDefault_DC(void)
- {
- //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
- rt_sprintf(device_status.tempture,"%d",-15);
- device_status.clt_time = 6; //收集时间
- device_status.dattim = 10; //数据上传间隔
- device_status.startBar = 20; //定时开始时间
- device_status.stopBar = 0; //定时结束时间
- device_status.heattime = 8; //加热时间
- device_status.shake_ws = 1; //针对开关
- #if TG_MODULE == 0
- device_status.shake_sec = 1; //震动时间,s
- #else
- device_status.shake_sec = 2; //震动时间,s
- #endif
- device_status.sunBar = 4;
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = -5; //低温保护阈值
- device_status.timeSwitch = 0; //定时模式
- device_status.heatvalue=100;
- device_status.ds = 1; //设备状态开机
- device_status.thsp = 300;
- device_status.passWord = 8888;
- device_status.timeSwitch = 0;
- device_status.Power_Type = 1;
- device_status.handle_bugger = 0;
- device_status.heat_sw = 0;
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- ef_set_env_blob("handle_bugger", &device_status.handle_bugger, sizeof(device_status.handle_bugger));
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("stime", &device_status.clt_time, sizeof(device_status.clt_time));
- ef_set_env_blob("startBar", &device_status.startBar, sizeof(device_status.startBar));
- ef_set_env_blob("stopBar", &device_status.stopBar, sizeof(device_status.stopBar));
- ef_set_env_blob("timeSwitch", &device_status.timeSwitch, sizeof(device_status.timeSwitch));
- ef_set_env_blob("sunBar", &device_status.sunBar, sizeof(device_status.sunBar));
- ef_set_env_blob("jtime", &device_status.heattime, sizeof(device_status.heattime));
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("shake_ws", &device_status.shake_ws, sizeof(device_status.shake_ws)); //震动开关
- ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
- ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
- ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
- ef_set_env_blob("heat_sw", &device_status.heat_sw,sizeof(device_status.heat_sw));
- /* 同上, 18 连写后同步一份整包快照(force 立即生效) */
- param_mark_dirty();
- param_flush_force();
- DTU_Write("reset.mp3");
- rt_thread_delay(1000);
- }
- //固件备份
- void app_partion_backup(void)
- {
- rt_kprintf("固件恢复!!!\n");
- const char *dst_part_name = "df_area";
- const char *src_part_name = "app";
- const struct fal_partition *dst_part;
- const struct fal_partition *src_part;
- /* ★ FAL 没建表成功时 fal_partition_find() 会撞 assert(init_ok) 死循环,
- 先查 fal_init_check() 门控。 */
- if (fal_init_check() != 1)
- {
- rt_kprintf("FAL is NOT initialized, partition backup aborted!\n");
- return;
- }
- dst_part = fal_partition_find(dst_part_name);
- src_part = fal_partition_find(src_part_name);
- if (dst_part == RT_NULL || src_part == RT_NULL)
- {
- if (dst_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", dst_part_name);
- if (src_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", src_part_name);
- }
- else
- {
- rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
- if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
- {
- int clone_pos = 0;
- int clone_len = 0, clone_tol_len;
- rt_uint8_t *buf = rt_malloc(4096);
- if (dst_part->len < src_part->len)
- clone_tol_len = dst_part->len;
- else
- clone_tol_len = src_part->len;
- while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
- {
- clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
- if (clone_len < 0)
- {
- rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
- break;
- }
- if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
- {
- rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
- break;
- }
- HMI_WRITE("#");
- rt_kprintf("#");
- clone_pos += clone_len;
- }
- if (clone_pos >= clone_tol_len)
- {
- rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
- }
- else
- {
- rt_kprintf("Clone partition failed!");
- }
- if (buf)
- rt_free(buf);
- }
- }
- }
- void Debug_log_ON(void)
- {
- rt_console_set_device("uart4");
- finsh_set_device("uart4");
- }
- void Debug_log_OFF(void)
- {
- rt_console_set_device("uart1");
- finsh_set_device("uart1");
- /* ★ 原来这里无条件再 rt_mq_init() 一次 + 再建一个 "Hmi_Fresh_thread":
- ① 对已初始化的消息队列重复 init 会重置其等待链表/读写指针(返回 -RT_ERROR),
- 正在排队的屏幕指令直接丢;
- ② 线程重复创建后同一消息队列有两个消费者, 每条屏幕指令会被处理两遍
- (reboot / 写 Flash 等动作执行两次), 且每次泄漏一份线程栈。
- 消息队列与接收回调在 INIT_BOARD 阶段已建立, 这里只需在升级结束线程退出后
- 重建(rt_thread_find 为空), 否则保持现状。 */
- if (serial != RT_NULL)
- {
- rt_device_set_rx_indicate(serial, uart_input);
- }
- if (rt_thread_find("Hmi_Fresh_thread") == RT_NULL)
- {
- hmi1 = rt_thread_create("Hmi_Fresh_thread",
- Hmi_thread_entry,
- RT_NULL,
- HMI_THREAD_STACK_SIZE,
- HMI_THREAD_PRIORITY,
- HMI_THREAD_TIMESLICE);
- if (hmi1 != RT_NULL)
- {
- rt_thread_startup(hmi1);
- }
- }
- }
- MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
- MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
- //
- INIT_BOARD_EXPORT(HMI_uart4_init);
|