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

Refactor display settings handling in BLE callback #1594

Closed
NeilHanlon opened this issue Feb 23, 2023 · 6 comments · Fixed by #1616
Closed

Refactor display settings handling in BLE callback #1594

NeilHanlon opened this issue Feb 23, 2023 · 6 comments · Fixed by #1616

Comments

@NeilHanlon
Copy link
Contributor

NeilHanlon commented Feb 23, 2023

I wrote the following in #1592 , but it's not great.

// @TODO refactor to make this more usable
if (uuid_value == SettingsOptions::OLEDInversion) {
OLED::setInverseDisplay(getSettingValue(SettingsOptions::OLEDInversion));
}
if (uuid_value == SettingsOptions::OLEDBrightness){
OLED::setBrightness(getSettingValue(SettingsOptions::OLEDBrightness));
}
if (uuid_value == SettingsOptions::OrientationMode){
OLED::setRotation(getSettingValue(SettingsOptions::OrientationMode) & 1);
}

There is probably a better way to handle this than three if statements in a row :)

Open to suggestions

@GPSBabelDeveloper
Copy link

Here's three different ways to make that a little less grubby. I prefer the style of the first one, all other things being equal.

switch (uuid_value) {
case SettingsOptions::OLEDInversion:
OLED::setInverseDisplay(getSettingValue(SettingsOptions::OLEDInversion));
break;
case SettingsOptions::OLEDBrightness:
OLED::setBrightness(getSettingValue(SettingsOptions::OLEDBrightness));
break;
case SettingsOptions::OrientationMode:
OLED::setRotation(getSettingValue(SettingsOptions::OrientationMode) & 1);
break;
default:
BT_WARN("Unhandled uuid_value in %s. Got 0x%x", PRETTY_FUNCTION, uuid_value);
break;
}

Or keep it like this, but don't force common subexpression elimination to determine the mutual exclusivity of the paths:

if (uuid_value == SettingsOptions::OLEDInversion) {
OLED::setInverseDisplay(getSettingValue(SettingsOptions::OLEDInversion));
} else if (uuid_value == SettingsOptions::OLEDBrightness) {
OLED::setBrightness(getSettingValue(SettingsOptions::OLEDBrightness));
} else if (uuid_value == SettingsOptions::OrientationMode) {
OLED::setRotation(getSettingValue(SettingsOptions::OrientationMode) & 1);
}

You could possibly shave a few clock cycles off making PMFs to the three methods, masking the bottom two bits from uuid_value and jumping through a function dispatch table with the constant arguments andtesting explictly for the last one and doing something similar after a mask. A self-respecting optimizer MAY do this for you if it can determine these are pure functions (i.e. they're marked const and can be proven to not modify larger scope. If you need clock cycles this badly, you're probably living wrong, but I"m all about giving choices.

Consider marking the functions const:

static void setBrightness(uint8_t contrast) const;
static void setInverseDisplay(bool inverted) const ;
Same for others

... if you can. If you're not modifying any state of the OLED class (e.g. no globals, no honking on the invisible this*) that even further clarifies the code and exposes additional optimizationopportunities.

@discip
Copy link
Collaborator

discip commented Mar 10, 2023

@NeilHanlon
So... are you planning to implement any of the proposed solutions? Or do you expect this to be done for you?
I could try to, if you want.

discip added a commit that referenced this issue Mar 11, 2023
Tried to implement #1594
@River-Mochi
Copy link
Contributor

@NeilHanlon of the new options for the code that GPSBabel suggested to make it better, which one are you going to Submit? it should be submitted asap though so that it can go into the 2.21 release yes?

@NeilHanlon
Copy link
Contributor Author

Yep, any of these would be fine for implementing. Was planning to get back to this soon, but thank you for taking it @discip !

@discip
Copy link
Collaborator

discip commented Mar 14, 2023

No, this is not implemented yet.
This will automatically close once merged.
It doesn't have to be done manually.

@River-Mochi
Copy link
Contributor

thanks @discip

discip added a commit that referenced this issue Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants