|
| 1 | +# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import math |
| 6 | +import board |
| 7 | +import busio |
| 8 | +from digitalio import DigitalInOut |
| 9 | +import neopixel |
| 10 | +from adafruit_esp32spi import adafruit_esp32spi |
| 11 | +from adafruit_esp32spi import adafruit_esp32spi_wifimanager |
| 12 | +import displayio |
| 13 | +import adafruit_touchscreen |
| 14 | +import adafruit_imageload |
| 15 | + |
| 16 | +# Set up the touchscreen |
| 17 | +ts = adafruit_touchscreen.Touchscreen( |
| 18 | + board.TOUCH_XL, |
| 19 | + board.TOUCH_XR, |
| 20 | + board.TOUCH_YD, |
| 21 | + board.TOUCH_YU, |
| 22 | + calibration=((5200, 59000), (5800, 57000)), |
| 23 | + size=(320, 240), |
| 24 | +) |
| 25 | + |
| 26 | +# Get wifi details and more from a secrets.py file |
| 27 | +try: |
| 28 | + from secrets import secrets |
| 29 | +except ImportError: |
| 30 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 31 | + raise |
| 32 | + |
| 33 | +# If you are using a board with pre-defined ESP32 Pins: |
| 34 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 35 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 36 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 37 | + |
| 38 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 39 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 40 | + |
| 41 | +status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
| 42 | + |
| 43 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 44 | + |
| 45 | +# Set the ip of your Roku here |
| 46 | +ip = "192.168.1.3" |
| 47 | + |
| 48 | +""" |
| 49 | +Possible keypress key values to send the Roku |
| 50 | +Home |
| 51 | +Rev |
| 52 | +Fwd |
| 53 | +Play |
| 54 | +Select |
| 55 | +Left |
| 56 | +Right |
| 57 | +Down |
| 58 | +Up |
| 59 | +Back |
| 60 | +InstantReplay |
| 61 | +Info |
| 62 | +Backspace |
| 63 | +Search |
| 64 | +Enter |
| 65 | +FindRemote |
| 66 | +
|
| 67 | +Keypress key values that only work on smart TVs with built-in Rokus |
| 68 | +VolumeDown |
| 69 | +VolumeMute |
| 70 | +VolumeUp |
| 71 | +PowerOff |
| 72 | +ChannelUp |
| 73 | +ChannelDown |
| 74 | +""" |
| 75 | + |
| 76 | + |
| 77 | +def getchannels(): |
| 78 | + """ Gets the channels installed on the device. Also useful because it |
| 79 | + verifies that the PyPortal can see the Roku""" |
| 80 | + try: |
| 81 | + print("Getting channels. Usually takes around 10 seconds...", end="") |
| 82 | + response = wifi.get("http://{}:8060/query/apps".format(ip)) |
| 83 | + channel_dict = {} |
| 84 | + for i in response.text.split("<app")[2:]: |
| 85 | + a = i.split("=") |
| 86 | + chan_id = a[1].split('"')[1] |
| 87 | + name = a[-1].split(">")[1].split("<")[0] |
| 88 | + channel_dict[name] = chan_id |
| 89 | + response.close() |
| 90 | + return channel_dict |
| 91 | + except (ValueError, RuntimeError) as e: |
| 92 | + print("Failed to get data\n", e) |
| 93 | + wifi.reset() |
| 94 | + return None |
| 95 | + response = None |
| 96 | + return None |
| 97 | + |
| 98 | + |
| 99 | +def sendkey(key): |
| 100 | + """ Sends a key to the Roku """ |
| 101 | + try: |
| 102 | + print("Sending key: {}...".format(key), end="") |
| 103 | + response = wifi.post("http://{}:8060/keypress/{}".format(ip, key)) |
| 104 | + if response: |
| 105 | + response.close() |
| 106 | + print("OK") |
| 107 | + except (ValueError, RuntimeError) as e: |
| 108 | + print("Failed to get data\n", e) |
| 109 | + wifi.reset() |
| 110 | + return |
| 111 | + response = None |
| 112 | + |
| 113 | + |
| 114 | +def sendletter(letter): |
| 115 | + """ Sends a letter to the Roku, not used in this guide """ |
| 116 | + try: |
| 117 | + print("Sending letter: {}...".format(letter), end="") |
| 118 | + response = wifi.post("http://{}:8060/keypress/lit_{}".format(ip, letter)) |
| 119 | + if response: |
| 120 | + response.close() |
| 121 | + print("OK") |
| 122 | + except (ValueError, RuntimeError) as e: |
| 123 | + print("Failed to get data\n", e) |
| 124 | + wifi.reset() |
| 125 | + return |
| 126 | + response = None |
| 127 | + |
| 128 | + |
| 129 | +def openchannel(channel): |
| 130 | + """ Tells the Roku to open the channel with the corresponding channel id """ |
| 131 | + try: |
| 132 | + print("Opening channel: {}...".format(channel), end="") |
| 133 | + response = wifi.post("http://{}:8060/launch/{}".format(ip, channel)) |
| 134 | + if response: |
| 135 | + response.close() |
| 136 | + print("OK") |
| 137 | + response = None |
| 138 | + except (ValueError, RuntimeError): |
| 139 | + print("Probably worked") |
| 140 | + wifi.reset() |
| 141 | + response = None |
| 142 | + |
| 143 | + |
| 144 | +def switchpage(tup): |
| 145 | + """ Used to switch to a different page """ |
| 146 | + p_num = tup[0] |
| 147 | + tile_grid = tup[1] |
| 148 | + new_page = pages[p_num - 1] |
| 149 | + new_page_vals = pages_vals[p_num - 1] |
| 150 | + my_display_group[-1] = tile_grid |
| 151 | + return new_page, new_page_vals |
| 152 | + |
| 153 | + |
| 154 | +# Verifies the Roku and Pyportal are connected and visible |
| 155 | +channels = getchannels() |
| 156 | + |
| 157 | +my_display_group = displayio.Group(max_size=25) |
| 158 | + |
| 159 | +image_1, palette_1 = adafruit_imageload.load( |
| 160 | + "images/page_1.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette |
| 161 | +) |
| 162 | +tile_grid_1 = displayio.TileGrid(image_1, pixel_shader=palette_1) |
| 163 | +my_display_group.append(tile_grid_1) |
| 164 | + |
| 165 | +image_2, palette_2 = adafruit_imageload.load( |
| 166 | + "images/page_2.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette |
| 167 | +) |
| 168 | +tile_grid_2 = displayio.TileGrid(image_2, pixel_shader=palette_2) |
| 169 | + |
| 170 | +image_3, palette_3 = adafruit_imageload.load( |
| 171 | + "images/page_3.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette |
| 172 | +) |
| 173 | +tile_grid_3 = displayio.TileGrid(image_3, pixel_shader=palette_3) |
| 174 | + |
| 175 | +# fmt: off |
| 176 | +# Page 1 |
| 177 | +page_1 = [sendkey, sendkey, sendkey, |
| 178 | + sendkey, sendkey, sendkey, |
| 179 | + sendkey, sendkey, sendkey, |
| 180 | + sendkey, sendkey, switchpage] |
| 181 | + |
| 182 | +page_1_vals = ["Back", "Up", "Home", |
| 183 | + "Left", "Select", "Right", |
| 184 | + "Search", "Down", "Info", |
| 185 | + "Rev", "Play", (2, tile_grid_2)] |
| 186 | + |
| 187 | +# Page 2 |
| 188 | +page_2 = [openchannel, openchannel, openchannel, |
| 189 | + openchannel, openchannel, openchannel, |
| 190 | + openchannel, openchannel, openchannel, |
| 191 | + switchpage, sendkey, switchpage] |
| 192 | + |
| 193 | +page_2_vals = [14362, 2285, 13, |
| 194 | + 12, 8378, 837, |
| 195 | + 38820, 47389, 7767, |
| 196 | + (1, tile_grid_1), "Home", (3, tile_grid_3)] |
| 197 | + |
| 198 | +page_3 = [None, None, None, |
| 199 | + sendkey, None, sendkey, |
| 200 | + sendkey, sendkey, sendkey, |
| 201 | + switchpage, sendkey, sendkey] |
| 202 | + |
| 203 | +page_3_vals = [None, None, None, |
| 204 | + "Search", None, "Info", |
| 205 | + "Rev", "Play", "Fwd", |
| 206 | + (2, tile_grid_2), "Back", "Home"] |
| 207 | +# fmt: on |
| 208 | + |
| 209 | +pages = [page_1, page_2, page_3] |
| 210 | +pages_vals = [page_1_vals, page_2_vals, page_3_vals] |
| 211 | + |
| 212 | +page = page_1 |
| 213 | +page_vals = page_1_vals |
| 214 | + |
| 215 | +board.DISPLAY.show(my_display_group) |
| 216 | +print("READY") |
| 217 | + |
| 218 | + |
| 219 | +last_index = 0 |
| 220 | +while True: |
| 221 | + p = ts.touch_point |
| 222 | + if p: |
| 223 | + x = math.floor(p[0] / 80) |
| 224 | + y = abs(math.floor(p[1] / 80) - 2) |
| 225 | + index = 3 * x + y |
| 226 | + # Used to prevent the touchscreen sending incorrect results |
| 227 | + if last_index == index: |
| 228 | + if page[index]: |
| 229 | + # pylint: disable=comparison-with-callable |
| 230 | + if page[index] == switchpage: |
| 231 | + page, page_vals = switchpage(page_vals[index]) |
| 232 | + else: |
| 233 | + page[index](page_vals[index]) |
| 234 | + time.sleep(0.1) |
| 235 | + |
| 236 | + last_index = index |
0 commit comments