tsl2561.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * tsl2561.c
  3. *
  4. * Created on: Aug 21, 2015
  5. * Author: Michael Lucas (Aeprox @github)
  6. */
  7. #include "module.h"
  8. #include "lauxlib.h"
  9. #include "platform.h"
  10. #include "../tsl2561/tsl2561.h"
  11. static uint16_t ch0;
  12. static uint16_t ch1;
  13. /* Initialises the device on pins sdapin and sclpin
  14. * Lua: status = tsl2561.init(sdapin, sclpin, address(optional), package(optional))
  15. */
  16. static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
  17. uint32_t sda;
  18. uint32_t scl;
  19. // check parameters
  20. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  21. return luaL_error(L, "wrong arg range");
  22. }
  23. sda = luaL_checkinteger(L, 1);
  24. scl = luaL_checkinteger(L, 2);
  25. if (scl == 0 || sda == 0) {
  26. return luaL_error(L, "no i2c for D0");
  27. }
  28. // init I2C
  29. uint8_t error = tsl2561Init(sda, scl);
  30. // Parse optional parameters
  31. if (lua_isnumber(L, 3)) {
  32. uint8_t address = luaL_checkinteger(L, 3);
  33. if (!((address == TSL2561_ADDRESS_GND) || (address == TSL2561_ADDRESS_FLOAT) || (address == TSL2561_ADDRESS_VDD))) {
  34. return luaL_error(L, "Invalid argument: address");
  35. }
  36. else{
  37. tsl2561SetAddress(address);
  38. }
  39. }
  40. if (lua_isnumber(L, 4)) {
  41. uint8_t package = luaL_checkinteger(L, 4);
  42. if (!((package == TSL2561_PACKAGE_T_FN_CL) || (package == TSL2561_PACKAGE_CS))) {
  43. return luaL_error(L, "Invalid argument: package");
  44. }
  45. else{
  46. tsl2561SetPackage(package);
  47. }
  48. }
  49. lua_pushinteger(L, error);
  50. return 1;
  51. }
  52. /* Sets the integration time and gain settings of the device
  53. * Lua: status = tsl2561.settiming(integration, gain)
  54. */
  55. static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) {
  56. // check variables
  57. if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
  58. return luaL_error(L, "wrong arg range");
  59. }
  60. uint8_t integration = luaL_checkinteger(L, 1);
  61. if (!((integration == TSL2561_INTEGRATIONTIME_13MS) || (integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))) {
  62. return luaL_error(L, "Invalid argument: integration");
  63. }
  64. uint8_t gain = luaL_checkinteger(L, 2);
  65. if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))) {
  66. return luaL_error(L, "Invalid argument: gain");
  67. }
  68. lua_pushinteger(L, tsl2561SetTiming(integration, gain));
  69. return 1;
  70. }
  71. /* Reads sensor values from device and return calculated lux
  72. * Lua: lux, status = tsl2561.getlux()
  73. */
  74. static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) {
  75. uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
  76. if (error) {
  77. lua_pushinteger(L, 0);
  78. lua_pushinteger(L, error);
  79. } else {
  80. lua_pushinteger(L, tsl2561CalculateLux(ch0, ch1));
  81. lua_pushinteger(L, error);
  82. }
  83. return 2;
  84. }
  85. /* Reads sensor values from device and returns them
  86. * Lua: ch0, ch1, status = tsl2561.getrawchannels()
  87. */
  88. static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) {
  89. uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
  90. lua_pushinteger(L, ch0);
  91. lua_pushinteger(L, ch1);
  92. lua_pushinteger(L, error);
  93. return 3;
  94. }
  95. // Module function map
  96. LROT_BEGIN(tsl2561, NULL, 0)
  97. LROT_FUNCENTRY( settiming, tsl2561_lua_settiming )
  98. LROT_FUNCENTRY( getlux, tsl2561_lua_calclux )
  99. LROT_FUNCENTRY( getrawchannels, tsl2561_lua_getchannels )
  100. LROT_FUNCENTRY( init, tsl2561_init )
  101. LROT_NUMENTRY( TSL2561_OK, TSL2561_ERROR_OK )
  102. LROT_NUMENTRY( TSL2561_ERROR_I2CINIT, TSL2561_ERROR_I2CINIT )
  103. LROT_NUMENTRY( TSL2561_ERROR_I2CBUSY, TSL2561_ERROR_I2CBUSY )
  104. LROT_NUMENTRY( TSL2561_ERROR_NOINIT, TSL2561_ERROR_NOINIT )
  105. LROT_NUMENTRY( TSL2561_ERROR_LAST, TSL2561_ERROR_LAST )
  106. LROT_NUMENTRY( INTEGRATIONTIME_13MS, TSL2561_INTEGRATIONTIME_13MS )
  107. LROT_NUMENTRY( INTEGRATIONTIME_101MS, TSL2561_INTEGRATIONTIME_101MS )
  108. LROT_NUMENTRY( INTEGRATIONTIME_402MS, TSL2561_INTEGRATIONTIME_402MS )
  109. LROT_NUMENTRY( GAIN_1X, TSL2561_GAIN_1X )
  110. LROT_NUMENTRY( GAIN_16X, TSL2561_GAIN_16X )
  111. LROT_NUMENTRY( PACKAGE_CS, TSL2561_PACKAGE_CS )
  112. LROT_NUMENTRY( PACKAGE_T_FN_CL, TSL2561_PACKAGE_T_FN_CL )
  113. LROT_NUMENTRY( ADDRESS_GND, TSL2561_ADDRESS_GND )
  114. LROT_NUMENTRY( ADDRESS_FLOAT, TSL2561_ADDRESS_FLOAT )
  115. LROT_NUMENTRY( ADDRESS_VDD, TSL2561_ADDRESS_VDD )
  116. LROT_END(tsl2561, NULL, 0)
  117. NODEMCU_MODULE(TSL2561, "tsl2561", tsl2561, NULL);