Skip to content

Commit

Permalink
Implemented #2317 + some code reduction (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
digant73 authored Mar 21, 2022
1 parent c9895ab commit 14ba07c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 97 deletions.
68 changes: 10 additions & 58 deletions TFT/src/User/API/CaseLightControl.c
Original file line number Diff line number Diff line change
@@ -1,84 +1,36 @@
#include "CaseLightControl.h"
#include "includes.h"

static uint8_t caseLightBrightness = 0;
static uint8_t lastCaseLightBrightness = 0;
static bool lastCaseLightState = true;
static bool caseLightState = true;
static uint8_t caseLightBrightness = 0;

static bool lightQueryWait = false;
static bool caseLight_applied = false;

// Sends a query (M355) to the printer asking for the current brightness
void caseLightValueQuery(void)
bool caseLightGetState(void)
{
if (infoMachineSettings.caseLightsBrightness == ENABLED && infoHost.connected &&
!infoHost.wait && !lightQueryWait)
{
lightQueryWait = storeCmd("M355\n");
}
return caseLightState;
}

void caseLightSetState(bool state)
{
caseLightState = state;
}

void caseLightSetBrightness(uint8_t brightness)
{
caseLightBrightness = NOBEYOND(0, brightness, 255);
}

void caseLightChangeBrightness(int8_t brightness_delta)
{
caseLightBrightness = NOBEYOND(0, caseLightBrightness + brightness_delta, 255);
}

void caseLightChangeBrightnessPrecent(int8_t brightness_delta_percent)
{
brightness_delta_percent = NOBEYOND(-100, brightness_delta_percent, 100);
caseLightBrightness = NOBEYOND(0, (caseLightGetBrightnessPercent() + brightness_delta_percent) * 255 / 100.0f, 255);
}

void caseLightQuerySetWait(bool wait)
{
lightQueryWait = wait;
}

uint8_t caseLightGetBrightness(void)
{
return caseLightBrightness;
}

uint8_t caseLightGetBrightnessPercent(void)
{
return (caseLightBrightness * 100.0f) / 255 + 0.5f;
}

bool caseLightGetState(void)
void caseLightSetBrightness(uint8_t brightness)
{
return caseLightState;
caseLightBrightness = NOBEYOND(0, brightness, 255);
}

void caseLightApplied(bool applied)
uint8_t caseLightGetBrightnessPercent(void)
{
caseLight_applied = applied;
return (caseLightBrightness * 100.0f) / 255 + 0.5f;
}

void loopCaseLight(void)
void caseLightSetBrightnessPercent(int8_t unit)
{
if (lastCaseLightBrightness != caseLightBrightness || lastCaseLightState != caseLightState)
{
if (caseLight_applied == false)
{
caseLight_applied = storeCmd("M355 S%d P%d\n", caseLightState ? 1 : 0, caseLightBrightness);
}

if (caseLight_applied == true)
{
lastCaseLightBrightness = caseLightBrightness;
lastCaseLightState = caseLightState;
caseLight_applied = false;
}
}
unit = NOBEYOND(-100, unit, 100);
caseLightBrightness = NOBEYOND(0, (caseLightGetBrightnessPercent() + unit) * 255 / 100.0f, 255);
}
13 changes: 5 additions & 8 deletions TFT/src/User/API/CaseLightControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ extern "C" {
#include <stdbool.h>
#include <stdint.h>

void caseLightValueQuery(void);
bool caseLightGetState(void);
void caseLightSetState(bool state);
void caseLightSetBrightness(uint8_t brightness);
void caseLightQuerySetWait(bool wait);

uint8_t caseLightGetBrightness(void);
void caseLightSetBrightness(uint8_t brightness);

uint8_t caseLightGetBrightnessPercent(void);
bool caseLightGetState(void);
void loopCaseLight(void);
void caseLightApplied(bool applied);
void caseLightChangeBrightness(int8_t brightness_delta);
void caseLightChangeBrightnessPrecent(int8_t brightness_delta_percent);
void caseLightSetBrightnessPercent(int8_t unit);

#ifdef __cplusplus
}
Expand Down
1 change: 0 additions & 1 deletion TFT/src/User/API/interfaceCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,6 @@ void sendQueueCmd(void)
{
caseLightSetBrightness(cmd_value());
}
caseLightApplied(true);
break;
}

Expand Down
5 changes: 0 additions & 5 deletions TFT/src/User/API/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,11 +1304,6 @@ void loopBackEnd(void)
if (GET_BIT(infoSettings.general_settings, INDEX_EVENT_LED) == 1)
LED_CheckEvent();

if (infoMachineSettings.caseLightsBrightness == ENABLED)
{
loopCaseLight();
}

// Query RRF status
rrfStatusQuery();
} // loopBackEnd
Expand Down
2 changes: 0 additions & 2 deletions TFT/src/User/API/parseACK.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,6 @@ void parseACK(void)
caseLightSetState(true);
caseLightSetBrightness(ack_value());
}
caseLightQuerySetWait(false);
caseLightApplied(true);
}
// parse and store M420 V1 T1, mesh data (e.g. from Mesh Editor menu)
//
Expand Down
65 changes: 42 additions & 23 deletions TFT/src/User/Menu/CaseLight.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include "CaseLight.h"
#include "includes.h"

#define CASE_LIGHT_UPDATE_TIME 1000 // 1 seconds is 1000

// Icons list for Case Light
const ITEM itemCaseLight[2] = {
// icon label
{ICON_RGB_OFF, LABEL_OFF},
{ICON_RGB_WHITE, LABEL_ON},
};

static uint8_t curUnit_index = 1;

static inline void updateCaseLightIcon(MENUITEMS * curmenu, bool state)
{
curmenu->items[KEY_ICON_4] = itemCaseLight[state ? 1 : 0];
curmenu->items[KEY_ICON_5] = itemCaseLight[state ? 1 : 0];
}

void caseLightBrightnessReDraw()
Expand All @@ -34,24 +39,25 @@ void menuCaseLight(void)
{ICON_NULL, LABEL_NULL},
{ICON_NULL, LABEL_NULL},
{ICON_INC, LABEL_INC},
{ICON_E_5_PERCENT, LABEL_5_PERCENT},
{ICON_RGB_WHITE, LABEL_ON},
{ICON_NULL, LABEL_NULL},
{ICON_NULL, LABEL_NULL},
{ICON_BACK, LABEL_BACK},
}
};

KEY_VALUES key_num = KEY_IDLE;

// Initiate query. Value will be compared in while loop
caseLightValueQuery();

bool currentCaseLightState = caseLightGetState();
bool previousCaseLightState = currentCaseLightState;
uint8_t currentCaseLightBrightness = caseLightGetBrightness();
uint8_t previousCaseLightBrightness = currentCaseLightBrightness;
bool sendingNeeded = false;

storeCmd("M355\n"); // retrieve the case light's state and brightness. Value will be compared in while loop

caseLightItems.items[KEY_ICON_4] = itemPercent[curUnit_index];
updateCaseLightIcon(&caseLightItems, currentCaseLightState);

menuDrawPage(&caseLightItems);
caseLightBrightnessReDraw();

Expand All @@ -61,52 +67,65 @@ void menuCaseLight(void)

switch (key_num)
{
// decrease case light
case KEY_ICON_0:
caseLightChangeBrightnessPrecent(-10);
caseLightBrightnessReDraw();
case KEY_DECREASE:
caseLightSetBrightnessPercent(- percentSteps[curUnit_index]);
break;

// increase case light
case KEY_ICON_3:
caseLightChangeBrightnessPrecent(10);
caseLightBrightnessReDraw();
case KEY_INCREASE:
caseLightSetBrightnessPercent(+ percentSteps[curUnit_index]);
break;

// change unit
case KEY_ICON_4:
curUnit_index = (curUnit_index + 1) % ITEM_PERCENT_STEPS_NUM;

caseLightItems.items[key_num] = itemPercent[curUnit_index];
menuDrawItem(&caseLightItems.items[key_num], key_num);
break;

// switch on/off case light
case KEY_ICON_5:
caseLightSetState(!currentCaseLightState);
caseLightBrightnessReDraw();
break;

case KEY_ICON_7:
CLOSE_MENU();
break;

default:
#if LCD_ENCODER_SUPPORT
if (encoderPosition)
{
caseLightChangeBrightnessPrecent(encoderPosition);
caseLightBrightnessReDraw();
encoderPosition = 0;
}
#endif
break;
}

currentCaseLightState = caseLightGetState();
if (previousCaseLightState != currentCaseLightState)
if (previousCaseLightState != currentCaseLightState) // dynamically change the light on/off icon based on the current state
{
// Dynamically change the light on/off icon based on the current state
previousCaseLightState = currentCaseLightState;

updateCaseLightIcon(&caseLightItems, currentCaseLightState);
menuDrawItem(&caseLightItems.items[KEY_ICON_4], KEY_ICON_4);
caseLightBrightnessReDraw();
menuDrawItem(&caseLightItems.items[KEY_ICON_5], KEY_ICON_5);

sendingNeeded = true;
}

currentCaseLightBrightness = caseLightGetBrightness();
if (previousCaseLightBrightness != currentCaseLightBrightness)
{
previousCaseLightBrightness = currentCaseLightBrightness;

caseLightBrightnessReDraw();

sendingNeeded = true;
}

if (sendingNeeded && nextScreenUpdate(CASE_LIGHT_UPDATE_TIME))
{
storeCmd("M355 S%d P%d\n", currentCaseLightState ? 1 : 0, currentCaseLightBrightness);

sendingNeeded = false;
}

loopProcess();
Expand Down

0 comments on commit 14ba07c

Please sign in to comment.