Skip to content

Commit

Permalink
Merging in missed xio_flash_file
Browse files Browse the repository at this point in the history
  • Loading branch information
giseburt committed Jan 24, 2017
1 parent a90fd09 commit 620fd81
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions g2core/xio.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
#define XIO_H_ONCE

//#include "g2core.h" // not required if used in g2core project
#include "config.h" // required for nvObj typedef
#include "config.h" // required for nvObj typedef
#include "canonical_machine.h" // needed for cm_has_hold()

/**** Defines, Macros, and Assorted Parameters ****/

Expand Down Expand Up @@ -97,6 +98,7 @@ enum xioDeviceEnum { // reconfigure this enum as you add
DEV_USB1, // must be 1
DEV_UART1, // must be 2
// DEV_SPI0, // We can't have it here until we actually define it
DEV_FLASH_FILE, // must be 0
DEV_MAX
};

Expand All @@ -106,7 +108,7 @@ enum xioSPIMode {
};


/**** readline stuff -- TODO *****/
/**** readline stuff *****/

#define RX_BUFFER_MIN_SIZE 256 // minimum requested buffer size (they are usually larger)

Expand Down Expand Up @@ -161,6 +163,71 @@ extern "C" {
#define CHAR_QUEUE_FLUSH (char)'%'
//#define CHAR_BOOTLOADER ESC


/**** xio_flash_file - object to hold in-flash (compiled-in) "files" to run ****/

struct xio_flash_file {
const char * const _data;
const int32_t _length;

int32_t _read_offset = 0;

xio_flash_file(const char * const data, int32_t length) : _data{data}, _length{length} {};

void reset() {
_read_offset = 0;
};

const char *readline(bool control_only, uint16_t &line_size) {
line_size = 0;
if (_read_offset == _length) { return nullptr; }

if (control_only) {
char c = _data[_read_offset];
if (!
((c == '!') ||
(c == '~') ||
(c == ENQ) || // request ENQ/ack
(c == CHAR_RESET) || // ^X - reset (aka cancel, terminate)
(c == CHAR_ALARM) || // ^D - request job kill (end of transmission)
(c == '%' && cm_has_hold()) // flush (only in feedhold or part of control header)
))
{
return nullptr;
}
}

const char *line_start = _data + _read_offset;

while (_read_offset < _length) {
char c = _data[_read_offset++];

if (c == '\n') {
break;
}

line_size++;
}

return line_start;
};

bool isDone() {
return _read_offset == _length;
}
};

/**** convenience function to construct a xio_flash_file ****/

template <int32_t length>
constexpr xio_flash_file make_xio_flash_file(const char (&data)[length]) {
return {data, length};
}

/**** function prototype for file-sending ****/

bool xio_send_file(xio_flash_file &file);

#ifdef __TEXT_MODE

void xio_print_spi(nvObj_t *nv);
Expand Down

0 comments on commit 620fd81

Please sign in to comment.