def main(): delay = 1E-2 # seconds # set up pins wiringpi.wiringPiSetup() for pin in pinsA: wiringpi.pinMode( pin, 1 ) for pin in pinsB: wiringpi.pinMode( pin, 1 ) for pin in pinsOut: wiringpi.pinMode( pin, 0 ) wiringpi.pinMode( isSignedPin, 1 ) wiringpi.pinMode( isSupportedPin, 0 ) numFails = testUnsigned(delay) numFails += testSigned(delay) # magic for coloured printing from https://pythonadventures.wordpress.com/2011/03/16/print-colored-text-in-terminal/ colorred = "\033[01;31m{0}\033[00m" colorgrn = "\033[1;36m{0}\033[00m" if numFails > 0: print colorred.format("%d tests were failed" % numFails) else: print colorgrn.format("All tests passed")
def scanQ(self): # steps (1) and (2) before reading GPIOs self.__preRead() # (3) scan rows for pushed key/button rowHi=1 while rowHi==1: for i in range(len(self.row)): tmpRead=wiringpi.digitalRead(self.row[i]) if tmpRead==0: rowHi=0 rowVal=i # (4) after finding which key/button from the row scans, convert columns to input for j in range(len(self.col)): wiringpi.pinMode(self.col[j],INPUT) # (5) switch the i-th row found from scan to output wiringpi.pinMode(self.row[rowVal],OUTPUT) wiringpi.digitalWrite(self.row[rowVal],HIGH) # (6) scan columns for still-pushed key/button colLo=0 while colLo==0: for j in range(len(self.col)): tmpRead=wiringpi.digitalRead(self.col[j]) if tmpRead==1: colLo=1 colVal=j # reinitialize used GPIOs self.__postRead() # (7) return the symbol of pressed key from keyPad mapping return self.keyPad[rowVal][colVal]
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 configureHardware(postMotionEvent, postButtonClickEvent): wiringpi.wiringPiSetupGpio() # PIR motion detector def reportMotion(): if wiringpi.digitalRead(BCM_PIR) == 1: postMotionEvent() wiringpi.pinMode(BCM_PIR, wiringpi.GPIO.INPUT) wiringpi.pullUpDnControl(BCM_PIR, wiringpi.GPIO.PUD_OFF) wiringpi.wiringPiISR(BCM_PIR, wiringpi.GPIO.INT_EDGE_BOTH, reportMotion) # Display buttons for btnNum, bcmPin in ( (1, BCM_BTN_1), (2, BCM_BTN_2), (3, BCM_BTN_3), ): @debounce(0.02) def reportClick(_bcmPin=bcmPin, _btnNum=btnNum): if wiringpi.digitalRead(_bcmPin) == 0: postButtonClickEvent(_btnNum) wiringpi.pinMode(bcmPin, wiringpi.GPIO.INPUT) wiringpi.pullUpDnControl(bcmPin, wiringpi.GPIO.PUD_UP) wiringpi.wiringPiISR(bcmPin, wiringpi.GPIO.INT_EDGE_BOTH, reportClick) # Servos wiringpi.pinMode(BCM_SERVO_ACTIVATION, wiringpi.GPIO.OUTPUT) wiringpi.pinMode(BCM_SERVO_SPRING, wiringpi.GPIO.OUTPUT) wiringpi.pinMode(BCM_SERVO_HOLDER, wiringpi.GPIO.OUTPUT)
def __init__(self, dev=(0,0),speed=5000000, brightness=256, contrast=CONTRAST): self.spi = spidev.SpiDev() self.speed = speed self.dev = dev self.spi.open(self.dev[0],self.dev[1]) self.spi.max_speed_hz=self.speed # Set pin directions. self.dc = DC self.rst = RST wiringpi.wiringPiSetup() for pin in [self.dc, self.rst]: # pin=self.dc wiringpi.pinMode(pin, 1) self.contrast=contrast self.brightness=brightness # Toggle RST low to reset. wiringpi.digitalWrite(self.rst, OFF) time.sleep(0.100) wiringpi.digitalWrite(self.rst, ON) # Extended mode, bias, vop, basic mode, non-inverted display. wiringpi.digitalWrite(self.dc, OFF) self.spi.writebytes([0x21, 0x14, self.contrast, 0x20, 0x0c])
def main(): #all needs to run automatically port0 = '/dev/ttyUSB0' port1 = '/dev/ttyUSB1' port2 = '/dev/ttyUSB2' ## th_cl1 = TestThread(port1, './original_data/data1208.csv') th_cl1 = TestThread(port1, './original_data/data1208.csv') #th_cl1.start() #th_cl2.start() f = open('./original_data/data1209.csv', 'w') ## servo_pin = 18 param = sys.argv set_degree = int(param[1]) print(set_degree) wiringpi.wiringPiSetupGpio() wiringpi.pinMode(servo_pin, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetRange(1024) wiringpi.pwmSetClock(375) while True: try: ser = serial.Serial(port0, 38400, timeout=10) # 10s-timeout baudrate=38400 ser.write(b'<RM,>??\r\n') # send command (CR+LF) line = ser.readline() line = str(datetime.datetime.now()) + str(",") + line.decode('utf-8') line = line.split(',') velocity = float(line[2]) temp = float(line[5]) print('CPU_temperature' + '/' + line[0] + ',' + line[2] + ',' + line[3] + ',' + line[4] + ',' + line[5]) f.write(line[0] + ',' + line[2] + ',' + line[3] + ',' + line[4] + ',' + line[5] + "\n") if temp >= 35.0: # print('too hot') wiringpi.pwmWrite(servo_pin, set_degree + 5) # LED(11, True) else: # print('oh.. cool') # move_deg = int (81+41/90*set_degree) wiringpi.pwmWrite(servo_pin, set_degree) # LED(11, False) time.sleep(5) except IndexError: pass
def __init__(self): threading.Thread.__init__(self) self.watchdog = Watchdog() self.watchdog.daemon = True self.watchdog.start() time.sleep(1) self.setup = settings.setup self.quit = False for i in range(0, 7): #Set GPIO pins to output wp.pinMode(i, 1) self.setup = settings.setup self.dutycycles = [0, 0, 0, 0, 0, 0] channels = ['1', '2', '3', '4', '5', '6'] # Setup up extra status for the diode relay status diode_channels = ['diode' + number for number in channels] self.diode_channel_last = {name: None for name in diode_channels} # Setup sockets self.livesocket = LiveSocket(self.setup + '-bakeout', channels + diode_channels) self.livesocket.start() self.pullsocket = DateDataPullSocket(self.setup + '-bakeout', channels, timeouts=None) self.pullsocket.start() self.pushsocket = DataPushSocket(self.setup + '-push_control', action='enqueue') self.pushsocket.start()
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 __init__(self, pin, name, pwmCountUpFrequency, pwmCycleRange): # このservoが接続されるGPIO番号 # GPIO12またはGPIO18のみサポートされる(GPIO12と13は同じ内容、GPIO18と19は同じ内容となる) self.pin = pin self.name = name self.pwmCountUpFrequency = pwmCountUpFrequency self.pwmCycleRange = pwmCycleRange # ピン設定 wiringpi.pinMode(pin, wiringpi.PWM_OUTPUT) # see: http://qiita.com/locatw/items/f15fd9df40153bbb4d27 # PWMのカウンタ周期:19.2[MHz]/clock=19.2∗10^6/400=48[KHz]≒0.0208[ms] self.pwmFrequency = float(SG90Direct.PRI_PWM_BASE_CLOCK_FREQUENCY / self.pwmCountUpFrequency) # PWM周波数:19.2[MHz]/clock/range=19.2∗106/400/1024=46.875[Hz]≒21.3[ms] self.pwmCycleSec = 1.0 / self.pwmFrequency * self.pwmCycleRange # 48[KHz]∗0.5[ms]=48000[Hz]∗0.0005[s]=24 self.minPmwValue = int(self.pwmFrequency * SG90Direct.MIN_ANGLE_SEC) # 48[KHz]∗2.4[ms]=48000[Hz]∗0.0024[s]=115.2≒115 self.maxPmwValue = int(self.pwmFrequency * SG90Direct.MAX_ANGLE_SEC) self.deltaPmwValue = self.maxPmwValue - self.minPmwValue print 'pin: ' + str(self.pin) + ', pwmCountUpFrequency: ' + str(self.pwmCountUpFrequency) + 'Hz, pwmCycleRange: ' + str(self.pwmCycleRange) print 'pwmFrequency: ' + ('%.3f' % self.pwmFrequency) + ' Hz(' + ('%.6f' % (1.0 / self.pwmFrequency)) + ' sec), pwmCycleSec: ' + ('%.6f' % self.pwmCycleSec) print 'reference pwmCycleSec: ' + str(SG90Direct.REFERENCE_PWM_1CYCLE_SEC) print 'minPmwValue: ' + str(self.minPmwValue) + ', maxPmwValue: ' + str(self.maxPmwValue) + ', deltaPmwValue: ' + str(self.deltaPmwValue)
def main(): if wp.wiringPiSetup() == -1: print ("Unable to start wiringPi") sys.exit(1) if wp.wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1: print ("wiringPiSPISetup Failed") sys.exit(1) wp.pinMode(CS_MCP3208, wp.OUTPUT) while 1: t = time.time() vt = read_adc(0) R = (10000*vt)/(5-vt) vt = 5*R/(R+10000) temp = -0.3167*(vt**6) + 4.5437*(vt**5) - 24.916*(vt**4) + 63.398*(vt**3) - 67.737*vt*vt - 13.24*vt + 98.432 vh = read_adc(1) humidity = (((vh/5.0)-0.16)/0.0062)/(1.0546-0.00216*temp) print('{} Temp={}C, Humid={}%'.format(t, temp, humidity)) data = 'rasptest temp={},hum={} {:d}'.format(temp, humidity, int(t * (10**9))) print("Send data to DB") r = requests.post('http://192.168.1.231:8086/write', auth=('mydb', 'O7Bf3CkiaK6Ou8eqYttU'), params={'db': 'mydb'}, data=data) print("Return status: {}", r.status_code) time.sleep(30)
def fan(self, status, waitTime=0): time.sleep(waitTime) if status: wiringpi.pinMode(PIN_FAN, ON) else: wiringpi.pinMode(PIN_FAN, OFF)
def ensure_button_setup(): import wiringpi ensure_wiringpi_setup() for button_pin in button_pins: wiringpi.pinMode(button_pin, wiringpi.INPUT) wiringpi.wiringPiISR(button_pin, wiringpi.INT_EDGE_BOTH, GPIO_callback)
def triggerRemote(): if PI_SETUP: wiringpi.pinMode(CAMERA_PIN, 1) wiringpi.digitalWrite(CAMERA_PIN, 1) utilities.wait(0.1) wiringpi.digitalWrite(CAMERA_PIN, 0) else: print "Can't trigger remote, not pi"
def __init__(self, config): import wiringpi Unlocker.__init__(self, config) # PIN numbers follow P1 header BCM GPIO numbering, see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ # Local copy of the P1 in repo mapping see gpio_vs_wiringpi_numbering_scheme.png. wiringpi.wiringPiSetupGpio() self.lockPin = self.config.getint("UnlockerWiringPi", "lock_pin") wiringpi.pinMode(self.lockPin, wiringpi.OUTPUT) #output
def __init__(self): wiringpi.wiringPiSetupGpio() wiringpi.pinMode(self.PIN_PWM, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) wiringpi.pwmSetClock(375) wiringpi.pwmWrite(self.PIN_PWM, self.FRONT)
def __init__(self, pt, pe): threading.Thread.__init__(self) Observable.__init__(self) self.pinTrigger = pt self.pinEcho = pe if not Globals.globSimulate: wiringpi.pinMode(self.pinTrigger, GPIO_OUTPUT) wiringpi.pinMode(self.pinEcho, GPIO_INPUT)
def __preRead(self): # (1) set all columns as output low for j in range(len(self.col)): wiringpi.pinMode(self.col[j],OUTPUT) wiringpi.digitalWrite(self.col[j],LOW) # (2) set all rows as input for i in range(len(self.row)): wiringpi.pinMode(self.row[i],INPUT)
def initOS(): os.environ['SDL_FBDEV'] = '/dev/fb1' os.environ['SDL_MOUSEDEV'] = '/dev/input/touchscreen' os.environ['SDL_MOUSEDRV'] = 'TSLIB' os.environ['SDL_VIDEODRIVER'] = 'fbcon' wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18, 2) wiringpi.pwmSetMode(wiringpi.PWM_MODE_MS) # default balanced mode makes screen dark at about 853/1024 wiringpi.pwmWrite(18, 1024)
def hot_wire(enable): if enable: logging.info('Heating wire enabled Temp={0:0.1f}* < Temp_min={1:0.1f}'.format(temperature, TEMP_MIN)) wiringpi.pinMode(PIN_WIRE, ON) logging.debug(wiringpi.digitalRead(PIN_WIRE)) else: logging.info('Heating wire disabled Temp={0:0.1f}* > Temp_max={1:0.1f}'.format(temperature, TEMP_MAX)) wiringpi.pinMode(PIN_WIRE, OFF) logging.debug(wiringpi.digitalRead(PIN_WIRE)) return;
def is_enabled(self, button): if not WIRING_PI: return False pin = self.button_pin[button] wiringpi.pinMode(pin, 1) state = wiringpi.digitalRead(pin) return state == 1
def wakeup(self): if self.isFirst: # setup, if first self.isFirst = False wiringpi.wiringPiSetupGpio() wiringpi.pinMode(self.pin, wiringpi.OUTPUT) self.quit() self.ledThread = LedThread(self.pin) self.ledThread.startRun() self.ledThread.startBlink()
def __init__(self, power_calculator, datasocket): threading.Thread.__init__(self) self.pinnumber = 0 self.pc = power_calculator self.datasocket = datasocket self.beatperiod = 5 # seconds self.beatsteps = 100 self.dutycycle = 0 self.quit = False wp.pinMode(self.pinnumber, 1) # Set pin 0 to output
def setup(): # Set pin directions. wiringpi.wiringPiSetup() for pin in [DC, RST]: wiringpi.pinMode(pin, ON) wiringpi.pinMode(LED,2) wiringpi.pwmWrite(LED,128) spi.open(0,0) spi.max_speed_hz=5000000
def disable(self, button): if not WIRING_PI: logger.warn("Disabled but no GPIO initialized") return False pin = self.button_pin[button] wiringpi.pinMode(pin, 1) wiringpi.digitalWrite(pin, 0) return True
def wakeup(self): self.isRun = True if self.isFirst: # setup, if first self.isFirst = False wiringpi.wiringPiSetupGpio() wiringpi.pinMode(self.pinLed, wiringpi.OUTPUT) wiringpi.pinMode(self.pinButton, wiringpi.INPUT) self.quitLed() self.ledThread = LedThread(self.pinLed) self.ledThread.startRun()
def __init__(self): threading.Thread.__init__(self) wp.pinMode(0, 1) self.timer = time.time() self.cycle_time = settings.safety_cycle_time self.safety_margin = settings.safety_margin self.watchdog_safe = True self.quit = False self.time_to_live = 0 self.reactivate()#Initially activate Watchdog self.reset_ttl()
def work(): pin = 17 wiringpi.pinMode(pin,wiringpi.GPIO.OUTPUT) try: for i in range (0,3): wiringpi.digitalWrite(pin,wiringpi.GPIO.HIGH) wiringpi.delay(500) wiringpi.digitalWrite(pin,wiringpi.GPIO.LOW) wiringpi.delay(500) finally: wiringpi.digitalWrite(pin,wiringpi.GPIO.LOW)
def run_wiringpi(args): import wiringpi global debug, last_time wiringpi.wiringPiSetupGpio() wiringpi.pinMode(args.pinout, 1) while True: last_time = "%.9f" % shared.time() wiringpi.digitalWrite(args.pinout, 1) time.sleep(0.00001) wiringpi.digitalWrite(args.pinout, 0) if debug: print(last_time) time.sleep(args.interval)
def calibrateServo(conn): print 'calibrateServo' # Set up the servo wiringpi.wiringPiSetupGpio() wiringpi.pinMode(19,1) wiringpi.digitalWrite(19,0) cal = [0,0] min1 = [] min2 = [] period_time = 0.02 # This pulse time puts the servo at 0 degrees pulse_time = 0.00037 for i in range(1,100): wiringpi.digitalWrite(19,1) sleep(pulse_time) wiringpi.digitalWrite(19,0) sleep(period_time-pulse_time) min1.append(readServo(conn)) # This pulse time puts teh servo at 180 degrees pulse_time = 0.0023 for i in range(101,200): wiringpi.digitalWrite(19,1) sleep(pulse_time) wiringpi.digitalWrite(19,0) sleep(period_time-pulse_time) min2.append(readServo(conn)) # This pulse time puts the servo at 0 degrees pulse_time = 0.00037 for i in range(1,100): wiringpi.digitalWrite(19,1) sleep(pulse_time) wiringpi.digitalWrite(19,0) sleep(period_time-pulse_time) min1.append(readServo(conn)) # This pulse time puts teh servo at 180 degrees pulse_time = 0.0023 for i in range(101,200): wiringpi.digitalWrite(19,1) sleep(pulse_time) wiringpi.digitalWrite(19,0) sleep(period_time-pulse_time) min2.append(readServo(conn)) cal[0] = numpy.mean(min1) cal[1] = numpy.mean(min2) print cal return cal
def __init__(self, valves, pullsocket, pushsocket): threading.Thread.__init__(self) wp.wiringPiSetup() time.sleep(1) for index in range(0, 21): # Set GPIO pins to output wp.pinMode(index, 1) wp.digitalWrite(index, 0) # Now that all output are low, we can open main safety output wp.digitalWrite(20, 1) self.pullsocket = pullsocket self.pushsocket = pushsocket self.running = True self.valves = valves
import time import wiringpi as pi NICHROME_PIN = 23 SWITCH_PIN = 24 TIME = 5 pi.wiringPiSetupGpio() pi.pinMode(NICHROME_PIN, pi.OUTPUT) pi.pinMode(SWITCH_PIN, pi.INPUT) pi.digitalWrite(NICHROME_PIN, pi.LOW) while True: if (pi.digitalRead(SWITCH_PIN) == 0): pi.digitalWrite(NICHROME_PIN, pi.HIGH) time.sleep(TIME) pi.digitalWrite(NICHROME_PIN, pi.LOW) break elif (pi.digitalRead(SWITCH_PIN) == 1): pass
def StartGPIO(): wp.wiringPiSetupPhys() wp.pinMode(CsPin, wp.OUTPUT) wp.pinMode(DrdyPin, wp.INPUT) wp.pinMode(ResetPin, wp.OUTPUT) wp.digitalWrite(CsPin, wp.HIGH) wp.digitalWrite(ResetPin, wp.HIGH) wp.pinMode(SPICS, wp.OUTPUT) wp.pinMode(RES11PIN, wp.OUTPUT) wp.pinMode(RES12PIN, wp.OUTPUT) wp.pinMode(RES21PIN, wp.OUTPUT) wp.pinMode(RES22PIN, wp.OUTPUT)
MISO->DOUT gpio 21 MOSI->DIN gpio 19 CE0->CS gpio 24 """ Motor1PWM = 1 # gpio pin 12 = wiringpi no. 1 (BCM 18) Motor1AIN1 = 4 # gpio pin 16 = wiringpi no. 4 (BCM 23) Motor1AIN2 = 5 # gpio pin 18 = wiringpi no. 5 (BCM 24) MotorStandby = 6 # gpio pin 22 = wiringpi no. 6 (BCM 25) Motor2PWM = 23 # gpio pin 33 = wiringpi no. 23 (BCM 13) Motor2BIN1 = 21 # gpio pin 29 = wiringpi no. 21 (BCM 5) Motor2BIN2 = 22 # gpio pin 31 = wiringpi no. 22 (BCM 6) # Initialize PWM output wiringpi.wiringPiSetup() wiringpi.pinMode(Motor1PWM, 2) # PWM mode wiringpi.pinMode(Motor1AIN1, 1) #Digital out mode wiringpi.pinMode(Motor1AIN2, 1) #Digital out mode wiringpi.pinMode(MotorStandby, 1) #Ditial out mode wiringpi.pinMode(Motor2PWM, 2) # PWM mode wiringpi.pinMode(Motor2BIN1, 1) #Digital out mode wiringpi.pinMode(Motor2BIN2, 1) #Digital out mode wiringpi.pwmWrite(Motor1PWM, 0) # OFF wiringpi.pwmWrite(Motor2PWM, 0) # OFF wiringpi.digitalWrite(Motor1AIN1, 1) #forward mode wiringpi.digitalWrite(Motor1AIN2, 0) #forward mode wiringpi.digitalWrite(Motor2BIN1, 1) wiringpi.digitalWrite(Motor2BIN2, 0)
def setPinConfig(self, EN, INA, INB): wiringpi.pinMode(EN, self.OUTPUT) wiringpi.pinMode(INA, self.OUTPUT) wiringpi.pinMode(INB, self.OUTPUT) wiringpi.softPwmCreate(EN, 0, 255)
import wiringpi print "Testing..." wiringpi.delay(1000) print "..1.." wiringpi.delay(1000) print "..2.." wiringpi.delay(1000) print "...3!" print "Testing under progress" OUTPUT = 2 SERVO_G4 = 4 print "Setting up wiringPi" wiringpi.wiringPiSetup() print "Setting PWM Output on G4" wiringpi.pinMode(4, 2) wiringpi.softPwmCreate(SERVO_G4, 0, 100) print "Going through PWM ranges" for r in range(0, 100): wiringpi.softPwmWrite(SERVO_G4, r) wiringpi.delay(100)
pinred = 6 pinblue = 12 pingreen = 5 red = [255, 0, 0] orange = [255, 127, 0] yellow = [255, 255, 0] white = [255, 255, 255] green = [0, 255, 0] blue = [0, 0, 255] i = [75, 0, 130] v = [159, 0, 255] black = [0, 0, 0] wiringpi.wiringPiSetupGpio() wiringpi.pinMode(pinred, OUTPUT) # as output wiringpi.softPwmCreate( pinred, 0, 255) # Setup PWM using Pin, Initial Value and Range parameters wiringpi.pinMode(pingreen, OUTPUT) # as output wiringpi.softPwmCreate( pingreen, 0, 255) # Setup PWM using Pin, Initial Value and Range parameters wiringpi.pinMode(pinblue, OUTPUT) # as output wiringpi.softPwmCreate( pinblue, 0, 255) # Setup PWM using Pin, Initial Value and Range parameters color = black if sys.argv[1]: farbe = str(sys.argv[1]) brightness = int(100) try:
# -*- coding: utf-8 -*- import wiringpi as pi import time # pin: 11, 12, 15, 16 # BCM: 17, 18, 22, 23 front_l = 17 front_r = 18 back_l = 22 back_r = 23 pi.wiringPiSetupGpio() pi.pinMode(front_l, pi.OUTPUT) pi.pinMode(front_r, pi.OUTPUT) pi.pinMode(back_l, pi.OUTPUT) pi.pinMode(back_r, pi.OUTPUT) pi.softPwmCreate(front_l, 0, 100) pi.softPwmWrite(front_l, 0) pi.softPwmCreate(front_r, 0, 100) pi.softPwmWrite(front_r, 0) pi.softPwmCreate(back_l, 0, 100) pi.softPwmWrite(back_l, 0) pi.softPwmCreate(back_r, 0, 100) pi.softPwmWrite(back_r, 0) try: while True:
# MIN = 50 # MIN = 100 MIN = 63 # MAX = 250 # MAX = 200 MAX = 237 #DELAY_PERIOD = 0.01 #DELAY_PERIOD = 0.005 DELAY_PERIOD = 0.01 # use 'GPIO naming' wiringpi.wiringPiSetupGpio() # set #18 to be a PWM output wiringpi.pinMode(18, wiringpi.GPIO.PWM_OUTPUT) # set the PWM mode to milliseconds stype wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) # divide down clock wiringpi.pwmSetClock(192) wiringpi.pwmSetRange(2000) for pulse in range(MAX, MIN, -1): wiringpi.pwmWrite(18, pulse) time.sleep(DELAY_PERIOD) for pulse in range(MIN, MAX, 1): wiringpi.pwmWrite(18, pulse) time.sleep(DELAY_PERIOD)
#R2 = pygame.mixer.Sound('/home/pi/Desktop/TheEye/sounds/r2d2.ogg') #blip1 = pygame.mixer.Sound('/home/pi/Desktop/TheEye/sounds/blip1.ogg') #blip2 = pygame.mixer.Sound('/home/pi/Desktop/TheEye/sounds/blip2.ogg') cv2.namedWindow('Video', cv2.WINDOW_NORMAL) cv2.resizeWindow('Video', 1280, 960) wiringpi.wiringPiSetupGpio() # use 'GPIO naming' # Initialise the PWM device using the default address pwm = PCA9685(0x40) pan = 0 tilt = 1 LED = 16 wiringpi.pinMode(LED, 1) face = [ 0, 0, 0, 0 ] # This will hold the array that OpenCV returns when it finds a face: (makes a rectangle) Cface = [0, 0] lastface = 0 faceX, faceY, faceW, faceH = 0, 0, 0, 0 camangle = 25 panCP = 0 panDP = 0 tiltCP = 0 tiltDP = 0
def setPinConfig(EN, INA , INB): wiringpi.pinMode(EN, OUTPUT) wiringpi.pinMode(INA, OUTPUT) wiringpi.pinMode(INB, OUTPUT) wiringpi.softPwmCreate(EN, 0, 255)
import time import threading import pygame.mixer #from pygame.mixer import Sound import logTable import ctypes logTable.create_table() pygame.mixer.init(frequency=44800, size=-16, channels=2, buffer=614) GPIO_LED = 12 GPIO_SW = 26 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(GPIO_LED, 1) # sets GPIO 12 to output wiringpi.pinMode(GPIO_SW, 0) # sets GPIO 26 to input wiringpi.pullUpDnControl(GPIO_SW, wiringpi.PUD_DOWN) wiringpi.digitalWrite(GPIO_LED, 0) # sets port 26 to 0 (0V, on) log_file = "./LOG/log.txt" error_log = "./LOG/error_log.txt" status_log = "./LOG/status_log.txt" global counter counter1 = 0 counter2 = 0 counter3 = 0 counter4 = 0 user_id = 0
def releaseGPIO(): log.info('Releasing all GPIO Connections') 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.
global last_commit now = int(round(time.time() * 1000)) if ((now - last_commit) > db_commit_rate): thread = Thread(target=db.commitValues, args=(getElapsed(), value)) thread.start() last_commit = int(round(time.time() * 1000)) #time.sleep(10) print("Dropping db values..") db.dropValues() wiringpi.wiringPiSetup() wiringpi.pinMode(0, 1) wiringpi.pinMode(22, 1) wiringpi.pinMode(23, 1) wiringpi.pinMode(24, 1) wiringpi.pinMode(25, 1) wiringpi.pinMode(27, 1) in1 = 24 in2 = 23 en = 25 temp1 = 1 GPIO.setmode(GPIO.BCM) GPIO.setup(in1, GPIO.OUT) GPIO.setup(in2, GPIO.OUT) GPIO.setup(en, GPIO.OUT)
import time import wiringpi wiringpi.wiringPiSetup() chan_list = [31, 33, 35, 37, 32, 36, 38, 40] #chan_list = [6] time.sleep(1) for pin in chan_list: print pin wiringpi.pinMode(pin, 1) n = 10 while n > 0: n = n - 1 print pin, n wiringpi.digitalWrite(pin, 0) time.sleep(0.1) wiringpi.digitalWrite(pin, 1) time.sleep(0.1)
print(message) def call(message, channel): print(message) if message['text'] == "on": GPIO.output(4, True) sleep(1) if message['text'] == "off": GPIO.output(4, False) sleep(1) wiringpi.wiringPiSetupGpio() # Initialise wiringpi GPIO wiringpi.pinMode(18, 2) # Set up GPIO 18 to PWM mode wiringpi.pinMode(17, 1) # GPIO 17 to output wiringpi.digitalWrite(17, 0) # port 17 off for rotation one way wiringpi.pwmWrite(18, 0) # set pwm to zero initially def button_callback(channel): GPIO.output(4, True) sleep(1) def get_adc(adcChannel): # read SPI data from MCP3002 chip r = spi.xfer2( [1, (2 + adcChannel) << 6, 0]) # these two lines are explained in more detail at the bottom ret = ((r[1] & 31) << 6) + (r[2] >> 2)
elif number==9: ret = [1,1,1,1,0,1,1] else: ret = [0,0,0,0,0,0,0] return ret inv_time = 1 out_7seg1=[3,2,14,15,18,4,17] out_7seg2=[27,22,23,24,25,26,19] pi.wiringPiSetupGpio() for i in range(7): pi.pinMode(out_7seg1[i],0) pi.pinMode(out_7seg2[i],0) count = 0 while(True): output1 = seg_decoder(count%10) output2 = seg_decoder((int)(count/10)) for i in range(7): pi.pinMode(out_7seg1[i],output1[i]) pi.pinMode(out_7seg2[i],output2[i]) count += 1 if count==100:
import wiringpi as pi import time LED_PIN = 23 pi.wiringPiSetupGpio() pi.pinMode(LED_PIN, pi.OUTPUT) pi.softPwmCreate(LED_PIN, 0, 100) while True: value = 0 while (value < 100): pi.softPwmWrite(LED_PIN, value) time.sleep(0.1) value = value + 1 while (value > 0): pi.softPwmWrite(LED_PIN, value) time.sleep(0.1) value = value - 1
import wiringpi from time import sleep wiringpi.wiringPiSetupGpio() wiringpi.pinMode(17, 1) wiringpi.digitalWrite(17, 1) sleep(.5) wiringpi.digitalWrite(17, 0) sleep(.5) wiringpi.digitalWrite(17, 1) sleep(.5) wiringpi.digitalWrite(17, 0) sleep(.5) wiringpi.digitalWrite(17, 1)
# 第1引数 # gostraight : まっすぐ前進 # goleft : 左折 # goright : 右折 # back : まっすぐ後進 # backleft : 左へ後進 # backright : 右へ後進 # break : ブレーキ order = param[1] # GPIO端子の設定 motor1_pin = 23 motor2_pin = 24 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(motor1_pin, 1) wiringpi.pinMode(motor2_pin, 1) # カメラの初期化(必ず関数の外側で!!!) camera = PiCamera() camera.resolution = (320, 240) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(320, 240)) # ラップトップがあるかどうかのフラグ not_exist = True stop_item = 'notebook' thread1 = CameraThread() thread2 = MorterThread()
#skrypt odpalany po zbootowaniu się odroida, odroid czeka na przycisk żeby odpalić odpowiedni skrypt a ten #zamknąć import wiringpi import subprocess PIN_TO_SENSE = 6 #numer pinu def gpioCallback(): #print ("GPIO CALLBACK - called by buttonclick!") subprocess.call('sudo python3 /home/odroid/MainOdroid.py ', shell=True) wiringpi.wiringPiSetup() wiringpi.pinMode(PIN_TO_SENSE, 0) wiringpi.pullUpDnControl(PIN_TO_SENSE, wiringpi.PUD_UP) #podciaganie rezystorem do 3.3V #wiringpi.wiringPiISR(PIN_TO_SENSE, wiringpi.GPIO.INT_EDGE_FALLING, gpioCallback) #wersja na przerwaniu while True: if wiringpi.digitalRead(PIN_TO_SENSE) == 0: gpioCallback() break wiringpi.delay(100) #ms
def setup(): for pin in pins: wiringpi.pinMode((pin + 100), 1)
import wiringpi as wpi PIN_IN = 18 PIN_LED1, PIN_LED2 = 23, 24 old, val = -1, 0 state = False wpi.wiringPiSetupGpio() wpi.pinMode(PIN_IN, wpi.INPUT) wpi.pinMode(PIN_LED1, wpi.OUTPUT) wpi.pinMode(PIN_LED2, wpi.OUTPUT) wpi.digitalWrite(PIN_LED1, True) while True: val = wpi.digitalRead(PIN_IN) if old != val : print(val) old = val if val == 0: wpi.digitalWrite(PIN_LED1, state) wpi.digitalWrite(PIN_LED2, not state) state = not state
# Servo Control import time import wiringpi # set the PWM mode to milliseconds stype wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) PIN_TO_PWM = 24 PIN_TO_PWM2 = 23 wiringpi.wiringPiSetupGpio() OUTPUT = 1 val = 15 wiringpi.pinMode(PIN_TO_PWM,OUTPUT) wiringpi.softPwmCreate(PIN_TO_PWM,15,25) wiringpi.pinMode(PIN_TO_PWM2,OUTPUT) wiringpi.softPwmCreate(PIN_TO_PWM2,14,25) #wiringpi.softPwmWrite(PIN_TO_PWM, 15) #wiringpi.softPwmWrite(PIN_TO_PWM2, 15) writeval = 14 #wiringpi.softPwmWrite(PIN_TO_PWM2,writeval) wiringpi.delay(20) #wiringpi.softPwmWrite(PIN_TO_PWM2,writeval) wiringpi.delay(20) # set the PWM mode to milliseconds stype while True: try:
#169.254.189.221 # -*- coding: utf-8 -*- import time import wiringpi as pi motor1_pin = 23 motor2_pin = 24 SW_PIN = 4 SW2_PIN = 10 pi.wiringPiSetupGpio() pi.pinMode(SW_PIN, pi.INPUT) pi.pinMode(motor1_pin, pi.OUTPUT) pi.pinMode(motor2_pin, pi.OUTPUT) pi.softPwmCreate(motor1_pin, 0, 100) pi.softPwmCreate(motor2_pin, 0, 100) pi.softPwmWrite(motor1_pin, 0) pi.softPwmWrite(motor2_pin, 0) max_speed = 30 max2_speed = max_speed - 1 def go_up(): print("go_start") pi.softPwmWrite(motor1_pin, 0) pi.softPwmWrite(motor2_pin, 0)
##from pins import * import time ##GPIO.setmode(GPIO.BCM) import wiringpi pinBase = 65 i2cAddr = 0x20 wiringpi.wiringPiSetup() wiringpi.mcp23017Setup(pinBase, i2cAddr) wiringpi.pinMode(78, 1) wiringpi.pinMode(79, 0) wiringpi.digitalWrite(79, 0) GPIO_TRIGGER = 78 GPIO_ECHO = 79 def measure(): # This function measures a distance stop = time.time() wiringpi.digitalWrite(GPIO_TRIGGER, 1) time.sleep(0.00001) wiringpi.digitalWrite(GPIO_TRIGGER, 0) start = time.time() while wiringpi.digitalRead(GPIO_ECHO) == 0: start = time.time() while wiringpi.digitalRead(GPIO_ECHO) == 1: stop = time.time()
hs = time.strftime("%H:%M:%S") # Hora completa para registro de Log h = int(time.strftime('%H')) data = time.strftime('%d/%m/%y') os.system("amixer set PCM 100%") # ajusta o volume master bus = smbus.SMBus(1) # esta linha especifica qual dispositivo I2C a ser usado. 1 significa que o dispositivo I2C está localizado em / dev / I2C-1 out_A = 0b11111111 # Todos reles em 0 wiringpi.wiringPiSetup() # configura os 3 PCF8574 da placa de expansao wiringpi.pcf8574Setup(100,0x25) # Definindo os pinos do rele como saida for x in range (0, 9): wiringpi.pinMode(100+x,1) wiringpi.digitalWrite(100+x,1) # Inicia com o rele desligado wiringpi.pcf8574Setup(200,0x26) # Definindo os pinos de saida digital como saida for x in range (0, 9): wiringpi.pinMode(200+x,1) wiringpi.digitalWrite(200+x,1) wiringpi.pcf8574Setup(300, 0x27); # # Definindo os pinos de entrada digital como entradas def timeout(signum, frame): print("Excedeu o tempo esperado",signum)
data = load(f) MQTT_AUTH = { 'username': data['mqtt_username'], 'password': data['mqtt_password'] } if __name__ == "__main__": port = data['gpio_touchpad_port'] logging.info('setup GPIO%02d as input port', port) wiringpi.wiringPiSetupSys() if not os.path.exists('/sys/class/gpio/gpio{:d}'.format(port)): logging.warning('setup GPIO with wiringpi failed') with open('/sys/class/gpio/export', 'w') as f: f.write('{:d}\n'.format(port)) wiringpi.pinMode(port, wiringpi.INPUT) logging.info('enter polling') while True: if wiringpi.digitalRead(port) == wiringpi.HIGH: logging.info('detect rising edge') publish.single('hass/rpi/touchpad', hostname=data['mqtt_broker'], port=data['mqtt_port'], auth=MQTT_AUTH) sleep(0.5) while wiringpi.digitalRead(port) == wiringpi.HIGH: sleep(0.1) logging.info('detect falling edge') sleep(0.1)
# # 02_led_wiringpi_softpwm.py # import wiringpi LED_PORT = 18 PWM_RANGE = 100 # Software PWM wiringpi.wiringPiSetupGpio() wiringpi.pinMode(LED_PORT, wiringpi.GPIO.PWM_OUTPUT) wiringpi.softPwmCreate(LED_PORT, 0, PWM_RANGE) # LEDを2秒間点灯する for duty in (5, 20, 50, 80, 100): print(duty) wiringpi.softPwmWrite(LED_PORT, duty) wiringpi.delay(2000) wiringpi.softPwmStop(LED_PORT) print('done')
# define shift reg pins DATA = 6 LATCH = 5 CLK = 4 OUTPUT = 1 LOW = 0 HIGH = 1 # common anode digital tube 16 BCD code LED_BCD = [ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e ] wiringpi.pinMode(DATA, OUTPUT) wiringpi.pinMode(LATCH, OUTPUT) wiringpi.pinMode(CLK, OUTPUT) # initialization print("Initialization...") wiringpi.digitalWrite(LATCH, LOW) wiringpi.digitalWrite(CLK, LOW) def LED_display(LED_number, LED_display, LED_dp): hc_ledcode_temp = 0 if LED_display > 15: LED_display = 0
def reset_ports(): # resets the ports for a safe exit wiringpi.pwmWrite(18, 0) # set pwm to zero wiringpi.digitalWrite(18, 0) # ports 17 & 18 off wiringpi.digitalWrite(17, 0) wiringpi.pinMode(17, 0) # set ports back to input mode wiringpi.pinMode(18, 0)