-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdk101go.cpp
133 lines (114 loc) · 3.48 KB
/
gdk101go.cpp
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
/***************************************************************************
gdk101go - a library for the GDK101 gamma sensor
Copyright (C) 2022 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
***************************************************************************/
#include "gdk101go.h"
gdk101::gdk101(int addr = GDK101_ADDR) {
/* the default constructor: assign the i2c address */
_addr = addr;
}
gdk101::gdk101(int a0, int a1) {
/* an alternative constructor, used when assigning
the address by means of the jumpers A0 and A1 */
int code = a0 * 10 + a1;
if (code == 11) {
_addr = GDK101_ADDR;
} else if (code == 10) {
_addr = 0x1A;
} else if (code == 1) {
_addr = 0x19;
} else if (code == 0) {
_addr = 0x1B;
}
}
void gdk101::poweron(int pin = GDK101_POWER) {
/* the sensor can be powered using a digital pin
such that it can be powered on and off by
software. The default pin is 8 */
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
begin();
while (status() == 0) {
// do nothing, just wait...
}
_uptime = millis();
}
float gdk101::onSince() {
/* returns the number of seconds elapsed since
last power on */
return (millis() - _uptime)/1000.;
}
void gdk101::poweroff(int pin = GDK101_POWER) {
/* power off; requires using a digital pin for
powering it on */
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
int gdk101::begin() {
/* initialise the sensor */
Wire.begin();
exec_cmd(GDK101_FRMWR);
sprintf(_fw, "%d.%d", _msb, _lsb);
return reset();
}
int gdk101::reset() {
/* reset the sensor */
exec_cmd(GDK101_RESET, 1);
return _msb;
}
int gdk101::status() {
/* get the status: 0 - not ready;
1 - on since less than 10 min;
2 - on since more than 10 min */
exec_cmd(GDK101_STATUS, 2);
return _msb;
}
int gdk101::vibration() {
/* check vibration status: 0 - no vibrations */
exec_cmd(GDK101_STATUS, 2);
return _lsb;
}
int gdk101::uptime() {
/* returns the actual measuring time */
exec_cmd(GDK101_UPTIME, 2);
return _lsb + _msb * 60;
}
float gdk101::reading10min() {
/* returns the reading consisting in the
measured dose averaged over 10 minutes;
the dose is given in uSv/h */
exec_cmd(GDK101_10MRT, 2);
return _msb + 0.1 * _lsb;
}
float gdk101::reading1min() {
/* returns the reading consisting in the
measured dose averaged over 1 minute;
the dose is given in uSv/h */
exec_cmd(GDK101_1MRT, 2);
return _msb + 0.1 * _lsb;
}
char* gdk101::firmware() {
/* returns the firmware version */
return _fw;
}
void gdk101::exec_cmd(int cmd, int retCount = 2) {
/* used internally to handle the i2c protocol */
Wire.beginTransmission(GDK101_ADDR);
Wire.write(cmd);
Wire.endTransmission();
Wire.requestFrom(GDK101_ADDR, retCount);
while (Wire.available()) {
_msb = Wire.read();
_lsb = Wire.read();
}
}