def interrupt(self, callback, pin, interrupt_trigger=None, timeout=0): """Init interrupt callback function for pin. Args: callback (function): function to call on interrupt pin (int): the pin to monitor for interrupts """ with self._gpio_lock: gpio_pin = GPIO(pin, "preserve") if gpio_pin.supports_interrupts: gpio_pin.direction = "in" gpio_pin.edge = interrupt_trigger if gpio_pin.poll(timeout): # Enter callback function pass gpio_pin.edge = "none" #need to set to none inorder to reuse pin for anything else else: self.logger.error( "GPIO pin {} does not support interrupts".format(pin)) self.logger.debug( "Set interrupt callback of GPIO pin {}".format(pin))
#!/usr/bin/env python3 # #Author: Giovanni Rasera Per Cynexo #Date: 2021 03 22 # from periphery import GPIO import sys import wave import getopt import signal import alsaaudio gpio_CynexoFrontButton = GPIO("/dev/gpiochip4", 12, "in") gpio_CynexoFrontButton.edge = "rising" def play(device, f): periodsize = f.getframerate() // 8 device = alsaaudio.PCM() #Aspetta per farlo partire data = f.readframes(periodsize) #Prebuffering #Interrupt gpio_CynexoFrontButton.read_event() #Leggi e suona while data: device.write(data) data = f.readframes(periodsize) def usage(): print('usage: playwav.py [-d <device>] <file>', file=sys.stderr) sys.exit(2)
def main(): print("initializing") pool = ThreadPool(processes=1) #Time operator have to push button after product is detected IMG_SAVE_DELAY = 500 #This is used to signal that if operator dont push button then save image to approved folder flagPhotoSensor = False #Resolution on image resolution = (1280, 720) #Paths to put images path_approved = '/media/storage/good/' path_not_approved = '/media/storage/bad/' #Create camera object that takes images and store in approved or not approved folder camera = Camera(resolution, 'png', path_approved, path_not_approved) #GPIO stuff on_off = GPIO(7, 'in') disallowed_img = GPIO(138, 'in') disallowed_img.edge = 'rising' photoSensor = GPIO(140, 'in') photoSensor.edge = 'rising' #Create Display object display = Display() #Variable to handle debounce on input from photosensor lastPhotoSensorValue = False #Variable to handle debounce on input from operator button lastOperatorBtnValue = False #Get time in ms currentTime_ms = lambda: int(round(time.time() * 1000)) #Calculate new time - old time to get time difference between two events deltaTime_ms = lambda oldTime_ms: int(round(time.time() * 1000) ) - oldTime_ms #Store time to calculate time difference oldTime_ms = currentTime_ms() print("initialization done") #Run forever while True: #If system is turned on by switch, run program display.off() if on_off.read() or True: print("System started") #Make sure no old values are stored flagPhotoSensor = False camera.camera.start() #Stay in loop as long as system is turned on while on_off.read() or True: #If there is a product in front of camera, take a picture and record the time if photoSensor.read( ) and not lastPhotoSensorValue and not flagPhotoSensor: #img = camera.camera.get_image() async_img = pool.apply_async(camera.take_image) oldTime_ms = currentTime_ms() #Save that photo sensor has been high flagPhotoSensor = True #If operator push button before IMG_SAVE_DELAY time, save image to not approved folder #and blink red light once to inform operator while deltaTime_ms(oldTime_ms) <= IMG_SAVE_DELAY: if disallowed_img.read() and not lastOperatorBtnValue: img = async_img.get() threading.Thread(target=display.show_img, args=(img, False)).start() camera.save_img(img, False) flagPhotoSensor = False lastOperatorBtnValue = disallowed_img.read() #if operator dont push button before IMG_SAVE_DELAY time run out, save #image to approved folder and blink green light. if (deltaTime_ms(oldTime_ms) > IMG_SAVE_DELAY) and flagPhotoSensor: img = async_img.get() threading.Thread(target=display.show_img, args=(img, True)).start() camera.save_img(img, True) flagPhotoSensor = False camera.camera.stop() lastPhotoSensorValue = photoSensor.read()