Working with GPIO

This commit is contained in:
Nicolas Bertrand 2024-11-15 11:15:51 +01:00
parent 8cfcf1ec55
commit c02991eb44
3 changed files with 105 additions and 60 deletions

View File

@ -8,17 +8,21 @@ DATABASE_PATH = join(DATA_DIR, 'db.sqlite')
AUTO_USE_LAST_CALIBRATION = False
LEDS_UUIDS = [
'ac59350e-3787-46d2-88fa-743c1d34fe86',
'83ab3133-d4de-4a42-99a7-8f8a3f3732ba',
'577d6e72-f518-4d65-af28-f8faf1ca3f5d',
'ec49a20c-bddd-4614-b828-fa45e01bfb19',
'5c249cce-d2b6-4b56-96c8-5caa43d8f040',
'22d783fb-ae55-4581-a3c6-e010d9d5e9de',
'12cb6a32-04a6-433b-8146-b753b8f1286d',
'461255a3-259a-4291-adc3-2fb736231a04',
'3896662f-9826-4445-ad70-86c2e6c143e7',
'f87698ec-3cba-42fe-9a61-5ac9f77d767a',
'4c77a655-4b68-4557-a696-29345c6676a1',
'b1cfe287-aa3b-445e-bcdc-75ae375efe43',
]
GPIO_CHIP = 'gpiochip0'
# LEDS_UUIDS = [
# 'ac59350e-3787-46d2-88fa-743c1d34fe86',
# '83ab3133-d4de-4a42-99a7-8f8a3f3732ba',
# '577d6e72-f518-4d65-af28-f8faf1ca3f5d',
# 'ec49a20c-bddd-4614-b828-fa45e01bfb19',
# '5c249cce-d2b6-4b56-96c8-5caa43d8f040',
# '22d783fb-ae55-4581-a3c6-e010d9d5e9de',
# '12cb6a32-04a6-433b-8146-b753b8f1286d',
# '461255a3-259a-4291-adc3-2fb736231a04',
# '3896662f-9826-4445-ad70-86c2e6c143e7',
# 'f87698ec-3cba-42fe-9a61-5ac9f77d767a',
# '4c77a655-4b68-4557-a696-29345c6676a1',
# 'b1cfe287-aa3b-445e-bcdc-75ae375efe43',
# ]
LEDS_UUIDS = [17, 18, 22, 23, 24, 27]

52
leds.py Normal file
View File

@ -0,0 +1,52 @@
import gpiod
from . import config
class GpioLed:
chip = gpiod.Chip(config.GPIO_CHIP)
def __init__(self, gpio_pin: int):
self.gpio_pin = gpio_pin
self.led = None
def __enter__(self):
self.led = GpioLed.chip.get_line(self.gpio_pin)
self.led.request(consumer=str(self), type=gpiod.LINE_REQ_DIR_OUT)
self.off()
def __exit__(self, *args):
self.off()
self.led.release()
self.led = None
def on(self):
self.led.set_value(1)
def off(self):
self.led.set_value(0)
def __str__(self):
return f'LED{self.gpio_pin:02}'
class GpioLeds:
def __init__(self, gpio_pins: list[int]):
self.leds = []
for pin in gpio_pins:
self.leds.append(GpioLed(pin))
def __enter__(self):
for led in self.leds:
led.__enter__()
def __exit__(self, *args):
for led in self.leds:
led.__exit__(*args)
def off(self):
for led in self.leds:
led.off()
def on(self):
for led in self.leds:
led.on()

View File

@ -1,31 +1,30 @@
import cv2
import os
from os.path import join
import subprocess
import shutil
import time
import gphoto2 as gp
import gpiod
from . import config
from . import config, leds
# Delay between to captures
DELAY = 0.5
LED2_PIN = 17
LED3_PIN = 18
def capture(output_path: str) -> bool:
def capture(camera, output_path: str) -> bool:
try:
camera = gp.Camera()
camera.init()
print('Capturing image')
file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
print('Camera file path: {0}/{1}'.format(file_path.folder, file_path.name))
target =output_path
target = output_path + '.cr2'
print('Copying image to', target)
camera_file = camera.file_get(
file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
camera_file = camera.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_RAW)
camera_file.save(target)
camera.exit()
camera_file = camera.file_get(file_path.folder, file_path.name, gp.GP_FILE_TYPE_PREVIEW)
camera_file.save(output_path + '.jpg')
s = True
except:
print(f'Somethings wrong on gphoto2')
@ -35,47 +34,37 @@ def capture(output_path: str) -> bool:
def scan(output_dir: str):
camera = gp.Camera()
camera.init()
os.makedirs(output_dir, exist_ok=True)
chip = gpiod.Chip('gpiochip0')
gpio_leds = leds.GpioLeds(config.LEDS_UUIDS)
led2 = chip.get_line(LED2_PIN)
led2.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
with gpio_leds:
for count, led in enumerate(gpio_leds.leds):
print(f'Turn on {led}')
img = join(output_dir, f'{led}')
led3 = chip.get_line(LED3_PIN)
led3.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
# Measure the time it takes to capture
start = time.time()
led3.set_value(0)
led3.set_value(0)
led.on()
capture(camera, img)
led.off()
# For debug purposes
# shutil.copyfile(join('data-keep/small', led + '.jpg'), img)
for count,led in enumerate(config.LEDS_UUIDS):
print(f'Turn on {count} {led}')
img = join(output_dir, led + '.jpg')
delta = time.time() - start
# Measure the time it takes to capture
start = time.time()
if count % 2 == 0:
led2.set_value(1)
else :
led3.set_value(1)
# Wait for at least one second between each capture
if delta < DELAY:
time.sleep(DELAY - delta)
capture(img)
# For debug purposes
#shutil.copyfile(join('data-keep/small', led + '.jpg'), img)
led2.set_value(0)
led3.set_value(0)
print(f'Turn off {led}')
if count == len(config.LEDS_UUIDS) -1:
gpio_leds.off()
camera.exit()
delta = time.time() - start
# Wait for at least one second between each capture
if delta < DELAY:
time.sleep(DELAY - delta)
print(f'Turn off {count} {led}')
if count == len(config.LEDS_UUIDS) -1 :
led2.set_value(0)
led3.set_value(0)
led2.release()
led3.release()
yield led
yield led