def _get_pulses_pulseio(self): """ _get_pulses implements the communication protcol for DHT11 and DHT22 type devices. It sends a start signal of a specific length and listens and measures the return signal lengths. return pulses (array.array uint16) contains alternating high and low transition times starting with a low transition time. Normally pulses will have 81 elements for the DHT11/22 type devices. """ pulses = array.array('H') # create the PulseIn object using context manager with pulseio.PulseIn(self._pin, 81, True) as pulse_in: # The DHT type device use a specialize 1-wire protocol # The microprocessor first sends a LOW signal for a # specific length of time. Then the device sends back a # series HIGH and LOW signals. The length the HIGH signals # represents the device values. pulse_in.pause() pulse_in.clear() pulse_in.resume(self._trig_wait) # loop until we get the return pulse we need or # time out after 1/4 second tmono = time.monotonic() while time.monotonic() - tmono < 0.25: pass # time out after 1/4 seconds pulse_in.pause() while pulse_in: pulses.append(pulse_in.popleft()) pulse_in.resume() return pulses
def get_scale_data(pin, timeout=1.0): """Read a pulse of SPI data on a pin that corresponds to DYMO scale output protocol (12 bytes of data at about 14KHz), timeout is in seconds""" timestamp = time.monotonic() with pulseio.PulseIn(pin, maxlen=96, idle_state=True) as pulses: pulses.pause() pulses.clear() pulses.resume() while len(pulses) < 35: if (time.monotonic() - timestamp) > timeout: raise RuntimeError("Timed out waiting for data") pulses.pause() bits = [0] * 96 # there are 12 bytes = 96 bits of data bit_idx = 0 # we will count a bit at a time bit_val = False # first pulses will be LOW print(pulses[1]) for i in range(len(pulses)): if pulses[i] == 65535: # This is the pulse between transmits break num_bits = int(pulses[i] / 75 + 0.5) # ~14KHz == ~7.5us per clock #print("%d (%d)," % (pulses[i], num_bits), end='') for bit in range(num_bits): #print("bit #", bit_idx) bits[bit_idx] = bit_val bit_idx += 1 if bit_idx == 96: # we have read all the data we wanted #print("DONE") break bit_val = not bit_val #print(bits) data_bytes = [0] * 12 for byte_n in range(12): thebyte = 0 for bit_n in range(8): thebyte <<= 1 thebyte |= bits[byte_n * 8 + bit_n] data_bytes[byte_n] = thebyte print([hex(i) for i in data_bytes]) # do some very basic data checking if data_bytes[0] != 3 or data_bytes[1] != 3 or data_bytes[7] != 4 \ or data_bytes[8] != 0x1C or data_bytes[9] != 0 or data_bytes[10] \ or data_bytes[11] != 0: raise RuntimeError("Bad data capture") reading = ScaleReading() reading.stable = data_bytes[2] & 0x4 reading.units = data_bytes[3] reading.weight = data_bytes[5] + (data_bytes[6] << 8) if data_bytes[2] & 0x1: reading.weight *= -1 if reading.units == ScaleReading.OUNCES: # oi no easy way to cast to int8_t if data_bytes[4] & 0x80: data_bytes[4] -= 0x100 reading.weight *= 10**data_bytes[4] return reading
def __init__(self): # Left Ain1(backward) Ain2(Forward) Right Bin1(backward) Bin2(Forward) # Ain2 self.driver_left_in2 = pulseio.PWMOut(board.SERVO3, frequency=RoboHatEduCar.DRIVER_PWM_FREQ) # Ain1 self.driver_left_in1 = pulseio.PWMOut(board.SERVO4, frequency=RoboHatEduCar.DRIVER_PWM_FREQ) # Bin1 self.driver_right_in1 = pulseio.PWMOut(board.SERVO5, frequency=RoboHatEduCar.DRIVER_PWM_FREQ) # Bin2 self.driver_right_in2 = pulseio.PWMOut(board.SERVO6, frequency=RoboHatEduCar.DRIVER_PWM_FREQ) self.wheel_enc_left = pulseio.PulseIn(board.SERVO1, maxlen=30) self.wheel_enc_right = pulseio.PulseIn(board.SERVO2, maxlen=30) self.wheel_pos_left = 0 self.wheel_pos_right = 0 self.direction_left_wheel = RoboHatEduCar.DIR_FORWARD self.direction_right_wheel = RoboHatEduCar.DIR_FORWARD self.ctr_rampdown = [0, 0] self.ctr_braking = [0, 0] self.stop() """Drive the car forward or backward by the specified distance.
def __init__(self, dht11, pin, trig_wait): """ :param boolean dht11: True if device is DHT11, otherwise DHT22. :param ~board.Pin pin: digital pin used for communication :param int trig_wait: length of time to hold trigger in LOW state (microseconds) """ self._dht11 = dht11 self._pin = pin self._trig_wait = trig_wait self._last_called = 0 self._humidity = None self._temperature = None # We don't use a context because linux-based systems are sluggish # and we're better off having a running process if _USE_PULSEIO: self.pulse_in = pulseio.PulseIn(self._pin, 81, True)
def get_feedback(pin): global pulses unitsFC = 360 dutyScale = 1000 tc = 0 dcMin = 29 dcMax = 971 q2min = unitsFC/4 q3max = q2min * 3 turns = 0 pulses = pulseio.PulseIn(pin) thetaP = 0 while True: while len(pulses) < 2: if pulses.paused == True: pulses.resume() continue pulses.pause() pulse_list = [pulses[0], pulses[1]] if sum(pulse_list) > 1000 and sum(pulse_list) < 1200: high = pulses[0] low = pulses[1] tc = sum(pulse_list) dc = (dutyScale * high) / tc theta = (unitsFC - 1) - ((dc - dcMin) * unitsFC) / (dcMax - dcMin + 1) if theta < 0: theta = 0 elif (theta > (unitsFC - 1)): theta = unitsFC - 1 if (theta < q2min) and (thetaP > q3max): turns = turns + 1 elif (thetaP < q2min) and (theta > q3max): turns = turns - 1 if (turns >= 0): angle = (turns * unitsFC) + theta; elif (turns < 0): angle = ((turns + 1) * unitsFC) - (unitsFC - theta) yield angle, turns, theta pulses.clear() thetaP = theta continue else: pulses.clear() continue
def _get_pulses(self): """ _get_pulses implements the commumication protcol for DHT11 and DHT22 type devices. It send a start signal of a specific length and listens and measures the return signal lengths. pin is a board pin connected to the data pin of the device trig_wait is the amount of time to hold the start signal in the low state. This value varies based on the devide type. return pulses (array.array uint16) contains alternating high and low transition times starting with a low transition time. Normally pulses will have 81 elements for the DHT11/22 type devices. """ pulses = array.array('H') tmono = time.monotonic() # create the PulseIn object using context manager with pulseio.PulseIn(self._pin, 81, True) as pulse_in: # The DHT type device use a specialize 1-wire protocol # The microprocess first sends a LOW signal for a # specific length of time. Then the device send back a # series HIGH and LOW signals. The length the signals # determine the device values. pulse_in.pause() pulse_in.clear() pulse_in.resume(self._trig_wait) # loop until we get the return pulse we need or # time out after 2 seconds while True: if len(pulse_in) >= 80: break if time.monotonic() - tmono > 0.5: # time out after 2 seconds break pulse_in.pause() while len(pulse_in): pulses.append(pulse_in.popleft()) pulse_in.resume() return pulses
def _ensure_pulseio(self): if self.pin is None: return if self.io is not None: if self.debug: print('%s.DigitalInOut(%r).deinit()' % ( self.index, self.pin, )) self.io.deinit() self.io = None if self.pulseio is None: if self.debug: print('%s = PulseIn(%r, maxlen=%d)' % ( self.index, self.pin, self.pulseio_maxlen, )) self.pulseio = pulseio.PulseIn( self.pin, maxlen=self.pulseio_maxlen) # , idle_state=False) self.pulseio_val = None
def __init__(self, pin=board.D13, sample_num=20, use_ble=False): # Might not want to use BLE, perhaps for testing purposes self.use_ble = use_ble self.sample_num = sample_num try: self.pulses = pulseio.PulseIn(pin, self.sample_num) except ValueError as e: raise ValueError(e) self.pulses.pause() # The samples list holds the readings that will be used in calculating the distance. Pulse readings that # are determined to be way off (say 655355....) will not be included so len(samples) is <= sample_num. self.samples = [] if self.use_ble: # Set up BLE based on Adafruit's CircuitPython libraries. self.ble = BLERadio() uart_service = UARTService() advertisement = ProvideServicesAdvertisement(uart_service) # Advertise when not connected. self.ble.start_advertising(advertisement)
import pulseio from time import sleep import adafruit_irremote pulses = pulseio.PulseIn(22, maxlen=200, idle_state=False) decoder = adafruit_irremote.GenericDecode() while True: pulse = decoder._read_pulses_non_blocking(pulses) print(pulse) # try: # for pulse in pulses: # print(pulse) # except Exception as e: # print(e) sleep(0.1)
import board import neopixel from adafruit_crickit import crickit import pulseio import time import countio pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=True, brightness=0.1) pixels.fill((0, 0, 255)) # declare a crickit seesaw object ss = crickit.seesaw # declare a pulseio object for the servo feedback servo_fb = pulseio.PulseIn(board.D5) # immedately stop the pulse stream and clear it servo_fb.pause() servo_fb.clear() MIN_P = 32 MAX_P = 1046 PULSE_RANGE = MAX_P - MIN_P # declare a servo objet servo = crickit.continuous_servo_1 servo.set_pulse_width_range(min_pulse=1200, max_pulse=1720) scount = countio.Counter(board.D6) # define a function to scale and translate pulsewidth into angle def servo_angle(feedback_obj):
import board import neopixel from adafruit_crickit import crickit import pulseio import time import countio import random pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=True, brightness=0.1) pixels.fill((0, 0, 255)) # declare a crickit seesaw object ss = crickit.seesaw # pulsein for servo feedback idle_state True counts up with positive throttle servo_fb = pulseio.PulseIn(board.D5, idle_state=True) # immedately stop the pulse stream and clear it servo_fb.pause() servo_fb.clear() MIN_P = 32 MAX_P = 1046 PULSE_RANGE = MAX_P - MIN_P # declare a servo objet servo = crickit.continuous_servo_1 servo.set_pulse_width_range(min_pulse=1200, max_pulse=1720) servo.throttle = 0.0 fw_min = 0.18 # from calibration data bw_min = -0.18 # from calibration data fw_max = 0.8
elif vtx == "TRAMP": return tr_command(frequencies["command"],frequencies["payload"][int(freq_index)],uart) else: print("not implemented yet") ################################### #MAIN LOOP# ################################### #print("booting") #input("press any key") time.sleep(5) #wait 5 second from power on to boot the vtx power_levels,frequencies,unlock,get_settings,uart = init(vtx) old_status = servo_levels[0] #init a the lowest servo level pulses = pulseio.PulseIn(board.A0,maxlen=10,idle_state=False) while True: while len(pulses) < 10: pass pulses_2 = [pulses[i] for i in range(0,10) if pulses[i] < 3000] #clean useless values (1 out of 2 is inverted 18000-19000) average_val = sum(pulses_2)/len(pulses_2) actual_val = min(servo_levels, key=lambda x:abs(x-average_val)) #print("avg ",average_val) #print("act ",actual_val) if old_status != actual_val: #print("value changed from",old_status,"to ",actual_val) old_status = actual_val #qui settare la potenza! power_index = int(round(float(servo_levels.index(actual_val)/(len(servo_levels)-1)) * (len(power_levels["payload"])-1),0)) #print("power index ",power_index)
def __init__(self, debug=False): self.pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True) self.decoder = adafruit_irremote.GenericDecode() self.debug = debug
import board import pulseio import adafruit_irremote import time IR_PIN = board.D2 # Pin connected to IR receiver. print('IR listener') print() # Create pulse input and IR decoder. pulses = pulseio.PulseIn(IR_PIN, maxlen=200, idle_state=True) decoder = adafruit_irremote.GenericDecode() # Loop waiting to receive pulses. while True: # make sure pulses is empty # small delay for cleaner results pulses.clear() pulses.resume() time.sleep(.1) # Wait for a pulse to be detected. detected = decoder.read_pulses(pulses) # print the number of pulses detected # note: pulse count is an excellent indicator as to the quality of IR code # received. # # If you are expecting 67 each time (Adafruit Mini Remote Control #389) # and only receive 57 this will result in a incomplete list print("pulse count: ", len(detected))
# Circuit Playground Express Demo Code # Adjust the pulseio 'board.PIN' if using something else import pulseio import board import adafruit_irremote pulsein = pulseio.PulseIn(board.D7, maxlen=120, idle_state=True) decoder = adafruit_irremote.GenericDecode() # size must match what you are decoding! for NEC use 4 received_code = bytearray(4) while True: pulses = decoder.read_pulses(pulsein) # print("Heard", len(pulses), "Pulses:", pulses) try: code = decoder.decode_bits(pulses, debug=False) print("Decoded:", code) except adafruit_irremote.IRNECRepeatException: # unusual short code! print("NEC repeat!") except adafruit_irremote.IRDecodeException as e: # failed to decode print("Failed to decode: ", e.args) print("----------------------------")
pixel_count = 6 # Number of NeoPixels speed = .1 # Animation speed (in seconds). # This is how long to spend in a single animation frame. # Higher values are slower. # Good values to try are 400, 200, 100, 50, 25, etc. animation = 1 # Type of animation, can be one of these values: # 0 - Solid color pulse # 1 - Moving color pulse brightness = 1.0 # 0-1, higher number is brighter ir_code_min = 60 ir_code_max = 70 pulsein = pulseio.PulseIn(board.D2, maxlen=100, idle_state=True) decoder = adafruit_irremote.GenericDecode() # Adafruit IR Remote Codes: # Button Code Button Code # ----------- ------ ------ ----- # VOL-: 255 0/10+: 207 # Play/Pause: 127 1: 247 # VOL+: 191 2: 119 # SETUP: 223 3: 183 # STOP/MODE: 159 4: 215 # UP: 95 5: 87 # DOWN: 79 6: 151 # LEFT: 239 7: 231 # RIGHT: 175 8: 103 # ENTER/SAVE: 111 9: 167
startpause = 3 print( "usb-rc - an adapter from RC car receiver to USB-gamepad/joystick. https://github.com/colzilla/usb-rc" ) print("starting in {} sec...".format(startpause)) sleep(startpause) debug = False shush = False #create the gampepad object gp = Gamepad() #setup the pulse readers ch1 = pulseio.PulseIn(board.A3, 5) ch2 = pulseio.PulseIn(board.A4, 5) ch3 = pulseio.PulseIn(board.A2, 5) #used in auto-calibration ch1min = 1499 ch1max = 1501 ch2min = 1499 ch2max = 1501 ch3min = 1499 ch3max = 1501 allowed_min = 800 allowed_max = 2200 loop = 0
ez1pin = board.D1 # Trinket GPIO #1 # i2c LCD initialize bus and class i2c = busio.I2C(board.SCL, board.SDA) cols = 16 rows = 2 lcd = adafruit_character_lcd.Character_LCD_I2C(i2c, cols, rows) # calculated mode or median distance mode_result = 0 # pulseio can store multiple pulses # read in time for pin to transition samples = 18 pulses = pulseio.PulseIn(board.D1, maxlen=samples) # sensor reads which are in range will be stored here rangevalue = [0, 0, 0, 0, 0, 0, 0, 0, 0] # 25ms sensor power up pause time.sleep(.25) def tof_cm(time_of_flight): """ EZ1 ultrasonic sensor is measuring "time of flight" Converts time of flight into distance in centimeters """ convert_to_cm = 58 cm = time_of_flight / convert_to_cm
# Light, Sound, Temperature # YELLOW ON LIGHT [3] IS ILLUMINATI PRESENCE ADMITTER import array import math import time import audiobusio import board import pulseio import adafruit_irremote decoder = adafruit_irremote.GenericDecode() from adafruit_circuitplayground import cp pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True) def mean(values): return sum(values) / len(values) def normalized_rms(values): minbuf = int(mean(values)) sum_of_samples = sum( float(sample - minbuf) * (sample - minbuf) for sample in values ) return math.sqrt(sum_of_samples / len(values))
import pulseio import time import board list_a = [3, 1, 2] list_b = range(4, 9) list_a += list_b list_c = list_a[:4] list_c += sorted(list_a[:4]) pulse_read_1 = pulseio.PulseIn(board.RCH1, 1) pulse_read_2 = pulseio.PulseIn(board.RCH2, 1) pulse_read_3 = pulseio.PulseIn(board.RCH3, 1) pulse_read_4 = pulseio.PulseIn(board.RCH4, 1) # while True: for i, j in zip(list_a, list_c): pwm_pin = "board.SERVO" + str(i) pwm = pulseio.PWMOut(eval(pwm_pin)) pulse_name = "pulse_read_" + str(j) pwm.duty_cycle = 65534 while len(eval(pulse_name)) == 0: pass eval(pulse_name).pause() print(pwm_pin, "at", pulse_name, ":", eval(pulse_name)[0]) eval(pulse_name).clear() time.sleep(1) pwm.duty_cycle = 800 time.sleep(1)
# (CircuitPython) # this circuit was designed for use with the Metro Express Explorers Guide on Learn.Adafruit.com # by Asher Lieber for Adafruit Industries import time import board import pulseio import digitalio import array # 38 for NEC ir_led = pulseio.PWMOut(board.D3, frequency=1000 * 38, duty_cycle=0) ir_led_send = pulseio.PulseOut(ir_led) recv = pulseio.PulseIn(board.D2, maxlen=150, idle_state=True) # creating DigitalInOut objects for a record and play button but0 = digitalio.DigitalInOut(board.D9) # record but1 = digitalio.DigitalInOut(board.D8) # playback # setting up indicator LED led = digitalio.DigitalInOut(board.D13) led.switch_to_output() # waits for IR to be detected, returns def get_ir(): ir_f = array.array('H') print('waiting for ir') recv.clear()
def enableIRIn(self): self.recvBuffer = pulseio.PulseIn(self.recvPin, maxlen=150, idle_state=True) self.prevLength = 0
""" FILE: main.py DESC: Sample solution for Trinket M0 Speedometer (interruptor) sensor """ import board import busio import pulseio from time import sleep target_rate = 10 # goal in ticks/sec delay = 0.67 pulses = pulseio.PulseIn(board.D2) uart = busio.UART(board.TX, board.RX, baudrate=9600) while True: count = len(pulses) rate = count/delay print("RATE: " + str(rate) + " ticks/sec") if rate < target_rate: uart.write("+") elif rate > target_rate: uart.write("-") else: uart.write("=") pulses.clear() sleep(delay)
import pulseio import time import board pwm = pulseio.PWMOut(board.SERVO2) pulse_read = pulseio.PulseIn(board.RCH2, 1) while True: pwm.duty_cycle = 65534 # print(duty) while len(pulse_read) == 0: pass pulse_read.pause() print("1:", pulse_read[0]) pulse_read.clear() pulse_read.resume(60) pwm.duty_cycle = 900 # print(2 ** 14) while len(pulse_read) == 0: pass pulse_read.pause() print("2:", pulse_read[0]) time.sleep(2) pulse_read.clear() pulse_read.resume(60)
# (re-)set digital pin modes esp_init_pin_modes(ESP_D_R_PIN, ESP_D_W_PIN) def esp_init_pin_modes(din, dout): # ESP32 Digital Input esp.set_pin_mode(din, 0x0) # ESP32 Digital Output (no output on pins 34-39) esp.set_pin_mode(dout, 0x1) # M4 R/W Pin Assignments M4_D_W_PIN = DigitalInOut(board.A1) # digital write to ESP_D_R_PIN M4_D_W_PIN.direction = Direction.OUTPUT M4_A_R_PIN = pulseio.PulseIn(board.A0, maxlen=64) # PWM read from ESP_A_W_PIN M4_A_R_PIN.pause() # ESP32 R/W Pin assignments & connections ESP_D_R_PIN = 12 # digital read from M4_D_W_PIN ESP_D_W_PIN = 13 # digital write to Red LED on Feather ESP32 and ESP32 Breakout # ESP32 Analog Input using ADC1 # esp.set_pin_mode(36, 0x0) # Hall Effect Sensor # esp.set_pin_mode(37, 0x0) # Not Exposed # esp.set_pin_mode(38, 0x0) # Not Exposed # esp.set_pin_mode(39, 0x0) # Hall Effect Sensor # esp.set_pin_mode(32, 0x0) # INPUT OK # esp.set_pin_mode(33, 0x0) # DO NOT USE: ESP32SPI Busy/!Rdy # esp.set_pin_mode(34, 0x0) # INPUT OK # esp.set_pin_mode(35, 0x0) # INPUT OK (1/2 of Battery on ESP32 Feather) ESP_A_R_PIN = 32 # analog read from 10k potentiometer
import pulseio pixpin = board.D1 ir_led = board.D0 ir_sensor = board.D2 basket_check_seconds = 0.1 numpix = 60 # NeoPixel LED strip = neopixel.NeoPixel(pixpin, numpix, brightness=1, auto_write=False) # IR LED output for 38kHz PWM pwm = pulseio.PWMOut(ir_led, frequency=38000) # IR Sensor input to detect basketball pulses = pulseio.PulseIn(ir_sensor, maxlen=200, idle_state=True) decoder = adafruit_irremote.GenericDecode() 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 (int(pos * 3), int(255 - (pos * 3)), 0) elif pos < 170: pos -= 85 return (int(255 - pos * 3), 0, int(pos * 3)) pos -= 170
from adafruit_circuitplayground import cp import board import time import pulseio import array # create IR input, maximum of 100 bits. pulseIn = pulseio.PulseIn(board.IR_RX, maxlen=100, idle_state=True) # clears any artifacts pulseIn.clear() pulseIn.resume() # creates IR output pulse pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15) pulseOut = pulseio.PulseOut(pwm) # array for pulse, this is the same pulse output when button a is pressed # inputs are compared against this same array # array.array('H'', [x]) must be used for IR pulse arrays when using pulseio # indented to multiple lines so its easier to see pulseArrayFan = array.array('H', [1296, 377, 1289, 384, 464, 1219, 1296, 379, 1287, 394, 464, 1226, 461, 1229, 458, 1233, 464, 1226, 462, 1229, 456, 1232, 1293, 6922, 1294, 379, 1287, 385, 463, 1220, 1295, 379, 1287, 395, 464, 1225, 461, 1229, 458, 1232, 465, 1225, 462, 1229, 457, 1231, 1294, 6904, 1291, 382, 1295, 377, 461, 1221, 1294, 380, 1296, 385, 464, 1226, 460, 1230, 457, 1233, 464, 1234, 453, 1229, 457, 1230, 1295, 6918, 1287, 386, 1291, 381, 457, 1225, 1289, 385, 1292, 388, 460, 1230, 457, 1232, 465, 1225, 462, 1228, 458, 1232, 465, 1222, 1293, 6905, 1290, 382, 1295, 377, 461, 1221, 1293, 387, 1290, 384, 464, 1225, 461, 1229, 458, 1231, 466, 1224, 463, 1226, 460, 1228, 1286, 6920, 1296, 377, 1289, 382, 466, 1216, 1288, 386, 1291, 390, 458, 1231, 456, 1233, 464, 1226, 460, 1229, 458, 1232, 465, 1222, 1292, 6904, 1291, 382, 1295, 376, 462, 1220,
# half second tones based on button selection [0-3] def play_tone(): if (remote_control_press == 0): simpleio.tone(speaker_pin, 400, .5) # 400Hz beep, 1/2 sec elif (remote_control_press == 1): simpleio.tone(speaker_pin, 500, .5) # 500Hz beep, 1/2 sec elif (remote_control_press == 2): simpleio.tone(speaker_pin, 600, .5) # 600Hz beep, 1/2 sec elif (remote_control_press == 3): simpleio.tone(speaker_pin, 700, .5) # 700Hz beep, 1/2 sec # Create pulse input and IR decoder. pulses = pulseio.PulseIn(ir_pin, maxlen=200, idle_state=True) decoder = adafruit_irremote.GenericDecode() # Loop waiting to receive pulses. while True: # total count of IR code matches for each button {0, 1, 2, 3} match_count = [0] * len(button_press) # make sure pulses is empty pulses.clear() pulses.resume() time.sleep(.1) # Wait for a pulse to be detected. detected = decoder.read_pulses(pulses)
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS gc.collect() import board import pulseio import adafruit_dotstar import adafruit_irremote import time # The keyboard object! time.sleep(1) # Sleep for a bit to avoid a race condition on some systems keyboard = Keyboard() keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) decoder = adafruit_irremote.GenericDecode() pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=100, idle_state=True) # Expected pulse, pasted in from previous recording REPL session: key1_pulses = [0] key2_pulses = [1] print('IR listener') # Fuzzy pulse comparison function: def fuzzy_pulse_compare(pulse1, pulse2, fuzzyness=0.2): if len(pulse1) != len(pulse2): return False for i in range(len(pulse1)): threshold = int(pulse1[i] * fuzzyness)
import board import time import pulseio import adafruit_irremote from adafruit_circuitplayground import cp irIn = pulseio.PulseIn(board.A1, maxlen=120, idle_state=True) decoder = adafruit_irremote.GenericDecode() NEC_PWR = [255, 0, 157, 98] NEC_A = [255, 0, 221, 34] PURPLE = (255, 0, 255) CLEAR = (0, 0, 0) print(int(time.monotonic())) Value = time.monotonic() time.sleep(1) T = int(time.monotonic() - Value) R = int(125 / T) G = int(60 / T) B = int(T * 5) X = T - 30 color = CLEAR