Autogain.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Autogain tests for Tsl2561Util namespace.
  3. Copyright: Joachim Banzhaf, 2018
  4. This file is part of the Joba_Tsl2561 Library.
  5. Joba_Tsl2561 is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Joba_Tsl2561 is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Joba_Tsl2561. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <Tsl2561Util.h>
  17. // to mimic Serial.printf() of esp8266 core for other platforms
  18. char *format( const char *fmt, ... ) {
  19. static char buf[128];
  20. va_list arg;
  21. va_start(arg, fmt);
  22. vsnprintf(buf, sizeof(buf), fmt, arg);
  23. buf[sizeof(buf)-1] = '\0';
  24. va_end(arg);
  25. return buf;
  26. }
  27. Tsl2561 Tsl(Wire);
  28. uint8_t id;
  29. void setup() {
  30. Serial.begin(115200);
  31. Wire.begin();
  32. while( !Tsl.begin() )
  33. ; // wait until chip detected or wdt reset
  34. Serial.println("\nStarting Tsl2561Util autogain loop");
  35. Tsl.on();
  36. Tsl.id(id);
  37. }
  38. void loop() {
  39. uint16_t scaledFull = 0xffff, scaledIr = 0xffff;
  40. uint32_t full = 0xffffffff, ir = 0xffffffff, milliLux = 0xffffffff;
  41. bool gain = false;
  42. Tsl2561::exposure_t exposure = Tsl2561::EXP_OFF;
  43. if( Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) ) {
  44. if( Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) ) {
  45. if( Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id), 5) ) {
  46. Serial.print(format("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, full: %7lu, ir: %7lu, gain: %d, exp: %d, lux: %5lu.%03lu\n",
  47. Tsl.address(), id, scaledFull, scaledIr, (unsigned long)full, (unsigned long)ir, gain, exposure, (unsigned long)milliLux/1000, (unsigned long)milliLux%1000));
  48. }
  49. else {
  50. Serial.print(format("Tsl2561Util::milliLux(full=%lu, ir=%lu) error\n", (unsigned long)full, (unsigned long)ir));
  51. }
  52. }
  53. else {
  54. Serial.print(format("Tsl2561Util::normalizedLuminosity(gain=%u, exposure=%u, sfull=%u, sir=%u, full=%lu, ir=%lu) error\n",
  55. gain, exposure, scaledFull, scaledIr, (unsigned long)full, (unsigned long)ir));
  56. }
  57. }
  58. else {
  59. Serial.print(format("Tsl2561Util::autoGain(gain=%u, exposure=%u, sfull=%u, sir=%u) error\n",
  60. gain, exposure, scaledFull, scaledIr));
  61. }
  62. delay(1000);
  63. }