Skip to content

Commit

Permalink
snekde: Support 57600 and 115200 baud
Browse files Browse the repository at this point in the history
8MHz atmega devices cannot use 115200 baud because the clock
resolution isn't fine enough to get a close match in bit rate. So,
those devices will run at 57600 baud instead. Support this by adding a
baud rate selection key (F8) which switches between 115200 and 57600
baud. Display this in the status bar along with the device name.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Feb 13, 2022
1 parent ab9f6db commit 61e0017
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions snekde/snekde.py
Expand Up @@ -49,6 +49,10 @@

snek_debug_file = False

baud_rates = [57600, 115200]

baud_selection = 1


def snek_debug(message):
global snek_debug_file
Expand Down Expand Up @@ -185,7 +189,7 @@ class SnekDevice:
# that gets data that are read
#

def __init__(self, port, interface):
def __init__(self, port, interface, rate=115200):
self.interface = interface
self.device = port.device

Expand All @@ -195,7 +199,6 @@ def __init__(self, port, interface):
self.synchronous_put = True
self.synchronous_limit = 16

rate = 115200
for port_mod in port_mods:
if port_mod in port.description or port_mod in port.hwid:
if "async" in port_mods[port_mod]:
Expand Down Expand Up @@ -345,6 +348,12 @@ def write(self, data):
def command(self, data, intr="\x03"):
self.write("\x0e" + intr + data)

def set_baud(self, baud):
self.serial.baudrate = baud
if self.serial.is_open:
self.serial.close()
self.serial.open()


class EditWin:
"""Editable text object"""
Expand Down Expand Up @@ -1112,6 +1121,7 @@ def screen_get_sizes():
("F5", "Load"),
("F6", "Save"),
("F7", "Switch"),
("F8", "Rate"),
)

# Paint the function key help text and the separator line
Expand All @@ -1128,6 +1138,7 @@ def screen_paint():
device_name = "<no device>"
if snek_device:
device_name = snek_device.device
device_name = "%s %d" % (device_name, baud_rates[baud_selection])
device_col = snek_cols - len(device_name)
if device_col < 0:
device_col = 0
Expand Down Expand Up @@ -1190,14 +1201,22 @@ def screen_fini():
curses.endwin()


def switch_baud():
global snek_device, baud_selection, baud_rates
baud_selection = (baud_selection + 1) % len(baud_rates)
if snek_device:
snek_device.set_baud(baud_rates[baud_selection])
screen_paint()


def snekde_open_device():
global snek_device, snek_monitor
dialog = GetPortWin()
port = dialog.run_dialog()
if not port:
return
try:
device = SnekDevice(port, snek_monitor)
device = SnekDevice(port, snek_monitor, rate=baud_rates[baud_selection])
device.start()
if snek_device:
snek_device.close()
Expand Down Expand Up @@ -1333,6 +1352,8 @@ def run():
snek_current_window = snek_repl_win
else:
snek_current_window = snek_edit_win
elif ch == curses.KEY_F8 or ch == ord("8") | 0x80:
switch_baud()
elif ch == ord("\n"):
if snek_current_window is snek_edit_win:
snek_current_window.dispatch(ch)
Expand Down

0 comments on commit 61e0017

Please sign in to comment.