yarf 0.1
Yet Another RepRap Firmware
Data Structures | Defines | Functions | Variables
src/input/gcode.c File Reference

The g-code component parses incoming g-code commands and calls the corresponding functions in the commands component. More...

#include "gcode.h"
#include "yarf.h"
#include "commands.h"
#include <math.h>
#include <stdbool.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  gcode_command
 Structure representing a parsed g-code command. More...

Defines

#define N_SEEN   _BV(0)
#define CODE_SEEN   _BV(1)
#define X_SEEN   _BV(2)
#define Y_SEEN   _BV(3)
#define Z_SEEN   _BV(4)
#define E_SEEN   _BV(5)
#define F_SEEN   _BV(6)
#define S_SEEN   _BV(7)
#define CS_SEEN   _BV(8)
#define COORD_SEEN   (X_SEEN | Y_SEEN | Z_SEEN | E_SEEN | F_SEEN | S_SEEN)

Functions

static int get_signed_long (char *s, long *l)
 Reads a signed long integer from a given string and returns the number of characters read.
static int get_signed_int (char *s, int *i)
 Reads a signed integer from a given string and returns the number of characters read.
static int get_uint8_t (char *s, uint8_t *u)
 Reads an unsigned 8-bit integer from a given string and returns the number of characters read or -1 if the integer read was too large to fit in an 8-bit unsigned integer field.
static int get_float (char *s, float *f)
 Reads a float from a given string and returns the number of characters read.
static unsigned char count_whitespace (char *s)
 Counts the number of whitespace characters at the start of a given string.
static void clear_command (gcode_command *cmd)
 Clears a given g-code command data structure.
static uint8_t checksum (char *s, size_t length)
 Calculates the checksum of a g-code command with a given length.
static int parse_command (gcode_command *cmd)
 Reads a g-code command from the host stream, parses it and places the result into a given g-code command data structure.
static void send_resend (unsigned long line_number)
 Sends a g-code resend command to the host for a given line number.
static void send_reply (gcode_command *cmd, cmd_response_t *r)
 Sends a given g-code response to the host, as a reply to a given g-code command.
static void command_unknown (cmd_response_t *r)
 Initializes a g-code response to be an 'unknown command' response.
static void dispatch_command (gcode_command *cmd, cmd_response_t *r)
 Dispatches a given g-code command to the command component for execution and stores the response in a variable pointed to by a given pointer.
void gcode_init (FILE *host_stream)
 Initializes the g-code component.
int gcode_process_command ()
 Reads and processes an incoming g-code command from the host stream.

Variables

static FILE * host
 Stream from which to read the g-codes and on which to output responses.
static long expected_line_number
 The expected line number of the next g-code command to be received.

Detailed Description

The g-code component parses incoming g-code commands and calls the corresponding functions in the commands component.

Author:
Pieter Agten (pieter.agten@gmail.com)
Date:
20 sep 2011

Definition in file gcode.c.


Function Documentation

static uint8_t checksum ( char *  s,
size_t  length 
) [static]

Calculates the checksum of a g-code command with a given length.

Parameters:
sthe entire g-code command
lengththe length of the g-code command
Returns:
The checksum of the given g-code command.

Definition at line 231 of file gcode.c.

static void clear_command ( gcode_command cmd) [static]

Clears a given g-code command data structure.

Parameters:
cmdpointer to the g-code command data structure to clear.

Definition at line 210 of file gcode.c.

static void command_unknown ( cmd_response_t r) [static]

Initializes a g-code response to be an 'unknown command' response.

Parameters:
rthe g-code response to initialize

Definition at line 478 of file gcode.c.

static unsigned char count_whitespace ( char *  s) [static]

Counts the number of whitespace characters at the start of a given string.

Parameters:
sthe string from which to count the number of leading whitespace characters
Returns:
The number of whitespace characters at the start of the given string.

Definition at line 189 of file gcode.c.

static void dispatch_command ( gcode_command cmd,
cmd_response_t r 
) [inline, static]

Dispatches a given g-code command to the command component for execution and stores the response in a variable pointed to by a given pointer.

Parameters:
cmdthe g-code command to dispatch to the command component
ra pointer to the variable to write the response to

Definition at line 492 of file gcode.c.

void gcode_init ( FILE *  host_stream)

Initializes the g-code component.

Parameters:
host_streamthe stream from which to read the g-codes and on which to output responses

Definition at line 583 of file gcode.c.

int gcode_process_command ( void  )

Reads and processes an incoming g-code command from the host stream.

Returns:
GCODE_PROCESS_COMMAND_SUCCESS if the command was handled successfully, or one of the error codes if not.

Definition at line 590 of file gcode.c.

static int get_float ( char *  s,
float *  f 
) [static]

Reads a float from a given string and returns the number of characters read.

Parameters:
sthe string from which to read the signed integer
fpointer to the variable in which to write the number read
Returns:
The number of characters read from the string, including the leading whitespace (if any) and excluding any other characters.

This function skips any leading whitespace, and includes that whitespace in the number of characters read.

Definition at line 170 of file gcode.c.

static int get_signed_int ( char *  s,
int *  i 
) [static]

Reads a signed integer from a given string and returns the number of characters read.

Parameters:
sthe string from which to read the signed integer
ipointer to the variable in which to write the number read
Returns:
The number of characters read from the string, including the leading whitespace (if any) and excluding any other characters.

This function skips any leading whitespace, and includes that whitespace in the number of characters read.

Definition at line 113 of file gcode.c.

static int get_signed_long ( char *  s,
long *  l 
) [static]

Reads a signed long integer from a given string and returns the number of characters read.

Parameters:
sthe string from which to read the signed long integer
lpointer to the variable in which to write the number read
Returns:
The number of characters read from the string, including the leading whitespace (if any) and excluding any other characters.

This function skips any leading whitespace, and includes that whitespace in the number of characters read.

Definition at line 89 of file gcode.c.

static int get_uint8_t ( char *  s,
uint8_t *  u 
) [static]

Reads an unsigned 8-bit integer from a given string and returns the number of characters read or -1 if the integer read was too large to fit in an 8-bit unsigned integer field.

Parameters:
sthe string from which to read the unsigned 8-bit integer
upointer to the variable in which to write the number read
Returns:
The number of characters read from the string, including the leading whitespace (if any) and excluding any other characters, or -1 if the integer read from the string was too large to fit in an 8-bit unsigned integer field.

This function skips any leading whitespace, and includes that whitespace in the number of characters read.

Definition at line 140 of file gcode.c.

static int parse_command ( gcode_command cmd) [static]

Reads a g-code command from the host stream, parses it and places the result into a given g-code command data structure.

Parameters:
cmdpointer to the variable in which to return the parsed g-code command
Returns:
GCODE_PARSE_COMMAND_SUCCESS on success or one of the error codes otherwise.

Definition at line 252 of file gcode.c.

static void send_reply ( gcode_command cmd,
cmd_response_t r 
) [inline, static]

Sends a given g-code response to the host, as a reply to a given g-code command.

Parameters:
cmdthe g-code command for which to send a response
rthe g-code response to send to the host

Definition at line 421 of file gcode.c.

static void send_resend ( unsigned long  line_number) [inline, static]

Sends a g-code resend command to the host for a given line number.

Parameters:
line_numberthe line number for which to send a resend request

Definition at line 407 of file gcode.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines