Skip to content

Commit

Permalink
Fix PWM frequency setting for esp32-c3,
Browse files Browse the repository at this point in the history
Allow to change PWM resolution for WebUI
  • Loading branch information
DrA1ex committed Sep 29, 2024
1 parent e1b323c commit a0754df
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ void setup() {
analogWriteResolution(PWM_RESOLUTION);

#ifdef ARDUINO_ARCH_ESP8266
analogWriteFreq(PWM_FREQUENCY);
analogWriteFreq(std::min<uint32_t>(PWM_FREQUENCY, PWM_MAX_FREQUENCY));
#else
analogWriteFrequency(PWM_FREQUENCY);
analogWriteFrequency(std::min<uint32_t>(PWM_FREQUENCY, PWM_MAX_FREQUENCY));
#endif

ApplicationInstance.begin();
Expand Down
6 changes: 6 additions & 0 deletions src/sys_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
#define PWM_FREQUENCY (22000u)
#define PWM_MAX_VALUE ((uint16_t)((1u << PWM_RESOLUTION) - 1))

#ifdef ARDUINO_ARCH_ESP32
#define PWM_MAX_FREQUENCY (40000000ul / (1u << PWM_RESOLUTION))
#else
#define PWM_MAX_FREQUENCY (60000ul)
#endif

#define NTP_UPDATE_INTERVAL (24ul * 3600 * 1000)

#define GAMMA (2.2f)
Expand Down
13 changes: 7 additions & 6 deletions www/src/props.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {PacketType} from "./cmd.js";
import {PWM_MAX_VALUE, TEMPERATURE_MAX_VALUE} from "./sys_constants.js";

/**@type {PropertiesConfig} */
export const PropertyConfig = [{
Expand All @@ -9,14 +10,14 @@ export const PropertyConfig = [{
{key: "showTemperature", type: "skip"},

{key: "power", title: "Power", type: "trigger", kind: "Boolean", cmd: PacketType.POWER},
{key: "brightness", title: "Brightness", type: "wheel", limit: 16383, kind: "Uint16", cmd: PacketType.BRIGHTNESS},
{key: "brightness", title: "Brightness", type: "wheel", limit: PWM_MAX_VALUE, kind: "Uint16", cmd: PacketType.BRIGHTNESS},

{key: "color", title: "Color", type: "color", kind: "Uint32", cmd: PacketType.COLOR, visibleIf: "rgbMode"},
{key: "calibration", title: "Calibration", type: "color", kind: "Uint32", cmd: PacketType.CALIBRATION, visibleIf: "rgbMode"},

{
key: "colorTemperature", title: "Color Temperature", type: "wheel", limit: 32767, kind: "Uint16", cmd: PacketType.TEMPERATURE,
anchor: 16383, anchorAmount: 0.02, anchored: true, visibleIf: "cctMode",
key: "colorTemperature", title: "Color Temperature", type: "wheel", limit: TEMPERATURE_MAX_VALUE, kind: "Uint16", cmd: PacketType.TEMPERATURE,
anchor: TEMPERATURE_MAX_VALUE / 2, anchorAmount: 0.02, anchored: true, visibleIf: "cctMode",
displayConverter: function (value) {
const percent = value / this.limit * 100;
let converted;
Expand All @@ -29,14 +30,14 @@ export const PropertyConfig = [{

let [int, fraction] = converted.toFixed(1).split(".");
if (converted > 0) int = `+${int}`;
if(int === "-0") int = "0";
if (int === "-0") int = "0";

return fraction === "0" ? int : [int, `.${fraction}`];
}
},

{
key: "colorTemperatureRgb", title: "Color Temperature", type: "wheel", limit: 32767, kind: "Uint16", cmd: PacketType.TEMPERATURE,
key: "colorTemperatureRgb", title: "Color Temperature", type: "wheel", limit: TEMPERATURE_MAX_VALUE, kind: "Uint16", cmd: PacketType.TEMPERATURE,
visibleIf: "rgbMode",
displayConverter: function (value) {
const {sysConfig: {ledMinTemperature, ledMaxTemperature}} = window.__app.app.config
Expand All @@ -50,7 +51,7 @@ export const PropertyConfig = [{
}, {
key: "night_mode", section: "Night Mode", collapse: true, props: [
{key: "nightMode.enabled", title: "Enabled", type: "trigger", kind: "Boolean", cmd: PacketType.NIGHT_MODE_ENABLED},
{key: "nightMode.brightness", title: "Brightness", type: "wheel", limit: 16383, kind: "Uint16", cmd: PacketType.NIGHT_MODE_BRIGHTNESS},
{key: "nightMode.brightness", title: "Brightness", type: "wheel", limit: PWM_MAX_VALUE, kind: "Uint16", cmd: PacketType.NIGHT_MODE_BRIGHTNESS},
{key: "nightMode.startTime", title: "Start Time", type: "time", kind: "Uint32", cmd: PacketType.NIGHT_MODE_START},
{key: "nightMode.endTime", title: "End Time", type: "time", kind: "Uint32", cmd: PacketType.NIGHT_MODE_END},
{key: "nightMode.switchInterval", title: "Switch Interval", type: "time", kind: "Uint16", cmd: PacketType.NIGHT_MODE_INTERVAL},
Expand Down
3 changes: 3 additions & 0 deletions www/src/sys_constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PWM_RESOLUTION = 14;
export const PWM_MAX_VALUE = (2 ** PWM_RESOLUTION - 1);
export const TEMPERATURE_MAX_VALUE = PWM_MAX_VALUE * 2;

0 comments on commit a0754df

Please sign in to comment.