Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jtornero/864b6babfcf51c5d595481f9092beb84 to your computer and use it in GitHub Desktop.
Save jtornero/864b6babfcf51c5d595481f9092beb84 to your computer and use it in GitHub Desktop.
Circuitpython keyboard with telnet and beep
import keypad
import digitalio
import board
import asyncio
import os
import socketpool
import wifi
import ipaddress
import time # for testing
# Getting preconfigured connection parameters
WIFI_SSID = os.getenv("WIFI_SSID")
WIFI_PASS = os.getenv("WIFI_PASS")
ICROS_IP = os.getenv("ICROS_IP")
ICROS_ID = os.getenv("ICROS_ID")
ICROS_PORT = os.getenv("ICROS_PORT")
# Definition of some pins (Buzzer, display)
buzzer = digitalio.DigitalInOut(board.GP13)
buzzer.direction = digitalio.Direction.OUTPUT
# Keyboard definition
key_matrix = keypad.KeyMatrix(
row_pins=(
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP6,
board.GP7,
board.GP8,
board.GP9,
board.GP10,
board.GP11,
board.GP12,
),
column_pins=(
board.GP28,
board.GP27,
board.GP26,
board.GP22,
board.GP21,
board.GP20,
board.GP19,
board.GP18,
board.GP17,
board.GP16,
board.GP15,
board.GP14,
),
)
measures = [
"NIMP", "UND", "FEM", "DEL", "FUN", "MALE", 600, 590, 580, 570, 560, 550,
540, 530, 520, 510, 500, 490, 480, 470, 460, 450, 440,
430, 420, 410, 400, 390, 380, 370, 360, 350, 340, 330,
320, 310, 300, 290, 280, 270, 260, 250, 240, 230, 220,
210, 200, 190, 180, 170, 160, 150, 140, 130, 120, 110,
100, 90, 80, 70, 60, 50, 40, 30, 20, 115, 105, 95,
85, 75, 65, 55, 45, 35, 25, 125, 235, 225, 215, 205,
195, 185, 175, 165, 155, 145, 135, 245, 355, 345, 335,
325, 315, 305, 295, 285, 275, 265, 255, 365, 475, 465,
455, 445, 435, 425, 415, 405, 395, 385, 375, 485, 595,
585, 575, 565, 555, 545, 535, 525, 515, 505, 495
]
### ACOUSTIC SIGNALS
async def singleBeep():
print('SINGLE')
buzzer.value = 1
await asyncio.sleep(0.1)
buzzer.value = 0
await asyncio.sleep(0.1)
async def doubleBeep():
print('DOUBLE')
buzzer.value = 1
await asyncio.sleep(0.1)
buzzer.value = 0
await asyncio.sleep(0.1)
buzzer.value = 1
await asyncio.sleep(0.1)
buzzer.value = 0
await asyncio.sleep(0.1)
### WIFI HANDLING
# Configure Wi-Fi connection
def startWifi():
print(1)
wifi.radio.enabled = True
print(2)
wifi.radio.stop_dhcp()
print(3)
wifi.radio.hostname = ICROS_ID
print(4)
ipv4 = ipaddress.IPv4Address(ICROS_IP)
netmask = ipaddress.IPv4Address("255.255.255.0")
gateway = ipaddress.IPv4Address(ICROS_IP)
print(5)
wifi.radio.set_ipv4_address(ipv4=ipv4, netmask=netmask, gateway=gateway)
print(6)
wifi.radio.connect(WIFI_SSID, WIFI_PASS)
print(7)
print("Wi-Fi connection established:", wifi.radio.ipv4_address)
def stopWifi():
wifi.radio.enabled = False
startWifi()
# Configure Telnet server socket
pool = socketpool.SocketPool(wifi.radio)
async def handle_telnet(client_socket):
# client_socket.sendall(b"Welcome to the Telnet server on MicroPython!\n")
while True:
try:
start_data = bytearray(100)
client_socket.recv_into(start_data, 100)
print('inside', start_data)
except Exception as e:
print(e, start_data)
break
while True:
event = key_matrix.events.get()
if event is not None:
if event.pressed == True:
print(event.key_number, event.pressed, event.released, event.timestamp)
pulsa = event.timestamp
elif event.released == True:
if (event.timestamp - pulsa) > 500:
try:
client_socket.sendall(b'MEAS;D;1;%s\r\n' % (measures[event.key_number]))
data = bytearray(10)
client_socket.recv_into(data)
msg = data.decode().rstrip('\r\n\x00')
print(msg)
if msg == 'd':
client_socket.sendall(b'ERASED\n')
await doubleBeep()
except BrokenPipeError as e:
print(e, 'No connection')
return
else:
try:
client_socket.sendall(b'MEAS;A;1;%s\r\n' % (measures[event.key_number]))
data = bytearray(10)
client_socket.recv_into(data)
msg = data.decode().rstrip('\r\n\x00')
if msg == 'a':
client_socket.sendall(b'STORED\n')
await singleBeep()
except BrokenPipeError as e:
print(e, 'No connection')
return
# Configure Telnet server socket
telnet_socket = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
telnet_socket.setsockopt(pool.SOL_SOCKET, pool.SO_REUSEADDR, 1)
telnet_socket.bind(("0.0.0.0", ICROS_PORT))
telnet_socket.listen(1)
print("Telnet server waiting for connections on 0.0.0.0:", ICROS_PORT)
# Wait for and handle client connections
async def main():
while True:
client_socket, client_address = telnet_socket.accept()
print("Client connected from:", client_address)
tarea_teclado = asyncio.create_task(handle_telnet(client_socket))
tarea_singlebeep = asyncio.create_task(singleBeep())
tarea_doublebeep = asyncio.create_task(doubleBeep())
print(await asyncio.gather(tarea_teclado, tarea_singlebeep, tarea_doublebeep))
print('x')
client_socket.close()
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment