/ BLE_TV_B_Gone / code.py
code.py
  1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  # Circuit Playground Bluefruit version 2022 John Park
  5  
  6  import array
  7  import time
  8  import board
  9  import pulseio
 10  from digitalio import DigitalInOut, Direction, Pull
 11  from adafruit_ble import BLERadio
 12  from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
 13  from adafruit_ble.services.nordic import UARTService
 14  from adafruit_bluefruit_connect.packet import Packet
 15  from adafruit_bluefruit_connect.button_packet import ButtonPacket
 16  
 17  # pylint: disable=eval-used
 18  # Switch to select 'stealth-mode'
 19  switch = DigitalInOut(board.SLIDE_SWITCH)
 20  switch.direction = Direction.INPUT
 21  switch.pull = Pull.UP
 22  # Button to see output debug
 23  led = DigitalInOut(board.D13)
 24  led.direction = Direction.OUTPUT
 25  # which pin for IR LED/blaster
 26  ir_pin = board.A2  # JST IR Blaster board
 27  # Speaker as haptic feedback
 28  spkr_en = DigitalInOut(board.SPEAKER_ENABLE)
 29  spkr_en.direction = Direction.OUTPUT
 30  spkr_en.value = True
 31  spkr = DigitalInOut(board.SPEAKER)
 32  spkr.direction = Direction.OUTPUT
 33  
 34  # Allow any button to trigger activity!
 35  button_a = DigitalInOut(board.BUTTON_A)
 36  button_a.direction = Direction.INPUT
 37  button_a.pull = Pull.DOWN
 38  button_b = DigitalInOut(board.BUTTON_B)
 39  button_b.direction = Direction.INPUT
 40  button_b.pull = Pull.DOWN
 41  
 42  # BLE setup
 43  ble = BLERadio()
 44  uart_service = UARTService()
 45  advertisement = ProvideServicesAdvertisement(uart_service)
 46  
 47  def ir_code_send(code):
 48      f = open(code, "r")
 49      for line in f:
 50          code = eval(line)
 51          print(code)
 52          if switch.value:
 53              led.value = True
 54          else:
 55              spkr.value = True
 56          # If this is a repeating code, extract details
 57          try:
 58              repeat = code["repeat"]
 59              delay = code["repeat_delay"]
 60          except KeyError:  # by default, repeat once only!
 61              repeat = 1
 62              delay = 0
 63          # The table holds the on/off pairs
 64          table = code["table"]
 65          pulses = []  # store the pulses here
 66          # Read through each indexed element
 67          for i in code["index"]:
 68              pulses += table[i]  # and add to the list of pulses
 69          pulses.pop()  # remove one final 'low' pulse
 70  
 71          with pulseio.PulseOut(
 72              ir_pin, frequency=code["freq"], duty_cycle=2**15
 73          ) as pulse:
 74              for i in range(repeat):
 75                  pulse.send(array.array("H", pulses))
 76                  time.sleep(delay)
 77  
 78          led.value = False
 79          spkr.value = False
 80          time.sleep(code["delay"])
 81  
 82      f.close()
 83  
 84  
 85  while True:
 86      ble.name = 'TVRemote'
 87      ble.start_advertising(advertisement)
 88  
 89      while not ble.connected:
 90          # Wait for a connection.
 91          if button_a.value or button_b.value:
 92              print("All codes")
 93              time.sleep(0.1)  # wait a moment
 94              ir_code_send("/full_codes.txt")
 95  
 96      while ble.connected:
 97          if button_a.value or button_b.value:
 98              print("all")
 99              time.sleep(0.1)  # wait a moment
100              ir_code_send("/full_codes.txt")
101          if uart_service.in_waiting:
102              # Packet is arriving.
103              packet = Packet.from_stream(uart_service)
104              if isinstance(packet, ButtonPacket) and packet.pressed:
105                  if packet.button == ButtonPacket.UP:
106                      print("Select codes")
107                      time.sleep(0.1)  # wait a moment
108                      ir_code_send("/codes.txt")
109  
110                  if packet.button == ButtonPacket.DOWN:
111                      print("All codes")
112                      time.sleep(0.1)  # wait a moment
113                      ir_code_send("/full_codes.txt")
114  
115                  elif packet.button == ButtonPacket.BUTTON_1:
116                      print("Sony power")
117                      time.sleep(0.1)  # wait a moment
118                      ir_code_send("/sony_pwr.txt")
119  
120                  elif packet.button == ButtonPacket.BUTTON_2:
121                      print("Toshiba power")
122                      time.sleep(0.1)  # wait a moment
123                      ir_code_send("/toshiba_pwr.txt")