Skip to content

Commit 620fd81

Browse files
committed
Merging in missed xio_flash_file
1 parent a90fd09 commit 620fd81

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

g2core/xio.h

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
#define XIO_H_ONCE
5050

5151
//#include "g2core.h" // not required if used in g2core project
52-
#include "config.h" // required for nvObj typedef
52+
#include "config.h" // required for nvObj typedef
53+
#include "canonical_machine.h" // needed for cm_has_hold()
5354

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

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

@@ -106,7 +108,7 @@ enum xioSPIMode {
106108
};
107109

108110

109-
/**** readline stuff -- TODO *****/
111+
/**** readline stuff *****/
110112

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

@@ -161,6 +163,71 @@ extern "C" {
161163
#define CHAR_QUEUE_FLUSH (char)'%'
162164
//#define CHAR_BOOTLOADER ESC
163165

166+
167+
/**** xio_flash_file - object to hold in-flash (compiled-in) "files" to run ****/
168+
169+
struct xio_flash_file {
170+
const char * const _data;
171+
const int32_t _length;
172+
173+
int32_t _read_offset = 0;
174+
175+
xio_flash_file(const char * const data, int32_t length) : _data{data}, _length{length} {};
176+
177+
void reset() {
178+
_read_offset = 0;
179+
};
180+
181+
const char *readline(bool control_only, uint16_t &line_size) {
182+
line_size = 0;
183+
if (_read_offset == _length) { return nullptr; }
184+
185+
if (control_only) {
186+
char c = _data[_read_offset];
187+
if (!
188+
((c == '!') ||
189+
(c == '~') ||
190+
(c == ENQ) || // request ENQ/ack
191+
(c == CHAR_RESET) || // ^X - reset (aka cancel, terminate)
192+
(c == CHAR_ALARM) || // ^D - request job kill (end of transmission)
193+
(c == '%' && cm_has_hold()) // flush (only in feedhold or part of control header)
194+
))
195+
{
196+
return nullptr;
197+
}
198+
}
199+
200+
const char *line_start = _data + _read_offset;
201+
202+
while (_read_offset < _length) {
203+
char c = _data[_read_offset++];
204+
205+
if (c == '\n') {
206+
break;
207+
}
208+
209+
line_size++;
210+
}
211+
212+
return line_start;
213+
};
214+
215+
bool isDone() {
216+
return _read_offset == _length;
217+
}
218+
};
219+
220+
/**** convenience function to construct a xio_flash_file ****/
221+
222+
template <int32_t length>
223+
constexpr xio_flash_file make_xio_flash_file(const char (&data)[length]) {
224+
return {data, length};
225+
}
226+
227+
/**** function prototype for file-sending ****/
228+
229+
bool xio_send_file(xio_flash_file &file);
230+
164231
#ifdef __TEXT_MODE
165232

166233
void xio_print_spi(nvObj_t *nv);

0 commit comments

Comments
 (0)