airtf.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. spi.setup(spi_id, nil, 0, 0, 8, 400 * 1000) -- 初始化SPI 接口
  55. operation_status = "SPI初始化完成"
  56. sys.wait(50)
  57. elseif operation_index == 2 then
  58. -- 挂载TF卡(带重试机制)
  59. mount_retry_count = mount_retry_count + 1
  60. fatfs.unmount("/sd")
  61. local mount_ok, mount_err = fatfs.mount(fatfs.SPI, "/sd", spi_id, pin_cs, 10 * 1000 * 1000) -- 传输tf 卡的片选
  62. if mount_ok then
  63. tf_mounted = true
  64. operation_status = "挂载成功"
  65. os.remove("/sd/io_test/testfile.txt")
  66. io.rmdir("/sd/io_test")
  67. else
  68. operation_status = "挂载失败("..(mount_err or "未知")..")"
  69. log.info(operation_status)
  70. fail = fail + 1
  71. operation_index = 1
  72. -- 最多重试3次
  73. if mount_retry_count < 3 then
  74. operation_index = operation_index - 1 -- 重试当前步骤
  75. operation_status = operation_status..",重试中("..mount_retry_count.."/3)"
  76. else
  77. operation_status = operation_status..",已放弃"
  78. end
  79. end
  80. sys.wait(50)
  81. elseif operation_index == 3 then
  82. -- 创建目录
  83. if tf_mounted then
  84. if io.mkdir("/sd/io_test") then
  85. operation_status = "创建目录完成"
  86. else
  87. operation_status = "创建目录失败1"
  88. log.info(operation_status)
  89. fail = fail + 1
  90. operation_index = 1
  91. end
  92. else
  93. operation_status = "跳过(TF卡未挂载)"
  94. end
  95. sys.wait(50)
  96. elseif operation_index == 4 then
  97. -- 创建测试文件
  98. if tf_mounted then
  99. local file = io.open("/sd/io_test/testfile.txt", "wb")
  100. if file then
  101. file:close()
  102. operation_status = "创建测试文件完成"
  103. else
  104. operation_status = "创建测试文件失败"
  105. log.info(operation_status)
  106. fail = fail + 1
  107. operation_index = 1
  108. end
  109. else
  110. operation_status = "跳过(TF卡未挂载)"
  111. end
  112. sys.wait(50)
  113. elseif operation_index == 5 then
  114. -- 写入内容
  115. if tf_mounted then
  116. local file = io.open("/sd/io_test/testfile.txt", "w")
  117. if file then
  118. file:write("LuatOS TF卡测试")
  119. file:close()
  120. operation_status = "写入内容完成"
  121. else
  122. operation_status = "写入内容失败"
  123. log.info(operation_status)
  124. fail = fail + 1
  125. operation_index = 1
  126. end
  127. else
  128. operation_status = "跳过(TF卡未挂载)"
  129. end
  130. sys.wait(50)
  131. elseif operation_index == 6 then
  132. -- 读取内容
  133. if tf_mounted then
  134. local file = io.open("/sd/io_test/testfile.txt", "r")
  135. if file then
  136. local content = file:read("*a") or ""
  137. file:close()
  138. operation_status = "读取成功"
  139. else
  140. operation_status = "读取失败"
  141. log.info(operation_status)
  142. fail = fail + 1
  143. operation_index = 1
  144. end
  145. else
  146. operation_status = "跳过(TF卡未挂载)"
  147. end
  148. sys.wait(50)
  149. elseif operation_index == 7 then
  150. -- 删除文件
  151. if tf_mounted then
  152. if os.remove("/sd/io_test/testfile.txt") then
  153. operation_status = "删除文件完成"
  154. else
  155. operation_status = "删除文件失败"
  156. log.info(operation_status)
  157. fail = fail + 1
  158. operation_index = 1
  159. end
  160. else
  161. operation_status = "跳过(TF卡未挂载)"
  162. end
  163. sys.wait(50)
  164. elseif operation_index == 8 then
  165. -- 删除目录
  166. if tf_mounted then
  167. if io.rmdir("/sd/io_test") then
  168. operation_status = "删除目录完成"
  169. else
  170. operation_status = "删除目录失败"
  171. log.info(operation_status)
  172. end
  173. else
  174. operation_status = "跳过(TF卡未挂载)"
  175. end
  176. sys.wait(50)
  177. elseif operation_index == 9 then
  178. -- 卸载TF卡
  179. if tf_mounted then
  180. if fatfs.unmount("/sd") then
  181. tf_mounted = false
  182. operation_status = "卸载TF卡完成"
  183. else
  184. operation_status = "卸载TF卡失败"
  185. log.info(operation_status)
  186. fail = fail + 1
  187. operation_index = 1
  188. end
  189. else
  190. operation_status = "未挂载"
  191. end
  192. sys.wait(50)
  193. elseif operation_index == 10 then
  194. -- 关闭SPI
  195. if spi_id then
  196. spi.close(spi_id)
  197. spi_id = nil
  198. operation_status = "关闭SPI完成"
  199. sucess = sucess +1
  200. else
  201. operation_status = "关闭SPI失败"
  202. log.info(operation_status)
  203. fail = fail + 1
  204. operation_index = 1
  205. end
  206. sys.wait(50)
  207. end
  208. -- 移动到下一步操作(如果未重试)
  209. if operation_index < #operations then
  210. operation_index = operation_index + 1
  211. else
  212. operation_index = 1
  213. mount_retry_count = 0
  214. cycle_count = cycle_count + 1 -- 增加循环次数
  215. sys.wait(50) -- 操作完成后延时1秒再重新开始
  216. end
  217. end
  218. -- 尝试清理资源
  219. if tf_mounted then
  220. fatfs.unmount("/sd")
  221. tf_mounted = false
  222. end
  223. if spi_id then
  224. spi.close(spi_id)
  225. end
  226. return true
  227. end
  228. function airtf.tp_handal(x, y, event)
  229. if x > 20 and x < 100 and y > 360 and y < 440 then -- 返回主界面
  230. run_state = false
  231. end
  232. end
  233. return airtf