Skip to content

Commit

Permalink
M256 LCD brightness (MarlinFirmware#22478)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and mh-dm committed May 15, 2022
1 parent e2079a3 commit aea15b2
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 250: M250(); break; // M250: Set LCD contrast
#endif

#if HAS_LCD_BRIGHTNESS
case 256: M256(); break; // M256: Set LCD brightness
#endif

#if ENABLED(EXPERIMENTAL_I2CBUS)
case 260: M260(); break; // M260: Send data to an i2c slave
case 261: M261(); break; // M261: Request data from an i2c slave
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/gcode/gcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
* M226 - Wait until a pin is in a given state: "M226 P<pin> S<state>" (Requires DIRECT_PIN_CONTROL)
* M240 - Trigger a camera to take a photograph. (Requires PHOTO_GCODE)
* M250 - Set LCD contrast: "M250 C<contrast>" (0-63). (Requires LCD support)
* M256 - Set LCD brightness: "M256 B<brightness>" (0-255). (Requires an LCD with brightness control)
* M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS)
* M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS)
* M280 - Set servo position absolute: "M280 P<index> S<angle|µs>". (Requires servos)
Expand Down Expand Up @@ -820,6 +821,10 @@ class GcodeSuite {
static void M250();
#endif

#if HAS_LCD_BRIGHTNESS
static void M256();
#endif

#if ENABLED(EXPERIMENTAL_I2CBUS)
static void M260();
static void M261();
Expand Down
37 changes: 37 additions & 0 deletions Marlin/src/gcode/lcd/M256.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../../inc/MarlinConfig.h"

#if HAS_LCD_BRIGHTNESS

#include "../gcode.h"
#include "../../lcd/marlinui.h"

/**
* M256: Set the LCD brightness
*/
void GcodeSuite::M256() {
if (parser.seenval('B')) ui.set_brightness(parser.value_int());
SERIAL_ECHOLNPAIR("LCD Brightness: ", ui.brightness);
}

#endif // HAS_LCD_BRIGHTNESS
4 changes: 2 additions & 2 deletions Marlin/src/gcode/lcd/M73.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
* M73 P25 ; Set progress to 25%
*/
void GcodeSuite::M73() {
if (parser.seen('P'))
if (parser.seenval('P'))
ui.set_progress((PROGRESS_SCALE) > 1
? parser.value_float() * (PROGRESS_SCALE)
: parser.value_byte()
);
#if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
if (parser.seen('R')) ui.set_remaining_time(60 * parser.value_ulong());
if (parser.seenval('R')) ui.set_remaining_time(60 * parser.value_ulong());
#endif
}

Expand Down
11 changes: 11 additions & 0 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
}
#endif

#if HAS_LCD_BRIGHTNESS
uint8_t MarlinUI::brightness = DEFAULT_LCD_BRIGHTNESS;
bool MarlinUI::backlight = true;

void MarlinUI::set_brightness(const uint8_t value) {
backlight = !!value;
if (backlight) brightness = constrain(value, MIN_LCD_BRIGHTNESS, MAX_LCD_BRIGHTNESS);
// Set brightness on enabled LCD here
}
#endif

#if ENABLED(SOUND_MENU_ITEM)
bool MarlinUI::buzzer_enabled = true;
#endif
Expand Down
16 changes: 16 additions & 0 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ class MarlinUI {
static void media_changed(const uint8_t old_stat, const uint8_t stat);
#endif

#if HAS_LCD_BRIGHTNESS
#ifndef MIN_LCD_BRIGHTNESS
#define MIN_LCD_BRIGHTNESS 1
#endif
#ifndef MAX_LCD_BRIGHTNESS
#define MAX_LCD_BRIGHTNESS 255
#endif
#ifndef DEFAULT_LCD_BRIGHTNESS
#define DEFAULT_LCD_BRIGHTNESS MAX_LCD_BRIGHTNESS
#endif
static uint8_t brightness;
static bool backlight;
static void set_brightness(const uint8_t value);
FORCE_INLINE static void refresh_brightness() { set_brightness(brightness); }
#endif

#if ENABLED(DWIN_CREALITY_LCD)
static void refresh();
#else
Expand Down
45 changes: 36 additions & 9 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/

// Change EEPROM version if the structure changes
#define EEPROM_VERSION "V83"
#define EEPROM_VERSION "V84"
#define EEPROM_OFFSET 100

// Check the integrity of data offsets.
Expand Down Expand Up @@ -353,6 +353,11 @@ typedef struct SettingsDataStruct {
//
int16_t lcd_contrast; // M250 C

//
// HAS_LCD_BRIGHTNESS
//
uint8_t lcd_brightness; // M256 B

//
// Controller fan settings
//
Expand Down Expand Up @@ -999,17 +1004,19 @@ void MarlinSettings::postprocess() {
//
{
_FIELD_TEST(lcd_contrast);

const int16_t lcd_contrast =
#if HAS_LCD_CONTRAST
ui.contrast
#else
127
#endif
;
const int16_t lcd_contrast = TERN(HAS_LCD_CONTRAST, ui.contrast, 127);
EEPROM_WRITE(lcd_contrast);
}

//
// LCD Brightness
//
{
_FIELD_TEST(lcd_brightness);
const uint8_t lcd_brightness = TERN(HAS_LCD_BRIGHTNESS, ui.brightness, 255);
EEPROM_WRITE(lcd_brightness);
}

//
// Controller Fan
//
Expand Down Expand Up @@ -1846,6 +1853,16 @@ void MarlinSettings::postprocess() {
}
}

//
// LCD Brightness
//
{
_FIELD_TEST(lcd_brightness);
uint8_t lcd_brightness;
EEPROM_READ(lcd_brightness);
TERN_(HAS_LCD_BRIGHTNESS, if (!validating) ui.set_brightness(lcd_brightness));
}

//
// Controller Fan
//
Expand Down Expand Up @@ -2829,6 +2846,11 @@ void MarlinSettings::reset() {
//
TERN_(HAS_LCD_CONTRAST, ui.set_contrast(DEFAULT_LCD_CONTRAST));

//
// LCD Brightness
//
TERN_(HAS_LCD_BRIGHTNESS, ui.set_brightness(DEFAULT_LCD_BRIGHTNESS));

//
// Controller Fan
//
Expand Down Expand Up @@ -3406,6 +3428,11 @@ void MarlinSettings::reset() {
CONFIG_ECHO_MSG(" M250 C", ui.contrast);
#endif

#if HAS_LCD_BRIGHTNESS
CONFIG_ECHO_HEADING("LCD Brightness:");
CONFIG_ECHO_MSG(" M256 B", ui.brightness);
#endif

TERN_(CONTROLLER_FAN_EDITABLE, M710_report(forReplay));

#if ENABLED(POWER_LOSS_RECOVERY)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/stepper/trinamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ void reset_trinamic_drivers() {
TMC_SW_DETAIL(Y), TMC_SW_DETAIL(Y2),
TMC_SW_DETAIL(Z), TMC_SW_DETAIL(Z2), TMC_SW_DETAIL(Z3), TMC_SW_DETAIL(Z4),
TMC_SW_DETAIL(I), TMC_SW_DETAIL(J), TMC_SW_DETAIL(K),
TMC_SW_DETAIL(E0), TMC_SW_DETAIL(E1), TMC_SW_DETAIL(E2), TMC_SW_DETAIL(E3), TMC_SW_DETAIL(E4), TMC_SW_DETAIL(E5), TMC_SW_DETAIL(E6), TMC_SW_DETAIL(E7)
TMC_SW_DETAIL(E0), TMC_SW_DETAIL(E1), TMC_SW_DETAIL(E2), TMC_SW_DETAIL(E3), TMC_SW_DETAIL(E4), TMC_SW_DETAIL(E5), TMC_SW_DETAIL(E6), TMC_SW_DETAIL(E7)
};

constexpr bool sc_sw_done(size_t start, size_t end) { return start == end; }
Expand Down
1 change: 1 addition & 0 deletions ini/features.ini
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ HAS_GCODE_M876 = src_filter=+<src/gcode/host/M876.cpp>
HAS_RESUME_CONTINUE = src_filter=+<src/gcode/lcd/M0_M1.cpp>
HAS_STATUS_MESSAGE = src_filter=+<src/gcode/lcd/M117.cpp>
HAS_LCD_CONTRAST = src_filter=+<src/gcode/lcd/M250.cpp>
HAS_LCD_BRIGHTNESS = src_filter=+<src/gcode/lcd/M256.cpp>
HAS_BUZZER = src_filter=+<src/gcode/lcd/M300.cpp>
LCD_SET_PROGRESS_MANUALLY = src_filter=+<src/gcode/lcd/M73.cpp>
TOUCH_SCREEN_CALIBRATION = src_filter=+<src/gcode/lcd/M995.cpp>
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ default_src_filter = +<src/*> -<src/config> -<src/HAL> +<src/HAL/shared>
-<src/gcode/host/M876.cpp>
-<src/gcode/lcd/M0_M1.cpp>
-<src/gcode/lcd/M117.cpp>
-<src/gcode/lcd/M250.cpp>
-<src/gcode/lcd/M250.cpp> -<src/gcode/lcd/M256.cpp>
-<src/gcode/lcd/M300.cpp>
-<src/gcode/lcd/M414.cpp>
-<src/gcode/lcd/M73.cpp>
Expand Down

0 comments on commit aea15b2

Please sign in to comment.