luat_lib_camera.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. @module camera
  3. @summary 摄像头
  4. @version 1.0
  5. @date 2022.01.11
  6. @demo camera
  7. */
  8. #include "luat_base.h"
  9. #include "luat_camera.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_fs.h"
  12. #include "luat_malloc.h"
  13. #include "luat_uart.h"
  14. #define LUAT_LOG_TAG "camera"
  15. #include "luat_log.h"
  16. #define MAX_DEVICE_COUNT 2
  17. typedef struct luat_camera_cb {
  18. int scanned;
  19. } luat_camera_cb_t;
  20. static luat_camera_cb_t camera_cbs[MAX_DEVICE_COUNT];
  21. int l_camera_handler(lua_State *L, void* ptr) {
  22. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  23. lua_pop(L, 1);
  24. int camera_id = msg->arg1;
  25. if (camera_cbs[camera_id].scanned) {
  26. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  27. if (lua_isfunction(L, -1)) {
  28. lua_pushinteger(L, camera_id);
  29. if (msg->ptr)
  30. {
  31. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  32. }
  33. else
  34. {
  35. lua_pushboolean(L, msg->arg2);
  36. }
  37. lua_call(L, 2, 0);
  38. }
  39. }
  40. lua_pushinteger(L, 0);
  41. return 1;
  42. }
  43. /*
  44. 初始化摄像头
  45. @api camera.init(InitReg)
  46. @table InitReg camera初始化命令 见demo/camera/AIR105 注意:如扫码 camera初始化时需设置为灰度输出
  47. @return int camera_id
  48. @usage
  49. camera_id = camera.init(GC032A_InitReg)--屏幕输出rgb图像
  50. --初始化后需要start才开始输出/扫码
  51. camera.start(camera_id)--开始指定的camera
  52. */
  53. static int l_camera_init(lua_State *L){
  54. luat_camera_conf_t conf = {0};
  55. conf.lcd_conf = luat_lcd_get_default();
  56. if (lua_istable(L, 1)) {
  57. lua_pushliteral(L, "zbar_scan");
  58. lua_gettable(L, 1);
  59. if (lua_isinteger(L, -1)) {
  60. conf.zbar_scan = luaL_checkinteger(L, -1);
  61. }
  62. lua_pop(L, 1);
  63. lua_pushliteral(L, "draw_lcd");
  64. lua_gettable(L, 1);
  65. if (lua_isinteger(L, -1)) {
  66. conf.draw_lcd = luaL_checkinteger(L, -1);
  67. }
  68. lua_pop(L, 1);
  69. lua_pushliteral(L, "i2c_id");
  70. lua_gettable(L, 1);
  71. if (lua_isinteger(L, -1)) {
  72. conf.i2c_id = luaL_checkinteger(L, -1);
  73. }
  74. lua_pop(L, 1);
  75. lua_pushliteral(L, "i2c_addr");
  76. lua_gettable(L, 1);
  77. if (lua_isinteger(L, -1)) {
  78. conf.i2c_addr = luaL_checkinteger(L, -1);
  79. }
  80. lua_pop(L, 1);
  81. lua_pushliteral(L, "pwm_id");
  82. lua_gettable(L, 1);
  83. if (lua_isinteger(L, -1)) {
  84. conf.pwm_id = luaL_checkinteger(L, -1);
  85. }
  86. lua_pop(L, 1);
  87. lua_pushliteral(L, "pwm_period");
  88. lua_gettable(L, 1);
  89. if (lua_isinteger(L, -1)) {
  90. conf.pwm_period = luaL_checkinteger(L, -1);
  91. }
  92. lua_pop(L, 1);
  93. lua_pushliteral(L, "pwm_pulse");
  94. lua_gettable(L, 1);
  95. if (lua_isinteger(L, -1)) {
  96. conf.pwm_pulse = luaL_checkinteger(L, -1);
  97. }
  98. lua_pop(L, 1);
  99. lua_pushliteral(L, "sensor_width");
  100. lua_gettable(L, 1);
  101. if (lua_isinteger(L, -1)) {
  102. conf.sensor_width = luaL_checkinteger(L, -1);
  103. }
  104. lua_pop(L, 1);
  105. lua_pushliteral(L, "sensor_height");
  106. lua_gettable(L, 1);
  107. if (lua_isinteger(L, -1)) {
  108. conf.sensor_height = luaL_checkinteger(L, -1);
  109. }
  110. lua_pop(L, 1);
  111. lua_pushliteral(L, "color_bit");
  112. lua_gettable(L, 1);
  113. if (lua_isinteger(L, -1)) {
  114. conf.color_bit = luaL_checkinteger(L, -1);
  115. }
  116. lua_pop(L, 1);
  117. lua_pushliteral(L, "id_reg");
  118. lua_gettable(L, 1);
  119. if (lua_isinteger(L, -1)) {
  120. conf.id_reg = luaL_checkinteger(L, -1);
  121. }
  122. lua_pop(L, 1);
  123. lua_pushliteral(L, "id_value");
  124. lua_gettable(L, 1);
  125. if (lua_isinteger(L, -1)) {
  126. conf.id_value = luaL_checkinteger(L, -1);
  127. }
  128. lua_pop(L, 1);
  129. lua_pushliteral(L, "init_cmd");
  130. lua_gettable(L, 1);
  131. if (lua_istable(L, -1)) {
  132. conf.init_cmd_size = lua_rawlen(L, -1);
  133. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  134. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  135. lua_geti(L, -1, i);
  136. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  137. lua_pop(L, 1);
  138. }
  139. }else if(lua_isstring(L, -1)){
  140. int len,cmd;
  141. const char *fail_name = luaL_checklstring(L, -1, &len);
  142. FILE* fd = luat_fs_fopen(fail_name, "rb");
  143. conf.init_cmd_size = 0;
  144. if (fd){
  145. #define INITCMD_BUFF_SIZE 128
  146. char init_cmd_buff[INITCMD_BUFF_SIZE] ;
  147. conf.init_cmd = luat_heap_malloc(sizeof(uint8_t));
  148. while (1) {
  149. memset(init_cmd_buff, 0, INITCMD_BUFF_SIZE);
  150. int readline_len = luat_fs_readline(init_cmd_buff, INITCMD_BUFF_SIZE-1, fd);
  151. if (readline_len < 1)
  152. break;
  153. if (memcmp(init_cmd_buff, "#", 1)==0){
  154. continue;
  155. }
  156. char *token = strtok(init_cmd_buff, ",");
  157. if (sscanf(token,"%x",&cmd) < 1){
  158. continue;
  159. }
  160. conf.init_cmd_size = conf.init_cmd_size + 1;
  161. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  162. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  163. while( token != NULL ) {
  164. token = strtok(NULL, ",");
  165. if (sscanf(token,"%x",&cmd) < 1){
  166. break;
  167. }
  168. conf.init_cmd_size = conf.init_cmd_size + 1;
  169. conf.init_cmd = luat_heap_realloc(conf.init_cmd,conf.init_cmd_size * sizeof(uint8_t));
  170. conf.init_cmd[conf.init_cmd_size-1]=cmd;
  171. }
  172. }
  173. conf.init_cmd[conf.init_cmd_size]= 0;
  174. luat_fs_fclose(fd);
  175. }else{
  176. LLOGE("init_cmd fail open error");
  177. }
  178. }
  179. lua_pop(L, 1);
  180. }
  181. lua_pushinteger(L, luat_camera_init(&conf));
  182. return 1;
  183. }
  184. /*
  185. 注册摄像头事件回调
  186. @api camera.on(id, event, func)
  187. @int camera id, camera 0写0, camera 1写1
  188. @string 事件名称
  189. @function 回调方法
  190. @return nil 无返回值
  191. @usage
  192. camera.on(0, "scanned", function(id, str)
  193. print(id, str)
  194. end)
  195. */
  196. static int l_camera_on(lua_State *L) {
  197. int camera_id = luaL_checkinteger(L, 1);
  198. const char* event = luaL_checkstring(L, 2);
  199. if (!strcmp("scanned", event)) {
  200. if (camera_cbs[camera_id].scanned != 0) {
  201. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  202. camera_cbs[camera_id].scanned = 0;
  203. }
  204. if (lua_isfunction(L, 3)) {
  205. lua_pushvalue(L, 3);
  206. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  207. }
  208. }
  209. return 0;
  210. }
  211. /**
  212. 开始指定的camera
  213. @api camera.start(id)
  214. @int camera id,例如0
  215. @return boolean 成功返回true,否则返回false
  216. @usage
  217. camera.start(0)
  218. */
  219. static int l_camera_start(lua_State *L) {
  220. int id = luaL_checkinteger(L, 1);
  221. lua_pushboolean(L, luat_camera_start(id) == 0 ? 1 : 0);
  222. return 1;
  223. }
  224. /**
  225. 停止指定的camera
  226. @api camera.stop(id)
  227. @int camera id,例如0
  228. @return boolean 成功返回true,否则返回false
  229. @usage
  230. camera.stop(0)
  231. */
  232. static int l_camera_stop(lua_State *L) {
  233. int id = luaL_checkinteger(L, 1);
  234. lua_pushboolean(L, luat_camera_stop(id) == 0 ? 1 : 0);
  235. return 1;
  236. }
  237. /**
  238. 关闭指定的camera,释放相应的IO资源
  239. @api camera.close(id)
  240. @int camera id,例如0
  241. @return boolean 成功返回true,否则返回false
  242. @usage
  243. camera.close(0)
  244. */
  245. static int l_camera_close(lua_State *L) {
  246. int id = luaL_checkinteger(L, 1);
  247. lua_pushboolean(L, luat_camera_close(id) == 0 ? 1 : 0);
  248. return 1;
  249. }
  250. LUAT_WEAK luat_camera_capture(int id, uint8_t quality, const char *path) {
  251. LLOGD("not support yet");
  252. return -1;
  253. }
  254. LUAT_WEAK luat_camera_video(int id, int w, int h, uint8_t uart_id) {
  255. LLOGD("not support yet");
  256. return -1;
  257. }
  258. /**
  259. camera拍照
  260. @api camera.capture(id, y_diff, save_path, quality)
  261. @int camera id,例如0
  262. @int y_diff,Y分量校准量,0~255,越大越亮,根据实际情况修改,GC032A测试下来需要填0
  263. @string save_path,文件保存路径,空则写在上次路径里,默认是/capture.jpg
  264. @int quality, jpeg压缩质量,1最差,占用空间小,3最高,占用空间最大而且费时间,默认1
  265. @return boolean 成功返回true,否则返回false
  266. @usage
  267. camera.capture(0)
  268. */
  269. static int l_camera_capture(lua_State *L) {
  270. int id = luaL_checkinteger(L, 1);
  271. const char* save_path = luaL_checkstring(L, 2);
  272. int quality = luaL_optinteger(L, 3, 1);
  273. luat_camera_capture(id, quality, save_path);
  274. return 0;
  275. }
  276. /**
  277. camera输出视频流
  278. @api camera.video(id, w, h, out_path)
  279. @int camera id,例如0
  280. @int 宽度
  281. @int 高度
  282. @int 输出路径,目前只能用虚拟串口0
  283. @return boolean 成功返回true,否则返回false
  284. @usage
  285. camera.video(0, 320, 240, uart.VUART_0)
  286. */
  287. static int l_camera_video(lua_State *L) {
  288. int id = luaL_checkinteger(L, 1);
  289. int w = luaL_optinteger(L, 2, 320);
  290. int h = luaL_optinteger(L, 3, 240);
  291. int param = luaL_optinteger(L, 4, LUAT_VUART_ID_0);
  292. luat_camera_video(id, w, h, param);
  293. return 0;
  294. }
  295. #include "rotable2.h"
  296. static const rotable_Reg_t reg_camera[] =
  297. {
  298. { "init" , ROREG_FUNC(l_camera_init )},
  299. { "start" , ROREG_FUNC(l_camera_start )},
  300. { "stop" , ROREG_FUNC(l_camera_stop)},
  301. { "capture", ROREG_FUNC(l_camera_capture)},
  302. { "video", ROREG_FUNC(l_camera_video)},
  303. { "close", ROREG_FUNC(l_camera_close)},
  304. { "on", ROREG_FUNC(l_camera_on)},
  305. { NULL, {}}
  306. };
  307. LUAMOD_API int luaopen_camera( lua_State *L ) {
  308. luat_newlib2(L, reg_camera);
  309. return 1;
  310. }