Skip to content

Commit

Permalink
improved code for simple stream (UNO)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Oct 10, 2023
1 parent 3494d04 commit 6fb119b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 68 deletions.
12 changes: 7 additions & 5 deletions uCNC/src/cnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,11 @@ static void cnc_io_dotasks(void)

void cnc_run_startup_blocks(void)
{
// serial_select(SERIAL_N0);
// cnc_exec_cmd();
// serial_select(SERIAL_N1);
// cnc_exec_cmd();
// serial_select(SERIAL_UART);
serial_broadcast(true);
serial_stream_eeprom(STARTUP_BLOCK0_ADDRESS_OFFSET);
cnc_exec_cmd();
serial_stream_eeprom(STARTUP_BLOCK1_ADDRESS_OFFSET);
cnc_exec_cmd();
serial_broadcast(false);
serial_stream_change(NULL);
}
39 changes: 15 additions & 24 deletions uCNC/src/core/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static uint8_t parser_grbl_command(void)
}
break;
}

serial_getc();
grbl_cmd_str[grbl_cmd_len++] = c;
} while ((grbl_cmd_len < GRBL_CMD_MAX_LEN));
Expand Down Expand Up @@ -383,7 +383,7 @@ static uint8_t parser_grbl_command(void)
{
float val = 0;
setting_offset_t setting_num = 0;
//serial_ungetc();
// serial_ungetc();
error = parser_get_float(&val);
if (!error)
{
Expand Down Expand Up @@ -452,33 +452,24 @@ static uint8_t parser_grbl_command(void)

settings_save_startup_gcode(block_address);
// run startup block
serial_broadcast(true);
serial_stream_eeprom(block_address);
error = parser_fetch_command(&next_state, &words, &cmd);
if (error == STATUS_OK)
{
error = parser_validate_command(&next_state, &words, &cmd);
}

if (error)
serial_broadcast(false);
serial_stream_change(NULL);

if (error != STATUS_OK)
{
// the Gcode is not valid then erase the startup block
mcu_eeprom_putc(block_address, 0);
return error;
}
// error = parser_fetch_command(&next_state, &words, &cmd);
// if (error)
// {
// // the Gcode is not valid then erase the startup block
// mcu_eeprom_putc(block_address, 0);
// return error;
// }
// error = parser_validate_command(&next_state, &words, &cmd);
// if (error)
// {
// return error;
// }
// // everything ok reverts string and saves it
// do
// {
// //serial_ungetc();
// } while (serial_peek() != '=');
// serial_getc();

return STATUS_OK;

return error;
case EOL:
return GRBL_SEND_STARTUP_BLOCKS;
}
Expand Down
16 changes: 16 additions & 0 deletions uCNC/src/hal/mcus/mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ extern "C"
#endif
#endif

#ifndef mcu_getc
#define mcu_getc (&mcu_uart_getc)
#endif
#ifndef mcu_available
#define mcu_available (&mcu_uart_available)
#endif
#ifndef mcu_clear
#define mcu_clear (&mcu_uart_clear)
#endif
#ifndef mcu_putc
#define mcu_putc (&mcu_uart_putc)
#endif
#ifndef mcu_flush
#define mcu_flush (&mcu_uart_flush)
#endif

#ifdef __cplusplus
}
#endif
Expand Down
52 changes: 26 additions & 26 deletions uCNC/src/hal/mcus/virtual/mcu_virtual.c
Original file line number Diff line number Diff line change
Expand Up @@ -1515,32 +1515,32 @@ void mcu_uart_flush(void)
// mcu_tx_enabled = true;
// }

uint8_t mcu_getc(void)
{
uint8_t c = 0;
if (g_mcu_buffertail != g_mcu_bufferhead)
{
c = g_mcu_combuffer[g_mcu_buffertail];
if (++g_mcu_buffertail == COM_BUFFER_SIZE)
{
g_mcu_buffertail = 0;
}

if (c == '\n')
{
g_mcu_buffercount--;
}
}

return c;
}

uint8_t mcu_peek(void)
{
if (g_mcu_buffercount == 0)
return 0;
return g_mcu_combuffer[g_mcu_buffertail];
}
//uint8_t mcu_getc(void)
//{
// uint8_t c = 0;
// if (g_mcu_buffertail != g_mcu_bufferhead)
// {
// c = g_mcu_combuffer[g_mcu_buffertail];
// if (++g_mcu_buffertail == COM_BUFFER_SIZE)
// {
// g_mcu_buffertail = 0;
// }
//
// if (c == '\n')
// {
// g_mcu_buffercount--;
// }
// }
//
// return c;
//}
//
//uint8_t mcu_peek(void)
//{
// if (g_mcu_buffercount == 0)
// return 0;
// return g_mcu_combuffer[g_mcu_buffertail];
//}

void mcu_bufferClear(void)
{
Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/interface/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void protocol_send_alarm(int8_t alarm)
protocol_send_newline();
}

void protocol_send_string(const uint8_t *__s)
void protocol_send_string(const char *__s)
{
uint8_t c = (uint8_t)rom_strptr(__s++);
do
Expand All @@ -80,7 +80,7 @@ void protocol_send_string(const uint8_t *__s)
} while (c != 0);
}

void protocol_send_feedback(const uint8_t *__s)
void protocol_send_feedback(const char *__s)
{
protocol_send_string(MSG_START);
protocol_send_string(__s);
Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/interface/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C"
void protocol_send_alarm(int8_t alarm);
void protocol_send_status(void);
DECL_EVENT_HANDLER(protocol_send_status);
void protocol_send_string(const uint8_t *__s);
void protocol_send_feedback(const uint8_t *__s);
void protocol_send_string(const char *__s);
void protocol_send_feedback(const char *__s);
void protocol_send_probe_result(uint8_t val);
void protocol_send_gcode_coordsys(void);
void protocol_send_gcode_modes(void);
Expand Down
70 changes: 62 additions & 8 deletions uCNC/src/interface/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#include <math.h>
#include "../cnc.h"

static uint8_t (*stream_getc)(void);
uint8_t (*stream_available)(void);
void (*stream_clear)(void);

#ifndef DISABLE_MULTISTREAM_SERIAL
static serial_stream_t *serial_stream;
static serial_stream_t *current_stream;

Expand All @@ -48,13 +53,21 @@ DECL_SERIAL_STREAM(wifi_serial_stream, mcu_wifi_getc, mcu_wifi_available, mcu_wi
#if defined(MCU_HAS_BLUETOOTH) && !defined(DETACH_BLUETOOTH_FROM_MAIN_PROTOCOL)
DECL_SERIAL_STREAM(bt_serial_stream, mcu_bt_getc, mcu_bt_available, mcu_bt_clear, mcu_bt_putc, mcu_bt_flush);
#endif
#else
// if not multistreaming
void (*stream_putc)(uint8_t);
void (*stream_flush)(void);

#endif

static uint8_t serial_peek_buffer;

void serial_init(void)
{
#ifdef FORCE_GLOBALS_TO_0
serial_stream = NULL;
#endif

#ifndef DISABLE_MULTISTREAM_SERIAL
#if defined(MCU_HAS_UART) && !defined(DETACH_UART_FROM_MAIN_PROTOCOL)
serial_stream_register(&uart_serial_stream);
#endif
Expand All @@ -70,9 +83,13 @@ void serial_init(void)
#if defined(MCU_HAS_BLUETOOTH) && !defined(DETACH_BLUETOOTH_FROM_MAIN_PROTOCOL)
serial_stream_register(&bt_serial_stream);
#endif
current_stream = serial_stream;
current_stream = serial_stream;
#else
serial_stream_change(NULL);
#endif
}

#ifndef DISABLE_MULTISTREAM_SERIAL
void serial_stream_register(serial_stream_t *stream)
{
if (serial_stream == NULL)
Expand All @@ -92,9 +109,19 @@ void serial_stream_register(serial_stream_t *stream)
}
}

static uint8_t serial_peek_buffer;
// on cleanup sets the correct stdin streams
void stream_stdin(uint8_t *p)
{
stream_getc = current_stream->stream_getc;
stream_available = current_stream->stream_available;
stream_clear = current_stream->stream_clear;
}
#endif

void serial_stream_change(serial_stream_t *stream)
{
#ifndef DISABLE_MULTISTREAM_SERIAL
uint8_t cleanup __attribute__((__cleanup__(stream_stdin))) = 0;
serial_peek_buffer = 0;
if (stream != NULL)
{
Expand All @@ -108,6 +135,24 @@ void serial_stream_change(serial_stream_t *stream)
{
current_stream = serial_stream;
}
#else
stream_getc = mcu_getc;
stream_available = mcu_available;
stream_clear = mcu_clear;
#endif
}

static uint16_t stream_eeprom_address;
static uint8_t stream_eeprom_getc(void)
{
return mcu_eeprom_getc(stream_eeprom_address++);
}

void serial_stream_eeprom(uint16_t address)
{
stream_eeprom_address = address;
stream_getc = &stream_eeprom_getc;
stream_available = NULL;
}

uint8_t serial_getc(void)
Expand All @@ -125,9 +170,9 @@ static FORCEINLINE uint8_t _serial_peek(void)
return peek;
}

while (!current_stream->stream_available())
while (!serial_available())
;
peek = current_stream->stream_getc();
peek = stream_getc();
serial_peek_buffer = peek;
return peek;
}
Expand All @@ -149,7 +194,12 @@ uint8_t serial_peek(void)

uint8_t serial_available(void)
{
return current_stream->stream_available();
if (!stream_available)
{
// if undef allow to continue
return 1;
}
return stream_available();
}

uint8_t serial_freebytes(void)
Expand All @@ -159,7 +209,11 @@ uint8_t serial_freebytes(void)

void serial_clear(void)
{
#ifndef DISABLE_MULTISTREAM_SERIAL
current_stream->stream_clear();
#else
mcu_clear();
#endif
}

#ifndef DISABLE_MULTISTREAM_SERIAL
Expand Down Expand Up @@ -191,7 +245,7 @@ void serial_putc(uint8_t c)
}
}
#else
serial_stream->stream_putc(c);
mcu_putc(c);
#endif

if (c == '\n')
Expand Down Expand Up @@ -221,7 +275,7 @@ void serial_flush(void)
}
}
#else
serial_stream->stream_flush();
mcu_flush();
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion uCNC/src/interface/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extern "C"

void serial_stream_register(serial_stream_t *stream);
void serial_stream_change(serial_stream_t *stream);
void serial_stream_eeprom(uint16_t address);

void serial_broadcast(bool enable);
void serial_putc(uint8_t c);
Expand All @@ -66,8 +67,8 @@ extern "C"
uint8_t serial_getc(void);
uint8_t serial_peek(void);
uint8_t serial_available(void);
uint8_t serial_freebytes(void);
void serial_clear(void);
uint8_t serial_freebytes(void);

// printing utils
typedef void (*print_cb)(uint8_t);
Expand Down

0 comments on commit 6fb119b

Please sign in to comment.