Skip to content

Commit

Permalink
cleanup in main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
digant73 committed Sep 4, 2023
1 parent 2d2813f commit 63403cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 84 deletions.
7 changes: 5 additions & 2 deletions TFT/src/User/API/Gcode/gcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ void detectAdvancedOk(void)
{
if (requestCommandInfo.cmd_rev_buf[cmd_index++] == 'B')
{
if (strtol(&requestCommandInfo.cmd_rev_buf[cmd_index], NULL, 10) != 0)
infoSettings.tx_slots = strtol(&requestCommandInfo.cmd_rev_buf[cmd_index], NULL, 10);
if (strtol(&requestCommandInfo.cmd_rev_buf[cmd_index], NULL, 10) != 0) // if different than 0
{
// set infoHost.target_tx_slots and infoSettings.tx_slots to the value detected by TFT
infoHost.target_tx_slots = infoSettings.tx_slots = strtol(&requestCommandInfo.cmd_rev_buf[cmd_index], NULL, 10);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion TFT/src/User/API/parseACK.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ void parseACK(void)
// if regular OK response ("ok\n")
if (ack_cache[ack_index] == '\n')
{
InfoHost_HandleOkAck(HOST_SLOTS_REGULAR_OK);
InfoHost_HandleOkAck(infoSettings.tx_slots);

goto parse_end; // nothing else to do
}
Expand Down
94 changes: 22 additions & 72 deletions TFT/src/User/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,21 @@ CLOCKS mcuClocks; // System clocks: SYSCLK, AHB, APB1, APB2, APB1_Timer, APB2_T

void InfoHost_Init(bool isConnected)
{
infoHost.target_tx_slots = infoSettings.tx_slots;
infoHost.tx_slots = 1; // set to 1 just to allow a soft start
infoHost.tx_count = 0;
infoHost.connected = isConnected;
infoHost.status = HOST_STATUS_IDLE;
}

void InfoHost_HandleOkAck(int16_t tx_slots)
void InfoHost_HandleOkAck(int16_t target_tx_slots)
{
// the following check should always be matched unless:
// - an ACK message not related to a gcode originated by the TFT is received
// - an ACK message for an out of band gcode (e.g. emergency gcode) is received
// - a bug is present in the TFT code or in the mainboard reply
//
if (infoHost.tx_count > 0)
{
infoHost.tx_count--;
}
else
{
// if the following check is matched, a bug is present.
// Reset infoSettings.tx_slots and infoHost.tx_count to try to avoid a possible TFT freeze
//
if (infoHost.tx_slots == 0)
{
handle_error:

#ifdef DEBUG_MONITORING
BUZZER_PLAY(SOUND_ERROR);

char str[100];

sprintf(str, "tx_slots=%d tx_count=%d", infoHost.tx_slots, infoHost.tx_count);
addNotification(DIALOG_TYPE_ERROR, "OK mismatch", str, false);
#endif

infoHost.tx_slots = 1; // set to 1 just to allow a soft start
infoHost.tx_count = 0;

return; // nothing else to do
}

// in case of generic OK response handling such as temperature response
// (e.g. "ok T:16.13 /0.00 B:16.64 /0.00 @:0 B@:0\n"), nothing else to do
//
if (tx_slots == HOST_SLOTS_GENERIC_OK)
return; // nothing else to do

// if here, proceed with the selected OK response handling to update infoHost.tx_slots and infoHost.tx_count
}

// NOTE: the following code always allows to align infoHost.tx_slots even in case of switching ON/OFF
// the ADVANCED_OK feature in TFT and/or in case infoHost.tx_slots is beeing also managed by
Expand All @@ -68,52 +34,36 @@ void InfoHost_HandleOkAck(int16_t tx_slots)
{
infoHost.tx_slots = 1;
}
// if ADVANCED_OK is enabled in TFT but not in Marlin, use the value for the static ADVANCED_OK provided by TFT
//
else if (tx_slots == HOST_SLOTS_REGULAR_OK)
// if ADVANCED_OK is enabled in TFT or Marlin
//
else if (target_tx_slots >= 0)
{
handle_static_ok:

// the following check should always be matched unless a bug is present in the TFT code or in the mainboard reply
// the following check is matched in case:
// - ADVANCED_OK is enabled in TFT. infoSettings.tx_slots for static ADVANCED_OK configured in TFT is used
// - ADVANCED_OK is enabled in Marlin but the mainboard reply (target_tx_slots) is out of sync with the current pending
// gcodes (it happens sometimes). infoSettings.tx_slots for Marlin ADVANCED_OK detected at TFT boot is used
//
if (infoSettings.tx_slots >= infoHost.tx_count)
if (target_tx_slots + infoHost.tx_count >= infoSettings.tx_slots)
infoHost.tx_slots = infoSettings.tx_slots - infoHost.tx_count;
// in case of bug, reset infoSettings.tx_slots and infoHost.tx_count to try to avoid a possible TFT freeze
//
else
goto handle_error;
}
// if ADVANCED_OK is enabled in both TFT and Marlin, use the value provided by Marlin
//
else if (tx_slots >= 0)
{
// the following check should always be matched unless the mainboard reply
// is out of synch with the current pending gcodes (it happens sometimes)
//
if (infoSettings.tx_slots >= tx_slots + infoHost.tx_count)
{
// if printing from onboard media tx_slots is always reported as 0 by Marlin even if there are no pending gcodes so
// just set infoHost.tx_slots to 1 to allow the transmission of one gcode per time avoiding a possible TFT freeze
//
if (tx_slots != 0 || infoHost.tx_count != 0)
infoHost.tx_slots = tx_slots;
// if printing from onboard media
//
else
infoHost.tx_slots = 1;
}
// in case of the mainboard reply is out of synch with the current pending gcodes
// if printing from onboard media target_tx_slots is always reported as 0 by Marlin even if there are no pending gcodes
// so just set infoHost.tx_slots to 1 to allow the transmission of one gcode per time avoiding a possible TFT freeze
//
else
{
goto handle_static_ok;
}
else if (target_tx_slots != 0 || infoHost.tx_count != 0) // if not printing from onboard media
infoHost.tx_slots = target_tx_slots;
else // if printing from onboard media
infoHost.tx_slots = 1;

infoHost.target_tx_slots = infoHost.tx_slots; // set new current target
}
// if generic OK response handling (e.g. temperature response), increment the current value
//
// if generic OK response handling (e.g. temperature response), increment the current value up to current target
//
else
{
infoHost.tx_slots++;
if (infoHost.tx_slots < infoHost.target_tx_slots)
infoHost.tx_slots++;
}
}

Expand Down
17 changes: 8 additions & 9 deletions TFT/src/User/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ typedef enum

typedef enum
{
HOST_SLOTS_GENERIC_OK = -2,
HOST_SLOTS_REGULAR_OK,
HOST_SLOTS_GENERIC_OK = -1,
} HOST_SLOTS;

typedef struct
{
uint8_t tx_slots; // keep track of available gcode tx slots (e.g. if ADVANCED_OK feature is enabled on both mainboard and TFT)
uint8_t tx_count; // keep track of pending gcode tx count
bool connected; // whether have connected to Marlin
HOST_STATUS status; // whether the host is busy in printing execution. (USB serial printing and gcode print from onboard)
uint8_t target_tx_slots; // keep track of target gcode tx slots (e.g. if ADVANCED_OK feature is enabled on both mainboard and TFT)
uint8_t tx_slots; // keep track of available gcode tx slots (e.g. if ADVANCED_OK feature is enabled on both mainboard and TFT)
uint8_t tx_count; // keep track of pending gcode tx count
bool connected; // whether have connected to Marlin
HOST_STATUS status; // whether the host is busy in printing execution. (USB serial printing and gcode print from onboard)
} HOST;

typedef struct
Expand All @@ -58,9 +58,8 @@ void InfoHost_Init(bool isConnected);

// handle OK response:
// - tx_slots (used/effective only in case "advanced_ok" configuration setting is also enabled in TFT):
// - HOST_SLOTS_GENERIC_OK: to increase infoHost.tx_slots and decrease infoHost.tx_count by 1 respectively
// - HOST_SLOTS_REGULAR_OK: to handle static ADVANCED_OK
// - >= 0: to handle Marlin ADVANCED_OK
// - < 0 (HOST_SLOTS_GENERIC_OK): to increase infoHost.tx_slots up to current target and decrease infoHost.tx_count by 1
// - >= 0: to handle static ADVANCED_OK and Marlin ADVANCED_OK
void InfoHost_HandleOkAck(int16_t tx_slots);

#ifdef __cplusplus
Expand Down

0 comments on commit 63403cc

Please sign in to comment.