Skip to content

Commit 320504f

Browse files
committed
Bringing Arduino Lib up to SF specs
Added boundary checks for parameters to functions Tested out examples Bumped version number on library.properties
1 parent c255aba commit 320504f

6 files changed

Lines changed: 70 additions & 2 deletions

File tree

examples/Example04_SetBrightness/Example04_SetBrightness.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ void setup() {
3636
}
3737

3838
Serial.println("Qwiic LED Stick ready!");
39+
40+
//Start by resetting the state of the LEDs
41+
LEDStick.LEDOff();
3942

4043
//Color the LEDStick according to the 3 arrays
4144
LEDStick.setLEDColor(redArray, greenArray, blueArray, 10);
45+
delay(10);
4246
}
4347

4448
void loop() {

examples/Example05_BinaryCounter/Example05_BinaryCounter.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ void setup() {
2929
}
3030

3131
Serial.println("Qwiic LED Stick ready!");
32+
33+
//Start by resetting the state of the LEDs
34+
LEDStick.LEDOff();
3235
}
3336

3437
void loop() {

examples/Example06_ColorGradient/Example06_ColorGradient.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void setup() {
2828
}
2929

3030
Serial.println("Qwiic LED Stick ready!");
31+
32+
//Start by resetting the state of the LEDs
33+
LEDStick.LEDOff();
3134

3235
//Set colors for the gradient
3336
//These are for the first color
@@ -62,6 +65,6 @@ void colorGradient(byte r1, byte g1, byte b1, byte r2, byte g2, byte b2, byte LE
6265
byte bValue = b1 + bSlope * i;
6366
//Set the (i+1)th pixel to the calculated color
6467
//the first LED corresponds to the 0th point on the line
65-
LEDStick.setLEDColor(i + 1, rValue, gValue, bValue);
68+
LEDStick.setLEDColor(i, rValue, gValue, bValue);
6669
}
6770
}

examples/Example09_ChangeLength/Example09_ChangeLength.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void setup() {
3333

3434
//First, turn all LEDs off
3535
LEDStick.LEDOff();
36+
delay(500); //Wait for settings to be updated
37+
3638
//Change LED length to 5
3739
//This will allow you to write to a maximum of 5 LEDs
3840
LEDStick.changeLength(5);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun Qwiic LED Stick Library
2-
version=1.0.3
2+
version=1.0.4
33
author=SparkFun Electronics
44
maintainer=SparkFun Electronics
55
sentence=Library for the SparkFun Qwiic LED Stick

src/Qwiic_LED_Stick.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ boolean LED::isConnected() {
4545
//each color must be a value between 0-255
4646
//LEDS indexed starting at 1
4747
boolean LED::setLEDColor(uint8_t number, uint8_t red, uint8_t green, uint8_t blue) {
48+
// First, boundary check
49+
if (red > 255)
50+
red = 255;
51+
if (red < 0)
52+
red = 0;
53+
if (green > 255)
54+
green = 255;
55+
if (green < 0)
56+
green = 0;
57+
if (blue > 255)
58+
blue = 255;
59+
if (blue < 0)
60+
blue = 0;
61+
4862
_i2cPort->beginTransmission(_LEDAddress); //communicate using address
4963
_i2cPort->write(COMMAND_WRITE_SINGLE_LED_COLOR); //command to change single LED's color
5064
_i2cPort->write(number); //choose which LED
@@ -61,6 +75,20 @@ boolean LED::setLEDColor(uint8_t number, uint8_t red, uint8_t green, uint8_t blu
6175
//Change the color of all LEDs
6276
//each color must be a value between 0-255
6377
boolean LED::setLEDColor(uint8_t red, uint8_t green, uint8_t blue) {
78+
// First, boundary check
79+
if (red > 255)
80+
red = 255;
81+
if (red < 0)
82+
red = 0;
83+
if (green > 255)
84+
green = 255;
85+
if (green < 0)
86+
green = 0;
87+
if (blue > 255)
88+
blue = 255;
89+
if (blue < 0)
90+
blue = 0;
91+
6492
_i2cPort->beginTransmission(_LEDAddress); //communicate using address
6593
_i2cPort->write(COMMAND_WRITE_ALL_LED_COLOR); //command to change all LEDs' colors
6694
_i2cPort->write(red); //update red value
@@ -77,6 +105,22 @@ boolean LED::setLEDColor(uint8_t red, uint8_t green, uint8_t blue) {
77105
//Pass in 3 arrays of color values
78106
//each color must be a value between 0-255
79107
boolean LED::setLEDColor(uint8_t redArray[], uint8_t greenArray[], uint8_t blueArray[], uint8_t length) {
108+
// First, boundary check
109+
for (int i = 0; i < length; i++){
110+
if (redArray[i] > 255)
111+
redArray[i] = 255;
112+
if (redArray[i] < 0)
113+
redArray[i] = 0;
114+
if (greenArray[i] > 255)
115+
greenArray[i] = 255;
116+
if (greenArray[i] < 0)
117+
greenArray[i] = 0;
118+
if (blueArray[i] > 255)
119+
blueArray[i] = 255;
120+
if (blueArray[i] < 0)
121+
blueArray[i] = 0;
122+
}
123+
80124
//ATtiny has a 16 uint8_t limit on a single I2C transmission,
81125
//so multiple calls to commands are required
82126
uint8_t n;
@@ -158,6 +202,12 @@ boolean LED::setLEDColor(uint8_t redArray[], uint8_t greenArray[], uint8_t blueA
158202
//To turn LEDs off but remember their previous color, set brightness to 0
159203
//LEDS indexed starting at 1
160204
boolean LED::setLEDBrightness(uint8_t number, uint8_t brightness) {
205+
// First, boundary check
206+
if (brightness > 31)
207+
brightness = 31;
208+
if (brightness < 0)
209+
brightness = 0;
210+
161211
_i2cPort->beginTransmission(_LEDAddress); //Communicate using the address
162212
_i2cPort->write(COMMAND_WRITE_SINGLE_LED_BRIGHTNESS); //Command to write single brightness
163213
_i2cPort->write(number); //Choose which LED
@@ -174,6 +224,12 @@ boolean LED::setLEDBrightness(uint8_t number, uint8_t brightness) {
174224
//brightness must be a value between 0-31
175225
//To turn all LEDs off but remember their previous color, set brightness to 0
176226
boolean LED::setLEDBrightness(uint8_t brightness) {
227+
// First, boundary check
228+
if (brightness > 31)
229+
brightness = 31;
230+
if (brightness < 0)
231+
brightness = 0;
232+
177233
_i2cPort->beginTransmission(_LEDAddress); //Communicate using the address
178234
_i2cPort->write(COMMAND_WRITE_ALL_LED_BRIGHTNESS); //Commaand to change all brightness
179235
_i2cPort->write(brightness); //Update brightness

0 commit comments

Comments
 (0)