Skip to content

Commit

Permalink
Added Command Checksum + code optimization + cleanup (#2889)
Browse files Browse the repository at this point in the history
* Added Command Checksum + code optimization + cleanup
  • Loading branch information
digant73 committed Feb 19, 2024
1 parent d9d8bac commit 4f722c3
Show file tree
Hide file tree
Showing 122 changed files with 1,866 additions and 1,405 deletions.
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ Only the TFTs listed below are currently supported. Trying to install the firmwa
**MKS TFT**

MKS_TFT28_V3.0 and V4.0
MKS_TFT28_NEW_GENIUS
MKS_TFT32_V1.3 and V1.4
MKS_TFT32L_V3_0
MKS_TFT35_V1_0
MKS_TFT32L_V3.0
MKS_TFT35_V1.0

**MKS TFT with GigaDevice MCUs**

Expand Down Expand Up @@ -457,23 +458,39 @@ Please, see [Customization Guides](https://github.com/bigtreetech/BIGTREETECH-To
<img src="https://user-images.githubusercontent.com/25599056/56637532-77115000-669e-11e9-809b-f6bc25412f75.png"></li>

<li>After opening the project, edit <a href="platformio.ini"><code>platformio.ini</code></a> and change the <code>default_envs</code> to one that matches your TFT model and version:
<pre>;BIGTREE_TFT35_V1_0
<pre>;BIGTREE_TFT24_V1_1
;BIGTREE_TFT28_V1_0
;BIGTREE_TFT28_V3_0
;BIGTREE_TFT35_V1_0
;BIGTREE_TFT35_V1_1
;BIGTREE_TFT35_V1_2
;BIGTREE_TFT35_V2_0
;BIGTREE_TFT35_V3_0
;BIGTREE_TFT35_E3_V3_0
;BIGTREE_TFT28_V1_0
;BIGTREE_TFT28_V3_0
;BIGTREE_TFT24_V1_1
;MKS_TFT32_V1_3
;MKS_TFT32_V1_4
;MKS_TFT32_V1_4_NOBL
;BIGTREE_TFT35_B1_V3_0
;BIGTREE_TFT43_V3_0
;BIGTREE_TFT50_V3_0
;BIGTREE_TFT70_V3_0

;BIGTREE_GD_TFT24_V1_1
;BIGTREE_GD_TFT35_V2_0
;BIGTREE_GD_TFT35_V3_0
;BIGTREE_GD_TFT35_E3_V3_0
;BIGTREE_GD_TFT35_B1_V3_0
;BIGTREE_GD_TFT43_V3_0
;BIGTREE_GD_TFT50_V3_0
;BIGTREE_GD_TFT70_V3_0

;MKS_TFT28_V3_0
;MKS_TFT28_V4_0
;MKS_TFT28_NEW_GENIUS
;MKS_TFT32_V1_3
;MKS_TFT32_V1_4
;MKS_TFT32_V1_4_NOBL
;MKS_TFT32L_V3_0
;MKS_TFT35_V1_0
;MKS_GD_TFT28_V1_2-4

[platformio]
src_dir = TFT
boards_dir = buildroot/boards
Expand Down
85 changes: 38 additions & 47 deletions TFT/src/User/API/AddonHardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
#include "GPIO_Init.h"

//
// Power Supply
// power supply
//

#ifdef PS_ON_PIN

// Power Supply Control pins Initialization
void PS_ON_Init(void)
{
GPIO_InitSet(PS_ON_PIN, MGPIO_MODE_OUT_PP, 0);
GPIO_SetLevel(PS_ON_PIN, infoSettings.ps_active_high);
}

// Power Supply Control turn on, M80
void PS_ON_On(void)
{
GPIO_SetLevel(PS_ON_PIN, infoSettings.ps_active_high);
}

// Power Supply Control turn off, M81
void PS_ON_Off(void)
{
GPIO_SetLevel(PS_ON_PIN, !infoSettings.ps_active_high);
Expand All @@ -30,20 +27,23 @@ void PS_ON_Off(void)
#endif // PS_ON_PIN

//
// Filament runout detection
// filament runout detection
//

#ifdef FIL_RUNOUT_PIN

#define FIL_POS_E_REFRESH_TIME 2000
#define FIL_ALARM_REMINDER_TIME 10000

enum
{
FILAMENT_SENSOR_NORMAL,
FILAMENT_SENSOR_SMART,
};

static uint32_t nextUpdateTime = FIL_ALARM_REMINDER_TIME; // Give TFT time to connect to mainboard first before polling for runout
static bool posE_updateWaiting = false;
static bool sfs_alive = false; // Use an encoder disc to toggles the runout. Suitable for BigTreeTech Smart Filament Sensor
static uint32_t posE_nextUpdateTime = FIL_ALARM_REMINDER_TIME; // give TFT time to connect to mainboard first before polling for runout
static bool posE_sendingWaiting = false;
static bool sfs_alive = false; // use an encoder disc to toggles the runout. Suitable for BigTreeTech Smart Filament Sensor

void FIL_Runout_Init(void)
{
Expand Down Expand Up @@ -73,15 +73,9 @@ void FIL_Runout_Init(void)
#endif
}

static inline void FIL_SetNextUpdateTime(uint32_t timeInterval)
{
nextUpdateTime = OS_GetTimeMs() + timeInterval;
}

// Set whether we need to query the current position
void FIL_PosE_SetUpdateWaiting(bool waiting)
void FIL_PosE_ClearSendingWaiting(void)
{
posE_updateWaiting = waiting;
posE_sendingWaiting = false;
}

void FIL_SFS_SetAlive(bool alive)
Expand All @@ -93,14 +87,9 @@ bool FIL_NormalRunoutDetect(void)
{
static bool runout = false;
static int32_t trigBalance = 0;
static uint32_t nextUpdateTime = 0;

if (OS_GetTimeMs() > nextUpdateTime)
{
runout = (trigBalance > 0);
trigBalance = 0;
FIL_SetNextUpdateTime(infoSettings.runout_noise);
}
else
if (OS_GetTimeMs() < nextUpdateTime)
{
bool pinState = false;
uint8_t toolNum = heatGetToolIndex();
Expand All @@ -116,34 +105,47 @@ bool FIL_NormalRunoutDetect(void)
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN_1);
break;
#endif

#ifdef FIL_RUNOUT_PIN_2
case 2:
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN_2);
break;
#endif

#ifdef FIL_RUNOUT_PIN_3
case 3:
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN_3);
break;
#endif

#ifdef FIL_RUNOUT_PIN_4
case 4:
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN_4);
break;
#endif

#ifdef FIL_RUNOUT_PIN_5
case 5:
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN_5);
break;
#endif

default:
pinState = GPIO_GetLevel(FIL_RUNOUT_PIN);
break;
}

trigBalance += (pinState == GET_BIT(infoSettings.runout, RUNOUT_INVERTED)) ? 1: -1; // if triggered add 1 else substract 1

return runout;
}

// if OS_GetTimeMs() >= nextUpdateTime

runout = (trigBalance > 0);
trigBalance = 0;
nextUpdateTime = OS_GetTimeMs() + infoSettings.runout_noise;

return runout;
}

Expand All @@ -156,34 +158,23 @@ static inline bool FIL_SmartRunoutDetect(void)
bool runout = FIL_NormalRunoutDetect();

do
{
// Send M114 E to query extrude position continuously
if (posE_updateWaiting == true)
{
FIL_SetNextUpdateTime(FIL_POS_E_UPDATE_TIME);
break;
}
{ // send M114 E to query extrude position continuously

if (OS_GetTimeMs() < nextUpdateTime)
if (OS_GetTimeMs() < posE_nextUpdateTime) // if next check time not yet elapsed, do nothing
break;

if (requestCommandInfoIsRunning()) // To avoid collision in gcode response processing
break;
posE_nextUpdateTime = OS_GetTimeMs() + FIL_POS_E_REFRESH_TIME; // extend next check time

if (!storeCmd("M114 E\n"))
// if M114 previously enqueued and not yet sent or pending command
// (to avoid collision in gcode response processing), do nothing
if (posE_sendingWaiting || requestCommandInfoIsRunning())
break;

FIL_SetNextUpdateTime(FIL_POS_E_UPDATE_TIME);
posE_updateWaiting = true;
posE_sendingWaiting = storeCmd("M114 E\n");
} while (0);

if (sfs_alive == false)
{
if (lastRunout != runout)
{
sfs_alive = true;
}
}
if (!sfs_alive && lastRunout != runout)
sfs_alive = true;

if (ABS(posE - lastPosE) >= infoSettings.runout_distance)
{
Expand Down Expand Up @@ -220,10 +211,10 @@ static inline bool FIL_IsRunout(void)

void FIL_BE_CheckRunout(void)
{
if (!GET_BIT(infoSettings.runout, RUNOUT_ENABLED)) // Filament runout turned off
if (!GET_BIT(infoSettings.runout, RUNOUT_ENABLED)) // filament runout turned off
return;

setPrintRunout(FIL_IsRunout()); // Need constant scanning to filter interference
setPrintRunout(FIL_IsRunout()); // need constant scanning to filter interference
}

void FIL_FE_CheckRunout(void)
Expand All @@ -233,13 +224,13 @@ void FIL_FE_CheckRunout(void)
if (!getPrintRunout() && !getRunoutAlarm())
return;

if (pausePrint(true, PAUSE_NORMAL) && !getRunoutAlarm()) // If not printing, pausePrint() function will always fail
if (pausePrint(true, PAUSE_NORMAL) && !getRunoutAlarm()) // if not printing, pausePrint() function will always fail
{ // so no useless error message is displayed
setRunoutAlarmTrue();
popupDialog(DIALOG_TYPE_ALERT, LABEL_WARNING, LABEL_FILAMENT_RUNOUT, LABEL_CONFIRM, LABEL_NULL, setRunoutAlarmFalse, NULL, NULL);
}

if ((OS_GetTimeMs() > nextReminderTime) && (getRunoutAlarm() == true))
if (OS_GetTimeMs() >= nextReminderTime && getRunoutAlarm())
{
BUZZER_PLAY(SOUND_ERROR);
nextReminderTime = OS_GetTimeMs() + FIL_ALARM_REMINDER_TIME;
Expand Down
19 changes: 8 additions & 11 deletions TFT/src/User/API/AddonHardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ extern "C" {
#include <stdbool.h>
#include "variants.h" // for PS_ON_PIN, FIL_RUNOUT_PIN etc.

// Power Supply
// power supply
#ifdef PS_ON_PIN
void PS_ON_Init(void);
void PS_ON_On(void);
void PS_ON_Off(void);
void PS_ON_Init(void); // power supply control pins initialization
void PS_ON_On(void); // power supply control turn on, M80
void PS_ON_Off(void); // power supply control turn off, M81
#endif

// Filament runout detection
// filament runout detection
#ifdef FIL_RUNOUT_PIN
#define FIL_POS_E_UPDATE_TIME 2000
#define FIL_ALARM_REMINDER_TIME 10000

void FIL_Runout_Init(void);
void FIL_PosE_SetUpdateWaiting(bool waiting);
void FIL_PosE_ClearSendingWaiting(void); // clear sending waiting for position query
void FIL_SFS_SetAlive(bool alive);
void FIL_BE_CheckRunout(void);
void FIL_FE_CheckRunout(void);
void FIL_BE_CheckRunout(void); // called in loopBackEnd()
void FIL_FE_CheckRunout(void); // called in loopFrontEnd()
#endif

#ifdef __cplusplus
Expand Down
36 changes: 17 additions & 19 deletions TFT/src/User/API/FanControl.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "FanControl.h"
#include "includes.h"

#define NEXT_FAN_WAIT 500 // 1 second is 1000
#define FAN_REFRESH_TIME 500 // 1 second is 1000

const char* fanID[MAX_FAN_COUNT] = FAN_DISPLAY_ID;
const char* fanCmd[MAX_FAN_COUNT] = FAN_CMD;
const char * fanID[MAX_FAN_COUNT] = FAN_DISPLAY_ID;
const char * fanCmd[MAX_FAN_COUNT] = FAN_CMD;

static uint8_t setFanSpeed[MAX_FAN_COUNT] = {0};
static uint8_t curFanSpeed[MAX_FAN_COUNT] = {0};
static uint8_t needSetFanSpeed = 0;

static bool ctrlFanQueryWait = false;
static uint32_t nextCtrlFanTime = 0;
static bool ctrlFanSendingWaiting = false;

void fanResetSpeed(void)
{
Expand All @@ -20,7 +19,6 @@ void fanResetSpeed(void)
memset(curFanSpeed, 0, sizeof(curFanSpeed));
}

// Check whether the index is a valid fan index.
bool fanIsValid(const uint8_t index)
{
if (index >= infoSettings.fan_count && index < MAX_COOLING_FAN_COUNT) // invalid cooling fan index
Expand Down Expand Up @@ -74,32 +72,32 @@ uint8_t fanGetCurPercent(const uint8_t i)
return (curFanSpeed[i] * 100.0f) / infoSettings.fan_max[i] + 0.5f;
}

void loopFan(void)
void loopCheckFan(void)
{
static uint32_t nextUpdateTime = 0;

if (OS_GetTimeMs() < nextUpdateTime) // avoid rapid fire, clogging the queue
return;

nextUpdateTime = OS_GetTimeMs() + FAN_REFRESH_TIME; // extend next check time

for (uint8_t i = 0; i < MAX_FAN_COUNT; i++)
{
if (GET_BIT(needSetFanSpeed, i) && (OS_GetTimeMs() > nextCtrlFanTime))
if (GET_BIT(needSetFanSpeed, i))
{
if (storeCmd(fanCmd[i], setFanSpeed[i]))
{
SET_BIT_OFF(needSetFanSpeed, i);
}

nextCtrlFanTime = OS_GetTimeMs() + NEXT_FAN_WAIT; // avoid rapid fire, clogging the queue
}
}
}

void ctrlFanQuerySetWait(const bool wait)
void ctrlFanQueryClearSendingWaiting(void)
{
ctrlFanQueryWait = wait;
ctrlFanSendingWaiting = false;
}

// query for controller fan only
void ctrlFanQuery(void)
{ // following conditions ordered by importance
if (!ctrlFanQueryWait && infoHost.tx_slots != 0 && infoHost.connected && infoSettings.ctrl_fan_en)
{
ctrlFanQueryWait = storeCmd("M710\n");
}
if (infoSettings.ctrl_fan_en && !ctrlFanSendingWaiting && infoHost.tx_slots != 0 && infoHost.connected)
ctrlFanSendingWaiting = storeCmd("M710\n");
}
Loading

0 comments on commit 4f722c3

Please sign in to comment.