-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaesora_PMT9123.cpp
247 lines (211 loc) · 6.5 KB
/
aesora_PMT9123.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
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
/**************************************************************************/
/*!
@file aesora_PMT9123.h
@author Matthias Breithaupt (Aesora)
@license BSD (see license.txt)
This is a library for the Aesora PMT9123 breakout board
@section HISTORY
v0.1 - Pre-release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "aesora_PMT9123.h"
static uint8_t i2cread(void) {
uint8_t x;
#if ARDUINO >= 100
x = Wire.read();
#else
x = Wire.receive();
#endif
//Serial.print("0x"); Serial.println(x, HEX);
return x;
}
static void i2cwrite(uint8_t x) {
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
/**************************************************************************/
/*!
@brief Instantiates a new PMT9123 class
*/
/**************************************************************************/
aesora_PMT9123::aesora_PMT9123() {
_resolution = 0;
}
/**************************************************************************/
/*!
@brief Sets the HW up (reads resolution value, etc.)
*/
/**************************************************************************/
void aesora_PMT9123::begin() {
Wire.begin();
// Read the resolution (this value is used to calculate real distances)
getResolution();
}
/**************************************************************************/
/*!
@brief Checks for a valid product id
*/
/**************************************************************************/
bool aesora_PMT9123::checkID() {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_PRODUCT_ID);
wire.endTransmission();
// Read product id and compare
Wire.requestFrom(PMT9123_ADDRESS, 1);
return i2cread() == PMT9123_PRODUCT_ID;
}
/**************************************************************************/
/*!
@brief Gets the resolution setting
*/
/**************************************************************************/
uint8_t aesora_PMT9123::getResolution() {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_RESOLUTION_STEP);
wire.endTransmission();
// Read resolution
Wire.requestFrom(PMT9123_ADDRESS, 1);
_resolution = i2cread();
return _resolution;
}
/**************************************************************************/
/*!
@brief Sets the resolution setting
*/
/**************************************************************************/
void aesora_PMT9123::setResolution(uint8_t value) {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_W_RESOLUTION_STEP);
i2cwrite(value);
wire.endTransmission();
// Store new setting
_resolution = value;
}
/**************************************************************************/
/*!
@brief Gets the delta in x and y
*/
/**************************************************************************/
void aesora_PMT9123::getDelta(int16_t *x, int16_t *y, uint8_t *mot) {
uint8_t _x, _y, temp;
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_MOTION);
wire.endTransmission();
// Read delta
Wire.requestFrom(PMT9123_ADDRESS, 4);
mot = i2cread();
_x = i2cread();
_y = i2cread();
temp = i2cread();
// Calculate
x = _x | ((uint16_t)(temp & 0xF0) << 4) | (temp & 0x80) > 0 ? 0xF000 : 0;
y = _y | ((uint16_t)(temp & 0x0F) << 8) | (temp & 0x08) > 0 ? 0xF000 : 0;
}
/**************************************************************************/
/*!
@brief Gets delta in x
*/
/**************************************************************************/
int16_t aesora_PMT9123::getDeltaX(void) {
int16_t x, y;
uint8_t mot;
getDelta(&x, &y, &mot);
return x;
}
/**************************************************************************/
/*!
@brief Gets delta in y
*/
/**************************************************************************/
int16_t aesora_PMT9123::getDeltaY(void) {
int16_t x, y;
uint8_t mot;
getDelta(&x, &y, &mot);
return y;
}
/**************************************************************************/
/*!
@brief Gets surface quality
*/
/**************************************************************************/
uint8_t aesora_PMT9123::getSQual(void) {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_SQUAL);
wire.endTransmission();
// Read squal
Wire.requestFrom(PMT9123_ADDRESS, 1);
return i2cread();
}
/**************************************************************************/
/*!
@brief Gets shutter time in clock cycles
*/
/**************************************************************************/
uint16_t aesora_PMT9123::getShutter(void) {
uint16_t ret = 0;
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_SHUTTER_MSB);
wire.endTransmission();
// Read shutter
Wire.requestFrom(PMT9123_ADDRESS, 2);
ret = i2cread();
ret = ret << 8 | i2cread();
return ret;
}
/**************************************************************************/
/*!
@brief Gets maximum pixel value in frame
*/
/**************************************************************************/
uint8_t aesora_PMT9123::getPixelMax(void) {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_PIXEL_MAX);
wire.endTransmission();
// Read max pixel
Wire.requestFrom(PMT9123_ADDRESS, 1);
return i2cread();
}
/**************************************************************************/
/*!
@brief Gets average pixel value in frame
*/
/**************************************************************************/
uint8_t aesora_PMT9123::getPixelAvg(void) {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_PIXEL_AVG);
wire.endTransmission();
// Read avg pixel
Wire.requestFrom(PMT9123_ADDRESS, 1);
return i2cread();
}
/**************************************************************************/
/*!
@brief Gets minimum pixel value in frame
*/
/**************************************************************************/
uint8_t aesora_PMT9123::getPixelMin(void) {
// Select register
Wire.beginTransmission(PMT9123_ADDRESS);
i2cwrite((uint8_t)PMT9123_REGISTER_R_PIXEL_MIN);
wire.endTransmission();
// Read min pixel
Wire.requestFrom(PMT9123_ADDRESS, 1);
return i2cread();
}