luat_lib_camera.c 20 KB

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