def handle(self): global isRunning request, socket = self.request # Read who spoke to us and what they said request = request.upper() # Convert command to upper case driveCommands = request.split( ',') # Separate the command into individual drives print 'abc: %s' % (driveCommands) if len(driveCommands) == 1: # Special commands if request == 'ALLOFF': # Turn all drives off MotorOff() print 'All drives off' elif request == 'EXIT': # Exit the program isRunning = False else: # Unknown command print 'Special command "%s" not recognised' % (request) elif len(driveCommands) == 2: leftVal = driveCommands[0] rightVal = driveCommands[1] ileftVal = int(leftVal) irightVal = int(rightVal) print ileftVal print irightVal wiringpi.softPwmWrite(DRIVE_2, ileftVal) wiringpi.softPwmWrite(DRIVE_3, irightVal) else: # Did not get the right number of drive commands print 'Command "%s" did not have %d parts!' % (request, len(lDrives))
def move(turn, throttle): throttle /= 2 l_power = throttle r_power = throttle if (throttle < 0): gpio.digitalWrite(22,1) gpio.digitalWrite(23,0) l_power = (throttle * -1) r_power = (throttle * -1) else: gpio.digitalWrite(22,0) gpio.digitalWrite(23,1) if (turn < 0): l_power = throttle r_power = throttle - (turn * -1) elif turn > 0: l_power = throttle - turn r_power = throttle gpio.softPwmWrite(18, l_power) gpio.softPwmWrite(17, r_power)
def fade_down(pin, step): """PWM ramp down to zero duty""" logging.debug("Down") for i in reversed(range(0, PWM_COUNT, step)): wiringpi.softPwmWrite(pin, i) IO.delay(12) IO.digitalWrite(pin, IO.LOW)
def fade_up(pin, step): """PWM ramp up to full duty""" logging.debug("Up") for i in range(0, PWM_COUNT, step): wiringpi.softPwmWrite(pin, i) IO.delay(12) IO.digitalWrite(pin, IO.HIGH)
def turn_off_light(pin, use_overrides=False): """ Turn off the specified light Taking into account various overrides if specified. :param pin: index of pin in _GPIO_PINS :type pin: int :param use_overrides: should overrides be used :type use_overrides: bool """ if use_overrides: if is_pin_pwm[pin]: turn_on_light(pin, use_overrides, _PWM_OFF) else: if pin + 1 not in _ALWAYS_OFF_CHANNELS: if pin + 1 not in _INVERTED_CHANNELS: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOINACTIVE) else: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOACTIVE) else: if is_pin_pwm[pin]: wiringpi.softPwmWrite(_GPIO_PINS[pin], _PWM_OFF) else: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOINACTIVE)
def all_off(): for pin in pins_pol: w.digitalWrite(pin, 0) for pin in pins_pwm: w.softPwmWrite(pin,0) mag_value=[0,0,0,0]
def wiringPiMotorBackward(self,PWM): if self.getStatus() == "V": self.wiringPiMotorStop() if self.getStatus() != "A": print PWM wiringpi2.softPwmWrite(self.backward,int(PWM)) self.status = -1
def FlashDesk(): for i in range(0, 5): for j in range(0, 100, STEP): wiringpi2.softPwmWrite(PIN_TO_PWM, j) wiringpi2.delay(50) for j in range(100, 0, (STEP * -1)): wiringpi2.softPwmWrite(PIN_TO_PWM, j) wiringpi2.delay(50)
def rotate(self, direction, speed): if direction == 1: io1.digitalWrite(self.__d1_pin,0) io1.digitalWrite(self.__d2_pin,1) io1.softPwmWrite(self.__p_pin,int(speed)) if direction == -1: io1.digitalWrite(self.__d1_pin,1) io1.digitalWrite(self.__d2_pin,0) io1.softPwmWrite(self.__p_pin,int(speed))
def magnet(nummer, staerke, polaritaet): if not ((0 <= nummer <=3) and (0<= staerke <= 100) and (0<= polaritaet <=1)): #fehler print("magnet: falscher Parameter. Magnet=" + str(nummer) + ", Staerke=" + str(staerke) + ", Polaritaet=" + str(polaritaet)) return -1 w.softPwmWrite(pins_pwm[nummer],staerke) mag_value[nummer] = staerke w.digitalWrite(pins_pol[nummer],polaritaet) return 0
def setmotors(): io.digitalWrite(DRIVEA0, A0) io.digitalWrite(DRIVEA1, A1) io.digitalWrite(STANDBY, io.HIGH) io.digitalWrite(DRIVEB0, B0) io.digitalWrite(DRIVEB1, B1) wiringpi2.softPwmWrite(PWMA, (int)((math.fabs(LeftTrack) - threshold) * 80)) wiringpi2.softPwmWrite(PWMB, (int)((math.fabs(RightTrack) - threshold) * 80))
def process_change(self, color, value): value = int(value) pin = 0 if color == 'r': pin = red elif color == 'g': pin = green elif color == 'b': pin = blue if pin != 0 and value >= 0 and value <= 100: wpi.softPwmWrite(pin, value)
def __init__(self, pin=DEFAULT_PIN, calibration_file=None, smoothing=None, wiringpi_obj=None, zero_pin=None, **kwargs): super(PWMCalibrator, self).__init__() if not SOFTPWM_SUPPORT and ((pin != WIRINGPI_PWM_PIN) or zero_pin != None): raise Exception( "No soft PWM support (is wiringpi2 installed?). Only pin 1 may be used." ) self.pin = pin if wiringpi_obj is not None: self.wp = wiringpi_obj else: wiringpi.wiringPiSetup() # PWM: hardware or software? if (not SOFTPWM_SUPPORT) or (self.pin == WIRINGPI_PWM_PIN): wiringpi.pinMode(self.pin, wiringpi.GPIO.PWM_OUTPUT) self.pwm_write = wiringpi.pwmWrite self.pwm_max = PWM_MAX else: wiringpi.softPwmCreate(self.pin, 0, SOFT_PWM_MAX) self.pwm_write = wiringpi.softPwmWrite self.pwm_max = kwargs.get('soft_pwm_max', SOFT_PWM_MAX) # zero out any zero pins, as necessary # (this is used for bidirectional meters; by default the pi's pins # are in a high-impedence mode that will prevent adequate calibration) if zero_pin != None: if zero_pin == WIRINGPI_PWM_PIN: wiringpi.pinMode(zero_pin, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmWrite(zero_pin, 0) else: wiringpi.softPwmCreate(zero_pin, 0, SOFT_PWM_MAX) wiringpi.softPwmWrite(zero_pin, 0) self.pwm_value = 0 self.calibration = [] if smoothing is True: self.smoothing = 0.005 else: self.smoothing = smoothing if calibration_file is not None: self.calibration_file = calibration_file else: self.calibration_file = DEFAULT_CALIBRATION_FILENAME
def pol(nummer,polaritaet): if not ((0 <= nummer <=3) and (0<= polaritaet <=1)): print("magnet: falscher Parameter. Magnet=" + str(nummer) + ", Polaritaet=" + str(polaritaet)) return -1 w.softPwmWrite(pins_pwm[nummer],0) # magnet abschalten, bevor wir die Polaritaet umschalten sleep(.1) w.digitalWrite(pins_pol[nummer],polaritaet) sleep(.1) w.softPwmWrite(pins_pwm[nummer],mag_value[nummer])# magnet wieder auf vorherige Staerke schalten return 0
def mag_st(nummer, staerke): if staerke < 0: staerke = 0 if staerke > 100: staerke = 100 if not ((0 <= nummer <=3) and (0<= staerke <= 100)): #fehler print("mag_st: falscher Parameter. Magnet=" + str(nummer) + ", Staerke=" + str(staerke)) return -1 w.softPwmWrite(pins_pwm[nummer],staerke) mag_value[nummer] = staerke return 0
def camera(self, angle1, angle2): if angle1 < 0 or angle1 > 180 or angle2 < 0 or angle2 > 180: return False # TODO angle1 = int(127 + int(127 * float(angle1) / 180)) angle2 = int(127 + int(127 * float(angle2) / 180)) wiringpi2.pinMode(self.config.get("pin1_cam"), 1) wiringpi2.softPwmCreate(self.config.get("pin1_cam"), 0, 100) wiringpi2.softPwmWrite(self.config.get("pin1_cam"), angle1) wiringpi2.pinMode(self.config.get("pin2_cam"), 1) wiringpi2.softPwmCreate(self.config.get("pin2_cam"), 0, 100) wiringpi2.softPwmWrite(self.config.get("pin2_cam"), angle2) time.sleep(0.100)
def setColors(red, green, blue): global colors #Check if inputs are in valid range for c in (red, green, blue): if c < 0 or c > 255: return -1 colors = [red, green, blue] for (clr, pin) in zip(colors, colorPins): wiringpi2.softPwmWrite(pin, int(clr*100.0/255.0)) return (red*65536 + green*256 + blue)
def softPwm(): print "SoftPwm Mode" wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_PWM,OUTPUT) wiringpi2.softPwmCreate(PIN_TO_PWM,0,100) # Setup PWM using Pin, Initial Value and Range parameters for time in range(0,4): for brightness in range(0,100): # Going from 0 to 100 will give us full off to full on wiringpi2.softPwmWrite(PIN_TO_PWM,brightness) # Change PWM duty cycle wiringpi2.delay(10) # Delay for 0.2 seconds for brightness in reversed(range(0,100)): wiringpi2.softPwmWrite(PIN_TO_PWM,brightness) wiringpi2.delay(10)
def MoveForward(n): """Move forward for 'n' seconds""" wiringpi.softPwmWrite(leftDrive, leftDriveLevel) wiringpi.softPwmWrite(rightDrive, rightDriveLevel) time.sleep(n) wiringpi.softPwmWrite(leftDrive, 0) wiringpi.softPwmWrite(rightDrive, 0)
def setMotorContorl(PWM, INA, INB, speed, stat): #모터 속도 제어 PWM wiringpi.softPwmWrite(PWM, speed) #앞으로 if stat == FORWARD: wiringpi.digitalWrite(INA, HIGH) wiringpi.digitalWrite(INB, LOW) #뒤로 elif stat == BACKWORD: wiringpi.digitalWrite(INA, LOW) wiringpi.digitalWrite(INB, HIGH) #정지 elif stat == STOP: wiringpi.digitalWrite(INA, LOW) wiringpi.digitalWrite(INB, LOW)
def set_light(pin, use_overrides=False, brightness=1.0): """Set the brightness of the specified light Taking into account various overrides if specified. The default is full on (1.0) To turn a light off pass 0 for brightness If brightness is a float between 0 and 1.0 that level will be set. This function replaces turn_on_light and turn_off_light :param pin: index of pin in cm.hardware.gpio_pins :type pin: int :param use_overrides: should overrides be used :type use_overrides: bool :param brightness: float, a float representing the brightness of the lights :type brightness: float """ if math.isnan(brightness): brightness = 0.0 if _ACTIVE_LOW_MODE: brightness = 1.0 - brightness if use_overrides: if pin + 1 in always_off_channels: brightness = 0 elif pin + 1 in always_on_channels: brightness = 1 if pin + 1 in inverted_channels: brightness = 1 - brightness if not network.playing and server: network.broadcast( cm.hardware.gpio_pins.index(cm.hardware.gpio_pins[pin]), brightness) if is_pin_pwm[pin]: wiringpi.softPwmWrite(cm.hardware.gpio_pins[pin], int(brightness * _PWM_MAX)) else: wiringpi.digitalWrite(cm.hardware.gpio_pins[pin], int(brightness > 0.5))
def __init__(self, pin=DEFAULT_PIN, calibration_file=None, smoothing=None, wiringpi_obj=None, zero_pin=None, **kwargs): super(PWMCalibrator, self).__init__() if not SOFTPWM_SUPPORT and ((pin!=WIRINGPI_PWM_PIN) or zero_pin!=None): raise Exception("No soft PWM support (is wiringpi2 installed?). Only pin 1 may be used.") self.pin = pin if wiringpi_obj is not None: self.wp = wiringpi_obj else: wiringpi.wiringPiSetup() # PWM: hardware or software? if (not SOFTPWM_SUPPORT) or (self.pin==WIRINGPI_PWM_PIN): wiringpi.pinMode(self.pin, wiringpi.GPIO.PWM_OUTPUT) self.pwm_write = wiringpi.pwmWrite self.pwm_max = PWM_MAX else: wiringpi.softPwmCreate(self.pin, 0, SOFT_PWM_MAX) self.pwm_write = wiringpi.softPwmWrite self.pwm_max = kwargs.get('soft_pwm_max', SOFT_PWM_MAX) # zero out any zero pins, as necessary # (this is used for bidirectional meters; by default the pi's pins # are in a high-impedence mode that will prevent adequate calibration) if zero_pin!=None: if zero_pin==WIRINGPI_PWM_PIN: wiringpi.pinMode(zero_pin, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmWrite(zero_pin, 0) else: wiringpi.softPwmCreate(zero_pin, 0, SOFT_PWM_MAX) wiringpi.softPwmWrite(zero_pin, 0) self.pwm_value = 0 self.calibration = [] if smoothing is True: self.smoothing = 0.005 else: self.smoothing = smoothing if calibration_file is not None: self.calibration_file = calibration_file else: self.calibration_file = DEFAULT_CALIBRATION_FILENAME
def set_light(pin, use_overrides=False, brightness=1.0): """Set the brightness of the specified light Taking into account various overrides if specified. The default is full on (1.0) To turn a light off pass 0 for brightness If brightness is a float between 0 and 1.0 that level will be set. This function replaces turn_on_light and turn_off_light :param pin: index of pin in cm.hardware.gpio_pins :type pin: int :param use_overrides: should overrides be used :type use_overrides: bool :param brightness: float, a float representing the brightness of the lights :type brightness: float """ if math.isnan(brightness): brightness = 0.0 if _ACTIVE_LOW_MODE: brightness = 1.0 - brightness if use_overrides: if pin + 1 in always_off_channels: brightness = 0 elif pin + 1 in always_on_channels: brightness = 1 if pin + 1 in inverted_channels: brightness = 1 - brightness if not network.playing and server: network.broadcast(cm.hardware.gpio_pins.index(cm.hardware.gpio_pins[pin]), brightness) if is_pin_pwm[pin]: wiringpi.softPwmWrite(cm.hardware.gpio_pins[pin], int(brightness * _PWM_MAX)) else: wiringpi.digitalWrite(cm.hardware.gpio_pins[pin], int(brightness > 0.5))
def turn_on_light(pin, use_overrides=False, brightness=1.0): """Turn on the specified light Taking into account various overrides if specified. :param pin: index of pin in _GPIO_PINS :type pin: int :param use_overrides: should overrides be used :type use_overrides: bool :param brightness: float, a float representing the brightness of the lights :type brightness: float """ if is_pin_pwm[pin]: if math.isnan(brightness): brightness = 0.0 if _ACTIVE_LOW_MODE: brightness = 1.0 - brightness if brightness < 0.0: brightness = 0.0 if brightness > 1.0: brightness = 1.0 if use_overrides: if pin + 1 in _ALWAYS_OFF_CHANNELS: brightness = 0 elif pin + 1 in _ALWAYS_ON_CHANNELS: brightness = 1 if pin + 1 in _INVERTED_CHANNELS: brightness = 1 - brightness wiringpi.softPwmWrite(_GPIO_PINS[pin], int(brightness * _PWM_MAX)) return if use_overrides: if pin + 1 not in _ALWAYS_OFF_CHANNELS: if pin + 1 not in _INVERTED_CHANNELS: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOACTIVE) else: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOINACTIVE) else: wiringpi.digitalWrite(_GPIO_PINS[pin], _GPIOACTIVE)
def sliderMoved(self,widget,data): if(data == "red" and self.redIsOn == True): wiringpi2.softPwmWrite(self.redPin, int(self.redSlider.get_value())) elif(data == "blue" and self.blueIsOn == True): wiringpi2.softPwmWrite(self.bluePin, int(self.blueSlider.get_value())) elif(data == "green" and self.greenIsOn==True): wiringpi2.softPwmWrite(self.greenPin, int(self.greenSlider.get_value()))
def set(self, red, green, blue): self.red = red self.green = green self.blue = blue wiringpi2.softPwmWrite(self.r_pin, red) wiringpi2.softPwmWrite(self.g_pin, green) wiringpi2.softPwmWrite(self.b_pin, blue)
def telmin(): REF=3.3 # 5.p or 3.3 mode=1 w.wiringPiSetup() w.wiringPiSPISetup(0,1000000) w.pinMode(0,1) w.pinMode(2,1) w.pinMode(1,2) #w.softPwmCreate(1,0,100) w.softPwmCreate(2,0,100) w.digitalWrite(0,1) while 1: data1 = MCP3008(1) data7 = MCP3008(7) time.sleep(0.01) data11 = int(data1)-860; w.softPwmWrite(2,data11) data71 = int(((data7/20)*5.8)-225) w.pwmWrite(1,data71*10) print('%4.2f'%data11,'%4.2f'%data71,'%.2f'%(data1/1024*REF),"volt",'%.2f'%(data7/1024*REF),"volt")
def cleanup(self, color = None): wiringpi2.softPwmWrite(self.redPin,0) wiringpi2.pinMode(self.redPin,0) wiringpi2.softPwmWrite(self.greenPin,0) wiringpi2.pinMode(self.greenPin,0) wiringpi2.softPwmWrite(self.bluePin,0) wiringpi2.pinMode(self.bluePin,0) print "goodbye" return False
def levelSet(self,color,level): if(color == "red" and self.redIsOn == True): self.redLevel = int(level) wiringpi2.softPwmWrite(self.redPin, int(level)) elif(color == "blue" and self.blueIsOn == True): self.blueLevel = int(level) wiringpi2.softPwmWrite(self.bluePin, int(level)) elif(color == "green" and self.greenIsOn==True): self.blueLevel = int(level) wiringpi2.softPwmWrite(self.greenPin, int(level))
def cleanupHandler(self, widget, data = None): print "goodbye" wiringpi2.softPwmWrite(self.redPin,0) wiringpi2.pinMode(self.redPin,0) wiringpi2.softPwmWrite(self.greenPin,0) wiringpi2.pinMode(self.greenPin,0) wiringpi2.softPwmWrite(self.bluePin,0) wiringpi2.pinMode(self.bluePin,0) gtk.main_quit() return False
def sliderMoved(self, widget, data): if (data == "red" and self.redIsOn == True): wiringpi2.softPwmWrite(self.redPin, int(self.redSlider.get_value())) elif (data == "blue" and self.blueIsOn == True): wiringpi2.softPwmWrite(self.bluePin, int(self.blueSlider.get_value())) elif (data == "green" and self.greenIsOn == True): wiringpi2.softPwmWrite(self.greenPin, int(self.greenSlider.get_value()))
def cleanupHandler(self, widget, data=None): print "goodbye" wiringpi2.softPwmWrite(self.redPin, 0) wiringpi2.pinMode(self.redPin, 0) wiringpi2.softPwmWrite(self.greenPin, 0) wiringpi2.pinMode(self.greenPin, 0) wiringpi2.softPwmWrite(self.bluePin, 0) wiringpi2.pinMode(self.bluePin, 0) gtk.main_quit() return False
def fade(self, red, green, blue, delay=500, step=5): for i in range(0, delay, step): f = (0.0 + i) / delay r = self.red + (red - self.red) * f wiringpi2.softPwmWrite(self.r_pin, int(r)) g = self.green + (green - self.green) * f wiringpi2.softPwmWrite(self.g_pin, int(g)) b = self.blue + (blue - self.blue) * f wiringpi2.softPwmWrite(self.b_pin, int(b)) wiringpi2.delay(step) self.red = red self.blue = blue self.green = green
def fade(self, red, green, blue, delay=500, step=5): for i in range(0, delay, step): f = (0.0+i)/delay r = self.red + (red -self.red) * f wiringpi2.softPwmWrite(self.r_pin, int(r)) g = self.green + (green-self.green) * f wiringpi2.softPwmWrite(self.g_pin, int(g)) b = self.blue + (blue -self.blue) * f wiringpi2.softPwmWrite(self.b_pin, int(b)) wiringpi2.delay(step) self.red = red self.blue = blue self.green = green
def adelante(): wiringpi2.softPwmWrite(1, 14) wiringpi2.softPwmWrite(0, 16)
def SetLedBorg(red, green, blue): wiringpi.softPwmWrite(PIN_RED, int(red * LED_MAX)) wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX)) wiringpi.softPwmWrite(PIN_BLUE, int(blue * LED_MAX))
def wiringPiMotorStop(self): if self.getStatus() == "A": wiringpi2.softPwmWrite(self.backward, 0) if self.getStatus() == "V": wiringpi2.softPwmWrite(self.forward, 0) self.status = 0
def brake(self): wiringpi2.digitalWrite(self.pin1, HIGH) wiringpi2.softPwmWrite(self.pin2, HIGH)
def turnoff(self): wiringpi2.digitalWrite(self.pin1, LOW) wiringpi2.softPwmWrite(self.pin2, LOW)
mindict['xcoord'], mindict['ycoord'], mindict['width'], mindict['height'] = cv2.boundingRect(cnt1) mindict['rank'] = abs((float(w1)/float(h1)) - .4) minlist.append(mindict) minlist = sorted(minlist, key=lambda x: x['rank']) #if len(minlist) > 0: # cv2.rectangle(frame, (minlist[0]['xcoord'],minlist[0]['ycoord']), (minlist[0]['xcoord']+minlist[0]['width'],minlist[0]['ycoord']+minlist[0]['height']), (0,255,0), 4) if len(minlist) > 1: if minlist[0]['width']-10 <= minlist[1]['width'] <= minlist[0]['width'] + 10 and minlist[0]['height']-10 <= minlist[1]['height'] <= minlist[0]['height']+10: if minlist[0]['ycoord']-10 <= minlist[1]['ycoord'] <= minlist[0]['ycoord']+10: #if 1.45 <= float(abs(minlist[0]['xcoord'] - minlist[1]['xcoord']))/float(minlist[0]['width']) <= 1.95: cv2.rectangle(frame, (minlist[0]['xcoord'],minlist[0]['ycoord']), (minlist[0]['xcoord']+minlist[0]['width'],minlist[0]['ycoord']+minlist[0]['height']), (0,255,0), 4) cv2.rectangle(frame, (minlist[1]['xcoord'],minlist[1]['ycoord']), (minlist[1]['xcoord']+minlist[1]['width'],minlist[1]['ycoord']+minlist[1]['height']), (0,255,0), 4) xwx=((float(minlist[0]['xcoord']+minlist[0]['width']) + float(minlist[1]['xcoord']))/2.0) wdth=(float(camera.get(cv2.CAP_PROP_FRAME_WIDTH))/2.0) print (wdth-xwx)*0.10375 wiringpi2.softPwmWrite(PIN_TO_PWM,((float(1023)/float(640))*x)) #if xwx <= wdth-5.0: # print "Turn Left" #elif xwx >= wdth+5.0: # print "Turn Right" #else: # print "Centered" minlist=[] #(x,y,w,h) = ecv2.boundingRect(cnt) #cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3) #if float(2.6) <= (float(w)/float(h)) <= float(2.9): # print "locked" # cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,0), 3) # show the frame to our screen cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF
def atras(): wiringpi2.softPwmWrite(0, 14) wiringpi2.softPwmWrite(1, 16)
def izquierda(): wiringpi2.softPwmWrite(1, 14) wiringpi2.softPwmWrite(0, 14)
def derecha(): wiringpi2.softPwmWrite(1, 16) wiringpi2.softPwmWrite(0, 16)
def parar(): wiringpi2.softPwmWrite(1, 0) wiringpi2.softPwmWrite(0, 0)
def process_change(self, color, value): value = int(value) pin = 0 if color == 'r': pin = red elif color == 'g': pin = green elif color == 'b': pin = blue if pin != 0 and value >= 0 and value <= 100: wpi.softPwmWrite(pin, value) #Create a web server and define the handler to manage the #incoming request try: wpi.wiringPiSetup() wpi.softPwmCreate(red, 0, 100) wpi.softPwmCreate(green, 0, 100) wpi.softPwmCreate(blue, 0, 100) server = HTTPServer(('', PORT_NUMBER), reqHandler) print 'Started pwm_server on port ' , PORT_NUMBER server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down the web server' wpi.softPwmWrite(red, 0) wpi.softPwmWrite(green, 0) wpi.softPwmWrite(blue, 0) server.socket.close()
def buttonToggled(self, widget, data): if (data == "red"): if (self.redIsOn == True): print "lightsoff" wiringpi2.softPwmWrite(self.redPin, 0) self.redIsOn = False elif (self.redIsOn == False): wiringpi2.softPwmWrite(self.redPin, int(self.redSlider.get_value())) self.redIsOn = True print "lightson" elif (data == "blue"): if (self.blueIsOn == True): wiringpi2.softPwmWrite(self.bluePin, 0) self.blueIsOn = False elif (self.blueIsOn == False): wiringpi2.softPwmWrite(self.bluePin, int(self.blueSlider.get_value())) self.blueIsOn = True elif (data == "green"): if (self.greenIsOn == True): wiringpi2.softPwmWrite(self.greenPin, 0) self.greenIsOn = False elif (self.greenIsOn == False): wiringpi2.softPwmWrite(self.greenPin, int(self.greenSlider.get_value())) self.greenIsOn = True
if wiringpi2.wiringPiSetupGpio() == -1: sys.exit(1) # 使用するGPIOポート port = 4 RANGE = 100 # PWMに指定する値の最大値 # softPwmの設定 wiringpi2.softPwmCreate(port, 0, RANGE) # ポート、初期値、最大値 SLEEP_TIME = 0.01 val = 0 while True: try: for pw in range(100): wiringpi2.softPwmWrite(port, pw) time.sleep(SLEEP_TIME) for pw in range(100-1, 0-1, -1): wiringpi2.softPwmWrite(port, pw) time.sleep(SLEEP_TIME) except KeyboardInterrupt: break except: raise sys.exit(0) # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
import psutil import atexit # WiringPi setup wiringpi.wiringPiSetup() # Setup the LedBorg GPIO pins PIN_RED = 0 PIN_GREEN = 2 PIN_BLUE = 3 LED_MAX = 100 wiringpi.softPwmCreate(PIN_RED, 0, LED_MAX) wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX) wiringpi.softPwmCreate(PIN_BLUE, 0, LED_MAX) wiringpi.softPwmWrite(PIN_RED, 0) wiringpi.softPwmWrite(PIN_GREEN, 0) wiringpi.softPwmWrite(PIN_BLUE, 0) # A function to set the LedBorg colours def SetLedBorg(red, green, blue): wiringpi.softPwmWrite(PIN_RED, int(red * LED_MAX)) wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX)) wiringpi.softPwmWrite(PIN_BLUE, int(blue * LED_MAX)) # A function to turn the LedBorg off def LedBorgOff(): SetLedBorg(0, 0, 0) # Function to handle exit def ExitHandler():
#!/usr/bin/env python # Pulsates an LED connected to GPIO pin 1 with a suitable resistor 4 times using softPwm # softPwm uses a fixed frequency import wiringpi2 OUTPUT = 1 PIN_TO_PWM = 7 wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_PWM,OUTPUT) wiringpi2.softPwmCreate(PIN_TO_PWM,0,100) # Setup PWM using Pin, Initial Value and Range parameters while(True): #for brightness in range(0,100): # Going from 0 to 100 will give us full off to full on wiringpi2.softPwmWrite(PIN_TO_PWM,0) # Change PWM duty cycle wiringpi2.delay(70) # Delay for 0.2 seconds #for brightness in reversed(range(0,100)): wiringpi2.softPwmWrite(PIN_TO_PWM,100) wiringpi2.delay(70)
OUTPUT = 1 PIN_TO_PWM = 1 wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_PWM, OUTPUT) wiringpi2.softPwmCreate( PIN_TO_PWM, 0, 100) # Setup PWM using Pin, Initial Value and Range parameters STEP = 2 DELAY = .5 def FlashDesk(): for i in range(0, 5): for j in range(0, 100, STEP): wiringpi2.softPwmWrite(PIN_TO_PWM, j) wiringpi2.delay(50) for j in range(100, 0, (STEP * -1)): wiringpi2.softPwmWrite(PIN_TO_PWM, j) wiringpi2.delay(50) wiringpi2.softPwmWrite(PIN_TO_PWM, 100) while True: wiringpi2.softPwmWrite(PIN_TO_PWM, 100) #FlashDesk()
# softPwm uses a fixed frequency import wiringpi2 OUTPUT = 1 # GPIO17 = GPIO_GEN0 = P1.11, connecting a red led PIN_TO_PWM = 0 wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_PWM, OUTPUT) wiringpi2.softPwmCreate( PIN_TO_PWM, 0, 100) # Setup PWM using Pin, Initial Value and Range parameters for time in range(0, 4): for brightness in range( 0, 100): # Going from 0 to 100 will give us full off to full on wiringpi2.softPwmWrite(PIN_TO_PWM, brightness) # Change PWM duty cycle wiringpi2.delay(10) # Delay for 0.2 seconds for brightness in reversed(range(0, 100)): wiringpi2.softPwmWrite(PIN_TO_PWM, brightness) wiringpi2.delay(10)
# softPwm uses a fixed frequency import wiringpi2 OUTPUT = 1 # GPIO17 = GPIO_GEN0 = P1.11, connecting a red led PIN_TO_PWM = 0 wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_PWM,OUTPUT) wiringpi2.softPwmCreate(PIN_TO_PWM,0,100) # Setup PWM using Pin, Initial Value and Range parameters for time in range(0,4): for brightness in range(0,100): # Going from 0 to 100 will give us full off to full on wiringpi2.softPwmWrite(PIN_TO_PWM,brightness) # Change PWM duty cycle wiringpi2.delay(10) # Delay for 0.2 seconds for brightness in reversed(range(0,100)): wiringpi2.softPwmWrite(PIN_TO_PWM,brightness) wiringpi2.delay(10)
#io.pinMode(PWMA, io.PWM_OUTPUT) io.pinMode(STANDBY, io.OUTPUT) io.pinMode(DRIVEB0, io.OUTPUT) io.pinMode(DRIVEB1, io.OUTPUT) #io.pinMode(PWMB, io.PWM_OUTPUT) # Set all the drives to 'off' io.digitalWrite(DRIVEA0, A0) io.digitalWrite(DRIVEA1, A1) io.digitalWrite(STANDBY, io.HIGH) io.digitalWrite(DRIVEB0, B0) io.digitalWrite(DRIVEB1, B1) # Enable PWM #wiringpi2.softPwmWrite(SERVOA, 0) wiringpi2.softPwmWrite(PWMA, 0) wiringpi2.softPwmWrite(PWMB, 0) # Wait for a joystick while pygame.joystick.get_count() == 0: print 'waiting for joystick count = %i' % pygame.joystick.get_count() io.digitalWrite(STANDBY, io.HIGH) time.sleep(1) io.digitalWrite(STANDBY, io.LOW) time.sleep(1) pygame.joystick.quit() pygame.joystick.init() j = pygame.joystick.Joystick(0) j.init()