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 __init__(self,stop_event): # Call the Thread class's init function threading.Thread.__init__(self) self.stop_event=stop_event self.prog='' g.setwarnings(False) g.setmode(g.BOARD)
def gpio_init(): GPIO.setboard(GPIO.PCPCPLUS) GPIO.setmode(GPIO.BOARD) # 这个接口接到LED的正极,LED的负极接到GND即可 GPIO.setup(16, GPIO.OUT) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) print("GPIO init success!")
def test_wait_for_edge_not_configured_for_input(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) with pytest.raises(RuntimeError) as ex: GPIO.wait_for_edge(12, GPIO.RISING) assert str(ex.value) == "Channel 12 is configured for output"
def __init__(self): # Distances in mm that each step of the stepper motor propels each axis self.mm_per_step = {'x': 0.000523, 'y': 0.000523, 'z': 0.000523} # This value will be used as speed for the 'rapid movement' of the machine # Measured in steps per second self.rapid_speed_maximum = 150.0 # This value will be used as the fastest to move the machine when mill is lowered # Measured in steps per second self.work_speed_maximum = 150.0 # These are the pins to control the L293D motor drivers for the CNC # in the order they are activated self.control_pins = {'x': (35, 31, 37, 33), 'y': (38, 32, 40, 36)} # 'z': (23, 27, 17, 22)} # Tracks how many steps have been taken on each axis at any point self.current_steps = {'x': 0, 'y': 0, 'z': 0} # Configure pinout options GPIO.setboard(GPIO.PCPCPLUS) GPIO.setmode(GPIO.BOARD) # Setup motor control pins for pins in self.control_pins.values(): for pin in pins: GPIO.setup(pin, GPIO.OUT)
def test_add_event_detect(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) with patch("OPi.GPIO.event") as mock: GPIO.add_event_detect(23, GPIO.BOTH) mock.add_edge_detect.assert_called_with(14, GPIO.BOTH, None)
def __init__(self, events): self.left_control_pins = [3, 5, 7, 16] self.right_control_pins = [15, 19, 21, 23] self.right_halfstep_seq = [ [1, 0, 0, 0], [1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [1, 0, 0, 1] ] self.right_step_index = 0 self.left_halfstep_seq = self.right_halfstep_seq[::-1] self.left_step_index = 0 self.left_wheel_enabled = False self.right_wheel_enabled = False self.out_pins = [0,0] self.out_values = [0,0] if is_arm_platform: # GPIO.setboard(GPIO.ZEROPLUS) GPIO.setmode(GPIO.BOARD) init_control_pins(self.left_control_pins) init_control_pins(self.right_control_pins) self.stop_event = events[0] self.enable_left_wheel_event = events[1] self.enable_right_wheel_event = events[2] self.left_wheel_backward_event = events[3] self.right_wheel_backward_event = events[4]
def test_add_event_callback(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) with patch("OPi.GPIO.event") as mock: GPIO.add_event_callback(23, None) mock.add_edge_callback(14, None)
def resetModem(): try: GPIO.setmode(GPIO.BOARD) GPIO.setup(onPin, GPIO.OUT) GPIO.setup(pwrKey, GPIO.OUT) GPIO.setup(statusPin, GPIO.IN) except Exception as e: pass GPIO.output(onPin, GPIO.LOW) # set the GSM ON/OFF pin to low to turn off the modem time.sleep(10) GPIO.output(onPin, GPIO.HIGH) # set the GSM ON/OFF pin to high to turn on the modem time.sleep(5) # Then Toggle the power key GPIO.output(pwrKey, GPIO.HIGH) GPIO.output(pwrKey, GPIO.LOW) time.sleep(5) GPIO.output(pwrKey, GPIO.HIGH) time.sleep(30) status = GPIO.input(statusPin) try: if status == 1: subprocess.call(['sudo sakis3g "connect" DNS="8.8.8.8" APN="CUSTOM_APN" CUSTOM_APN="airtelgprs.com" APN_USER="******" APN_PASS="******" USBINTERFACE="3" OTHER="USBMODEM" USBMODEM="1e0e:9001"'], shell=True) except Exception as e: print(e) time.sleep(5) print('GSM Status: ', GPIO.input(statusPin))
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_input_not_configured_for_output(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) with pytest.raises(RuntimeError) as ex: GPIO.output(23, GPIO.LOW) assert str(ex.value) == "Channel 23 is configured for input"
def test_event_detected_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.event_detected(23) assert str(ex.value) == "Channel 23 is configured for output"
def test_setup_single_input_channel(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.SUNXI) GPIO.setup("PA01", GPIO.IN) mock.export.assert_called_with(1) mock.direction.assert_called_with(1, GPIO.IN) assert "PA01" in GPIO._exports
def test_input(): with patch("OPi.GPIO.sysfs") as mock: mock.input.return_value = GPIO.HIGH GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) assert GPIO.input(23) == GPIO.HIGH mock.input.assert_called_with(14)
def setup(): """ Configure the Raspberry GPIOs :rtype : None """ GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # LED, RST, and NSS GPIO.setup(BOARD.LED, GPIO.OUT) GPIO.setup(BOARD.RST, GPIO.OUT) GPIO.setup(BOARD.NSS, GPIO.OUT) GPIO.output(BOARD.LED, 0) GPIO.output(BOARD.RST, 0) time.sleep(.01) GPIO.output(BOARD.RST, 1) time.sleep(.01) GPIO.output(BOARD.NSS, 1) # switch if BOARD.SWITCH is not None: GPIO.setup(BOARD.SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # DIOx for gpio_pin in [BOARD.DIO0, BOARD.DIO1, BOARD.DIO2, BOARD.DIO3]: if gpio_pin is not None: GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # blink 2 times to signal the board is set up BOARD.blink(.1, 2)
def test_event_detected(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.IN) with patch("OPi.GPIO.event") as mock: mock.edge_detected.return_value = True assert GPIO.event_detected(23)
def __init__(self): self.volume = 1 self.preMuteVolume = self.volume self.button = 0 self.last_screen_update = time.time() self.cast = False print('Trying to get handle to Chromecast ' + config.chromecasts[0] + '...') try: self.get_cast_handle(config.chromecasts[0]) except cat_api.CastError as error: logging.error(logging.exception(error)) sys.exit("ERROR: Couldn't connect to '" + config.chromecasts[0] + "'. Check config.py and name/IP.") if config.use_display: print('Trying to initialize screen on default i2c bus...') try: self.screen = Oled(); self.show(';)') sleep(.5) except Exception as error: logging.error(logging.exception(error)) sys.exit('ERROR Could not access screen: ' + str(error)) if config.board_type == 'orange': GPIO.setboard(GPIO.ZERO) # for orange pi zero GPIO.setmode(GPIO.BOARD) # for orange pi zero if config.board_type == 'raspberry': GPIO.setmode(GPIO.BCM) # for rasbpian on raspberry pi GPIO.setup(config.clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(config.dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(config.sw, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(config.sw, GPIO.FALLING, callback=self.toggleMute, bouncetime=500)
def test_wait_for_edge_completes(): with patch("OPi.GPIO.sysfs"): GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.IN) with patch("OPi.GPIO.event") as mock: mock.blocking_wait_for_edge.return_value = 1 assert GPIO.wait_for_edge(12, GPIO.BOTH) == 12 mock.blocking_wait_for_edge.assert_called_with(7, GPIO.BOTH, -1)
def gpio_init(): GPIO.setboard(GPIO.PCPCPLUS) GPIO.setmode(GPIO.BOARD) # 这个接口接到LED的正极,LED的负极接到GND即可 GPIO.setup(16, GPIO.OUT) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(18, GPIO.RISING, button_event, bouncetime=200) print("GPIO init success!")
def test_input_and_output(): with patch("OPi.GPIO.sysfs") as mock: mock.input.return_value = GPIO.HIGH GPIO.setmode(GPIO.BOARD) GPIO.setup(23, GPIO.OUT) GPIO.output(23, not GPIO.input(23)) mock.input.assert_called_with(14) mock.output.assert_called_with(14, GPIO.LOW)
def test_setup_single_output_channel_with_initial_value(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.SUNXI) GPIO.setup("PG06", GPIO.OUT, GPIO.HIGH) mock.export.assert_called_with(198) mock.direction.assert_called_with(198, GPIO.OUT) mock.output.assert_called_with(198, GPIO.HIGH) assert "PG06" in GPIO._exports
def test_mode(): assert GPIO.getmode() is None GPIO.setmode(GPIO.BCM) assert GPIO.getmode() == GPIO.BCM GPIO.cleanup() assert GPIO.getmode() is None with pytest.raises(AssertionError): GPIO.setmode(54335)
def __init__(self, dev='/dev/spidev0.0', spd=1000000): global spidev spidev = spi.openSPI(device=dev, speed=spd) #GPIO.setboard(GPIO.PCPCPLUS) GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(self.NRSTPD, 1) self.MFRC522_Init()
def setupPins(): try: GPIO.setmode(GPIO.BOARD) GPIO.setup(onPin, GPIO.OUT) GPIO.setup(pwrKey, GPIO.OUT) GPIO.setup(statusPin, GPIO.IN) except Exception as e: LOG.error('Error setting up the GSM PINs : %s', e)
def test_setup_single_output_channel(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.SUNXI) GPIO.setup("PG07", GPIO.OUT) mock.export.assert_called_with(199) mock.direction.assert_called_with(199, GPIO.OUT) mock.output.assert_not_called() assert "PG07" in GPIO._exports
def test_multiple_output(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.BOARD) GPIO.setup([23, 13, 3], GPIO.OUT) GPIO.output([23, 13, 3], GPIO.LOW) mock.output.assert_has_calls( [call(14, GPIO.LOW), call(0, GPIO.LOW), call(12, GPIO.LOW)])
def startGPIO(self): try: GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) except Exception as ex: template = "An exception of type {0} occurred on startGPIO. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) self._logger.warn(message) pass
def test_setup_channel_already_in_use_raises_IOError(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.SUNXI) mock.export.side_effect = [IOError(16, "test"), None] GPIO.setup("PG06", GPIO.OUT) mock.export.assert_called_with(198) mock.unexport.assert_called_with(198) mock.direction.assert_called_with(198, GPIO.OUT) mock.output.assert_not_called() assert "PG06" in GPIO._exports
def test_setup_channel_already_setup(): with patch("OPi.GPIO.sysfs") as mock: GPIO.setmode(GPIO.SUNXI) GPIO.setup("PA01", GPIO.IN) mock.export.assert_called_with(1) mock.direction.assert_called_with(1, GPIO.IN) assert "PA01" in GPIO._exports with pytest.raises(RuntimeError) as ex: GPIO.setup("PA01", GPIO.OUT) assert str(ex.value) == "Channel PA01 is already configured"
def setup_button(): GPIO.setboard(GPIO.ZERO) # Orange Pi Zero board GPIO.setmode(GPIO.SOC) # set up SOC numbering button = GPIO.PA + 10 # button is on PA10 led_button = GPIO.PA + 14 GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN ) # set PA10 as an output (Status led of board) GPIO.setup(led_button, GPIO.OUT) print('Button ready') return button, led_button
def __init__(self, dev='/dev/spidev1.0', spd=1000000): spi.openSPI(device=dev,speed=spd) GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(self.NRSTPD, 1) self.MFRC522_Init()