Skip to content

Commit

Permalink
Merge pull request #154 from OctoPrint/pre-flash-gcode
Browse files Browse the repository at this point in the history
Add pre-flash gcode command option
  • Loading branch information
benlye authored Nov 27, 2020
2 parents 7a5f95d + 70c8202 commit 53adb61
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,35 @@ Erase: `{bossac} -i -p {port} -U true -e -w {disableverify} -b {firmware} -R`
Flash:

## Pre and Post-flash Settings
#### Pre-flash Command
Specify a system command to run on the host prior to flashing.

#### Pre-flash Command
Specify a system command to run on the host after flashing.
The flash sequence is:
1. Execute the pre-flash system command(s) on the host
1. Send the pre-flash gcode commands(s) to the printer
1. Pause for the pre-flash gcode delay
1. Disconnect the printer
1. Execute the firmware update
1. Pause for the post-flash delay
1. Execute the post-flash system command(s) on the host
1. Reconnect the printer
1. Send the post-flash gcode command(s) to the printer

#### Pre-flash System Command
Specify a system command or script to run on the host prior to flashing. Multiple commands can be separated with a semicolon.

#### Pre-flash Gcode
Specify gcode commands to run on the printer prior to flashing. Multiple commands can be separated with a semicolon.
**Commands are only run if the printer is connected when flashing is initiated**

#### Pre-flash Gcode Delay
Delay after sending pre-flash gcode. Allows time for code to complete before initiating flash.

#### Post-flash Delay ####
This setting can be used to insert a delay of up to 180s after the firmware has been uploaded. This can be useful if the board takes some time to restart. A delay of 20-30s is usually enough.

#### Post-flash Gcode ####
#### Post-flash System Command
Specify a system command or script to run on the host after flashing. Multiple commands can be separated with a semicolon.

#### Post-flash Gcode
You can use the post-flash gcode settings to run gcode commands after a successful firmware flash.
The post-flash code will run more or less immediately if the printer was connected before the flash started (so reconnects automatically when the flash finishes), or whenever the printer is manually reconnected after the firmware is flashed.

Expand Down
24 changes: 21 additions & 3 deletions octoprint_firmwareupdater/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def flash_firmware(self):
uploaded_hex_path = flask.request.values[input_upload_path]

# create a temporary

try:
file_to_flash = tempfile.NamedTemporaryFile(mode='r+b', delete=False)
file_to_flash.close()
Expand Down Expand Up @@ -192,7 +192,7 @@ def _flash_worker(self, method, firmware, printer_port):
except:
e = sys.exc_info()[0]
self._logger.error("Error executing pre-flash commandline '{}'".format(preflash_command))

self._logger.info("Pre-flash command '{}' returned: {}".format(preflash_command, r))

try:
Expand All @@ -211,6 +211,20 @@ def _flash_worker(self, method, firmware, printer_port):
self._send_status("flasherror", message=error_message)
return

preflash_gcode = self._settings.get(["preflash_gcode"])
if preflash_gcode is not None and self._settings.get_boolean(["enable_preflash_gcode"]):
if self._printer.is_operational():
self._logger.info("Sending pre-flash gcode commands: {}".format(preflash_gcode))
self._printer.commands(preflash_gcode.split(";"))

preflash_delay = self._settings.get(["preflash_delay"]) or 3
if float(preflash_delay) > 0 and self._settings.get(["enable_preflash_delay"]):
self._logger.info("Pre-flash delay: {}s".format(preflash_delay))
time.sleep(float(preflash_delay))

else:
self._logger.info("Printer not connected, not sending pre-flash gcode commands")

reconnect = None
if self._printer.is_operational():
_, current_port, current_baudrate, current_profile = self._printer.get_current_connection()
Expand Down Expand Up @@ -245,7 +259,7 @@ def _flash_worker(self, method, firmware, printer_port):
except:
e = sys.exc_info()[0]
self._logger.error("Error executing post-flash commandline '{}'".format(postflash_command))

self._logger.info("Post-flash command '{}' returned: {}".format(postflash_command, r))

postflash_gcode = self._settings.get(["postflash_gcode"])
Expand Down Expand Up @@ -309,14 +323,18 @@ def get_settings_defaults(self):
"lpc1768_path": None,
"lpc1768_preflashreset": True,
"postflash_delay": "0",
"preflash_delay": "3",
"postflash_gcode": None,
"preflash_gcode": None,
"run_postflash_gcode": False,
"preflash_commandline": None,
"postflash_commandline": None,
"enable_preflash_commandline": None,
"enable_postflash_commandline": None,
"enable_postflash_delay": None,
"enable_preflash_delay": None,
"enable_postflash_gcode": None,
"enable_preflash_gcode": None,
"disable_bootloadercheck": None
}

Expand Down
26 changes: 22 additions & 4 deletions octoprint_firmwareupdater/static/js/firmwareupdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ $(function() {
self.showStm32flashConfig = ko.observable(false);
self.showPostflashConfig = ko.observable(false);
self.configEnablePostflashDelay = ko.observable();
self.configEnablePreflashDelay = ko.observable();
self.configPostflashDelay = ko.observable();
self.configPreflashDelay = ko.observable();
self.configEnablePostflashGcode = ko.observable();
self.configPostflashGcode = ko.observable();
self.configDisableBootloaderCheck = ko.observable();
self.configEnablePreflashCommandline = ko.observable();
self.configPreflashCommandline = ko.observable();
self.configEnablePostflashCommandline = ko.observable();
self.configPostflashCommandline = ko.observable();
self.configEnablePreflashGcode = ko.observable();
self.configPreflashGcode = ko.observable();

// Config settings for avrdude
self.configAvrdudeMcu = ko.observable();
Expand Down Expand Up @@ -436,7 +440,9 @@ $(function() {
self.configPreflashCommandline(self.settingsViewModel.settings.plugins.firmwareupdater.preflash_commandline());
self.configPostflashCommandline(self.settingsViewModel.settings.plugins.firmwareupdater.postflash_commandline());
self.configPostflashDelay(self.settingsViewModel.settings.plugins.firmwareupdater.postflash_delay());
self.configPreflashDelay(self.settingsViewModel.settings.plugins.firmwareupdater.preflash_delay());
self.configPostflashGcode(self.settingsViewModel.settings.plugins.firmwareupdater.postflash_gcode());
self.configPreflashGcode(self.settingsViewModel.settings.plugins.firmwareupdater.preflash_gcode());

if(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_commandline() != 'false') {
self.configEnablePreflashCommandline(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_commandline());
Expand All @@ -449,11 +455,19 @@ $(function() {
if(self.settingsViewModel.settings.plugins.firmwareupdater.enable_postflash_delay() != 'false') {
self.configEnablePostflashDelay(self.settingsViewModel.settings.plugins.firmwareupdater.enable_postflash_delay());
}


if(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_delay() != 'false') {
self.configEnablePreflashDelay(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_delay());
}

if(self.settingsViewModel.settings.plugins.firmwareupdater.enable_postflash_gcode() != 'false') {
self.configEnablePostflashGcode(self.settingsViewModel.settings.plugins.firmwareupdater.enable_postflash_gcode());
}


if(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_gcode() != 'false') {
self.configEnablePreflashGcode(self.settingsViewModel.settings.plugins.firmwareupdater.enable_preflash_gcode());
}

if(self.settingsViewModel.settings.plugins.firmwareupdater.disable_bootloadercheck() != 'false') {
self.configDisableBootloaderCheck(self.settingsViewModel.settings.plugins.firmwareupdater.disable_bootloadercheck());
}
Expand All @@ -473,13 +487,13 @@ $(function() {
self.configBossacPath(self.settingsViewModel.settings.plugins.firmwareupdater.bossac_path());
self.configBossacDisableVerification(self.settingsViewModel.settings.plugins.firmwareupdater.bossac_disableverify());
self.configBossacCommandLine(self.settingsViewModel.settings.plugins.firmwareupdater.bossac_commandline());

// Load the dfu-programmer settings
self.configDfuPath(self.settingsViewModel.settings.plugins.firmwareupdater.dfuprog_path());
self.configDfuMcu(self.settingsViewModel.settings.plugins.firmwareupdater.dfuprog_avrmcu());
self.configDfuCommandLine(self.settingsViewModel.settings.plugins.firmwareupdater.dfuprog_commandline());
self.configDfuEraseCommandLine(self.settingsViewModel.settings.plugins.firmwareupdater.dfuprog_erasecommandline());

// Load the lpc1768 settings
self.configLpc1768Path(self.settingsViewModel.settings.plugins.firmwareupdater.lpc1768_path());
if(self.settingsViewModel.settings.plugins.firmwareupdater.lpc1768_preflashreset() != 'false') {
Expand Down Expand Up @@ -542,9 +556,13 @@ $(function() {
enable_postflash_commandline: self.configEnablePostflashCommandline(),
postflash_commandline: self.configPostflashCommandline(),
postflash_delay: self.configPostflashDelay(),
preflash_delay: self.configPreflashDelay(),
postflash_gcode: self.configPostflashGcode(),
preflash_gcode: self.configPreflashGcode(),
enable_postflash_delay: self.configEnablePostflashDelay(),
enable_preflash_delay: self.configEnablePreflashDelay(),
enable_postflash_gcode: self.configEnablePostflashGcode(),
enable_preflash_gcode: self.configEnablePreflashGcode(),
disable_bootloadercheck: self.configDisableBootloaderCheck()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@
</label>
<!-- Pre-flash commandline -->
<div class="control-group">
<label class="control-label">{{ _('Pre-flash command') }}</label>
<label class="control-label">{{ _('Pre-flash system command') }}</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: configEnablePreflashCommandline"> Enabled
Expand All @@ -445,10 +445,41 @@
</div>
</div>
<!-- End pre-flash commandline -->


<!-- Pre-flash gcode -->
<div class="control-group">
<label class="control-label">{{ _('Pre-flash gcode') }}</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: configEnablePreflashGcode"> Enabled
</label>
<div class="input">
<input type="text" class="input-block-level" data-bind="value: configPreflashGcode, enable: configEnablePreflashGcode">
</div>
<span class="help-block">{{ _('Gcode commands to run before blashing. <b>Pre-flash gcode commands will only run if the printer is connected.</b> Separate multiple commands with a semi colon.') }}</span>
</div>
</div>
<!-- End pre-flash gcode -->

<!-- Pre-flash gcode delay -->
<div class="control-group">
<label class="control-label">{{ _('Pre-flash gcode delay') }}</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: configEnablePreflashDelay"> Enabled
</label>
<div class="input-append">
<input type="number" class="input-mini text-right" step=any min="0" max="30" data-bind="value: configPreflashDelay, enable: configEnablePreflashDelay">
<span class="add-on">seconds</span>
</div>
<span class="help-block">{{ _('Delay after sending pre-flash gcode. Allows time for code to complete before initiating flash.') }}</span>
</div>
</div>
<!-- End pre-flash delay -->

<!-- Post-flash commandline -->
<div class="control-group">
<label class="control-label">{{ _('Post-flash command') }}</label>
<label class="control-label">{{ _('Post-flash system command') }}</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: configEnablePostflashCommandline"> Enabled
Expand All @@ -463,7 +494,7 @@

<!-- Post-flash delay -->
<div class="control-group">
<label class="control-label">{{ _('Post-flash delay') }}</label>
<label class="control-label">{{ _('Post-flash command delay') }}</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: configEnablePostflashDelay"> Enabled
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-FirmwareUpdater"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.7.0"
plugin_version = "1.7.1"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 53adb61

Please sign in to comment.