Skip to content

Commit

Permalink
modify serial to deal with multiple streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Oct 9, 2023
1 parent 44c1fa4 commit 72cc38f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
45 changes: 31 additions & 14 deletions uCNC/src/core/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ static uint8_t parser_grbl_command(void)

do
{
c = serial_getc();
c = serial_peek();
// toupper
if (c >= 'a' && c <= 'z')
{
Expand All @@ -339,8 +339,14 @@ static uint8_t parser_grbl_command(void)

if (!(c >= 'A' && c <= 'Z'))
{
if (c < '0' || c > '9' || grbl_cmd_len) // replaces old ungetc
{
serial_getc();
}
break;
}

serial_getc();
grbl_cmd_str[grbl_cmd_len++] = c;
} while ((grbl_cmd_len < GRBL_CMD_MAX_LEN));

Expand Down Expand Up @@ -444,23 +450,34 @@ static uint8_t parser_grbl_command(void)
return STATUS_INVALID_STATEMENT;
}

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

if (error)
{
// the Gcode is not valid then erase the startup block
mcu_eeprom_putc(block_address, 0);
return error;
}
// everything ok reverts string and saves it
do
{
//serial_ungetc();
} while (serial_peek() != '=');
serial_getc();
settings_save_startup_gcode(block_address);
// 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;
case EOL:
return GRBL_SEND_STARTUP_BLOCKS;
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/boards/avr/avr.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lib_ignore = EEPROM, SPI, Wire
extends = common_avr
board = uno
;saves a bit of flash
build_flags = ${common_avr.build_flags} -D DISABLE_SETTINGS_MODULES -D DISABLE_MULTISTRTEAM_SERIAL
build_flags = ${common_avr.build_flags} -D DISABLE_SETTINGS_MODULES -D DISABLE_MULTISTREAM_SERIAL

[env:uno]
extends = atmega328p
Expand Down
4 changes: 2 additions & 2 deletions uCNC/src/hal/mcus/mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ extern "C"


#ifdef MCU_HAS_USB
uint8_t mcu_usb_getc(bool peek);
uint8_t mcu_usb_getc(void);
uint8_t mcu_usb_available(void);
void mcu_usb_putc(uint8_t c);
void mcu_usb_flush(void);
Expand All @@ -539,7 +539,7 @@ extern "C"
#endif

#ifdef MCU_HAS_UART
uint8_t mcu_uart_getc(bool peek);
uint8_t mcu_uart_getc(void);
uint8_t mcu_uart_available(void);
void mcu_uart_clear(void);
void mcu_uart_putc(uint8_t c);
Expand Down
31 changes: 18 additions & 13 deletions uCNC/src/interface/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,30 @@ void serial_stream_change(serial_stream_t *stream)
}

uint8_t serial_getc(void)
{
uint8_t peek = serial_peek();
serial_peek_buffer = 0;
return peek;
}

static FORCEINLINE uint8_t _serial_peek(void)
{
uint8_t peek = serial_peek_buffer;
if (peek)
{
serial_peek_buffer = 0;
return peek;
}

while (!current_stream->stream_available())
;
peek = current_stream->stream_getc();
serial_peek_buffer = peek;
return peek;
}

uint8_t serial_peek(void)
{
uint8_t peek = _serial_peek();
switch (peek)
{
case '\n':
Expand All @@ -134,14 +147,6 @@ uint8_t serial_getc(void)
return peek;
}

uint8_t serial_peek(void)
{
uint8_t peek = serial_getc();
serial_peek_buffer = peek;

return peek;
}

uint8_t serial_available(void)
{
return current_stream->stream_available();
Expand All @@ -157,12 +162,12 @@ void serial_clear(void)
current_stream->stream_clear();
}

#ifndef DISABLE_MULTISTRTEAM_SERIAL
#ifndef DISABLE_MULTISTREAM_SERIAL
static bool serial_broadcast_enabled;
#endif
void serial_broadcast(bool enable)
{
#ifndef DISABLE_MULTISTRTEAM_SERIAL
#ifndef DISABLE_MULTISTREAM_SERIAL
serial_broadcast_enabled = enable;
#endif
}
Expand All @@ -171,7 +176,7 @@ static uint8_t serial_tx_count;
void serial_putc(uint8_t c)
{
serial_tx_count++;
#ifndef DISABLE_MULTISTRTEAM_SERIAL
#ifndef DISABLE_MULTISTREAM_SERIAL
if (!serial_broadcast_enabled)
{
current_stream->stream_putc(c);
Expand Down Expand Up @@ -201,7 +206,7 @@ void serial_putc(uint8_t c)

void serial_flush(void)
{
#ifndef DISABLE_MULTISTRTEAM_SERIAL
#ifndef DISABLE_MULTISTREAM_SERIAL
if (!serial_broadcast_enabled)
{
current_stream->stream_flush();
Expand Down

0 comments on commit 72cc38f

Please sign in to comment.