/ Owl_IR_TV_Remote / code.py
code.py
1 # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 4 import array 5 import pulseio 6 import board 7 from digitalio import DigitalInOut, Direction, Pull 8 from adafruit_debouncer import Debouncer 9 import neopixel 10 11 # button setup with Debouncer 12 pin = DigitalInOut(board.A2) 13 pin.direction = Direction.INPUT 14 pin.pull = Pull.UP 15 button = Debouncer(pin) 16 17 # button LED 18 led = DigitalInOut(board.A1) 19 led.direction = Direction.OUTPUT 20 21 # onboard neopixel 22 pix = board.NEOPIXEL 23 num_pixels = 1 24 pixel = neopixel.NeoPixel(pix, num_pixels, brightness=0.8, auto_write=False) 25 26 # PWM setup for IR LEDs 27 remote = pulseio.PulseOut(board.TX, frequency=38000, duty_cycle=2**15) 28 # power on pulse array 29 # Prevent black from reformatting the arrays. 30 # fmt: off 31 power_on = array.array('H', [9027, 4490, 577, 563, 549, 1677, 579, 1674, 582, 558, 32 554, 559, 553, 561, 551, 562, 551, 1675, 580, 1674, 572, 33 567, 555, 1672, 573, 567, 556, 558, 554, 559, 553, 560, 34 552, 562, 550, 1675, 581, 560, 552, 561, 552, 561, 551, 35 563, 549, 1677, 579, 1674, 581, 560, 552, 561, 552, 1674, 36 581, 1673, 573, 1680, 575, 1679, 577, 563, 549, 565, 547, 37 1679, 577]) 38 # power off pulse array 39 power_off = array.array('H', [9028, 4491, 576, 563, 549, 1678, 578, 1701, 554, 533, 40 579, 561, 551, 562, 551, 536, 576, 1703, 552, 1700, 556, 41 558, 554, 1698, 547, 540, 582, 558, 554, 532, 580, 560, 42 552, 561, 552, 562, 550, 563, 549, 564, 548, 565, 547, 43 566, 546, 1707, 549, 1704, 551, 562, 550, 1703, 553, 1699, 44 556, 1697, 548, 1705, 551, 1701, 554, 560, 553, 560, 552, 45 1701, 554]) 46 # fmt: on 47 # array of the pulses 48 signals = [power_on, power_off] 49 # neopixel colors 50 RED = (255, 0, 0) 51 GREEN = (0, 255, 0) 52 # array of colors 53 colors = [GREEN, RED] 54 # index variable 55 s = 0 56 57 while True: 58 # scan button for update 59 button.update() 60 # if the button is pressed.. 61 if button.fell: 62 # send the pulse 63 remote.send(signals[s]) 64 # update onboard neopixel 65 pixel.fill(colors[s]) 66 pixel.show() 67 # turn on button LED 68 led.value = True 69 # advance the index variable 70 s = (s + 1) % 2 71 # if the button is released.. 72 if button.rose: 73 # turn off the button LED 74 led.value = False