当前位置: 代码迷 >> 综合 >> 【Luat-air105】8.2 fatfs及照片存储
  详细解决方案

【Luat-air105】8.2 fatfs及照片存储

热度:73   发布时间:2023-12-05 20:24:33.0

文章目录

  • 1 前言
  • 2 硬件连接
  • 3 官方代码
  • 4 代码
  • 5 结果
  • 6 小结

1 前言

本章测试下camera的拍照并存储照片到tf卡中。

2 硬件连接

在这里插入图片描述
注意:该tf卡模块的MOSI对应SPI2的MOSI。
在这里插入图片描述

3 官方代码

拍照
https://gitee.com/openLuat/LuatOS/blob/master/demo/camera/Air105/capture/main.lua

存储
https://gitee.com/openLuat/LuatOS/blob/master/demo/fatfs/Air105/main.lua

4 代码

分成三个lua文件,main、t5_fatfs、t8_camera

main.lua

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "camera_capture"
VERSION = "1.0.0"-- sys库是标配
_G.sys = require("sys")
require("t5_fatfs")
require("t8_camera")if wdt thenwdt.init(15000)--初始化watchdog设置为15ssys.timerLoopStart(wdt.feed, 10000)--10s喂一次狗
end-- 初始化屏幕
spi_lcd = spi.deviceSetup(5,pin.PC14,0,0,8,20*1000*1000,spi.MSB,1,1)log.info("lcd.init",
lcd.init("gc9306",{
    port = "device",pin_dc = pin.PE08 ,pin_rst = pin.PC12,pin_pwr = pin.PE09,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd))local uartid = 1 -- 根据实际设备选取不同的uartid
--初始化
local result = uart.setup(uartid,--串口id115200,--波特率8,--数据位1--停止位
)t8_camera_init()
t8_camera_start()function camera_capture()log.debug("摄像头捕获图像")camera.capture(camera_id, "/sd/temp.jpg", 1)sys.wait(2000)local f = io.open("/sd/temp.jpg", "r")local dataif f thendata = f:read("*a")log.info("fs", #data)f:close()enduart.write(uartid, data) --找个能保存数据的串口工具保存成文件就能在电脑上看了, 格式为JPG
endsys.taskInit(function()--sys.wait(1000) -- 启动延时-- 测试1: tf卡读写t5_fatfs_init()test_boot_time()test_add()test_readline()-- 测试2: 拍照存储while 1 docamera_capture()sys.wait(5000)endend)-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

t8_camera.lua

--GC032A输出rgb图像初始化命令
local GC032A_InitReg =
{
    zbar_scan = 0,--是否为扫码draw_lcd = 1,--是否向lcd输出i2c_id = 0,i2c_addr = 0x21,pwm_id = 5;pwm_period  = 12*1000*1000,pwm_pulse = 0,sensor_width = 640,sensor_height = 480,color_bit = 16,init_cmd ={
    0xf3,0xff,0xf7,0x01,0xf8,0x03,0xf9,0xce,0xfa,0x00,0xfc,0x02,0xfe,0x02,0x81,0x03,0xfe, 0x00,0x77, 0x64,0x78, 0x40,0x79, 0x60,0xfe, 0x00,0x03, 0x01,0x04, 0xcb,0x05, 0x01,0x06, 0xb2,0x07, 0x00,0x08, 0x10,0x0a, 0x00,0x0c, 0x00,0x0d, 0x01,0x0e, 0xe8,0x0f, 0x02,0x10, 0x88,0x17, 0x54,0x19, 0x08,0x1a, 0x0a,0x1f, 0x40,0x20, 0x30,0x2e, 0x80,0x2f, 0x2b,0x30, 0x1a,0xfe, 0x02,0x03, 0x02,0x05, 0xd7,0x06, 0x60,0x08, 0x80,0x12, 0x89,0xfe, 0x00,0x18, 0x02,0xfe, 0x02,0x40, 0x22,0x45, 0x00,0x46, 0x00,0x49, 0x20,0x4b, 0x3c,0x50, 0x20,0x42, 0x10,0xfe, 0x01,0x0a, 0xc5,0x45, 0x00,0xfe, 0x00,0x40, 0xff,0x41, 0x25,0x42, 0xef,0x43, 0x10,0x44, 0x82,0x46, 0x22,0x49, 0x03,0x52, 0x02,0x54, 0x00,0xfe, 0x02,0x22, 0xf6,0xfe, 0x01,0xc1, 0x38,0xc2, 0x4c,0xc3, 0x00,0xc4, 0x2c,0xc5, 0x24,0xc6, 0x18,0xc7, 0x28,0xc8, 0x11,0xc9, 0x15,0xca, 0x20,0xdc, 0x7a,0xdd, 0xa0,0xde, 0x80,0xdf, 0x88,0xfe, 0x01,0x50, 0xc1,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cf, 0xff,0xfe, 0x00,0x5a, 0x08,0x5b, 0x0f,0x5c, 0x15,0x5d, 0x1c,0x5e, 0x28,0x5f, 0x36,0x60, 0x45,0x61, 0x51,0x62, 0x6a,0x63, 0x7d,0x64, 0x8d,0x65, 0x98,0x66, 0xa2,0x67, 0xb5,0x68, 0xc3,0x69, 0xcd,0x6a, 0xd4,0x6b, 0xdc,0x6c, 0xe3,0x6d, 0xf0,0x6e, 0xf9,0x6f, 0xff,0xfe, 0x00,0x70, 0x50,0xfe, 0x00,0x4f, 0x01,0xfe, 0x01,0x0c, 0x01,0x0d, 0x00,0x12, 0xa0,0x13, 0x4a,0x44, 0x04,0x1f, 0x40,0x20, 0x40,0x23, 0x0a,0x26, 0x9a,0x3e, 0x20,0x3f, 0x2d,0x40, 0x40,0x41, 0x5b,0x42, 0x82,0x43, 0xb7,0x04, 0x0a,0x02, 0x79,0x03, 0xc0,0xfe, 0x01,0xcc, 0x08,0xcd, 0x08,0xce, 0xa4,0xcf, 0xec,0xfe, 0x00,0x81, 0xb8,0x82, 0x04,0x83, 0x10,0x84, 0x01,0x86, 0x50,0x87, 0x18,0x88, 0x10,0x89, 0x70,0x8a, 0x20,0x8b, 0x10,0x8c, 0x08,0x8d, 0x0a,0xfe, 0x00,0x8f, 0xaa,0x90, 0x1c,0x91, 0x52,0x92, 0x03,0x93, 0x03,0x94, 0x08,0x95, 0x6a,0x97, 0x00,0x98, 0x00,0xfe, 0x00,0x9a, 0x30,0x9b, 0x50,0xa1, 0x30,0xa2, 0x66,0xa4, 0x28,0xa5, 0x30,0xaa, 0x28,0xac, 0x32,0xfe, 0x00,0xd1, 0x3f,0xd2, 0x3f,0xd3, 0x38,0xd6, 0xf4,0xd7, 0x1d,0xdd, 0x72,0xde, 0x84,0xfe, 0x00,0x05, 0x01,0x06, 0xad,0x07, 0x00,0x08, 0x10,0xfe, 0x01,0x25, 0x00,0x26, 0x4d,0x27, 0x01,0x28, 0xce,0x29, 0x01,0x2a, 0xce,0x2b, 0x01,0x2c, 0xce,0x2d, 0x01,0x2e, 0xce,0x2f, 0x01,0x30, 0xce,0x31, 0x01,0x32, 0xce,0x33, 0x01,0x34, 0xce,0x3c, 0x10,0xfe, 0x00,0xfe,0x00,0x44,0x06,--RGB565输出0x46,0x0f,}
}local camera_pwdn = gpio.setup(pin.PD06, 1, gpio.PULLUP) -- PD06 camera_pwdn引脚
local camera_rst = gpio.setup(pin.PD07, 1, gpio.PULLUP) -- PD07 camera_rst引脚camera_rst(0)function t8_camera_init()-- 拍照, 自然就是RGB输出了camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
endfunction t8_camera_start()log.info("摄像头启动")camera.start(camera_id)--开始指定的camera-- -- PA10作为中断,上升沿/下降沿都触发-- gpio.setup(pin.PA10, function()-- sys.publish("CAPTURE", true)-- end, gpio.PULLUP,gpio.FALLING)end

t5_fatfs.lua

--[[ 接线要求:SPI 使用常规4线解法 Air105开发板 TF模块 PB3 CS PB2(SPI1_CLK) CLK PB5(SPI1_MISO) MISO PB4(SPI1_MOSI) MOSI 5V VCC GND GND ]]function t5_fatfs_init()local spiId = 2local result = spi.setup(spiId,--串口id255, -- 不使用默认CS脚0,--CPHA0,--CPOL8,--数据宽度400*1000  -- 初始化时使用较低的频率)print("spi setup:",result)local TF_CS = pin.PB3gpio.setup(TF_CS, 1)-- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因fatfs.mount("SD", spiId, TF_CS, 12000000)local data, err = fatfs.getfree("SD")if data thenlog.info("fatfs", "getfree", json.encode(data))elselog.info("fatfs", "err", err)end-- 重新设置spi,使用更高速率-- spi.close(0)-- sys.wait(100)-- spi.setup(spiId, 255, 0, 0, 8, 24*1000*1000)
end-- #################################################
-- 文件操作测试
-- #################################################
function test_boot_time()local f = io.open("/sd/boottime", "rb")local c = 0if f thenlocal data = f:read("*a")log.info("fs", "data", data, data:toHex())c = tonumber(data)f:close()endlog.info("fs", "boot count", c)c = c + 1f = io.open("/sd/boottime", "wb")if f ~= nil thenlog.info("fs", "write c to file", c, tostring(c))f:write(tostring(c))f:close()elselog.warn("sdio", "mount not good?!")endif fs thenlog.info("fsstat", fs.fsstat("/"))log.info("fsstat", fs.fsstat("/sd"))end
endfunction test_add()-- 测试一下追加, fix in 2021.12.21os.remove("/sd/test_a")sys.wait(50)f = io.open("/sd/test_a", "w")if f thenf:write("ABC")f:close()endf = io.open("/sd/test_a", "a+")if f thenf:write("def")f:close()endf = io.open("/sd/test_a", "r")if  f thenlocal data = f:read("*a")log.info("data", data, data == "ABCdef")f:close()end
endfunction test_readline()-- 测试一下按行读取, fix in 2022-01-16f = io.open("/sd/testline", "w")if f thenf:write("abc\n")f:write("123\n")f:write("wendal\n")f:close()endsys.wait(100)f = io.open("/sd/testline", "r")if f thenlog.info("sdio", "line1", f:read("*l"))log.info("sdio", "line2", f:read("*l"))log.info("sdio", "line3", f:read("*l"))f:close()end
end-- #################################################

5 结果

在这里插入图片描述

[2022-03-27 18:00:21.742] D/user.摄像头捕获图像
[2022-03-27 18:00:21.964] D/fatfs f_open /temp.jpg w
[2022-03-27 18:00:21.967] D/camera capture data 9243byte
[2022-03-27 18:00:23.741] D/fatfs f_open /temp.jpg r
[2022-03-27 18:00:23.745] I/user.fs	9243

6 小结

成功存储照片,后续再尝试

  1. uart将jpg图片传给esp32
  2. 将图片传给服务器,网页显示
  3. uart传给迪文屏显示