/ PipBoy_2040 / code.py
code.py
  1  # SPDX-FileCopyrightText: 2021 john park for Adafruit Industries
  2  # SPDX-License-Identifier: MIT
  3  import time
  4  import board
  5  from adafruit_simplemath import map_range
  6  import displayio
  7  from adafruit_seesaw.seesaw import Seesaw
  8  import adafruit_imageload
  9  from adafruit_st7789 import ST7789
 10  
 11  displayio.release_displays()
 12  
 13  i2c_bus = board.I2C()
 14  ss = Seesaw(i2c_bus)
 15  
 16  spi = board.SPI()  # setup for display over SPI
 17  tft_cs = board.D5
 18  tft_dc = board.D6
 19  display_bus = displayio.FourWire(
 20      spi, command=tft_dc, chip_select=tft_cs, reset=board.D9
 21  )
 22  
 23  display = ST7789(display_bus, width=280, height=240, rowstart=20, rotation=270)
 24  
 25  screen = displayio.Group()  # Create a Group to hold content
 26  display.show(screen)  # Add it to the Display
 27  
 28  # display image
 29  image = displayio.OnDiskBitmap("/img/bootpip0.bmp")
 30  palette = image.pixel_shader
 31  background = displayio.TileGrid(image, pixel_shader=palette)
 32  screen.append(background)
 33  
 34  # load cursor on top
 35  cursor_on = True
 36  if cursor_on:
 37      image, palette = adafruit_imageload.load("/img/cursor_green.bmp")
 38      palette.make_transparent(0)
 39      cursor = displayio.TileGrid(image, pixel_shader=palette)
 40      screen.append(cursor)
 41  
 42      cursor.x = 0  # hide cursor during bootup
 43      cursor.y = 0
 44  
 45  display.show(screen)
 46  
 47  boot_file_names = [
 48      "/img/bootpip0.bmp",
 49      "/img/bootpip1.bmp",
 50      "/img/bootpip2.bmp",
 51      "/img/bootpip3.bmp",
 52      "/img/bootpip4.bmp",
 53      "/img/bootpip5.bmp",
 54      "/img/statpip0.bmp",
 55  ]
 56  
 57  screenmap = {
 58      (0): (
 59          "/img/statpip0.bmp",
 60          "/img/statpip1.bmp",
 61          "/img/statpip2.bmp",
 62          "/img/statpip3.bmp",
 63          "/img/statpip4.bmp",
 64          "/img/statpip2.bmp",
 65          "/img/statpip6.bmp",
 66          "/img/statpip7.bmp",
 67          "/img/statpip8.bmp",
 68      ),
 69      (1): ("/img/invpip0.bmp", "/img/invpip1.bmp"),
 70      (2): ("/img/datapip0.bmp", "/img/datapip1.bmp", "/img/datapip2.bmp"),
 71      (3): ("/img/mappip0.bmp", "/img/mappip1.bmp", "/img/mappip2.bmp"),
 72      (4): ("/img/radiopip0.bmp", "/img/radiopip1.bmp"),
 73      (5): ("/img/holopip0.bmp", "/img/holopip1.bmp"),
 74  }
 75  
 76  BUTTON_UP = 6  # A is UP
 77  BUTTON_RIGHT = 7  # B is RIGHT
 78  BUTTON_DOWN = 9  # Y is DOWN
 79  BUTTON_LEFT = 10  # X is LEFT
 80  BUTTON_SEL = 14  # SEL button is unused
 81  button_mask = (
 82      (1 << BUTTON_UP)
 83      | (1 << BUTTON_RIGHT)
 84      | (1 << BUTTON_DOWN)
 85      | (1 << BUTTON_LEFT)
 86      | (1 << BUTTON_SEL)
 87  )
 88  
 89  ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)
 90  
 91  tab_number = 0
 92  sub_number = 0
 93  
 94  def image_switch(direction):  # advance or go back through image list
 95      # pylint: disable=global-statement
 96      global tab_number
 97      # pylint: disable=global-statement
 98      global sub_number
 99      # pylint: disable=global-statement
100      global image
101      # pylint: disable=global-statement
102      global palette
103      if direction == 0:  # right
104          tab_number = (tab_number + 1) % len(screenmap)
105      if direction == 1:  # left
106          tab_number = (tab_number - 1) % len(screenmap)
107      if direction == 2:  # down
108          sub_number = (sub_number + 1) % len((screenmap[tab_number]))
109      if direction == 3:  # up
110          sub_number = (sub_number - 1) % len((screenmap[tab_number]))
111  
112      image = displayio.OnDiskBitmap(screenmap[tab_number][sub_number])
113      palette = image.pixel_shader
114      screen[0] = displayio.TileGrid(image, pixel_shader=palette)
115  
116  
117  last_joy_x = 0
118  last_joy_y = 0
119  
120  #  bootup images
121  for i in range(len(boot_file_names)):
122      image = displayio.OnDiskBitmap(boot_file_names[i])
123      palette = image.pixel_shader
124      screen[0] = displayio.TileGrid(image, pixel_shader=palette)
125      time.sleep(0.1)
126  
127  while True:
128      time.sleep(0.01)
129      joy_x = ss.analog_read(2)
130      joy_y = ss.analog_read(3)
131      if (abs(joy_x - last_joy_x) > 3) or (abs(joy_y - last_joy_y) > 3):
132          if cursor_on:
133              cursor.x = int(map_range(joy_x, 10, 1023, 0, 264))
134              cursor.y = int(map_range(joy_y, 10, 1023, 224, 0))
135          last_joy_x = joy_x
136          last_joy_y = joy_y
137  
138      buttons = ss.digital_read_bulk(button_mask)
139  
140      if not buttons & (1 << BUTTON_UP):
141          image_switch(3)
142          time.sleep(0.15)
143  
144      if not buttons & (1 << BUTTON_RIGHT):
145          sub_number = 0  # go back to top level screen of tab grouping
146          image_switch(0)
147          time.sleep(0.15)
148  
149      if not buttons & (1 << BUTTON_DOWN):
150          image_switch(2)
151          time.sleep(0.15)
152  
153      if not buttons & (1 << BUTTON_LEFT):
154          sub_number = 0
155          image_switch(1)
156          time.sleep(0.15)
157  
158      if not buttons & (1 << BUTTON_SEL):
159          print("unused select button")