-
Notifications
You must be signed in to change notification settings - Fork 174
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
Added individual led brightness option to led strip #73
base: master
Are you sure you want to change the base?
Changes from 5 commits
eb5a03d
9066f46
903b118
1b58fec
35b243c
0dc3695
96fc125
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,8 @@ public enum Direction { | |
private Mode mLedMode; | ||
|
||
// RGB LED strip settings that have sensible defaults. | ||
private int mLedBrightness = MAX_BRIGHTNESS >> 1; // default to half | ||
private int mLedBrightnessGlobal = MAX_BRIGHTNESS >> 1; // Default to half | ||
private int mLedBrightness[]; | ||
|
||
// Direction of the led strip; | ||
private Direction mDirection; | ||
|
@@ -162,13 +163,33 @@ public void setBrightness(int ledBrightness) { | |
throw new IllegalArgumentException("Brightness needs to be between 0 and " | ||
+ MAX_BRIGHTNESS); | ||
} | ||
mLedBrightness = ledBrightness; | ||
mLedBrightnessGlobal = ledBrightness; | ||
mLedBrightness = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you make it null? (issues later that you have to null check) Can you just make an empty array / clear it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I make it null to invalidate it, so when the user calls write later, the global value is used instead. This is to allow switching from individual brightness to global brightness. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could fill the array with the global brightness instead of relying on null to say that there is no individual brightness (Connascence of Values) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but the original request for change suggested by jdkoren was to keep them separated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not my repo, so it was just a suggestion 👍 I'd go with the mod's advice ;-) |
||
} | ||
|
||
/** | ||
* Get the current brightness level | ||
* Sets the brightness for all LEDs in the strip. | ||
* @param ledBrightness The brightness of the LED strip, between 0 and {@link #MAX_BRIGHTNESS}. | ||
*/ | ||
public void setIndividualBrightness(int[] ledBrightness) { | ||
mLedBrightness = new int[ledBrightness.length]; | ||
for (int i=0; i<ledBrightness.length; i++) { | ||
if (ledBrightness[i] < 0 || ledBrightness[i] > MAX_BRIGHTNESS) { | ||
throw new IllegalArgumentException("Brightness needs to be between 0 and " | ||
+ MAX_BRIGHTNESS); | ||
} | ||
mLedBrightness[i] = ledBrightness[i]; | ||
} | ||
} | ||
|
||
/** | ||
* Get the current brightness | ||
*/ | ||
public int getBrightness() { | ||
return mLedBrightnessGlobal; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like that we can only provide one value here. To me it makes better sense to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we can return an array of int, then we can be consistent about returning the array of brightness as it is or null if only global brightness is set. I do think that keeping them in sync makes sense as long as the logic is clear. Also, I agree. I do not expect a developer to mix them on the same project. |
||
} | ||
|
||
public int[] getIndividualBrightness() { | ||
return mLedBrightness; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can now return null, you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, thanks! |
||
} | ||
|
||
|
@@ -212,9 +233,12 @@ public void write(int[] colors) throws IOException { | |
pos += APA_START_FRAME_PACKET_LENGTH; | ||
|
||
// Compute the packets to send. | ||
byte brightness = (byte) (0xE0 | mLedBrightness); // Less brightness possible | ||
final Direction currentDirection = mDirection; // Avoids reading changes of mDirection during loop | ||
byte brightness = (byte) (0xE0 | mLedBrightnessGlobal); // Default initialization | ||
for (int i = 0; i < colors.length; i++) { | ||
if (mLedBrightness != null) { | ||
brightness = (byte) (0xE0 | mLedBrightness[i]); // Less brightness possible | ||
} | ||
int di = currentDirection == Direction.NORMAL ? i : colors.length - i - 1; | ||
copyApaColorData(brightness, colors[di], mLedMode, mLedData, pos); | ||
pos += APA_COLOR_PACKET_LENGTH; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
best practice dictates to put the array initialiser on the field type, so you can easily see the difference between a single int and an int array.
private int[] mLedBrightness;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, thanks!