Module gcode::ffi[][src]

A FFI interface to the gcode library.

Error Handling

Most functions will return a boolean success value to indicate whether they were successful or not.

Examples

use gcode::ffi::{self, SIZE_OF_PARSER, SIZE_OF_GCODE};
use gcode::{Parser, Gcode};
use std::mem;

// allocate some space on the stack for our parser. Normally you'd just use
// malloc(), but because we don't have an allocator, we create an
// appropriately sized byte buffer and use pointer casts to "pretend" 
// it's the right thing.
let mut parser = [0_u8; SIZE_OF_PARSER];
let parser = parser.as_mut_ptr() as *mut Parser;

let src = "G01 X-52.4 G4 P50.0";

unsafe {
    let success = ffi::parser_new(parser, src.as_ptr(), src.len() as i32);
    assert!(success, "Creation failed");

    let mut gcode_memory = [0; SIZE_OF_GCODE];
    let mut code = gcode_memory.as_mut_ptr() as *mut Gcode;
    let mut num_gcodes = 0;
    let mut cumulative_x = 0.0;
    let mut cumulative_y = 0.0;

    while ffi::parser_next(parser, code) {
        let mut x = 0.0;
        if ffi::gcode_arg_value(code, 'X', &mut x) {
            cumulative_x += x;
        }

        let mut y = 0.0;
        if ffi::gcode_arg_value(code, 'Y', &mut y) {
            cumulative_y += y;
        }

        num_gcodes += 1;
    }

    assert_eq!(num_gcodes, 2);
    assert_eq!(cumulative_x, -52.4);
    assert_eq!(cumulative_y, 0.0);
}

Constants

SIZE_OF_GCODE
SIZE_OF_PARSER

Functions

gcode_arg_value

Get the value for the argument with a particular letter.

gcode_args

Get a pointer to this Gcode's arguments.

gcode_line_number

Get a Gcode's line number (the N20 argument), if it was assigned.

gcode_major_number
gcode_mnemonic

The overall category this Gcode belongs to.

gcode_num_args

The number of arguments in this Gcode.

gcode_number
gcode_span

The Gcode's location in its source code.

parser_new

Create a new parser.

parser_next

Get the next Gcode, returning false when there are no more Gcodes in the input.