Skip to content

Commit b75662e

Browse files
committed
Add DAC mode
1 parent 18cd092 commit b75662e

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

PreferenceHandler.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ void PreferenceHandler::initGpios()
175175
Serial.printf("Preferences: init pin %i on mode %i\n", gpio.pin, gpio.mode);
176176
#endif
177177
attach(gpio);
178-
// Only write saved state if we have analog mode
178+
// Only write saved state if we have analog mode ...
179179
if (gpio.mode == -1) {
180180
ledcWrite(gpio.channel, gpio.state);
181+
// ... or DAC mode
182+
} else if (gpio.mode == -1) {
183+
dacWrite(gpio.pin, gpio.state);
181184
} else if (gpio.mode == 2) {
182185
// Default OUTPIN IO to low
183186
digitalWrite(gpio.pin, gpio.save ? gpio.state : 0);
@@ -223,8 +226,9 @@ bool PreferenceHandler::attach(GpioFlash& gpio) {
223226
return true;
224227
} else if (gpio.mode == -3) {
225228
adcAttachPin(gpio.pin);
229+
} else if (gpio.mode == -5) {
230+
dacWrite(gpio.pin,0);
226231
}
227-
228232
return false;
229233
}
230234

@@ -530,6 +534,8 @@ void PreferenceHandler::setGpioState(int pin, int value, bool persist) {
530534
digitalWrite(pin, newValue);
531535
} else if (gpio.mode==-1 && gpio.channel!=CHANNEL_NOT_ATTACHED) {
532536
ledcWrite(gpio.channel, newValue);
537+
} else if (gpio.mode==-5) {
538+
dacWrite(gpio.pin, newValue);
533539
}
534540
// Persist state in flash only if we have digital input in output or analog mode
535541
if (persist) {
@@ -581,8 +587,8 @@ int PreferenceHandler::getGpioState(int pin) {
581587
// every time we have a new value greater than TOUCH_VARIATION_ALLOWED, start a new timer. If the variation is steady during TOUCH_TIME_INTERVAL, we can say we have a touch event... fiou
582588
lastTouchDebounceTimes[gpio.pin] = millis();
583589
}
584-
return gpio.state;
585590
}
591+
return gpio.state;
586592
}
587593

588594
String PreferenceHandler::gpioToJson(GpioFlash& gpio) {

PreferenceHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef struct
5353
{
5454
uint8_t pin;
5555
char label[MAX_LABEL_TEXT_SIZE];
56-
int8_t mode; // 1 is INPUT, 2 is OUTPUT, 5 is INPUT_PULLUP, -1 is LEDCONTROL, -2 is I2C, -3 is analog read, -4 is Touch read,-100 is blocked (not available in the list of pins)
56+
int8_t mode; // 1 is INPUT, 2 is OUTPUT, 5 is INPUT_PULLUP, -1 is LEDCONTROL, -2 is I2C, -3 is ADC (analog read), -4 is Touch read, -5 is DAC (digital to analog), -100 is blocked (not available in the list of pins)
5757
uint32_t frequency;
5858
uint8_t resolution;
5959
int8_t channel;

ServerHandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ void ServerHandler::handleAvailableGpios(AsyncWebServerRequest *request) {
352352
JsonObject object = doc.createNestedObject();
353353
object["inputOnly"] = !GPIO_IS_VALID_OUTPUT_GPIO(i);
354354
object["adc"] = (i>=32); // Only GPIOs 32 to 39 are working correctly for adc mode when wifi is active.
355+
object["dac"] = (i==26 || i==25);
355356
if (i == 4 || i == 2 || i == 12 || i == 13 || i == 14 || i == 15 || i == 27 || i == 32 || i ==33) {
356357
object["touch"] = true;
357358
}

data/gpio.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const createPinOptions = (gpio) => {
2323
(((+mode === 2 || +mode === -1 || +mode === -2) && !availableGpio.inputOnly)
2424
|| (+mode === -3 && availableGpio.adc)
2525
|| (+mode === -4 && availableGpio.touch)
26+
|| (+mode === -5 && availableGpio.dac)
2627
|| +mode === 1 || +mode === 4 || +mode === 5 || +mode === 8 || +mode === 9)) {
2728
prev += `<option value=${availableGpioPin} ${+gpio.pin === availableGpioPin ? "selected" : ""}>${availableGpioPin}</option>`;
2829
}
@@ -70,6 +71,7 @@ const createEditGpioPanel = (gpio) => {
7071
<option ${+gpio.mode === -2 ? "selected" : ""} value=-2>I2C</option>
7172
<option ${+gpio.mode === -3 ? "selected" : ""} value=-3>ADC (analog read)</option>
7273
<option ${+gpio.mode === -4 ? "selected" : ""} value=-4>Touch</option>
74+
<option ${+gpio.mode === -5 ? "selected" : ""} value=-5>DAC</option>
7375
</select>
7476
</div>
7577
<div class="row">
@@ -116,7 +118,7 @@ const createEditGpioPanel = (gpio) => {
116118
<label class="slider" for="setGpioInvertState-${gpio.pin}"></label>
117119
</div>
118120
</div>
119-
<div class="row ${+gpio.mode !== -1 && +gpio.mode !== 2 ? "hidden" : ""}" id="setGpioSaveRow">
121+
<div class="row ${+gpio.mode !== -1 && +gpio.mode !== 2 && +gpio.mode !== -5 ? "hidden" : ""}" id="setGpioSaveRow">
120122
<div class="switch">
121123
<label for="setGpioSave-${gpio.pin}">Save state:</label>
122124
<input id="setGpioSave-${gpio.pin}" type="checkbox" class="switch-input" value="${gpio.save}">
@@ -143,19 +145,27 @@ const updateGpioOptions = (element) => {
143145
document.getElementById("setGpioInvertStateRow").classList.add("hidden");
144146
document.getElementById("i2c-options").classList.add("hidden");
145147
document.getElementById("adc-options").classList.add("hidden");
146-
// I2C mode
148+
// I2C mode
147149
} else if (option === -2) {
148150
document.getElementById("setGpioSaveRow").classList.add("hidden");
149151
document.getElementById("led-options").classList.add("hidden");
150152
document.getElementById("setGpioInvertStateRow").classList.add("hidden");
151153
document.getElementById("i2c-options").classList.remove("hidden");
152154
document.getElementById("adc-options").classList.add("hidden");
155+
// ADC mode
153156
} else if (option === -3) {
154157
document.getElementById("setGpioSaveRow").classList.add("hidden");
155158
document.getElementById("led-options").classList.add("hidden");
156159
document.getElementById("setGpioInvertStateRow").classList.add("hidden");
157160
document.getElementById("i2c-options").classList.add("hidden");
158161
document.getElementById("adc-options").classList.remove("hidden");
162+
// DAC mode
163+
} else if (option === -5) {
164+
document.getElementById("setGpioSaveRow").classList.remove("hidden");
165+
document.getElementById("led-options").classList.add("hidden");
166+
document.getElementById("setGpioInvertStateRow").classList.add("hidden");
167+
document.getElementById("i2c-options").classList.add("hidden");
168+
document.getElementById("adc-options").classList.add("hidden");
159169
} else {
160170
if (option === 2) {
161171
document.getElementById("setGpioSaveRow").classList.remove("hidden");
@@ -180,7 +190,8 @@ const switchGpioState = async (element) => {
180190
element.classList.remove(isOn ? "on" : "off");
181191
element.classList.add(isOn ? "off" : "on");
182192
element.innerText = (isOn ? "off" : "on");
183-
} else if (gpio.mode === -1) {
193+
// if PWM of DAC, send value directly
194+
} else if (gpio.mode === -1 || gpio.mode === -5) {
184195
await fetch(`${window.location.href}gpio/value?pin=${gpio.pin}&value=${element.value}`);
185196
}
186197
} catch (err) {

data/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ <h2 id='blocker-title'>Loading</h2>
238238
let child = document.createElement("div");
239239
const digitalState = (gpio.state && !gpio.invert) || (!gpio.state && gpio.invert);
240240
let additionnalButton = `<a onclick="switchGpioState(this)" id="stateGpio-${gpio.pin}" class="btn ${digitalState ? "on" : "off"} ${+gpio.mode !== 2 ? "input-mode" : ""}">${digitalState ? "on" : "off"}</a>`;
241-
if (+gpio.mode === -1) {
242-
// For PWM
241+
if (+gpio.mode === -1 || +gpio.mode === -5) {
242+
// For PWM or DAC
243243
additionnalButton = `<input type="number" onchange="switchGpioState(this)" id="stateGpio-${gpio.pin}" value="${gpio.state}">`;
244244
} else if (+gpio.mode === -2) {
245245
// For I2C mode

0 commit comments

Comments
 (0)