airtf.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. local airtf = {}
  2. dnsproxy = require("dnsproxy")
  3. dhcpsrv = require("dhcpsrv")
  4. httpplus = require("httpplus")
  5. local run_state = false -- 判断本UI DEMO 是否运行
  6. local fail = 0
  7. local sucess = 0
  8. local function fatfs_spi_pin()
  9. return 1, 20 -- Air8000整机开发板上的pin_cs为gpio20,spi_id为1
  10. end
  11. function airtf.run()
  12. log.info("airtf.run")
  13. lcd.setFont(lcd.font_opposansm12_chinese) -- 设置中文字体
  14. run_state = true
  15. -- TF卡操作步骤
  16. local operations = {
  17. "初始化SPI接口",
  18. "挂载TF卡",
  19. "创建目录",
  20. "创建测试文件",
  21. "写入内容",
  22. "读取内容",
  23. "删除文件",
  24. "删除目录",
  25. "卸载TF卡",
  26. "关闭SPI"
  27. }
  28. local operation_index = 1
  29. local operation_status = ""
  30. local spi_id, pin_cs
  31. local tf_mounted = false
  32. local mount_retry_count = 0 -- 挂载重试计数器
  33. local cycle_count = 0 -- 循环次数计数器
  34. while run_state do
  35. sys.wait(10)
  36. lcd.clear(_G.bkcolor)
  37. lcd.drawStr(0, 80, "TF卡测试")
  38. -- 显示当前操作状态
  39. lcd.drawStr(0, 110, operations[operation_index]..": "..operation_status)
  40. -- 显示进度
  41. local progress = math.floor(operation_index * 100 / #operations)
  42. lcd.drawStr(0, 140, "进度: "..progress.."%")
  43. -- 显示循环次数
  44. lcd.drawStr(0, 170, "循环次数: "..cycle_count )
  45. lcd.drawStr(0, 190, "成功次数: "..sucess )
  46. lcd.drawStr(0, 210, "失败次数:".. fail )
  47. -- 显示返回按钮
  48. lcd.showImage(20, 360, "/luadb/back.jpg")
  49. lcd.flush()
  50. -- 执行当前操作
  51. if operation_index == 1 then
  52. -- 初始化SPI接口
  53. spi_id, pin_cs = fatfs_spi_pin()
  54. -- 启用CH390供电(GPIO140)
  55. local ch390_power_ok, ch390_error = pcall(function()
  56. gpio.setup(140, 1, gpio.PULLUP)
  57. sys.wait(500) -- 等待电源稳定
  58. end)
  59. if ch390_power_ok then
  60. operation_status = "CH390供电已启用"
  61. else
  62. operation_status = "CH390供电失败"
  63. fail = fail + 1
  64. operation_index = 1
  65. end
  66. -- 设置SPI
  67. spi.setup(spi_id, nil, 0, 0, pin_cs, 400 * 1000)
  68. gpio.setup(pin_cs, 1)
  69. operation_status = operation_status..",SPI初始化完成"
  70. sys.wait(50)
  71. elseif operation_index == 2 then
  72. -- 挂载TF卡(带重试机制)
  73. mount_retry_count = mount_retry_count + 1
  74. local mount_ok, mount_err = fatfs.mount(fatfs.SPI, "/sd", spi_id, pin_cs, 10 * 1000 * 1000) -- 降低SPI速率
  75. if mount_ok then
  76. tf_mounted = true
  77. operation_status = "挂载成功"
  78. else
  79. operation_status = "挂载失败("..(mount_err or "未知")..")"
  80. fail = fail + 1
  81. operation_index = 1
  82. -- 最多重试3次
  83. if mount_retry_count < 3 then
  84. operation_index = operation_index - 1 -- 重试当前步骤
  85. operation_status = operation_status..",重试中("..mount_retry_count.."/3)"
  86. else
  87. operation_status = operation_status..",已放弃"
  88. end
  89. end
  90. sys.wait(50)
  91. elseif operation_index == 3 then
  92. -- 创建目录
  93. if tf_mounted then
  94. if io.mkdir("/sd/io_test") then
  95. operation_status = "完成"
  96. else
  97. operation_status = "失败"
  98. fail = fail + 1
  99. operation_index = 1
  100. end
  101. else
  102. operation_status = "跳过(TF卡未挂载)"
  103. end
  104. sys.wait(50)
  105. elseif operation_index == 4 then
  106. -- 创建测试文件
  107. if tf_mounted then
  108. local file = io.open("/sd/io_test/testfile.txt", "wb")
  109. if file then
  110. file:close()
  111. operation_status = "完成"
  112. else
  113. operation_status = "失败"
  114. fail = fail + 1
  115. operation_index = 1
  116. end
  117. else
  118. operation_status = "跳过(TF卡未挂载)"
  119. end
  120. sys.wait(50)
  121. elseif operation_index == 5 then
  122. -- 写入内容
  123. if tf_mounted then
  124. local file = io.open("/sd/io_test/testfile.txt", "w")
  125. if file then
  126. file:write("LuatOS TF卡测试")
  127. file:close()
  128. operation_status = "完成"
  129. else
  130. operation_status = "失败"
  131. fail = fail + 1
  132. operation_index = 1
  133. end
  134. else
  135. operation_status = "跳过(TF卡未挂载)"
  136. end
  137. sys.wait(50)
  138. elseif operation_index == 6 then
  139. -- 读取内容
  140. if tf_mounted then
  141. local file = io.open("/sd/io_test/testfile.txt", "r")
  142. if file then
  143. local content = file:read("*a") or ""
  144. file:close()
  145. operation_status = "读取成功"
  146. else
  147. operation_status = "失败"
  148. fail = fail + 1
  149. operation_index = 1
  150. end
  151. else
  152. operation_status = "跳过(TF卡未挂载)"
  153. end
  154. sys.wait(50)
  155. elseif operation_index == 7 then
  156. -- 删除文件
  157. if tf_mounted then
  158. if os.remove("/sd/io_test/testfile.txt") then
  159. operation_status = "完成"
  160. else
  161. operation_status = "失败"
  162. fail = fail + 1
  163. operation_index = 1
  164. end
  165. else
  166. operation_status = "跳过(TF卡未挂载)"
  167. end
  168. sys.wait(50)
  169. elseif operation_index == 8 then
  170. -- 删除目录
  171. if tf_mounted then
  172. if io.rmdir("/sd/io_test") then
  173. operation_status = "完成"
  174. else
  175. operation_status = "失败"
  176. end
  177. else
  178. operation_status = "跳过(TF卡未挂载)"
  179. end
  180. sys.wait(50)
  181. elseif operation_index == 9 then
  182. -- 卸载TF卡
  183. if tf_mounted then
  184. if fatfs.unmount("/sd") then
  185. tf_mounted = false
  186. operation_status = "完成"
  187. else
  188. operation_status = "失败"
  189. fail = fail + 1
  190. operation_index = 1
  191. end
  192. else
  193. operation_status = "未挂载"
  194. end
  195. sys.wait(50)
  196. elseif operation_index == 10 then
  197. -- 关闭SPI
  198. if spi_id then
  199. spi.close(spi_id)
  200. spi_id = nil
  201. operation_status = "完成"
  202. sucess = sucess +1
  203. else
  204. operation_status = "未初始化"
  205. fail = fail + 1
  206. operation_index = 1
  207. end
  208. sys.wait(50)
  209. end
  210. -- 移动到下一步操作(如果未重试)
  211. if operation_index < #operations then
  212. operation_index = operation_index + 1
  213. else
  214. operation_index = 1
  215. mount_retry_count = 0
  216. cycle_count = cycle_count + 1 -- 增加循环次数
  217. sys.wait(50) -- 操作完成后延时1秒再重新开始
  218. end
  219. end
  220. -- 尝试清理资源
  221. if tf_mounted then
  222. fatfs.unmount("/sd")
  223. end
  224. if spi_id then
  225. spi.close(spi_id)
  226. end
  227. return true
  228. end
  229. function airtf.tp_handal(x, y, event)
  230. if x > 20 and x < 100 and y > 360 and y < 440 then -- 返回主界面
  231. run_state = false
  232. end
  233. end
  234. return airtf