Skip to content

Commit de57a80

Browse files
authored
Merge pull request #2 from arduino-libraries/orange-led-brightness
Make brightness range consistent
2 parents ebc4a53 + c97d7d1 commit de57a80

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

examples/OrangeLED/OrangeLED.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
void pulseLED(OrangeLED& led) {
1010
// Fade in
11-
for (uint8_t i = 0; i < 64; ++i) {
11+
for (uint8_t i = 0; i < 255; ++i) {
1212
led.setBrightness(i);
13-
delay(10);
13+
delay(4);
1414
}
1515

1616
// Fade out
17-
for (int8_t i = 63; i >= 0; --i) {
17+
for (int8_t i = 255; i >= 0; --i) {
1818
led.setBrightness(i);
19-
delay(10);
19+
delay(4);
2020
}
2121
}
2222

src/OrangeLED.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ OrangeLED::OrangeLED(uint8_t deviceAddress) : I2CDevice(deviceAddress) {}
77
uint8_t OrangeLED::brightness() {
88
// Read bits 0 - 5 from orange_led register
99
uint8_t data = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
10-
return data & 63;
10+
uint8_t brightness = data & 63;
11+
return map(brightness, 0, 63, 0, 255);
1112
}
1213

1314
void OrangeLED::setBrightness(uint8_t brightness) {
14-
if (brightness > 63) {
15+
if (brightness > 255) {
1516
return; // Invalid brightness value
1617
}
1718

19+
uint8_t mappedBrightness = map(brightness, 0, 255, 0, 63);
1820
uint8_t currentRegisterData = readFromRegister<uint8_t>(ORANGE_LED_REGISTER_INFO);
1921
// Overwrite bits 0 - 5 with the new value
20-
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | brightness);
22+
writeToRegister<uint8_t>(ORANGE_LED_REGISTER_INFO, (currentRegisterData & ~63) | mappedBrightness);
2123
}
2224

2325
bool OrangeLED::errorStatusEnabled() {

src/OrangeLED.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class OrangeLED : public I2CDevice {
2626

2727
/**
2828
* Gets the brightness of the orange LED.
29-
* @return The brightness of the orange LED. Range is 0 to 63.
29+
* @return The brightness of the orange LED. Range is 0 to 255.
3030
*/
3131
uint8_t brightness();
3232

0 commit comments

Comments
 (0)