Skip to content

Instantly share code, notes, and snippets.

@jeffeb3
Created July 22, 2020 04:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeffeb3/4ee3c57ff4fbe9328d4697357f5bbf0d to your computer and use it in GitHub Desktop.
#!/bin/python3
#
# A little python script that can act enough like a Marlin printer to test out a UART screen.
# I connected it to the screen with an FTDI. It connects at /dev/ttyUSB0
import serial
import time
class Screen(object):
def __init__(self, port, speed):
self.serial = serial.Serial(port, speed, timeout=0.05)
print("Opened on port: {}".format(self.serial.name))
self.x = 10.0
self.y = 100.0
self.z = 1.0
def send(self, message):
if message != b'ok' and not message.startswith(b'T:24.62') and not message.startswith(b'X:'):
print(" "*60 + message.decode("utf-8"))
self.serial.write(message + b'\r\n')
def parseLine(self, message):
if b'M105' not in message and b'M114' not in message:
print(message.decode("utf-8").strip())
if b'M105' in message:
screen.send(b'T:24.62 /0.00 B:24.75 /0.00 @:0 B@:0')
elif b'M115' in message:
screen.send(b'FIRMWARE_NAME:Marlin 2.0.5.3 (GitHub, jeffeb3) SOURCE_CODE_URL:https://github.com/MarlinFirmware/Marlin PROTOCOL_VERSION:1.0 MACHINE_TYPE:3D Printer EXTRUDER_COUNT:1 UUID:cede2a2f-41a2-4748-9b12-c55c62f367ff')
screen.send(b'Cap:SERIAL_XON_XOFF:0')
screen.send(b'Cap:BINARY_FILE_TRANSFER:0')
screen.send(b'Cap:EEPROM:1')
screen.send(b'Cap:VOLUMETRIC:1')
screen.send(b'Cap:AUTOREPORT_TEMP:1')
screen.send(b'Cap:PROGRESS:0')
screen.send(b'Cap:PRINT_JOB:1')
screen.send(b'Cap:AUTOLEVEL:1')
screen.send(b'Cap:Z_PROBE:1')
screen.send(b'Cap:LEVELING_DATA:1')
screen.send(b'Cap:BUILD_PERCENT:0')
screen.send(b'Cap:SOFTWARE_POWER:0')
screen.send(b'Cap:TOGGLE_LIGHTS:0')
screen.send(b'Cap:CASE_LIGHT_BRIGHTNESS:0')
screen.send(b'Cap:EMERGENCY_PARSER:0')
screen.send(b'Cap:PROMPT_SUPPORT:0')
screen.send(b'Cap:AUTOREPORT_SD_STATUS:0')
screen.send(b'Cap:THERMAL_PROTECTION:1')
screen.send(b'Cap:MOTION_MODES:0')
screen.send(b'Cap:CHAMBER_TEMPERATURE:0')
elif b'M503 S0' in message:
screen.send(b'G21 ; Units in mm (mm)')
screen.send(b'')
screen.send(b'M200 D1.75')
screen.send(b'M200 D0')
screen.send(b'M92 X80.00 Y80.00 Z400.00 E400.00')
screen.send(b'M203 X200.00 Y200.00 Z10.00 E50.00')
screen.send(b'M201 X3000.00 Y3000.00 Z100.00 E20000.00')
screen.send(b'M204 P4500.00 R3000.00 T9000.00')
screen.send(b'M205 B20000.00 S0.00 T0.00 X10.00 Y10.00 Z0.40 E5.00')
screen.send(b'M206 X0.00 Y0.00 Z0.00')
screen.send(b'M420 S1 Z0.00')
screen.send(b'M301 P26.15 I2.12 D80.66')
screen.send(b'M304 P38.35 I2.40 D408.81')
screen.send(b'M851 X30.00 Y0.00 Z-0.52')
screen.send(b'M906 X900 Y900 Z900')
screen.send(b'M906 T0 E900')
screen.send(b'')
screen.send(b'M913 X100 Y100 Z20')
screen.send(b'M913 T0 E30')
screen.send(b'')
screen.send(b'M914 X50 Y50')
screen.send(b'M569 S1 X Y')
screen.send(b'M900 K0.50')
elif b'M92' in message:
screen.send(b'echo: M92 X80.00 Y80.00 Z400.00 E400.00')
elif b'M420 S1' in message:
screen.send(b"echo:Jeffeb3's mad world of TFT sim")
elif b'M114' in message:
screen.send('X:{} Y:{} Z:{}'.format(self.x, self.y, self.z).encode('utf-8'))
elif message.startswith(b'G1 '):
fields = message.split()
for field in fields:
if field.startswith(b'X'):
distance = float(field[1:])
self.x += distance
if field.startswith(b'Y'):
distance = float(field[1:])
self.y += distance
if field.startswith(b'Z'):
distance = float(field[1:])
self.z += distance
elif message.startswith(b'G4 '):
fields = message.split()
for field in fields:
if field.startswith(b'S'):
seconds = float(field[1:])
print('waiting {} seconds'.format(seconds))
time.sleep(seconds)
break
screen.send(b'ok')
screen = Screen("/dev/ttyUSB0", 250000)
while True:
recv = screen.serial.readline()
if (recv):
screen.parseLine(recv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment