Skip to content

Commit 8bb1708

Browse files
committed
AS7343: Tufty 2040 spectrometer example.
1 parent 6fcbaf5 commit 8bb1708

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_P4
2+
from breakout_as7343 import BreakoutAS7343
3+
from pimoroni_i2c import PimoroniI2C
4+
5+
6+
display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_P4, rotate=90)
7+
8+
WIDTH, HEIGHT = display.get_bounds()
9+
10+
BLACK = display.create_pen(0, 0, 0)
11+
12+
FZ = display.create_pen(0, 0, 128)
13+
FY = display.create_pen(0, 255, 0)
14+
FXL = display.create_pen(255, 128, 0)
15+
NIR = display.create_pen(128, 0, 0)
16+
F2 = display.create_pen(128, 0, 128)
17+
F3 = display.create_pen(0, 0, 255)
18+
F4 = display.create_pen(0, 128, 255)
19+
F6 = display.create_pen(128, 64, 0)
20+
21+
F1 = display.create_pen(196, 0, 196)
22+
F5 = display.create_pen(64, 255, 64)
23+
F7 = display.create_pen(255, 0, 0)
24+
F8 = display.create_pen(196, 0, 0)
25+
26+
WHITE = display.create_pen(255, 255, 255)
27+
28+
i2c = PimoroniI2C(sda=4, scl=5)
29+
as7343 = BreakoutAS7343(i2c)
30+
31+
as7343.set_channels(18)
32+
as7343.set_gain(1024)
33+
as7343.set_measurement_time(33) # Roughly 30fps at 16ms/measurement
34+
as7343.set_integration_time(27800)
35+
36+
as7343.set_illumination_current(4)
37+
as7343.set_illumination_led(True)
38+
39+
40+
BAR_WIDTH = 18
41+
BAR_SPACING = 4
42+
MARGIN = display.measure_text("NIR") + 2
43+
BAR_HEIGHT = WIDTH - MARGIN
44+
45+
OFFSET_LEFT = int((HEIGHT - ((BAR_WIDTH + BAR_SPACING) * 12)) / 2)
46+
47+
# Satrting max values for auto-ranging
48+
# From figure 8 of the datasheet
49+
MAX_VALUES = [
50+
2711,
51+
4684,
52+
5970,
53+
13226,
54+
2371,
55+
962,
56+
3926,
57+
4170,
58+
7760,
59+
1967,
60+
6774,
61+
1166
62+
]
63+
64+
LABELS = [
65+
"FZ",
66+
"FY",
67+
"FXL",
68+
"NIR",
69+
"F2",
70+
"F3",
71+
"F4",
72+
"F6",
73+
"F1",
74+
"F5",
75+
"F7",
76+
"F8"
77+
]
78+
79+
80+
while True:
81+
display.set_pen(0)
82+
display.clear()
83+
readings = as7343.read()
84+
for i, reading in enumerate(readings):
85+
MAX_VALUES[i] = max(reading, MAX_VALUES[i])
86+
scaled = int(reading / MAX_VALUES[i] * BAR_HEIGHT)
87+
88+
y = i * (BAR_WIDTH + BAR_SPACING)
89+
y += OFFSET_LEFT
90+
91+
display.set_pen(i + 1)
92+
display.rectangle(MARGIN, y, scaled, BAR_WIDTH)
93+
94+
display.set_pen(WHITE)
95+
display.text(LABELS[i], 0, y + 1)
96+
97+
display.update()

0 commit comments

Comments
 (0)