forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P152_MCP42010.ino
143 lines (113 loc) · 4.46 KB
/
_P152_MCP42010.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//#######################################################################################################
//#################################### Plugin 152: MCP42010 #############################################
//################################## I use GPIO 12 / 14 / 15 ############################################
//#######################################################################################################
// written by antibill
// Usage:
// (1): Set value to potentiometer (http://xx.xx.xx.xx/control?cmd=digipot,0,255)
// (2): Set value to potentiometer (http://xx.xx.xx.xx/control?cmd=digipot,1,0)
#include <MCP42010.h>
// https://github.com/mensink/arduino-lib-MCP42010
static float Plugin_152_PotDest[2] = {0,0};
#define PLUGIN_152
#define PLUGIN_ID_152 152
#define PLUGIN_NAME_152 "MCP42010"
#define PLUGIN_VALUENAME1_152 "DigiPot0"
#define PLUGIN_VALUENAME2_152 "DigiPot1"
int Plugin_152_pin[3] = {-1,-1,-1};
boolean Plugin_152(byte 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_DUMMY; // SPI pins for ESP8266 are CS=15, CLK=14, MOSI=13
Device[deviceCount].Ports = 0;
Device[deviceCount].VType = SENSOR_TYPE_DUAL;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_152);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_152));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_152));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
// char tmpString[128];
addHtml(F("<TR><TD>GPIO:<TD>"));
addHtml(F("<TR><TD>1st GPIO (CS):<TD>"));
addPinSelect(false, "taskdevicepin1", Plugin_152_pin[0] = Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
addHtml(F("<TR><TD>2nd GPIO (CLK):<TD>"));
addPinSelect(false, "taskdevicepin2", Plugin_152_pin[1] = Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addHtml(F("<TR><TD>3rd GPIO (MOSI):<TD>"));
addPinSelect(false, "taskdevicepin3", Plugin_152_pin[2] = Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin2 = WebServer.arg("taskdevicepin1");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin2.toInt();
String plugin3 = WebServer.arg("taskdevicepin2");
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin3.toInt();
String plugin4 = WebServer.arg("taskdevicepin3");
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = plugin4.toInt();
success = true;
break;
}
case PLUGIN_INIT:
{
int pCS = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int pCLK = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
int pMOSI = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
Plugin_152_pin[0] = pCS;
Plugin_152_pin[1] = pCLK;
Plugin_152_pin[2] = pMOSI;
success = true;
break;
}
case PLUGIN_WRITE:
{
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("digipot")))
{
int pot;
int value;
pot = event->Par1;
value = event->Par2;
MCP42010 digipot(Plugin_152_pin[0], Plugin_152_pin[1], Plugin_152_pin[2]);
Plugin_152_PotDest[pot] = value;
digipot.setPot(pot,value);
success = true;
}
break;
}
case PLUGIN_READ:
{
UserVar[event->BaseVarIndex + 0]= Plugin_152_PotDest[0] ;
UserVar[event->BaseVarIndex + 1] = Plugin_152_PotDest[1] ;
success = true;
break;
}
}
return success;
}