summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/tsl2561/tsl2561.cxx
blob: 7bbbfc0eecbb0e4718745017e58bbfd79c122aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Author: Nandkishor Sonar <Nandkishor.Sonar@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561]
 *   Inspiration and lux calculation formulas from data sheet
 *   URL: http://www.adafruit.com/datasheets/TSL2561.pdf
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#include <string>
#include <stdexcept>
#include <unistd.h>
#include "tsl2561.h"

using namespace upm;


TSL2561::TSL2561(int bus, uint8_t devAddr, uint8_t gain, uint8_t integrationTime)
                                                : m_i2ControlCtx(bus)
{
    m_controlAddr = devAddr;
    m_bus = bus;
    m_gain = gain ;
    m_integrationTime = integrationTime;

    m_name = "TSL2561- Digital Light Sensor";

    mraa::Result error = m_i2ControlCtx.address(m_controlAddr);
    if (error != mraa::SUCCESS) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_i2c_address() failed");
        return;
    }

    // POWER UP.
    error = m_i2ControlCtx.writeReg(REGISTER_Control, CONTROL_POWERON);
    if (error != mraa::SUCCESS) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                               ": Unable to power up TSL2561");
        return;
    }
    // Power on Settling time
    usleep(1000);

    // Gain & Integration time .
    error = m_i2ControlCtx.writeReg(REGISTER_Timing, m_gain | m_integrationTime);
    if (error != mraa::SUCCESS) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": Unable to set gain/time");
        return;
    }

    // Set interrupt threshold to default.
    error = m_i2ControlCtx.writeReg(REGISTER_Interrupt, 0x00);
    if (error != mraa::SUCCESS) {
        throw std::runtime_error(std::string(__FUNCTION__) +
                                 ": Unable to set interrupt threshold");
        return;
    }
}

TSL2561::~TSL2561()
{
    // POWER DOWN
    m_i2ControlCtx.writeReg(REGISTER_Control, CONTROL_POWEROFF);
}

int
TSL2561::getLux()
{
    mraa::Result error = mraa::SUCCESS;
    int lux;
    uint16_t rawLuxCh0;
    uint16_t rawLuxCh1;
    uint8_t ch0_low, ch0_high, ch1_low, ch1_high;

    error = i2cReadReg(REGISTER_Channal0L, ch0_low);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: Unable to read channel0L in getRawLux()\n");
        return error;
    }

    error = i2cReadReg(REGISTER_Channal0H, ch0_high);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: Unable to read channel0H in getRawLux()\n");
        return error;
    }

    rawLuxCh0 = ch0_high*256+ch0_low;

    error= i2cReadReg(REGISTER_Channal1L, ch1_low);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: Unable to read channel1L in getRawLux()\n");
        return error;
    }

    error = i2cReadReg(REGISTER_Channal1H, ch1_high);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: Unable to read channel1H in getRawLux()\n");
        return error;
    }

    rawLuxCh1 = ch1_high*256+ch1_low;

    uint64_t scale = 0;

    switch (m_integrationTime)
    {
      case 0: // 13.7 msec
         scale = LUX_CHSCALE_TINT0;
      break;
      case 1: // 101 msec
         scale = LUX_CHSCALE_TINT1;
      break;
      default: // assume no scaling
         scale = (1 << LUX_CHSCALE);
      break;
    }

    // scale if gain is NOT 16X
    if (!m_gain) scale = scale << 4;

    uint64_t channel1 = 0;
    uint64_t channel0 = 0;
    // scale the channel values
    channel0 = (rawLuxCh0 * scale) >> LUX_CHSCALE;
    channel1 = (rawLuxCh1 * scale) >> LUX_CHSCALE;

    // find the ratio of the channel values (Channel1/Channel0)
    // protect against divide by zero
    unsigned long ratio1 = 0;
    if (channel0 != 0) ratio1 = (channel1 << (LUX_RATIOSCALE+1)) / channel0;

    // round the ratio value
    unsigned long ratio = (ratio1 + 1) >> 1;

    unsigned int b, m;

    // CS package
    // Check if ratio <= eachBreak ?
    if ((ratio >= 0) && (ratio <= LUX_K1C))
       {b=LUX_B1C; m=LUX_M1C;}
    else if (ratio <= LUX_K2C)
       {b=LUX_B2C; m=LUX_M2C;}
    else if (ratio <= LUX_K3C)
       {b=LUX_B3C; m=LUX_M3C;}
    else if (ratio <= LUX_K4C)
       {b=LUX_B4C; m=LUX_M4C;}
    else if (ratio <= LUX_K5C)
       {b=LUX_B5C; m=LUX_M5C;}
    else if (ratio <= LUX_K6C)
       {b=LUX_B6C; m=LUX_M6C;}
    else if (ratio <= LUX_K7C)
       {b=LUX_B7C; m=LUX_M7C;}
    else if (ratio > LUX_K8C)
       {b=LUX_B8C; m=LUX_M8C;}

    uint64_t tempLux = 0;
    tempLux = ((channel0 * b) - (channel1 * m));
    // do not allow negative lux value
    if (tempLux < 0) tempLux = 0;

    // round lsb (2^(LUX_SCALE-1))
    tempLux += (1 << (LUX_SCALE-1));

    // strip off fractional portion
    lux = tempLux >> LUX_SCALE;

    return lux;
}


mraa::Result
TSL2561::i2cWriteReg (uint8_t reg, uint8_t value)
{
    mraa::Result error = mraa::SUCCESS;

    // Start transmission to device
    error = m_i2ControlCtx.address (m_controlAddr);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: on i2c bus address setup in i2cWriteReg()\n");
        return error;
    }
    // Write register to I2C
    error = m_i2ControlCtx.writeByte (reg);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: on i2c bus write reg in i2cWriteReg()\n");
        return error;
    }

    // Write value to I2C
    error = m_i2ControlCtx.writeByte (value);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: on i2c bus write value in i2cWriteReg()\n");
        return error;
    }

    usleep(100000);

    return error;
}

mraa::Result
TSL2561::i2cReadReg(uint8_t reg, uint8_t &data)
{
    mraa::Result error = mraa::SUCCESS;

    // Start transmission to device
    error = m_i2ControlCtx.address(m_controlAddr);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: on i2c bus address setup in i2cReadReg()\n");
        return error;
    }

    // Send address of register to be read.
    error = m_i2ControlCtx.writeByte(reg);
    if (error != mraa::SUCCESS) {
        fprintf(stderr, "Error: on i2c bus write in i2cReadReg()\n");
        return error;
    }

    // Read byte.
    data = m_i2ControlCtx.readByte();

    usleep(10000);

    return error;
}