def _switch(self, switch): self.bit = [ 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 136, 128, 0, 0, 0 ] for t in range(5): if self.system_code[t]: self.bit[t] = 136 x = 1 for i in range(1, 6): if self.unit_code & x > 0: self.bit[4 + i] = 136 x = x << 1 if switch == wiringpi.HIGH: self.bit[10] = 136 self.bit[11] = 142 bangs = [] for y in range(16): x = 128 for i in range(1, 9): b = (self.bit[y] & x > 0) and wiringpi.HIGH or wiringpi.LOW bangs.append(b) x = x >> 1 wiringpi.wiringPiSetupSys() wiringpi.pinMode(self.pin, wiringpi.OUTPUT) wiringpi.digitalWrite(self.pin, wiringpi.LOW) for z in range(self.repeat): for b in bangs: wiringpi.digitalWrite(self.pin, b) time.sleep(self.pulselength / 1000000.)
def run(self): motionFirstDetectedTime = None SLEEP_TIME = 0.5 # Set up the GPIO input for motion detection PIR_PIN = 18 wiringpi.wiringPiSetupSys() wiringpi.pinMode(PIR_PIN, wiringpi.INPUT) # Loop through and detect motion while True: if wiringpi.digitalRead(PIR_PIN): if motionFirstDetectedTime is None: motionFirstDetectedTime = datetime.now() print('Motion detected at: ' + str(motionFirstDetectedTime)) # Do we need to send out a notification? now = datetime.now() if (motionFirstDetectedTime is not None) and (now - motionFirstDetectedTime) > timedelta (minutes = 1): print('Sending out notification now!') motiondate = datetime.strftime(motionFirstDetectedTime, '%Y-%m-%d %H:%M:%S') msg = self.message + ': ' + motiondate self._send_email(msg) # reset state motionFirstDetectedTime = None time.sleep(SLEEP_TIME)
def emergency_call(): w.wiringPiSetupSys() w.pinMode(PIN, INPUT) pub = rospy.Publisher('emergency_flag', Bool, queue_size=10) rospy.init_node('emergency', anonymous=True) r = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): # if w.input(PIN) == 1: # e_flag = True % rospy.get_time() # rospy.loginfo(e_flag) # pub.publish(e_flag) # # else: # e_flag = False % rospy.get_time() # rospy.loginfo(e_flag) # pub.publish(e_flag) e_flag = True % rospy.get_time() rospy.loginfo(e_flag) pub.publish(e_flag) wiringPiISR(PIN, INT_EDGE_FALLING, pub_e_flag) if e_flag: print('e_flag true') r.sleep()
def main(): config = configparser.ConfigParser() config.read(CONF) device = config['device']['addr'] pin = int(config['device']['pin']) signal_length = int(config['device']['signal_length']) wp.wiringPiSetupSys() wp.pinMode(pin, 1) wp.digitalWrite(pin, HIGH) k = 0 while True: config = configparser.ConfigParser() config.read(CONF) if config['calibration']['activate'] == '1': code = config['power']['code'] code += config['calibration']['code'] sendCommands(code, device, pin, signal_length) # Reset config to defaults (except power) config['calibration']['activate'] = '0' config['calibration']['code'] = '0000' with open(CONF, 'w') as updated_conf: config.write(updated_conf) if k == CHK_DELAY: device = config['device']['addr'] signal_length = int(config['device']['signal_length']) pin = int(config['device']['pin']) k += 1 k %= (CHK_DELAY + 1) time.sleep(1)
def _switch(self, switch): self.bit = [142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 136, 128, 0, 0, 0] for t in range(5): if self.system_code[t]: self.bit[t] = 136 x = 1 for i in range(1, 6): if self.unit_code & x > 0: self.bit[4 + i] = 136 x = x << 1 if switch == wiringpi.HIGH: self.bit[10] = 136 self.bit[11] = 142 bangs = [] for y in range(16): x = 128 for i in range(1, 9): b = (self.bit[y] & x > 0) and wiringpi.HIGH or wiringpi.LOW bangs.append(b) x = x >> 1 wiringpi.wiringPiSetupSys() wiringpi.pinMode(self.pin, wiringpi.OUTPUT) wiringpi.digitalWrite(self.pin, wiringpi.LOW) for z in range(self.repeat): for b in bangs: wiringpi.digitalWrite(self.pin, b) time.sleep(self.pulselength / 1000000.0)
def __init__(self): gpio.setmode(gpio.BOARD) gpio.setup(READY_PIN_ID, gpio.IN) wiringpi.wiringPiSetupSys() self.i2c = wiringpi.I2C() self.bus = self.i2c.setup(SHT31_ADDRESS) time.sleep(0.03)
def gpio_setup(pc): _gpio_cleanup(pc) _gpio_export(settings.PCS[pc]['gpio']['power'], 'out') _gpio_export(settings.PCS[pc]['gpio']['mains_power'], 'out') _gpio_export(settings.PCS[pc]['gpio']['power_led'], 'in') wiringpi.wiringPiSetupSys() wiringpi.pinMode(settings.PCS[pc]['gpio']['power'], 1) wiringpi.pinMode(settings.PCS[pc]['gpio']['mains_power'], 1) wiringpi.pinMode(settings.PCS[pc]['gpio']['power_led'], 0)
def configureGPIO(): wiringpi.wiringPiSetupSys() # gpio export 4 high # gpio export 17 high subprocess.call(["gpio", "export", str(HEATER_PIN), "high"]) subprocess.call(["gpio", "export", str(FAN_PIN), "high"]) wiringpi.pinMode(HEATER_PIN, 1) # Output mode wiringpi.digitalWrite(HEATER_PIN, 1) # Pins are activeHI, so turn off. wiringpi.pinMode(FAN_PIN, 1) # Output mode wiringpi.digitalWrite(FAN_PIN, 1) # Pins are activeHI, so turn off.
def __init__(self): os.system(INIT) wiringpi.wiringPiSetupSys() self.io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS) #self.io.pinMode(pwmPin, self.io.PWM_OUTPUT) #self.io.pinMode(leftSensorPin, self.io.INPUT) #self.io.pinMode(rightSensorPin, self.io.INPUT) self.engines = Engines(self) Ping.start(self) Web.start(self) while 1: time.sleep(10)
def __init__(self, names, ns='~motors'): self.ns = ns wp.wiringPiSetupSys() self.gpio = wp i2c_id = MotorManager.getPiI2CBusNumber() rospy.loginfo('Opening i2c bus [ {} ]'.format(i2c_id)) self.bus = smbus.SMBus( i2c_id ) self.motors = {name: Motor(self.bus, self.gpio, ns + '/' + name) for name in names} self.rc = DynamicReconfigureServer(MotorManagerConfig, self.reconfigureCallback, ns) rospy.on_shutdown(self.cleanup)
def gpioInitSetup(): wiringpi.wiringPiSetupSys() subprocess.call(["gpio", "export", str(HEATER_PIN), "high"]) subprocess.call(["gpio", "export", str(FAN_PIN), "high"]) # # By default, we turn off the heater and the FAN when # we first startup. No harm done because THERMOD will # put them back on if that's the state that's config'd # in STATUS_FILE # wiringpi.pinMode(HEATER_PIN, 1) # Output mode wiringpi.digitalWrite(HEATER_PIN, 1) # Pins are activeHI, so turn off. wiringpi.pinMode(FAN_PIN, 1) # Output mode wiringpi.digitalWrite(FAN_PIN, 1) # Pins are activeHI, so turn off.
def build(self): self.handlingKey = False self.manager = ScanRoot() # Set up GPIO pin for foot pedal os.system("gpio export 21 up") wiringpi.wiringPiSetupSys() #wiringpi.pinMode(21, 0) #wiringpi.pullUpDnControl(21, 2) self.lastPedal = 0 Clock.schedule_interval(self.update, 0.5) Clock.schedule_interval(self.checkPedal, 0.05) Window.bind(on_key_down=self.on_key_down) Window.bind(on_key_up=self.on_key_up) return self.manager
def gpio_init(self): wp.wiringPiSetupSys() # Set output for those pins : wp.pinMode(self.pin_power_left, wp.OUTPUT) wp.pinMode(self.pin_power_right, wp.OUTPUT) wp.pinMode(self.pin_direction_left_forward, wp.OUTPUT) wp.pinMode(self.pin_direction_right_forward, wp.OUTPUT) wp.pinMode(self.pin_direction_left_rear, wp.OUTPUT) wp.pinMode(self.pin_direction_right_rear, wp.OUTPUT) ## create the SoftPwm on power pins : wp.softPwmCreate(self.pin_power_left, 0, self.max_speed) wp.softPwmCreate(self.pin_power_right, 0, self.max_speed) ## reset everyone : self.gpio_zero()
def __init__(self, address=0x3E, interruptPin=None, resetPin=None, oscillatorPin=None): self.address = address self.interruptPin = interruptPin self.resetPin = resetPin self.oscillatorPin = oscillatorPin wiringpi.wiringPiSetupSys() self.i2c = wiringpi.I2C() self.device = self.i2c.setup(self.address) data = [0x00, 0x00] data[0] = self.i2c.readReg8(self.device, self.REGISTERS['CHECK']) data[1] = self.i2c.readReg8(self.device, self.REGISTERS['CHECK'] + 1) if data[0] != 0xFF or data[1] != 0x00: raise Error('SX1509 not detected!') self.reset(False)
def setup(): # use Gpio pin numbering # w.wiringPiSetupGpio() w.wiringPiSetupSys() # use the non-root mode. Needs to export pins via gpio first! # polarity pins are outputs: for pin in pins_pol: w.pinMode(pin, 1) # defined as output w.digitalWrite(pin,0) # all output off # PWM pins: for pin in pins_pwm: w.softPwmCreate(pin,0,100) #define pin as pwm output # all magnets turned off magnet(0,0,0) magnet(1,0,0) magnet(2,0,0) magnet(3,0,0)
def setup(): wp.wiringPiSetupSys() for pd_pin in pd_pins: wp.pinMode(pd_pin, wp.GPIO.INPUT) atexit.register(cleanup) print('setup completed')
def __init__(self): rospy.init_node('rpi_gpio', log_level=rospy.DEBUG) # Pin numbers are the GPIO# value, not the literal pin number. # Setting direction here is necessary because wiringpi doesn't support setMode() at time of writing. self.directions = rospy.get_param("~directions", {}) # These states will be set to pins right before the node shuts down. self.shutdown_states = rospy.get_param("~shutdown_states", {}) # Set default states. self.states = rospy.get_param("~states", {}) # Cleanup when termniating the node rospy.on_shutdown(self.shutdown) # Overall loop rate: should be faster than fastest sensor rate self.rate = int(rospy.get_param("~rate", 50)) r = rospy.Rate(self.rate) for pin, direction in self.directions.items(): if not isinstance(pin, int): del self.directions[pin] pin = int(pin) self.directions[pin] = direction # May give the error: # bash: echo: write error: Device or resource busy # if the pin was left exported assert pin in RPI2_GPIO_PINS, 'Invalid pin: %s' % pin assert direction in (IN, OUT), 'Invalid direction for pin %s: %s' % (pin, direction) self.export_pin(pin, direction) wiringpi.wiringPiSetupSys() for pin, state in self.states.items(): if not isinstance(pin, int): del self.states[pin] pin = int(pin) self.states[pin] = state msg = DigitalWrite() msg.pin = int(pin) msg.state = state self._set_pin_handler(msg) for pin, state in self.shutdown_states.items(): if not isinstance(pin, int): del self.shutdown_states[pin] pin = int(pin) self.shutdown_states[pin] = state assert pin in RPI2_GPIO_PINS, 'Invalid pin: %s' % pin assert state in (0, 1), 'Invalid shutdown state for pin %s: %s' % (pin, state) # Define publishers and services. #self.get_pin_pub = rospy.Publisher('~get_pin', msgs.DigitalRead) self.pin_change_pub = rospy.Publisher('~pin_change', msgs.DigitalChange, queue_size=10) rospy.Service('~set_pin', DigitalWrite, self._set_pin_handler) # Start polling the sensors and base controller while not rospy.is_shutdown(): # Publish all sensor values on a single topic for convenience now = rospy.Time.now() #TODO: read all readable-pins and publish an event on change r.sleep()
def __init__(self, wrapper): GpioInterface.__init__(self, wrapper) self._pwmfreq = wrapper.PWM_FREQ # os.popen('gpio export 18 out') wpi.wiringPiSetupSys() # no sudo
#!/usr/bin/env python import time import wiringpi as w COUNT = 5 PIN = 13 w.wiringPiSetupSys() w.pinMode(PIN, 1) for x in xrange(COUNT): w.digitalWrite(PIN, 1) time.sleep(1) w.digitalWrite(PIN, 0) time.sleep(1)
def init(self): wiringpi.wiringPiSetupSys()
import asyncio import datetime import random import websockets import wiringpi import time import os import Adafruit_DHT #for DHT11 sensor #settingup the wiringPI wiringpi.wiringPiSetupSys() #putting 17th gpio to out mode os.system('gpio export 17 out') #making the selected pin high as default wiringpi.pinMode(17, 1) #for dht11 sensor start # Set sensor type : Options are DHT11,DHT22 or AM2302 sensor = Adafruit_DHT.DHT11 # Set GPIO sensor is connected to gpio = 27 #sensor end async def time(websocket, path): while True: #data=await websocket.recv() # Use read_retry method. This will retry up to 15 times to
import wiringpi as pi import time def displayNumber(n): "自然数nを二進数としてLEDに表示する." for j in range(len(pinLed)): pi.digitalWrite(pinLed[j], n & (1 << j)) pinLed = [2, 3, 4, 5] pinSw = 26 pi.wiringPiSetupSys() for pin in pinLed: pi.pinMode(pin, 1) pi.pinMode(pinSw, 0) counter = 1 lastState = False startTime = time.time() try: displayNumber(counter) while True: newState = pi.digitalRead(pinSw)
def setup(self): if not WiringPiSingleton.already_setup: WiringPiSingleton.already_setup = True wiringpi.wiringPiSetupSys()
import wiringpi import time import os pinR = 17 pinG = 18 pinB = 22 os.system('gpio export ' + str(pinR) + ' out') os.system('gpio export ' + str(pinG) + ' out') os.system('gpio export ' + str(pinB) + ' out') wiringpi.wiringPiSetupSys() wiringpi.pinMode(pinR,wiringpi.OUTPUT) wiringpi.pinMode(pinG,wiringpi.OUTPUT) wiringpi.pinMode(pinB,wiringpi.OUTPUT) while True: time.sleep(0.5) wiringpi.digitalWrite(pinR,1) time.sleep(0.5) wiringpi.digitalWrite(pinG,1) time.sleep(0.5) wiringpi.digitalWrite(pinB,1) time.sleep(0.5) wiringpi.digitalWrite(pinR,0) time.sleep(0.5) wiringpi.digitalWrite(pinG,0) time.sleep(0.5) wiringpi.digitalWrite(pinB,0)
#!/usr/bin/python import time import threading import irclib import wiringpi LED_PIN = 3 wiringpi.wiringPiSetupSys() # for unprivileged access to GPIO pins wiringpi.pinMode(LED_PIN, 1) # output def flash_LED(count=5, ontime=100, offtime=200): """Flash the LED count times, one for ontime ms, off for offtime ms""" for i in xrange(count): wiringpi.digitalWrite(LED_PIN, 1) time.sleep(ontime/1000.) wiringpi.digitalWrite(LED_PIN, 0) time.sleep(offtime/1000.) def on_pubmsg(c, e): if [i for i in e.arguments()[0] if i in bot.users.keys()]: th = threading.Timer(0, flash_LED) th.start() def on_action(c, e): if [i for i in e.arguments()[0] if i in bot.users.keys()]: th = threading.Timer(0, flash_LED) th.start()
elif num == 9: ret = [[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [1, 1, 1, 1, 1]] else: ret = [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]] return ret pin_X = [21, 16, 13, 11, 5] pin_Y = [26, 19, 20, 6, 12, 7, 8] wp.wiringPiSetupSys() for i in range(len(pin_X)): wp.pinMode(pin_X[i], wp.GPIO.OUTPUT) for i in range(len(pin_Y)): wp.pinMode(pin_Y[i], wp.GPIO.OUTPUT) for i in range(len(pin_X)): wp.digitalWrite(pin_X[i], wp.GPIO.OUTPUT) for i in range(len(pin_Y)): wp.digitalWrite(pin_Y[i], wp.GPIO.OUTPUT) count = 0