forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P025_ADS1115_mod.ino
212 lines (176 loc) · 8.29 KB
/
_P025_ADS1115_mod.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifdef USES_P025
//#######################################################################################################
//#################################### Plugin 025: ADS1115 I2C 0x48) ###############################################
//#######################################################################################################
#define PLUGIN_025
#define PLUGIN_ID_025 25
#define PLUGIN_NAME_025 "Analog input - ADS1115"
#define PLUGIN_VALUENAME1_025 "Analog"
boolean Plugin_025_init = false;
uint16_t readRegister025(uint8_t i2cAddress, uint8_t reg) {
Wire.beginTransmission(i2cAddress);
Wire.write((0x00));
Wire.endTransmission();
if (Wire.requestFrom(i2cAddress, (uint8_t)2) != 2)
return 0x8000;
return ((Wire.read() << 8) | Wire.read());
}
boolean Plugin_025(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
// static byte portValue = 0;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_025;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_025);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_025));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte port = Settings.TaskDevicePort[event->TaskIndex];
if (port > 0) //map old port logic to new gain and mode settings
{
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = Settings.TaskDevicePluginConfig[event->TaskIndex][0] / 2;
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = 0x48 + ((port-1)/4);
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = ((port-1) & 3) | 4;
Settings.TaskDevicePort[event->TaskIndex] = 0;
}
#define ADS1115_I2C_OPTION 4
byte addr = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[ADS1115_I2C_OPTION] = { 0x48, 0x49, 0x4A, 0x4B };
addFormSelectorI2C(F("plugin_025_i2c"), ADS1115_I2C_OPTION, optionValues, addr);
addFormSubHeader(F("Input"));
#define ADS1115_PGA_OPTION 6
byte pga = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String pgaOptions[ADS1115_PGA_OPTION] = {
F("2/3x gain (FS=6.144V)"),
F("1x gain (FS=4.096V)"),
F("2x gain (FS=2.048V)"),
F("4x gain (FS=1.024V)"),
F("8x gain (FS=0.512V)"),
F("16x gain (FS=0.256V)")
};
addFormSelector(F("Gain"), F("plugin_025_gain"), ADS1115_PGA_OPTION, pgaOptions, NULL, pga);
addFormNote(F("2/3x gain +/- 6.144V 1 bit = 0.1875mV"));
addFormNote(F(" 1x gain +/- 4.096V 1 bit = 0.125mV"));
addFormNote(F(" 2x gain +/- 2.048V 1 bit = 0.0625mV"));
addFormNote(F(" 4x gain +/- 1.048V 1 bit = 0.03125mV"));
addFormNote(F(" 8x gain +/- 0.512V 1 bit = 0.015625mV"));
addFormNote(F(" 16x gain +/- 0.512V 1 bit = 0.0078125mV"));
#define ADS1115_MUX_OPTION 8
byte mux = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
String muxOptions[ADS1115_MUX_OPTION] = {
F("AIN0 - AIN1 (Differential)"),
F("AIN0 - AIN3 (Differential)"),
F("AIN1 - AIN3 (Differential)"),
F("AIN2 - AIN3 (Differential)"),
F("AIN0 - GND (Single-Ended)"),
F("AIN1 - GND (Single-Ended)"),
F("AIN2 - GND (Single-Ended)"),
F("AIN3 - GND (Single-Ended)"),
};
addFormSelector(F("Input Multiplexer"), F("plugin_025_mode"), ADS1115_MUX_OPTION, muxOptions, NULL, mux);
addFormSubHeader(F("Two Point Calibration"));
addFormCheckBox(F("Calibration Enabled"), F("plugin_025_cal"), Settings.TaskDevicePluginConfig[event->TaskIndex][3]);
addFormNumericBox(F("Point 1"), F("plugin_025_adc1"), Settings.TaskDevicePluginConfigLong[event->TaskIndex][0], -32768, 32767);
addHtml(F(" ≙ "));
addTextBox(F("plugin_025_out1"), String(Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0], 3), 10);
addFormNumericBox(F("Point 2"), F("plugin_025_adc2"), Settings.TaskDevicePluginConfigLong[event->TaskIndex][1], -32768, 32767);
addHtml(F(" ≙ "));
addTextBox(F("plugin_025_out2"), String(Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1], 3), 10);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_025_i2c"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_025_gain"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_025_mode"));
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = isFormItemChecked(F("plugin_025_cal"));
Settings.TaskDevicePluginConfigLong[event->TaskIndex][0] = getFormItemInt(F("plugin_025_adc1"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0] = getFormItemFloat(F("plugin_025_out1"));
Settings.TaskDevicePluginConfigLong[event->TaskIndex][1] = getFormItemInt(F("plugin_025_adc2"));
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1] = getFormItemFloat(F("plugin_025_out2"));
Plugin_025_init = false; // Force device setup next time
success = true;
break;
}
case PLUGIN_INIT:
{
Plugin_025_init = true;
success = true;
break;
}
case PLUGIN_READ:
{
//int value = 0;
//byte unit = (Settings.TaskDevicePort[event->TaskIndex] - 1) / 4;
//byte port = Settings.TaskDevicePort[event->TaskIndex] - (unit * 4);
//uint8_t address = 0x48 + unit;
uint8_t address = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
uint16_t config = (0x0003) | // Disable the comparator (default val)
(0x0000) | // Non-latching (default val)
(0x0000) | // Alert/Rdy active low (default val)
(0x0000) | // Traditional comparator (default val)
(0x0080) | // 128 samples per second (default)
(0x0100); // Single-shot mode (default)
uint16_t pga = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
config |= pga << 9;
uint16_t mux = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
config |= mux << 12;
config |= (0x8000); // Start a single conversion
Wire.beginTransmission(address);
Wire.write((uint8_t)(0x01));
Wire.write((uint8_t)(config >> 8));
Wire.write((uint8_t)(config & 0xFF));
Wire.endTransmission();
String log = F("ADS1115 : Analog value: ");
delay(8);
int16_t value = readRegister025((address), (0x00));
UserVar[event->BaseVarIndex] = (float)value;
log += value;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][3]) //Calibration?
{
int adc1 = Settings.TaskDevicePluginConfigLong[event->TaskIndex][0];
int adc2 = Settings.TaskDevicePluginConfigLong[event->TaskIndex][1];
float out1 = Settings.TaskDevicePluginConfigFloat[event->TaskIndex][0];
float out2 = Settings.TaskDevicePluginConfigFloat[event->TaskIndex][1];
if (adc1 != adc2)
{
float normalized = (float)(value - adc1) / (float)(adc2 - adc1);
UserVar[event->BaseVarIndex] = normalized * (out2 - out1) + out1;
log += F(" ");
log += UserVar[event->BaseVarIndex];
}
}
//TEST log += F(" @0x");
//TEST log += String(config, 16);
addLog(LOG_LEVEL_DEBUG,log);
success = true;
break;
}
}
return success;
}
#endif // USES_P025