Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cnc state generic function #591

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions uCNC/src/cnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,3 +1067,59 @@ void cnc_run_startup_blocks(void)
// reset streams
serial_stream_change(NULL);
}

uint8_t cnc_get_state(void)
{
// state check must obay this order
if (cnc_has_alarm())
{
return GRBL_STATE_ALARM;
}
else if (mc_get_checkmode())
{
return GRBL_STATE_CHECKMODE;
}
else
{
uint8_t state = cnc_get_exec_state(EXEC_ALLACTIVE);

if (state & (EXEC_UNHOMED | EXEC_LIMITS))
{
return (!cnc_get_exec_state(EXEC_HOMING)) ? GRBL_STATE_ALARM : GRBL_STATE_HOMING;
}

#if ASSERT_PIN(SAFETY_DOOR)
if (state & EXEC_DOOR)
{
if (CHECKFLAG(io_get_controls(), SAFETY_DOOR_MASK))
{
return (CHECKFLAG(state, EXEC_RUN)) ? GRBL_STATE_DOOR_2 : GRBL_STATE_DOOR_1;
}

return (CHECKFLAG(state, EXEC_RUN)) ? GRBL_STATE_DOOR_3 : GRBL_STATE_DOOR_0;
}
#endif

if (state & EXEC_HOMING)
{
return GRBL_STATE_HOMING;
}

if (state & EXEC_HOLD)
{
return (CHECKFLAG(state, EXEC_RUN)) ? GRBL_STATE_HOLD_1 : GRBL_STATE_HOLD_0;
}

if (state & EXEC_JOG)
{
return GRBL_STATE_JOG;
}

if (state & EXEC_RUN)
{
return GRBL_STATE_RUN;
}
}

return GRBL_STATE_IDLE;
}
18 changes: 18 additions & 0 deletions uCNC/src/cnc.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ extern "C"
#define EXEC_GCODE_LOCKED (EXEC_ALARM | EXEC_DOOR | EXEC_JOG) // Gcode is locked by an alarm or any special motion state
#define EXEC_ALLACTIVE 255 // All states

// this is a new list of states that can be used more easilly to get the current "Grbl" state
#define GRBL_STATE_CODE(C, M) ((C << 3) | M)
#define GRBL_STATE_CODE_BASE(C) (C & ~0x07)
#define GRBL_STATE_CODE_MANTISSA(C) (C & 0x07)
#define GRBL_STATE_ALARM GRBL_STATE_CODE(7, 0)
#define GRBL_STATE_CHECKMODE GRBL_STATE_CODE(6, 0)
#define GRBL_STATE_DOOR_0 GRBL_STATE_CODE(5, 0)
#define GRBL_STATE_DOOR_1 GRBL_STATE_CODE(5, 1)
#define GRBL_STATE_DOOR_2 GRBL_STATE_CODE(5, 2)
#define GRBL_STATE_DOOR_3 GRBL_STATE_CODE(5, 3)
#define GRBL_STATE_HOMING GRBL_STATE_CODE(4, 0)
#define GRBL_STATE_HOLD_0 GRBL_STATE_CODE(3, 0)
#define GRBL_STATE_HOLD_1 GRBL_STATE_CODE(3, 1)
#define GRBL_STATE_JOG GRBL_STATE_CODE(2, 0)
#define GRBL_STATE_RUN GRBL_STATE_CODE(1, 0)
#define GRBL_STATE_IDLE GRBL_STATE_CODE(0, 0)

// creates a set of helper masks used to configure the controller
#define ESTOP_MASK 1
#define SAFETY_DOOR_MASK 2
Expand Down Expand Up @@ -162,6 +179,7 @@ extern "C"
void cnc_home(void);
void cnc_alarm(int8_t code);
bool cnc_has_alarm(void);
uint8_t cnc_get_state(void);
uint8_t cnc_get_alarm(void);
void cnc_stop(void);
uint8_t cnc_unlock(bool force);
Expand Down
110 changes: 29 additions & 81 deletions uCNC/src/interface/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,92 +366,40 @@ void protocol_send_status(void)
uint8_t controls = io_get_controls();
uint8_t limits = io_get_limits();
bool probe = io_get_probe();
uint8_t state = cnc_get_exec_state(0xFF);
uint8_t filter = 0x80;
while (!(state & filter) && filter)
{
filter >>= 1;
}

state &= filter;
uint8_t state = cnc_get_state();

serial_putc('<');
if (cnc_has_alarm())

switch (GRBL_STATE_CODE_BASE(state))
{
case GRBL_STATE_ALARM:
protocol_send_string(MSG_STATUS_ALARM);
}
else if (mc_get_checkmode())
{
break;
case GRBL_STATE_CHECKMODE:
protocol_send_string(MSG_STATUS_CHECK);
}
else
{
switch (state)
{
#if ASSERT_PIN(SAFETY_DOOR)
case EXEC_DOOR:
protocol_send_string(MSG_STATUS_DOOR);
serial_putc(':');
if (CHECKFLAG(controls, SAFETY_DOOR_MASK))
{
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('2');
}
else
{
serial_putc('1');
}
}
else
{
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('3');
}
else
{
serial_putc('0');
}
}
break;
#endif
case EXEC_UNHOMED:
case EXEC_LIMITS:
if (!cnc_get_exec_state(EXEC_HOMING))
{
protocol_send_string(MSG_STATUS_ALARM);
}
else
{
protocol_send_string(MSG_STATUS_HOME);
}
break;
case EXEC_HOLD:
protocol_send_string(MSG_STATUS_HOLD);
serial_putc(':');
if (cnc_get_exec_state(EXEC_RUN))
{
serial_putc('1');
}
else
{
serial_putc('0');
}
break;
case EXEC_HOMING:
protocol_send_string(MSG_STATUS_HOME);
break;
case EXEC_JOG:
protocol_send_string(MSG_STATUS_JOG);
break;
case EXEC_RUN:
protocol_send_string(MSG_STATUS_RUN);
break;
default:
protocol_send_string(MSG_STATUS_IDLE);
break;
}
break;
case GRBL_STATE_DOOR_0:
protocol_send_string(MSG_STATUS_DOOR);
serial_putc(':');
serial_putc('0' + GRBL_STATE_CODE_MANTISSA(state));
break;
case GRBL_STATE_HOMING:
protocol_send_string(MSG_STATUS_HOME);
break;
case GRBL_STATE_HOLD_0:
protocol_send_string(MSG_STATUS_HOLD);
serial_putc(':');
serial_putc('0' + GRBL_STATE_CODE_MANTISSA(state));
break;
case GRBL_STATE_JOG:
protocol_send_string(MSG_STATUS_JOG);
break;
case GRBL_STATE_RUN:
protocol_send_string(MSG_STATUS_RUN);
break;
default:
protocol_send_string(MSG_STATUS_IDLE);
break;
}

if ((g_settings.status_report_mask & 1))
Expand Down
Loading