-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_SPI_CLS-ALS.cpp
81 lines (64 loc) · 2.44 KB
/
test_SPI_CLS-ALS.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
#include "WF_SDK/WF_SDK.h" // include all classes and functions
#include <iostream> // needed for input/output
#include <vector> // needed for vector handling
#include <sstream> // needed for string manipulation
#include <string> // needed for error handling
using namespace wf;
// define DIO lines
int CLS_cs = 0;
int ALS_cs = 1;
int sck = 2;
int miso = 3;
int mosi = 4;
/* ----------------------------------------------------- */
int main(void) {
// connect to the device
Device::Data *device_data;
try {
device_data = device.open();
// check for connection errors
device.check_error(device_data);
/* ----------------------------------------------------- */
// start the positive supply
Supplies::Data supplies_data;
supplies_data.master_state = true;
supplies_data.positive_state = true;
supplies_data.positive_voltage = 3.3;
supplies.switch_(device_data, supplies_data);
tools.sleep(500); // delay
// initialize the spi interface on DIO0, DIO1, DIO2, DIO3 and DIO4
spi.open(device_data, CLS_cs, sck, miso, mosi);
spi.open(device_data, ALS_cs, sck, miso, mosi);
tools.keyboard_interrupt_reset(device_data);
std::cout << "Press Ctrl+C to exit..." << std::endl;
while (true) {
// repeat
// clear the screen and home cursor
spi.write(device_data, "\x1b[j", CLS_cs);
// display a message
spi.write(device_data, "Lum: ", CLS_cs);
// read the temperature
std::vector<unsigned char> message = spi.read(device_data, 2, ALS_cs);
double value = ((int(message[0]) << 3) | (int(message[1]) >> 4)) / 1.27;
// display the temperature
std::ostringstream output;
output.precision(2);
output << std::fixed << value;
spi.write(device_data, output.str(), CLS_cs);
// display a message
spi.write(device_data, "%", CLS_cs);
// delay 1s
tools.sleep(1000);
}
}
catch (Error error) {
// if an error occurs display it
std::cout << "Error: ";
std::cout << error.instrument << " -> ";
std::cout << error.function << " -> ";
std::cout << error.message << std::endl;
// close the connection
device.close(device_data);
}
return 0;
}