nenuscanner/scanner.py

61 lines
1.6 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 config.DELAY is not None and delta < config.DELAY:
time.sleep(config.DELAY - delta)
return output
def delay_save(cam, source, target):
# Measure the time it takes to save
start = time.time()
output = cam.capture()
cam.save(source, target)
delta = time.time() - start
# Wait for at least one second between each save
if config.DELAY is not None and 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}')
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()
file_paths.append(('all_on', delay_capture(cam)))
gpio_leds.off()
file_paths.append(('all_off', delay_capture(cam)))
with camera.get() as cam:
for target, source in file_paths:
delay_save(cam, source, join(output_dir, target))
yield target