-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtsl2561.c
215 lines (183 loc) · 6.54 KB
/
tsl2561.c
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
/**
* @file tsl2561.c
* @author Otavio Ribeiro
* @date 02 Ago 2019
* @brief Communicating with tsl2561 digital light sensor through i2c
*
* Copyright (c) 2019 Otávio Ribeiro <[email protected]>
*
* 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 "nrf_log.h"
#include "twi_config.h"
#include "nrf_drv_twi.h"
#include "tsl2561.h"
#include "tsl2561_lux.h"
/* Buffer for samples read from light sensor. */
volatile static char tsl2561_twi_processing = 0;
static uint8_t tsl2561_buffer[5];
/**
* @brief TWI events handler.
*/
void tsl2561_twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
if(tsl2561_twi_processing == 0) return;
switch (p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
tsl2561_twi_processing = 0;
break;
default:
NRF_LOG_INFO("Unexpected TWI event received of type = 0x%x, xfer = 0x%x", p_event->type, p_event->xfer_desc.type);
tsl2561_twi_processing = 0;
break;
}
}
/**
* @brief Function for reading sensor id
*/
uint8_t tsl2561_read_id(const nrf_drv_twi_t* m_twi_master)
{
tsl2561_twi_processing = 1;
uint8_t byte_send[1] = {TSL2561_COMMAND | TSL2561_REG_ID};
ret_code_t err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 1, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
tsl2561_twi_processing = 1;
err_code = nrf_drv_twi_rx(m_twi_master, TSL2561_ADDR, tsl2561_buffer, 1);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
return tsl2561_buffer[0];
}
/**
* @brief Function for adjust sensor timing and sensitivity
*/
void tsl2561_sensitivity(const nrf_drv_twi_t* m_twi_master, tsl2561_sensitivity_t sens)
{
ret_code_t err_code;
uint8_t byte_send[2];
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_TIMMING;
switch(sens){
case TSL2561_SENSITIVITY_LOW:
/**
* GAIN => (0) 1x low gain
* MANUAL => (0)
* RESERV => (0)
* INTEG => (10) 402ms
*/
byte_send[1] = 0x2;
break;
case TSL2561_SENSITIVITY_MEDIUM:
/**
* GAIN => (1) 16x high gain
* MANUAL => (0)
* RESERV => (0)
* INTEG => (01) 101ms
*/
byte_send[1] = 0x11;
break;
case TSL2561_SENSITIVITY_HIGH:
default:
/**
* GAIN => (1) 16x high gain
* MANUAL => (0)
* RESERV => (0)
* INTEG => (10) 402ms
*/
byte_send[1] = 0x12;
break;
}
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 2, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
}
/**
* @brief Function for starting conversion
*/
void tsl2561_init(const nrf_drv_twi_t* m_twi_master)
{
ret_code_t err_code;
uint8_t byte_send[2];
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_CONTROL;
byte_send[1] = 0x3;
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 2, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
}
/**
* @brief Function for reading the last conversion
*/
tsl2561_luxerror_t tsl2561_read_lux(const nrf_drv_twi_t* m_twi_master, uint16_t* lux)
{
ret_code_t err_code;
uint8_t byte_send[1];
uint16_t data0;
uint16_t data1;
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_DATA0LOW;
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 1, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
tsl2561_twi_processing = 1;
err_code = nrf_drv_twi_rx(m_twi_master, TSL2561_ADDR, tsl2561_buffer, 1);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
data0 = tsl2561_buffer[0];
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_DATA0HIGH;
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 1, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
tsl2561_twi_processing = 1;
err_code = nrf_drv_twi_rx(m_twi_master, TSL2561_ADDR, tsl2561_buffer, 1);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
data0 |= (tsl2561_buffer[0] << 8);
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_DATA1LOW;
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 1, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
tsl2561_twi_processing = 1;
err_code = nrf_drv_twi_rx(m_twi_master, TSL2561_ADDR, tsl2561_buffer, 1);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
data1 = tsl2561_buffer[0];
tsl2561_twi_processing = 1;
byte_send[0] = TSL2561_COMMAND | TSL2561_REG_DATA1HIGH;
err_code = nrf_drv_twi_tx(m_twi_master, TSL2561_ADDR, byte_send, 1, true);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
tsl2561_twi_processing = 1;
err_code = nrf_drv_twi_rx(m_twi_master, TSL2561_ADDR, tsl2561_buffer, 1);
APP_ERROR_CHECK(err_code);
while(tsl2561_twi_processing == 1);
data1 |= (tsl2561_buffer[0] << 8);
//NRF_LOG_INFO("Illumination value 0 is: 0x%x", data0);
//NRF_LOG_INFO("Illumination value 1 is: 0x%x", data1);
if(data0 == 0xFFFF) {
*lux = 0;
return TSL2561_LUX_ERROR_HIGHLUM;
}
*lux = tsl2561_calculate_lux(data0, data1, 'T') & 0xFFFF;
return TSL2561_LUX_ERROR_NOERROR;
}