diff --git a/src/main.py b/src/main.py index 1568581..6b4c891 100644 --- a/src/main.py +++ b/src/main.py @@ -77,10 +77,14 @@ printer = Printer(app,0x04b8, 0x0e28) printer.init_printer() # Find out if we are running on a Raspberry Pi -rpi = Raspberry(printer, app, socketio, configuration_file['rpi']['button_gpio_port_number'], configuration_file['rpi']['indicator_gpio_port_number'], configuration_file['rpi']['flash'] ) +rpi = Raspberry(printer, + 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'] ) + RASPBERRY_PI_CONNECTED = rpi.is_raspberry_pi() -if RASPBERRY_PI_CONNECTED: - rpi.initialise_gpio() ############################################################# @@ -91,7 +95,7 @@ if RASPBERRY_PI_CONNECTED: web = Web(app, printer) if __name__ == "__main__": - app.run(ssl_context='adhoc') + app.run(debug=True, use_reloader=False, host='0.0.0.0', ssl_context='adhoc') limiter = Limiter( get_remote_address, diff --git a/src/raspberry.py b/src/raspberry.py index fbfc632..48b5cbe 100644 --- a/src/raspberry.py +++ b/src/raspberry.py @@ -1,5 +1,5 @@ from flask_socketio import SocketIO -from gpiozero import Button, LED +from gpiozero import Button, LED, DigitalOutputDevice from time import sleep import io # To check if we are on a Raspberry Pi import subprocess @@ -13,24 +13,17 @@ class Raspberry(object): - Flash an indicator light """ - def __init__(self, printer, app, socketio, button_gpio_port_number, indicator_gpio_port_number, is_flash_present,): + def __init__(self, printer, app, socketio, button_gpio_port_number, indicator_gpio_port_number, flash_gpio_port_number, is_flash_present,): self.printer = printer 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.image_path = self.app.config['UPLOAD_FOLDER'] + '/image.jpg' - def initialise_gpio(self): - - self.button = Button(self.button_gpio) - self.led = LED(self.led_gpio) - - button.when_pressed = self.on_button_pressed() - - def is_raspberry_pi(self, raise_on_errors=False): # Check if we are running on a raspberry pi try: @@ -59,48 +52,71 @@ class Raspberry(object): return False self.app.logger.debug('It seems we are on a Raspberry Pi') - return True - def take_picture(self): - # return a path to a picture try: - subprocess.run(['fswebcam', '--no-banner', self.image_path]) + self.initialise_gpio() except Exception as e: - self.app.logger.error('Unable to take a picture :' + e) + self.app.logger.debug('Could not init GPIO : ' + str(e)) raise e return True - def flash(state): - if state: - # turn on the flash - pass - else: - # turn off the flash - pass + def initialise_gpio(self): + self.app.logger.debug('Initializing GPIO') - def flash_indicator(self): - # The indicator will flash three times before return true - self.led.on() - sleep(0.1) - self.led.off() - sleep(1) - self.led.on() - sleep(0.1) - self.led.off() - sleep(1) - self.led.on() - sleep(0.1) - self.led.off() + self.led = LED(self.led_gpio) + self.app.logger.debug('Activated indicator LED') - def on_button_pressed(): + self.button = Button(self.button_gpio, 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.app.logger.debug('Activated flash') + + + def take_picture(self): + try: + subprocess.run(['fswebcam', '--no-banner', self.image_path]) + except Exception as e: + self.app.logger.error('Unable to take a picture :' + str(e)) + raise e + return True + + def indicator_countdown(self,iters=10,multi=10): + for i in range(iters,0,-1): + self.led.on() + sleep(i/multi) + self.led.off() + sleep(i/multi) + + def indicator_led(self,timing=0.2,l=5): + for i in range(l): + self.app.logger.debug("LED turned on") + self.led.on() + sleep(timing) + self.led.off() + self.app.logger.debug("LED turned off") + sleep(timing) + + def flash_toggle(self): + self.app.logger.debug("Flash turned on") + self.flash.on() + sleep(0.3) + self.flash.off() + self.app.logger.debug("Flash turned off") + + + + def on_button_pressed(self): + self.app.logger.debug("Button has been pressed") self.socketio.emit('button_pressed') # Notify clients that a button has been pressed on the raspberry pi - self.flash_indicator() # The indicator will flash a countdown LED - self.flash(True) + self.indicator_countdown(iters=5,multi=20) # The indicator will flash a countdown LED + self.flash_toggle() try: self.take_picture() self.printer.print_image(self.image_path) + self.socketio.emit('picture_taken') # Notify clients that a picture has been taken except Exception as e: - app.logger.error("Could not take a picture after the button press : " + e) - self.led.on() - self.flash(False) - self.socketio.emit('picture_taken') # Notify clients that a picture has been taken + self.app.logger.error("Could not take a picture after the button press : " + str(e)) + return True \ No newline at end of file