cngt: tool change utility for MDX15/20 (in progress)

- Makefile: basic Makefile
- cngt.c: argument processing and main loop
- serial.h, serial.c: serial port output for MDX-15/20, from cncmap/millp/
- getkey.h, getkey.c: single-key (raw) TTY input
This commit is contained in:
Werner Almesberger 2010-12-15 20:47:58 -03:00
parent 99592248d9
commit 154be2bada
6 changed files with 430 additions and 0 deletions

77
cngt/Makefile Normal file
View File

@ -0,0 +1,77 @@
#
# Makefile - Makefile of drl2gp
#
# Written 2010 by Werner Almesberger
# Copyright 2010 by Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
PREFIX ?= /usr/local
SHELL=/bin/bash
MAIN=cngt
OBJS=cngt.o getkey.o serial.o
CFLAGS_WARN=-Wall -Wshadow -Wmissing-prototypes \
-Wmissing-declarations -Wno-format-zero-length
CFLAGS=$(CFLAGS_WARN) -g
LDFLAGS=-lm
# ----- Verbosity control -----------------------------------------------------
CC_normal := $(CC)
DEPEND_normal := $(CPP) $(CFLAGS) -MM -MG
CC_quiet = @echo " CC " $@ && $(CC_normal)
DEPEND_quiet = @$(DEPEND_normal)
ifeq ($(V),1)
CC = $(CC_normal)
DEPEND = $(DEPEND_normal)
else
CC = $(CC_quiet)
DEPEND = $(DEPEND_quiet)
endif
# ----- Rules -----------------------------------------------------------------
.PHONY: all clean spotless
all: $(MAIN)
$(MAIN): $(OBJS)
clean:
rm -f $(OBJS) $(OBJS:.o=.d)
spotless: clean
rm -f $(MAIN)
# ----- Install / uninstall ---------------------------------------------------
install: all
mkdir -p $(DESTDIR)/$(PREFIX)/bin/
install -m 755 $(MAIN) $(DESTDIR)/$(PREFIX)/bin/
uninstall:
rm -f $(DESTDIR)/$(PREFIX)/bin/$(MAIN)
# ----- Dependencies ----------------------------------------------------------
# compile and generate dependencies, from fped, based on
# http://scottmcpeak.com/autodepend/autodepend.html
%.o: %.c
$(CC) -c $(CFLAGS) $*.c -o $*.o
$(DEPEND) $*.c | \
sed -e \
'/^\(.*:\)\? */{p;s///;s/ *\\\?$$/ /;s/ */:\n/g;H;}' \
-e '$${g;p;}' -e d >$*.d; \
[ "$${PIPESTATUS[*]}" = "0 0" ] || { rm -f $*.d; exit 1; }
-include $(OBJS:.o=.d)

134
cngt/cngt.c Normal file
View File

@ -0,0 +1,134 @@
/*
* cngt.c - Tool change utility for MDX-15/20
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include "serial.h"
#include "getkey.h"
#define MAX_POS 10
#define STEP 5 /* xy movement step, 5 mm */
static double pos_x[MAX_POS];
static double pos_y[MAX_POS];
static double z0, height;
static double x, y, z;
#define UNITS(mm) ((mm)*40.0)
static void move(void)
{
serial_printf("!PZ%.1f,0;PD%.1f,%.1f\n", UNITS(z), UNITS(x), UNITS(y));
}
static void up(void)
{
z = z0+height;
move();
}
static void down(void)
{
z = z0;
move();
}
static int do_key(char c)
{
switch (c) {
case 'u':
up();
break;
case 'd':
down();
break;
case 'h':
up();
x -= STEP;
move();
break;
case 'j':
up();
y -= STEP;
move();
break;
case 'k':
up();
y += STEP;
move();
break;
case 'l':
up();
x -= STEP;
move();
break;
case 'q':
return 1;
default:
if (c < '0' || c > '9')
break;
up();
x = pos_x[c-'0'];
y = pos_y[c-'0'];
move();
}
return 0;
}
static void usage(const char *name)
{
fprintf(stderr, "usage: %s z0 height (file | x y ...)\n", name);
exit(1);
}
int main(int argc, char **argv)
{
int i;
char c;
if (argc < 4)
usage(*argv);
if (!(argc & 1))
usage(*argv);
z0 = atof(argv[1]);
height = atof(argv[2]);
for (i = 3; i != argc; i += 2) {
pos_x[(i-3)/2] = atof(argv[i]);
pos_y[(i-3)/2] = atof(argv[i+1]);
}
serial_open("/dev/ttyUSB0");
serial_printf("\nIN;!MC0\n");
x = pos_x[0];
y = pos_y[0];
z = z0+height;
while ((c = getkey()))
if (do_key(c))
break;
up();
serial_close();
return 0;
}

78
cngt/getkey.c Normal file
View File

@ -0,0 +1,78 @@
/*
* getkey.c - Single-key TTY input
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include "getkey.h"
#define TTY_NAME "/dev/tty"
static int tty = -1;
static struct termios old;
static void reset_tty(void)
{
if (tty >= 0)
if (tcsetattr(tty, TCSADRAIN, &old) < 0)
perror("tcsetattr");
}
static void tty_open(void)
{
struct termios new;
tty = open(TTY_NAME, O_WRONLY | O_NOCTTY);
if (tty < 0) {
perror(TTY_NAME);
exit(1);
}
if (tcgetattr(tty, &old) < 0) {
perror("tcgetattr");
exit(1);
}
atexit(reset_tty);
new = old;
cfmakeraw(&new);
if (tcsetattr(tty, TCSANOW, &new) < 0) {
perror("tcsetattr");
exit(1);
}
}
char getkey(void)
{
char buf;
ssize_t got;
if (tty == -1)
tty_open();
got = read(tty, &buf, 1);
if (got < 0) {
perror("read");
reset_tty();
exit(1);
}
return got ? buf : 0;
}

20
cngt/getkey.h Normal file
View File

@ -0,0 +1,20 @@
/*
* getkey.h - Raw TTY input
*
* Written 2010 by Werner Almesberger
* Copyright 2010 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef GETKEY_H
#define GETKEY_H
char getkey(void);
#endif /* !GETKEY_H */

100
cngt/serial.c Normal file
View File

@ -0,0 +1,100 @@
/*
* serial.c - Send data to a Modela MDX-15/20
*
* Written 2009 by Werner Almesberger
* Copyright 2009 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include "serial.h"
static int tty;
static struct termios old;
static void reset_tty(void)
{
if (tty >= 0)
if (tcsetattr(tty, TCSADRAIN, &old) < 0)
perror("tcsetattr");
}
void serial_open(const char *name)
{
struct termios new;
tty = open(name, O_WRONLY | O_NOCTTY);
if (tty < 0) {
perror(name);
exit(1);
}
if (tcgetattr(tty, &old) < 0) {
perror("tcgetattr");
exit(1);
}
atexit(reset_tty);
new = old;
cfmakeraw(&new);
cfsetospeed(&new, B9600);
new.c_iflag |= IXON;
new.c_cflag |= CLOCAL | CRTSCTS;
new.c_lflag &= ~ECHO;
if (tcsetattr(tty, TCSAFLUSH, &new) < 0) {
perror("tcsetattr");
exit(1);
}
}
void serial_close(void)
{
reset_tty();
(void) close(tty);
tty = -1;
}
void serial_printf(const char *fmt, ...)
{
char buf[10000]; /* more than enough :) */
va_list ap;
int len;
ssize_t wrote;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len >= sizeof(buf)-1) {
fprintf(stderr, "buffer too small\n");
exit(1);
}
wrote = write(tty, buf, strlen(buf));
if (wrote < 0) {
perror("write");
exit(1);
}
if (wrote != len) {
fprintf(stderr, "short write: %d < %d\n",
(int) wrote, (int) len);
exit(1);
}
}

21
cngt/serial.h Normal file
View File

@ -0,0 +1,21 @@
/*
* serial.h - Send data to a Modela MDX-15/20
*
* Written 2009 by Werner Almesberger
* Copyright 2009 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef SERIAL_H
#define SERIAL_H
void serial_open(const char *name);
void serial_close(void);
void serial_printf(const char *fmt, ...);
#endif /* !SERIAL_H */