luat_lib_camera.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. @module camera
  3. @summary 摄像头
  4. @version 1.0
  5. @date 2022.01.11
  6. */
  7. #include "luat_base.h"
  8. #include "luat_camera.h"
  9. #include "luat_msgbus.h"
  10. #define LUAT_LOG_TAG "camera"
  11. #include "luat_log.h"
  12. #define MAX_DEVICE_COUNT 2
  13. typedef struct luat_camera_cb {
  14. int scanned;
  15. } luat_camera_cb_t;
  16. static luat_camera_cb_t camera_cbs[MAX_DEVICE_COUNT];
  17. int l_camera_handler(lua_State *L, void* ptr) {
  18. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  19. lua_pop(L, 1);
  20. int camera_id = msg->arg1;
  21. if (camera_cbs[camera_id].scanned) {
  22. lua_geti(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  23. if (lua_isfunction(L, -1)) {
  24. lua_pushinteger(L, camera_id);
  25. lua_pushlstring(L, (char *)msg->ptr,msg->arg2);
  26. lua_call(L, 2, 0);
  27. }
  28. }
  29. lua_pushinteger(L, 0);
  30. return 1;
  31. }
  32. static int l_camera_init(lua_State *L){
  33. luat_camera_conf_t conf = {0};
  34. conf.lcd_conf = luat_lcd_get_default();
  35. if (lua_istable(L, 1)) {
  36. lua_pushliteral(L, "zbar_scan");
  37. lua_gettable(L, 1);
  38. if (lua_isinteger(L, -1)) {
  39. conf.zbar_scan = luaL_checkinteger(L, -1);
  40. }
  41. lua_pop(L, 1);
  42. lua_pushliteral(L, "i2c_id");
  43. lua_gettable(L, 1);
  44. if (lua_isinteger(L, -1)) {
  45. conf.i2c_id = luaL_checkinteger(L, -1);
  46. }
  47. lua_pop(L, 1);
  48. lua_pushliteral(L, "i2c_addr");
  49. lua_gettable(L, 1);
  50. if (lua_isinteger(L, -1)) {
  51. conf.i2c_addr = luaL_checkinteger(L, -1);
  52. }
  53. lua_pop(L, 1);
  54. lua_pushliteral(L, "sensor_width");
  55. lua_gettable(L, 1);
  56. if (lua_isinteger(L, -1)) {
  57. conf.sensor_width = luaL_checkinteger(L, -1);
  58. }
  59. lua_pop(L, 1);
  60. lua_pushliteral(L, "sensor_height");
  61. lua_gettable(L, 1);
  62. if (lua_isinteger(L, -1)) {
  63. conf.sensor_height = luaL_checkinteger(L, -1);
  64. }
  65. lua_pop(L, 1);
  66. lua_pushliteral(L, "color_bit");
  67. lua_gettable(L, 1);
  68. if (lua_isinteger(L, -1)) {
  69. conf.color_bit = luaL_checkinteger(L, -1);
  70. }
  71. lua_pop(L, 1);
  72. lua_pushliteral(L, "id_reg");
  73. lua_gettable(L, 1);
  74. if (lua_isinteger(L, -1)) {
  75. conf.id_reg = luaL_checkinteger(L, -1);
  76. }
  77. lua_pop(L, 1);
  78. lua_pushliteral(L, "id_value");
  79. lua_gettable(L, 1);
  80. if (lua_isinteger(L, -1)) {
  81. conf.id_value = luaL_checkinteger(L, -1);
  82. }
  83. lua_pop(L, 1);
  84. lua_pushliteral(L, "init_cmd");
  85. lua_gettable(L, 1);
  86. if (lua_istable(L, -1)) {
  87. conf.init_cmd_size = lua_rawlen(L, -1);
  88. conf.init_cmd = luat_heap_malloc(conf.init_cmd_size * sizeof(uint8_t));
  89. for (size_t i = 1; i <= conf.init_cmd_size; i++){
  90. lua_geti(L, -1, i);
  91. conf.init_cmd[i-1] = luaL_checkinteger(L, -1);
  92. lua_pop(L, 1);
  93. }
  94. }
  95. lua_pop(L, 1);
  96. }
  97. luat_camera_init(&conf);
  98. return 0;
  99. }
  100. /*
  101. 注册摄像头事件回调
  102. @api camera.on(id, event, func)
  103. @int camera id, camera 0写0, camera 1写1
  104. @string 事件名称
  105. @function 回调方法
  106. @return nil 无返回值
  107. @usage
  108. camera.on(0, "scanned", function(id, str)
  109. print(id, str)
  110. end)
  111. */
  112. static int l_camera_on(lua_State *L) {
  113. int camera_id = luaL_checkinteger(L, 1);
  114. const char* event = luaL_checkstring(L, 2);
  115. if (!strcmp("scanned", event)) {
  116. if (camera_cbs[camera_id].scanned != 0) {
  117. luaL_unref(L, LUA_REGISTRYINDEX, camera_cbs[camera_id].scanned);
  118. camera_cbs[camera_id].scanned = 0;
  119. }
  120. if (lua_isfunction(L, 3)) {
  121. lua_pushvalue(L, 3);
  122. camera_cbs[camera_id].scanned = luaL_ref(L, LUA_REGISTRYINDEX);
  123. }
  124. }
  125. return 0;
  126. }
  127. #include "rotable.h"
  128. static const rotable_Reg reg_camera[] =
  129. {
  130. { "init" , l_camera_init , 0},
  131. // { "open" , l_camera_open , 0},
  132. // { "close" , l_camera_close, 0},
  133. { "on", l_camera_on, 0},
  134. { NULL, NULL , 0}
  135. };
  136. LUAMOD_API int luaopen_camera( lua_State *L ) {
  137. luat_newlib(L, reg_camera);
  138. return 1;
  139. }