def _set_led(self, pin, pwm, val): LOG.debug('set LED on pin %d to %s', pin, val) if val == 0 or val == 1: PWM.clear_channel(pwm) GPIO.output(pin, 1 - int(val)) else: PWM.add_channel_pulse(pwm, pin, 0, 1999 - int(1999 * val))
def beep(self, t=T_MED, duration=D_MED): for x in range(0, self._range, t * 2): PWM.add_channel_pulse(self._channel, self._pin, x, t) sleep(duration) PWM.clear_channel(self._channel)
def __exit__(self, eType, eValue, eTrace): """ Clean up routine for context manager (with-statement). Takes care of closing DMA channel - failure to do this breaks OS and a reboot will be needed.""" log.info("Destroying " + str(self)) self.set([0, 0, 0]) sleep(PWM_PRD / 1e6) # wait a period to ensure set() has been actioned PWM.clear_channel(PWM_DMA) PWM.clear_channel(PWM_DMA1) PWM.cleanup() return False
def updatecolor (led_int, led_red, led_green, led_blue): time.sleep(1) PWM.clear_channel(0) print('Set color using MOOD %1d: Intensity: %1d | Red: %1d | Green: %1d | Blue: %1d' % (mood, led_int, led_red, led_green, led_blue)) time.sleep(1) PWM.add_channel_pulse(0, LI_PIN, 0, led_int) PWM.add_channel_pulse(0, LR_PIN, 0, led_red) PWM.add_channel_pulse(0, LG_PIN, 0, led_green) PWM.add_channel_pulse(0, LB_PIN, 0, led_blue) return
def set(self, channel, value): if (channel < 0 or channel > 2): return if (value <= 0): PWM.clear_channel(channel) else: if (value > 1000): value = 1000 PWM.add_channel_pulse(channel, self.mapping[channel], 0, value)
def setLEDs(self): if self.cnf.offline: self.logger.info("TEST: topled=%d bottomled=%d", self.topLedTransitionsPerCycle, self.bottomLedTransitionsPerCycle) return PWM.clear_channel(ledChannel) for pin, state in { topLed: self.topLedTransitionsPerCycle, bottomLed: self.bottomLedTransitionsPerCycle }.iteritems(): if state: ds = ledFull / (state * 2 - 1) for i in range(0, state): PWM.add_channel_pulse(ledChannel, pin, start=i * ds * 2, width=ds) else: PWM.add_channel_pulse(ledChannel, pin, start=0, width=1)
def set(self, rgb, blink=False, fast=False): """Set desired output RGB values. rgb: RGB vector of 0/1 or False/True. [blink]: If the LED should blink. """ if not len(rgb) == 3: raise ValueError("RGB vector must have three entries (rgb=" + str(rgb) + ")") PWM.clear_channel(PWM_DMA) PWM.clear_channel(PWM_DMA1) timing_offset = 0 # cant set both low and high at the same time, using one channel for value, pin in zip(rgb, self._pins): log.debug("value=%s", str(value)) if value in (1, True, "HIGH"): if blink: if fast: PWM.add_channel_pulse(PWM_DMA, pin, timing_offset, PWM_PRD / PWM_RES / 2) else: PWM.add_channel_pulse(PWM_DMA1, pin, timing_offset, PWM_PRD / PWM_RES / 2) else: PWM.add_channel_pulse(PWM_DMA, pin, timing_offset, PWM_PRD / PWM_RES - 3) else: PWM.add_channel_pulse(PWM_DMA, pin, timing_offset, 0) timing_offset = timing_offset + 1
def power(self, state): if self.cnf.offline: self.logger.info("TEST: setting power=%d", state) return if state: PWM.clear_channel(relayChannel) PWM.add_channel_pulse(relayChannel, relay, start=0, width=relayFull) time.sleep(holdDelay) PWM.clear_channel(relayChannel) PWM.add_channel_pulse(relayChannel, relay, start=0, width=relayLow) else: PWM.add_channel_pulse(relayChannel, relay, start=0, width=0) PWM.clear_channel(relayChannel)
def apply(self): for c in (self.r,self.g,self.b): if c >255 or c<0: print "Invalid value: ",c return; maxval = 5000.0 - 1 rval = int(round(self.dimFactor * ((self.r/255.0) * (maxval)))) gval = int(round(self.dimFactor * ((self.g/255.0) * (maxval)))) bval = int(round(self.dimFactor * ((self.b/255.0) * (maxval)))) PWM.clear_channel(1) PWM.add_channel_pulse(1, 17, 0, bval) PWM.clear_channel(2) PWM.add_channel_pulse(2, 27, 0, rval) PWM.clear_channel(3) PWM.add_channel_pulse(3, 22, 0, gval)
import RPIO.PWM as PWM PWM.setup(30) PWM.init_channel(1, 3000) PWM.clear_channel_gpio(1, 18) PWM.clear_channel(1) PWM.cleanup()
try: while True: # LED per vedere se tutto funziona for t in led_time: for i in gpio_port: RPIO.output(i, RPIO.HIGH) sleep(t) RPIO.output(i, RPIO.LOW) sleep(t) for d in canale_dma: subcycle_T = PWM.get_channel_subcycle_time_us(d) incr_T = PWM.get_pulse_incr_us() print("CANALE ", d, "subcycle_T =", subcycle_T, "--- incr_T =", incr_T) sleep(2) # if PWM.is_setup(): #è stato fatto il setup d'inizializzazione # granularity=granularity*10 #se sì: incremento la granularity # PWM.setup(granularity,0) #rifaccio il setup except KeyboardInterrupt: pass for i in canale_dma: PWM.clear_channel(i) PWM.clear_channel_gpio(i,gpio_port(i)) PWM.cleanup() print "STOP!" print "Ciao!"
if Cface[0] < 140: # and diffrent dirrections depending on what side of center if finds a face. CamRight(1, 1) if Cface[0] < 130: CamRight(3, 2) if Cface[0] < 120: CamRight(6, 3) if Cface[1] > 140: # and moves diffrent servos depending on what axis we are talking about. CamDown(1, 1) if Cface[1] > 150: CamDown(3, 2) if Cface[1] > 160: CamDown(6, 3) if Cface[1] < 100: CamUp(1, 1) if Cface[1] < 90: CamUp(3, 2) if Cface[1] < 80: CamUp(6, 3) except KeyboardInterrupt: pass finally: PWM.clear_channel(0) PWM.cleanup() capture.release() cv2.cv.DestroyWindow("video")
######################## if (prv_pl_state == 0 or toggle_prv == True): time.sleep(1) # debounce if (prv_pl_state == 0): toggle_prv = True if (prv_pl_state == 1): toggle_prv = False if (toggle_prv == True and prv_pl_state == 0): print ('PREVIOUS PLAYLIST (Mood: %1d)' % (mood)) if (mood == 1): #tn.write("PLY,3,CLR;" + "\n") tn.write("*SKIP,1,DEC;" + "\n") #tn.write("PLY,4,PLAY,SET;" + "\n") if (mood == 4): #tn.write("PLY,4,CLR;" + "\n") tn.write("*SKIP,2,DEC;" + "\n") #tn.write("PLY,4,PLAY,SET;" + "\n") #colorswitch(mood,lastmood) print ('Closing') tn.close() PWM.clear_channel(0) PWM.cleanup()
def Close(self): PWM.clear_channel(0) PWM.cleanup()
try: while True: # LED per vedere se tutto funziona for t in led_time: for p in gpio_port: RPIO.output(p, RPIO.HIGH) sleep(t) RPIO.output(p, RPIO.LOW) sleep(t) for d in canale_dma: subcycle_T = PWM.get_channel_subcycle_time_us(d) incr_T = PWM.get_pulse_incr_us() print("CANALE ", d, "subcycle_T =", subcycle_T, "--- incr_T =", incr_T) sleep(2) # if PWM.is_setup(): #è stato fatto il setup d'inizializzazione # granularity=granularity*10 #se sì: incremento la granularity # PWM.setup(granularity,0) #rifaccio il setup except KeyboardInterrupt: RPIO.setwarnings(False) for i in canale_dma: PWM.clear_channel(i) PWM.clear_channel_gpio(i,gpio_port(i)) PWM.cleanup() print "STOP!" print "Ciao!"
def stop(): PWM.clear_channel(pwm_controller._dma_channel)
PWM.set_loglevel(PWM.LOG_LEVEL_DEBUG) PWM.setup() PWM.init_channel(CHANNEL) PWM.print_channel(CHANNEL) PWM.add_channel_pulse(CHANNEL, GPIO, 0, 500) raw_input('Enter your input:') PWM.init_channel(CHANNEL2) PWM.print_channel(CHANNEL2) PWM.add_channel_pulse(CHANNEL2, GPIO2, 600, 200) raw_input('Enter your input:') PWM.clear_channel(CHANNEL) raw_input('Enter your input:') PWM.clear_channel(CHANNEL2) raw_input('Enter your input:') PWM.add_channel_pulse(CHANNEL, GPIO, 0, 200) raw_input('Enter your input:') PWM.cleanup()
def stop(self): RPIO.del_interrupt_callback(self.pPanicBtn) PWM.clear_channel(0) PWM.cleanup()