Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use serial crsf battery sensor if detected #1968

Merged
merged 6 commits into from
Mar 2, 2023
2 changes: 1 addition & 1 deletion src/lib/AnalogVbat/devAnalogVbat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void reportVbat()

static int timeout()
{
if (GPIO_ANALOG_VBAT == UNDEF_PIN)
if (GPIO_ANALOG_VBAT == UNDEF_PIN || telemetry.GetCrsfBatterySensorDetected())
{
return DURATION_NEVER;
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/Telemetry/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ bool Telemetry::ShouldSendDeviceFrame()
return deviceFrame;
}

void Telemetry::CheckCrsfBatterySensorDetected()
{
if (CRSFinBuffer[CRSF_TELEMETRY_TYPE_INDEX] == CRSF_FRAMETYPE_BATTERY_SENSOR)
{
crsfBatterySensorDetected = true;
}
}

PAYLOAD_DATA(GPS, BATTERY_SENSOR, ATTITUDE, DEVICE_INFO, FLIGHT_MODE, VARIO, BARO_ALTITUDE);

Expand Down Expand Up @@ -171,6 +178,11 @@ bool Telemetry::RXhandleUARTin(uint8_t data)
if (data == crc)
{
AppendTelemetryPackage(CRSFinBuffer);

// Special case to check here and not in AppendTelemetryPackage(). devAnalogVbat sends
// direct to AppendTelemetryPackage() and we want to detect packets only received through serial.
CheckCrsfBatterySensorDetected();

receivedPackages++;
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/Telemetry/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Telemetry
bool ShouldCallEnterBind();
bool ShouldCallUpdateModelMatch();
bool ShouldSendDeviceFrame();
void CheckCrsfBatterySensorDetected();
bool GetCrsfBatterySensorDetected() { return crsfBatterySensorDetected; };
uint8_t GetUpdatedModelMatch() { return modelMatchId; }
bool GetNextPayload(uint8_t* nextPayloadSize, uint8_t **payloadData);
uint8_t UpdatedPayloadCount();
Expand All @@ -74,5 +76,6 @@ class Telemetry
bool callEnterBind;
bool callUpdateModelMatch;
bool sendDeviceFrame;
bool crsfBatterySensorDetected;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong, but its probably not the best practice to assume this bool will initialise to false...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ guarantees that variables are created with a specific initial value. And for booleans that is the false value 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats not what 5mins on stackoverflow taught me, but fair enough. I was always told that member vars should always be initialised (i.e. in the class constructor etc.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im happy to change it just for the readability. But there are multiple instances that should be updated and probably not as part of this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are right Wez, only globals are automatically set to defaults.

uint8_t modelMatchId;
};