TestPage.qml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. import QtQuick
  2. import Felgo
  3. import QtMultimedia
  4. import QtQuick.Controls
  5. // import QtQuick.Controls.Material 2.0 // 移除:与Felgo主题冲突,导致AppSwitch颜色异常
  6. import "../controls"
  7. import "../common"
  8. import Frames 0.1
  9. AppPage {
  10. objectName: "autoFocus_Page"
  11. navigationBarHidden: true
  12. backgroundColor: app.theme_backgroundColor
  13. property var videoFormats
  14. // 定义电机状态
  15. property int currentAxis: 0 // 当前选择的轴 (0: X, 1: Y, 2: Z)
  16. property var axisPositions: [0, 0, 0] // 各轴当前位置
  17. property int speed: 50 // 当前速度 (0-100)
  18. property int stepDistance: 20 // 步进脉冲数量
  19. property string fExposureTime: "0"
  20. function autotake(sw){
  21. if(sw)
  22. {
  23. console.log("autotakestart")
  24. frames.camera_sw = true
  25. //frames.auto_clicked = true //开始对焦
  26. auto_timer.start() //启动对焦定时器
  27. console.log("相机已打开")
  28. photoTimer.interval=60*1000
  29. photoTimer.start()
  30. }else{
  31. frames.camera_sw = true
  32. console.log("打开相机单次拍照!")
  33. }
  34. }
  35. Frames {
  36. objectName: "Frames"
  37. id: frames
  38. videoSink: output.videoSink
  39. onCameraOpenFailed:function(err){
  40. console.log("onCameraOpenFailed",err)
  41. nativeUtils.displayMessageBox("相机打开失败!",err,1)
  42. focus_switch.checked = false
  43. focus_switch.enabled = true
  44. takephoto_switch.checked = false
  45. takephoto_switch.enabled = true
  46. }
  47. onVideoSinkChanged: {
  48. console.log("onVideoSinkChanged")
  49. photoTimer.start() //两秒后关闭相机,因为相机加载的慢,否则关不住
  50. }
  51. onQulityStringChanged: {
  52. loginpage.logintimeout = 600; //拍照刷新熄屏计时
  53. photoTimer.interval = 180*1000 //3分钟超时
  54. photoTimer.restart()
  55. }
  56. onCameraStaChanged: function(sta_cam) {
  57. if(sta_cam){
  58. //camera.active = sta_cam
  59. //camera.start()
  60. loginpage.logintimeout = 600; //拍照刷新熄屏计时
  61. output.visible = sta_cam
  62. photoTimer.interval = 180*1000 //3分钟超时
  63. photoTimer.start()
  64. //uvc_camera.visible = false
  65. }else{
  66. //camera.active = sta_cam
  67. //camera.stop()
  68. output.visible= sta_cam
  69. photoTimer.stop() //停止计时器
  70. //uvc_camera.visible = true
  71. //关闭相机重置按钮状态
  72. focus_switch.enabled = true
  73. focus_switch.checked = false
  74. takephoto_switch.checked = false
  75. takephoto_switch.enabled = true
  76. }
  77. console.log("相机状态改变",sta_cam)
  78. }
  79. onStepCountChanged: function(step){
  80. console.log("坐标改变",step)
  81. step_apptextfield.text = step
  82. }
  83. onExposureTimeChanged: {
  84. fExposureTime = frames.cam_ExposureTime
  85. }
  86. }
  87. Timer {
  88. id: photoTimer
  89. interval: 2000 // 设置点击频率限制的时间间隔(以毫秒为单位)
  90. running: false // 初始状态下定时器停止
  91. repeat: false // 设置为false,以便只执行一次
  92. onTriggered: {
  93. // 定时器触发后,设置按钮可用
  94. // camera.active = false
  95. // camera.stop()
  96. output.visible = false
  97. frames.camera_sw = false
  98. }
  99. }
  100. Timer {
  101. id: auto_timer
  102. interval: 1000 // 设置点击频率限制的时间间隔(以毫秒为单位)
  103. running: false // 初始状态下定时器停止
  104. repeat: false // 设置为false,以便只执行一次
  105. onTriggered: {
  106. // 定时器触发后,开始对焦
  107. frames.auto_clicked = true
  108. }
  109. }
  110. Connections{
  111. target: mqtt
  112. function onMessagePub(message) {
  113. if(app.showPubData){
  114. var time = new Date().toLocaleTimeString()
  115. app.txDataLog = "[" + time + "] " + message + "\n" + app.txDataLog
  116. if(app.txDataLog.length > 5000) app.txDataLog = app.txDataLog.substring(0, 5000)
  117. }
  118. }
  119. function onMessageSub(message) {
  120. if(app.showSubData){
  121. var time = new Date().toLocaleTimeString()
  122. app.rxDataLog = "[" + time + "] " + message + "\n" + app.rxDataLog
  123. if(app.rxDataLog.length > 5000) app.rxDataLog = app.rxDataLog.substring(0, 5000)
  124. }
  125. }
  126. }
  127. Connections {
  128. target: serial
  129. function onTakephotodone() {
  130. console.log("立即拍照完成!")
  131. apptext_res.text = "立即拍照完成!"
  132. focus_switch.enabled = true
  133. focus_switch.checked = false
  134. takephoto_switch.checked = false
  135. takephoto_switch.enabled = true
  136. }
  137. function onAutotestdone() {
  138. console.log("自动对焦完成!")
  139. apptext_res.text = "自动对焦完成!"
  140. autotest_switch.enabled = true
  141. autotest_switch.checked = false
  142. }
  143. onTranslatedone:function() {
  144. console.log("胶带传送完成!")
  145. apptext_res.text = "胶带传送完成!"
  146. jiaodai_switch.enabled = true
  147. jiaodai_switch.checked = false
  148. }
  149. onDataReceived:function(data) {
  150. if(app.showRxData){
  151. var time = new Date().toLocaleTimeString()
  152. app.rxDataLog = "[" + time + "] " + data + "\n" + app.rxDataLog
  153. if(app.rxDataLog.length > 5000) app.rxDataLog = app.rxDataLog.substring(0, 5000)
  154. }
  155. }
  156. onNetMessage:function(data) {
  157. if(app.showTxData){
  158. var time = new Date().toLocaleTimeString()
  159. app.txDataLog = "[" + time + "] " + data + "\n" + app.txDataLog
  160. if(app.txDataLog.length > 5000) app.txDataLog = app.txDataLog.substring(0, 5000)
  161. }
  162. }
  163. }
  164. SplitView{
  165. anchors.fill: parent
  166. orientation: Qt.Horizontal
  167. Rectangle {
  168. SplitView.preferredWidth: parent.width * 0.7
  169. height: parent.height
  170. color: app.cardColor
  171. radius: 5
  172. AppButton {
  173. id: normButton
  174. //anchors.horizontalCenter: parent.horizontalCenter
  175. anchors.centerIn: parent
  176. anchors.fill: parent
  177. //text: "打开相机"
  178. flat: true //边框是否可视
  179. onClicked: {
  180. console.log("相机状态:",frames.camera_sw)
  181. //if(camera.active){
  182. if(frames.camera_sw){
  183. text = "相机已关闭"
  184. output.visible = false
  185. frames.camera_sw = false
  186. //uvc_camera.visible = false
  187. }else{
  188. text = "打开相机"
  189. output.visible = true
  190. frames.camera_sw = true
  191. //uvc_camera.visible = true
  192. }
  193. }
  194. AppImage{
  195. id: image_picdis
  196. anchors.centerIn: parent
  197. source: "../../assets/photoico.svg"
  198. }
  199. }
  200. VideoOutput{
  201. id: output
  202. anchors.fill: parent
  203. visible: false
  204. fillMode: VideoOutput.PreserveAspectFit //根据视频的纵横比例等比例缩放
  205. //fillMode: VideoOutput.Stretch
  206. Component.onCompleted: {
  207. console.log("output onCompleted")
  208. }
  209. }
  210. Row {
  211. AppText {
  212. id: photoclarity_text
  213. width: parent.width
  214. text: frames.fpsString
  215. color: "red"
  216. }
  217. AppText {
  218. visible: true//app.photoclarity
  219. id: photoclarity2_text
  220. anchors.top: photoclarity_text.bottom
  221. width: parent.width
  222. text: frames.qulityString
  223. color: "green"
  224. }
  225. }
  226. }
  227. ScrollView
  228. {
  229. SplitView.preferredWidth: parent.width*0.29
  230. SplitView.minimumWidth: 200
  231. clip: true
  232. Column {
  233. width: parent.width
  234. spacing: dp(15)
  235. Row {
  236. spacing: 80
  237. AppText {
  238. id: text_title
  239. text: "测试调试"
  240. font.bold: true
  241. color: "#28a779"
  242. fontSize: 20
  243. }
  244. AppText {
  245. id: apptext_res
  246. text: "[测试结果]"
  247. fontSize:15
  248. color: "grey"
  249. anchors.verticalCenter: text_title.verticalCenter
  250. }
  251. }
  252. Row {
  253. spacing: 100
  254. AppText{
  255. text: "一键测试"
  256. fontSize: 15
  257. color: app.fontColor
  258. //font.bold: true
  259. anchors.verticalCenter: autotest_switch.verticalCenter
  260. }
  261. AppSwitch {
  262. id: autotest_switch
  263. onToggled: {
  264. if(autotest_switch.checked==true){
  265. autotest_switch.enabled = false
  266. serial.sendCmd("autotest",true,"","");
  267. t_autotest.start()
  268. }else{
  269. console.log("t_autotest 计数:",t_autotest.interval)
  270. }
  271. }
  272. }
  273. Timer {
  274. id: t_autotest
  275. interval: 1000*60
  276. running: false
  277. repeat: false
  278. onTriggered: {
  279. autotest_switch.enabled = true
  280. autotest_switch.checked = false
  281. }
  282. }
  283. }
  284. Row {
  285. spacing: 100
  286. AppText{
  287. text: "风机测试"
  288. color: app.fontColor
  289. fontSize: 15
  290. //font.bold: true
  291. anchors.verticalCenter: fantest_switch.verticalCenter
  292. }
  293. AppSwitch {
  294. id: fantest_switch
  295. onToggled: {
  296. if(fantest_switch.checked==true){
  297. serial.sendCmd("fan",true,"","");
  298. apptext_res.text = "打开风机"
  299. }else{
  300. serial.sendCmd("fan",false,"","");
  301. apptext_res.text = "关闭风机"
  302. }
  303. }
  304. }
  305. }
  306. Row {
  307. spacing: 100
  308. AppText{
  309. text: "胶带测试"
  310. fontSize: 15
  311. color: app.fontColor
  312. //font.bold: true
  313. anchors.verticalCenter: jiaodai_switch.verticalCenter
  314. }
  315. AppSwitch {
  316. id: jiaodai_switch
  317. onToggled: {
  318. if(jiaodai_switch.checked==true){
  319. // 检查是否处于温度保护状态
  320. if(app.tempProtectionState === true){
  321. // 弹出提示对话框
  322. nativeUtils.displayMessageBox(
  323. "提示",
  324. "当前处于温控状态,请解除温控后再进行测试胶带传送",
  325. 1
  326. );
  327. // 恢复开关状态为未选中
  328. jiaodai_switch.checked = false;
  329. return;
  330. }
  331. // 正常执行胶带测试
  332. //jiaodai_switch.enabled = false
  333. apptext_res.text = "胶带测试开始"
  334. serial.sendCmd("jiaodai",true,"","");
  335. }
  336. }
  337. }
  338. }
  339. Row {
  340. spacing: 100
  341. AppText{
  342. text: "补光测试"
  343. color: app.fontColor
  344. fontSize: 15
  345. //font.bold: true
  346. anchors.verticalCenter: led_switch.verticalCenter
  347. }
  348. AppSwitch {
  349. id: led_switch
  350. onToggled: {
  351. if(led_switch.checked==true){
  352. serial.sendCmd("led",true,"","");
  353. apptext_res.text = "补光灯已打开"
  354. }else{
  355. serial.sendCmd("led",false,"","");
  356. apptext_res.text = "补光灯已关闭"
  357. }
  358. }
  359. }
  360. }
  361. Row {
  362. spacing: 100
  363. AppText{
  364. text: "立即拍照"
  365. color: app.fontColor
  366. fontSize: 15
  367. //font.bold: true
  368. anchors.verticalCenter: takephoto_switch.verticalCenter
  369. }
  370. AppSwitch {
  371. id: takephoto_switch
  372. onToggled: {
  373. if(takephoto_switch.checked==true){
  374. apptext_res.text = "立即拍照"
  375. frames.captureImage()
  376. }
  377. }
  378. }
  379. }
  380. Row {
  381. spacing: 100
  382. AppText{
  383. text: "对焦拍照"
  384. color: app.fontColor
  385. fontSize: 15
  386. //font.bold: true
  387. anchors.verticalCenter: focus_switch.verticalCenter
  388. }
  389. AppSwitch {
  390. id: focus_switch
  391. onToggled: {
  392. if(focus_switch.checked==true){
  393. apptext_res.text = "开始对焦"
  394. focus_switch.enabled = false
  395. serial.auto_takephoto(true)
  396. }
  397. }
  398. }
  399. }
  400. AppText {
  401. id: text_title2
  402. text: "步进电机控制"
  403. font.bold: true
  404. color: "#28a779"
  405. fontSize: 20
  406. }
  407. // 轴选择
  408. Row {
  409. spacing: dp(1)
  410. width: parent.width
  411. AppText {
  412. text: "选择轴:"
  413. width: dp(60) // 固定文本宽度
  414. anchors.verticalCenter: parent.verticalCenter
  415. }
  416. ComboBox {
  417. width: dp(100) // 剩余宽度
  418. height: dp(40)
  419. model: ["Y轴"]
  420. onCurrentIndexChanged: currentAxis = currentIndex
  421. }
  422. AppButton{
  423. text: "校准"
  424. textColor: "black"
  425. flat: false
  426. borderColor: "black"
  427. borderWidth: dp(1)
  428. backgroundColor: "white"
  429. minimumHeight: dp(30)
  430. minimumWidth: dp(50)
  431. onClicked:
  432. {
  433. autotake(false)
  434. if(currentAxis==0)
  435. {
  436. console.log("校准Y")
  437. serial.sendCmd("calibrate",false,"y",speed.toString())
  438. }
  439. }
  440. }
  441. }
  442. // 速度调节
  443. Row {
  444. spacing: dp(2)
  445. width: parent.width
  446. AppText {
  447. text: "速度:"
  448. width: dp(40) // 固定文本宽度
  449. anchors.verticalCenter: parent.verticalCenter
  450. }
  451. AppSlider {
  452. width: parent.width - dp(60) // 剩余宽度
  453. from: 0
  454. to: 100
  455. value: speed
  456. stepSize: 1
  457. onValueChanged: speed = value
  458. anchors.verticalCenter: parent.verticalCenter
  459. }
  460. AppText {
  461. text: speed
  462. width: dp(40) // 固定文本宽度
  463. anchors.verticalCenter: parent.verticalCenter
  464. }
  465. }
  466. // 步进距离设置
  467. Row {
  468. spacing: dp(10)
  469. width: parent.width
  470. AppText {
  471. text: "步进脉冲次数:"
  472. width: dp(120) // 固定文本宽度
  473. anchors.verticalCenter: parent.verticalCenter
  474. }
  475. AppTextField {
  476. id: step_apptextfield
  477. width: parent.width - dp(120) // 剩余宽度
  478. text: stepDistance
  479. validator: IntValidator { bottom: 0; top: 2000 }
  480. onTextChanged: stepDistance = parseInt(text)
  481. }
  482. }
  483. // 运动控制按钮
  484. Row {
  485. spacing: dp(10)
  486. width: parent.width
  487. AppButton {
  488. text: " 正转 "
  489. textColor: "black"
  490. flat: false
  491. borderColor: "black"
  492. borderWidth: dp(1)
  493. backgroundColor: "grey"
  494. minimumHeight: dp(30)
  495. minimumWidth: dp(50)
  496. width: (parent.width - dp(30))/3 // 平均分配宽度
  497. onClicked:
  498. {
  499. if(currentAxis==0)
  500. {
  501. console.log("Y轴正转:",stepDistance)
  502. serial.sendCmd("movy",false,"f"+stepDistance,speed.toString())
  503. }
  504. }
  505. }
  506. AppButton {
  507. text: " 停止 "
  508. textColor: "black"
  509. flat: false
  510. borderColor: "black"
  511. borderWidth: dp(1)
  512. backgroundColor: "red"
  513. minimumHeight: dp(30)
  514. minimumWidth: dp(50)
  515. width: (parent.width - dp(30))/3 // 平均分配宽度
  516. onClicked:
  517. {
  518. console.log("停止")
  519. serial.sendCmd("movy",false,"f0","")
  520. }
  521. }
  522. AppButton {
  523. text: " 反转 "
  524. textColor: "black"
  525. flat: false
  526. borderColor: "black"
  527. borderWidth: dp(1)
  528. backgroundColor: "grey"
  529. minimumHeight: dp(30)
  530. minimumWidth: dp(50)
  531. width: (parent.width - dp(30))/3 // 平均分配宽度
  532. onClicked:
  533. {
  534. if(currentAxis==0)
  535. {
  536. console.log("Y轴反转:",stepDistance)
  537. serial.sendCmd("movy",false,"b"+stepDistance,speed.toString())
  538. }
  539. }
  540. }
  541. AppButton {
  542. text: " 复位 "
  543. textColor: "black"
  544. flat: false
  545. borderColor: "black"
  546. borderWidth: dp(1)
  547. backgroundColor: "grey"
  548. minimumHeight: dp(30)
  549. minimumWidth: dp(50)
  550. width: (parent.width - dp(30))/3 // 平均分配宽度
  551. onClicked: {
  552. if(currentAxis==0)
  553. {
  554. console.log("Y轴复位!")
  555. serial.sendCmd("movy",true,"",speed.toString())
  556. }
  557. }
  558. }
  559. }
  560. AppText {
  561. id: text_camera
  562. text: "相机参数"
  563. font.bold: true
  564. color: "#28a779"
  565. fontSize: 20
  566. }
  567. Row {
  568. spacing: dp(10)
  569. width: parent.width
  570. AppText {
  571. id: txt_fExposureTime
  572. text: "曝光值:"
  573. }
  574. AppTextField {
  575. anchors.verticalCenter: txt_fExposureTime.verticalCenter
  576. placeholderText: fExposureTime
  577. text: fExposureTime
  578. width: parent.width - dp(10) // 剩余宽度
  579. EnterKey.type: Qt.EnterKeyDone
  580. wrapMode: Text.NoWrap
  581. inputMethodHints: Qt.ImhFormattedNumbersOnly |
  582. Qt.ImhPreferDone |
  583. Qt.ImhNoPredictiveText
  584. validator: DoubleValidator {
  585. bottom: 100.00 // 最小值
  586. top: 500000.00 // 最大值
  587. decimals: 0
  588. }
  589. // 🔥 关键 3:兜底(IME 再作妖也不怕)
  590. Keys.onReturnPressed: {
  591. frames.cam_ExposureTime = text
  592. console.log("设置曝光时间:",text)
  593. Qt.inputMethod.hide()
  594. }
  595. onAccepted: {
  596. frames.cam_ExposureTime = text
  597. console.log("设置曝光时间",text)
  598. }
  599. }
  600. }
  601. Row {
  602. spacing: 100
  603. AppText{
  604. text: "显示Serial发送数据"
  605. color: app.fontColor
  606. fontSize: 15
  607. anchors.verticalCenter: txSwitch.verticalCenter
  608. }
  609. AppSwitch {
  610. id: txSwitch
  611. onToggled: {
  612. app.floatPanel = checked
  613. app.showTxData = checked
  614. app.floatPanel_title = "Serial发送数据"
  615. }
  616. }
  617. }
  618. Row {
  619. spacing: 100
  620. AppText{
  621. text: "显示Serial接收数据"
  622. color: app.fontColor
  623. fontSize: 15
  624. anchors.verticalCenter: rxSwitch.verticalCenter
  625. }
  626. AppSwitch {
  627. id: rxSwitch
  628. onToggled: {
  629. app.floatPanel = checked
  630. app.showRxData = checked
  631. app.floatPanel_title = "Serial接收数据"
  632. }
  633. }
  634. }
  635. Row {
  636. spacing: 100
  637. AppText{
  638. text: "显示MQTT发送数据"
  639. color: app.fontColor
  640. fontSize: 15
  641. anchors.verticalCenter: txSwitch.verticalCenter
  642. }
  643. AppSwitch {
  644. id: pubSwitch
  645. onToggled: {
  646. app.floatPanel = checked
  647. app.showPubData = checked
  648. app.showSubData = false
  649. app.floatPanel_title = "MQTT发送数据"
  650. }
  651. }
  652. }
  653. Row {
  654. spacing: 100
  655. AppText{
  656. text: "显示MQTT接收数据"
  657. color: app.fontColor
  658. fontSize: 15
  659. anchors.verticalCenter: txSwitch.verticalCenter
  660. }
  661. AppSwitch {
  662. id: subSwitch
  663. onToggled: {
  664. app.floatPanel = checked
  665. app.showSubData = checked
  666. app.showPubData = false
  667. app.floatPanel_title = "MQTT接收数据"
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }