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

Add adjustable PWM frequency to minimize buzzing noise #1179

Merged
merged 8 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/system/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef struct settings_s {
int ingame_long_press;
int ingame_double_press;
bool disable_standby;
int pwmfrequency;
bool enable_logging;
int blue_light_state;
int blue_light_level;
Expand Down Expand Up @@ -106,6 +107,7 @@ static settings_s __default_settings = (settings_s){
.blue_light_rgb = 8421504,
.blue_light_time = "20:00",
.blue_light_time_off = "08:00",
.pwmfrequency = 7,
.mainui_button_x = "",
.mainui_button_y = ""};

Expand Down Expand Up @@ -205,6 +207,7 @@ void settings_load(void)
config_get("display/blueLightTime", CONFIG_STR, &settings.blue_light_time);
config_get("display/blueLightTimeOff", CONFIG_STR, &settings.blue_light_time_off);
config_get("display/blueLightRGB", CONFIG_INT, &settings.blue_light_rgb);
config_get("pwmfrequency", CONFIG_INT, &settings.pwmfrequency);

if (config_flag_get(".menuInverted")) { // flag is deprecated, but keep compatibility
settings.ingame_single_press = 2;
Expand Down Expand Up @@ -331,6 +334,7 @@ void settings_save(void)
config_setString("display/blueLightTime", settings.blue_light_time);
config_setString("display/blueLightTimeOff", settings.blue_light_time_off);

config_setNumber("pwmfrequency", settings.pwmfrequency);
// remove deprecated flags
remove(CONFIG_PATH ".noLowBatteryAutoSave");
remove(CONFIG_PATH ".noBatteryWarning");
Expand Down
10 changes: 10 additions & 0 deletions src/tweaks/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@ void action_advancedSetFrameThrottle(void *pt)
stored_value_frame_throttle_changed = true;
}

void action_advancedSetPWMFreqency(void *pt)
{
FILE *fp;
int item_value = ((ListItem *)pt)->value;
int pwmfrequency = atoi(((ListItem *)pt)->value_labels[item_value]);
char *filename = "/sys/class/pwm/pwmchip0/pwm0/period";
file_put(fp, filename, "%d", pwmfrequency);
config_setNumber(".pwmfrequency", item_value);
}

void action_advancedSetSwapTriggers(void *pt)
{
int item_value = ((ListItem *)pt)->value;
Expand Down
4 changes: 4 additions & 0 deletions src/tweaks/formatters.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
{ \
"None", "Subtle", "Moderate", "Balanced", "Strong", "Intense" \
}
#define PWM_FREQUENCIES \
{ \
"100 Hz", "200 Hz", "300 Hz", "400 Hz", "500 Hz", "600 Hz", "700 Hz", "800 Hz (Default)", "900 Hz", "1000 Hz" \
}

void formatter_timezone(void *pt, char *out_label)
{
Expand Down
13 changes: 12 additions & 1 deletion src/tweaks/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ void menu_diagnostics(void *pt)
void menu_advanced(void *_)
{
if (!_menu_advanced._created) {
_menu_advanced = list_createWithTitle(6, LIST_SMALL, "Advanced");
_menu_advanced = list_createWithTitle(7, LIST_SMALL, "Advanced");
list_addItemWithInfoNote(&_menu_advanced,
(ListItem){
.label = "Swap triggers (L<>L2, R<>R2)",
Expand Down Expand Up @@ -707,6 +707,17 @@ void menu_advanced(void *_)
.value_formatter = formatter_fastForward,
.action = action_advancedSetFrameThrottle},
"Set the maximum fast forward rate.");
list_addItemWithInfoNote(&_menu_advanced,
(ListItem){
.label = "PWM frequency",
.item_type = MULTIVALUE,
.value_max = 9,
.value_labels = PWM_FREQUENCIES,
.value = value_getPWMFrequency(),
.action = action_advancedSetPWMFreqency},
"Change the PWM frequency\n"
"Lower values for less buzzing\n"
"Experimental feature");
if (DEVICE_ID == MIYOO354) {
list_addItemWithInfoNote(&_menu_advanced,
(ListItem){
Expand Down
7 changes: 7 additions & 0 deletions src/tweaks/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ void value_setSwapTriggers(void)
r_btn, l2_btn, r2_btn);
}

int value_getPWMFrequency(void)
{
int pwmfrequency = 0;
config_get(".pwmfrequency", CONFIG_INT, &pwmfrequency);
return pwmfrequency;
}

int value_getLcdVoltage(void)
{
int value = 0x0;
Expand Down
Binary file modified static/build/.tmp_update/bin/MainUI-283-clean
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-283-expert
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-354-clean
Binary file not shown.
Binary file modified static/build/.tmp_update/bin/MainUI-354-expert
Binary file not shown.
9 changes: 8 additions & 1 deletion static/build/.tmp_update/runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,14 @@ init_system() {

# init backlight
echo 0 > /sys/class/pwm/pwmchip0/export
echo 800 > /sys/class/pwm/pwmchip0/pwm0/period
pwmfile="$sysdir/config/.pwmfrequency"
if [ -s $pwmfile ]; then
# 0 - 9 = 100 - 1000 Hz
frequency=$((($(cat "$pwmfile") + 1) * 100))
else
frequency=800
fi
echo $frequency > /sys/class/pwm/pwmchip0/pwm0/period
echo $brightness_raw > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
}
Expand Down
1 change: 1 addition & 0 deletions static/configs/.tmp_update/config/.pwmfrequency
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7