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 AUTO_USE_LAST_CALIBRATION = False
LEDS_UUIDS = [ GPIO_CHIP = 'gpiochip0'
'ac59350e-3787-46d2-88fa-743c1d34fe86',
'83ab3133-d4de-4a42-99a7-8f8a3f3732ba', # LEDS_UUIDS = [
'577d6e72-f518-4d65-af28-f8faf1ca3f5d', # 'ac59350e-3787-46d2-88fa-743c1d34fe86',
'ec49a20c-bddd-4614-b828-fa45e01bfb19', # '83ab3133-d4de-4a42-99a7-8f8a3f3732ba',
'5c249cce-d2b6-4b56-96c8-5caa43d8f040', # '577d6e72-f518-4d65-af28-f8faf1ca3f5d',
'22d783fb-ae55-4581-a3c6-e010d9d5e9de', # 'ec49a20c-bddd-4614-b828-fa45e01bfb19',
'12cb6a32-04a6-433b-8146-b753b8f1286d', # '5c249cce-d2b6-4b56-96c8-5caa43d8f040',
'461255a3-259a-4291-adc3-2fb736231a04', # '22d783fb-ae55-4581-a3c6-e010d9d5e9de',
'3896662f-9826-4445-ad70-86c2e6c143e7', # '12cb6a32-04a6-433b-8146-b753b8f1286d',
'f87698ec-3cba-42fe-9a61-5ac9f77d767a', # '461255a3-259a-4291-adc3-2fb736231a04',
'4c77a655-4b68-4557-a696-29345c6676a1', # '3896662f-9826-4445-ad70-86c2e6c143e7',
'b1cfe287-aa3b-445e-bcdc-75ae375efe43', # '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 cv2
import os import os
from os.path import join from os.path import join
import subprocess
import shutil import shutil
import time import time
import gphoto2 as gp import gphoto2 as gp
import gpiod import gpiod
from . import config from . import config, leds
# Delay between to captures # Delay between to captures
DELAY = 0.5 DELAY = 0.5
LED2_PIN = 17 def capture(camera, output_path: str) -> bool:
LED3_PIN = 18
def capture(output_path: str) -> bool:
try: try:
camera = gp.Camera()
camera.init()
print('Capturing image') print('Capturing image')
file_path = camera.capture(gp.GP_CAPTURE_IMAGE) file_path = camera.capture(gp.GP_CAPTURE_IMAGE)
print('Camera file path: {0}/{1}'.format(file_path.folder, file_path.name)) 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) 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_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 s = True
except: except:
print(f'Somethings wrong on gphoto2') print(f'Somethings wrong on gphoto2')
@ -35,47 +34,37 @@ def capture(output_path: str) -> bool:
def scan(output_dir: str): def scan(output_dir: str):
camera = gp.Camera()
camera.init()
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
chip = gpiod.Chip('gpiochip0') gpio_leds = leds.GpioLeds(config.LEDS_UUIDS)
led2 = chip.get_line(LED2_PIN) with gpio_leds:
led2.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT) 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) # Measure the time it takes to capture
led3.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT) start = time.time()
led3.set_value(0) led.on()
led3.set_value(0) 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): delta = time.time() - start
print(f'Turn on {count} {led}')
img = join(output_dir, led + '.jpg')
# Measure the time it takes to capture # Wait for at least one second between each capture
start = time.time() if delta < DELAY:
if count % 2 == 0: time.sleep(DELAY - delta)
led2.set_value(1)
else :
led3.set_value(1)
capture(img) print(f'Turn off {led}')
# For debug purposes if count == len(config.LEDS_UUIDS) -1:
#shutil.copyfile(join('data-keep/small', led + '.jpg'), img) gpio_leds.off()
led2.set_value(0) camera.exit()
led3.set_value(0)
delta = time.time() - start yield led
# 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