forked from RAKWireless/WisBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAK1903_Optical_OPT3001.ino
145 lines (120 loc) · 3.35 KB
/
RAK1903_Optical_OPT3001.ino
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
/**
@file RAK1903_Optical_OPT3001.ino
@author rakwireless.com
@brief Setup and read values from a opt3001 sensor
@version 0.1
@date 2020-12-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#include <ClosedCube_OPT3001.h> // Click here to get the library: http://librarymanager/All#OPT3001
// Forward declarations for functions
void printError(String text, OPT3001_ErrorCode error);
ClosedCube_OPT3001 g_opt3001;
#define OPT3001_ADDRESS 0x44
void configureSensor()
{
OPT3001_Config newConfig;
newConfig.RangeNumber = B1100;
newConfig.ConvertionTime = B0;
newConfig.Latch = B1;
newConfig.ModeOfConversionOperation = B11;
OPT3001_ErrorCode errorConfig = g_opt3001.writeConfig(newConfig);
if (errorConfig != NO_ERROR)
printError("OPT3001 configuration", errorConfig);
else
{
OPT3001_Config sensorConfig = g_opt3001.readConfig();
Serial.println("OPT3001 Current Config:");
Serial.println("------------------------------");
Serial.print("Conversion ready (R):");
Serial.println(sensorConfig.ConversionReady, HEX);
Serial.print("Conversion time (R/W):");
Serial.println(sensorConfig.ConvertionTime, HEX);
Serial.print("Fault count field (R/W):");
Serial.println(sensorConfig.FaultCount, HEX);
Serial.print("Flag high field (R-only):");
Serial.println(sensorConfig.FlagHigh, HEX);
Serial.print("Flag low field (R-only):");
Serial.println(sensorConfig.FlagLow, HEX);
Serial.print("Latch field (R/W):");
Serial.println(sensorConfig.Latch, HEX);
Serial.print("Mask exponent field (R/W):");
Serial.println(sensorConfig.MaskExponent, HEX);
Serial.print("Mode of conversion operation (R/W):");
Serial.println(sensorConfig.ModeOfConversionOperation, HEX);
Serial.print("Polarity field (R/W):");
Serial.println(sensorConfig.Polarity, HEX);
Serial.print("Overflow flag (R-only):");
Serial.println(sensorConfig.OverflowFlag, HEX);
Serial.print("Range number (R/W):");
Serial.println(sensorConfig.RangeNumber, HEX);
Serial.println("------------------------------");
}
}
void opt3001_read_data()
{
OPT3001 result = g_opt3001.readResult();
if (result.error == NO_ERROR)
{
Serial.print("OPT3001");
Serial.print(": ");
Serial.print(result.lux);
Serial.println(" lux");
uint16_t luminosity = result.lux;
}
else
{
printError("OPT3001", result.error);
}
}
void printResult(String text, OPT3001 result)
{
if (result.error == NO_ERROR)
{
Serial.print(text);
Serial.print(": ");
Serial.print(result.lux);
Serial.println(" lux");
}
else
{
printError(text, result.error);
}
}
void printError(String text, OPT3001_ErrorCode error)
{
Serial.print(text);
Serial.print(": [ERROR] Code #");
Serial.println(error);
}
void setup()
{
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
/* opt3001 init */
g_opt3001.begin(OPT3001_ADDRESS);
Serial.print("OPT3001 Manufacturer ID");
Serial.println(g_opt3001.readManufacturerID());
Serial.print("OPT3001 Device ID");
Serial.println(g_opt3001.readDeviceID());
configureSensor();
printResult("High-Limit", g_opt3001.readHighLimit());
printResult("Low-Limit", g_opt3001.readLowLimit());
}
void loop()
{
opt3001_read_data();
delay(1000);
}