luat_lib_camera.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. static uint64_t camera_idp = 0;
  25. int l_camera_handler(lua_State *L, void* ptr) {
  26. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  27. lua_pop(L, 1);
  28. int camera_id = msg->arg1;
  29. if (camera_id >= LUAT_CAMERA_TYPE_USB)
  30. {
  31. camera_id = MAX_DEVICE_COUNT + camera_id - LUAT_CAMERA_TYPE_USB;
  32. }
  33. #if (defined TYPE_EC718) || (defined TYPE_EC718M)
  34. if (camera_cbs[0].scanned)
  35. {
  36. camera_id = 0;
  37. }
  38. else if (camera_cbs[1].scanned)
  39. {
  40. camera_id = 1;
  41. }
  42. #endif
  43. if (camera_cbs[camera_id].scanned) {
  44. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  45. if (lua_isfunction(L, -1)) {
  46. lua_pushinteger(L, camera_id);
  47. if (msg->ptr)
  48. {
  49. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  50. }
  51. else if (msg->arg2 > 1)
  52. {
  53. lua_pushinteger(L, msg->arg2);
  54. }
  55. else
  56. {
  57. lua_pushboolean(L, msg->arg2);
  58. }
  59. lua_call(L, 2, 0);
  60. }
  61. }
  62. lua_pushinteger(L, 0);
  63. return 1;
  64. }
  65. /*
  66. 初始化摄像头
  67. @api camera.init(InitReg_or_cspi_id, cspi_speed, mode, is_msb, rx_bit, seq_type, is_ddr, only_y, scan_mode, w, h)
  68. @table/integer 如果是table,则是DVP摄像头的配置见demo/camera/dvp_camera,同时忽略后续参数;如果是数字,则是camera spi总线序号
  69. @int camera spi总线速度
  70. @int camera spi模式,0~3
  71. @int 字节的bit顺序是否是msb,0否1是
  72. @int 同时接收bit数,1,2,4
  73. @int byte序列,0~1
  74. @int 双边沿采样配置,0不启用,其他值根据实际SOC决定
  75. @int 只接收Y分量,0不启用,1启用,扫码必须启用,否则会失败
  76. @int 工作模式,camera.AUTO自动,camera.SCAN扫码
  77. @int 摄像头宽度
  78. @int 摄像头高度
  79. @return int/false 成功返回camera_id,失败返回false
  80. @usage
  81. camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
  82. --初始化后需要start才开始输出/扫码
  83. camera.start(camera_id)--开始指定的camera
  84. */
  85. static int l_camera_init(lua_State *L){
  86. int result;
  87. if (lua_istable(L, 1)) {
  88. luat_camera_conf_t conf = {0};
  89. conf.lcd_conf = luat_lcd_get_default();
  90. conf.stream = 1;
  91. lua_pushliteral(L, "zbar_scan");
  92. lua_gettable(L, 1);
  93. if (lua_isinteger(L, -1)) {
  94. conf.zbar_scan = luaL_checkinteger(L, -1);
  95. }
  96. lua_pop(L, 1);
  97. lua_pushliteral(L, "draw_lcd");
  98. lua_gettable(L, 1);
  99. if (lua_isinteger(L, -1)) {
  100. conf.draw_lcd = luaL_checkinteger(L, -1);
  101. }
  102. lua_pop(L, 1);
  103. lua_pushliteral(L, "i2c_id");
  104. lua_gettable(L, 1);
  105. if (lua_isinteger(L, -1)) {
  106. conf.i2c_id = luaL_checkinteger(L, -1);
  107. }
  108. lua_pop(L, 1);
  109. lua_pushliteral(L, "i2c_addr");
  110. lua_gettable(L, 1);
  111. if (lua_isinteger(L, -1)) {
  112. conf.i2c_addr = luaL_checkinteger(L, -1);
  113. }
  114. lua_pop(L, 1);
  115. lua_pushliteral(L, "pwm_id");
  116. lua_gettable(L, 1);
  117. if (lua_isinteger(L, -1)) {
  118. conf.pwm_id = luaL_checkinteger(L, -1);
  119. }
  120. lua_pop(L, 1);
  121. lua_pushliteral(L, "pwm_period");
  122. lua_gettable(L, 1);
  123. if (lua_isinteger(L, -1)) {
  124. conf.pwm_period = luaL_checkinteger(L, -1);
  125. }
  126. lua_pop(L, 1);
  127. lua_pushliteral(L, "pwm_pulse");
  128. lua_gettable(L, 1);
  129. if (lua_isinteger(L, -1)) {
  130. conf.pwm_pulse = luaL_checkinteger(L, -1);
  131. }
  132. lua_pop(L, 1);
  133. lua_pushliteral(L, "sensor_width");
  134. lua_gettable(L, 1);
  135. if (lua_isinteger(L, -1)) {
  136. conf.sensor_width = luaL_checkinteger(L, -1);
  137. }
  138. lua_pop(L, 1);
  139. lua_pushliteral(L, "sensor_height");
  140. lua_gettable(L, 1);
  141. if (lua_isinteger(L, -1)) {
  142. conf.sensor_height = luaL_checkinteger(L, -1);
  143. }
  144. lua_pop(L, 1);
  145. lua_pushliteral(L, "color_bit");
  146. lua_gettable(L, 1);
  147. if (lua_isinteger(L, -1)) {
  148. conf.color_bit = luaL_checkinteger(L, -1);
  149. }
  150. lua_pop(L, 1);
  151. lua_pushliteral(L, "id_reg");
  152. lua_gettable(L, 1);
  153. if (lua_isinteger(L, -1)) {
  154. conf.id_reg = luaL_checkinteger(L, -1);
  155. }
  156. lua_pop(L, 1);
  157. lua_pushliteral(L, "id_value");
  158. lua_gettable(L, 1);
  159. if (lua_isinteger(L, -1)) {
  160. conf.id_value = luaL_checkinteger(L, -1);
  161. }
  162. lua_pop(L, 1);
  163. lua_pushliteral(L, "id");
  164. lua_gettable(L, 1);
  165. if (lua_isinteger(L, -1)) {
  166. conf.id = luaL_checkinteger(L, -1);
  167. }
  168. lua_pop(L, 1);
  169. lua_pushliteral(L, "usb_port");
  170. lua_gettable(L, 1);
  171. if (lua_isinteger(L, -1)) {
  172. conf.usb_port = luaL_checkinteger(L, -1);
  173. }
  174. lua_pop(L, 1);
  175. lua_pushliteral(L, "stream");
  176. lua_gettable(L, 1);
  177. if (lua_isinteger(L, -1)) {
  178. conf.stream = luaL_checkinteger(L, -1);
  179. }
  180. lua_pop(L, 1);
  181. lua_pushliteral(L, "async");
  182. lua_gettable(L, 1);
  183. if (lua_isinteger(L, -1)) {
  184. conf.async = luaL_checkinteger(L, -1);
  185. }
  186. lua_pop(L, 1);
  187. lua_pushliteral(L, "init_cmd");
  188. lua_gettable(L, 1);
  189. if (lua_istable(L, -1)) {
  190. conf.init_cmd_size = lua_rawlen(L, -1);
  191. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  192. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  193. lua_geti(L, -1, i);
  194. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  195. lua_pop(L, 1);
  196. }
  197. }else if(lua_isstring(L, -1)){
  198. size_t len;
  199. int cmd;
  200. const char *fail_name = luaL_checklstring(L, -1, &len);
  201. FILE* fd = luat_fs_fopen(fail_name, "rb");
  202. conf.init_cmd_size = 0;
  203. if (fd){
  204. #define INITCMD_BUFF_SIZE 128
  205. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  206. conf.init_cmd = luat_heap_malloc(sizeof(uint8_t));
  207. while (1) {
  208. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  209. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  210. if (readline_len < 1)
  211. break;
  212. if (memcmp(init_cmd_buff, "#", 1)==0){
  213. continue;
  214. }
  215. char *token = strtok(init_cmd_buff, ",");
  216. if (sscanf(token,"%x",&cmd) < 1){
  217. continue;
  218. }
  219. conf.init_cmd_size = conf.init_cmd_size + 1;
  220. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  221. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  222. while( token != NULL ) {
  223. token = strtok(NULL, ",");
  224. if (sscanf(token,"%x",&cmd) < 1){
  225. break;
  226. }
  227. conf.init_cmd_size = conf.init_cmd_size + 1;
  228. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  229. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  230. }
  231. }
  232. conf.init_cmd[conf.init_cmd_size]= 0;
  233. luat_fs_fclose(fd);
  234. }else{
  235. LLOGE("init_cmd fail open error");
  236. }
  237. }
  238. lua_pop(L, 1);
  239. result = luat_camera_init(&conf);
  240. if (result < 0) {
  241. if (conf.async) {
  242. camera_idp = luat_pushcwait(L);
  243. lua_pushboolean(L, 0);
  244. luat_pushcwait_error(L,1);
  245. }
  246. else {
  247. lua_pushboolean(L, 0);
  248. }
  249. } else {
  250. if (conf.async) {
  251. camera_idp = luat_pushcwait(L);
  252. } else {
  253. lua_pushinteger(L, result);
  254. }
  255. }
  256. } else if (lua_isinteger(L, 1)) {
  257. luat_spi_camera_t conf = {0};
  258. conf.lcd_conf = luat_lcd_get_default();
  259. int cspi_id = lua_tointeger(L, 1);
  260. int default_value = 24000000;
  261. conf.camera_speed = lua_tointegerx(L, 2, &default_value);
  262. default_value = 0;
  263. conf.spi_mode = lua_tointegerx(L, 3, &default_value);
  264. conf.is_msb = lua_tointegerx(L, 4, &default_value);
  265. conf.is_two_line_rx = lua_tointegerx(L, 5, &default_value) - 1;
  266. conf.seq_type = lua_tointegerx(L, 6, &default_value);
  267. result = lua_tointegerx(L, 7, &default_value);
  268. memcpy(conf.plat_param, &result, 4);
  269. conf.only_y = lua_tointegerx(L, 8, &default_value);
  270. int mode = lua_tointegerx(L, 9, &default_value);
  271. default_value = 240;
  272. conf.sensor_width = lua_tointegerx(L, 10, &default_value);
  273. default_value = 320;
  274. conf.sensor_height = lua_tointegerx(L, 11, &default_value);
  275. luat_camera_init(NULL);
  276. luat_camera_work_mode(cspi_id, mode);
  277. result = luat_camera_setup(cspi_id, &conf, NULL, 0);
  278. if (result < 0) {
  279. lua_pushboolean(L, 0);
  280. } else {
  281. lua_pushinteger(L, result);
  282. }
  283. } else {
  284. lua_pushboolean(L, 0);
  285. }
  286. return 1;
  287. }
  288. /**
  289. 注册摄像头事件回调
  290. @api camera.on(id, event, func)
  291. @int camera id, camera 0写0, camera 1写1
  292. @string 事件名称
  293. @function 回调方法
  294. @return nil 无返回值
  295. @usage
  296. camera.on(0, "scanned", function(id, event)
  297. --id int camera id
  298. --event 多种类型,详见下表
  299. print(id, event)
  300. end)
  301. --[[
  302. event可能出现的值有
  303. boolean型 false 摄像头没有正常工作,检查硬件和软件配置
  304. boolean型 true 拍照模式下拍照成功并保存完成,可以读取照片文件数据进一步处理,比如读出数据上传
  305. int型 原始图像大小 RAW模式下,采集完一帧图像后回调,回调值为图像数据大小,可以对传入的zbuff做进一步处理,比如读出数据上传
  306. string型 扫码结果 扫码模式下扫码成功一次,并且回调解码值,可以对回调值做进一步处理,比如打印到LCD上
  307. ]]
  308. */
  309. static int l_camera_on(lua_State *L) {
  310. int camera_id = luaL_checkinteger(L, 1);
  311. const char* event = luaL_checkstring(L, 2);
  312. if (camera_id >= LUAT_CAMERA_TYPE_USB)
  313. {
  314. camera_id = MAX_DEVICE_COUNT + camera_id - LUAT_CAMERA_TYPE_USB;
  315. }
  316. if (!strcmp("scanned", event)) {
  317. if (camera_cbs[camera_id].scanned != 0) {
  318. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  319. camera_cbs[camera_id].scanned = 0;
  320. }
  321. if (lua_isfunction(L, 3)) {
  322. lua_pushvalue(L, 3);
  323. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  324. }
  325. }
  326. return 0;
  327. }
  328. /**
  329. 开始指定的camera
  330. @api camera.start(id)
  331. @int camera id,例如0
  332. @return boolean 成功返回true,否则返回false
  333. @usage
  334. camera.start(0)
  335. */
  336. static int l_camera_start(lua_State *L) {
  337. int id = luaL_checkinteger(L, 1);
  338. lua_pushboolean(L, luat_camera_start(id) == 0 ? 1 : 0);
  339. return 1;
  340. }
  341. /**
  342. 停止指定的camera
  343. @api camera.stop(id)
  344. @int camera id,例如0
  345. @return boolean 成功返回true,否则返回false
  346. @usage
  347. camera.stop(0)
  348. */
  349. static int l_camera_stop(lua_State *L) {
  350. int id = luaL_checkinteger(L, 1);
  351. lua_pushboolean(L, luat_camera_stop(id) == 0 ? 1 : 0);
  352. return 1;
  353. }
  354. /**
  355. 关闭指定的camera,释放相应的IO资源
  356. @api camera.close(id)
  357. @int camera id,例如0
  358. @return boolean 成功返回true,否则返回false
  359. @usage
  360. camera.close(0)
  361. */
  362. static int l_camera_close(lua_State *L) {
  363. int id = luaL_checkinteger(L, 1);
  364. lua_pushboolean(L, luat_camera_close(id) == 0 ? 1 : 0);
  365. return 1;
  366. }
  367. LUAT_WEAK int luat_camera_setup(int id, luat_spi_camera_t *conf, void * callback, void *param) {
  368. LLOGD("not support yet");
  369. return -1;
  370. }
  371. LUAT_WEAK int luat_camera_capture(int id, uint8_t quality, const char *path) {
  372. LLOGD("not support yet");
  373. return -1;
  374. }
  375. 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) {
  376. LLOGD("not support yet");
  377. return -1;
  378. }
  379. LUAT_WEAK int luat_camera_capture_in_ram(int id, uint8_t quality, void *buffer) {
  380. LLOGD("not support yet");
  381. return -1;
  382. }
  383. LUAT_WEAK int luat_camera_get_raw_start(int id, int w, int h, uint8_t *data, uint32_t max_len) {
  384. LLOGD("not support yet");
  385. return -1;
  386. }
  387. LUAT_WEAK int luat_camera_get_raw_again(int id) {
  388. LLOGD("not support yet");
  389. return -1;
  390. }
  391. LUAT_WEAK int luat_camera_video(int id, int w, int h, uint8_t uart_id) {
  392. LLOGD("not support yet");
  393. return -1;
  394. }
  395. LUAT_WEAK int luat_camera_preview(int id, uint8_t on_off){
  396. LLOGD("not support yet");
  397. return -1;
  398. }
  399. LUAT_WEAK int luat_camera_work_mode(int id, int mode){
  400. LLOGD("not support yet");
  401. return -1;
  402. }
  403. LUAT_WEAK int luat_camera_config(int id, int key, int value) {
  404. LLOGD("not support yet");
  405. return -1;
  406. }
  407. LUAT_WEAK void luat_camera_pwdn_pin(int id, uint8_t level) {
  408. LLOGD("not support yet");
  409. return;
  410. }
  411. LUAT_WEAK void luat_camera_reset_pin(int id, uint8_t level) {
  412. LLOGD("not support yet");
  413. return;
  414. }
  415. /**
  416. camera拍照
  417. @api camera.capture(id, save_path, quality, x, y, w, h)
  418. @int camera id,例如0
  419. @string/zbuff/nil save_path,文件保存路径,空则写在上次路径里,默认是/capture.jpg,如果是zbuff,则将图片保存在buff内不写入文件系统
  420. @int quality, jpeg压缩质量, 见下面的使用说明
  421. @int x, 裁剪起始横坐标,从x列开始
  422. @int y, 裁剪起始纵坐标,从y行开始
  423. @int w, 裁剪后的宽度
  424. @int h, 裁剪后的高度
  425. @return boolean 成功返回true,否则返回false,真正完成后通过camera.on设置的回调函数回调接收到的长度
  426. @usage
  427. -- 保存到文件,质量为80
  428. camera.capture(0, "/capture.jpg", 80)
  429. -- 保存到内存文件系统
  430. camera.capture(0, "/ram/123.jpg", 80)
  431. -- 保存到zbuff,质量为80
  432. camera.capture(0, buff, 80)
  433. -- jpeg压缩质量,请使用 50 - 95 之间的数值
  434. -- 为保持兼容性, 质量值1/2/3, 分别对应 90/95/99
  435. */
  436. static int l_camera_capture(lua_State *L) {
  437. int id = luaL_checkinteger(L, 1);
  438. int quality = luaL_optinteger(L, 3, 1);
  439. 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)))
  440. {
  441. lua_pushboolean(L, 0);
  442. return 1;
  443. }
  444. if (lua_isstring(L, 2)){
  445. const char* save_path = luaL_checkstring(L, 2);
  446. lua_pushboolean(L, !luat_camera_capture(id, quality, save_path));
  447. } else {
  448. luat_zbuff_t *buff = luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE);
  449. lua_pushboolean(L, !luat_camera_capture_in_ram(id, quality, buff));
  450. }
  451. return 1;
  452. }
  453. /**
  454. camera输出视频流到USB
  455. @api camera.video(id, w, h, out_path)
  456. @int camera id,例如0
  457. @int 宽度
  458. @int 高度
  459. @int 输出路径,目前只能用虚拟串口0
  460. @return boolean 成功返回true,否则返回false
  461. @usage
  462. camera.video(0, 320, 240, uart.VUART_0)
  463. */
  464. static int l_camera_video(lua_State *L) {
  465. int id = luaL_checkinteger(L, 1);
  466. int w = luaL_optinteger(L, 2, 320);
  467. int h = luaL_optinteger(L, 3, 240);
  468. int param = luaL_optinteger(L, 4, LUAT_VUART_ID_0);
  469. lua_pushboolean(L, !luat_camera_video(id, w, h, param));
  470. return 1;
  471. }
  472. /**
  473. 启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请调用camera.getRaw
  474. @api camera.startRaw(id, w, h, buff)
  475. @int camera id,例如0
  476. @int 宽度
  477. @int 高度
  478. @zbuff 用于存放数据的缓存区,大小必须不小于w X h X 2 byte
  479. @return boolean 成功返回true,否则返回false
  480. @usage
  481. camera.startRaw(0, 320, 240, buff)
  482. */
  483. static int l_camera_start_raw(lua_State *L) {
  484. int id = luaL_checkinteger(L, 1);
  485. int w = luaL_optinteger(L, 2, 320);
  486. int h = luaL_optinteger(L, 3, 240);
  487. luat_zbuff_t *buff = luaL_checkudata(L, 4, LUAT_ZBUFF_TYPE);
  488. lua_pushboolean(L, !luat_camera_get_raw_start(id, w, h, buff->addr, buff->len));
  489. return 1;
  490. }
  491. /**
  492. 再次启动camera输出原始数据到用户的zbuff缓存区,输出1fps后会停止,并通过camera.on设置的回调函数回调接收到的长度,如果需要再次输出,请继续调用本API
  493. @api camera.getRaw(id)
  494. @int camera id,例如0
  495. @return boolean 成功返回true,否则返回false
  496. @usage
  497. camera.getRaw(0)
  498. */
  499. static int l_camera_get_raw(lua_State *L) {
  500. int id = luaL_checkinteger(L, 1);
  501. lua_pushboolean(L, !luat_camera_get_raw_again(id));
  502. return 1;
  503. }
  504. /**
  505. 启停camera预览功能,直接输出到LCD上,只有硬件支持的SOC可以运行,启动预览前必须调用lcd.int等api初始化LCD,预览时自动选择已经初始化过的lcd。
  506. @api camera.preview(id, onoff)
  507. @int camera id,例如0
  508. @boolean true开启,false停止
  509. @return boolean 成功返回true,否则返回false
  510. @usage
  511. camera.preview(1, true)
  512. */
  513. static int l_camera_preview(lua_State *L) {
  514. int id = luaL_checkinteger(L, 1);
  515. uint8_t onoff = lua_toboolean(L, 2);
  516. lua_pushboolean(L, !luat_camera_preview(id, onoff));
  517. return 1;
  518. }
  519. /**
  520. 配置摄像头参数
  521. @api camera.config(id, key, value)
  522. @int camera id,例如0
  523. @int 配置项的id
  524. @int 配置项的值
  525. @return nil 当前无返回值
  526. @usage
  527. -- 本函数于 2025.3.17 新增, 当前仅Air8101可用
  528. camera.config(0, camera.CONF_H264_QP_INIT, 16)
  529. camera.config(0, camera.CONF_H264_QP_I_MAX, 16)
  530. camera.config(0, camera.CONF_H264_QP_P_MAX, 8)
  531. camera.config(0, camera.CONF_H264_IMB_BITS, 3)
  532. camera.config(0, camera.CONF_H264_PMB_BITS, 1)
  533. */
  534. static int l_camera_config(lua_State *L) {
  535. int id = luaL_checkinteger(L, 1);
  536. int key = luaL_checkinteger(L, 2);
  537. int value = luaL_checkinteger(L, 3);
  538. int ret = luat_camera_config(id, key, value);
  539. lua_pushinteger(L, ret);
  540. return 1;
  541. }
  542. /*
  543. 对于无法用GPIO控制camera pwdn脚的平台,手动控制camera pwdn脚拉高或者拉低,2025/9/6启用
  544. @api camera.pwdn_pin(id, level)
  545. @int camera id,例如0
  546. @int pwdn脚电平,1高电平,0低电平
  547. @usage
  548. -- camera pwdn脚低电平
  549. camera.pwdn_pin(camera_id, 0)
  550. */
  551. static int l_camera_set_pwdn_pin(lua_State* L) {
  552. luat_camera_pwdn_pin(luaL_checkinteger(L, 1), luaL_optinteger(L, 2, 0));
  553. return 0;
  554. }
  555. /*
  556. 对于无法用GPIO控制camera reset脚的平台,手动控制camera reset脚拉高或者拉低,2025/9/6启用
  557. @api camera.reset_pin(level)
  558. @int camera id,例如0
  559. @int reset脚电平,1高电平,0低电平
  560. @usage
  561. -- camera reset脚高电平
  562. camera.reset_pin(camera_id, 1)
  563. */
  564. static int l_camera_set_reset_pin(lua_State* L) {
  565. luat_camera_reset_pin(luaL_checkinteger(L, 1), luaL_optinteger(L, 2, 1));
  566. return 0;
  567. }
  568. #include "rotable2.h"
  569. static const rotable_Reg_t reg_camera[] =
  570. {
  571. { "init" , ROREG_FUNC(l_camera_init )},
  572. { "start" , ROREG_FUNC(l_camera_start )},
  573. { "preview", ROREG_FUNC(l_camera_preview)},
  574. { "stop" , ROREG_FUNC(l_camera_stop)},
  575. { "capture", ROREG_FUNC(l_camera_capture)},
  576. { "video", ROREG_FUNC(l_camera_video)},
  577. { "startRaw", ROREG_FUNC(l_camera_start_raw)},
  578. { "getRaw", ROREG_FUNC(l_camera_get_raw)},
  579. { "close", ROREG_FUNC(l_camera_close)},
  580. { "on", ROREG_FUNC(l_camera_on)},
  581. { "config", ROREG_FUNC(l_camera_config)},
  582. { "pwdn_pin", ROREG_FUNC(l_camera_set_pwdn_pin)},
  583. { "reset_pin", ROREG_FUNC(l_camera_set_reset_pin)},
  584. //@const AUTO number 摄像头工作在自动模式
  585. { "AUTO", ROREG_INT(LUAT_CAMERA_MODE_AUTO)},
  586. //@const SCAN number 摄像头工作在扫码模式,只输出Y分量
  587. { "SCAN", ROREG_INT(LUAT_CAMERA_MODE_SCAN)},
  588. //@const USB number 摄像头类型,USB
  589. { "USB", ROREG_INT(LUAT_CAMERA_TYPE_USB)},
  590. //@const DVP number 摄像头类型,DVP
  591. { "DVP", ROREG_INT(LUAT_CAMERA_TYPE_DVP)},
  592. //@const ROTATE_0 number 摄像头预览,画面不旋转
  593. { "ROTATE_0", ROREG_INT(LUAT_CAMERA_PREVIEW_ROTATE_0)},
  594. //@const ROTATE_90 number 摄像头预览,画面旋转90度
  595. { "ROTATE_90", ROREG_INT(LUAT_CAMERA_PREVIEW_ROTATE_90)},
  596. //@const ROTATE_270 number 摄像头预览,画面旋转270度
  597. { "ROTATE_270", ROREG_INT(LUAT_CAMERA_PREVIEW_ROTATE_270)},
  598. //@const CONF_H264_QP_INIT number H264编码器初始化QP值
  599. { "CONF_H264_QP_INIT", ROREG_INT(LUAT_CAMERA_CONF_H264_QP_INIT)},
  600. //@const CONF_H264_QP_I_MAX number H264编码器I的最大QP值
  601. { "CONF_H264_QP_I_MAX", ROREG_INT(LUAT_CAMERA_CONF_H264_QP_I_MAX)},
  602. //@const CONF_H264_QP_P_MAX number H264编码器P的最大QP值
  603. { "CONF_H264_QP_P_MAX", ROREG_INT(LUAT_CAMERA_CONF_H264_QP_P_MAX)},
  604. //@const CONF_H264_IMB_BITS number H264编码器IMB_BITS值
  605. { "CONF_H264_IMB_BITS", ROREG_INT(LUAT_CAMERA_CONF_H264_IMB_BITS)},
  606. //@const CONF_H264_PMB_BITS number H264编码器PMB_BITS值
  607. { "CONF_H264_PMB_BITS", ROREG_INT(LUAT_CAMERA_CONF_H264_PMB_BITS)},
  608. //@const CONF_PREVIEW_ENABLE number 是否启动摄像头预览功能,默认开启
  609. { "CONF_PREVIEW_ENABLE", ROREG_INT(LUAT_CAMERA_CONF_PREVIEW_ENABLE)},
  610. //@const CONF_PREVIEW_ROTATE number 摄像头预览画面的旋转角度
  611. { "CONF_PREVIEW_ROTATE", ROREG_INT(LUAT_CAMERA_CONF_PREVIEW_ROTATE)},
  612. { NULL, ROREG_INT(0)}
  613. };
  614. LUAMOD_API int luaopen_camera( lua_State *L ) {
  615. luat_newlib2(L, reg_camera);
  616. return 1;
  617. }
  618. //------------------------------------------------------
  619. static int32_t l_camera_callback(lua_State *L, void* ptr) {
  620. rtos_msg_t *msg = (rtos_msg_t *)lua_topointer(L, -1);
  621. if (camera_idp) {
  622. if (msg->arg1 < 0) {
  623. lua_pushinteger(L, 0);
  624. } else {
  625. lua_pushinteger(L, msg->arg1);
  626. }
  627. luat_cbcwait(L, camera_idp, 1);
  628. camera_idp = 0;
  629. }
  630. return 0;
  631. }
  632. void luat_camera_async_init_result(int result) {
  633. rtos_msg_t msg = {0};
  634. msg.handler = l_camera_callback;
  635. msg.arg1 = result;
  636. luat_msgbus_put(&msg, 0);
  637. }