forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_adcs.py
More file actions
43 lines (32 loc) · 1.34 KB
/
read_adcs.py
File metadata and controls
43 lines (32 loc) · 1.34 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
import time
from inventorhatmini import InventorHATMini, NUM_GPIOS, LED_GPIO_1
from ioexpander import ADC
"""
Shows how to initialise and read the 4 ADC headers of Inventor HAT Mini.
Press "User" to exit the program.
"""
# Constants
BRIGHTNESS = 0.4 # The brightness of the LEDs
UPDATES = 10 # How many times to update LEDs per second
ADC_NAMES = ("A0", "A1", "A2", "A3") # Friendly names to give the ADCs
USE_LEDS = True # Whether to use the LEDs to show ADC state (requires code to run with sudo)
# Create a new InventorHATMini
board = InventorHATMini(init_leds=USE_LEDS)
# Setup each GPIO as an analog input
for i in range(NUM_GPIOS):
board.gpio_pin_mode(i, ADC)
# Read the ADCs until the user button is pressed
while not board.switch_pressed():
# Read each ADC in turn and print its voltage
for i in range(NUM_GPIOS):
voltage = board.gpio_pin_value(i)
print(ADC_NAMES[i], " = ", round(voltage, 3), sep="", end=", ")
# Set the neighbouring LED to a colour based on the
# voltage, with Green for high and Blue for low
hue = (2.0 - (voltage / 3.3)) * 0.333
board.leds.set_hsv(i + LED_GPIO_1, hue, 1.0, BRIGHTNESS)
# Print a new line
print()
time.sleep(1.0 / UPDATES)
# Turn off the LED bars
board.leds.clear()