Skip to content

Instantly share code, notes, and snippets.

@hdo
Created August 27, 2017 09:01
Show Gist options
  • Save hdo/849d13f77f892bcc3459ab1bb2b9069a to your computer and use it in GitHub Desktop.
Save hdo/849d13f77f892bcc3459ab1bb2b9069a to your computer and use it in GitHub Desktop.
Simple pygame script to test new jog feature in GRBL v1.1 (now including X,Y and Z axis)
# requires pygame and pyserial
import pygame
import os
import serial
import time
class KeyButton(object):
def __init__(self, key_id, name, cmd_short, cmd_long):
self.key_id = key_id
self.name = name
self.last_ms_down = 0
self.last_state = 0 # 1 = pressed
self.active = False
self.cmd_short = cmd_short
self.cmd_long = cmd_long
def check_key_down(self, current_ms, event_key):
if event_key == self.key_id:
self.active = True
self.last_ms_down = current_ms
def check_key_up(self, current_ms, event_key):
if event_key == self.key_id:
self.active = False
self.last_state = 0
def is_pressed_long(self, current_ms, seconds):
return current_ms - self.last_ms_down > seconds
def create_screen():
if os.name == "nt":
return pygame.display.set_mode((320, 200))
else:
drivers = ['fbcon', 'directfb', 'svgalib']
found = False
for driver in drivers:
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
continue
found = True
break
return pygame.display.set_mode((320, 200))
pygame.init()
clock = pygame.time.Clock()
screen = create_screen()
gameExit = False
print "start"
jog_active = False
speed = 400
cmd_base = "$J=G91 G21 X%d Y%d F%d\n"
cmd = "$J=G91 G21 X-1 F1500\n"
serial_enable = True
if serial_enable:
ser = serial.Serial('/dev/ttyACM0', baudrate=115200)
print ser.portstr
all_keys = []
all_keys.append(KeyButton(pygame.K_UP, "up", "$J=G91 G21 Y1 F500\n", "$J=G91 G21 Y1000 F4000\n"))
all_keys.append(KeyButton(pygame.K_DOWN, "down", "$J=G91 G21 Y-1 F500\n", "$J=G91 G21 Y-1000 F4000\n"))
all_keys.append(KeyButton(pygame.K_LEFT, "left", "$J=G91 G21 X-1 F500\n", "$J=G91 G21 X-1000 F4000\n"))
all_keys.append(KeyButton(pygame.K_RIGHT, "right", "$J=G91 G21 X1 F500\n", "$J=G91 G21 X1000 F4000\n"))
all_keys.append(KeyButton(pygame.K_PAGEUP, "right", "$J=G91 G21 Z0.1 F500\n", "$J=G91 G21 Z200 F1000\n"))
all_keys.append(KeyButton(pygame.K_PAGEDOWN, "right", "$J=G91 G21 Z-0.1 F500\n", "$J=G91 G21 Z-200 F1000\n"))
while not gameExit:
last_ms = time.time()
x_value = 0
y_value = 0
mods = pygame.key.get_mods();
if mods & pygame.KMOD_CTRL:
speed = 800
if mods & pygame.KMOD_ALT:
speed = 1500
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
gameExit = True
for key_button in all_keys:
key_button.check_key_down(last_ms, event.key)
if event.type == pygame.KEYUP:
for key_button in all_keys:
key_button.check_key_up(last_ms, event.key)
clock.tick(30)
send = False
has_action = False
for key_button in all_keys:
if not has_action and key_button.active:
has_action = True
if key_button.last_state == 0:
key_button.last_state = 1
print "once " + key_button.name
cmd = key_button.cmd_short
send = True
if key_button.is_pressed_long(last_ms, 0.5):
print "repeat"
if not jog_active:
jog_active = True
print "send LONG command " + key_button.name
cmd = key_button.cmd_long
send = True
if send:
print cmd
if serial_enable:
ser.write(cmd)
if not has_action and jog_active:
jog_active = False
print "cancel jog"
if serial_enable:
ser.write([0x85])
print "done"
pygame.quit()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment