From 3fa96e64f7a5576cf2188b45c49d32eb8c2e41be Mon Sep 17 00:00:00 2001 From: E4ST2W3ST Date: Sun, 31 Dec 2023 17:58:27 -0600 Subject: [PATCH] Whitespace changes --- config/printer-elegoo-neptune3-pro-2023.cfg | 2 +- docs/Config_Reference.md | 18 +- docs/G-Codes.md | 4 +- klippy/extras/neptune_screen.py | 703 +++++++++++++------- klippy/extras/serial_bridge.py | 69 +- src/generic/serial_bridge_irq.c | 29 +- src/generic/serial_bridge_irq.h | 4 +- src/serial_bridge.c | 17 +- src/stm32/Kconfig | 2 +- src/stm32/serial_bridge.c | 33 +- src/stm32/serial_bridge.h | 5 +- 11 files changed, 565 insertions(+), 321 deletions(-) diff --git a/config/printer-elegoo-neptune3-pro-2023.cfg b/config/printer-elegoo-neptune3-pro-2023.cfg index 0e00333144f3..ef9d9948f3eb 100644 --- a/config/printer-elegoo-neptune3-pro-2023.cfg +++ b/config/printer-elegoo-neptune3-pro-2023.cfg @@ -3,7 +3,7 @@ # "32KiB bootloader" and serial (on USART1 PA10/PA9) communication. # For screen support, enable "Enable extra low-level configuration options" -# and select "Enable serial bridge" and then "USART6" then uncommit the [neptune_screen] +# and select "Enable serial bridge" and then "USART6" then uncommit the [neptune_screen] # and serial_bridge sections diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index f638267edb47..b8fb7075c6fc 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -4303,14 +4303,14 @@ information on menu attributes available during template rendering. ### [neptune screen] Support for the Elegoo screen for the Neptune 3/3 Plus/3 Max. -A [serial bridge](#serial_bridge) must be setup for communciation to +A [serial bridge](#serial_bridge) must be setup for communciation to take place. Communication is based on versions (1.4.2) and earlier. ``` [neptune_screen] #serial_bridge: # Name of the serial_bridge to be used for communication -#variant: +#variant: # The device variant to report to the screen. Possible values are # 3Pro: Neptune 3 Pro # 3Max: Neptune 3 Max @@ -4755,31 +4755,31 @@ via the `i2c_speed` parameter. All other Klipper micro-controllers use a ``` ### [serial_bridge] -Communication between the mcu and devices connected to its UART ports +Communication between the mcu and devices connected to its UART ports such as third-party screens can be bridged with this module. This feature must be enabled with the low-level configuration options when compiling the -firmware specifying which ports should be available for the bridge to use. +firmware specifying which ports should be available for the bridge to use. Currently only STM32 devices are supported. -See the [command reference](G-Codes.md#serial_bridge) for the different +See the [command reference](G-Codes.md#serial_bridge) for the different options and configurations available. ``` [serial_bridge bridge_screen] #eol: -# The character or string of characters to be sent at the end of each +# The character or string of characters to be sent at the end of each # call. Defaults to \n #baud # The baud rate for communication. Default is 115200 #config: -# The serial configuration to use. These define the port and pins. +# The serial configuration to use. These define the port and pins. # List them using the [command reference](G-Codes.md#serial_bridge) # SERIAL_BRIDGE_LIST_CONFIGS #tx_pin: -# Pin used for Tx. This is used to reserve the pin and to look up +# Pin used for Tx. This is used to reserve the pin and to look up # the appropriate MCU #rx_pin -# Pin used for Rx. This is used to reserve the pin and to look up +# Pin used for Rx. This is used to reserve the pin and to look up # the appropriate MCU #logging # Boolean to turn logging of or on for debugging communication diff --git a/docs/G-Codes.md b/docs/G-Codes.md index b4121b79ea91..c99672557bcb 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -1109,12 +1109,12 @@ has been enabled. send a serial message (TEXT) to the bridge specificed (BRIDGE). #### SERIAL_BRIDGE_LIST_CONFIGS -`SERIAL_BRIDGE_LIST_CONFIGS`: This command will list the available +`SERIAL_BRIDGE_LIST_CONFIGS`: This command will list the available configurations reported by the MCU for use. This config should be used when setting up a new [serial_bridge](Config_Reference.md#serial_bridge). #### SERIAL_BRIDGE_LIST_BRIDGES -`SERIAL_BRIDGE_LIST_BRIDGES`: This command will list the available +`SERIAL_BRIDGE_LIST_BRIDGES`: This command will list the available bridges ready for use from the printer configuration [serial_bridge](Config_Reference.md#serial_bridge). diff --git a/klippy/extras/neptune_screen.py b/klippy/extras/neptune_screen.py index 4251b1c3621d..ba68c8e077be 100644 --- a/klippy/extras/neptune_screen.py +++ b/klippy/extras/neptune_screen.py @@ -1,6 +1,6 @@ import logging import time -from abc import ABC, abstractmethod +from abc import ABCMeta, abstractmethod SERIAL_STATE_HEADER_NONE = 0 SERIAL_STATE_HEADER_ONE = 1 @@ -14,7 +14,7 @@ DGUS_CMD_READVAR = 0x83 class NeptuneScreen: - def __init__(self, config): + def __init__(self, config): self._serial_state = None self._serial_state = SERIAL_STATE_HEADER_NONE self._axis_unit = 1 @@ -37,52 +37,59 @@ def __init__(self, config): self.config = config self.mutex = self.printer.get_reactor().mutex() self.name = config.get_name() - self.reactor = self.printer.get_reactor() + self.reactor = self.printer.get_reactor() self._logging = config.getboolean("logging", False) self.heaters = [] self.leds = [] self._last_gcode_output = "" - - self.printer.register_event_handler("klippy:ready", self.handle_ready) + + self.printer.register_event_handler("klippy:ready", self.handle_ready) self.gcode = self.printer.lookup_object('gcode') self.gcode.register_output_handler(self.gcode_output_handler) bridge = config.get('serial_bridge') - + self.variant = config.get('variant', '3Pro') - self.serial_bridge = self.printer.lookup_object(f'serial_bridge {bridge}') - self.serial_bridge.register_callback(self._handle_serial_bridge_response) + self.serial_bridge = self.printer.lookup_object( + 'serial_bridge %s' %(bridge)) + self.serial_bridge.register_callback( + self._handle_serial_bridge_response) self._update_interval = 2 - self._update_timer = self.reactor.register_timer(self._screen_update) + self._update_timer = self.reactor.register_timer(self._screen_update) def gcode_output_handler(self, msg): self._last_gcode_output = msg - def _screen_update(self, eventtime): - stats = self.printer.lookup_object("print_stats").get_status(self.reactor.monotonic()) + def _screen_update(self, eventtime): + stats = self.printer.lookup_object( + "print_stats").get_status(self.reactor.monotonic()) - for heater in self.heaters: - current_temp, target_temp = heater.get_temp(eventtime) + for heater in self.heaters: + current_temp, target_temp = heater.get_temp(eventtime) if heater.name == 'heater_bed': - self.send_text("main.bedtemp.txt=\"" f'{current_temp:.0f} / {target_temp:.0f}' + "\"") + self.updateTextVariable("main.bedtemp.txt", + '%.0f / %.0f' % (current_temp, target_temp)) else: - self.send_text("main.nozzletemp.txt=\"" f'{current_temp:.0f} / {target_temp:.0f}' + "\"") - + self.updateTextVariable("main.nozzletemp.txt", + '%.0f / %.0f' % (current_temp, target_temp)) + if self._is_led_on(eventtime): self.send_text("status_led2=1") else: self.send_text("status_led2=0") - g_status = self.printer.lookup_object("gcode_move").get_status() + g_status = self.printer.lookup_object("gcode_move").get_status() self.updateNumericVariable("printpause.zvalue.vvs1", "2") - self.send_text(f"printpause.zvalue.val={(g_status['position'].z * 100):.0f}") - + self.send_text( + "printpause.zvalue.val=%.0f" % (g_status['position'].z * 100)) + fan = self.printer.lookup_object("fan") - self.send_text(f"printpause.fanspeed.txt=\"{(fan.get_status(eventtime)['speed'] * 100):.0f}%\"") + self.updateTextVariable("printpause.fanspeed.txt", + "%.0f%%" % (fan.get_status(eventtime)['speed'] * 100)) last_state = self._print_state self._print_state = stats['state'] @@ -90,7 +97,7 @@ def _screen_update(self, eventtime): if stats['state'] == 'printing' and last_state != stats['state']: self.send_text("page printpause") if stats['state'] == 'complete' and last_state != stats['state']: - self.send_text("page main") + self.send_text("page main") return eventtime + self._update_interval @@ -98,7 +105,7 @@ def _is_led_on(self, eventtime): for led in self.leds: status = led.get_status(eventtime) white = status["color_data"][0][3] - + if white > 0: return True else: @@ -109,7 +116,7 @@ def _handle_serial_bridge_response(self, data): self.log("Received message: " + byte_debug) messages = [] message = self._last_message if self._last_message else None - + for byte in data: #self.log(f"Process data: state {self._serial_state} {message}") if self._serial_state == SERIAL_STATE_HEADER_NONE: @@ -125,10 +132,10 @@ def _handle_serial_bridge_response(self, data): elif self._serial_state == SERIAL_STATE_HEADER_TWO: self._serial_state = SERIAL_STATE_HEADER_MESSAGE message = Message() - message.payload = [] + message.payload = [] message.length = byte self._last_message = message - elif self._serial_state == SERIAL_STATE_HEADER_MESSAGE: + elif self._serial_state == SERIAL_STATE_HEADER_MESSAGE: message.payload.append(byte) if len(message.payload) == message.length: @@ -152,14 +159,17 @@ def process_message(self, message): Processor.process_if_match(message, self) def run_delayed_gcode(self, gcode, callback=None): - self._gcode_callbacks[f'{time.time()}'] = {"gcode": gcode, "callback": callback} - self.reactor.register_timer(self.gcode_command_timer, self.reactor.monotonic()) - - def gcode_command_timer(self, eventtime): - with self.mutex: + self._gcode_callbacks[ + str(time.time())] = {"gcode": gcode, "callback": callback} + + self.reactor.register_timer( + self.gcode_command_timer, self.reactor.monotonic()) + + def gcode_command_timer(self, eventtime): + with self.mutex: for time in list(self._gcode_callbacks.keys()): command = self._gcode_callbacks[time] - del self._gcode_callbacks[time] + del self._gcode_callbacks[time] code = command["gcode"] callback = command["callback"] @@ -168,15 +178,17 @@ def gcode_command_timer(self, eventtime): self.gcode.run_script(code) if callback: callback() - except Exception as e: + except Exception as e: self.send_text("page wait") - self.updateTextVariable("wait.t1.txt", self._last_gcode_output) + self.updateTextVariable( + "wait.t1.txt", self._last_gcode_output) self.send_text("beep 2000") - self.reactor.register_timer(self.load_main_page, self.reactor.monotonic() + 4) + self.reactor.register_timer( + self.load_main_page, self.reactor.monotonic() + 4) self.error("Error running gcode script: " + str(e)) self.log("Running delayed complete: " + code) - + return self.reactor.NEVER def load_main_page(self, eventtime): @@ -184,29 +196,32 @@ def load_main_page(self, eventtime): return self.reactor.NEVER def _screen_init(self, eventtime): - - move = self.printer.lookup_object("gcode_move").get_status(self.reactor.monotonic()) + + move = self.printer.lookup_object( + "gcode_move").get_status(self.reactor.monotonic()) probe = self.printer.lookup_object("probe") - self.send_text("page boot") - self.send_text("com_star") - self.send_text(f"main.va0.val={self._get_variant()}") - self.send_text("page main") - self.send_text(f"information.sversion.txt=\"Klipper\"") - self.updateNumericVariable("restFlag1", f"1") #paused - self.updateNumericVariable("restFlag2", f"1") #allow pause + self.send_text("page boot") + self.send_text("com_star") + self.send_text("main.va0.val=%d" % (self._get_variant())) + self.send_text("page main") + self.send_text("information.sversion.txt=\"Klipper\"") + self.updateNumericVariable("restFlag1", "1") #paused + self.updateNumericVariable("restFlag2", "1") #allow pause (x,y,z) = probe.get_offsets() homing_z = move['homing_origin'].z - self.updateNumericVariable("leveldata.z_offset.val", f"{((homing_z - z) * 100):.0f}") + self.updateNumericVariable( + "leveldata.z_offset.val", "%.0f" % ((homing_z - z) * 100)) - self.reactor.update_timer(self._update_timer, eventtime + self._update_interval) + self.reactor.update_timer( + self._update_timer, eventtime + self._update_interval) return self.reactor.NEVER def updateTextVariable(self, key, value): - self.send_text(f"{key}=\"{value}\"") + self.send_text("%s=\"%s\"" % (key, value)) def updateNumericVariable(self, key, value): - self.send_text(f"{key}={value}") + self.send_text("%s=%s" % (key, value)) def _get_variant(self): if self.variant == "3Pro": @@ -219,11 +234,15 @@ def _get_variant(self): return 1 def get_estimated_print_time(self): - stats = self.printer.lookup_object("print_stats").get_status(self.reactor.monotonic()) - sd = self.printer.lookup_object("virtual_sdcard").get_status(self.reactor.monotonic()) + stats = self.printer.lookup_object( + "print_stats").get_status(self.reactor.monotonic()) + sd = self.printer.lookup_object( + "virtual_sdcard").get_status(self.reactor.monotonic()) if sd: - return (stats['print_duration'] / sd['progress']) if sd['progress'] > 0 else 0 + return ( + stats['print_duration'] / sd['progress'] + ) if sd['progress'] > 0 else 0 else: return stats['print_duration'] @@ -231,37 +250,43 @@ def update_file_list(self): sd = self.printer.lookup_object("virtual_sdcard") if sd: - self._file_list = sd.get_file_list() + self._file_list = sd.get_file_list() index = 0 current_file_index = self._file_page_number * self._file_per_page next_file_index = current_file_index + self._file_per_page for i in range(8): - self.updateTextVariable(f"printfiles.t{i}.txt", "") + self.updateTextVariable("printfiles.t%d.txt" % (i), "") for fname, fsize in self._file_list: if index >= current_file_index and index < next_file_index: - self.log(F"Sending file {fname}") - self.updateTextVariable(f"printfiles.t{(index % self._file_per_page)}.txt", fname) + self.log("Sending file %s" % (fname)) + self.updateTextVariable( + "printfiles.t%d.txt" %((index % self._file_per_page)), + fname) index+=1 def handle_ready(self): self.log("Ready") pheaters = self.printer.lookup_object('heaters') self.printer.load_object(self.config, 'heaters') - heater_names = self.config.getlist("heater", ("extruder", "heater_bed")) + heater_names = self.config.getlist( + "heater", ("extruder", "heater_bed")) self.heaters = [pheaters.lookup_heater(n) for n in heater_names] - self.reactor.register_timer(self._reset_screen, self.reactor.monotonic()) + self.reactor.register_timer( + self._reset_screen, self.reactor.monotonic()) pled = self.printer.lookup_object("led") - self.leds = [pled.led_helpers.get(n) for n in pled.led_helpers.keys() ] + self.leds = [ + pled.led_helpers.get(n) for n in pled.led_helpers.keys() + ] #for n in self.printer.lookup_objects(): # self.log(f"object: {n}" ) - + def send_text(self, text): self.serial_bridge.send_text(text) @@ -276,9 +301,10 @@ def _reset_screen(self, eventtime): self.log("Reset") self.send_text("com_star") self.send_text("rest") - self.reactor.register_timer(self._screen_init, self.reactor.monotonic() + 2.) + self.reactor.register_timer( + self._screen_init, self.reactor.monotonic() + 2.) return self.reactor.NEVER - + def load_config(config): return NeptuneScreen(config) @@ -293,7 +319,8 @@ def __init__(self): def process_datagram(self): self.command = self.payload[0] - self.command_address = ((self.payload[1] & 0xff) << 8) | (self.payload[2] & 0xff) + self.command_address = ( + (self.payload[1] & 0xff) << 8) | (self.payload[2] & 0xff) self.command_data_length = self.payload[3] self.command_data = [] @@ -302,9 +329,19 @@ def process_datagram(self): self.command_data.append(((byte & 0xff) << 8) | (next(it) & 0xff)) def __str__(self): - payload_str = ' '.join([f'0x{byte:02x}' for byte in self.payload]) - return f'payload: { payload_str }, length: {self.length}, command: 0x{self.command:02x}, command_address: 0x{self.command_address:04x} command_data_length: {self.command_data_length}, command_data: {self.command_data}' - + payload_str = ' '.join(['0x%02x' % byte for byte in self.payload]) + return 'payload: %s, ' \ + 'length: %s, command: 0x%02x, ' \ + 'command_address: 0x%04x ' \ + 'command_data_length: %s, ' \ + 'command_data: %s' % ( + payload_str, + self.length, + self.command, + self.command_address, + self.command_data_length, + self.command_data + ) DGUS_KEY_MAIN_PAGE = 0x1002 DGUS_KEY_STOP_PRINT = 0x1008 DGUS_KEY_PAUSE_PRINT = 0x100A @@ -331,14 +368,16 @@ def __str__(self): DGUS_KEY_PRINT_FILES = 0x2204 DGUS_KEY_PRINT_CONFIRM = 0x2205 -class CommandProcessor(ABC): +class CommandProcessor(): + __metaclass = ABCMeta def __init__(self, address, command=None): self.address = address self.command = command pass def is_match(self, message): - return message.command_address == self.address and (self.command is None or self.command == message.command_data[0]) + return message.command_address == self.address and ( + self.command is None or self.command == message.command_data[0]) def process_if_match(self, message, screen): if self.is_match(message): @@ -347,11 +386,12 @@ def process_if_match(self, message, screen): @abstractmethod def process(self, data, screen): pass - + class MainPageProcessor(CommandProcessor): def process(self, message, screen): if message.command_data[0] == 0x1: #print button - status = screen.printer.lookup_object("print_stats").get_status(screen.reactor.monotonic()) + status = screen.printer.lookup_object( + "print_stats").get_status(screen.reactor.monotonic()) sd = screen.printer.lookup_object("virtual_sdcard") if status['state'] in ['printing', 'paused']: @@ -366,113 +406,160 @@ def process(self, message, screen): screen.send_text("page file1") limit = 25 - screen._file_list = sd.get_file_list() + screen._file_list = sd.get_file_list() index = 0 for fname, fsize in screen._file_list: if(index <= limit): - screen.log(F"Sending file {fname}") + screen.log("Sending file %s" %(fname)) page = ((index // 5) + 1) - screen.updateTextVariable(f"file{page}.t{index}.txt", fname) + screen.updateTextVariable( + "file%d.t%d.txt" % (page, index), fname) index+=1 class BedLevelProcessor(CommandProcessor): def process(self, message, screen): - if message.command_data[0] == 0x2 or message.command_data[0] == 0x3: #z-offset up/down - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) + if message.command_data[0] == 0x2 or ( + message.command_data[0] == 0x3): #z-offset up/down + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) probe = screen.printer.lookup_object("probe") unit = screen._zoffset_unit if message.command_data[0] == 0x3: - unit *= -1 - + unit *= -1 + (x,y,z) = probe.get_offsets() homing_z = move['homing_origin'].z new_offset = homing_z + unit - screen.run_delayed_gcode(f"SET_GCODE_OFFSET Z={new_offset} MOVE=1") + screen.run_delayed_gcode( + "SET_GCODE_OFFSET Z=%.3f MOVE=1" % (new_offset)) - screen.updateNumericVariable("leveldata.z_offset.val", f"{((new_offset - z) * 100):.0f}") - screen.updateNumericVariable("adjustzoffset.z_offset.val", f"{((new_offset - z) * 100):.0f}") + screen.updateNumericVariable( + "leveldata.z_offset.val", "%.0f" % ((new_offset - z) * 100)) + screen.updateNumericVariable("adjustzoffset.z_offset.val", + "%.0f" % ((new_offset - z) * 100)) if message.command_data[0] == 0x4: #z-offset unit 0.01 screen._zoffset_unit = 0.01 - screen.updateNumericVariable("adjustzoffset.zoffset_value.val", f'{1}') + screen.updateNumericVariable( + "adjustzoffset.zoffset_value.val", "1") if message.command_data[0] == 0x5: #z-offset unit 0.1 screen._zoffset_unit = 0.1 - screen.updateNumericVariable("adjustzoffset.zoffset_value.val", f'{2}') + screen.updateNumericVariable( + "adjustzoffset.zoffset_value.val", "2") if message.command_data[0] == 0x6: #z-offset unit 1 screen._zoffset_unit = 1 - screen.updateNumericVariable("adjustzoffset.zoffset_value.val", f'{3}') + screen.updateNumericVariable( + "adjustzoffset.zoffset_value.val", "3") if message.command_data[0] == 0x8: #light button pled = screen.printer.lookup_object("led") for n in pled.led_helpers.keys(): status = pled.led_helpers[n].get_status(None) white = status["color_data"][0][3] - + if white > 0: - screen.run_delayed_gcode(f"SET_LED LED={n} WHITE=0") + screen.run_delayed_gcode("SET_LED LED=%s WHITE=0" % (n)) else: - screen.run_delayed_gcode(f"SET_LED LED={n} WHITE=1") + screen.run_delayed_gcode("SET_LED LED=%s WHITE=1" % (n)) if message.command_data[0] == 0x9: #bed level calibrate - screen.run_delayed_gcode("BED_MESH_CLEAR\nM140 S60\nM104 S140\nM109 S140\nM190 S60\nBED_MESH_CALIBRATE LIFT_SPEED=2\nG28\nG1 F200 Z0", lambda: ( + screen.run_delayed_gcode( + "BED_MESH_CLEAR\n" \ + "M140 S60\nM104 S140\n" \ + "M109 S140\nM190 S60\n" \ + "BED_MESH_CALIBRATE LIFT_SPEED=2\n" \ + "G28\n" \ + "G1 F200 Z0", lambda: ( screen.send_text("page leveldata_36"), screen.send_text("page warn_zoffset") )) if message.command_data[0] == 0xa: #print pause request status - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) - stats = screen.printer.lookup_object("print_stats").get_status(screen.reactor.monotonic()) - sd = screen.printer.lookup_object("virtual_sdcard").get_status(screen.reactor.monotonic()) + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) + stats = screen.printer.lookup_object( + "print_stats").get_status(screen.reactor.monotonic()) + sd = screen.printer.lookup_object( + "virtual_sdcard").get_status(screen.reactor.monotonic()) fan = screen.printer.lookup_object("fan") estimated_time_left = screen.get_estimated_print_time() if sd: - screen.updateNumericVariable("printpause.printprocess.val", f"{(sd['progress'] * 100):.0f}") - screen.updateTextVariable("printpause.printvalue.txt", f"{(sd['progress'] * 100):.0f}") + screen.updateNumericVariable("printpause.printprocess.val", + "%.0f" % (sd['progress'] * 100)) + screen.updateTextVariable("printpause.printvalue.txt", + "%.0f" % (sd['progress'] * 100)) else: - screen.updateNumericVariable("printpause.printprocess.val", f"{(0 * 100):.0f}") - screen.updateTextVariable("printpause.printvalue.txt", f"{(0* 100):.0f}") + screen.updateNumericVariable( + "printpause.printprocess.val", "0") + screen.updateTextVariable( + "printpause.printvalue.txt","0") + + screen.updateTextVariable( + "printpause.t0.txt", stats['filename']) - screen.updateTextVariable("printpause.t0.txt", f"{stats['filename']}") - screen.updateTextVariable("printpause.printtime.txt", f"{(stats['print_duration'] / 60.0):.0f} / {(estimated_time_left / 60):.0f} min") - - screen.updateTextVariable("printpause.fanspeed.txt", f"{(fan.get_status(screen.reactor.monotonic())['speed'] * 100):.0f}%") + current_time = stats['print_duration'] / 60.0 + estimated_time = estimated_time_left / 60 + screen.updateTextVariable("printpause.printtime.txt", + "%.0f / %.0f min" % (current_time, estimated_time)) + + mono = screen.reactor.monotonic() + screen.updateTextVariable("printpause.fanspeed.txt", + "%.0f%%" % (fan.get_status(mono)['speed'] * 100)) if message.command_data[0] == 0x16: #print initial request - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) - stats = screen.printer.lookup_object("print_stats").get_status(screen.reactor.monotonic()) - sd = screen.printer.lookup_object("virtual_sdcard").get_status(screen.reactor.monotonic()) + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) + stats = screen.printer.lookup_object( + "print_stats").get_status(screen.reactor.monotonic()) + sd = screen.printer.lookup_object( + "virtual_sdcard").get_status(screen.reactor.monotonic()) estimated_time_left = screen.get_estimated_print_time() - screen.updateTextVariable("printpause.printspeed.txt", f"{(move['speed_factor'] * 100):.0f}") - screen.updateTextVariable("printpause.printtime.txt", f"{(stats['print_duration'] / 60.0):.0f} / {(estimated_time_left / 60):.0f} min") + screen.updateTextVariable("printpause.printspeed.txt", + "%.0f" % (move['speed_factor'] * 100)) + + current_time = stats['print_duration'] / 60.0 + estimated_time = estimated_time_left / 60 + screen.updateTextVariable("printpause.printtime.txt", + "%.0f / %.0f min" % (current_time, estimated_time)) if sd: - screen.updateNumericVariable("printpause.printprocess.val", f"{(sd['progress'] * 100):.0f}") - screen.updateTextVariable("printpause.printvalue.txt", f"{(sd['progress'] * 100):.0f}") + screen.updateNumericVariable("printpause.printprocess.val", + "%.0f" % (sd['progress'] * 100)) + screen.updateTextVariable("printpause.printvalue.txt", + "%.0f" % (sd['progress'] * 100)) else: - screen.updateNumericVariable("printpause.printprocess.val", f"{(0 * 100):.0f}") - screen.updateTextVariable("printpause.printvalue.txt", f"{(0* 100):.0f}") - + screen.updateNumericVariable( + "printpause.printprocess.val", "0") + screen.updateTextVariable( + "printpause.printvalue.txt", "0") + #restFlag1: 0 - printing, 1- paused - #restFlag2: m76 pauses print timer setting this to 0 and restflag to 1, 1 --abort sd, 1 when hotend temp reached - #can only pause print when restflag2=1 - + #restFlag2: m76 pauses print timer + # setting this to 0 and restflag to 1, 1 + # --abort sd, 1 when hotend temp reached + # can only pause print when restflag2=1 + if stats['state'] == 'printing': - screen.updateNumericVariable("restFlag1", f"0") + screen.updateNumericVariable("restFlag1", "0") else: - screen.updateNumericVariable("restFlag1", f"1") + screen.updateNumericVariable("restFlag1", "1") class AdjustmentProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): if message.command_data[0] == 0x1: screen._temp_and_rate_unit = 10 screen._temp_ctrl = 'extruder' - heater = screen.printer.lookup_object('heaters').lookup_heater(screen._temp_ctrl) - (current_temp, target_temp) = heater.get_temp(screen.reactor.monotonic()) - screen.updateNumericVariable("adjusttemp.targettemp.val", f'{target_temp:.0f}') + heater = screen.printer.lookup_object( + 'heaters').lookup_heater(screen._temp_ctrl) + + mono = screen.reactor.monotonic() + (current_temp, target_temp) = heater.get_temp(mono) + screen.updateNumericVariable( + "adjusttemp.targettemp.val", "%.0f" % (target_temp)) if message.command_data[0] == 0x02: screen.send_text("page printpause") if message.command_data[0] == 0x05: @@ -482,45 +569,59 @@ def process(self, message, screen): screen._temp_and_rate_unit = 10 screen._speed_ctrl = 'feedrate' - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(move['speed_factor'] * 100):.0f}") + screen.updateNumericVariable("adjustspeed.targetspeed.val", + "%.0f" % (move['speed_factor'] * 100)) screen.send_text("page adjustspeed") if message.command_data[0] == 0x07: #adjust screen button - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) probe = screen.printer.lookup_object("probe") - screen._zoffset_unit = 0.1 + screen._zoffset_unit = 0.1 (x,y,z) = probe.get_offsets() homing_z = move['homing_origin'].z - screen.updateNumericVariable("adjustzoffset.zoffset_value.val", f'{2}') - screen.updateNumericVariable("adjustzoffset.z_offset.val", f"{(((homing_z - z)) * 100):.0f}") + screen.updateNumericVariable( + "adjustzoffset.zoffset_value.val", "2") + screen.updateNumericVariable("adjustzoffset.z_offset.val", + "%.0f" % ((homing_z - z) * 100)) screen.send_text("page adjustzoffset") if message.command_data[0] == 0x08: #reset target feedrate - screen.run_delayed_gcode("M220 S100") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(100):.0f}") + screen.run_delayed_gcode("M220 S100") + screen.updateNumericVariable( + "adjustspeed.targetspeed.val", "100") if message.command_data[0] == 0x09: #reset target flow screen.run_delayed_gcode("M221 S100") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(100):.0f}") + screen.updateNumericVariable( + "adjustspeed.targetspeed.val", "100") if message.command_data[0] == 0x0A: #reset target fanspeed - 100% screen.run_delayed_gcode("M106 S255") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(100):.0f}") + screen.updateNumericVariable( + "adjustspeed.targetspeed.val", "100") class TempScreenProcessor(CommandProcessor): def process(self, message, screen): if message.command_data[0] == 0x1: #get hotend temp screen._temp_ctrl = 'extruder' - heater = screen.printer.lookup_object('heaters').lookup_heater(screen._temp_ctrl) - (current_temp, target_temp) = heater.get_temp(screen.reactor.monotonic()) - screen.updateNumericVariable("adjusttemp.targettemp.val", f'{target_temp:.0f}') + heater = screen.printer.lookup_object( + 'heaters').lookup_heater(screen._temp_ctrl) + (current_temp, target_temp) = heater.get_temp( + screen.reactor.monotonic()) + screen.updateNumericVariable( + "adjusttemp.targettemp.val", '%.0f' % (target_temp)) if message.command_data[0] == 0x3: #get bed temp screen._temp_ctrl = 'heater_bed' - heater = screen.printer.lookup_object('heaters').lookup_heater(screen._temp_ctrl) - (current_temp, target_temp) = heater.get_temp(screen.reactor.monotonic()) - screen.updateNumericVariable("adjusttemp.targettemp.val", f'{target_temp:.0f}') + heater = screen.printer.lookup_object( + 'heaters').lookup_heater(screen._temp_ctrl) + (current_temp, target_temp) = heater.get_temp( + screen.reactor.monotonic()) + screen.updateNumericVariable( + "adjusttemp.targettemp.val", '%.0f' % (target_temp)) if message.command_data[0] == 0x5: #.1mm screen._axis_unit = 0.1 screen._temp_and_rate_unit = 1 @@ -533,57 +634,75 @@ def process(self, message, screen): screen._axis_unit = 10.0 screen._temp_and_rate_unit = 10 screen._acceleration_unit = 100 - if message.command_data[0] == 0x8 or message.command_data[0] == 0x9: #increase hotend temp by temp_unit - heater = screen.printer.lookup_object('heaters').lookup_heater(screen._temp_ctrl) - (current_temp, target_temp) = heater.get_temp(screen.reactor.monotonic()) + if message.command_data[0] == 0x8 or ( + message.command_data[0] == 0x9): #increase + #hotend temp by temp_unit + heater = screen.printer.lookup_object( + 'heaters').lookup_heater(screen._temp_ctrl) + (current_temp, target_temp) = heater.get_temp( + screen.reactor.monotonic()) min_temp = 25 max_temp = 0 - if screen._temp_ctrl == 'extruder': + if screen._temp_ctrl == 'extruder': max_temp = 230 elif screen._temp_ctrl == 'heater_bed': max_temp = 125 - new_target_temp = target_temp + screen._temp_and_rate_unit * (1 if message.command_data[0] == 0x8 else - 1) + new_target_temp = target_temp + screen._temp_and_rate_unit * ( + 1 if message.command_data[0] == 0x8 else - 1) + if new_target_temp >= min_temp and new_target_temp <= max_temp: gcode = ('M104' if screen._temp_ctrl == 'extruder' else 'M140') - screen.run_delayed_gcode(F"{gcode} S{new_target_temp}") - screen.updateNumericVariable("adjusttemp.targettemp.val", f'{new_target_temp:.0f}') - + screen.run_delayed_gcode("%s S%.0f" % (gcode, new_target_temp)) + screen.updateNumericVariable( + "adjusttemp.targettemp.val", '%.0f' % (new_target_temp)) + if message.command_data[0] == 0xA: #speed rate get screen._speed_ctrl = 'feedrate' - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(move['speed_factor'] * 100):.0f}") + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) + screen.updateNumericVariable("adjustspeed.targetspeed.val", + "%.0f" % (move['speed_factor'] * 100)) if message.command_data[0] == 0xB: #flow control get screen._speed_ctrl = 'flowrate' - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(move['extrude_factor'] * 100):.0f}") + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) + screen.updateNumericVariable("adjustspeed.targetspeed.val", + "%.0f" % (move['extrude_factor'] * 100)) if message.command_data[0] == 0xC: #fan speed get screen._speed_ctrl = 'fanspeed' fan = screen.printer.lookup_object("fan") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(fan.get_status(screen.reactor.monotonic())['speed'] * 100):.0f}") - if message.command_data[0] == 0xD or message.command_data[0] == 0xE: #increase/decrease rate + + mono = screen.reactor.monotonic() + screen.updateNumericVariable("adjustspeed.targetspeed.val", + "%.0f" % (fan.get_status(mono)['speed'] * 100)) + if message.command_data[0] == 0xD or ( + message.command_data[0] == 0xE): #increase/decrease rate unit = screen._temp_and_rate_unit if message.command_data[0] == 0xE: #increase unit *= -1 if screen._speed_ctrl == 'feedrate': - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) - + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) + new_rate = (move['speed_factor'] + (unit / 100.0)) * 100 min_rate = 0 if new_rate < min_rate: new_rate = min_rate - screen.run_delayed_gcode(f"M220 S{new_rate:.0f}") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(new_rate):.0f}") + screen.run_delayed_gcode("M220 S%.0f" % (new_rate)) + screen.updateNumericVariable( + "adjustspeed.targetspeed.val", "%.0f" % (new_rate)) if screen._speed_ctrl == 'flowrate': - move = screen.printer.lookup_object("gcode_move").get_status(screen.reactor.monotonic()) + move = screen.printer.lookup_object( + "gcode_move").get_status(screen.reactor.monotonic()) new_rate = (move['extrude_factor'] + (unit / 100.0)) * 100 - max_rate = 150 + max_rate = 150 if new_rate > max_rate: new_rate = max_rate @@ -591,13 +710,15 @@ def process(self, message, screen): if new_rate < min_rate: new_rate = min_rate - screen.run_delayed_gcode(f"M221 S{new_rate:.0f}") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(new_rate):.0f}") + screen.run_delayed_gcode("M221 S%.0f" % (new_rate)) + screen.updateNumericVariable( + "adjustspeed.targetspeed.val","%.0f" % (new_rate)) if screen._speed_ctrl == 'fanspeed': fan = screen.printer.lookup_object("fan") - new_rate = fan.get_status(screen.reactor.monotonic())['speed'] + (unit / 100.0) + new_rate = fan.get_status( + screen.reactor.monotonic())['speed'] + (unit / 100.0) - max_rate = 1 + max_rate = 1 if new_rate > max_rate: new_rate = max_rate @@ -605,11 +726,14 @@ def process(self, message, screen): if new_rate < min_rate: new_rate = min_rate - screen.run_delayed_gcode(f"M106 S{(new_rate * 255.0):.0f}") - screen.updateNumericVariable("adjustspeed.targetspeed.val", f"{(new_rate * 100):.0f}") - if message.command_data[0] in [0x10, 0x0f]: #speed setting page, acceleration page + screen.run_delayed_gcode("M106 S%.0f" % ((new_rate * 255.0))) + screen.updateNumericVariable( + "adjustspeed.targetspeed.val", "%.0f" % (new_rate * 100)) + if message.command_data[0] in [0x10, 0x0f]: #speed setting page, + #acceleration page screen._acceleration_unit = 100 - toolhead = screen.printer.lookup_object("toolhead").get_status(screen.reactor.monotonic()) + toolhead = screen.printer.lookup_object( + "toolhead").get_status(screen.reactor.monotonic()) #speedsetvalue.t0.txt - x #speedsetvalue.t1.txt - y #speedsetvalue.t2.txt - z @@ -619,44 +743,76 @@ def process(self, message, screen): #speesetvalue.zaxis.val #speesetvalue.eaxis.val screen.updateTextVariable("speedsetvalue.t0.txt", "Accel.") - screen.updateTextVariable("speedsetvalue.t1.txt", "Max Accel. to Decel.") + screen.updateTextVariable( + "speedsetvalue.t1.txt", "Max Accel. to Decel.") screen.updateTextVariable("speedsetvalue.t2.txt", "SCV") screen.updateTextVariable("speedsetvalue.t3.txt", "Velocity") - screen.updateNumericVariable("speedsetvalue.xaxis.val", f'{toolhead["max_accel"]:.0f}') - screen.updateNumericVariable("speedsetvalue.yaxis.val", f'{toolhead["max_accel_to_decel"]:.0f}') - screen.updateNumericVariable("speedsetvalue.zaxis.val", f'{toolhead["square_corner_velocity"]:.0f}') - screen.updateNumericVariable("speedsetvalue.eaxis.val", f'{toolhead["max_velocity"]:.0f}') - if message.command_data[0] in [0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18]: #axis acceleration down (0x11 - 0x14) / up (0x15, - 0x18) + screen.updateNumericVariable( + "speedsetvalue.xaxis.val", '%.0f' % (toolhead["max_accel"])) + screen.updateNumericVariable("speedsetvalue.yaxis.val", + '%.0f' % (toolhead["max_accel_to_decel"])) + screen.updateNumericVariable("speedsetvalue.zaxis.val", + '%.0f' % (toolhead["square_corner_velocity"])) + screen.updateNumericVariable("speedsetvalue.eaxis.val", + '%.0f' % (toolhead["max_velocity"])) + #axis acceleration down (0x11 - 0x14) / up (0x15, - 0x18) + if message.command_data[0] in [ + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18]: toolhead = screen.printer.lookup_object("toolhead") status = toolhead.get_status(screen.reactor.monotonic()) unit = screen._acceleration_unit - if message.command_data[0] == 0x11: #accel - - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT ACCEL={(status['max_accel'] - unit):.0f}") - screen.updateNumericVariable("speedsetvalue.xaxis.val", f"{(status['max_accel'] - unit):.0f}") + if message.command_data[0] == 0x11: #accel - + val = status['max_accel'] - unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT ACCEL=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.xaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x12: #decel - - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT ACCEL_TO_DECEL={(status['max_accel_to_decel'] - unit):.0f}") - screen.updateNumericVariable("speedsetvalue.yaxis.val", f"{(status['max_accel_to_decel'] - unit):.0f}") + val = status['max_accel_to_decel'] - unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT ACCEL_TO_DECEL=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.yaxis.val", + '%.0f' % (val)) + if message.command_data[0] == 0x13: #scv - unit = unit // 10 - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={(status['square_corner_velocity'] - unit):.0f}") - screen.updateNumericVariable("speedsetvalue.zaxis.val", f"{(status['square_corner_velocity'] - unit):.0f}") + val = status['square_corner_velocity'] - unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.zaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x14: #velocity - - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT VELOCITY={(status['max_velocity'] - unit):.0f}") - screen.updateNumericVariable("speedsetvalue.eaxis.val", f"{(status['max_velocity'] - unit):.0f}") + val = status['max_velocity'] - unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT VELOCITY=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.eaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x15: #accel + - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT ACCEL={(status['max_accel'] + unit):.0f}") - screen.updateNumericVariable("speedsetvalue.xaxis.val", f"{(status['max_accel'] + unit):.0f}") + val = status['max_accel'] + unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT ACCEL=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.xaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x16: #decel + - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT ACCEL_TO_DECEL={(status['max_accel_to_decel'] + unit):.0f}") - screen.updateNumericVariable("speedsetvalue.yaxis.val", f"{(status['max_accel_to_decel'] + unit):.0f}") + val = status['max_accel_to_decel'] + unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT ACCEL_TO_DECEL=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.yaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x17: #scv + unit = unit // 10 - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={(status['square_corner_velocity'] + unit):.0f}") - screen.updateNumericVariable("speedsetvalue.zaxis.val", f"{(status['square_corner_velocity'] + unit):.0f}") + val = status['square_corner_velocity'] + unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.zaxis.val", + '%.0f' % (val)) if message.command_data[0] == 0x18: #velocity + - screen.run_delayed_gcode(f"SET_VELOCITY_LIMIT VELOCITY={(status['max_velocity'] + unit):.0f}") - screen.updateNumericVariable("speedsetvalue.eaxis.val", f"{(status['max_velocity'] + unit):.0f}") + val = status['max_velocity'] + unit + screen.run_delayed_gcode( + "SET_VELOCITY_LIMIT VELOCITY=%.0f" % (val)) + screen.updateNumericVariable("speedsetvalue.eaxis.val", + '%.0f' % (val)) class CoolScreenProcessor(CommandProcessor): def process(self, message, screen): @@ -692,13 +848,13 @@ def process(self, message, screen): screen.send_text("page autohome") screen.run_delayed_gcode("G28 X", lambda:( screen.send_text("page premove") - )) + )) elif message.command_data[0] == 6: screen.send_text("page autohome") screen.run_delayed_gcode("G28 Y", lambda:( screen.send_text("page premove") - )) - elif message.command_data[0] == 7: + )) + elif message.command_data[0] == 7: screen.send_text("page autohome") screen.run_delayed_gcode("G28 Z", lambda:( screen.send_text("page premove") @@ -710,14 +866,16 @@ def process(self, message, screen): current_z = move.get_status()["gcode_position"].z if message.command_data[0] == 0x01: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 Z{(current_z + screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 Z%.0f" %(current_z + screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 Z+{screen._axis_unit}") + screen.run_delayed_gcode("G0 Z+%.0f" % (screen._axis_unit)) else: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 Z{(current_z - screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 Z%.0f" %(current_z - screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 Z-{screen._axis_unit}") + screen.run_delayed_gcode("G0 Z-%.0f" % (screen._axis_unit)) class YAxisMoveKeyProcessor(CommandProcessor): def process(self, message, screen): @@ -725,14 +883,16 @@ def process(self, message, screen): current_y = move.get_status()["gcode_position"].y if message.command_data[0] == 0x01: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 Y{(current_y + screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 Y%.0f" %(current_y + screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 Y+{screen._axis_unit}") + screen.run_delayed_gcode("G0 Y+%.0f" % (screen._axis_unit)) else: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 Y{(current_y - screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 Y%.0f" %(current_y - screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 Y-{screen._axis_unit}") + screen.run_delayed_gcode("G0 Y-%.0f" % (screen._axis_unit)) class XAxisMoveKeyProcessor(CommandProcessor): def process(self, message, screen): @@ -740,51 +900,59 @@ def process(self, message, screen): current_x = move.get_status()["gcode_position"].x if message.command_data[0] == 0x01: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 X{(current_x + screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 X%.0f" %(current_x + screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 X+{screen._axis_unit}") + screen.run_delayed_gcode("G0 X+%.0f" % (screen._axis_unit)) else: if move.absolute_coord: - screen.run_delayed_gcode(F"G0 X{(current_x - screen._axis_unit)}") + screen.run_delayed_gcode( + "G0 X%.0f" %(current_x - screen._axis_unit)) else: - screen.run_delayed_gcode(F"G0 X-{screen._axis_unit}") + screen.run_delayed_gcode("G0 X-%.0f" % (screen._axis_unit)) class Heater0KeyProcessor(CommandProcessor): #heater temp def process(self, message, screen): - temp = ((message.command_data[0] & 0xff00) >> 8) | ((message.command_data[0] & 0x00ff) << 8) - screen.run_delayed_gcode(f"M104 S{temp}") - screen.send_text("pretemp.nozzletemp.txt=\" {} / {}\"") + temp = ((message.command_data[0] & 0xff00) >> 8) \ + | ((message.command_data[0] & 0x00ff) << 8) + screen.run_delayed_gcode("M104 S%d" % (temp)) + screen.send_text( + "pretemp.nozzletemp.txt=\" %d / %d\"" % (0, temp)) #todo class HeaterBedKeyProcessor(CommandProcessor): #bed temp def process(self, message, screen): - temp = ((message.command_data[0] & 0xff00) >> 8) | ((message.command_data[0] & 0x00ff) << 8) - screen.run_delayed_gcode(f"M140 S{temp}") - screen.send_text("pretemp.bedtemp.txt=\" {} / {}\"") + temp = ((message.command_data[0] & 0xff00) >> 8) | \ + ((message.command_data[0] & 0x00ff) << 8) + screen.run_delayed_gcode("M140 S%d" % (temp)) + screen.send_text("pretemp.bedtemp.txt=\" %d / %d\"" % (0, temp)) #todo class SettingScreenProcessor(CommandProcessor): def process(self, message, screen): - if message.command_data[0] == 0x1: + if message.command_data[0] == 0x1: screen.send_text("page autohome") screen.run_delayed_gcode("G28\ng1 f200 Z0.00", lambda: ( - screen.updateNumericVariable("leveling.va1.val", f"{screen._get_variant()}"), + screen.updateNumericVariable( + "leveling.va1.val", "%d" % (screen._get_variant())), screen.send_text("page leveldata_36"), screen.send_text("leveling_36.tm0.en=0"), - screen.send_text("leveling.tm0.en=0") - )) + screen.send_text("leveling.tm0.en=0") + )) if message.command_data[0] == 0x6: screen.run_delayed_gcode("M84") if message.command_data[0] == 0x7: fan = screen.printer.lookup_object("fan") if fan.get_status(screen.reactor.monotonic())['speed']: - screen.run_delayed_gcode(f"M106 S0") + screen.run_delayed_gcode("M106 S0") screen.updateNumericVariable("set.va0.val", "0") else: - screen.run_delayed_gcode(f"M106 S255") + screen.run_delayed_gcode("M106 S255") screen.updateNumericVariable("set.va0.val", "1") if message.command_data[0] == 0x0A: #load filamnet screen screen.send_text("page prefilament") - screen.updateTextVariable("prefilament.filamentlength.txt", f"{screen._filament_load_length}") - screen.updateTextVariable("prefilament.filamentspeed.txt", f"{screen._filament_load_feedrate}") + screen.updateTextVariable("prefilament.filamentlength.txt", + "%d" % (screen._filament_load_length)) + screen.updateTextVariable("prefilament.filamentspeed.txt", + "%d" % (screen._filament_load_feedrate)) if message.command_data[0] == 0xD: #settings page screen.send_text("page multiset") @@ -792,7 +960,7 @@ class ResumePrintProcessor(CommandProcessor): def process(self, message, screen): if message.command_data[0] == 0x1: #resume screen.updateNumericVariable("restFlag1", "0") - screen.send_text("page wait") + screen.send_text("page wait") screen.run_delayed_gcode("M24", lambda: ( screen.send_text("page printpause") )) @@ -806,7 +974,7 @@ def process(self, message, screen): pass #do nothing, screen change handled in tft if message.command_data[0] == 0xF1: #pause button confirmed screen.updateNumericVariable("restFlag1", "1") - screen.send_text("page wait") + screen.send_text("page wait") screen.run_delayed_gcode("M25", lambda: ( screen.send_text("page printpause") )) @@ -814,40 +982,45 @@ def process(self, message, screen): class StopPrintProcessor(CommandProcessor): def process(self, message, screen): - if message.command_data[0] == 0x1 or message.command_data[0] == 0xF1: #confirm stop print - screen.send_text("page wait") + if message.command_data[0] == 0x1 or ( + message.command_data[0] == 0xF1): #confirm stop print + screen.send_text("page wait") screen.run_delayed_gcode("CANCEL_PRINT", lambda: ( screen.send_text("page main") )) - + class HardwareTestProcessor(CommandProcessor): def process(self, message, screen): - if message.command_data[0] == 0x0F: #HARDWARE TEST CALLED FROM MAIN SCREEN + if message.command_data[0] == 0x0F: #HARDWARE TEST + #CALLED FROM MAIN SCREEN pass - + class SettingBackProcessor(CommandProcessor): def restart_if_config_needed(self, screen): - config = screen.printer.lookup_object("configfile").get_status(screen.reactor.monotonic()) + config = screen.printer.lookup_object( + "configfile").get_status(screen.reactor.monotonic()) if config['save_config_pending']: screen.send_text("page wait") screen.run_delayed_gcode("SAVE_CONFIG") - def process(self, message, screen): + def process(self, message, screen): if message.command_data[0] == 0x01: #setting back key from leveling - screen.run_delayed_gcode("Z_OFFSET_APPLY_PROBE\nG0 Z.2", lambda:( + screen.run_delayed_gcode( + "Z_OFFSET_APPLY_PROBE\nG1 F1000 Z.2", lambda:( self.restart_if_config_needed(screen) - )) + )) if message.command_data[0] == 0x7: #lcd version screen._version = message.command_data[1] class PrintFileProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): if message.command_data[0] == 0x01: #confirm print - screen.run_delayed_gcode(f"SDCARD_PRINT_FILE FILENAME=\"{screen._requested_file}\"") + screen.run_delayed_gcode( + "SDCARD_PRINT_FILE FILENAME=\"%s\"" % (screen._requested_file)) if message.command_data[0] == 0x0B: #printfiles prev/next if message.command_data[1] == 0x0: #previous - screen._file_page_number -= 1 + screen._file_page_number -= 1 if screen._file_page_number < 0: screen._file_page_number = 0 @@ -855,14 +1028,15 @@ def process(self, message, screen): screen.update_file_list() if message.command_data[1] == 0x1: #next - if (screen._file_page_number + 1) * screen._file_per_page < len(screen._file_list): + if (screen._file_page_number + 1) \ + * screen._file_per_page < len(screen._file_list): screen._file_page_number += 1 screen.update_file_list() class SelectFileProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): screen.updateTextVariable("askprint.t0.txt", "") screen.updateTextVariable("printpause.t0.txt", "") @@ -872,72 +1046,101 @@ def process(self, message, screen): if requested_file > max_file: screen.send_text("beep 2000") else: - screen.updateTextVariable("askprint.t0.txt", screen._file_list[requested_file][0]) - screen.updateTextVariable("printpause.t0.txt", screen._file_list[requested_file][0]) + screen.updateTextVariable("askprint.t0.txt", + screen._file_list[requested_file][0]) + screen.updateTextVariable("printpause.t0.txt", + screen._file_list[requested_file][0]) screen._requested_file = screen._file_list[requested_file][0] screen.send_text("page askprint") class PowerContinueProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): if message.command_data[0] == 0x03: #resume printing screen.send_text("page multiset") class PrintFilesProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): screen.updateTextVariable("printcnfirm.t0.txt", "") screen.updateTextVariable("printpause.t0.txt", "") max_file = len(screen._file_list) - 1 - requested_file = screen._file_page_number * screen._file_per_page + message.command_data[0] + requested_file = screen._file_page_number \ + * screen._file_per_page + message.command_data[0] if requested_file > max_file: screen.send_text("beep 2000") else: - screen.updateTextVariable("printcnfirm.t0.txt", screen._file_list[requested_file][0]) - screen.updateTextVariable("printpause.t0.txt", screen._file_list[requested_file][0]) + screen.updateTextVariable("printcnfirm.t0.txt", + screen._file_list[requested_file][0]) + screen.updateTextVariable("printpause.t0.txt", + screen._file_list[requested_file][0]) screen._requested_file = screen._file_list[requested_file][0] screen.updateNumericVariable("printcnfirm.t1.font", 0) screen.updateNumericVariable("printcnfirm.t2.font", 0) screen.updateNumericVariable("printcnfirm.t3.font", 0) - screen.updateTextVariable("printcnfirm.t1.txt", "Print this model?") + screen.updateTextVariable("printcnfirm.t1.txt", + "Print this model?") screen.updateTextVariable("printcnfirm.t2.txt", "Confirm") screen.updateTextVariable("printcnfirm.t3.txt", "Cancel") - screen.send_text("page printcnfirm") + screen.send_text("page printcnfirm") class FilamentLoadProcessor(CommandProcessor): - def process(self, message, screen): + def process(self, message, screen): move = screen.printer.lookup_object("gcode_move") current_e = move.get_status()["gcode_position"].e if message.command_data[0] == 0x01: #unload if move.absolute_coord: - screen.run_delayed_gcode(F"G0 E{(current_e - screen._filament_load_length)} F{screen._filament_load_feedrate}") + screen.run_delayed_gcode( + "G0 E%.2f F%.2f" % ( + (current_e - screen._filament_load_length), + screen._filament_load_feedrate + )) else: - screen.run_delayed_gcode(F"G0 E-{screen._filament_load_length} F{screen._filament_load_feedrate}") + screen.run_delayed_gcode( + "G0 E-%.2f F%d" %( + screen._filament_load_length, + screen._filament_load_feedrate + )) if message.command_data[0] == 0x02: #load if move.absolute_coord: - screen.run_delayed_gcode(F"G0 E{(current_e + screen._filament_load_length)} F{screen._filament_load_feedrate}") + screen.run_delayed_gcode( + "G0 E%.2f F%.2f" % ( + current_e + screen._filament_load_length, + screen._filament_load_feedrate + )) else: - screen.run_delayed_gcode(F"G0 E+{screen._filament_load_length} F{screen._filament_load_feedrate}") + screen.run_delayed_gcode( + "G0 E+%.2f F%d" %( + screen._filament_load_length, + screen._filament_load_feedrate + )) class Heater0LoadEnterProcessor(CommandProcessor): - def process(self, message, screen): - length = (message.command_data[0] & 0xff) << 8 | ((message.command_data[0] >> 8) & 0xff) + def process(self, message, screen): + length = (message.command_data[0] & 0xff) << 8 \ + | ((message.command_data[0] >> 8) & 0xff) screen._filament_load_length = length - screen.updateTextVariable("prefilament.filamentlength.txt", f"{screen._filament_load_length}") + screen.updateTextVariable("prefilament.filamentlength.txt", + "%d" % (screen._filament_load_length)) class Heater1LoadEnterProcessor(CommandProcessor): - def process(self, message, screen): - feedrate = (message.command_data[0] & 0xff) << 8 | ((message.command_data[0] >> 8) & 0xff) + def process(self, message, screen): + feedrate = (message.command_data[0] & 0xff) << 8 \ + | ((message.command_data[0] >> 8) & 0xff) screen._filament_load_feedrate = feedrate - screen.updateTextVariable("prefilament.filamentspeed.txt", f"{screen._filament_load_feedrate}") + screen.updateTextVariable("prefilament.filamentspeed.txt", + "%d" % (screen._filament_load_feedrate)) class PrintConfirmProcessor(CommandProcessor): - def process(self, message, screen): - screen.run_delayed_gcode(f"SDCARD_PRINT_FILE FILENAME=\"{screen._requested_file}\"") + def process(self, message, screen): + screen.run_delayed_gcode( + "SDCARD_PRINT_FILE FILENAME=\"%s\"" % ( + screen._requested_file + )) CommandProcessors = [ MainPageProcessor(DGUS_KEY_MAIN_PAGE), diff --git a/klippy/extras/serial_bridge.py b/klippy/extras/serial_bridge.py index 013432bcfa80..f22a68f01784 100644 --- a/klippy/extras/serial_bridge.py +++ b/klippy/extras/serial_bridge.py @@ -9,15 +9,22 @@ class SerialBridge: def __init__(self, config): - self.mcus = [] + self.mcus = {} self.configs = [] - self.printer = config.get_printer() + self.printer = config.get_printer() self.gcode = self.printer.lookup_object("gcode") - self.gcode.register_command("SERIAL_BRIDGE_SEND", self.cmd_SERIAL_BRIDGE_SEND, desc="Send a message to a uart bridge") - self.gcode.register_command("SERIAL_BRIDGE_LIST_CONFIGS", self.cmd_SERIAL_BRIDGE_LIST_CONFIGS, desc="List Available serial configs") - self.gcode.register_command("SERIAL_BRIDGE_LIST_BRIDGES", self.cmd_SERIAL_BRIDGE_LIST_BRIDGES, desc="List current bridges") + self.gcode.register_command("SERIAL_BRIDGE_SEND", + self.cmd_SERIAL_BRIDGE_SEND, + desc="Send a message to a uart bridge") + self.gcode.register_command("SERIAL_BRIDGE_LIST_CONFIGS", + self.cmd_SERIAL_BRIDGE_LIST_CONFIGS, + desc="List Available serial configs") + self.gcode.register_command("SERIAL_BRIDGE_LIST_BRIDGES", + self.cmd_SERIAL_BRIDGE_LIST_BRIDGES, + desc="List current bridges") self.printer.register_event_handler("klippy:ready", self.handle_ready) - self.printer.register_event_handler("klippy:disconnect", self.handle_disconnect) + self.printer.register_event_handler("klippy:disconnect", + self.handle_disconnect) self.bridges = {} def handle_ready(self): @@ -28,22 +35,26 @@ def handle_ready(self): self.configs = [] for n, mcu in self.mcus: constants = mcu.get_constants() - configs= ([f"{v}={k}" for k,v in constants.items() if k.startswith("SERIAL_BRIDGE_CONFIG")]) + configs= ( + ["%s=%s" % (k, v) for k,v in constants.items() \ + if k.startswith("SERIAL_BRIDGE_CONFIG")]) + self.configs.extend(configs) - logging.info(f"Serial bridge: available configs for {n}: " + ", ".join(configs)) + logging.info("Serial bridge: available configs for %s: " % (n) + + ", ".join(configs)) def handle_disconnect(self): pass def setup_bridge(self, bridge): - self.bridges[bridge.name.split()[-1]] = bridge + self.bridges[bridge.name.split()[-1]] = bridge def cmd_SERIAL_BRIDGE_LIST_CONFIGS(self, gcmd): gcmd.respond_info((", ".join(self.configs))) def cmd_SERIAL_BRIDGE_LIST_BRIDGES(self, gcmd): gcmd.respond_info((", ".join(self.bridges.keys()))) - + def cmd_SERIAL_BRIDGE_SEND(self, gcmd): text = gcmd.get("TEXT") bridge = gcmd.get("BRIDGE") @@ -51,12 +62,13 @@ def cmd_SERIAL_BRIDGE_SEND(self, gcmd): if not bridge: gcmd.respond_info("BRIDGE is required") return - + if bridge not in self.bridges: gcmd.respond_info("BRIDGE not found") return - self.bridges[bridge].send_serial(self.perform_replacement(gcmd.get("TEXT"))) + self.bridges[bridge].send_serial( + self.perform_replacement(gcmd.get("TEXT"))) def get_configs(self): return self.configs @@ -76,12 +88,12 @@ def perform_replacement(self, input_string): last_index = match.end() replaced_bytes.extend(input_string[last_index:].encode('utf-8')) - + return replaced_bytes class PrinterSerialBridge: def __init__(self, config): - self.callbacks = [] + self.callbacks = [] self.printer = config.get_printer() self.name = config.get_name().split()[-1] self.eol = config.get('eol', default='\n') @@ -92,8 +104,9 @@ def __init__(self, config): self.reactor = self.printer.get_reactor() self.printer.register_event_handler("klippy:ready", self.handle_ready) - self.printer.register_event_handler("klippy:disconnect", self.handle_disconnect) - + self.printer.register_event_handler("klippy:disconnect", + self.handle_disconnect) + ppins = self.printer.lookup_object("pins") pin_params = ppins.lookup_pin(config.get("tx_pin")) rx_pin_params = ppins.lookup_pin(config.get("rx_pin")) @@ -107,12 +120,12 @@ def __init__(self, config): self.serial_bridge.setup_bridge(self) def register_callback(self, callback): - self.callbacks.append(callback) + self.callbacks.append(callback) def chunkstring(self, msg, length): return (msg[0+i:length+i] for i in range(0, len(msg), length)) - def send_text(self, msg): + def send_text(self, msg): self.send_serial(bytes(msg, encoding='utf-8')) def send_serial(self, msg): @@ -120,7 +133,8 @@ def send_serial(self, msg): self.warn("Can't send message in a disconnected state") return - chunks = self.chunkstring(msg + self.serial_bridge.perform_replacement(self.eol), 40) + chunks = self.chunkstring( + msg + self.serial_bridge.perform_replacement(self.eol), 40) for chunk in chunks: byte_debug = ' '.join(['0x{:02x}'.format(byte) for byte in chunk]) self.log("Sending message: " + byte_debug) @@ -129,14 +143,19 @@ def send_serial(self, msg): def build_config(self): rest_ticks = self.mcu.seconds_to_clock(QUERY_TIME) clock = self.mcu.get_query_slot(self.oid) - self.mcu.add_config_cmd("command_config_serial_bridge oid=%d clock=%d rest_ticks=%d config=%d baud=%d" % (self.oid, clock, rest_ticks, self.serial_config, self.baud)) + self.mcu.add_config_cmd( + "command_config_serial_bridge oid=%d clock=%d rest_ticks=%d "\ + "config=%d baud=%d" % ( + self.oid, clock, rest_ticks, self.serial_config, self.baud + )) cmd_queue = self.mcu.alloc_command_queue() - self.mcu.register_response(self._handle_serial_bridge_response, "serial_bridge_response", self.oid) + self.mcu.register_response(self._handle_serial_bridge_response, + "serial_bridge_response", self.oid) self.serial_bridge_send_cmd = self.mcu.lookup_command( "serial_bridge_send oid=%c text=%*s", - cq=cmd_queue) + cq=cmd_queue) def _handle_serial_bridge_response(self, params): data = params["text"] @@ -149,14 +168,14 @@ def handle_ready(self): self._ready = True def handle_disconnect(self): - self._ready = False + self._ready = False def log(self, msg, *args, **kwargs): if self._logging: - logging.info(f"SERIAL BRIDGE {self.name}: " + str(msg)) + logging.info("SERIAL BRIDGE %s: " % (self.name) + str(msg) ) def warn(self, msg, *args, **kwargs): - logging.warning(f"SERIAL BRIDGE {self.name}: " + str(msg)) + logging.warning("SERIAL BRIDGE %s: " % (self.name) + str(msg)) def load_config(config): return SerialBridge(config) diff --git a/src/generic/serial_bridge_irq.c b/src/generic/serial_bridge_irq.c index 430e38598ab9..330db1fcbd80 100644 --- a/src/generic/serial_bridge_irq.c +++ b/src/generic/serial_bridge_irq.c @@ -13,13 +13,17 @@ #include "command.h" // DECL_CONSTANT #include "sched.h" // sched_wake_tasks #include "serial_bridge_irq.h" // serial_enable_tx_irq -#include "board/serial_bridge.h" //SERIAL_BRIDGE_COUNT +#include "board/serial_bridge.h" //SERIAL_BRIDGE_CNT -static uint8_t receive_bridge_buf[SERIAL_BRIDGE_COUNT][SERIAL_BRIDGE_RX_BUFFER_SIZE], receive_bridge_pos[SERIAL_BRIDGE_COUNT]; -static uint8_t transmit_bridge_buf[SERIAL_BRIDGE_COUNT][SERIAL_BRIDGE_RX_BUFFER_SIZE], transmit_bridge_pos[SERIAL_BRIDGE_COUNT], transmit_bridge_max[SERIAL_BRIDGE_COUNT]; +static uint8_t receive_bridge_buf + [SERIAL_BRIDGE_CNT][SERIAL_BRIDGE_RX_BUFF_SIZE], + receive_bridge_pos[SERIAL_BRIDGE_CNT]; +static uint8_t transmit_bridge_buf + [SERIAL_BRIDGE_CNT][SERIAL_BRIDGE_RX_BUFF_SIZE], +transmit_bridge_pos[SERIAL_BRIDGE_CNT], transmit_bridge_max[SERIAL_BRIDGE_CNT]; void serial_bridge_rx_byte(uint_fast8_t data, uint8_t usart_index) { - if (receive_bridge_pos[usart_index] >= SERIAL_BRIDGE_RX_BUFFER_SIZE) + if (receive_bridge_pos[usart_index] >= SERIAL_BRIDGE_RX_BUFF_SIZE) // Serial overflow - ignore return; sched_wake_tasks(); @@ -29,14 +33,16 @@ void serial_bridge_rx_byte(uint_fast8_t data, uint8_t usart_index) { int serial_bridge_get_tx_byte(uint8_t *pdata, uint8_t usart_index) { if (transmit_bridge_pos[usart_index] >= transmit_bridge_max[usart_index]) return -1; - *pdata = transmit_bridge_buf[usart_index][transmit_bridge_pos[usart_index]++]; + *pdata = + transmit_bridge_buf[usart_index][transmit_bridge_pos[usart_index]++]; return 0; } void serial_bridge_send(uint8_t* data, uint_fast8_t size, uint8_t config) { - uint8_t* usart_index_ptr = serial_bridge_get_uart_index_from_config(config); + uint8_t* usart_index_ptr + = serial_bridge_get_uart_index_from_config(config); if(usart_index_ptr == NULL){ return; @@ -45,7 +51,10 @@ serial_bridge_send(uint8_t* data, uint_fast8_t size, uint8_t config) uint8_t usart_index = *usart_index_ptr; // Verify space for message - uint_fast8_t tpos = readb(&transmit_bridge_pos[usart_index]), tmax = readb(&transmit_bridge_max[usart_index]); + uint_fast8_t tpos = + readb(&transmit_bridge_pos[usart_index]), + tmax = readb(&transmit_bridge_max[usart_index]); + if (tpos >= tmax) { tpos = tmax = 0; writeb(&transmit_bridge_max[usart_index], 0); @@ -60,7 +69,8 @@ serial_bridge_send(uint8_t* data, uint_fast8_t size, uint8_t config) writeb(&transmit_bridge_max[usart_index], 0); tpos = readb(&transmit_bridge_pos[usart_index]); tmax -= tpos; - memmove(&transmit_bridge_buf[usart_index][0], &transmit_bridge_buf[usart_index][tpos], tmax); + memmove(&transmit_bridge_buf[usart_index][0], + &transmit_bridge_buf[usart_index][tpos], tmax); writeb(&transmit_bridge_pos[usart_index], 0); writeb(&transmit_bridge_max[usart_index], tmax); serial_bridge_enable_tx_irq(usart_index); @@ -79,7 +89,8 @@ serial_bridge_send(uint8_t* data, uint_fast8_t size, uint8_t config) uint8_t serial_bridge_get_data(uint8_t* data, uint8_t config) { - uint8_t* usart_index_ptr = serial_bridge_get_uart_index_from_config(config); + uint8_t* usart_index_ptr + = serial_bridge_get_uart_index_from_config(config); if(usart_index_ptr == NULL){ return 0; diff --git a/src/generic/serial_bridge_irq.h b/src/generic/serial_bridge_irq.h index a9662cc628eb..e3d6f0a32dda 100644 --- a/src/generic/serial_bridge_irq.h +++ b/src/generic/serial_bridge_irq.h @@ -3,7 +3,7 @@ #include // uint32_t -#define SERIAL_BRIDGE_RX_BUFFER_SIZE 192 +#define SERIAL_BRIDGE_RX_BUFF_SIZE 192 #define SERIAL_BRIDGE_NUMBER_OF_CONFIGS = 5 // callback provided by board specific code @@ -21,4 +21,4 @@ uint8_t serial_bridge_get_data(uint8_t* data, uint8_t config); int8_t serial_bridge_configure(uint8_t* usart_index, uint32_t* baud); -#endif // serial_bridge_irq.h \ No newline at end of file +#endif // serial_bridge_irq.h diff --git a/src/serial_bridge.c b/src/serial_bridge.c index 780c158803ff..f8e1ef58488a 100644 --- a/src/serial_bridge.c +++ b/src/serial_bridge.c @@ -17,7 +17,7 @@ struct serial_bridge { struct timer timer; - uint8_t config; + uint8_t config; uint32_t baud; uint32_t rest_time; }; @@ -50,12 +50,14 @@ command_config_serial_bridge(uint32_t *args) sched_add_timer(&bridge->timer); } DECL_COMMAND(command_config_serial_bridge, - "command_config_serial_bridge oid=%c clock=%u rest_ticks=%u config=%c baud=%u"); + "command_config_serial_bridge oid=%c clock=%u" + " rest_ticks=%u config=%c baud=%u"); void command_serial_bridge_send(uint32_t *args) { - struct serial_bridge *sb = oid_lookup(args[0], command_config_serial_bridge); + struct serial_bridge *sb = oid_lookup(args[0], + command_config_serial_bridge); uint8_t data_len = args[1]; uint8_t *data = command_decode_ptr(args[2]); @@ -69,15 +71,16 @@ serial_bridge_task(void) if (!sched_check_wake(&serial_bridge_wake)) return; - static uint8_t buf[SERIAL_BRIDGE_RX_BUFFER_SIZE]; + static uint8_t buf[SERIAL_BRIDGE_RX_BUFF_SIZE]; uint8_t oid; struct serial_bridge *sb; foreach_oid(oid, sb, command_config_serial_bridge) { uint32_t data_len = serial_bridge_get_data(buf, sb->config); if (data_len) { - sendf("serial_bridge_response oid=%c text=%*s", oid, (uint8_t)data_len, buf); + sendf("serial_bridge_response oid=%c text=%*s", + oid, (uint8_t)data_len, buf); } - } + } } -DECL_TASK(serial_bridge_task); \ No newline at end of file +DECL_TASK(serial_bridge_task); diff --git a/src/stm32/Kconfig b/src/stm32/Kconfig index c02fbb9d7ca6..5b7a9d7d5958 100644 --- a/src/stm32/Kconfig +++ b/src/stm32/Kconfig @@ -499,7 +499,7 @@ config SERIAL_BRIDGE config ENABLE_SERIAL_BRIDGE_USART1 bool "USART1" if SERIAL_BRIDGE && !(STM32_SERIAL_USART1 || STM32_SERIAL_USART1_ALT_PB7_PB6) depends on SERIAL_BRIDGE -config ENABLE_SERIAL_BRIDGE_USART2 +config ENABLE_SERIAL_BRIDGE_USART2 bool "USART2" if SERIAL_BRIDGE && !(STM32_SERIAL_USART2 || STM32_SERIAL_USART2_ALT_PA15_PA14 || STM32_SERIAL_USART2_ALT_PB4_PB3 || STM32_SERIAL_USART2_ALT_PD6_PD5) depends on SERIAL_BRIDGE config ENABLE_SERIAL_BRIDGE_USART6 diff --git a/src/stm32/serial_bridge.c b/src/stm32/serial_bridge.c index 28c85939e22a..87d839771949 100644 --- a/src/stm32/serial_bridge.c +++ b/src/stm32/serial_bridge.c @@ -24,7 +24,7 @@ typedef struct serial_bridge_config { uint8_t rx_alt_function; uint8_t tx_alt_function; uint32_t baud; - uint8_t active; + uint8_t active; uint8_t usart_index; } serial_bridge_config_t; @@ -36,7 +36,7 @@ DECL_CONSTANT("SERIAL_BRIDGE_CONFIG_USART1_PB7,PB6", 1); #if CONFIG_ENABLE_SERIAL_BRIDGE_USART2 DECL_CONSTANT("SERIAL_BRIDGE_CONFIG_USART2_PD6,PD5", 2); DECL_CONSTANT("SERIAL_BRIDGE_CONFIG_USART2_PA3,PA2", 3); -#endif +#endif #if CONFIG_ENABLE_SERIAL_BRIDGE_USART6 DECL_CONSTANT("SERIAL_BRIDGE_CONFIG_USART6_PA12,PA11", 4); #endif @@ -120,7 +120,8 @@ serial_bridge_config_t* serial_bridge_get_config_by_number(uint8_t number){ return NULL; } -serial_bridge_config_t* serial_bridge_get_active_config_for_usart(USART_TypeDef* usart){ +serial_bridge_config_t* +serial_bridge_get_active_config_for_usart(USART_TypeDef* usart){ for(int8_t i = 0; i < (sizeof(configs) / sizeof(configs[0])); i++){ if(configs[i].usart == usart && configs[i].active){ return &configs[i]; @@ -149,38 +150,42 @@ void serial_bridge_handle_uart_irq(serial_bridge_config_t* config){ void USART1_serial_bridge_IRQHandler(void) { - serial_bridge_handle_uart_irq(serial_bridge_get_active_config_for_usart(USART1)); + serial_bridge_handle_uart_irq( + serial_bridge_get_active_config_for_usart(USART1)); } void USART2_serial_bridge_IRQHandler(void) { - serial_bridge_handle_uart_irq(serial_bridge_get_active_config_for_usart(USART2)); + serial_bridge_handle_uart_irq( + serial_bridge_get_active_config_for_usart(USART2)); } void USART6_serial_bridge_IRQHandler(void) { - serial_bridge_handle_uart_irq(serial_bridge_get_active_config_for_usart(USART6)); + serial_bridge_handle_uart_irq( + serial_bridge_get_active_config_for_usart(USART6)); } void serial_bridge_enable_tx_irq(int8_t usart_index) -{ +{ for(int8_t i = 0; i < (sizeof(configs) / sizeof(configs[0])); i++){ if(configs[i].usart_index == usart_index && configs[i].active){ configs[i].usart->CR1 = CR1_FLAGS | USART_CR1_TXEIE; } - } + } } -int8_t +int8_t serial_bridge_configure(uint8_t* config, uint32_t* baud) { - serial_bridge_config_t* s_config = serial_bridge_get_config_by_number(*config); + serial_bridge_config_t* s_config = + serial_bridge_get_config_by_number(*config); if (config == NULL) { return -1; - } + } s_config->baud = *baud; s_config->active = 1; @@ -192,8 +197,10 @@ serial_bridge_configure(uint8_t* config, uint32_t* baud) | ((div % 16) << USART_BRR_DIV_Fraction_Pos)); s_config->usart->CR1 = CR1_FLAGS; - gpio_peripheral(s_config->rx_pin, GPIO_FUNCTION(s_config->rx_alt_function), 1); - gpio_peripheral(s_config->tx_pin, GPIO_FUNCTION(s_config->tx_alt_function), 0); + gpio_peripheral(s_config->rx_pin, + GPIO_FUNCTION(s_config->rx_alt_function), 1); + gpio_peripheral(s_config->tx_pin, + GPIO_FUNCTION(s_config->tx_alt_function), 0); return 0; } diff --git a/src/stm32/serial_bridge.h b/src/stm32/serial_bridge.h index 17ada8923b8e..69685f557ff6 100644 --- a/src/stm32/serial_bridge.h +++ b/src/stm32/serial_bridge.h @@ -1,5 +1,6 @@ #include -#define SERIAL_BRIDGE_COUNT CONFIG_ENABLE_SERIAL_BRIDGE_USART1 + CONFIG_ENABLE_SERIAL_BRIDGE_USART2 + CONFIG_ENABLE_SERIAL_BRIDGE_USART6 +#define SERIAL_BRIDGE_CNT CONFIG_ENABLE_SERIAL_BRIDGE_USART1 + \ + CONFIG_ENABLE_SERIAL_BRIDGE_USART2 + CONFIG_ENABLE_SERIAL_BRIDGE_USART6 -uint8_t* serial_bridge_get_uart_index_from_config(uint8_t config); \ No newline at end of file +uint8_t* serial_bridge_get_uart_index_from_config(uint8_t config);