gcode_interpreter/main.c

168 lines
3.0 KiB
C

/*
* main.c
*
* Created on: 10.04.2018
* Author: julian
*/
#include <stdio.h>
#include "parser.h"
char buffer[100];
// Parser error handler
void error(const char * format, ...) {
fputs("ERR: ", stderr);
va_list list;
va_start(list, format);
vfprintf(stderr, format, list);
va_end(list);
notok = 1;
}
/** movement related stuff **/
enum movement_types {
MOVE_ABSOLUTE = 0,
MOVE_RELATIVE,
};
typedef struct coordinate_system {
float position[2];
enum movement_types movement_type;
} coordinate_system_t;
coordinate_system_t coordinate_system;
void move(const char * start, const char * end) {
argument_t arg;
float posa = 0, posb = 0;
static float feed = 100;
int count = 0;
// parse all arguments
while(nextArgument(&arg, &start, end)) {
switch(toUpper(arg.c)) {
case 'A':
posa = arg.num;
break;
case 'B':
posb = arg.num;
break;
case 'F':
if (arg.num <= 0) {
error("F%f: feed must be greater than zero!\n", arg.num);
return;
}
feed = arg.num;
break;
default:
continue;
}
count++;
}
if (!count) {
error("no arguments provided! need at least a new position for either A or B axis\n");
return;
}
printf("move A: %f, B: %f, feed %f\n", posa, posb, feed);
switch (coordinate_system.movement_type) {
case MOVE_RELATIVE:
coordinate_system.position[0] += posa;
coordinate_system.position[1] += posb;
break;
case MOVE_ABSOLUTE:
coordinate_system.position[0] = posa;
coordinate_system.position[1] = posb;
break;
}
}
void home(const char * start, const char * end) {
argument_t arg;
while(nextArgument(&arg, &start, end)) {
switch(toUpper(arg.c)) {
case 'A':
coordinate_system.position[0] = 0;
break;
case 'B':
coordinate_system.position[1] = 0;
break;
}
}
}
void state(const char * start, const char * end) {
printf("current position: A\t%f, B\t%f\n", coordinate_system.position[0], coordinate_system.position[1]);
}
/** Settings **/
void set_move_absolute() {
coordinate_system.movement_type = MOVE_ABSOLUTE;
}
void set_move_relative() {
coordinate_system.movement_type = MOVE_RELATIVE;
}
// reads one line of text in
// handles overflow
void readline(FILE * input) {
char * pos = buffer;
char * end = buffer + sizeof(buffer);
while(!feof(input)) {
char c = fgetc(input);
*pos = c;
pos++;
if (c == '\n') {
parse(buffer, pos);
pos = buffer;
}
if (pos >= end) {
printf("buffer overflow!");
pos = buffer;
}
}
}
#include <string.h>
int testAsFloat() {
float result = 0;
const char * str = "10";
const char * start = str, * end = str + strlen(str);
if (!asFloat(&result, &start, end)) return 0;
if (result < 9.999 || result > 10.001) return 0;
str = "0.001";
start = str, end = str + strlen(str);
if (!asFloat(&result, &start, end)) return 0;
if (result < 0.0005 || result > 0.0015) return 0;
return 1;
}
int main() {
printf("Test Float parsing: ");
if (testAsFloat()) {
printf(" PASS\n");
} else {
printf(" FAIL\n");
}
readline(stdin);
return 0;
}