-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_SPI_CLS-ALS.py
71 lines (52 loc) · 1.92 KB
/
test_SPI_CLS-ALS.py
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
from WF_SDK import device, supplies, error # import instruments
from WF_SDK.protocol import spi # import protocol instrument
from time import sleep # needed for delays
"""-----------------------------------------------------------------------"""
try:
# connect to the device
device_data = device.open()
"""-----------------------------------"""
# define chip select lines
CLS_cs = 0
ALS_cs = 1
# start the power supplies
supplies_data = supplies.data()
supplies_data.master_state = True
supplies_data.state = True
supplies_data.voltage = 3.3
supplies.switch(device_data, supplies_data)
# initialize the spi interface on DIO0, DIO1, DIO2, DIO3 and DIO4
spi.open(device_data, CLS_cs, sck=2, miso=3, mosi=4)
spi.open(device_data, ALS_cs, sck=2, miso=3, mosi=4)
try:
# repeat
while True:
# 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
message = spi.read(device_data, 2, ALS_cs)
value = ((int(message[0]) << 3) | (int(message[1]) >> 4)) / 1.27
# display the temperature
spi.write(device_data, str(round(value, 2)), CLS_cs)
# display a message
spi.write(device_data, "%", CLS_cs)
# delay 1s
sleep(1)
except KeyboardInterrupt:
# exit on Ctrl+C
pass
# reset the interface
spi.close(device_data)
# stop and reset the power supplies
supplies_data.master_state = False
supplies.switch(device_data, supplies_data)
supplies.close(device_data)
"""-----------------------------------"""
# close the connection
device.close(device_data)
except error as e:
print(e)
# close the connection
device.close(device.data)