I have a lot of boards and usually need to re-use code even for different projects.

code.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from keyb import Keyboard # type: ignore
  2. import json, wifi, os, socketpool, asyncio, sys, select # type: ignore
  3. import board, busio, terminalio, displayio # type: ignore
  4. from adafruit_display_text import label # type: ignore
  5. from adafruit_display_shapes.rect import Rect # type: ignore
  6. async def main():
  7. keyb_task = asyncio.create_task(keyb_scan())
  8. socket_task = asyncio.create_task(socket_recv())
  9. await asyncio.gather(keyb_task, socket_task)
  10. async def keyb_scan():
  11. while True:
  12. if select.select([sys.stdin,],[],[],0.1)[0]:
  13. code=hex(ord(sys.stdin.readline(1)))
  14. key = keyb.scan(keycode = code)
  15. if keyb.is_alphanumeric(key):
  16. if len(textbar.text) < 31:
  17. textbar.text += str(key)
  18. elif key == "\n":
  19. a = {"header":"message",
  20. "message":textbar.text,
  21. "client name":client_name}
  22. data = json.dumps(a)
  23. sock.sendto(data, address)
  24. textbar.text = ""
  25. elif key == "BACKSP":
  26. textbar.text = textbar.text[:-1]
  27. elif key == " ":
  28. if len(textbar.text) < 31:
  29. textbar.text += " "
  30. else:
  31. await asyncio.sleep(0.1)
  32. async def socket_recv():
  33. while True:
  34. try:
  35. buffer = bytearray(200)
  36. size, addr = sock.recvfrom_into(buffer)
  37. try:
  38. a = json.loads(buffer)
  39. header = a["header"]
  40. if header == "response":
  41. msg = a["message"]
  42. try:
  43. color = a["color"]
  44. except KeyError:
  45. color = 0xffffff
  46. try:
  47. recv_client_name = a["client name"]
  48. except KeyError:
  49. recv_client_name = "(unknown)"
  50. cmsg = recv_client_name + " >> " + msg
  51. messages_list.append([cmsg, color])
  52. update_gui()
  53. elif header == "connection":
  54. motd = a["motd"]
  55. conn_client = a["client name"]
  56. try:
  57. color = a["color"]
  58. except KeyError:
  59. color = 0xffffff
  60. con_msg = ">> " + conn_client + " <<"
  61. messages_list.append([con_msg, color])
  62. update_gui()
  63. except Exception as e:
  64. error_msg = str(e) + " (buffer size?)"
  65. color = 0xff0000
  66. messages_list.append([error_msg, color])
  67. update_gui()
  68. except OSError:
  69. pass
  70. await asyncio.sleep(0.1)
  71. def update_gui():
  72. x = 0
  73. for message in messages_list[-max_messages:]:
  74. messages_list_labels[x].text = message[0]
  75. messages_list_labels[x].color = message[1]
  76. x += 1
  77. keyb = Keyboard()
  78. # SET UP DISPLAY & WIFI
  79. display = board.DISPLAY
  80. display_group = displayio.Group()
  81. display.root_group = display_group
  82. wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
  83. [os.getenv("WIFI2"),os.getenv("WIFIPW2")],
  84. [os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
  85. h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
  86. h1.x = 10
  87. h1.y = 15
  88. count = 0
  89. menu_labs = []
  90. for option in wifi_options:
  91. if count == 0:
  92. menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
  93. else:
  94. menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
  95. menu_lab.x = 10
  96. menu_lab.y = 35 + count * 10
  97. display.root_group.append(menu_lab)
  98. menu_labs.append(menu_lab)
  99. count+=1
  100. display.root_group.append(h1)
  101. pos = 0
  102. while True:
  103. key = keyb.scan()
  104. if key == "UP":
  105. if pos == 0:
  106. pass
  107. else:
  108. menu_labs[pos].text = wifi_options[pos][0]
  109. pos -= 1
  110. menu_labs[pos].text = "> " + wifi_options[pos][0]
  111. elif key == "DOWN":
  112. if pos == len(wifi_options)-1:
  113. pass
  114. else:
  115. menu_labs[pos].text = wifi_options[pos][0]
  116. pos += 1
  117. menu_labs[pos].text = "> " + wifi_options[pos][0]
  118. elif key == "\n":
  119. WIFI_SSID = wifi_options[pos][0]
  120. WIFI_PASS = wifi_options[pos][1]
  121. break
  122. try:
  123. for lab in menu_labs:
  124. display.root_group.remove(lab)
  125. txt = label.Label(terminalio.FONT, text = "", color=0x00ff00, scale=1)
  126. txt.text = "Connecting to " + WIFI_SSID + "..."
  127. txt.x = 10
  128. txt.y = 35
  129. display.root_group.append(txt)
  130. wifi.radio.connect(ssid=WIFI_SSID,
  131. password=WIFI_PASS)
  132. my_ip = str(wifi.radio.ipv4_address).strip()
  133. display.root_group.remove(h1)
  134. display.root_group.remove(txt)
  135. except ConnectionError as e:
  136. h1.text = "Aw..."
  137. txt.text = "Could not connect to " + WIFI_SSID
  138. txt.color = 0xff0000
  139. txt.x = 10
  140. txt.y = 35
  141. txt2 = label.Label(terminalio.FONT, text = "", color=0x89a0a8, scale=1)
  142. txt2.text = "Press CTRL + C followed by CTRL + D\nto soft-reboot"
  143. txt2.x = 10
  144. txt2.y = 100
  145. display.root_group.append(txt2)
  146. while True:
  147. pass
  148. # Header Bar
  149. header_bar = Rect(0, 0, 238, 30, fill=0x1f4476)
  150. display.root_group.append(header_bar)
  151. # Page Title
  152. title_label = label.Label(terminalio.FONT, text="Chat", color=0xffffff, scale=2)
  153. display.root_group.append(title_label)
  154. title_label.x = 10
  155. title_label.y = 15
  156. # IP Label
  157. ip_label = label.Label(terminalio.FONT, text=my_ip, color=0x89a0a8, scale=1)
  158. display.root_group.append(ip_label)
  159. ip_label.x = 150
  160. ip_label.y = 20
  161. pool = socketpool.SocketPool(wifi.radio)
  162. sock = pool.socket(pool.AF_INET, pool.SOCK_DGRAM)
  163. sock.settimeout(3)
  164. sock.setblocking(False)
  165. ip = "cubes.link"
  166. port = 1337
  167. address = (ip, port)
  168. client_name = "CARDPUTER"
  169. a = {"header":"connection",
  170. "client name":client_name}
  171. data = json.dumps(a)
  172. sock.sendto(data, address)
  173. textbar = label.Label(terminalio.FONT, text = "", color = 0x89a0a8, scale=1)
  174. display.root_group.append(textbar)
  175. textbar.x = 10
  176. textbar.y = 120
  177. messages_list = []
  178. messages_list_labels = []
  179. max_messages = 8
  180. for x in range(0, max_messages):
  181. mlab = label.Label(terminalio.FONT, text="", color=0xffffff, scale=1)
  182. mlab.x = 8
  183. mlab.y = 38 + (x * 10)
  184. display.root_group.append(mlab)
  185. messages_list_labels.append(mlab)
  186. asyncio.run(main())