-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADSensor.cpp
More file actions
52 lines (41 loc) · 1.02 KB
/
ADSensor.cpp
File metadata and controls
52 lines (41 loc) · 1.02 KB
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
/*
* ADSensor.cpp
*
* Created on: Apr 1, 2013
* Author: dam7633
*/
#include "ADSensor.h"
#include <hw/inout.h>
#define MAX_VALUE (326768)
#define MAX_VOLTAGE (10)
#define MAX_CHAR (127)
#define MIN_CHAR (-128)
#define CONVERSION_FACTOR (127/5)
ADSensor::ADSensor(uintptr_t c, uintptr_t s, uintptr_t m, uintptr_t l) :
Thread("ADSensor"),
ctrl_handle(c),
status_handle(s),
msb_handle(m),
lsb_handle(l) {
}
ADSensor::~ADSensor() {
}
double ADSensor::getValue() {
// 4. Perform an A/D conversion on the current channel
out8(ctrl_handle, 0x80);
// 5. Wait for the conversion to finish
while (in8(status_handle) >= 128){
} // wait for the value to be ready
// 6. Read the data from the board
unsigned char lsb = in8(lsb_handle);
unsigned char msb = in8(msb_handle);
short raw_data = msb * 256 + lsb;
// 7. Convert the data to a meaningful value
double voltage = (raw_data / (double)MAX_VALUE) * (double)MAX_VOLTAGE;
return (voltage * MAX_VOLTAGE);
}
void *ADSensor::run() {
while (!killThread) {
}
return NULL;
}