Enable single PIN mode for SR04 sensor

Enable single PIN mode for SR04 sensor (#8189)
This commit is contained in:
Theo Arends 2020-04-15 16:22:20 +02:00
parent 97abf68612
commit 3a4cf26aba

View File

@ -32,22 +32,18 @@
#define XSNS_22 22 #define XSNS_22 22
uint8_t sr04_type = 1; uint8_t sr04_type = 1;
int sr04_echo_pin = 0;
int sr04_trig_pin = 0;
real64_t distance; real64_t distance;
NewPing* sonar = nullptr; NewPing* sonar = nullptr;
TasmotaSerial* sonar_serial = nullptr; TasmotaSerial* sonar_serial = nullptr;
uint8_t Sr04TModeDetect(void) uint8_t Sr04TModeDetect(void)
{ {
sr04_type = 0; sr04_type = 0;
if (pin[GPIO_SR04_ECHO]>=99) return sr04_type; if (99 == pin[GPIO_SR04_ECHO]) { return sr04_type; }
sr04_echo_pin = pin[GPIO_SR04_ECHO]; int sr04_echo_pin = pin[GPIO_SR04_ECHO];
sr04_trig_pin = (pin[GPIO_SR04_TRIG] < 99) ? pin[GPIO_SR04_TRIG] : -1; int sr04_trig_pin = (pin[GPIO_SR04_TRIG] < 99) ? pin[GPIO_SR04_TRIG] : -1;
sonar_serial = new TasmotaSerial(sr04_echo_pin, sr04_trig_pin, 1); sonar_serial = new TasmotaSerial(sr04_echo_pin, sr04_trig_pin, 1);
if (sonar_serial->begin(9600,1)) { if (sonar_serial->begin(9600,1)) {
@ -65,6 +61,9 @@ uint8_t Sr04TModeDetect(void)
if (sr04_type < 2) { if (sr04_type < 2) {
delete sonar_serial; delete sonar_serial;
sonar_serial = nullptr; sonar_serial = nullptr;
if (-1 == sr04_trig_pin) {
sr04_trig_pin = pin[GPIO_SR04_ECHO]; // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_ECHO only
}
sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, 300); sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, 300);
} else { } else {
if (sonar_serial->hardwareSerial()) { if (sonar_serial->hardwareSerial()) {