| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819 |
- /*
- * 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
- */
- #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 "sys_monitor.h"
- #include "param_mirror.h"
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "hmi"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- #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];
- /* 串口原始包缓冲(与解析缓冲 rx_buffer 分离:帧组装时两者不能是同一块内存) */
- static char rx_pkt[HMI_UART_RXBUFFER_SIZE + 1];
- void list_mem(void);
- int mb_master_samlpe();
- /**/
- Device_Status_typedef device_status;
- System_stats_t system_status;
- 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; /*串口设备句柄*/
- 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);
- 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
- config.bufsz = 512; //缓冲区128字节
- 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 接收及轮询发送模式打开串口设备 */
- rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
- return RT_EOK;
- }
- /*串口屏写数据*/
- /* ==================== UART4(屏幕)发送:互斥 / 升级状态 / 批量 / 去重 ====================
- * 背景:workSta 页每秒被 getFullData 刷新一次,原实现每条属性 = 一次 rt_device_write
- * + 一次加解锁,115200bps 下每秒 60+ 次写、约 2KB 数据,既占串口带宽也占 CPU,
- * 还会把"屏幕数据"挤在日志里。2026-10-10 做如下优化(HMI_WRITE 签名不变,调用方无感):
- * ① 批量发送:HMI_BATCH_BEGIN()/END() 之间累积成整块再一次写入(缓冲满自动 flush);
- * ② 重复过滤:同一调用点(用格式化串地址唯一标识)输出内容与上次相同则不发送;
- * 过滤表为直接映射表,槽位冲突时按"未命中"处理——只会多发、绝不会漏发;
- * ③ 过滤表定期(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位,并在收到屏幕 Swversion 帧
- * (屏幕重启/换页握手)时立即复位,保证屏幕重启后最多 HMI_DEDUPE_RESYNC_SEC 秒
- * 即恢复全量刷新,不会长期停在旧值。
- * 维护提示:新增刷新项时无需关心批量/去重,直接用 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=最近一次升级数据时间 */
- 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()
- 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)
- {
- rt_bool_t locked = hmi_write_lock();
- rt_device_write(serial, 0, hmi_batch_buf, hmi_batch_len);
- if (locked)
- {
- hmi_write_unlock();
- }
- system_status.uart4_tx_bytes += hmi_batch_len;
- 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();
- rt_device_write(serial, 0, data, len);
- if (locked)
- {
- hmi_write_unlock();
- }
- system_status.uart4_tx_bytes += len;
- }
- void HMI_WRITE(char *fmt, ...)
- {
- va_list ap;
- char str[256 + 3]; /* 预留 3 字节结束符 0xFF 0xFF 0xFF */
- rt_size_t str_length;
- if (serial == 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) {
- 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)
- {
- /* 队列满:本条的"唤醒消息"丢弃即可——数据仍在串口 FIFO 中,
- * 下一帧到达时消费者会一并读取。原实现在中断回调里 RESET 整个队列,
- * 会把已入队的唤醒消息全部吞掉,多帧突发时反而丢更多帧。 */
- LOG_I("hmi message queue full, drop this wakeup!\n");
- }
- return result;
- }
- /* UART4(屏幕口)接收回调重新注册。
- * 注意:rt_device_close() 会清空 rx_indicate,设备被 close/open 后(如串口环回自检、
- * 屏幕升级流程)必须重新注册,否则屏幕数据无法被 Hmi_thread_entry 读取。 */
- void hmi_uart4_reinit_rx(void)
- {
- if (serial != RT_NULL)
- {
- rt_device_set_rx_indicate(serial, uart_input);
- LOG_I("hmi uart4 rx_indicate re-registered");
- }
- }
- // 预计算转换表
- 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)
- {
- rt_size_t sent;
- rt_bool_t locked;
- if (!serial || !data || size == 0) return -1;
- /* 与 HMI_WRITE 共用同一把锁,避免升级大包数据与业务指令互相穿插 */
- locked = hmi_write_lock();
- sent = rt_device_write(serial, 0, data, size);
- if (locked)
- {
- hmi_write_unlock();
- }
- return (sent == size) ? 0 : -1;
- }
- // 最简洁版本 - 每4096字节直接清空
- void process_hex_payload(const char* hex_str, int req_id) {
- int data_len;
- int progress;
- uint8_t temp[JSON_CHUNK_SIZE];
- char progress_json[256];
- if (!hex_str) return;
- current_state = STATE_RECEIVING;
- s_hmi_upgrade_tick = rt_tick_get(); /* 刷新"升级活动中"时间戳 */
- // 转换HEX
- 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("HMI升级缓冲区溢出(recv=%u,new=%d), 丢弃本包", (unsigned)recv_write_pos, data_len);
- 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, (unsigned)recv_write_pos, (unsigned)sizeof(recv_buffer));
- /* 每满 4096 字节发送一块。
- * 修复:原实现发送后直接清空缓冲,若积攒超过 4096 字节(分块长度不固定时)
- * 会把超出的部分丢掉导致屏幕固件损坏;这里改为发送后把剩余数据前移保留。 */
- if (recv_write_pos >= 4096) {
- LOG_I("发送 4096 字节到HMI...");
- /* 失败时重试一次,仍失败才丢弃本块 */
- if (send_raw_to_hmi(recv_buffer, 4096) != 0 &&
- send_raw_to_hmi(recv_buffer, 4096) != 0)
- {
- LOG_E("发送 4096 字节到HMI失败(已重试), 丢弃本块(请检查屏幕连接)");
- recv_write_pos = 0;
- }
- else
- {
- recv_write_pos -= 4096;
- if (recv_write_pos > 0) {
- memmove(recv_buffer, recv_buffer + 4096, recv_write_pos);
- }
- }
- return;
- }
- // 检查完成
- if (bytes_received >= total_file_size && total_file_size > 0) {
- if (recv_write_pos > 0) {
- LOG_I("发送最后 %u 字节", (unsigned)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;
- }
- // 发送进度
- progress = total_file_size > 0 ?
- (int)(bytes_received * 100 / total_file_size) : 0;
- rt_snprintf(progress_json, sizeof(progress_json),
- "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
- "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
- (unsigned)bytes_received, (unsigned)total_file_size, progress, req_id);
- DTU_Write("%s", progress_json); /* 修复:进度串必须作为参数而不是格式串 */
- }
- /* ==================== UART4(屏幕)接收:帧组装 ====================
- * 屏幕每条指令以 "\r\n" 结尾(见原 Hmi_thread_entry 注释),而串口按 DMA 包分段:
- * ① 一条指令可能被拆到两次 read —— 旧实现靠 strstr 前缀匹配,残段会误命中别的
- * 分支(2026-10-08 的 SwversionV4/V43 误切版本类型即此问题);
- * ② 一次 read 可能含多条指令 —— 旧实现是 if/else 链,只处理第一条匹配的分支,
- * 例如 "getFullData\r\n" 与 "putTest.switchHeat=1\r\n" 同包时,按键会被丢掉。
- * 现在:整包先喂入组装缓冲,按 '\n' 切出完整帧逐条交给 HMI_Decode(帧内容保留
- * "\r\n",与旧行为一致);残段留待下次 read 拼接;超长无结束符的帧丢弃并告警。
- */
- /* 单帧上限:常规屏幕指令都很短;惟 debug1 "dbg:" 帧会送回整个文本框内容
- * (含中文日志时按 UTF-8 3 字节/字计算,120 字符最坏约 380B + 前缀/结束符),
- * 因此由 256 提升到 512(接收缓冲 rx_buffer 同为 512,配套一致)。 */
- #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 可以发送升级文件了 */
- char *upgrade_hmi = makeJsonRpc2_0("upgradehmi", NULL, 1);
- if (upgrade_hmi)
- {
- DTU_Write("%s",upgrade_hmi);
- free(upgrade_hmi);
- }
- 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.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';
- /* 屏幕每帧原始数据:改为 DBG_DBG 级日志(原为无条件 rt_kprintf,
- * 每秒刷新时刷屏且占串口时间),需要排查时把 DBG_LEVEL 调到 DBG_DBG */
- LOG_D("hmi rx(%u): %s", (unsigned)rx_length, rx_pkt);
- if (Hmi_Rx_Feed(rx_pkt, (rt_uint16_t)rx_length))
- {
- hmi1 = RT_NULL; /* 线程即将退出,句柄置空以便后续按需重建 */
- 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;
- }
- /* ==================== 屏幕测试加热:定时自动关闭 ====================
- * 需求:屏幕测试页打开加热(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);
- }
- }
- /***************************** 解析从屏幕收到的数据 begin **********************************/
- /* ==================== 屏幕命令严格匹配(2026-10-08 修复) ====================
- * 屏幕命令以 \r 结尾(见 Hmi_thread_entry 中说明),但串口按包分段:
- * 一条 "SwversionV43" 可能被拆成 "SwversionV4" + "3" 两段先后到达。
- * 原实现用 strstr() 前缀包含匹配,前段 "SwversionV4" 会误命中"水稻"分支,
- * 把 version_type 错误切换(并误报 RTU、重复写 Flash、随后完整帧又切回,
- * 配置来回翻转,日志表现为 SwversionV4/save/SwversionV43/save 连环)。
- * 本函数要求:命令字完整相等,且其后紧跟 \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;
- }
- /* ==================== 屏幕整页刷新:getFullData / Swversion 帧触发 ====================
- * 本函数原为 HMI_Decode 中 if/else 链的第一个分支(约 240 行),抽出为独立函数,
- * 便于定位、维护与文档化;调用处用 HMI_BATCH_BEGIN/END 包裹,整页 60+ 条属性
- * 合并为 1~2 次串口写(见文件头部 HMI_WRITE 处的发送优化说明)。
- * 参数 ptm:调用方已取好的当前 RTC 时间,避免重复 time()/gmtime_r()。 */
- static void hmi_refresh_pages(const struct tm *ptm)
- {
- const struct tm tm = *ptm;
- if(device_status.refresh_num==5)
- {
- device_status.refresh_num = 0;
- char *readinfo = makeJson_ReadInfo();
- DTU_Write("%s",readinfo);
- rt_free(readinfo);
- list_mem();
- /* 2026-10-15 修复:不得覆盖云端 read 触发的待补发上报标志(hmi_fresh==2)。
- * 原实现无条件置 1,会把 read→getinfo→sync→补发上报 链路上的待办请求吃掉:
- * 本屏幕刷新周期(refresh_num==5)恰好落在 RTU 应答 synchronization 的
- * 1.5~2.2s 窗口内时,==2 判定失效 → 补发被吞 → read 无应答、RTU 等待超时
- * (设备实测 2026-10-15:read 后数百 ms 内即被覆盖)。 */
- if(device_status.hmi_fresh!=2)
- {
- 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(1)
- {
- 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("menu.va_net.val=%d",device_status.link_sta);
- //更新CSQ
- HMI_WRITE("menu.csq_value.txt=\"%s\"",device_status.csq);
- HMI_WRITE("menu.va_csq.val=%d",atoi(device_status.csq));
- //更新收集时间
- 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("sysTest.switchHeat.val=%d",device_status.heat_status);
- //更新密码
- HMI_WRITE("inputPasswd.pwdData.txt=\"8888\"");
- //更新时间设置中的参数
- //更新定时模式
- 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);
- //震动时间
- HMI_WRITE("cangWei.h0.val=%d",device_status.shake_sec);
- HMI_WRITE("menu.version.txt=\"%s\"",device_status.driver);
- HMI_WRITE("menu.cpu_usage.val=%d",system_status.cpu_usage);
- //更新熄屏时间
- if(device_status.thsp<60) device_status.thsp = device_status.thsp * 60;
- HMI_WRITE("thsp=%d",device_status.thsp);
- HMI_WRITE("menu.cam_sta.val=%d",device_status.camsta);
- HMI_WRITE("menu.mn.txt=\"%s\"",device_status.cam_model);
- }
- 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(device_status.sensor_485_sta==1)
- {
- HMI_WRITE("menu.sensor_state.txt=\"传感器已连接:(%s lux-%s hpa)\"",device_status.lux,device_status.ap);
- }
- else
- {
- HMI_WRITE("menu.sensor_state.txt=\"传感器未连接\"");
- }
- //更新联网状态
- if(atoi(device_status.net)==1)
- {
- HMI_WRITE("menu.net.val=1");
- HMI_WRITE("workSta.net.val=1");
- }
- else {
- HMI_WRITE("workSta.net.val=0");
- HMI_WRITE("menu.net.val=0");
- }
- if(atoi(device_status.csq)==0&&atoi(device_status.net)==1)
- {
- HMI_WRITE("menu.net.val=1");
- HMI_WRITE("workSta.net.val=2");
- }
- /**********************************************************************/
- }
- /* ==================== 屏幕 workSta 页:拍照/清扫状态 + 工作过程显示 ====================
- * 随整页刷新每秒执行一次(getFullData 帧触发):
- * 拍照状态 workSta.photoSta / 清扫状态 workSta.clearSta:0-停止 1-进行中(双态按钮,写 .val)
- * t1 剩余加热时间(s) t2 收集倒计时(s) t3 仓门开/关动作倒计时(s)
- * t4 工作进度(%) t5 拍照结束剩余(s) t6 清扫结束剩余(s) (文本控件,写 .txt)
- * 数据来源:control.c 各控制流程实时写入 device_status.work_*、photo_left、clear_left;
- * t3/t5 的倒计时源在 Ctr_Mortor_entry 中随仓门电机/拍照时序刷新;
- * t4 由 Work_Progress_Get() 按"已用时间/预计总时长"换算(见 control.c)。
- * 数值显示规则(2026-10-10 调整):t1~t6 数值为 0 时下发**空字符串**(屏幕该栏显示空白,
- * 而不是"0"),非 0 才下发数值;t4 非 0 时带 % 号(显示形如 "45%")。
- * 下发条件:HMI_WORK_PROC_VT(vt) 当前为 (vt)!=6 —— 除吸虫塔外的测报灯页面都下发,
- * 孢子仪(dtype!=3)在前一行条件已被挡掉。若想只给特定页面下发,
- * 把该宏改成例如 (vt)==41(仅 4.0 海康)即可。
- * 若 HMI 工程里控件名/类型与此不一致,只需改本函数内的 HMI_WRITE 目标名。 */
- #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 || !HMI_WORK_PROC_VT(device_status.version_type))
- {
- 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) */
- }
- /* ==================== 屏幕 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 之类的长命令把屏幕刷新线程卡死。
- * 注意(2026-10-13 修复):转发设备只 find 句柄、不自行 open/close,并在每次
- * 控制台切换后由 dbg_chain_fixup() 补回接收模式位(详见该函数注释)——
- * 否则 rt_serial_open 的 open_flag 覆盖会洗掉 INT_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 "STM32F103RE" /* 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;
- }
- }
- /* ---------- 本机调试口"读路径保护"(2026-10-13 修复:打开调试后串口无法输入) ----------
- * 现象:点"打开调试"后,PC 上的调试串口(本机口 uart1)敲键盘无任何反应。
- * 根因(已读内核源码取证):
- * RT-Thread 4.0.2 的 rt_serial_open() 用 `dev->open_flag = oflag & 0xff` 无条件覆盖
- * 设备标志位(components/drivers/serial/serial.c:593),而
- * RT_DEVICE_FLAG_INT_RX(0x100) 高于 0xff —— 任何一次"不带 INT_RX 的 open"
- * (如 rt_console_set_device() 内部的 OFLAG_RDWR|STREAM)都会把接收模式位洗掉。
- * 而接收路径 rt_serial_read() 按该位选模式(serial.c:803):位丢失后
- * tshell 的 read() 掉进 _serial_poll_rx 轮询分支(stm32_getc 直读 RDR 寄存器),
- * 字符却早已被 RX 中断搬进 fifo —— fifo 里的输入永远读不出来,即"无法输入"。
- * 处理:uart1 的 fifo/RX 中断本身不受影响(引用计数从未归零,device.c 的 close
- * 只在 ref 减到 0 时才真正关闭设备),**唯一要做的就是把接收模式位补回 open_flag**:
- * ① dbg_chain_fixup():记录 uart1 原始接收模式位(INT_RX/DMA_RX)并补回;
- * ② 每次控制台切换(打开/关闭调试、屏幕命令的临时切换)之后都调用一次;
- * ③ 调试页心跳(每秒 getDebugData)也调用 —— 即使其它代码误改,1 秒内自愈。
- * 另:不再自行 open/close 转发设备(旧实现的 open 正是肇事者,close 还会错配引用
- * 计数),转发只需 rt_device_write 时设备已被 finsh/console 持有引用即可。 */
- static rt_uint16_t dbg_chain_rx_flag = 0; /* 本机口的接收模式位(INT_RX/DMA_RX),首次读取后固定 */
- static void dbg_chain_fixup(void)
- {
- struct rt_serial_device *ser;
- if (DBG1_CHAIN_DEV[0] == '\0') /* 置 "" 关闭转发(编译期常量) */
- {
- return;
- }
- if (dbg_chain_dev == RT_NULL)
- {
- dbg_chain_dev = rt_device_find(DBG1_CHAIN_DEV);
- if (dbg_chain_dev == RT_NULL)
- {
- return;
- }
- }
- ser = (struct rt_serial_device *)dbg_chain_dev;
- if (dbg_chain_rx_flag == 0)
- {
- /* 首次:从当前 open_flag 读接收模式(uart1 无 DMA 配置,默认 INT_RX) */
- dbg_chain_rx_flag = dbg_chain_dev->open_flag &
- (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
- if (dbg_chain_rx_flag == 0)
- {
- dbg_chain_rx_flag = RT_DEVICE_FLAG_INT_RX;
- }
- }
- if (ser->serial_rx == RT_NULL)
- {
- /* 接收 fifo 不存在(设备曾被整体关闭过)→ 重新 open 重建 fifo 并重开 RX 中断。
- * open 走"首次打开"分支,会 malloc fifo 并执行 SET_INT(见 serial.c:596-615),
- * 且 open 的 oflag 已带接收位,open_flag 自动正确。 */
- rt_device_open(dbg_chain_dev, RT_DEVICE_OFLAG_RDWR | dbg_chain_rx_flag | RT_DEVICE_FLAG_STREAM);
- return;
- }
- /* 补回接收模式位(仅设备框架层标志,不动硬件;fifo 数据原样可读) */
- dbg_chain_dev->open_flag |= dbg_chain_rx_flag;
- }
- /* ---------- uart4(屏幕口)接收模式位保护 ----------
- * 与 uart1 同一机理(见上方 dbg_chain_fixup 注释):uart4 由 HMI_uart4_init() 以
- * RT_DEVICE_FLAG_DMA_RX 打开;rt_console_set_device("uart4")/finsh_set_device("uart4")
- * 等路径内部的 open 会按 `open_flag = oflag & 0xff` 把 DMA_RX 位(0x400)洗掉
- * (serial.c:593),rt_serial_read() 随即掉进轮询读分支(serial.c:803)——
- * 屏幕数据读不出来,现象为"屏幕点不动",且屏幕自身无法再下发恢复指令。
- * 处理:Debug_log_ON/OFF(含屏幕 DebugEnable/DebugDisable 指令)切换后立即补回;
- * 电机线程循环每 100ms 再补一次(自愈)。 */
- static rt_uint16_t hmi_uart4_rx_flag = 0; /* uart4 接收模式位,首次读取后固定 */
- static void hmi_uart4_fixup_rx(void)
- {
- if (serial == RT_NULL)
- {
- return;
- }
- if (hmi_uart4_rx_flag == 0)
- {
- /* 首次:读当前 open_flag;若已被洗掉按实际打开方式(DMA_RX)兜底 */
- hmi_uart4_rx_flag = serial->open_flag &
- (RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX);
- if (hmi_uart4_rx_flag == 0)
- {
- hmi_uart4_rx_flag = RT_DEVICE_FLAG_DMA_RX;
- }
- }
- /* 仅补框架层标志位,不动硬件(DMA 仍在搬运,fifo 数据原样可读) */
- serial->open_flag |= hmi_uart4_rx_flag;
- }
- 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_chain_fixup(); /* 补回本机口接收模式位,保 tshell 输入 */
- }
- 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_chain_fixup(); /* 保住本机口接收模式位,PC 输入不受影响 */
- 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_chain_fixup(); /* 必须补回,否则恢复后本机口无法输入 */
- }
- 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(FAL_USING_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_mirror_sync();
- }
- 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_mirror_sync();
- }
- 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_mirror_sync();
- }
- 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_mirror_sync();
- }
- 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_mirror_sync();
- }
- 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_mirror_sync();
- }
- 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_mirror_sync();
- }
- else {
- }
- return;
- }
- //吸虫塔
- else if (strstr(frame,"SwversionV6")!=NULL) {
- device_status.dtype = 3;
- if(device_status.version_type != 6)
- {
- device_status.version_type = 6;
- sprintf(device_status.driver, "%s",VERSION_XCT);
- 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_mirror_sync();
- }
- }
- 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_mirror_sync();
- }
- }
- }
- static void HMI_Decode(void)
- {
- char tmp[128];
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- char *json_temp;
- char *var=NULL;
- if(device_status.refresh_num==0) device_status.refresh_num = 5;
- /* ---- 屏幕 debug1 调试页:心跳刷新(getDebugData)+ 按钮/控制台指令 ----
- * 必须放在 if/else 链最前:dbg:<命令> 的内容里可能包含 reboot/clear 等
- * 既有分支的关键字,让它们先匹配会导致误动作。 */
- if (strstr(rx_buffer, "getDebugData") != RT_NULL)
- {
- s_dbg1_page_tick = rt_tick_get(); /* 调试页心跳:离开该页(超时)后暂停日志推送 */
- dbg_chain_fixup(); /* 自愈:本机口接收位若被其它路径改动,1 秒内自动恢复 */
- 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,"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);
- }
- HMI_BATCH_BEGIN();
- hmi_refresh_pages(&tm);
- hmi_refresh_work_proc(); /* 拍照/清扫状态 + t1~t6 工作过程显示 */
- HMI_BATCH_END();
- }
- 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 1
- device_status.test_flag = 1;
- device_status.photo_ws = 0;
- rt_thread_delay(500);
- device_status.photo_ws = 1;
- char *photo = makeJson_Warn("takephoto","control");
- DTU_Write("%s",photo);
- rt_free(photo);
- #else
- Hk_Take_io();
- #endif
- }
- else if(strstr(rx_buffer,"putTest.switchPhoto=0")!=NULL) {
- device_status.photo_ws = 0;
- rt_thread_delay(500);
- LOG_I("停止拍照!");
- }
- else if (strstr(rx_buffer,"putTest.switchPic=1")!=NULL) {
- #if 1
- device_status.photo_ws = 0;
- rt_thread_delay(500);
- device_status.photo_ws = 1;
- char *photo = makeJson_Warn("takephoto","control");
- DTU_Write("%s",photo);
- rt_free(photo);
- #else
- Hk_Take_io();
- #endif
- }
- //转盘复位
- else if (strstr(rx_buffer,"turn_init")!=NULL) {
- LOG_I("转仓复位!");
- ctr_turn(0,0);
- }
- //转盘
- else if (strstr(rx_buffer,"turn")!=NULL) {
- LOG_I("转仓!");
- ctr_turn(90,0);
- }
- //打开电磁阀
- else if (strstr(rx_buffer,"putTest.switchDrop=1")!=NULL) {
- LOG_I("开始滴液!");
- #if Color_EN == 1
- device_status.run_sta = 5;
- #else
- ctr_shake(ON, device_status.shake_sec);
- #endif
- }
- //关闭电磁阀
- else if (strstr(rx_buffer,"putTest.switchDrop=0")!=NULL) {
- LOG_I("停止滴液!");
- ctr_shake(OFF, 0);
- }
- //打开补光
- 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!");
- }
- //打开加热(屏幕测试加热:HMI_TEST_HEAT_TIMEOUT 秒后自动关闭)
- else if(strstr(rx_buffer,"putTest.switchHeat=1")!=NULL) {
- device_status.heat_status = 1;
- ctr_Heat(ON);
- hmi_test_heat_start();
- LOG_I("Open Heat! (auto off in %d s)", HMI_TEST_HEAT_TIMEOUT);
- }
- //打开制冷
- else if(strstr(rx_buffer,"putTest.switchCold=1")!=NULL) {
- ctr_Cold(ON);
- LOG_I("Open Cold!");
- }
- //关闭制冷
- else if(strstr(rx_buffer,"putTest.switchCold=0")!=NULL) {
- ctr_Cold(OFF);
- LOG_I("Close Cold!");
- }
- //打开采集
- else if(strstr(rx_buffer,"putTest.switchCollect=1")!=NULL){
- ctr_Collect_Fan(ON);
- LOG_I("采集风机已打开!");
- }
- //关闭采集
- else if(strstr(rx_buffer,"putTest.switchCollect=0")!=NULL){
- ctr_Collect_Fan(OFF);
- LOG_I("采集风机已关闭!");
- }
- //打开散热
- else if(strstr(rx_buffer,"putTest.switchCool=1")!=NULL){
- ctr_Cool_Fan(ON);
- LOG_I("散热风机已打开!");
- }
- //关闭散热
- else if(strstr(rx_buffer,"putTest.switchCool=0")!=NULL){
- ctr_Cool_Fan(OFF);
- LOG_I("散热风机已关闭!");
- }
- //开始对焦拍照
- else if(strstr(rx_buffer,"putTest.switchAutopic=1")!=NULL){
- device_status.autotakepic=1;
- rt_kprintf("开始对焦拍照!\n");
- }
- //停止对焦拍照
- else if(strstr(rx_buffer,"putTest.switchAutopic=0")!=NULL){
- device_status.autotakepic=0;
- }
- //粘附测试-开
- else if(strstr(rx_buffer,"putTest.switchFsl=1")!=NULL){
- rt_kprintf("粘附测试开!\n");
- device_status.fsl_sw = 1;
- }
- //粘附测试-关
- else if(strstr(rx_buffer,"putTest.switchFsl=0")!=NULL){
- rt_kprintf("粘附测试关!\n");
- pwm_off(2, 0);
- pwm_off(3,0);
- }
- else if (strstr(rx_buffer,"putTest.switchAutotest=1")!=NULL)
- {
- LOG_I("开始一键测试!!!");
- device_status.auto_test_sw=1;
- }
- /* 修复(2026-10-10):原条件误写成 switchAutotest=1,与上一个分支完全重复、
- * 永远不可达,导致屏幕上的"停止一键测试"无效;按日志本意改为 =0。 */
- else if (strstr(rx_buffer,"putTest.switchAutotest=0")!=NULL)
- {
- LOG_I("停止一键测试!!!");
- device_status.auto_test_sw=0;
- }
- //清扫
- else if (strstr(rx_buffer,"clear")!=NULL) {
- rt_kprintf("\n清扫!\n");
- if(device_status.version_type==4|| //水稻
- device_status.version_type==41|| //4.0
- device_status.version_type==42|| //低端款
- device_status.version_type==43|| //中端款
- device_status.version_type==12) //高空带拍照
- {
- device_status.clear_status=1;
- }
- else if (device_status.version_type==1) {
- device_status.clear_status=2;
- }
- else if (device_status.version_type==6) {
- ctr_JD_Clear(ON);
- }
- }
- //震动
- 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);
- }
- }
- //打开补光
- 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;
- if(Read_Rain_Voltage()<250)
- device_status.rain_status = 0;
- rt_kprintf("Open Lamp!\n");
- ctr_lamp(ON,1);
- #if LAMP_EN == 1
- ctr_lamp(ON,2);
- ctr_lamp(ON,3);
- #endif
- }
- //关闭灯管
- else if (strstr(rx_buffer,"putTest.switchLamp=0")!=NULL)
- {
- device_status.test_flag=0;
- rt_kprintf("Close Lamp1!\n");
- ctr_lamp(OFF,1);
- #if LAMP_EN == 1
- ctr_lamp(OFF,2);
- ctr_lamp(OFF,3);
- #endif
- }
- /* 说明(2026-10-10):此处原有第二组 "putTest.switchHeat=0/1" 分支,与上方的
- * 同名分支条件完全相同、永远不可达(死代码),已删除;加热的开关动作统一由
- * 上方第一组分支处理(含屏幕测试加热 10 分钟自动关闭,见 hmi_test_heat_start())。
- * 同时修复了一处同类笔误:下方"停止一键测试"分支原条件误写成 switchAutotest=1
- * (与上一个分支重复、不可达),已改为 =0,使屏幕上的停止按钮生效。 */
- //打开上仓门
- 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));
- param_mirror_sync();
- }
- 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));
- param_mirror_sync();
- }
- //设置设备时间
- 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));
- 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();
- }
- //上传最新数据(立即触发一次上报更新RTU/平台时间;原为 num_updata=2 延时2秒上报)
- dtu_request_report();
- }
- else {
- LOG_E("set_date failed!");
- }
- }
- }
- //设备编码设置
- 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);
- }
- }
- //定时设置
- #if DZ_260630_1 == 0
- 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();
- param_mirror_sync();
- 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)
- {
- /* 注:LAMP_EN==1(三灯管)时工作时长不再由 sunBar 决定
- * (灯管按 LAMP_ROTATE_SEC 每小时轮换),此处值仅做记录 */
- 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));
- }
- // time_t now;
- // time(&now);
- // static struct tm nowstamp;
- // nowstamp =*localtime(&now);
- // nowstamp.tm_hour = nowstamp.tm_hour - 8;
- // static rt_uint32_t nowstamp_num = 0;
- // nowstamp_num = mktime(&nowstamp);
- // device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
- }
- 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:
- rt_snprintf(tmp, sizeof(tmp), "屏幕操作:修改工作时间:开始-%d,结束-%d,模式-%d(0-光控,1-时控)操作时间:%s",device_status.startBar,device_status.stopBar,device_status.timeSwitch,device_status.stamp); /* 原 rt_sprintf 无长度上限,改带界变体 */
- json_temp = makeJson_Warn(tmp, "修改工作时间");
- DTU_Write("%s",json_temp);
- rt_free(json_temp);
- rt_thread_delay(2000);
- MA_save();
- param_mirror_sync();
- /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
- rt_kprintf("工作时间参数已保存:模式-%d,光控时间-%d,时控开始时间-%d,时控结束时间-%d\n",device_status.timeSwitch,device_status.sunBar,device_status.startBar,device_status.stopBar);
- }
- #endif
- 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();
- param_mirror_sync();
- 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)
- {
- rt_kprintf("加热时间:%d\n",atoi(heattime_str));
- device_status.heattime = 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);
- 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); //音量
- DTU_Write("volume=");
- }
- strncpy(thsp_str,var+10,4);
- if(atoi(thsp_str)>0&&atoi(thsp_str)<3600)
- {
- device_status.thsp = atoi(thsp_str); //熄屏时间
- }
- else {
- rt_kprintf("参数设置失败!!!\n");
- }
- rt_thread_delay(1000);
- MA_save();
- param_mirror_sync();
- /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
- 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(…,2) 无结束符:两位数值(如 20)时
- * atoi() 会越界读栈(未定义行为)。数组加 1 字节并补 '\0'。 */
- char rtc_h[3];
- var = strstr(rx_buffer,"=");
- var++;
- strncpy(rtc_h,var,2);
- rtc_h[2] = '\0';
- if(atoi(rtc_h)>=0 && atoi(rtc_h) <= 24)
- {
- set_time(atoi(rtc_h), 99, 99);
- }
- }
- else if (strstr(rx_buffer,"rtc_m=")!=NULL) {
- char rtc_m[3];
- var = strstr(rx_buffer,"=");
- var++;
- strncpy(rtc_m,var,2);
- rtc_m[2] = '\0';
- if(atoi(rtc_m)>=0 && atoi(rtc_m) <= 24)
- {
- set_time(99, atoi(rtc_m), 99);
- }
- }
- else if (strstr(rx_buffer,"rtc_s=")!=NULL) {
- char rtc_s[3];
- var = strstr(rx_buffer,"=");
- var++;
- strncpy(rtc_s,var,2);
- rtc_s[2] = '\0';
- if(atoi(rtc_s)>=0 && atoi(rtc_s) <= 24)
- {
- set_time(99, 99, atoi(rtc_s));
- }
- }
- 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();
- param_mirror_sync();
- }
- else {
- rt_kprintf("设置失败!!!\n");
- }
- /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
- 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-06-01:修复*10导致震动很久
- }
- else {
- LOG_E("设置失败!!!");
- }
- /* 注意:var 指向静态 rx_buffer 内部,不是堆内存,不能 rt_free(原实现会误导后续维护) */
- LOG_D("震动时间设置:%d",device_status.shake_sec);
- }
- //恢复出厂设置电池
- else if (strstr(rx_buffer,"resetdcDefault")!=NULL)
- {
- ResetParamDefault_DC();
- rt_thread_delay(1000);
- //2022/06/23,add:
- rt_snprintf(tmp, sizeof(tmp), "屏幕操作:系统重置:电池供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
- json_temp = makeJson_Warn(tmp, "系统重置");
- 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:
- rt_snprintf(tmp, sizeof(tmp), "屏幕操作:系统重置:交流供电,操作时间:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
- json_temp = makeJson_Warn(tmp, "系统重置");
- DTU_Write("%s",json_temp);
- rt_free(json_temp);
- }
- else if (strstr(rx_buffer,"DebugEnable")!=NULL) {
- Debug_log_ON();
- }
- else if (strstr(rx_buffer,"DebugDisable")!=NULL) {
- Debug_log_OFF();
- }
- /* 屏幕版本类型切换(SwversionV12/V11/V1/V43/V42/V41/V4/V6/V30/V7)已迁移:
- * 原链位于本 else-if 阶梯内,因上方“SwversionV 整页刷新”分支会先命中所有
- * 版本帧而永远不可达(死代码);现改由“版本串变化检测”分支在
- * hmi_ver_changed() 为真时调用 hmi_switch_version() 完成切换(2026-10-15)。 */
- else if (strstr(rx_buffer,"putTest.switchFAN_PU=1")!=NULL) {
- ctr_Fan_PUT(ON);
- }
- else if (strstr(rx_buffer,"putTest.switchFAN_PU=0")!=NULL) {
- ctr_Fan_PUT(OFF);
- }
- else if (strstr(rx_buffer,"putTest.switchFAN_IN=1")!=NULL) {
- ctr_Fan_IN(ON);
- }
- else if (strstr(rx_buffer,"putTest.switchFAN_IN=0")!=NULL) {
- ctr_Fan_IN(OFF);
- }
- else if(strstr(rx_buffer,"DisplayPower")!=RT_NULL)
- {
- HMI_WRITE("sysTest.sysTest_power.txt=\"%s\"",device_status.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) */
- }
- //立即上传
- 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();
- }
- //软件重启
- else if (strstr(rx_buffer,"reboot")!=NULL) {
- rt_hw_cpu_reset();
- }
- //屏幕上触发远程升级
- else if (strstr(rx_buffer,"hmi_update")!=NULL) {
- DTU_Write("hmi_update");
- }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 返回值,每次保存相机参数泄漏一份 cJSON 缓冲 */
- char *cam_json = makeJson_Cam("SaveParam",start_pos);
- if (cam_json != RT_NULL)
- {
- DTU_Write("%s",cam_json);
- rt_free(cam_json);
- }
- }
- //开启超时定时器
- 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);
- device_status.selfcheck_ws = 2;
- LOG_W("开始自检:%d",device_status.selfcheck_ws);
- }
- memset(rx_buffer,0,sizeof(rx_buffer));
- }
- //入口函数-电机控制及
- 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
- rt_uint8_t num_tg_open = 200; //20S
- rt_uint8_t num_tg_init = 200;
- static rt_uint16_t mock_uart_timer2_cnt = 0;
- while(1)
- {
- /* uart4 接收位自愈:任何路径误洗(控制台切换等)后 100ms 内自动补回,
- * 保证屏幕始终可接收(屏幕失灵时无法靠屏幕指令恢复) */
- hmi_uart4_fixup_rx();
- #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);
- if(rep){
- DTU_Write("%s",rep);
- free(rep);
- }
- rt_thread_delay(1000);
- }
- #endif
- if(device_status.mock_uart2_timer_flag==1)
- {
- mock_uart_timer2_cnt++;
- if(mock_uart_timer2_cnt>=200){
- device_status.mock_uart2_timer_flag = 0;
- HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
- LOG_I("t_uart2_callback");
- }
- }else{
- mock_uart_timer2_cnt = 0;
- }
- if(device_status.ctr_up_motor==1)
- {
- ctr_cangmen_up(OPEN);
- num_up_open--;
- if (num_up_open == 0)
- {
- ctr_cangmen_up(OFF);
- 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)
- {
- ctr_cangmen_up(OFF);
- 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 (num_down_open == 0)
- {
- ctr_cangmen_down(OFF);
- 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)
- {
- ctr_cangmen_down(OFF);
- num_down_close = 200;
- system_status.lower_door_close_count += 1;
- }
- num_down_open = 200;
- }
- /* ---- 屏幕 workSta.t3:当前正在动作的那路仓门电机剩余倒计时(秒) ----
- * 仓门电机每次动作上限 20s(对应 num_*_open/close 从 200 递减到 0),
- * 无动作时清 0;开门/关门方向按 ctr_*_motor(1-开 2-关) 区分。 */
- 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==880)
- {
- system_status.photo_taken_count+=1;
- LOG_I("测试拍照!");
- #if 1
- if(device_status.version_type!=41){
- 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();
- if(device_status.version_type==41){
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- }
- }
- photo_led_sec--;
- /* 屏幕 workSta.t5:拍照结束剩余时间(秒)。整个拍照流程 900→300(100ms 计数)=60s,
- * 到 300 关闭补光灯并结束拍照 */
- device_status.photo_left = (photo_led_sec > 300) ? (uint16_t)((photo_led_sec - 300) / 10) : 0;
- if(photo_led_sec==300) //
- {
- ctr_led(OFF);
- photo_led_sec = 900;
- device_status.photo_ws = 0;
- device_status.photo_left = 0;
- if(device_status.test_flag==0){
- device_status.clear_status = 1; //20260522
- }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.collt_box_sta==111){
- ctr_Fan_IN(ON); //推杆电机收缩(供电打开,复位)
- num_tg_init--;
- if(num_tg_init%10==0)
- HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
- if(num_tg_init==0){
- HMI_WRITE("sysTest.result.txt=\"排虫复位完成\"");
- ctr_Fan_PUT(ON);
- num_tg_init = 200;
- device_status.collt_box_sta=112;
- }
- }else if(device_status.collt_box_sta==112){
- num_tg_open--;
- if(num_tg_open%10==0)
- HMI_WRITE("sysTest.result.txt=\"开始执行排虫:%d\"",num_tg_open/10);
- if(num_tg_open==0){
- HMI_WRITE("sysTest.result.txt=\"排虫复位中\"");
- ctr_Fan_PUT(OFF);
- num_tg_open = 200;
- device_status.collt_box_sta=113;
- }
- }else if(device_status.collt_box_sta==113){
- num_tg_init--;
- if(num_tg_init%10==0)
- HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
- if(num_tg_init==0){
- HMI_WRITE("sysTest.result.txt=\"排虫机执行已完成!\"");
- ctr_Fan_PUT(OFF);
- ctr_Fan_IN(OFF);
- num_tg_init = 200;
- device_status.collt_box_sta=0;
- }
- }
- if(device_status.run_sta==111){ //排虫-收缩
- ctr_Fan_IN(ON); //推杆电机收缩(供电打开,复位)
- ctr_Fan_PUT(OFF);
- num_tg_init--;
- if(num_tg_init%10==0)
- HMI_WRITE("sysTest.result.txt=\"排虫复位中:%d\"",num_tg_init/10);
- if(num_tg_init==0){
- HMI_WRITE("sysTest.result.txt=\"排虫复位完成\"");
- ctr_Fan_IN(OFF);
- num_tg_init = 200;
- device_status.run_sta=0;
- }
- }else if(device_status.run_sta==112){ //收虫-推出
- ctr_Fan_IN(ON);
- ctr_Fan_PUT(ON);
- num_tg_open--;
- if(num_tg_open%10==0)
- HMI_WRITE("sysTest.result.txt=\"开始执行收虫:%d\"",num_tg_open/10);
- if(num_tg_open==0){
- HMI_WRITE("sysTest.result.txt=\"收虫中...\"");
- ctr_Fan_PUT(OFF);
- ctr_Fan_IN(OFF);
- num_tg_open = 200;
- device_status.run_sta=0;
- }
- }
- /* 屏幕刷新重复过滤表:到点(每 HMI_DEDUPE_RESYNC_SEC 秒)整体复位一次,
- * 保证屏幕重启/换页后最迟该周期内恢复全量刷新(详见 HMI_WRITE 处说明) */
- hmi_dedupe_tick();
- rt_thread_delay(100);
- }
- }
- /*屏幕写数据*/
- int Hmi_Fresh(void)
- {
- //初始化串口4
- 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_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)
- {
- /* 2026-10-29:开机参数加载耗时打点起点。终点在下方"参数加载完成"处,
- * 不能放在函数末尾——末尾还有等待联网循环(最长3分钟)与 dtu_data_play()。 */
- rt_tick_t load_t0 = rt_tick_get();
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- if(easyflash_init()==EF_NO_ERR) //初始化成功
- {
- ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
- ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
- /* 2026-10-29:原实现每次开机都写一条 reboot_time —— EasyFlash 是追加式 KV,
- * 每次写占一条记录并推动扇区轮换/GC,属持续写放大。开机次数对精度要求不高,
- * 改为每 10 次落盘一次(落盘值 1,11,21…,断电最多倒退 9 次计数)。 */
- device_status.rebootcnt++;
- if ((device_status.rebootcnt % 10) == 1)
- {
- ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
- }
- LOG_I("rebootcnt:%d",device_status.rebootcnt);
- LOG_I("iSave:%d",device_status.iSave);
- }
- else {
- LOG_E("easyflash init ERRPR!!!");
- }
- /* If iSave was lost (EF has no record of it, value stays 0), check the
- * mirror before deciding to apply defaults. A valid mirror means the
- * device was configured before — recover from there instead of blowing
- * all params back to factory defaults. */
- if (device_status.iSave != FLASH_FLAG)
- {
- param_payload_t boot_mirror;
- if (param_mirror_load(&boot_mirror) == RT_EOK)
- {
- LOG_W("iSave lost but mirror is valid (seq=%u), recovering",
- boot_mirror.write_seq);
- mirror_to_device_status(&boot_mirror);
- /* iSave is now FLASH_FLAG; set iFlag=0 to match the normal
- * "load user config" path semantics. */
- device_status.iFlag = 0;
- }
- }
- //保存默认配置
- 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; //震动开关,默认开
- device_status.shake_sec = 1; //震动时间,s
- device_status.vol = 7; //音量
- device_status.timeSwitch = 0; //工作模式:光控
- device_status.passWord = 8888; //屏幕密码
- device_status.dattim = 10; //数据上传间隔
- device_status.ds = 1; //设备状态
- device_status.heat_sw =0;
- device_status.cul_time = 1;
- device_status.run_sta = 0;
- device_status.handle_bugger = 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");
- 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("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));
- 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);
- /* Seed the mirror with the same defaults we just wrote to EF.
- * After this point, all EF writes below also sync to the mirror. */
- param_mirror_sync();
- }//从easyflash读取参数配置
- else if (device_status.iSave==FLASH_FLAG) {
- /* Try to recover from the mirror first. If the mirror has a valid
- * copy, prefer it over single-key EF lookups: a single dropped EF
- * entry no longer causes a stale default to surface. Fields that
- * are NOT in the mirror (imei, iccid, deviceId, json_w_*, driver_bl,
- * platform, env_t/h) are still fetched from EF below. */
- device_status.iFlag = 0;
- param_payload_t mirror;
- if (param_mirror_load(&mirror) == RT_EOK)
- {
- LOG_I("param mirror loaded (seq=%u)", mirror.write_seq);
- mirror_to_device_status(&mirror);
- }
- else
- {
- LOG_W("param mirror unavailable, falling back to EF single-key");
- }
- 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); //加热时间
- 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); //环境湿度
- 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("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); //排虫
- device_status.light_over_tim = device_status.sunBar * 3600; //光控计数
- device_status.work_done = 0;
- if(device_status.dattim==0 ||
- device_status.thsp==0||
- device_status.sunBar==0||
- device_status.tph==0){
- device_status.iFlag = 1;
- }
- //丢参数时的异常处理
- if(device_status.dattim==0) device_status.dattim = 10;
- if(device_status.thsp==0) device_status.thsp = 300;
- if(device_status.sunBar==0) device_status.sunBar = 4;
- if(device_status.tph==0) device_status.tph = 70;
- 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;
- /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
- if(device_status.dtype!=7)
- {
- device_status.dtype = 3;
- }
- else {
- device_status.version_type = 7;
- }
- if(device_status.version_type==0)
- {
- app_partion_backup(); //固件备份至df_area
- device_status.version_type = 41;
- rt_sprintf(device_status.driver, "%s",VERSION_HK);
- 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);
- }
- else if (device_status.version_type==6)
- {
- //打开相机供电;
- ctr_Camera_Power(ON);
- rt_sprintf(device_status.driver, "%s",VERSION_XCT);
- }
- else if (device_status.version_type==7) {
- rt_sprintf(device_status.driver, "%s",VERSION_BZY);
- //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
- if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
- {
- device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
- }
- }
- /*定制三色光源*/
- #if LAMP_EN == 1
- /* 三灯管模式下工作时长由 Lamp_Cycle(每只灯管轮换 LAMP_ROTATE_SEC)决定,
- * sunBar 不再限制实际工作时间,仅按参数记录,不再强制改写为3 */
- // device_status.timeSwitch = 1;
- // device_status.startBar = 20;
- // device_status.stopBar = 23;
- #endif
- 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.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("PowerType:%d",device_status.Power_Type);
- LOG_I("deviceId:%s",device_status.deviceId);
- LOG_I("handle_bugger:%d",device_status.handle_bugger);
- LOG_W("heat_sw加热开关:%d",device_status.heat_sw);
- }
- /* ===== 开机 Flash 参数加载耗时打点(2026-10-29 新增)=====
- * 终点必须落在"参数读取/默认值写入"结束处:下方还有等待联网循环(最长 3 分钟)
- * 与 dtu_data_play(),若放到函数末尾,测到的将是联网等待时间而非参数加载时间。
- * 口径(本机 RT_TICK_PER_SECOND=1000,1 tick = 1ms):
- * boot_param_ms = rt_tick 起点(板级初始化) → 参数加载完成,含前段全部 INIT_EXPORT,
- * 即上报字段 bootlt(对"开机慢"最直观);
- * param_load_ms = 进入本函数 → 参数加载完成,即 Flash 读取/默认值写入自身耗时。 */
- {
- rt_tick_t load_t1 = rt_tick_get();
- ef_boot_diag_t efdiag;
- #if RT_TICK_PER_SECOND == 1000
- device_status.boot_param_ms = (rt_uint32_t)load_t1;
- device_status.param_load_ms = (rt_uint32_t)(load_t1 - load_t0);
- #else
- device_status.boot_param_ms = (rt_uint32_t)(((rt_uint64_t)load_t1 * 1000UL) / RT_TICK_PER_SECOND);
- device_status.param_load_ms = (rt_uint32_t)(((rt_uint64_t)(load_t1 - load_t0) * 1000UL) / RT_TICK_PER_SECOND);
- #endif
- /* EasyFlash 重建诊断(2026-10-29):把原先"静默格式化扇区"的行为变成可观测指标。
- * 扇区头非法 → ef_env.c 的 check_sec_hdr_cb() 会当场擦 4KB 并重写头,是开机
- * 参数加载慢的直接原因(详见 doc/readme.md 同日条目)。 */
- ef_env_get_boot_diag(&efdiag);
- device_status.ef_fmt_cnt = (efdiag.format_cnt > 0xFFFFU) ? 0xFFFFU : (uint16_t)efdiag.format_cnt;
- device_status.ef_fmt_fail_cnt = (efdiag.format_fail_cnt > 0xFFFFU) ? 0xFFFFU : (uint16_t)efdiag.format_fail_cnt;
- LOG_I("开机参数加载耗时: boot=%lu ms(含前段初始化), load=%lu ms, EF重建扇区:%u 失败:%u",
- (unsigned long)device_status.boot_param_ms,
- (unsigned long)device_status.param_load_ms,
- (unsigned)device_status.ef_fmt_cnt,
- (unsigned)device_status.ef_fmt_fail_cnt);
- if (device_status.boot_param_ms > BOOT_PARAM_SLOW_MS)
- {
- LOG_W("开机参数加载超时(%lu ms > %lu ms),待上报预警",
- (unsigned long)device_status.boot_param_ms,
- (unsigned long)BOOT_PARAM_SLOW_MS);
- }
- if (device_status.ef_fmt_fail_cnt != 0)
- {
- LOG_W("EF 扇区擦写失败(%u 个),擦不干净会在每次开机重复格式化,待上报预警",
- (unsigned)device_status.ef_fmt_fail_cnt);
- }
- }
- {
- device_status.update_flag = 0;
- //初始化联网状态
- rt_sprintf(device_status.net,"1");
- rt_sprintf(device_status.csq,"0");
- rt_sprintf(device_status.sim,"1");
- ctr_router(ON);
- Hmi_Fresh();
- #if Color_EN == 1
- Control_clear();
- Control_heat();
- #endif
- if(device_status.dtype!=7)
- {
- Control_clear();
- Control_heat();
- }
- dtu_data_play();
- sht30_collect();
- #if SENSOR_EN==1
- mb_master_samlpe();
- #endif
- #if ROBOT_EN == 0
- uint16_t connect_num=0;
- //等待联网,
- while(atoi(device_status.net)==1)
- {
- if(device_status.dtype==7)
- HMI_WRITE("sysTest.auto_flag.txt=\"0\"");
- char *readinfo = makeJson_ReadInfo();
- DTU_Write("%s",readinfo);
- rt_free(readinfo);
- rt_thread_delay(1000);
- connect_num++;
- LOG_I("联网中......%d",connect_num);
- //超时时间为3分钟
- if(connect_num==180)
- {
- device_status.timeSwitch = 0;
- LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
- #if 0
- set_date(2020, 1, 1);
- set_time(0, 0, 0);
- #endif
- break;
- }
- if(tm.tm_year>=2021)
- break;
- }
- connect_num = 0;
- while(atoi(device_status.net)==0)
- {
- char *readinfo = makeJson_ReadInfo();
- DTU_Write("%s",readinfo);
- rt_free(readinfo);
- rt_thread_delay(2000);
- connect_num++;
- if(connect_num>5) break;
- }
- if(device_status.version_type==6) device_status.timeSwitch = 1;
- {
- /* 修复:原实现直传 makeJson_Warn 返回值,每次开机泄漏一份 cJSON 缓冲 */
- char *turnon_json = makeJson_Warn("turnon", "device_stm32");
- if (turnon_json != RT_NULL)
- {
- DTU_Write("%s",turnon_json);
- rt_free(turnon_json);
- }
- }
- rt_thread_delay(1000);
- dtu_send_data_start();
- #endif
- }
- }
- /* device_status_to_mirror / mirror_to_device_status / param_mirror_sync
- * have been moved to param_mirror.c so any translation unit can call
- * param_mirror_sync() without needing internal hmi.c linkage. */
- void ResetParamDefault(void)
- {
- rt_sprintf(device_status.tempture,"%d",-15);
- 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;
- device_status.heat_sw = 1;
- ef_set_env_blob("heat_sw", &device_status.heat_sw, sizeof(device_status.heat_sw));
- 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; //收集时间
- #if LAMP_EN == 0
- device_status.startBar = 20; //定时开始时间
- device_status.stopBar = 5; //定时结束时间
- #endif
- device_status.heattime = 8; //加热时间
- device_status.shake_ws = 1; //针对开关
- device_status.shake_sec = 1; //震动时间1-100ms
- #if LAMP_EN == 1
- device_status.sunBar = 3; //光控定时时长
- #else
- device_status.sunBar = 8;
- #endif
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = 5; //低温保护阈值
- device_status.timeSwitch = 0; //定时模式:光控
- device_status.heatvalue=100; //加热温度
- 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));
- param_mirror_sync();
- 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; //数据上传间隔
- #if LAMP_EN == 0
- device_status.startBar = 20; //定时开始时间
- device_status.stopBar = 0; //定时结束时间
- #endif
- device_status.heattime = 8; //加热时间
- device_status.shake_ws = 1; //针对开关
- device_status.shake_sec = 1; //震动时间1-100ms
- #if LAMP_EN == 1
- device_status.sunBar = 3; //光控定时时长
- #else
- device_status.sunBar = 4;
- #endif
- 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("heat_sw",&device_status.heat_sw,sizeof(device_status.heat_sw)); //上传时间
- 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));
- param_mirror_sync();
- 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;
- 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);
- }
- }
- }
- int delete_hmi_thread(void)
- {
- int32_t try_cnt;
- LOG_W("delete_hmi_thread.");
- /* 安全删除:两线程平时阻塞在收消息/延时上,若恰好处于 IPC 等待则稍后重试,
- * 避免 rt_thread_delete 造成 IPC 等待链表野指针(见 control.c 说明)。
- * 删除成功后 thread_delete_safe 会把句柄置空,便于后续按需重建。 */
- for (try_cnt = 0; try_cnt < 30; try_cnt++)
- {
- if (thread_delete_safe(&ctr_motor) == 0 && thread_delete_safe(&hmi1) == 0)
- {
- break;
- }
- rt_thread_mdelay(50);
- }
- return 0;
- }
- void Debug_log_ON(void)
- {
- hmi_uart4_fixup_rx(); /* 先记录 uart4 原始接收位(切换前) */
- rt_console_set_device("uart4");
- finsh_set_device("uart4");
- /* 上述 open 会把 uart4 的 DMA_RX 接收位洗掉(serial.c:593),
- * 不补回则屏幕接收失效("屏幕点不动")——立即补回 */
- hmi_uart4_fixup_rx();
- }
- void Debug_log_OFF(void)
- {
- rt_console_set_device("uart1");
- finsh_set_device("uart1");
- /* 同族加固(2026-10-13):rt_console_set_device 恢复时的 open 会按
- * `open_flag = oflag & 0xff` 洗掉 uart1 的 INT_RX 位,而 finsh 在
- * "uart1 已是 shell 设备"时直接 return 不补位 → 控制台切回后本机口无法输入。
- * 统一由 dbg_chain_fixup() 把接收模式位补回(详见该函数注释)。 */
- dbg_chain_fixup();
- /* uart4(屏幕口)接收位同样在调试打开/关闭链路中被洗过,这里一并补回 */
- hmi_uart4_fixup_rx();
- /* 线程已存在时不再重复创建(原实现每次调用都会新建线程且不回收,造成线程泄漏) */
- if (hmi1 == RT_NULL)
- {
- rt_mq_init(&rx_mq, "rx_mq",
- msg_pool, /* 存放消息的缓冲区 */
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- 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);
- 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);
|