Advertisement
jamrab

Digital_Light_TSL2561.cpp

Apr 23rd, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. /*
  2.     Digital_Light_TSL2561.cpp
  3.     A library for TSL2561
  4.  
  5.     Copyright (c) 2012 seeed technology inc.
  6.     Website    : www.seeed.cc
  7.     Author     : zhangkun
  8.     Create Time:
  9.     Change Log :  Jack Shao, Nov 2014, bug fix and update for user experience
  10.  
  11.     The MIT License (MIT)
  12.  
  13.     Permission is hereby granted, free of charge, to any person obtaining a copy
  14.     of this software and associated documentation files (the "Software"), to deal
  15.     in the Software without restriction, including without limitation the rights
  16.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17.     copies of the Software, and to permit persons to whom the Software is
  18.     furnished to do so, subject to the following conditions:
  19.  
  20.     The above copyright notice and this permission notice shall be included in
  21.     all copies or substantial portions of the Software.
  22.  
  23.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29.     THE SOFTWARE.
  30. */
  31. #include <Digital_Light_TSL2561.h>
  32. #include <Particle.h>
  33. #include <Wire.h>
  34. uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address) {
  35.  
  36.     uint8_t value;
  37.     Wire.beginTransmission(deviceAddress);
  38.     Wire.write(address);                // register to read
  39.     Wire.endTransmission();
  40.     Wire.requestFrom(deviceAddress, 1); // read a byte
  41.     while (!Wire.available());
  42.     value = Wire.read();
  43.     //delay(100);
  44.     return value;
  45. }
  46.  
  47. void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val) {
  48.     Wire.beginTransmission(deviceAddress);  // start transmission to device
  49.     Wire.write(address);                    // send register address
  50.     Wire.write(val);                        // send value to write
  51.     Wire.endTransmission();                 // end transmission
  52.     //delay(100);
  53. }
  54. void TSL2561_CalculateLux::getLux(void) {
  55.     CH0_LOW = readRegister(TSL2561_Address, TSL2561_Channal0L);
  56.     CH0_HIGH = readRegister(TSL2561_Address, TSL2561_Channal0H);
  57.     //read two bytes from registers 0x0E and 0x0F
  58.     CH1_LOW = readRegister(TSL2561_Address, TSL2561_Channal1L);
  59.     CH1_HIGH = readRegister(TSL2561_Address, TSL2561_Channal1H);
  60.  
  61.     ch0 = (CH0_HIGH << 8) | CH0_LOW;
  62.     ch1 = (CH1_HIGH << 8) | CH1_LOW;
  63. }
  64. void TSL2561_CalculateLux::init() {
  65.     writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  66.     writeRegister(TSL2561_Address, TSL2561_Timing, 0x00); //No High Gain (1x), integration time of 13ms
  67.     writeRegister(TSL2561_Address, TSL2561_Interrupt, 0x00);
  68.     writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  69. }
  70.  
  71.  
  72. uint16_t TSL2561_CalculateLux::readIRLuminosity() { // read Infrared channel value only, not convert to lux.
  73.     writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  74.     delay(14);
  75.     getLux();
  76.  
  77.     writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  78.     if (ch1 == 0) {
  79.         return 0;
  80.     }
  81.     if (ch0 / ch1 < 2 && ch0 > 4900) {
  82.         return -1;  //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  83.     }
  84.     return ch1;
  85. }
  86.  
  87. uint16_t TSL2561_CalculateLux::readFSpecLuminosity() { //read Full Spectrum channel value only,  not convert to lux.
  88.     writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  89.     delay(14);
  90.     getLux();
  91.  
  92.     writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  93.     if (ch1 == 0) {
  94.         return 0;
  95.     }
  96.     if (ch0 / ch1 < 2 && ch0 > 4900) {
  97.         return -1;  //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  98.     }
  99.     return ch0;
  100. }
  101.  
  102. signed long TSL2561_CalculateLux::readVisibleLux() {
  103.     writeRegister(TSL2561_Address, TSL2561_Control, 0x03); // POWER UP
  104.     delay(14);
  105.     getLux();
  106.  
  107.     writeRegister(TSL2561_Address, TSL2561_Control, 0x00); // POWER Down
  108.     if (ch1 == 0) {
  109.         return 0;
  110.     }
  111.     if (ch0 / ch1 < 2 && ch0 > 4900) {
  112.         return -1;  //ch0 out of range, but ch1 not. the lux is not valid in this situation.
  113.     }
  114.     return calculateLux(0, 0, 0);  //T package, no gain, 13ms
  115. }
  116. unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt, int iType) {
  117.     switch (tInt) {
  118.         case 0:  // 13.7 msec
  119.             chScale = CHSCALE_TINT0;
  120.             break;
  121.         case 1: // 101 msec
  122.             chScale = CHSCALE_TINT1;
  123.             break;
  124.         default: // assume no scaling
  125.             chScale = (1 << CH_SCALE);
  126.             break;
  127.     }
  128.     if (!iGain) {
  129.         chScale = chScale << 4;    // scale 1X to 16X
  130.     }
  131.     // scale the channel values
  132.     channel0 = (ch0 * chScale) >> CH_SCALE;
  133.     channel1 = (ch1 * chScale) >> CH_SCALE;
  134.  
  135.     ratio1 = 0;
  136.     if (channel0 != 0) {
  137.         ratio1 = (channel1 << (RATIO_SCALE + 1)) / channel0;
  138.     }
  139.     // round the ratio value
  140.     unsigned long ratio = (ratio1 + 1) >> 1;
  141.  
  142.     switch (iType) {
  143.         case 0: // T package
  144.             if ((ratio >= 0) && (ratio <= K1T)) {
  145.                 b = B1T;
  146.                 m = M1T;
  147.             } else if (ratio <= K2T) {
  148.                 b = B2T;
  149.                 m = M2T;
  150.             } else if (ratio <= K3T) {
  151.                 b = B3T;
  152.                 m = M3T;
  153.             } else if (ratio <= K4T) {
  154.                 b = B4T;
  155.                 m = M4T;
  156.             } else if (ratio <= K5T) {
  157.                 b = B5T;
  158.                 m = M5T;
  159.             } else if (ratio <= K6T) {
  160.                 b = B6T;
  161.                 m = M6T;
  162.             } else if (ratio <= K7T) {
  163.                 b = B7T;
  164.                 m = M7T;
  165.             } else if (ratio > K8T) {
  166.                 b = B8T;
  167.                 m = M8T;
  168.             }
  169.             break;
  170.         case 1:// CS package
  171.             if ((ratio >= 0) && (ratio <= K1C)) {
  172.                 b = B1C;
  173.                 m = M1C;
  174.             } else if (ratio <= K2C) {
  175.                 b = B2C;
  176.                 m = M2C;
  177.             } else if (ratio <= K3C) {
  178.                 b = B3C;
  179.                 m = M3C;
  180.             } else if (ratio <= K4C) {
  181.                 b = B4C;
  182.                 m = M4C;
  183.             } else if (ratio <= K5C) {
  184.                 b = B5C;
  185.                 m = M5C;
  186.             } else if (ratio <= K6C) {
  187.                 b = B6C;
  188.                 m = M6C;
  189.             } else if (ratio <= K7C) {
  190.                 b = B7C;
  191.                 m = M7C;
  192.             }
  193.     }
  194.     temp = ((channel0 * b) - (channel1 * m));
  195.     if (temp < 0) {
  196.         temp = 0;
  197.     }
  198.     temp += (1 << (LUX_SCALE - 1));
  199.     // strip off fractional portion
  200.     lux = temp >> LUX_SCALE;
  201.     return (lux);
  202. }
  203. TSL2561_CalculateLux TSL2561;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement