main.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. PROJECT = "camerademo"
  2. VERSION = "1.0.0"
  3. -- 实际使用时选1个就行
  4. require "bf30a2"
  5. require "gc032a"
  6. require "gc0310"
  7. require "gc2145"
  8. sys = require("sys")
  9. log.style(1)
  10. local SCAN_MODE = 0 -- 写1演示扫码
  11. local scan_pause = true
  12. local getRawStart = false
  13. local RAW_MODE = 0 -- 写1演示获取原始图像
  14. local DONE_WITH_CLOSE = false
  15. local MIPI_MODE = false
  16. if hmeta.chip() == "UIS8910" then --Air722测试要把VLCD电压拉高到3.2V
  17. MIPI_MODE = true
  18. pm.ioVol(pm.IOVOL_LCD, 3200)
  19. end
  20. -- SCAN_MODE和RAW_MODE都没有写1就是拍照
  21. -- 根据不同的BSP返回不同的值
  22. -- spi_id,pin_reset,pin_dc,pin_cs,bl
  23. local function lcd_pin()
  24. local rtos_bsp = rtos.bsp()
  25. local chip_type = hmeta.chip()
  26. if string.find(rtos_bsp,"EC718") or string.find(chip_type,"EC718") then
  27. return lcd.HWID_0, 36, 0xff, 0xff, 0xff -- 注意:EC718P有硬件lcd驱动接口, 无需使用spi,当然spi驱动也支持
  28. elseif string.find(chip_type,"UIS8910") then
  29. return lcd.HWID_0, 0xff, 0xff, 0xff, 0xff
  30. else
  31. log.info("main", "bsp not support")
  32. return
  33. end
  34. end
  35. local spi_id, pin_reset, pin_dc, pin_cs, bl = lcd_pin()
  36. if spi_id ~= lcd.HWID_0 then
  37. spi_lcd = spi.deviceSetup(spi_id, pin_cs, 0, 0, 8, 20 * 1000 * 1000, spi.MSB, 1, 0)
  38. port = "device"
  39. else
  40. port = spi_id
  41. end
  42. --[[ 此为合宙售卖的1.8寸TFT LCD LCD 分辨率:128X160 屏幕ic:st7735 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.19.6c2275a1Pa8F9o&id=560176729178]]
  43. -- lcd.init("st7735",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 128,h = 160,xoffset = 0,yoffset = 0},spi_lcd)
  44. --[[ 此为合宙售卖的1.54寸TFT LCD LCD 分辨率:240X240 屏幕ic:st7789 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.20.391445d5Ql4uJl&id=659456700222]]
  45. -- lcd.init("st7789",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd,true)
  46. --[[ 此为合宙售卖的0.96寸TFT LCD LCD 分辨率:160X80 屏幕ic:st7735s 购买地址:https://item.taobao.com/item.htm?id=661054472686]]
  47. -- lcd.init("st7735v",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 1,w = 160,h = 80,xoffset = 0,yoffset = 24},spi_lcd)
  48. -- 如果显示颜色相反,请解开下面一行的注释,关闭反色
  49. -- lcd.invoff()
  50. -- 如果显示依旧不正常,可以尝试老版本的板子的驱动
  51. -- lcd.init("st7735s",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 2,w = 160,h = 80,xoffset = 0,yoffset = 0},spi_lcd)
  52. if MIPI_MODE then
  53. lcd.init("st7796", {
  54. port = port,
  55. pin_dc = pin_dc,
  56. pin_pwr = bl,
  57. pin_rst = pin_reset,
  58. direction = 0,
  59. w = 320,
  60. h = 480,
  61. xoffset = 0,
  62. yoffset = 0
  63. }, spi_lcd, true)
  64. log.info("init st7796")
  65. else
  66. lcd.init("gc9306x", {
  67. port = port,
  68. pin_dc = pin_dc,
  69. pin_pwr = bl,
  70. pin_rst = pin_reset,
  71. direction = 0,
  72. w = 240,
  73. h = 320,
  74. xoffset = 0,
  75. yoffset = 0
  76. }, spi_lcd, true)
  77. end
  78. local uartid = uart.VUART_0 -- 根据实际设备选取不同的uartid
  79. -- 初始化
  80. local result = uart.setup(uartid, -- 串口id
  81. 115200, -- 波特率
  82. 8, -- 数据位
  83. 1 -- 停止位
  84. )
  85. local function cb(id, str)
  86. if type(str) == 'string' then
  87. log.info("扫码结果", str)
  88. elseif str == false then
  89. log.error("摄像头没有数据")
  90. else
  91. log.info("摄像头数据", str)
  92. sys.publish("capture done", true)
  93. end
  94. end
  95. local function press_key()
  96. log.info("boot press")
  97. sys.publish("PRESS", true)
  98. end
  99. if hmeta.chip() == "UIS8910" then
  100. gpio.setup(9, press_key, gpio.PULLDOWN, gpio.RISING)
  101. gpio.debounce(9, 100, 1)
  102. else
  103. gpio.setup(0, press_key, gpio.PULLDOWN, gpio.RISING)
  104. gpio.debounce(0, 100, 1)
  105. end
  106. local rawbuff, err
  107. if RAW_MODE ~= 1 then
  108. if MIPI_MODE then
  109. rawbuff, err = zbuff.create(400 * 1024, 0, zbuff.HEAP_AUTO) -- gc2145
  110. else
  111. rawbuff, err = zbuff.create(60 * 1024, 0, zbuff.HEAP_AUTO)
  112. end
  113. else
  114. rawbuff, err = zbuff.create(640 * 480 * 2, 0, zbuff.HEAP_AUTO) -- gc032a
  115. -- local rawbuff = zbuff.create(240 * 320 * 2, zbuff.HEAP_AUTO) --bf302a
  116. end
  117. if rawbuff == nil then
  118. log.info(err)
  119. end
  120. local function device_init()
  121. local cspiId, i2cId
  122. if hmeta.chip() == "UIS8910" then
  123. cspiId, i2cId = 0,1
  124. i2c.setup(i2cId, i2c.FAST)
  125. if MIPI_MODE then
  126. gpio.setup(11, 1) -- 722 mipi camera reset ctrl by gpio11
  127. return gc2145Init(cspiId, i2cId, 24000000, SCAN_MODE, SCAN_MODE)
  128. else
  129. return gc0310Init(cspiId, i2cId, 25000000, SCAN_MODE, SCAN_MODE)
  130. end
  131. else
  132. cspiId, i2cId = 1,0
  133. i2c.setup(i2cId, i2c.FAST)
  134. -- return bf30a2Init(cspiId,i2cId,25500000,SCAN_MODE,SCAN_MODE)
  135. -- return gc032aInit(cspiId,i2cId,24000000,SCAN_MODE,SCAN_MODE)
  136. return gc0310Init(cspiId, i2cId, 25500000, SCAN_MODE, SCAN_MODE)
  137. end
  138. end
  139. local function pdwn(id, level)
  140. if hmeta.chip() == "UIS8910" then
  141. camera.pwdn_pin(id, level)
  142. else
  143. gpio.setup(5, level) -- PD拉低
  144. end
  145. end
  146. local tx_buff = zbuff.create(16384)
  147. local function send_to_pc(uart_id, buff)
  148. local pos = 0
  149. log.info("需要发送", buff:used(), "byte")
  150. while pos < buff:used() do
  151. if buff:used() - pos > 16384 then
  152. tx_buff:copy(0, buff, pos, 16384)
  153. pos = pos + 16384
  154. else
  155. tx_buff:copy(0, buff, pos, buff:used() - pos)
  156. pos = buff:used()
  157. end
  158. log.info("发送", tx_buff:used(), "byte")
  159. uart.tx(uart_id, tx_buff)
  160. tx_buff:del()
  161. sys.wait(5)
  162. end
  163. end
  164. sys.taskInit(function()
  165. log.info("摄像头启动")
  166. local camera_id = device_init()
  167. camera.on(camera_id, "scanned", cb)
  168. pdwn(camera_id, 0)
  169. if DONE_WITH_CLOSE then
  170. camera.close(camera_id)
  171. else
  172. camera.stop(camera_id)
  173. camera.preview(camera_id, true)
  174. end
  175. log.info("按下boot开始测试")
  176. log.info(rtos.meminfo("sys"))
  177. log.info(rtos.meminfo("psram"))
  178. while 1 do
  179. result, data = sys.waitUntil("PRESS", 30000)
  180. if result == true and data == true then
  181. if SCAN_MODE == 1 then
  182. if scan_pause then
  183. log.info("启动扫码")
  184. if DONE_WITH_CLOSE then
  185. camera_id = device_init()
  186. end
  187. camera.start(camera_id)
  188. scan_pause = false
  189. sys.wait(200)
  190. log.info(rtos.meminfo("sys"))
  191. log.info(rtos.meminfo("psram"))
  192. else
  193. log.info("停止扫码")
  194. if DONE_WITH_CLOSE then
  195. camera.close(camera_id)
  196. else
  197. camera.stop(camera_id)
  198. end
  199. scan_pause = true
  200. sys.wait(200)
  201. log.info(rtos.meminfo("sys"))
  202. log.info(rtos.meminfo("psram"))
  203. end
  204. elseif RAW_MODE == 1 then
  205. if getRawStart == false then
  206. getRawStart = true
  207. log.debug("摄像头首次捕获原始图像")
  208. if DONE_WITH_CLOSE then
  209. camera_id = device_init()
  210. end
  211. camera.startRaw(camera_id, 640, 480, rawbuff) -- gc032a
  212. -- camera.startRaw(camera_id,240,320,rawbuff) --bf302a
  213. else
  214. log.debug("摄像头继续捕获原始图像")
  215. camera.getRaw(camera_id)
  216. end
  217. result, data = sys.waitUntil("capture done", 30000)
  218. if DONE_WITH_CLOSE then
  219. camera.close(camera_id)
  220. else
  221. camera.stop(camera_id)
  222. end
  223. log.info("摄像头捕获原始图像完成")
  224. log.info(rtos.meminfo("sys"))
  225. log.info(rtos.meminfo("psram"))
  226. send_to_pc(uartid, rawbuff) --找个能保存数据的串口工具保存成文件就能在电脑上看了, 格式为JPG
  227. rawbuff:del()
  228. else
  229. log.debug("摄像头拍照")
  230. if DONE_WITH_CLOSE then
  231. camera_id = device_init()
  232. end
  233. camera.capture(camera_id, rawbuff, 1) -- 2和3需要非常多非常多的psram,尽量不要用
  234. result, data = sys.waitUntil("capture done", 30000)
  235. log.info(rawbuff:used())
  236. if DONE_WITH_CLOSE then
  237. camera.close(camera_id)
  238. else
  239. camera.stop(camera_id)
  240. end
  241. send_to_pc(uartid, rawbuff) -- 找个能保存数据的串口工具保存成文件就能在电脑上看了, 格式为JPG
  242. rawbuff:del()
  243. log.info(rtos.meminfo("sys"))
  244. log.info(rtos.meminfo("psram"))
  245. end
  246. end
  247. end
  248. end)
  249. -- 用户代码已结束---------------------------------------------
  250. -- 结尾总是这一句
  251. sys.run()
  252. -- sys.run()之后后面不要加任何语句!!!!!