PCB lives now in its own git repo https://git.la10cy.net/DeltaLima/CanGrow-12V-PCB
112 lines
4 KiB
C
112 lines
4 KiB
C
/*
|
|
*
|
|
* include/Sensor/01_ADC_builtin.h - sensor header for builtin ADC
|
|
*
|
|
*
|
|
* "Driver" for the internal ADC of the ESP8266 and ESP32
|
|
*
|
|
* Bit dirty hacky workaround to support both boards ADC
|
|
*
|
|
* ESP8266 only has one ADC onboard. For "Multiplexing" I have added
|
|
* the to "add a GPIO" to it. It simply turns on the given GPIO for
|
|
* 100ms, and then turns it off.
|
|
* This is kinda a poor (wo)mans multiplexer. Control the supply voltage
|
|
* of your analog sensor with the GPIO, put a diode to the AOUT and enjoy.
|
|
*
|
|
* You can theoretically use all available pins as "multiplexer", thats why
|
|
* the Sensor_01_ADC[] array is as large as GPIOindex_length
|
|
* ************
|
|
*
|
|
* ESP32 has a bunch of ADCs onboard. So it is not needed to go this hacky way,
|
|
* we can just use all the nice ADCs available.
|
|
*
|
|
* in GPIOindex.note we get the info if the GPIO is an ADC or not
|
|
* INT_ADC and INPUT_ONLY tells us this.
|
|
* To save memory, I build an own "index" for the available
|
|
* ADCs. Thats wahat Sensor_01_ADC_ArrId() is for.
|
|
*
|
|
* It returns which Index / slot in the array the given GPIO ID
|
|
* from GPIOindex has.
|
|
*/
|
|
|
|
#define SENSOR_01_NAME "ADC builtin"
|
|
|
|
/* Create main data array specifying max amount of readings */
|
|
#ifdef ESP8266
|
|
const byte SENSOR_01_MAX = GPIOindex_length + 1;
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
/* indexing function for our ADC GPIO pins we could theoretically all use */
|
|
byte Sensor_01_ADC_ArrId(const byte GPIOid) {
|
|
const static char LogLoc[] PROGMEM = "[Sensor:01_ADC:Slot]";
|
|
byte count = 0;
|
|
//Log.verbose(F("%s GPIO %d (%d) START" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid);
|
|
for(byte i = 1; i <= GPIOindex_length; i++) {
|
|
//Log.verbose(F("%s GPIO %d (%d) NOTE %d - %S" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid, GPIOindex[i].note, GPIO_Index_note_descr[GPIOindex[i].note]);
|
|
if((GPIOindex[i].note == INPUT_ONLY) || (GPIOindex[i].note == INT_ADC)) {
|
|
if(i == GPIOid) {
|
|
//Log.verbose(F("%s ??? GPIO %d (%d) i %d" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid, i);
|
|
return count;
|
|
} else {
|
|
count++;
|
|
//Log.verbose(F("%s GPIO %d (%d) i %d count" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid, i, count);
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* this dumb, but yeah - i counted the avail ADC manually (INT_ADC or INPUT_ONLY) */
|
|
const byte SENSOR_01_MAX = 6;
|
|
#endif
|
|
|
|
int Sensor_01_ADC[SENSOR_01_MAX];
|
|
|
|
void Sensor_01_ADC_Update(const byte GPIOid) {
|
|
const static char LogLoc[] PROGMEM = "[Sensor:01_ADC:Update]";
|
|
#ifdef ESP8266
|
|
if(GPIOid > 0) {
|
|
//digitalWrite(GPIOindex[GPIOid].gpio, HIGH);
|
|
//Log.notice(F("%s GPIO %d (%d) delay ON" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid);
|
|
digitalWrite(GPIOindex[GPIOid].gpio, HIGH);
|
|
delay(50);
|
|
Sensor_01_ADC[GPIOid] = analogRead(A0);
|
|
digitalWrite(GPIOindex[GPIOid].gpio, LOW);
|
|
//Log.notice(F("%s GPIO %d (%d) delay OFF" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid);
|
|
} else {
|
|
//Log.notice(F("%s GPIO %d (%d) READ" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid);
|
|
Sensor_01_ADC[GPIOid] = analogRead(A0);
|
|
}
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
byte slot = Sensor_01_ADC_ArrId(GPIOid);
|
|
//Log.notice(F("%s GPIO %d (%d) READ - slot %d" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid, slot);
|
|
Sensor_01_ADC[slot] = analogRead(GPIOindex[GPIOid].gpio);
|
|
//Log.notice(F("%s GPIO %d (%d) READ - slot %d Val %d" CR), LogLoc, GPIOindex[GPIOid].gpio, GPIOid, slot, Sensor_01_ADC[slot]);
|
|
#endif
|
|
|
|
}
|
|
|
|
bool Sensor_01_ADC_Init(const byte GPIOid) {
|
|
/* Sensor Init function
|
|
*
|
|
* returns true (1) when Init was successful
|
|
* returns false (0) if not.
|
|
*/
|
|
const static char LogLoc[] PROGMEM = "[Sensor:01_ADC:Init]";
|
|
|
|
#ifdef ESP8266
|
|
//Log.notice(F("%s setting GPIO ID %d (%d) as OUTPUT for internal ADC" CR), LogLoc, GPIOid, GPIOindex[GPIOid].gpio);
|
|
pinMode(GPIOindex[GPIOid].gpio, OUTPUT);
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
//Log.notice(F("%s GPIO ID %d (%d) as INPUT for internal ADC slot %d" CR), LogLoc, GPIOid, GPIOindex[GPIOid].gpio, Sensor_01_ADC_ArrId(GPIOid));
|
|
//pinMode(GPIOindex[GPIOid].gpio, INPUT);
|
|
#endif
|
|
|
|
Sensor_01_ADC_Update(GPIOid);
|
|
return true;
|
|
}
|