-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (60 loc) · 2.83 KB
/
main.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
import sys
from machine import I2C, Pin
from sensor_pack_2.bus_service import I2cAdapter
import veml6040mod
import time
def show_colors(clrs: [tuple, None], _lux: float = 0, hex_format: bool = False):
"""Выводит инфу о цветах и освещенности в люксах"""
if tuple is None:
return
if hex_format:
print(f"Red: 0x{clrs[0]:X}; Green: 0x{clrs[1]:X}; Blue: 0x{clrs[2]:X}; White: 0x{clrs[3]:X};")
return
print(f"Red: {clrs[0]}; Green: {clrs[1]}; Blue: {clrs[2]}; White: {clrs[3]}; lux: {_lux}")
def show_info(integr_time: int):
_g_sens, _max_lux = veml6040mod.get_g_max_lux(integr_time)
print(f"g sensitivity: {_g_sens}; max detectable lux: {_max_lux}")
def get_als_lux(green_channel: int, sensitivity: float) -> float:
"""возвращает освещенность в люксах по данным зеленого канала"""
return sensitivity * green_channel
if __name__ == '__main__':
i2c = I2C(id=1, scl=Pin(7), sda=Pin(6), freq=400_000) # on Raspberry Pi Pico
adapter = I2cAdapter(i2c)
sensor = veml6040mod.VEML6040(adapter)
if sensor.shutdown:
sensor.shutdown = False
wait_func = time.sleep_ms
print("Режим однократных измерений!")
sensor.start_measurement(integr_time=1, auto_mode=False)
wait_time_ms = sensor.get_conversion_cycle_time()
print(f"integration time: {sensor.integration_time}")
show_info(sensor.integration_time)
g_sens, max_lux = veml6040mod.get_g_max_lux(sensor.integration_time)
print(f"Запуск измерения датчика явно из кода!")
print(f"wait_time_ms: {wait_time_ms} мс")
for _ in range(100):
wait_func(wait_time_ms)
colors = sensor.get_colors()
lux = get_als_lux(colors[1], g_sens)
show_colors(colors, lux)
sensor.start_measurement(integr_time=1, auto_mode=False)
print(32*"-")
sensor.start_measurement(integr_time=2, auto_mode=True)
wait_time_ms = sensor.get_conversion_cycle_time()
print(f"Автоматический запуск измерения датчиком!")
print(f"Использование протокола итератора. wait_time_ms: {wait_time_ms} мс")
print(f"integration time: {sensor.integration_time}")
show_info(sensor.integration_time)
g_sens, max_lux = veml6040mod.get_g_max_lux(sensor.integration_time)
counter = 0
for colors in sensor:
wait_func(wait_time_ms)
if colors:
lux = get_als_lux(colors[1], g_sens)
show_colors(colors, lux)
else:
print(f"shutdown: {sensor.shutdown}; auto mode: {sensor.auto_mode}; it: {sensor.integration_time} [ms];")
counter = 1
if counter > 1000:
# завершаю программу
sys.exit(0)