50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import os
|
|
from os.path import join
|
|
import time
|
|
from . import leds, camera, config
|
|
|
|
|
|
def delay_capture(cam):
|
|
# Measure the time it takes to capture
|
|
start = time.time()
|
|
output = cam.capture()
|
|
delta = time.time() - start
|
|
|
|
# Wait for at least one second between each capture
|
|
if delta < config.DELAY:
|
|
time.sleep(config.DELAY - delta)
|
|
|
|
return output
|
|
|
|
|
|
def scan(output_dir: str, on_and_off: bool = True):
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
file_paths = []
|
|
with camera.get() as cam:
|
|
with leds.get() as gpio_leds:
|
|
for count, led in enumerate(gpio_leds.leds):
|
|
print(f'Turn on {led}')
|
|
img = join(output_dir, f'{led}')
|
|
|
|
led.on()
|
|
file_paths.append((str(led), delay_capture(cam)))
|
|
led.off()
|
|
|
|
print(f'Turn off {led}')
|
|
|
|
# capture with all leds ON OFF
|
|
if on_and_off:
|
|
gpio_leds.on()
|
|
img = join(output_dir, 'all_on')
|
|
file_paths.append(('all_on', delay_capture(cam)))
|
|
|
|
gpio_leds.off()
|
|
img = join(output_dir, 'all_off')
|
|
file_paths.append(('all_off', delay_capture(cam)))
|
|
|
|
with camera.get() as cam:
|
|
for target, source in file_paths:
|
|
cam.save(source, join(output_dir, target))
|
|
yield target
|