Sound2Light auf RGB(W)-LED-Leisten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_utils.c 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * vim: sw=2 ts=2 expandtab
  3. *
  4. * THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
  5. * <cfr34k@tkolb.de> wrote this file. As long as you retain this notice you can
  6. * do whatever you want with this stuff. If we meet some day, and you think
  7. * this stuff is worth it, you can buy me a pizza in return. - Thomas Kolb
  8. */
  9. #include <lua.h>
  10. #include <lualib.h>
  11. #include <lauxlib.h>
  12. #include "lua_utils.h"
  13. void lua_showerror(lua_State *L, const char *msg) {
  14. fprintf(stderr, "\nLUA ERROR:\n %s: %s\n\n",
  15. msg, lua_tostring(L, -1));
  16. }
  17. void lua_pushdoublearray(lua_State *L, double *numbers, size_t len) {
  18. size_t i;
  19. // create an empty table
  20. lua_createtable(L, len, 0);
  21. for(i = 0; i < len; i++) {
  22. // push key and value
  23. lua_pushnumber(L, i+1); // lua arrays count from 1
  24. lua_pushnumber(L, numbers[i]);
  25. // store the values in the table
  26. lua_settable(L, -3);
  27. }
  28. }
  29. void lua_pushsamplearray(lua_State *L, sample *numbers, size_t len) {
  30. size_t i;
  31. // create an empty table
  32. lua_createtable(L, len, 0);
  33. for(i = 0; i < len; i++) {
  34. // push key and value
  35. lua_pushnumber(L, i+1); // lua arrays count from 1
  36. lua_pushnumber(L, numbers[i]);
  37. // store the values in the table
  38. lua_settable(L, -3);
  39. }
  40. }
  41. void lua_readdoublearray(lua_State *L, double *numbers, size_t len) {
  42. size_t k;
  43. double v;
  44. // go to the top of the stack
  45. lua_pushnil(L);
  46. while(lua_next(L, -2)) {
  47. v = lua_tonumber(L, -1);
  48. lua_pop(L, 1);
  49. k = lua_tointeger(L, -1);
  50. if(k > len || k < 1) {
  51. fprintf(stderr, "Warning: Lua index (%u) is out of C array range (%u)!\n", k, len);
  52. } else {
  53. numbers[k-1] = v;
  54. }
  55. }
  56. lua_pop(L, 1);
  57. }