Apply linting to the Raspberry Pi
This commit is contained in:
@@ -100,11 +100,7 @@ print_queue = PrintQueue(app)
|
||||
rpi = Raspberry(
|
||||
print_queue,
|
||||
app,
|
||||
socketio,
|
||||
configuration_file["rpi"]["button_gpio_port_number"],
|
||||
configuration_file["rpi"]["indicator_gpio_port_number"],
|
||||
configuration_file["rpi"]["flash_gpio_port_number"],
|
||||
configuration_file["rpi"]["flash"],
|
||||
configuration_file
|
||||
)
|
||||
RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi()
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import io # To check if we are on a Raspberry Pi
|
||||
import subprocess
|
||||
import os
|
||||
from time import sleep, gmtime, strftime
|
||||
from flask_socketio import SocketIO
|
||||
from gpiozero import Button, LED, DigitalOutputDevice
|
||||
from PIL import Image
|
||||
from task import TextTask, ImageTask, CutTask
|
||||
@@ -24,29 +23,22 @@ class Raspberry:
|
||||
- Activating a flash ( or light )
|
||||
- Flash an indicator light
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
# dede
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
print_queue,
|
||||
app,
|
||||
socketio,
|
||||
button_gpio_port_number,
|
||||
indicator_gpio_port_number,
|
||||
flash_gpio_port_number,
|
||||
is_flash_present,
|
||||
configuration_file
|
||||
):
|
||||
self.print_queue = print_queue
|
||||
self.socketio = socketio
|
||||
self.app = app
|
||||
|
||||
self.flash_gpio = flash_gpio_port_number
|
||||
self.is_flash_present = is_flash_present
|
||||
self.button_gpio = button_gpio_port_number
|
||||
self.led_gpio = indicator_gpio_port_number
|
||||
self.configuration_file = configuration_file
|
||||
self.image_path = self.app.config["UPLOAD_FOLDER"] + "/image.jpg"
|
||||
|
||||
def is_raspberry_pi(self, raise_on_errors=False):
|
||||
def is_raspberry_pi(self):
|
||||
"""
|
||||
Checking if we are on a Raspberry Pi by checking
|
||||
information on the /proc/cpuinfo file
|
||||
@@ -73,9 +65,10 @@ class Raspberry:
|
||||
return False
|
||||
|
||||
if not found:
|
||||
self.app.logger.error(
|
||||
"Couldn't get sufficient hardware information from /proc/cpuinfo, Unable to determine if we are on a Raspberry Pi."
|
||||
self.app.logger.warning(
|
||||
"Couldn't get sufficient hardware information from /proc/cpuinfo"
|
||||
)
|
||||
self.app.logger.error("Unable to determine if we are on a Raspberry Pi.")
|
||||
return False
|
||||
except IOError:
|
||||
self.app.logger.error("Unable to open `/proc/cpuinfo`.")
|
||||
@@ -84,28 +77,38 @@ class Raspberry:
|
||||
self.app.logger.debug("It seems we are on a Raspberry Pi")
|
||||
|
||||
try:
|
||||
self.initialise_gpio()
|
||||
self._initialise_gpio()
|
||||
except Exception as e:
|
||||
self.app.logger.debug("Could not init GPIO : " + str(e))
|
||||
raise e
|
||||
return True
|
||||
|
||||
def initialise_gpio(self):
|
||||
def _initialise_gpio(self):
|
||||
"""
|
||||
Set GPIO ports from configuration and activate them
|
||||
to show the user it's working.
|
||||
"""
|
||||
self.app.logger.debug("Initializing GPIO")
|
||||
|
||||
self.led = LED(self.led_gpio)
|
||||
self.led = LED(self.configuration_file["rpi"]["indicator_gpio_port_number"])
|
||||
self.app.logger.debug("Activated indicator LED")
|
||||
self.indicator_countdown(iters=3)
|
||||
self.button = Button(self.button_gpio, pull_up=True, bounce_time=0.1)
|
||||
self.button = Button(
|
||||
self.configuration_file["rpi"]["button_gpio_port_number"],
|
||||
pull_up=True, bounce_time=0.1
|
||||
)
|
||||
self.button.when_pressed = self.on_button_pressed
|
||||
self.app.logger.debug("Activated button")
|
||||
|
||||
# The "flash" is a relay-controlled device ( light bulb for example )
|
||||
self.flash = DigitalOutputDevice(self.flash_gpio)
|
||||
self.flash = DigitalOutputDevice(self.configuration_file["rpi"]["flash_gpio_port_number"])
|
||||
self.flash_toggle()
|
||||
self.app.logger.debug("Activated flash")
|
||||
|
||||
def indicator_countdown(self, iters=10, multi=10):
|
||||
"""
|
||||
Activates the LED faster and faster to show a countdown
|
||||
"""
|
||||
for i in range(iters, 0, -1):
|
||||
self.led.on()
|
||||
sleep(i / multi)
|
||||
@@ -113,7 +116,10 @@ class Raspberry:
|
||||
sleep(i / multi)
|
||||
|
||||
def indicator_led(self, timing=0.2, l=5):
|
||||
for i in range(l):
|
||||
"""
|
||||
Turns on the indicator LED for a certain period of time
|
||||
"""
|
||||
for _ in range(l):
|
||||
self.app.logger.debug("LED turned on")
|
||||
self.led.on()
|
||||
sleep(timing)
|
||||
@@ -122,6 +128,9 @@ class Raspberry:
|
||||
sleep(timing)
|
||||
|
||||
def flash_toggle(self):
|
||||
"""
|
||||
Flashes the flash
|
||||
"""
|
||||
self.app.logger.debug("Flash turned on")
|
||||
self.flash.on()
|
||||
sleep(0.3)
|
||||
@@ -129,6 +138,9 @@ class Raspberry:
|
||||
self.app.logger.debug("Flash turned off")
|
||||
|
||||
def take_picture(self):
|
||||
"""
|
||||
Takes a picture via the USB webcam
|
||||
"""
|
||||
# Validate if the image path is valid
|
||||
if not os.path.isdir(os.path.dirname(self.image_path)):
|
||||
self.app.logger.error(
|
||||
@@ -174,6 +186,9 @@ class Raspberry:
|
||||
position="bottom_right",
|
||||
margin=10,
|
||||
):
|
||||
"""
|
||||
Takes an image and overlays it with a another picture.
|
||||
"""
|
||||
try:
|
||||
image = Image.open(image_path).convert("RGBA")
|
||||
logo = Image.open(logo_path).convert("RGBA")
|
||||
@@ -200,7 +215,9 @@ class Raspberry:
|
||||
y = image.height - logo.height - margin
|
||||
else:
|
||||
raise ValueError(
|
||||
"Invalid position. Choose from 'bottom_right', 'top_left', 'top_right', or 'bottom_left'."
|
||||
"Invalid position." +
|
||||
"Choose from 'bottom_right', 'top_left', " +
|
||||
" 'top_right', or 'bottom_left'."
|
||||
)
|
||||
|
||||
# Composite the logo onto the image
|
||||
@@ -218,6 +235,9 @@ class Raspberry:
|
||||
return True
|
||||
|
||||
def crop_to_square(self, image_path, output_path=None):
|
||||
"""
|
||||
Crop an image so that it becomes a square
|
||||
"""
|
||||
try:
|
||||
image = Image.open(image_path)
|
||||
width, height = image.size
|
||||
@@ -245,6 +265,10 @@ class Raspberry:
|
||||
return True
|
||||
|
||||
def on_button_pressed(self):
|
||||
"""
|
||||
When a button press is detected, a picture is taken from the webcam
|
||||
and added to the print queue.
|
||||
"""
|
||||
self.app.logger.debug("Button has been pressed")
|
||||
self.led.on()
|
||||
self.app.logger.debug("Counting down")
|
||||
|
||||
Reference in New Issue
Block a user