Testing.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Tests for the Tsl2561 class.
  3. It shows how to use every available method.
  4. Copyright: Joachim Banzhaf, 2018
  5. This file is part of the Joba_Tsl2561 Library.
  6. Joba_Tsl2561 is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Joba_Tsl2561 is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Joba_Tsl2561. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <Tsl2561.h>
  18. // to mimic Serial.printf() of esp8266 core for other platforms
  19. char *format( const char *fmt, ... ) {
  20. static char buf[128];
  21. va_list arg;
  22. va_start(arg, fmt);
  23. vsnprintf(buf, sizeof(buf), fmt, arg);
  24. buf[sizeof(buf)-1] = '\0';
  25. va_end(arg);
  26. return buf;
  27. }
  28. Tsl2561 Tsl(Wire);
  29. void showError( Tsl2561 &tsl ) {
  30. Tsl2561::status_t status = tsl.status();
  31. Serial.print(format("Error was %u: ", status));
  32. switch( status ) {
  33. case Tsl2561::ERR_OK: Serial.println("None"); break;
  34. case Tsl2561::ERR_RW: Serial.println("Read/Write"); break;
  35. case Tsl2561::ERR_BUSY: Serial.println("Busy"); break;
  36. case Tsl2561::ERR_GONE: Serial.println("Gone"); break;
  37. case Tsl2561::ERR_GENERAL: Serial.println("General"); break;
  38. default: Serial.println("Unknown"); break;
  39. }
  40. }
  41. void testSensitivity( Tsl2561 &tsl, bool newGain, Tsl2561::exposure_t newExp ) {
  42. if( tsl.on() ) {
  43. uint32_t start = millis();
  44. Serial.print(format("Chip powered on at %lu\n", (unsigned long)start));
  45. bool chipGain;
  46. Tsl2561::exposure_t chipExp;
  47. bool change = true;
  48. if( tsl.getSensitivity(chipGain, chipExp) ) {
  49. if( chipGain == newGain && chipExp == newExp ) {
  50. change = false;
  51. }
  52. }
  53. else {
  54. Serial.print("getSensitivity failed. ");
  55. showError(tsl);
  56. }
  57. bool check = true;
  58. if( change ) {
  59. if( tsl.setSensitivity(newGain, newExp) ) {
  60. Serial.print(format("New gain = %d, exposure = 0x%02x\n", newGain, newExp));
  61. }
  62. else {
  63. check = false;
  64. Serial.print("setSensitivity failed. ");
  65. showError(tsl);
  66. }
  67. }
  68. if( check ) {
  69. uint16_t ir, full = 0;
  70. while( !full && millis() - start < 1000 ) {
  71. if( !tsl.fullLuminosity(full) ) {
  72. Serial.print("Check full luminosity failed. ");
  73. showError(tsl);
  74. }
  75. if( full ) {
  76. if( !tsl.irLuminosity(ir) ) {
  77. Serial.print("Check ir luminosity failed. ");
  78. showError(tsl);
  79. }
  80. }
  81. else {
  82. delay(10);
  83. }
  84. }
  85. if( !full ) {
  86. Serial.println("No luminosity reading after 1s. Too dark?");
  87. }
  88. else {
  89. Serial.print(format("Got luminosity after %lu ms. Full spectrum is %u and IR only is %u\n", (unsigned long)millis() - start, full, ir));
  90. }
  91. }
  92. if( !tsl.off() ) {
  93. Serial.print("Power off failed. ");
  94. showError(tsl);
  95. }
  96. }
  97. else {
  98. Serial.print("Power on failed. ");
  99. showError(tsl);
  100. }
  101. }
  102. bool testPackage( Tsl2561 &tsl ) {
  103. uint8_t id;
  104. if( tsl.id(id) ) {
  105. Serial.print(format("Chip has type %02x and revision %x\n", Tsl2561::type(id), Tsl2561::revision(id)));
  106. if( Tsl2561::packageT_FN_CL(id) ) {
  107. Serial.println("Chip is a T, FN or CL type package");
  108. }
  109. else if( Tsl2561::packageCS(id) ) {
  110. Serial.println("Chip is a CS type package");
  111. }
  112. else {
  113. Serial.println("Chip is an unknown package");
  114. }
  115. return true;
  116. }
  117. else {
  118. Serial.print("Get Chip ID failed. ");
  119. showError(tsl);
  120. }
  121. return false;
  122. }
  123. void test( Tsl2561 &tsl ) {
  124. bool ok = tsl.available();
  125. Serial.print(format("\nTesting Tsl2561 at address %02x: %sfound\n", tsl.address(), ok ? "" : "NOT "));
  126. if( ok ) {
  127. if( testPackage(tsl) ) {
  128. testSensitivity(tsl, Tsl2561::GAIN_OFF, Tsl2561::EXP_402);
  129. testSensitivity(tsl, Tsl2561::GAIN_ON, Tsl2561::EXP_402);
  130. testSensitivity(tsl, Tsl2561::GAIN_OFF, Tsl2561::EXP_101);
  131. testSensitivity(tsl, Tsl2561::GAIN_ON, Tsl2561::EXP_101);
  132. testSensitivity(tsl, Tsl2561::GAIN_OFF, Tsl2561::EXP_14);
  133. testSensitivity(tsl, Tsl2561::GAIN_ON, Tsl2561::EXP_14);
  134. }
  135. }
  136. else {
  137. showError(tsl);
  138. }
  139. }
  140. void setup() {
  141. Serial.begin(115200);
  142. Wire.begin();
  143. Serial.println("\nStarting Tsl2561 testing loop");
  144. }
  145. void loop() {
  146. Tsl.begin(Tsl2561::ADDR_GND);
  147. test(Tsl);
  148. Tsl.begin(Tsl2561::ADDR_FLOAT);
  149. test(Tsl);
  150. Tsl.begin(Tsl2561::ADDR_VDD);
  151. test(Tsl);
  152. Serial.println("\nNext test in 5s\n");
  153. delay(5000);
  154. }