def disable_rx(self): """Disable RX, remove GPIO event detection.""" if self.rx_enabled: GPIO.remove_event_detect(self.gpio) self.rx_enabled = False _LOGGER.debug("RX disabled") return True
def test_remove_event_detect(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) with patch("OPi.GPIO.event") as mock: GPIO.remove_event_detect(23) mock.remove_edge_detect.assert_called_with(14)
def test_remove_event_detect_not_configured_for_input(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.OUT) with pytest.raises(RuntimeError) as ex: GPIO.remove_event_detect(23) assert str(ex.value) == "Channel 23 is configured for output"
def _setup_sensor(self): GPIO.cleanup() GPIO.setmode(GPIO.SUNXI) # Enable Filament Sensor if self.filament_sensor_enabled(): self._logger.info("Filament Sensor active on GPIO Pin [%s]" % self.pin) GPIO.setup(self.pin, GPIO.IN) try: GPIO.remove_event_detect(self.pin) except: self._logger.info("Pin " + str(self.pin) + " not used before") GPIO.add_event_detect(self.pin, GPIO.BOTH, callback=self.filament_sensor_callback, bouncetime=self.poll_time) # Enable Relay Sensor if self.relay_sensor_enabled(): self._logger.info("Relay Sensor active on GPIO Pin [%s]" % self.pin_relay) GPIO.setup(self.pin_relay, GPIO.IN) try: GPIO.remove_event_detect(self.pin_relay) except: self._logger.info("Pin " + str(self.pin_relay) + " not used before") GPIO.add_event_detect(self.pin_relay, GPIO.BOTH, callback=self.relay_sensor_callback, bouncetime=self.poll_time)
def stopFilamentDetection(self): try: for rpi_input in self.rpi_inputs: if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament': GPIO.remove_event_detect(self.toInt(rpi_input['gpioPin'])) except Exception as ex: template = "An exception of type {0} occurred on stopFilamentDetection. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) self._logger.warn(message) pass
def clearGPIO(self): try: for control in self.temperature_control: if control['isEnabled']: GPIO.cleanup(self.toInt(control['gpioPin'])) for rpi_output in self.rpi_outputs: if self.toInt(rpi_output['gpioPin']) not in self.previous_rpi_outputs: GPIO.cleanup(self.toInt(rpi_output['gpioPin'])) for rpi_input in self.rpi_inputs: try: GPIO.remove_event_detect(self.toInt(rpi_input['gpioPin'])) except: pass GPIO.cleanup(self.toInt(rpi_input['gpioPin'])) except Exception as ex: template = "An exception of type {0} occurred on clearGPIO. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) self._logger.warn(message) pass
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)
def resetPinMode(self): print "resetting pin mode" self.stopServod() print "servod stopped" 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
def removeReader(self): RPIO.remove_event_detect(self.GPIO_0) RPIO.remove_event_detect(self.GPIO_1)
def relstop(): print("stop doorrelays") GPIO.setwarnings(False) for gi in inputs: GPIO.remove_event_detect(gi) GPIO.cleanup()
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] try: #print "pinUpdate p,v,t,pwmref: ",pin,value,type,self.pinRef[pin] #print ("Stopping previous instance on pin",pin) #print "pinref on pin" ,pin , "is" ,self.pinRef[pin] self.pinRef[pin].stop() #print ("previous instance on pin",pin ,"stopped") self.pinRef[pin] = None except: pass self.pinUse[pin] = self.POUTPUT # switch it to output GPIO.setup(pin,GPIO.OUT) #print "switched to output" GPIO.output(pin, int(value)) # set output to 1 to 0 #print 'pin' , pin , ' was PWM now set to ' , value except ValueError: print "mistake made in trying to update an invalid pin" print pin,value,type pass
def setPinMode(self): for pin in self.validPins: #print pin try: GPIO.remove_event_detect(pin) except: pass try: self.callbackInUse[pin] = False except: pass if (self.pinUse[pin] == self.POUTPUT): print 'setting pin' , pin , ' to out' 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")
def __init__(self, freqBand, nodeID, networkID, isRFM69HW=False, intPin=18, rstPin=None, spiBus=0, spiDevice=0): self.freqBand = freqBand self.nodeID = nodeID self.networkID = networkID self.isRFM69HW = isRFM69HW self.intPin = intPin self.rstPin = rstPin self.spiBus = spiBus self.spiDevice = spiDevice self.intLock = False self.mode = "" self.promiscuousMode = False self.SENDERID = 0 self.TARGETID = 0 self.PAYLOADLEN = 0 self.ACK_REQUESTED = 0 self.ACK_RECEIVED = 0 self.RSSI = 0 self.DATA = [] self.pk_queue = queue.Queue() self.recv_ack_queue = queue.Queue() self.send_ack_queue = queue.Queue() GPIO.setmode(GPIO.SUNXI) GPIO.setup(self.intPin, GPIO.IN) GPIO.setup("PG8", GPIO.OUT) if self.rstPin is not None: GPIO.setup(self.rstPin, GPIO.OUT) frfMSB = { RF69_315MHZ: RF_FRFMSB_315, RF69_433MHZ: RF_FRFMSB_433, RF69_868MHZ: RF_FRFMSB_868, RF69_915MHZ: RF_FRFMSB_915 } frfMID = { RF69_315MHZ: RF_FRFMID_315, RF69_433MHZ: RF_FRFMID_433, RF69_868MHZ: RF_FRFMID_868, RF69_915MHZ: RF_FRFMID_915 } frfLSB = { RF69_315MHZ: RF_FRFLSB_315, RF69_433MHZ: RF_FRFLSB_433, RF69_868MHZ: RF_FRFLSB_868, RF69_915MHZ: RF_FRFLSB_915 } self.CONFIG = { 0x01: [ REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY ], #no shaping 0x02: [ REG_DATAMODUL, RF_DATAMODUL_DATAMODE_PACKET | RF_DATAMODUL_MODULATIONTYPE_FSK | RF_DATAMODUL_MODULATIONSHAPING_00 ], #default:4.8 KBPS 0x03: [REG_BITRATEMSB, RF_BITRATEMSB_55555], 0x04: [REG_BITRATELSB, RF_BITRATELSB_55555], #default:5khz, (FDEV + BitRate/2 <= 500Khz) 0x05: [REG_FDEVMSB, RF_FDEVMSB_50000], 0x06: [REG_FDEVLSB, RF_FDEVLSB_50000], 0x07: [REG_FRFMSB, frfMSB[freqBand]], 0x08: [REG_FRFMID, frfMID[freqBand]], 0x09: [REG_FRFLSB, frfLSB[freqBand]], # looks like PA1 and PA2 are not implemented on RFM69W, hence the max output power is 13dBm # +17dBm and +20dBm are possible on RFM69HW # +13dBm formula: Pout=-18+OutputPower (with PA0 or PA1**) # +17dBm formula: Pout=-14+OutputPower (with PA1 and PA2)** # +20dBm formula: Pout=-11+OutputPower (with PA1 and PA2)** and high power PA settings (section 3.3.7 in datasheet) #0x11: [REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | RF_PALEVEL_OUTPUTPOWER_11111], #over current protection (default is 95mA) #0x13: [REG_OCP, RF_OCP_ON | RF_OCP_TRIM_95], # RXBW defaults are { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_5} (RxBw: 10.4khz) #//(BitRate < 2 * RxBw) 0x19: [REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_2], #for BR-19200: //* 0x19 */ { REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_24 | RF_RXBW_EXP_3 }, #DIO0 is the only IRQ we're using 0x25: [REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01], #must be set to dBm = (-Sensitivity / 2) - default is 0xE4=228 so -114dBm 0x28: [REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN], # Clear the fifo (p70) 0x29: [REG_RSSITHRESH, 220], #/* 0x2d */ { REG_PREAMBLELSB, RF_PREAMBLESIZE_LSB_VALUE } // default 3 preamble bytes 0xAAAAAA 0x2e: [ REG_SYNCCONFIG, RF_SYNC_ON | RF_SYNC_FIFOFILL_AUTO | RF_SYNC_SIZE_2 | RF_SYNC_TOL_0 ], #attempt to make this compatible with sync1 byte of RFM12B lib 0x2f: [REG_SYNCVALUE1, 0x2D], #NETWORK ID 0x30: [REG_SYNCVALUE2, networkID], 0x37: [ REG_PACKETCONFIG1, RF_PACKET1_FORMAT_VARIABLE | RF_PACKET1_DCFREE_OFF | RF_PACKET1_CRC_ON | RF_PACKET1_CRCAUTOCLEAR_ON | RF_PACKET1_ADRSFILTERING_OFF ], #in variable length mode: the max frame size, not used in TX 0x38: [REG_PAYLOADLENGTH, 66], #* 0x39 */ { REG_NODEADRS, nodeID }, //turned off because we're not using address filtering #TX on FIFO not empty 0x3C: [ REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY | RF_FIFOTHRESH_VALUE ], #RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent) 0x3d: [ REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_2BITS | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF ], #for BR-19200: //* 0x3d */ { REG_PACKETCONFIG2, RF_PACKET2_RXRESTARTDELAY_NONE | RF_PACKET2_AUTORXRESTART_ON | RF_PACKET2_AES_OFF }, //RXRESTARTDELAY must match transmitter PA ramp-down time (bitrate dependent) #* 0x6F */ { REG_TESTDAGC, RF_DAGC_CONTINUOUS }, // run DAGC continuously in RX mode # run DAGC continuously in RX mode, recommended default for AfcLowBetaOn=0 0x6F: [REG_TESTDAGC, RF_DAGC_IMPROVED_LOWBETA0], 0x00: [255, 0] } #initialize SPI self.spi = spidev.SpiDev() self.spi.open(self.spiBus, self.spiDevice) self.spi.max_speed_hz = 4000000 # Hard reset the RFM module if self.rstPin is not None: GPIO.output(self.rstPin, GPIO.HIGH) time.sleep(0.1) GPIO.output(self.rstPin, GPIO.LOW) time.sleep(0.1) #verify chip is syncing? logger.debug(">> syncing") while self.readReg(REG_SYNCVALUE1) != 0xAA: self.writeReg(REG_SYNCVALUE1, 0xAA) while self.readReg(REG_SYNCVALUE1) != 0x55: self.writeReg(REG_SYNCVALUE1, 0x55) logger.debug("<< syncing") #write config for value in self.CONFIG.values(): self.writeReg(value[0], value[1]) self.encrypt(0) self.setHighPower(self.isRFM69HW) # Wait for ModeReady self.setMode(RF69_MODE_STANDBY, waitReady=True) self.ack_sender = RFM69AckSender(self.send_ack_queue, self.sendACK) GPIO.remove_event_detect(self.intPin) GPIO.add_event_detect(self.intPin, GPIO.RISING, callback=self.interruptHandler)