/
_P152_DAC.ino
125 lines (101 loc) · 3.35 KB
/
_P152_DAC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifdef USES_P152
// #######################################################################################################
// #################################### Plugin 152: ESP32 DAC ############################################
// ### DAC (Digital Analog converter) for ESP32 and ESP32-S2 ###
// #######################################################################################################
/** Changelog:
* 2023-04-10 tonhuisman: Minor corrections
* 2023-04-09 tonhuisman: Adopt to latest mega branch changes as the plugin is last changed some years ago
* 2023-04-09 tonhuisman: Migrate plugin from ESPEasyPluginPlayground to mega branch
*/
#include "_Plugin_Helper.h"
#define PLUGIN_152
#define PLUGIN_ID_152 152
#define PLUGIN_NAME_152 "Output - ESP32 DAC"
#define PLUGIN_VALUENAME1_152 "Output"
#define P152_DAC_VALUE UserVar[event->BaseVarIndex]
#define P152_SET_DAC_VALUE(x) UserVar.setFloat(event->TaskIndex, 0, x)
#if !(defined(ESP32_CLASSIC) || defined(ESP32S2))
# error P152 ESP32 DAC not supported on this CPU type!
#endif // if !(defined(ESP32_CLASSIC) || defined(ESP32S2))
boolean Plugin_152(uint8_t function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_152;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_SINGLE;
Device[deviceCount].Custom = true;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_152);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_152));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
addRowLabel(F("Analog Output"));
addDAC_PinSelect(F("taskdevicepin1"), CONFIG_PIN1);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
success = true;
break;
}
case PLUGIN_INIT:
{
int dac;
success = getDAC_gpio_info(CONFIG_PIN1, dac);
break;
}
case PLUGIN_READ:
{
success = true;
break;
}
case PLUGIN_WRITE:
{
const String command = parseString(string, 1);
// Command: dac,<dac>,<value> : <dac>: 1 or 2, <value>: 0..255
if (equals(command, F("dac"))) {
int dac;
if (getDAC_gpio_info(CONFIG_PIN1, dac) && (dac == event->Par1)) {
int value = min(255, max(0, event->Par2)); // Limit value
P152_SET_DAC_VALUE(value);
dacWrite(CONFIG_PIN1, value); // Set output value
addLog(LOG_LEVEL_INFO,
formatGpioName_DAC(CONFIG_PIN1) +
concat(F(" : Output: "), value));
success = true;
}
}
break;
}
case PLUGIN_WEBFORM_SHOW_CONFIG:
{
int dac;
if (getDAC_gpio_info(CONFIG_PIN1, dac)) {
string += formatGpioName_DAC(CONFIG_PIN1);
success = true;
}
break;
}
}
return success;
}
#endif // USES_P152