luat_lib_camera.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. @module camera
  3. @summary 摄像头
  4. @version 1.0
  5. @date 2022.01.11
  6. @demo camera
  7. @tag LUAT_USE_CAMERA
  8. */
  9. #include "luat_base.h"
  10. #include "luat_camera.h"
  11. #include "luat_msgbus.h"
  12. #include "luat_fs.h"
  13. #include "luat_mem.h"
  14. #include "luat_uart.h"
  15. #include "luat_zbuff.h"
  16. #define LUAT_LOG_TAG "camera"
  17. #include "luat_log.h"
  18. #define MAX_DEVICE_COUNT 2
  19. #define MAX_USB_DEVICE_COUNT 1
  20. typedef struct luat_camera_cb {
  21. int scanned;
  22. } luat_camera_cb_t;
  23. static luat_camera_cb_t camera_cbs[MAX_DEVICE_COUNT + MAX_USB_DEVICE_COUNT];
  24. int l_camera_handler(lua_State *L, void* ptr) {
  25. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  26. lua_pop(L, 1);
  27. int camera_id = msg->arg1;
  28. if (camera_id >= LUAT_CAMERA_TYPE_USB)
  29. {
  30. camera_id = MAX_DEVICE_COUNT + camera_id - LUAT_CAMERA_TYPE_USB;
  31. }
  32. if (camera_cbs[camera_id].scanned) {
  33. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  34. if (lua_isfunction(L, -1)) {
  35. lua_pushinteger(L, camera_id);
  36. if (msg->ptr)
  37. {
  38. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  39. }
  40. else if (msg->arg2 > 1)
  41. {
  42. lua_pushinteger(L, msg->arg2);
  43. }
  44. else
  45. {
  46. lua_pushboolean(L, msg->arg2);
  47. }
  48. lua_call(L, 2, 0);
  49. }
  50. }
  51. lua_pushinteger(L, 0);
  52. return 1;
  53. }
  54. /*
  55. 初始化摄像头
  56. @api camera.init(InitReg_or_cspi_id, cspi_speed, mode, is_msb, rx_bit, seq_type, is_ddr, only_y, scan_mode, w, h)
  57. @table/integer 如果是table,则是DVP摄像头的配置见demo/camera/AIR105,同时忽略后续参数;如果是数字,则是camera spi总线序号
  58. @int camera spi总线速度
  59. @int camera spi模式,0~3
  60. @int 字节的bit顺序是否是msb,0否1是
  61. @int 同时接收bit数,1,2,4
  62. @int byte序列,0~1
  63. @int 双边沿采样配置,0不启用,其他值根据实际SOC决定
  64. @int 只接收Y分量,0不启用,1启用,扫码必须启用,否则会失败
  65. @int 工作模式,camera.AUTO自动,camera.SCAN扫码
  66. @int 摄像头宽度
  67. @int 摄像头高度
  68. @return int/false 成功返回camera_id,失败返回false
  69. @usage
  70. camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
  71. --初始化后需要start才开始输出/扫码
  72. camera.start(camera_id)--开始指定的camera
  73. */
  74. static int l_camera_init(lua_State *L){
  75. int result;
  76. if (lua_istable(L, 1)) {
  77. luat_camera_conf_t conf = {0};
  78. conf.lcd_conf = luat_lcd_get_default();
  79. lua_pushliteral(L, "zbar_scan");
  80. lua_gettable(L, 1);
  81. if (lua_isinteger(L, -1)) {
  82. conf.zbar_scan = luaL_checkinteger(L, -1);
  83. }
  84. lua_pop(L, 1);
  85. lua_pushliteral(L, "draw_lcd");
  86. lua_gettable(L, 1);
  87. if (lua_isinteger(L, -1)) {
  88. conf.draw_lcd = luaL_checkinteger(L, -1);
  89. }
  90. lua_pop(L, 1);
  91. lua_pushliteral(L, "i2c_id");
  92. lua_gettable(L, 1);
  93. if (lua_isinteger(L, -1)) {
  94. conf.i2c_id = luaL_checkinteger(L, -1);
  95. }
  96. lua_pop(L, 1);
  97. lua_pushliteral(L, "i2c_addr");
  98. lua_gettable(L, 1);
  99. if (lua_isinteger(L, -1)) {
  100. conf.i2c_addr = luaL_checkinteger(L, -1);
  101. }
  102. lua_pop(L, 1);
  103. lua_pushliteral(L, "pwm_id");
  104. lua_gettable(L, 1);
  105. if (lua_isinteger(L, -1)) {
  106. conf.pwm_id = luaL_checkinteger(L, -1);
  107. }
  108. lua_pop(L, 1);
  109. lua_pushliteral(L, "pwm_period");
  110. lua_gettable(L, 1);
  111. if (lua_isinteger(L, -1)) {
  112. conf.pwm_period = luaL_checkinteger(L, -1);
  113. }
  114. lua_pop(L, 1);
  115. lua_pushliteral(L, "pwm_pulse");
  116. lua_gettable(L, 1);
  117. if (lua_isinteger(L, -1)) {
  118. conf.pwm_pulse = luaL_checkinteger(L, -1);
  119. }
  120. lua_pop(L, 1);
  121. lua_pushliteral(L, "sensor_width");
  122. lua_gettable(L, 1);
  123. if (lua_isinteger(L, -1)) {
  124. conf.sensor_width = luaL_checkinteger(L, -1);
  125. }
  126. lua_pop(L, 1);
  127. lua_pushliteral(L, "sensor_height");
  128. lua_gettable(L, 1);
  129. if (lua_isinteger(L, -1)) {
  130. conf.sensor_height = luaL_checkinteger(L, -1);
  131. }
  132. lua_pop(L, 1);
  133. lua_pushliteral(L, "color_bit");
  134. lua_gettable(L, 1);
  135. if (lua_isinteger(L, -1)) {
  136. conf.color_bit = luaL_checkinteger(L, -1);
  137. }
  138. lua_pop(L, 1);
  139. lua_pushliteral(L, "id_reg");
  140. lua_gettable(L, 1);
  141. if (lua_isinteger(L, -1)) {
  142. conf.id_reg = luaL_checkinteger(L, -1);
  143. }
  144. lua_pop(L, 1);
  145. lua_pushliteral(L, "id_value");
  146. lua_gettable(L, 1);
  147. if (lua_isinteger(L, -1)) {
  148. conf.id_value = luaL_checkinteger(L, -1);
  149. }
  150. lua_pop(L, 1);
  151. lua_pushliteral(L, "id");
  152. lua_gettable(L, 1);
  153. if (lua_isinteger(L, -1)) {
  154. conf.id = luaL_checkinteger(L, -1);
  155. }
  156. lua_pop(L, 1);
  157. lua_pushliteral(L, "usb_port");
  158. lua_gettable(L, 1);
  159. if (lua_isinteger(L, -1)) {
  160. conf.usb_port = luaL_checkinteger(L, -1);
  161. }
  162. lua_pop(L, 1);
  163. lua_pushliteral(L, "init_cmd");
  164. lua_gettable(L, 1);
  165. if (lua_istable(L, -1)) {
  166. conf.init_cmd_size = lua_rawlen(L, -1);
  167. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  168. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  169. lua_geti(L, -1, i);
  170. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  171. lua_pop(L, 1);
  172. }
  173. }else if(lua_isstring(L, -1)){
  174. size_t len;
  175. int cmd;
  176. const char *fail_name = luaL_checklstring(L, -1, &len);
  177. FILE* fd = luat_fs_fopen(fail_name, "rb");
  178. conf.init_cmd_size = 0;
  179. if (fd){
  180. #define INITCMD_BUFF_SIZE 128
  181. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  182. conf.init_cmd = luat_heap_malloc(sizeof(uint8_t));
  183. while (1) {
  184. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  185. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  186. if (readline_len < 1)
  187. break;
  188. if (memcmp(init_cmd_buff, "#", 1)==0){
  189. continue;
  190. }
  191. char *token = strtok(init_cmd_buff, ",");
  192. if (sscanf(token,"%x",&cmd) < 1){
  193. continue;
  194. }
  195. conf.init_cmd_size = conf.init_cmd_size + 1;
  196. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  197. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  198. while( token != NULL ) {
  199. token = strtok(NULL, ",");
  200. if (sscanf(token,"%x",&cmd) < 1){
  201. break;
  202. }
  203. conf.init_cmd_size = conf.init_cmd_size + 1;
  204. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  205. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  206. }
  207. }
  208. conf.init_cmd[conf.init_cmd_size]= 0;
  209. luat_fs_fclose(fd);
  210. }else{
  211. LLOGE("init_cmd fail open error");
  212. }
  213. }
  214. lua_pop(L, 1);
  215. result = luat_camera_init(&conf);
  216. if (result < 0) {
  217. lua_pushboolean(L, 0);
  218. } else {
  219. lua_pushinteger(L, result);
  220. }
  221. } else if (lua_isinteger(L, 1)) {
  222. luat_spi_camera_t conf = {0};
  223. conf.lcd_conf = luat_lcd_get_default();
  224. int cspi_id = lua_tointeger(L, 1);
  225. int default_value = 24000000;
  226. conf.camera_speed = lua_tointegerx(L, 2, &default_value);
  227. default_value = 0;
  228. conf.spi_mode = lua_tointegerx(L, 3, &default_value);
  229. conf.is_msb = lua_tointegerx(L, 4, &default_value);
  230. conf.is_two_line_rx = lua_tointegerx(L, 5, &default_value) - 1;
  231. conf.seq_type = lua_tointegerx(L, 6, &default_value);
  232. result = lua_tointegerx(L, 7, &default_value);
  233. memcpy(conf.plat_param, &result, 4);
  234. conf.only_y = lua_tointegerx(L, 8, &default_value);
  235. int mode = lua_tointegerx(L, 9, &default_value);
  236. default_value = 240;
  237. conf.sensor_width = lua_tointegerx(L, 10, &default_value);
  238. default_value = 320;
  239. conf.sensor_height = lua_tointegerx(L, 11, &default_value);
  240. luat_camera_init(NULL);
  241. luat_camera_work_mode(cspi_id, mode);
  242. result = luat_camera_setup(cspi_id, &conf, NULL, 0);
  243. if (result < 0) {
  244. lua_pushboolean(L, 0);
  245. } else {
  246. lua_pushinteger(L, result);
  247. }
  248. } else {
  249. lua_pushboolean(L, 0);
  250. }
  251. return 1;
  252. }
  253. /**
  254. 注册摄像头事件回调
  255. @api camera.on(id, event, func)
  256. @int camera id, camera 0写0, camera 1写1
  257. @string 事件名称
  258. @function 回调方法
  259. @return nil 无返回值
  260. @usage
  261. camera.on(0, "scanned", function(id, str)
  262. --id int camera id
  263. --str 多种类型 false 摄像头没有正常工作,true 拍照模式下拍照成功并保存完成, int 原始数据模式下本次返回的数据大小, string 扫码模式下扫码成功后的解码值
  264. print(id, str)
  265. end)
  266. */
  267. static int l_camera_on(lua_State *L) {
  268. int camera_id = luaL_checkinteger(L, 1);
  269. const char* event = luaL_checkstring(L, 2);
  270. if (camera_id >= LUAT_CAMERA_TYPE_USB)
  271. {
  272. camera_id = MAX_DEVICE_COUNT + camera_id - LUAT_CAMERA_TYPE_USB;
  273. }
  274. if (!strcmp("scanned", event)) {
  275. if (camera_cbs[camera_id].scanned != 0) {
  276. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  277. camera_cbs[camera_id].scanned = 0;
  278. }
  279. if (lua_isfunction(L, 3)) {
  280. lua_pushvalue(L, 3);
  281. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  282. }
  283. }
  284. return 0;
  285. }
  286. /**
  287. 开始指定的camera
  288. @api camera.start(id)
  289. @int camera id,例如0
  290. @return boolean 成功返回true,否则返回false
  291. @usage
  292. camera.start(0)
  293. */
  294. static int l_camera_start(lua_State *L) {
  295. int id = luaL_checkinteger(L, 1);
  296. lua_pushboolean(L, luat_camera_start(id) == 0 ? 1 : 0);
  297. return 1;
  298. }
  299. /**
  300. 停止指定的camera
  301. @api camera.stop(id)
  302. @int camera id,例如0
  303. @return boolean 成功返回true,否则返回false
  304. @usage
  305. camera.stop(0)
  306. */
  307. static int l_camera_stop(lua_State *L) {
  308. int id = luaL_checkinteger(L, 1);
  309. lua_pushboolean(L, luat_camera_stop(id) == 0 ? 1 : 0);
  310. return 1;
  311. }
  312. /**
  313. 关闭指定的camera,释放相应的IO资源
  314. @api camera.close(id)
  315. @int camera id,例如0
  316. @return boolean 成功返回true,否则返回false
  317. @usage
  318. camera.close(0)
  319. */
  320. static int l_camera_close(lua_State *L) {
  321. int id = luaL_checkinteger(L, 1);
  322. lua_pushboolean(L, luat_camera_close(id) == 0 ? 1 : 0);
  323. return 1;
  324. }
  325. LUAT_WEAK int luat_camera_setup(int id, luat_spi_camera_t *conf, void * callback, void *param) {
  326. LLOGD("not support yet");
  327. return -1;
  328. }
  329. LUAT_WEAK int luat_camera_capture(int id, uint8_t quality, const char *path) {
  330. LLOGD("not support yet");
  331. return -1;
  332. }
  333. LUAT_WEAK int luat_camera_capture_config(int id, uint16_t start_x, uint16_t start_y, uint16_t new_w, uint16_t new_h) {
  334. LLOGD("not support yet");
  335. return -1;
  336. }
  337. LUAT_WEAK int luat_camera_capture_in_ram(int id, uint8_t quality, void *buffer) {
  338. LLOGD("not support yet");
  339. return -1;
  340. }
  341. LUAT_WEAK int luat_camera_get_raw_start(int id, int w, int h, uint8_t *data, uint32_t max_len) {
  342. LLOGD("not support yet");
  343. return -1;
  344. }
  345. LUAT_WEAK int luat_camera_get_raw_again(int id) {
  346. LLOGD("not support yet");
  347. return -1;
  348. }
  349. LUAT_WEAK int luat_camera_video(int id, int w, int h, uint8_t uart_id) {
  350. LLOGD("not support yet");
  351. return -1;
  352. }
  353. LUAT_WEAK int luat_camera_preview(int id, uint8_t on_off){
  354. LLOGD("not support yet");
  355. return -1;
  356. }
  357. LUAT_WEAK int luat_camera_work_mode(int id, int mode){
  358. LLOGD("not support yet");
  359. return -1;
  360. }
  361. /**
  362. camera拍照
  363. @api camera.capture(id, save_path, quality, x, y, w, h)
  364. @int camera id,例如0
  365. @string/zbuff/nil save_path,文件保存路径,空则写在上次路径里,默认是/capture.jpg,如果是zbuff,则将图片保存在buff内不写入文件系统
  366. @int quality, jpeg压缩质量,1最差,占用空间小,3最高,占用空间最大而且费时间,默认1
  367. @int x, 裁剪起始横坐标,从x列开始
  368. @int y, 裁剪起始纵坐标,从y行开始
  369. @int w, 裁剪后的宽度
  370. @int h, 裁剪后的高度
  371. @return boolean 成功返回true,否则返回false,真正完成后通过camera.on设置的回调函数回调接收到的长度
  372. @usage
  373. camera.capture(0)
  374. */
  375. static int l_camera_capture(lua_State *L) {
  376. int id = luaL_checkinteger(L, 1);
  377. int quality = luaL_optinteger(L, 3, 1);
  378. if (luat_camera_capture_config(id, luaL_optinteger(L, 4, 0), luaL_optinteger(L, 5, 0), luaL_optinteger(L, 6, 0), luaL_optinteger(L, 7, 0)))
  379. {
  380. lua_pushboolean(L, 0);
  381. return 1;
  382. }
  383. if (lua_isstring(L, 2)){
  384. const char* save_path = luaL_checkstring(L, 2);
  385. lua_pushboolean(L, !luat_camera_capture(id, quality, save_path));
  386. } else {
  387. luat_zbuff_t *buff = luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  388. lua_pushboolean(L, !luat_camera_capture_in_ram(id, quality, buff));
  389. }
  390. return 1;
  391. }
  392. /**
  393. camera输出视频流到USB
  394. @api camera.video(id, w, h, out_path)
  395. @int camera id,例如0
  396. @int 宽度
  397. @int 高度
  398. @int 输出路径,目前只能用虚拟串口0
  399. @return boolean 成功返回true,否则返回false
  400. @usage
  401. camera.video(0, 320, 240, uart.VUART_0)
  402. */
  403. static int l_camera_video(lua_State *L) {
  404. int id = luaL_checkinteger(L, 1);
  405. int w = luaL_optinteger(L, 2, 320);
  406. int h = luaL_optinteger(L, 3, 240);
  407. int param = luaL_optinteger(L, 4, LUAT_VUART_ID_0);
  408. lua_pushboolean(L, !luat_camera_video(id, w, h, param));
  409. return 1;
  410. }
  411. /**
  412. 启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请调用camera.getRaw
  413. @api camera.startRaw(id, w, h, buff)
  414. @int camera id,例如0
  415. @int 宽度
  416. @int 高度
  417. @zbuff 用于存放数据的缓存区,大小必须不小于w X h X 2 byte
  418. @return boolean 成功返回true,否则返回false
  419. @usage
  420. camera.startRaw(0, 320, 240, buff)
  421. */
  422. static int l_camera_start_raw(lua_State *L) {
  423. int id = luaL_checkinteger(L, 1);
  424. int w = luaL_optinteger(L, 2, 320);
  425. int h = luaL_optinteger(L, 3, 240);
  426. luat_zbuff_t *buff = luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE);
  427. lua_pushboolean(L, !luat_camera_get_raw_start(id, w, h, buff->addr, buff->len));
  428. return 1;
  429. }
  430. /**
  431. 再次启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请继续调用本API
  432. @api camera.getRaw(id)
  433. @int camera id,例如0
  434. @return boolean 成功返回true,否则返回false
  435. @usage
  436. camera.getRaw(0)
  437. */
  438. static int l_camera_get_raw(lua_State *L) {
  439. int id = luaL_checkinteger(L, 1);
  440. lua_pushboolean(L, !luat_camera_get_raw_again(id));
  441. return 1;
  442. }
  443. /**
  444. 启停camera预览功能,直接输出到LCD上,只有硬件支持的SOC可以运行
  445. @api camera.preview(id, onoff)
  446. @int camera id,例如0
  447. @boolean true开启,false停止
  448. @return boolean 成功返回true,否则返回false
  449. @usage
  450. camera.preview(1, true)
  451. */
  452. static int l_camera_preview(lua_State *L) {
  453. int id = luaL_checkinteger(L, 1);
  454. uint8_t onoff = lua_toboolean(L, 2);
  455. lua_pushboolean(L, !luat_camera_preview(id, onoff));
  456. return 1;
  457. }
  458. #include "rotable2.h"
  459. static const rotable_Reg_t reg_camera[] =
  460. {
  461. { "init" , ROREG_FUNC(l_camera_init )},
  462. { "start" , ROREG_FUNC(l_camera_start )},
  463. { "preview", ROREG_FUNC(l_camera_preview)},
  464. { "stop" , ROREG_FUNC(l_camera_stop)},
  465. { "capture", ROREG_FUNC(l_camera_capture)},
  466. { "video", ROREG_FUNC(l_camera_video)},
  467. { "startRaw", ROREG_FUNC(l_camera_start_raw)},
  468. { "getRaw", ROREG_FUNC(l_camera_get_raw)},
  469. { "close", ROREG_FUNC(l_camera_close)},
  470. { "on", ROREG_FUNC(l_camera_on)},
  471. //@const AUTO number 摄像头工作在自动模式
  472. { "AUTO", ROREG_INT(LUAT_CAMERA_MODE_AUTO)},
  473. //@const SCAN number 摄像头工作在扫码模式,只输出Y分量
  474. { "SCAN", ROREG_INT(LUAT_CAMERA_MODE_SCAN)},
  475. //@const TYPE number 摄像头类型,USB
  476. { "USB", ROREG_INT(LUAT_CAMERA_TYPE_USB)},
  477. { NULL, {}}
  478. };
  479. LUAMOD_API int luaopen_camera( lua_State *L ) {
  480. luat_newlib2(L, reg_camera);
  481. return 1;
  482. }