def __init__(self, num_leds, state_machine, pin, mode="RGB", delay=0.0001): self.pixels = array.array("I", [0 for _ in range(num_leds)]) self.mode = set(mode) # set for better performance if 'W' in self.mode: # RGBW uses different PIO state machine configuration self.sm = rp2.StateMachine(state_machine, sk6812, freq=8000000, sideset_base=Pin(pin)) # dictionary of values required to shift bit into position (check class desc.) self.shift = { 'R': (mode.index('R') ^ 3) * 8, 'G': (mode.index('G') ^ 3) * 8, 'B': (mode.index('B') ^ 3) * 8, 'W': (mode.index('W') ^ 3) * 8 } else: self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin)) self.shift = { 'R': ((mode.index('R') ^ 3) - 1) * 8, 'G': ((mode.index('G') ^ 3) - 1) * 8, 'B': ((mode.index('B') ^ 3) - 1) * 8, 'W': 0 } self.sm.active(1) self.num_leds = num_leds self.delay = delay self.brightnessvalue = 255
def __init__(self, get_pin=None, put_pin=None, sm_freq=1_000_000): self.get_done = False self.sm_get_nr = 0 if get_pin is not None: if (sm_freq * 2) > machine.freq(): raise (ValueError, "frequency too high") self.sm_get = rp2.StateMachine(self.sm_get_nr, self.sm_get_pulses, freq=sm_freq * 2, jmp_pin=get_pin, in_base=get_pin, set_base=get_pin) self.sm_get.irq(self.irq_finished) else: self.sm_get = None self.put_done = False self.sm_put_nr = 4 if put_pin is not None: if (sm_freq) > machine.freq(): raise (ValueError, "frequency too high") self.sm_put = rp2.StateMachine(self.sm_put_nr, self.sm_put_pulses, freq=sm_freq, out_base=put_pin) self.sm_put.irq(self.irq_finished) else: self.sm_put = None
def __init__(self, rs_pin, enable_pin=None, data_port=None, fourbit=True, rw_pin=None, backlight_pin=None, num_lines=2, num_columns=16): """Constructs the PIOLcd object. The xx_pin arguments must be machine.Pin objects. data_port is the number of the lowest order GPIO pin for data, which must be consecutive. fourbit tells, whether the display is connected in 4-bit or 8-bit mode. enable_pin is the GPIO number of the enable pin. The rw pin isn't used by this library, but if you specify it, then it will be set low. """ self.rs_pin = rs_pin self.rw_pin = rw_pin self.backlight_pin = backlight_pin self._4bit = fourbit self.rs_pin.init(Pin.OUT) self.rs_pin.value(0) if self.rw_pin: self.rw_pin.init(Pin.OUT) self.rw_pin.value(0) if self.backlight_pin is not None: self.backlight_pin.init(Pin.OUT) self.backlight_pin.value(0) # activate the four bit state machine in single nibble mode self.sm = rp2.StateMachine(0, self._4bit_write, freq=1000000, sideset_base=enable_pin, out_base=data_port, pull_thresh=4) self.sm.active(1) sleep_ms(20) # Allow LCD time to powerup # Send reset 3 times self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) sleep_ms(5) # need to delay at least 4.1 msec self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) sleep_ms(1) self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) sleep_ms(1) cmd = self.LCD_FUNCTION if not self._4bit: cmd |= self.LCD_FUNCTION_8BIT self.hal_write_init_nibble(cmd) sleep_ms(1) if self._4bit is True: # switch to dual níbble mode mode by overriding pull_thresh self.sm.active(0) self.sm = rp2.StateMachine(0, self._4bit_write, freq=1000000, sideset_base=enable_pin, out_base=data_port, pull_thresh=8) self.sm.active(1) else: # switch the state machine to 8 bit self.sm.active(0) self.sm = rp2.StateMachine(0, self._8bit_write, freq=1000000, sideset_base=enable_pin, out_base=data_tbase) self.sm.active(1) LcdApi.__init__(self, num_lines, num_columns) if num_lines > 1: cmd |= self.LCD_FUNCTION_2LINES self.hal_write_command(cmd)
def __init__(self, num_leds, state_machine, pin, delay=0.001): self.pixels = array.array("I", [0 for _ in range(num_leds)]) self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin)) self.sm.active(1) self.num_leds = num_leds self.delay = delay self.brightnessvalue = 255
def __init__(self,pin=PIN_NUM,num=NUM_LEDS,brightness=0.8): self.pin=pin self.num=num self.brightness = brightness # Create the StateMachine with the ws2812 program, outputting on pin self.sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM)) # Start the StateMachine, it will wait for data on its FIFO. self.sm.active(1) # Display a pattern on the LEDs via an array of LED RGB values. self.ar = array.array("I", [0 for _ in range(self.num)]) self.BLACK = (0, 0, 0) self.RED = (15, 0, 0) self.YELLOW = (15, 15, 0) self.GREEN = (0, 15, 0) self.CYAN = (0, 15, 15) self.BLUE = (0, 0, 15) self.PURPLE = (15, 0, 15) self.WHITE = (15, 15, 15) self.COLORS = [self.RED, self.YELLOW, self.GREEN, self.CYAN, self.BLUE, self.PURPLE, self.WHITE, self.BLACK ] self.lattice = [self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.RED, self.RED, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.RED, self.RED, self.RED, self.CYAN, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.CYAN, self.RED, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.RED, self.RED, self.RED, self.RED, self.RED, self.RED, self.CYAN, self.CYAN, self.RED, self.RED, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN, self.CYAN]
def __init__(self,trigger=1,echo=0,sm=0): echopin=Pin(echo,Pin.IN) print(echopin) self.sm=rp2.StateMachine(sm,_SR04_PIO,freq=343000,set_base=echopin,jmp_pin=echopin,sideset_base=Pin(trigger)) self.sm.active(1) self.result=0 self.sm.irq(self.get)
def __init__(self, sm_no, base_pin, scale=1): self.scale = scale self._pos = array("i", (0,)) # [pos] self.sm = rp2.StateMachine(sm_no, self.pio_quadrature, in_base=base_pin) self.sm.irq(make_isr(self._pos)) # Instantiate the closure self.sm.exec("set(y, 99)") # Initialise y: guarantee different to the input self.sm.active(1)
def __init__(self, gpio): self.sm = rp2.StateMachine(0, dht11, freq=100000, in_base=Pin(gpio), set_base=Pin(gpio), jmp_pin=Pin(gpio)) self.sm.active(1)
def __init__(self,ir=0,sm=5): irpin=Pin(ir,Pin.IN) self.sm=rp2.StateMachine(sm,RC6PIO,freq=1400*12,in_base=irpin,jmp_pin=irpin) self.sm.active(1) self.lastkey=0 self.keytime=0 self.sm.irq(self.get_ir) self.sm.put(0x25)
def __init__(self, sm_id, pin): self.sm = rp2.StateMachine(0, pulse_counter, in_base=pin) # Initialize x to zero self.sm.put(0) self.sm.exec("pull()") self.sm.exec("mov(x, osr)") # Start the StateMachine's running. self.sm.active(1)
def __init__(self, sm_id, mosi, sck, freq=4000000): self._sm = rp2.StateMachine(sm_id, _spi_send_only, freq=4*freq, sideset_base=Pin(sck), out_base=Pin(mosi), in_base=Pin(sck) ) self._sm.active(1)
def __init__(self, dataPin, powerPin=None, dht11=False): self.dataPin = dataPin self.powerPin = powerPin self.dht11 = dht11 self.dataPin.init(Pin.IN, Pin.PULL_UP) if self.powerPin is not None: self.powerPin.init(Pin.OUT) self.powerPin.value(0) self.sm = rp2.StateMachine(1)
def __init__(self, pin, n=1, brightness=1.0, autowrite=False): self.pin = Pin(pin, Pin.OUT) self.n = n self.buffer = [0] * self.n self.brightness = brightness self.autowrite = autowrite self.sm = rp2.StateMachine(0, _ws2812, freq=8_000_000, sideset_base=self.pin) self.sm.active(1) self.fill((0, 0, 0)) if not self.autowrite: self.show()
def __init__(self, num_leds, state_machine, pin, colour_order="RGB", delay=0.001, brightness = 1, autoshow = True): self.num_leds = num_leds self.pixels = array.array("I", [0 for _ in range(num_leds)]) self.red_shift = colour_order.index("R") * 8 self.green_shift = colour_order.index("G") * 8 self.blue_shift = colour_order.index("B") * 8 self.white_shift = colour_order.find("W") * 8 # Use the "find" method in case the Neopixels are 3 colour if self.white_shift >= 0: self.sm = rp2.StateMachine(state_machine, ws2812_4, freq=8000000, sideset_base=board.pin) else: self.sm = rp2.StateMachine(state_machine, ws2812_3, freq=8000000, sideset_base=board.pin) """ if self.white_shift >= 0: self.sm = rp2.StateMachine(state_machine, ws2812_4, freq=8000000, sideset_base=Pin(pin)) else: self.sm = rp2.StateMachine(state_machine, ws2812_3, freq=8000000, sideset_base=Pin.pin)) """ self.sm.active(1) self.delay = delay self.brightness = brightness self.autoshow = autoshow
def __init__(self, id, dir_pin, step_pin, step_callback=None): self.dir_pin = Pin(dir_pin, Pin.OUT) # at 1_000_000Hz clock a single instruction is 1us self._sm = rp2.StateMachine(id, stepper, freq=1_000_000, set_base=Pin(step_pin, Pin.OUT)) if step_callback is not None: self._sm.irq(step_callback) self._sm.put(0) self._sm.active(1)
def __init__(self, pin, model, statemachine=0): self._pin = Pin(pin) self._model = model if model in (DHT.DHT_11, DHT.DHT_22) else DHT.DHT_11 self._humid = 0 self._temp = 0 self._cks = False self._sm = rp2.StateMachine(statemachine, DHT._dht, freq=490196, in_base=self._pin, set_base=self._pin, jmp_pin=self._pin) self._sm.active(1)
def rainbow(brightness=0.2, ntimes=1): """Show rainbow on NeoPixel.""" def pixels_show(): dimmer_ar = array.array("I", [0 for _ in range(NEOS_COUNT)]) for i, c in enumerate(ar): r = int(((c >> 8) & 0xFF) * brightness) g = int(((c >> 16) & 0xFF) * brightness) b = int((c & 0xFF) * brightness) dimmer_ar[i] = (g << 16) + (r << 8) + b sm.put(dimmer_ar, 8) sleep_ms(10) def pixels_set(i, color): ar[i] = (color[1] << 16) + (color[0] << 8) + color[2] def wheel(pos): # Input a value 0 to 255 to get a color value. # The colours are a transition r - g - b - back to r. if pos < 0 or pos > 255: return (0, 0, 0) if pos < 85: return (255 - pos * 3, pos * 3, 0) if pos < 170: pos -= 85 return (0, 255 - pos * 3, pos * 3) pos -= 170 return (pos * 3, 0, 255 - pos * 3) # Create the StateMachine with the ws2812 program, outputting on pin sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(NEOS_PIN_NUM)) # Start the StateMachine, it will wait for data on its FIFO. sm.active(1) # Display a pattern on the LEDs via an array of LED RGB values. ar = array.array("I", [0 for _ in range(NEOS_COUNT)]) for t in range(ntimes): for j in range(255): for i in range(NEOS_COUNT): rc_index = (i * 256 // NEOS_COUNT) + j pixels_set(i, wheel(rc_index & 255)) pixels_show() for i in range(NEOS_COUNT): pixels_set(i, (0, 0, 0)) pixels_show()
def __init__(self, pin, nNPs=1): """ Requires a pin index and the number of NeoPixels """ self._numLEDs = nNPs self._pin = pin self._brightness = 0.1 self._data = array.array("I", [0 for _ in range(nNPs)]) # Create the StateMachine with the WS2812 program and start it; it will # still wait for data self._SM = rp2.StateMachine(0, ws2812, freq=FREQ, sideset_base=Pin(pin)) self._SM.active(1)
def __init__(self, sm_id, pin_mosi, pin_miso, pin_sck, cpha=False, cpol=False, freq=1000000): assert (not (cpol or cpha)) self._sm = rp2.StateMachine(sm_id, spi_cpha0, freq=4 * freq, sideset_base=Pin(pin_sck), out_base=Pin(pin_mosi), in_base=Pin(pin_sck)) self._sm.active(1)
def __init__(self, pin, n=1, brightness=1.0, autowrite=False, statemachine=0): self.brightness = brightness self.autowrite = autowrite self._sm = rp2.StateMachine(statemachine, NeoPixel._ws2812, freq=2400000, sideset_base=Pin(pin, Pin.OUT)) self._sm.active(1) self.buffer = [(0, 0, 0)] * n if not self.autowrite: self.show()
def __init__(self, clk, data, gain=128): self.pSCK = clk self.pOUT = data self.pSCK.value(False) self.GAIN = 0 self.OFFSET = 0 self.SCALE = 1 self.time_constant = 0.25 self.filtered = 0 self.sm_timer = Timer() # create the state machine self.sm = rp2.StateMachine(0, self.hx711_pio, freq=1_000_000, sideset_base=self.pSCK, in_base=self.pOUT, jmp_pin=self.pOUT) self.set_gain(gain)
class neopixel: def __init__(self, channel, pixels, pin, colours=4, ): print("This is the constructor method.") self.channel = channel self.pixels = pixels self.pin = pin self.pull_thresh = colours * 8 @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=self.pull_thresh) def ws2812(): T1 = 2 T2 = 5 T3 = 3 wrap_target() label("bitloop") out(x, 1) .side(0) [T3 - 1] jmp(not_x, "do_zero") .side(1) [T1 - 1] jmp("bitloop") .side(1) [T2 - 1] label("do_zero") nop() .side(0) [T2 - 1] wrap() # Create the StateMachine with the ws2812 program, outputting on Pin(22). self.sm = rp2.StateMachine(self.channel, self.ws2812, freq=8_000_000, sideset_base=machine.Pin(self.pin)) # Start the StateMachine, it will wait for data on its FIFO. self.sm.active(1) # Display a pattern on the LEDs via an array of LED RGB values. self.ar = array.array("I", [0 for _ in range(self.pixels)]) setpixel(self, index, colour): self.ar[index][0] = colour[1] self.ar[index][1] = colour[0] self.ar[index][2] = colour[2] if self.colours = 4: self.ar[index][3] = colour[3]
import time import rp2 from machine import Pin @rp2.asm_pio(sideset_init=(rp2.PIO.OUT_LOW, rp2.PIO.OUT_LOW)) def blink(): wrap_target() nop().side(0x3) nop().side(0x0) [2] nop().side(0x1) nop().side(0x0) [1] set(y, 31) #32 loops label("loop") nop().side(0x1) nop().side(0x0) [1] jmp(y_dec, "loop") nop() [3] nop() [3] wrap() # easy eda # The frequency (which must be between 2000 and 125000000) #sm = rp2.StateMachine(0, blink, freq=48_310, sideset_base=Pin(1,0)) # 125000000 sm = rp2.StateMachine(0, blink, freq=16000, sideset_base=Pin(1,0)) sm.active(1) time.sleep(2000) sm.active(0)
def ws2812(): T1 = 2 T2 = 5 T3 = 3 wrap_target() label("bitloop") out(x, 1).side(0)[T3 - 1] jmp(not_x, "do_zero").side(1)[T1 - 1] jmp("bitloop").side(1)[T2 - 1] label("do_zero") nop().side(0)[T2 - 1] wrap() # Create the StateMachine with the ws2812 program, outputting on pin sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM)) # Start the StateMachine, it will wait for data on its FIFO. sm.active(1) # Display a pattern on the LEDs via an array of LED RGB values. ar = array.array("I", [0 for _ in range(NUM_LEDS)]) ########################################################################## def pixels_show(): dimmer_ar = array.array("I", [0 for _ in range(NUM_LEDS)]) for i, c in enumerate(ar): r = int(((c >> 8) & 0xFF) * brightness) g = int(((c >> 16) & 0xFF) * brightness) b = int((c & 0xFF) * brightness)
label("function") set(pins, 0)[2] set(pins, 1)[7] jmp(x_dec, "loop") wait(1, pin, 0) jmp("entry_point") receiver = Pin(13, Pin.IN, Pin.PULL_UP) # The frequency (which must be between 2000 and 125000000) # frequency of arduino sketch of clock is approx. 80us, 12500 sm = rp2.StateMachine(0, test_1, freq=25000, sideset_base=Pin(10), set_base=Pin(11), in_base=receiver) trigger = Pin(12, Pin.OUT) trigger.value(1) sm.active(1) for i in range(5): time.sleep(0.5) trigger.value(1) time.sleep(0.5) trigger.value(0) sm.active(0)
nop()[31] nop()[31] nop()[31] nop()[31] set(pins, 0)[31] nop()[31] nop()[31] nop()[31] nop()[31] wrap() # Instantiate a state machine with the blink program, at 2000Hz, with set bound to Pin(25) (LED on the rp2 board) # There are eight statemachines numbered from 0 to 7. Minimum frequency is 1908. frequency = 87654 sm = rp2.StateMachine(0, blink, freq=frequency, set_base=Pin(25)) # Run the state machine for 1 second. The LED should blink. sm.active(1) time.sleep(0.5) SM0_CLKDIV = 0x50200000 + 0x0c8 print(bin(mem32[SM0_CLKDIV] & 2**32 - 1)) print(bin((mem32[SM0_CLKDIV] >> 16) & 2**16 - 1)) print(bin((mem32[SM0_CLKDIV] >> 8) & 2**8 - 1)) print(int((mem32[SM0_CLKDIV] >> 16) & 2**16 - 1)) print(int((mem32[SM0_CLKDIV] >> 8) & 2**8 - 1)) #clock freq / (CLKDIV_INT + CLKDIV_FRAC / 256) CLKDIV_INT = int((mem32[SM0_CLKDIV] >> 16) & 2**16 - 1) CLKDIV_FRAC = int((mem32[SM0_CLKDIV] >> 8) & 2**8 - 1) print(machine.freq() / (CLKDIV_INT + CLKDIV_FRAC / 256)) sm.active(0)
import time import rp2 from machine import Pin, mem32 # Define the blink program. It has one GPIO to bind to on the set instruction, which is an output pin. # Use lots of delays to make the blinking visible by eye. @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) def blink(): wrap_target() set(pins, 1) set(pins, 0) wrap() # Instantiate a state machine with the blink program, at 2000Hz, with set bound to Pin(25) (LED on the rp2 board) sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(16)) # Run the state machine for 3 seconds. The LED should blink. SM0_CLKDIV = 0x50200000 + 0x0c8 # Description # Clock divisor register for state machine N # Frequency = clock freq / (CLKDIV_INT + CLKDIV_FRAC / 256) # Bits Name Description Type Reset # 31:16 INT Effective frequency is sysclk/(int + frac/256). Value of 0 is interpreted as 65536. If INT is 0, FRAC must also be 0.RW 0x0001 # 15:8 FRAC Fractional part of clock divisor RW 0x00 # 7:0 Reserved. - - - word = mem32[SM0_CLKDIV]
@rp2.asm_pio(set_init=rp2.PIO.OUT_HIGH) def zuendsignal2(): pull() mov(x, osr) label("delay") jmp(x_dec, "delay") #nop() [4] wrap_target() set(pins, 0) set(pins, 1)[2] nop()[31] wrap() # possible frequencies 2000-125_000_000 sm = rp2.StateMachine(1, nockenwelle, freq=1_0_000, set_base=Pin(18)) sm2 = rp2.StateMachine(0, zuendsignal2, freq=1_0_000, set_base=Pin(17)) sm3 = rp2.StateMachine(2, kurbelwelle, freq=1_0_000, set_base=Pin(19)) ADR_PIO0_CTRL = 0x50200000 + 0x000 SM0_ADDR = 0x50200000 + 0x0d4 SM1_ADDR = 0x50200000 + 0x0ec SM0_CLKDIV = 0x50200000 + 0x0c8 SM1_CLKDIV = 0x50200000 + 0x0e0 SM2_CLKDIV = 0x50200000 + 0x0f8 SM3_CLKDIV = 0x50200000 + 0x110 print(sm) print(sm2) print(sm3)
set(pins, 1) set(pins, 0) label("delay") jmp(x_dec, "delay") mov(x, y) # label("end") def turn(sm): print("irq") sm = rp2.StateMachine(0, stepper, freq=10000, set_base=Pin(25)) sm.irq(turn) sm.put(10000) sm.active(1) print("moving") time.sleep(5) print("faster") sm.put(5000) time.sleep(5) print("even faster") sm.put(1000) time.sleep(5) sm.active(0)
#nop().side(0) jmp(x, "a").side(0) [1] nop() set(pins, 1) [2] nop().side(1) [3] jmp("start") #wrap() label("a") nop() set(pins, 0) [2] nop().side(1) [3] jmp("start") #wrap() sm = rp2.StateMachine(0, send_data, freq=100_000, sideset_base=Pin(18), out_base=Pin(17), set_base=Pin(17)) #char output[] = {0x00, 0x00, 0x7e, 0x01, 0x68, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x7e, 0x00, 0x00}; # direction is last from right to left and then top to buttom # <----------- start # <----------- # <----------- # end <----------- def put_data(x): sm.put(0b01111111100000011111111111111111); sm.put(0b11111111111110101111111111101001); sm.put(0b01001101111111111111111111111111); sm.put(0b11111111111111111000000111111111);