Skip to content

Make brightness range consistent #2

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

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/OrangeLED/OrangeLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

void pulseLED(OrangeLED& led) {
// Fade in
for (uint8_t i = 0; i < 64; ++i) {
for (uint8_t i = 0; i < 255; ++i) {
led.setBrightness(i);
delay(10);
delay(4);
}

// Fade out
for (int8_t i = 63; i >= 0; --i) {
for (int8_t i = 255; i >= 0; --i) {
led.setBrightness(i);
delay(10);
delay(4);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/OrangeLED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ OrangeLED::OrangeLED(uint8_t deviceAddress) : I2CDevice(deviceAddress) {}
uint8_t OrangeLED::brightness() {
// Read bits 0 - 5 from orange_led register
uint8_t data = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
return data & 63;
uint8_t brightness = data & 63;
return map(brightness, 0, 63, 0, 255);
}

void OrangeLED::setBrightness(uint8_t brightness) {
if (brightness > 63) {
if (brightness > 255) {
return; // Invalid brightness value
}

uint8_t mappedBrightness = map(brightness, 0, 255, 0, 63);
uint8_t currentRegisterData = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
// Overwrite bits 0 - 5 with the new value
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | brightness);
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | mappedBrightness);
}

bool OrangeLED::errorStatusEnabled() {
Expand Down
2 changes: 1 addition & 1 deletion src/OrangeLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OrangeLED : public I2CDevice {

/**
* Gets the brightness of the orange LED.
* @return The brightness of the orange LED. Range is 0 to 63.
* @return The brightness of the orange LED. Range is 0 to 255.
*/
uint8_t brightness();

Expand Down
Loading