luat_lib_dac.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. @module dac
  3. @summary 数模转换
  4. @version 1.0
  5. @date 2021.12.03
  6. @demo multimedia
  7. @tag LUAT_USE_DAC
  8. */
  9. #include "luat_base.h"
  10. #include "luat_dac.h"
  11. #include "luat_fs.h"
  12. #include "luat_mem.h"
  13. #define LUAT_LOG_TAG "dac"
  14. #include "luat_log.h"
  15. /*
  16. 打开DAC通道,并配置参数
  17. @api dac.open(ch, freq, mode)
  18. @int 通道编号,例如0
  19. @int 输出频率,单位hz
  20. @int 模式,默认为0,预留
  21. @return true 成功返回true,否则返回false
  22. @return int 底层返回值,调试用
  23. @usage
  24. if dac.open(0, 44000) then
  25. log.info("dac", "dac ch0 is opened")
  26. end
  27. */
  28. static int l_dac_open(lua_State *L) {
  29. int ch = luaL_checkinteger(L, 1);
  30. int freq = luaL_checkinteger(L, 2);
  31. int mode = luaL_optinteger(L, 3, 0);
  32. int ret = luat_dac_setup(ch, freq, mode);
  33. lua_pushboolean(L, ret == 0 ? 1 : 0);
  34. lua_pushinteger(L, ret);
  35. return 2;
  36. }
  37. /*
  38. 从指定DAC通道输出一段波形,或者单个值
  39. @api dac.write(ch, data)
  40. @int 通道编号,例如0
  41. @string 若输出固定值,可以填数值, 若输出波形,填string
  42. @return true 成功返回true,否则返回false
  43. @return int 底层返回值,调试用
  44. @usage
  45. if dac.open(0, 44000) then
  46. log.info("dac", "dac ch0 is opened")
  47. dac.write(0, string.fromHex("ABCDABCD"))
  48. end
  49. dac.close(0)
  50. */
  51. static int l_dac_write(lua_State *L) {
  52. uint16_t* buff;
  53. size_t len;
  54. int ch;
  55. uint16_t value;
  56. ch = luaL_checkinteger(L, 1);
  57. if (lua_isinteger(L, 2)) {
  58. value = luaL_checkinteger(L, 2);
  59. buff = &value;
  60. len = 2;
  61. }
  62. else if (lua_isuserdata(L, 2)) {
  63. return 0; // TODO 支持zbuff
  64. }
  65. else if (lua_isstring(L, 2)) {
  66. buff = (uint16_t*)luaL_checklstring(L, 2, &len);
  67. }
  68. else {
  69. return 0;
  70. }
  71. // 防御过短的数据
  72. if (len < 2) {
  73. return 0;
  74. }
  75. int ret = luat_dac_write(ch, buff, len >> 1);
  76. lua_pushboolean(L, ret == 0 ? 1 : 0);
  77. lua_pushinteger(L, ret);
  78. return 2;
  79. }
  80. // /*
  81. // 从指定DAC通道输出一段波形,数据从文件读取
  82. // @api dac.writeFile(ch, fd)
  83. // @int 通道编号,例如0
  84. // @string 文件路径
  85. // @return true 成功返回true,否则返回false
  86. // @return int 底层返回值,调试用
  87. // if dac.open(0, 44000) then
  88. // log.info("dac", "dac ch0 is opened")
  89. // dac.writeFile(0, "/luadb/test.wav")
  90. // end
  91. // dac.close(0)
  92. // */
  93. // static int l_dac_write_file(lua_State *L) {
  94. // uint16_t* buff;
  95. // int ch;
  96. // size_t buff_size = 4096;
  97. // size_t len;
  98. // ch = luaL_checkinteger(L, 1);
  99. // const char* path = luaL_checkstring(L, 2);
  100. // FILE* fd = luat_fs_fopen(path, "rb");
  101. // if (fd == NULL) {
  102. // LLOGD("file not exist %s", path);
  103. // lua_pushboolean(L, 0);
  104. // return 1;
  105. // }
  106. // if (lua_isinteger(L, 3)) {
  107. // buff_size = luaL_checkinteger(L, 3);
  108. // }
  109. // buff = luat_heap_malloc(buff_size);
  110. // if (buff == NULL) {
  111. // luat_fs_fclose(fd);
  112. // LLOGW("buff size too big? %d", buff_size);
  113. // lua_pushboolean(L, 0);
  114. // return 1;
  115. // }
  116. // while (1) {
  117. // len = luat_fs_fread(buff, buff_size, 1, fd);
  118. // if (len == 0) {
  119. // break;
  120. // }
  121. // luat_dac_write(ch, buff, len);
  122. // }
  123. // luat_heap_free(buff);
  124. // luat_fs_fclose(fd);
  125. // lua_pushboolean(L, 1);
  126. // return 1;
  127. // }
  128. /*
  129. 关闭DAC通道
  130. @api dac.close(ch)
  131. @int 通道编号,例如0
  132. @return true 成功返回true,否则返回false
  133. @return int 底层返回值,调试用
  134. @usage
  135. if dac.open(0, 44000) then
  136. log.info("dac", "dac ch0 is opened")
  137. dac.write(0, string.fromHex("ABCDABCD"))
  138. end
  139. dac.close(0)
  140. */
  141. static int l_dac_close(lua_State *L) {
  142. int ch = luaL_checkinteger(L, 1);
  143. int ret = luat_dac_close(ch);
  144. lua_pushboolean(L, ret == 0 ? 1 : 0);
  145. lua_pushinteger(L, ret);
  146. return 2;
  147. }
  148. #include "rotable2.h"
  149. static const rotable_Reg_t reg_dac[] =
  150. {
  151. { "open" , ROREG_FUNC(l_dac_open)},
  152. { "write" , ROREG_FUNC(l_dac_write)},
  153. // { "writeFile", ROREG_FUNC(l_dac_write_file)},
  154. { "close" , ROREG_FUNC(l_dac_close)},
  155. { NULL, ROREG_INT(0) }
  156. };
  157. LUAMOD_API int luaopen_dac( lua_State *L ) {
  158. luat_newlib2(L, reg_dac);
  159. return 1;
  160. }