Skip to content

Commit

Permalink
Merge pull request #2 from arduino-libraries/orange-led-brightness
Browse files Browse the repository at this point in the history
Make brightness range consistent
  • Loading branch information
sebromero authored Jun 13, 2024
2 parents ebc4a53 + c97d7d1 commit de57a80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
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

0 comments on commit de57a80

Please sign in to comment.