Example #1
0
 def on_event(self, event, payload):
     # Early abort in case of out ot filament when start printing, as we
     # can't change with a cold nozzle
     if event is Events.PRINT_STARTED and self.no_filament():
         self._logger.info("Printing aborted: no filament detected!")
         self._printer.cancel_print()
     # Enable sensor
     if event in (
         Events.PRINT_STARTED,
         Events.PRINT_RESUMED
     ):
         self._logger.info("%s: Enabling filament sensor." % (event))
         if self.sensor_enabled():
             GPIO.remove_event_detect(self.pin)
             GPIO.add_event_detect(
                 self.pin, GPIO.BOTH,
                 callback=self.sensor_callback,
                 bouncetime=self.bounce
             )
     # Disable sensor
     elif event in (
         Events.PRINT_DONE,
         Events.PRINT_FAILED,
         Events.PRINT_CANCELLED,
         Events.ERROR
     ):
         self._logger.info("%s: Disabling filament sensor." % (event))
         GPIO.remove_event_detect(self.pin)
Example #2
0
 def testInvalidBouncetime(self):
     with self.assertRaises(ValueError):
         GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=-1)
     with self.assertRaises(ValueError):
         GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=-1)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=123)
     with self.assertRaises(RuntimeError):
         GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=321)
     GPIO.remove_event_detect(LOOP_IN)
Example #3
0
 def testRisingEventDetected(self):
     GPIO.output(LOOP_OUT, GPIO.LOW)
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.output(LOOP_OUT, GPIO.HIGH)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), True)
     GPIO.output(LOOP_OUT, GPIO.LOW)
     time.sleep(0.01)
     self.assertEqual(GPIO.event_detected(LOOP_IN), False)
     GPIO.remove_event_detect(LOOP_IN)
Example #4
0
    def testExceptionInCallback(self):
        self.run_cb = False

        def cb(channel):
            with self.assertRaises(ZeroDivisionError):
                self.run_cb = True
                a = 1 / 0

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
        time.sleep(0.01)
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        time.sleep(0.01)
        self.assertEqual(self.run_cb, True)
        GPIO.remove_event_detect(LOOP_IN)
Example #5
0
    def testWaitForEdgeWithCallback(self):
        def cb():
            raise Exception("Callback should not be called")

        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t = Timer(0.1, makehigh)

        GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
        t.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_callback(LOOP_IN, callback=cb)
        with self.assertRaises(RuntimeError):  # conflicting edge exception
            GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.remove_event_detect(LOOP_IN)
Example #6
0
 def resetPinMode(self):
     #print "resetting pin mode" 
     self.stopServod()
     for pin in self.validPins:
         try:
             self.pinRef[pin].stop() # stop PWM from running
             self.pinRef[pin] = None
         except:
             pass
         self.pinRef[pin] = None #reset pwm flag
             
         try:
             GPIO.remove_event_detect(pin) #Stop Any event detection for input and counting
         except:
             pass
             
         try:
             self.callbackInUse[pin] = False  #reset event callback flags
         except:
             pass
             
         if (self.pinUse[pin] == self.POUTPUT):
             GPIO.setup(pin,GPIO.IN)   
         elif (self.pinUse[pin] == self.PINPUT):
             GPIO.setup(pin,GPIO.IN)   
         elif (self.pinUse[pin] == self.PINPUTDOWN):
             GPIO.setup(pin,GPIO.IN)  
         elif (self.pinUse[pin] == self.PINPUTNONE):
             GPIO.setup(pin,GPIO.IN)
         elif (self.pinUse[pin] == self.PCOUNT):
             GPIO.setup(pin,GPIO.IN)
         self.pinUse[pin] = self.PUNUSED
         self.pinServoValue[pin] = None
         
         print "reset pin", pin
         self.pinValue[pin] = 0
         self.pinInvert[pin] = False
Example #7
0
    def testAddEventCallback(self):
        def cb(channel):
            self.callback_count += 1

        # falling test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        GPIO.add_event_detect(LOOP_IN, GPIO.FALLING)
        GPIO.add_event_callback(LOOP_IN, cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 2048)

        # rising test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 2048)

        # both test
        self.callback_count = 0
        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.BOTH, callback=cb)
        time.sleep(0.01)
        for i in range(2048):
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.001)
        GPIO.remove_event_detect(LOOP_IN)
        self.assertEqual(self.callback_count, 4096)
Example #8
0
    def pinUpdate(self, pin, value,type = 'plain',stepDelay = 0.003):
        print "pinUpdate p,v,t,pwmref: ",pin,value,type,self.pinRef[pin]
        print "pin",pin
        print "pvalue",self.pinValue
        self.pinValue[pin] = value
        self.mFreq = max(5,abs(value/2))
        if (self.ledDim < 100) and (type == 'plain'):
            type = "pwm"
            value = value * self.ledDim
        try:
            print pin,value,type,self.pinUse[pin]
            if type[0:3] == "pwm": # 
                print "processing pwm"
                #return
                if (self.pinInvert[pin] == True): # Invert data value (needed for active low devices)
                    value = 100 - abs(value)
                    
                print "motor freq calc", self.mFreq
                try: 
                    print "try jsut updating pwm"
                    self.pinRef[pin].ChangeDutyCycle(max(0,min(100,abs(value)))) # just update PWM value
                    if type == "pwmmotor":
                        #print "motor freq used a", self.mFreq
                        self.pinRef[pin].ChangeFrequency(self.mFreq) # change freq to motor freq
                    elif type != "pwmbeep":
                        #print "motor freq used a", self.mFreq
                        self.pinRef[pin].ChangeFrequency(self.pFreq) # change freq to motor freq                        
             
                    print "updating pwm suceceed"
                except:
                    print "pwm not set so now setting up"
                    try:
                        GPIO.remove_event_detect(pin)
                        self.callbackInUse[pin] = False
                    except:
                        pass
                       
                    GPIO.setup(pin,GPIO.OUT) # Setup
                    if type == "pwmmotor":
                        #print "motor freq used b", self.mFreq
                        self.pinRef[pin] = GPIO.PWM(pin,self.mFreq) # create new PWM instance
                    elif type != "pwmbeep":
                        #print "motor freq used a", self.mFreq
                        self.pinRef[pin] = GPIO.PWM(pin,self.pFreq) # create new PWM instance
                    self.pinRef[pin].start(max(0,min(100,abs(value)))) # update PWM value
                    print "pwm setup on pin",pin, "now has ref",self.pinRef[pin]
                    self.pinUse[pin] = self.PPWM # set pin use as PWM
         
            elif type == "plain":
                print "Plain processing- Pin " , pin , " commanded to be " , value
                print "pinUpdate p,v,t,pwmref: ",pin,value,type,self.pinRef[pin]
                if (self.pinInvert[pin] == True): # Invert data value (useful for 7 segment common anode displays)
                    value = 1 - abs(value)
                if (self.pinUse[pin] == self.POUTPUT): # if already an output
                    GPIO.output(pin, int(value)) # set output to 1 ot 0
                    print 'pin' , pin , ' was already an output.  Now set to' , value
                    
                elif (self.pinUse[pin] in [self.PINPUT,self.PINPUTNONE,self.PINPUTDOWN]): # if pin is an input
                    try:
                        GPIO.remove_event_detect(pin)
                        self.callbackInUse[pin] = False
                    except:
                        pass  
                    self.pinUse[pin] = self.POUTPUT # switch it to output
                    GPIO.setup(pin,GPIO.OUT)
                    GPIO.output(pin, int(value)) # set output to 1 to 0
                    #print 'pin' , pin , ' was an input - change to output value' , value                    
               
                  
                elif (self.pinUse[pin] == self.PUNUSED): # if pin is not allocated
                    self.pinUse[pin] = self.POUTPUT # switch it to output
                    GPIO.setup(pin,GPIO.OUT)
                    GPIO.output(pin,int(value)) # set output to 1 or 0
                    print 'pin' , pin , ' was ununsed - now out value ' , value            

                elif (self.pinUse[pin] == self.PPWM): # pin was set as pwm
                    print "pinUpdate p,v,t,pwmref: ",pin,value,type,self.pinRef[pin]
                    print "pwm pin", pin , " sent digital on off: ",value
                    value = 100 if value else 0
                    self.pinRef[pin].ChangeDutyCycle(value)

                    
        except ValueError:
            print "mistake made in trying to update an invalid pin"
            print pin,value,type
            pass
Example #9
0
 def setPinMode(self):
     for pin in self.validPins:
         #print pin
         if (self.pinUse[pin] == self.POUTPUT):
             print 'setting pin' , pin , ' to out' 
             try:
                 GPIO.remove_event_detect(pin)
             except:
                 pass
             try:
                 self.callbackInUse[pin] = False
             except:
                 pass                    
             GPIO.setup(pin,GPIO.OUT)
             if (self.pinInvert[pin] == True):
                 GPIO.output(pin,1)
             else:
                 GPIO.output(pin,0)
             self.pinValue[pin] = 0
         elif (self.pinUse[pin] == self.PINPUT):
             print 'setting pin' , pin , ' to in with pull up' 
             GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass
         elif (self.pinUse[pin] == self.PINPUTDOWN):
             print 'setting pin' , pin , ' to in with pull down' 
             GPIO.setup(pin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass             
         elif (self.pinUse[pin] == self.PINPUTNONE):
             print 'setting pin' , pin , ' to in with pull down' 
             GPIO.setup(pin,GPIO.IN)
             try:
                 GPIO.add_event_detect(pin, GPIO.BOTH, callback=self.gpioBoth,bouncetime=50)  # add rising edge detection on a channel
             except:
                 pass             
         elif (self.pinUse[pin] == self.PCOUNT):
             if self.callbackInUse[pin] == False:
                 print 'setting pin' , pin , ' as counting pin' 
                 GPIO.setup(pin,GPIO.IN)#,pull_up_down=GPIO.PUD_DOWN)#,pull_up_down=GPIO.PUD_DOWN)
                 try: # add event callback but use try block just in case its already set
                     if self.encoderCallback == 1:
                         #GPIO.add_event_detect(pin, GPIO.RISING, callback=self.my_callbackB)#,bouncetime=10)  # add rising edge detection on a channel
                         self.callbackInUse[pin] = True
                         self.encoderCallback = 2
                         if self.debug:
                             print "callback B set for pin ", pin
                         
                     if self.encoderCallback == 0:
                         #GPIO.add_event_detect(pin, GPIO.RISING, callback=self.my_callbackA)#,bouncetime=10)  # add rising edge detection on a channel
                         self.callbackInUse[pin] = True
                         self.encoderCallback = 1
                         if self.debug:
                             print "callback A set for pin ", pin
                         
                 except Exception,e: 
                     print "Error on event detection setup on pin" ,pin
                     print str(e)
             else:
                 print ("Callback already in use")
Example #10
0
 def testAlreadyAdded(self):
     GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     with self.assertRaises(RuntimeError):
         GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
     GPIO.remove_event_detect(LOOP_IN)