gcode: Add minval/maxval/above/below options to get_X parsers
Add value checking to gcode parameter parsing code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
8f4f5da11c
commit
d5dc6b785d
|
@ -183,8 +183,6 @@ class CartKinematics:
|
||||||
cmd_SET_DUAL_CARRIAGE_help = "Set which carriage is active"
|
cmd_SET_DUAL_CARRIAGE_help = "Set which carriage is active"
|
||||||
def cmd_SET_DUAL_CARRIAGE(self, params):
|
def cmd_SET_DUAL_CARRIAGE(self, params):
|
||||||
gcode = self.printer.lookup_object('gcode')
|
gcode = self.printer.lookup_object('gcode')
|
||||||
carriage = gcode.get_int('CARRIAGE', params)
|
carriage = gcode.get_int('CARRIAGE', params, minval=0, maxval=1)
|
||||||
if carriage not in (0, 1):
|
|
||||||
raise gcode.error("Invalid carriage")
|
|
||||||
self._activate_carriage(carriage)
|
self._activate_carriage(carriage)
|
||||||
gcode.reset_last_position()
|
gcode.reset_last_position()
|
||||||
|
|
|
@ -49,14 +49,13 @@ class PrinterOutputPin:
|
||||||
return pin.cmd_SET_PIN(params)
|
return pin.cmd_SET_PIN(params)
|
||||||
if self.is_static:
|
if self.is_static:
|
||||||
raise self.gcode.error("Static pin can not be changed at run-time")
|
raise self.gcode.error("Static pin can not be changed at run-time")
|
||||||
value = self.gcode.get_float('VALUE', params) / self.scale
|
value = self.gcode.get_float('VALUE', params, minval=0., maxval=1.)
|
||||||
|
value /= self.scale
|
||||||
if value == self.last_value:
|
if value == self.last_value:
|
||||||
return
|
return
|
||||||
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
||||||
print_time = max(print_time, self.last_value_time + PIN_MIN_TIME)
|
print_time = max(print_time, self.last_value_time + PIN_MIN_TIME)
|
||||||
if self.is_pwm:
|
if self.is_pwm:
|
||||||
if value < 0. or value > 1.:
|
|
||||||
raise self.gcode.error("Invalid pin value")
|
|
||||||
self.mcu_pin.set_pwm(print_time, value)
|
self.mcu_pin.set_pwm(print_time, value)
|
||||||
else:
|
else:
|
||||||
if value not in [0., 1.]:
|
if value not in [0., 1.]:
|
||||||
|
|
|
@ -102,7 +102,7 @@ class VirtualSD:
|
||||||
# Set SD position
|
# Set SD position
|
||||||
if self.work_timer is not None:
|
if self.work_timer is not None:
|
||||||
raise self.gcode.error("SD busy")
|
raise self.gcode.error("SD busy")
|
||||||
pos = self.gcode.get_int('S', params)
|
pos = self.gcode.get_int('S', params, minval=0)
|
||||||
self.file_position = pos
|
self.file_position = pos
|
||||||
def cmd_M27(self, params):
|
def cmd_M27(self, params):
|
||||||
# Report SD print status
|
# Report SD print status
|
||||||
|
|
|
@ -228,16 +228,16 @@ class PrinterExtruder:
|
||||||
def cmd_SET_PRESSURE_ADVANCE(self, params):
|
def cmd_SET_PRESSURE_ADVANCE(self, params):
|
||||||
self.printer.lookup_object('toolhead').get_last_move_time()
|
self.printer.lookup_object('toolhead').get_last_move_time()
|
||||||
gcode = self.printer.lookup_object('gcode')
|
gcode = self.printer.lookup_object('gcode')
|
||||||
if 'ADVANCE' in params:
|
pressure_advance = gcode.get_float(
|
||||||
v = gcode.get_float('ADVANCE', params)
|
'ADVANCE', params, self.pressure_advance, minval=0.)
|
||||||
self.pressure_advance = v if v > 0. else 0.
|
pressure_advance_lookahead_time = gcode.get_float(
|
||||||
if 'ADVANCE_LOOKAHEAD_TIME' in params:
|
'ADVANCE_LOOKAHEAD_TIME', params,
|
||||||
v = gcode.get_float('ADVANCE_LOOKAHEAD_TIME', params)
|
self.pressure_advance_lookahead_time, minval=0.)
|
||||||
self.pressure_advance_lookahead_time = v if v > 0. else 0.
|
self.pressure_advance = pressure_advance
|
||||||
|
self.pressure_advance_lookahead_time = pressure_advance_lookahead_time
|
||||||
msg = ("pressure_advance: %.6f\n"
|
msg = ("pressure_advance: %.6f\n"
|
||||||
"pressure_advance_lookahead_time: %.6f\n" % (
|
"pressure_advance_lookahead_time: %.6f" % (
|
||||||
self.pressure_advance,
|
pressure_advance, pressure_advance_lookahead_time))
|
||||||
self.pressure_advance_lookahead_time))
|
|
||||||
self.printer.set_rollover_info(self.name, "%s: %s" % (self.name, msg))
|
self.printer.set_rollover_info(self.name, "%s: %s" % (self.name, msg))
|
||||||
gcode.respond_info(msg)
|
gcode.respond_info(msg)
|
||||||
|
|
||||||
|
|
|
@ -250,20 +250,38 @@ class GCodeParser:
|
||||||
self.respond('!! %s' % (lines[0].strip(),))
|
self.respond('!! %s' % (lines[0].strip(),))
|
||||||
# Parameter parsing helpers
|
# Parameter parsing helpers
|
||||||
class sentinel: pass
|
class sentinel: pass
|
||||||
def get_str(self, name, params, default=sentinel, parser=str):
|
def get_str(self, name, params, default=sentinel, parser=str,
|
||||||
if name in params:
|
minval=None, maxval=None, above=None, below=None):
|
||||||
try:
|
if name not in params:
|
||||||
return parser(params[name])
|
if default is self.sentinel:
|
||||||
except:
|
raise error("Error on '%s': missing %s" % (
|
||||||
raise error("Error on '%s': unable to parse %s" % (
|
params['#original'], name))
|
||||||
params['#original'], params[name]))
|
|
||||||
if default is not self.sentinel:
|
|
||||||
return default
|
return default
|
||||||
raise error("Error on '%s': missing %s" % (params['#original'], name))
|
try:
|
||||||
def get_int(self, name, params, default=sentinel):
|
value = parser(params[name])
|
||||||
return self.get_str(name, params, default, parser=int)
|
except:
|
||||||
def get_float(self, name, params, default=sentinel):
|
raise error("Error on '%s': unable to parse %s" % (
|
||||||
return self.get_str(name, params, default, parser=float)
|
params['#original'], params[name]))
|
||||||
|
if minval is not None and value < minval:
|
||||||
|
raise self.error("Error on '%s': %s must have minimum of %s" % (
|
||||||
|
params['#original'], name, minval))
|
||||||
|
if maxval is not None and value > maxval:
|
||||||
|
raise self.error("Error on '%s': %s must have maximum of %s" % (
|
||||||
|
params['#original'], name, maxval))
|
||||||
|
if above is not None and value <= above:
|
||||||
|
raise self.error("Error on '%s': %s must be above %s" % (
|
||||||
|
params['#original'], name, above))
|
||||||
|
if below is not None and value >= below:
|
||||||
|
raise self.error("Error on '%s': %s must be below %s" % (
|
||||||
|
params['#original'], name, below))
|
||||||
|
return value
|
||||||
|
def get_int(self, name, params, default=sentinel, minval=None, maxval=None):
|
||||||
|
return self.get_str(name, params, default, parser=int,
|
||||||
|
minval=minval, maxval=maxval)
|
||||||
|
def get_float(self, name, params, default=sentinel,
|
||||||
|
minval=None, maxval=None, above=None, below=None):
|
||||||
|
return self.get_str(name, params, default, parser=float, minval=minval,
|
||||||
|
maxval=maxval, above=above, below=below)
|
||||||
extended_r = re.compile(
|
extended_r = re.compile(
|
||||||
r'^\s*(?:N[0-9]+\s*)?'
|
r'^\s*(?:N[0-9]+\s*)?'
|
||||||
r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)'
|
r'(?P<cmd>[a-zA-Z_][a-zA-Z_]+)(?:\s+|$)'
|
||||||
|
@ -310,9 +328,9 @@ class GCodeParser:
|
||||||
if is_bed:
|
if is_bed:
|
||||||
heater = self.heaters[-1]
|
heater = self.heaters[-1]
|
||||||
elif 'T' in params:
|
elif 'T' in params:
|
||||||
heater_index = self.get_int('T', params)
|
index = self.get_int(
|
||||||
if heater_index >= 0 and heater_index < len(self.heaters) - 1:
|
'T', params, minval=0, maxval=len(self.heaters)-2)
|
||||||
heater = self.heaters[heater_index]
|
heater = self.heaters[index]
|
||||||
elif self.extruder is not None:
|
elif self.extruder is not None:
|
||||||
heater = self.extruder.get_heater()
|
heater = self.extruder.get_heater()
|
||||||
if heater is None:
|
if heater is None:
|
||||||
|
@ -349,11 +367,8 @@ class GCodeParser:
|
||||||
self.respond_info('Unknown command:"%s"' % (cmd,))
|
self.respond_info('Unknown command:"%s"' % (cmd,))
|
||||||
def cmd_Tn(self, params):
|
def cmd_Tn(self, params):
|
||||||
# Select Tool
|
# Select Tool
|
||||||
index = self.get_int('T', params)
|
|
||||||
extruders = extruder.get_printer_extruders(self.printer)
|
extruders = extruder.get_printer_extruders(self.printer)
|
||||||
if self.extruder is None or index < 0 or index >= len(extruders):
|
index = self.get_int('T', params, minval=0, maxval=len(extruders)-1)
|
||||||
self.respond_error("Extruder %d not configured" % (index,))
|
|
||||||
return
|
|
||||||
e = extruders[index]
|
e = extruders[index]
|
||||||
if self.extruder is e:
|
if self.extruder is e:
|
||||||
return
|
return
|
||||||
|
@ -409,9 +424,9 @@ class GCodeParser:
|
||||||
def cmd_G4(self, params):
|
def cmd_G4(self, params):
|
||||||
# Dwell
|
# Dwell
|
||||||
if 'S' in params:
|
if 'S' in params:
|
||||||
delay = self.get_float('S', params)
|
delay = self.get_float('S', params, minval=0.)
|
||||||
else:
|
else:
|
||||||
delay = self.get_float('P', params, 0.) / 1000.
|
delay = self.get_float('P', params, 0., minval=0.) / 1000.
|
||||||
self.toolhead.dwell(delay)
|
self.toolhead.dwell(delay)
|
||||||
def cmd_G28(self, params):
|
def cmd_G28(self, params):
|
||||||
# Move to origin
|
# Move to origin
|
||||||
|
@ -472,15 +487,11 @@ class GCodeParser:
|
||||||
self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f" % tuple(p))
|
self.respond("X:%.3f Y:%.3f Z:%.3f E:%.3f" % tuple(p))
|
||||||
def cmd_M220(self, params):
|
def cmd_M220(self, params):
|
||||||
# Set speed factor override percentage
|
# Set speed factor override percentage
|
||||||
value = self.get_float('S', params, 100.) / (60. * 100.)
|
value = self.get_float('S', params, 100., above=0.) / (60. * 100.)
|
||||||
if value <= 0.:
|
|
||||||
raise error("Invalid factor in '%s'" % (params['#original'],))
|
|
||||||
self.speed_factor = value
|
self.speed_factor = value
|
||||||
def cmd_M221(self, params):
|
def cmd_M221(self, params):
|
||||||
# Set extrude factor override percentage
|
# Set extrude factor override percentage
|
||||||
new_extrude_factor = self.get_float('S', params, 100.) / 100.
|
new_extrude_factor = self.get_float('S', params, 100., above=0.) / 100.
|
||||||
if new_extrude_factor <= 0.:
|
|
||||||
raise error("Invalid factor in '%s'" % (params['#original'],))
|
|
||||||
last_e_pos = self.last_position[3]
|
last_e_pos = self.last_position[3]
|
||||||
e_value = (last_e_pos - self.base_position[3]) / self.extrude_factor
|
e_value = (last_e_pos - self.base_position[3]) / self.extrude_factor
|
||||||
self.base_position[3] = last_e_pos - e_value * new_extrude_factor
|
self.base_position[3] = last_e_pos - e_value * new_extrude_factor
|
||||||
|
@ -523,7 +534,7 @@ class GCodeParser:
|
||||||
self.set_temp(params, is_bed=True, wait=True)
|
self.set_temp(params, is_bed=True, wait=True)
|
||||||
def cmd_M106(self, params):
|
def cmd_M106(self, params):
|
||||||
# Set fan speed
|
# Set fan speed
|
||||||
self.set_fan_speed(self.get_float('S', params, 255.) / 255.)
|
self.set_fan_speed(self.get_float('S', params, 255., minval=0.) / 255.)
|
||||||
def cmd_M107(self, params):
|
def cmd_M107(self, params):
|
||||||
# Turn fan off
|
# Turn fan off
|
||||||
self.set_fan_speed(0.)
|
self.set_fan_speed(0.)
|
||||||
|
|
Loading…
Reference in New Issue