Utility.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Simple 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::address_t addr[] = { Tsl2561::ADDR_GND, Tsl2561::ADDR_FLOAT, Tsl2561::ADDR_VDD };
  28. Tsl2561 Tsl(Wire);
  29. void setup() {
  30. Serial.begin(115200);
  31. Wire.begin();
  32. Serial.println("\nStarting Tsl2561Util loop");
  33. }
  34. void loop() {
  35. bool found = false;
  36. for( uint8_t i = 0; i < sizeof(addr)/sizeof(addr[0]); i++ ) {
  37. if( Tsl.begin(addr[i]) ) {
  38. found = true;
  39. Serial.println();
  40. uint16_t scaledFull = 0, scaledIr;
  41. uint32_t full, ir, milliLux;
  42. uint8_t id;
  43. bool gain;
  44. Tsl2561::exposure_t exposure;
  45. for( uint8_t g=0; g<2; g++ ) {
  46. gain = g;
  47. for( uint8_t e=0; e<3; e++ ) {
  48. exposure = (Tsl2561::exposure_t)e;
  49. Tsl.on();
  50. Tsl.setSensitivity(gain, exposure);
  51. Tsl2561Util::waitNext(exposure);
  52. Tsl.id(id);
  53. Tsl.getSensitivity(gain, exposure);
  54. Tsl.fullLuminosity(scaledFull);
  55. Tsl.irLuminosity(scaledIr);
  56. Serial.print(format("Tsl2561 addr: 0x%02x, id: 0x%02x, sfull: %5u, sir: %5u, gain: %d, exp: %d",
  57. addr[i], id, scaledFull, scaledIr, gain, exposure));
  58. if( Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) ) {
  59. if( Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id)) ) {
  60. Serial.print(format(", full: %5lu, ir: %5lu, lux: %5lu.%03lu\n", (unsigned long)full, (unsigned long)ir, (unsigned long)milliLux/1000, (unsigned long)milliLux%1000));
  61. }
  62. else {
  63. Serial.print(format(", full: %5lu, ir: %5lu: Tsl2561Util::milliLux() error\n", (unsigned long)full, (unsigned long)ir));
  64. }
  65. }
  66. else {
  67. Serial.print(format(", full: %5lu, ir: %5lu: Tsl2561Util::normalizedLuminosity() error\n", (unsigned long)full, (unsigned long)ir));
  68. }
  69. Tsl.off();
  70. }
  71. }
  72. }
  73. }
  74. if( !found ) {
  75. Serial.println("No Tsl2561 found. Check wiring.");
  76. }
  77. delay(5000);
  78. }