Skip to content

Commit

Permalink
Redesign distance sensors HRXL and DYP
Browse files Browse the repository at this point in the history
Redesign distance sensors HRXL and DYP to use cm instead of mm (#17021)
  • Loading branch information
arendst committed Nov 8, 2022
1 parent c14e508 commit 05b43fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Support for BP1658CJ RGBCW led bulbs like Orein OS0100411267 by Cossid (#17011)

### Breaking Changed
- Redesign distance sensors HRXL and DYP to use cm instead of mm (#17021)

### Changed
- Default Flash Mode changed from ``DOUT`` to ``DIO`` for ESP8266/ESP8285
Expand Down
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
- ESP32 Support for DMX ArtNet Led matrix animations [#16984](https://github.com/arendst/Tasmota/issues/16984)

### Breaking Changed
- Redesign distance sensors HRXL and DYP to use cm instead of mm [#17021](https://github.com/arendst/Tasmota/issues/17021)

### Changed
- ESP32 Framework (Core) from v2.0.5 to v2.0.5.2
Expand Down
101 changes: 41 additions & 60 deletions tasmota/tasmota_xsns_sensor/xsns_64_hrxl.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,54 @@
* Hardware Serial will be selected if GPIO1 = [HRXL Rx]
\*********************************************************************************************/

#define XSNS_64 64

#include <TasmotaSerial.h>
#define XSNS_64 64

#define HRXL_READ_TIMEOUT 400 // us; enough for 6 bytes@9600bps

#include <TasmotaSerial.h>
TasmotaSerial *HRXLSerial = nullptr;

uint32_t hrxl_distance_mm = 0; // distance, mm
bool hrxl_found = false;
uint32_t hrxl_distance_cm = 0; // distance, cm

/*********************************************************************************************/

void HRXLInit(void)
{
hrxl_found = false;
if (PinUsed(GPIO_HRXL_RX))
{
HRXLSerial = new TasmotaSerial(Pin(GPIO_HRXL_RX), -1, 1);
if (HRXLSerial->begin(9600))
{
if (HRXLSerial->hardwareSerial())
ClaimSerial();
hrxl_found = true;
HRXLSerial->setTimeout(HRXL_READ_TIMEOUT);
}
void HRXLInit(void) {
if (PinUsed(GPIO_HRXL_RX)) {
HRXLSerial = new TasmotaSerial(Pin(GPIO_HRXL_RX), -1, 1);
if (HRXLSerial->begin(9600)) {
if (HRXLSerial->hardwareSerial()) {
ClaimSerial();
}
HRXLSerial->setTimeout(HRXL_READ_TIMEOUT);
}
}
}

void HRXLEverySecond(void)
{
if (!hrxl_found)
return;

int num_read=0;
int sum=0;
while (HRXLSerial->available()>5)
{
if (HRXLSerial->read() != 'R')
continue;

int d = HRXLSerial->parseInt();
if (d >= 30 && d<=5000)
{
sum += d;
num_read++;
}
void HRXLEverySecond(void) {
int num_read = 0;
int sum = 0;
while (HRXLSerial->available() > 5) {
if (HRXLSerial->read() != 'R') {
continue;
}
if (num_read>1)
hrxl_distance_mm = int(sum / num_read);

int d = HRXLSerial->parseInt();
if (d >= 30 && d <= 5000) {
sum += d;
num_read++;
}
}
if (num_read > 1) {
hrxl_distance_cm = int(sum / num_read) / 10; // cm
}
}


void HRXLShow(bool json)
{
void HRXLShow(bool json) {
char types[5] = "HRXL";
if (json)
{
ResponseAppend_P(PSTR(",\"%s\":{\"" D_DISTANCE "\":%d}"), types, hrxl_distance_mm);
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%d}"), types, hrxl_distance_cm);
#ifdef USE_WEBSERVER
}
else
{
WSContentSend_PD(HTTP_SNS_RANGE, types, hrxl_distance_mm);
} else {
WSContentSend_PD(HTTP_SNS_DISTANCE_CM, types, hrxl_distance_cm);
#endif // USE_WEBSERVER
}
}
Expand All @@ -98,15 +81,12 @@ void HRXLShow(bool json)
* Interface
\*********************************************************************************************/

bool Xsns64(uint8_t function)
{
if (!PinUsed(GPIO_HRXL_RX)) { return false; }

switch (function)
{
case FUNC_INIT:
HRXLInit();
break;
bool Xsns64(uint8_t function) {
if (FUNC_INIT == function) {
HRXLInit();
}
else if (HRXLSerial) {
switch (function) {
case FUNC_EVERY_SECOND:
HRXLEverySecond();
break;
Expand All @@ -118,8 +98,9 @@ bool Xsns64(uint8_t function)
HRXLShow(0);
break;
#endif // USE_WEBSERVER
}
return false;
}
}
return false;
}

#endif // USE_HRXL
50 changes: 21 additions & 29 deletions tasmota/tasmota_xsns_sensor/xsns_76_dyp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#ifdef USE_DYP

/*********************************************************************************************\
* DYP ME007 ultrasonic distance sensor (300...4000mm), serial version
*
Expand All @@ -28,13 +27,11 @@
* 300...4000 for measured distance
* 4999 for distance above 4000mm
* 5999 for not connected sensor
*
\*********************************************************************************************/

#define XSNS_76 76
#define XSNS_76 76

#include <TasmotaSerial.h>

TasmotaSerial *DYPSerial = nullptr;

#define DYP_CRCERROR -1
Expand All @@ -44,27 +41,22 @@ TasmotaSerial *DYPSerial = nullptr;
#define DYP_ABOVEMAX 4999
#define DYP_NOSENSOR 5999

uint16_t DYPDistance = 0; // distance in milimeters
bool DYPSensor = false; // sensor available
uint16_t DYPDistance = 0; // distance in centimeters

/*********************************************************************************************/

void DYPInit(void) {
DYPSensor = false;
if (PinUsed(GPIO_DYP_RX)) {
DYPSerial = new TasmotaSerial(Pin(GPIO_DYP_RX), -1, 1);
if (DYPSerial->begin(9600)) {
if (DYPSerial->hardwareSerial()) {
ClaimSerial();
}
DYPSensor = true;
}
}
}

void DYPEverySecond(void) {
if (!DYPSensor) { return; }

// check for serial data
if (DYPSerial->available() < 6) {
DYPDistance = DYP_NOSENSOR;
Expand Down Expand Up @@ -95,7 +87,7 @@ void DYPEverySecond(void) {
if (data > DYP_MAX) {
data = DYP_ABOVEMAX;
}
DYPDistance = data;
DYPDistance = data / 10; // cm
} else {
DYPDistance = DYP_CRCERROR;
}
Expand All @@ -104,12 +96,12 @@ void DYPEverySecond(void) {
}

void DYPShow(bool json) {
char types[5] = "DYP";
char types[4] = "DYP";
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"" D_DISTANCE "\":%d}"), types, DYPDistance);
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%d}"), types, DYPDistance);
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_RANGE, types, DYPDistance);
WSContentSend_PD(HTTP_SNS_DISTANCE_CM, types, DYPDistance);
#endif // USE_WEBSERVER
}
}
Expand All @@ -119,23 +111,23 @@ void DYPShow(bool json) {
\*********************************************************************************************/

bool Xsns76(uint8_t function) {
if (!PinUsed(GPIO_DYP_RX)) { return false; }

switch (function) {
case FUNC_INIT:
DYPInit();
break;
case FUNC_EVERY_SECOND:
DYPEverySecond();
break;
case FUNC_JSON_APPEND:
DYPShow(1);
break;
if (FUNC_INIT == function) {
DYPInit();
}
else if (DYPSerial) {
switch (function) {
case FUNC_EVERY_SECOND:
DYPEverySecond();
break;
case FUNC_JSON_APPEND:
DYPShow(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
DYPShow(0);
break;
case FUNC_WEB_SENSOR:
DYPShow(0);
break;
#endif // USE_WEBSERVER
}
}
return false;
}
Expand Down

0 comments on commit 05b43fb

Please sign in to comment.