Skip to content

Commit a1c56a9

Browse files
committed
Refactor lib includes for Arduino Cloud
1 parent 02e634b commit a1c56a9

File tree

6 files changed

+76
-85
lines changed

6 files changed

+76
-85
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Include the library in your sketch and initialize it with your WiFi credentials.
2121
## Methods
2222

2323
- `begin(const char* ssid, const char* password)`: Initialize the library with your WiFi credentials.
24-
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak.
25-
- `showColorName()`: Show the current CheerLights color name (e.g. "red").
26-
- `showColorHex()`: Show the current CheerLights color as a hex value (e.g. 0xFF0000).
27-
- `showRed()`, `showGreen()`, `showBlue()`: Show the RGB values for the current CheerLights color (e.g. 255, 0, 0).
24+
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak channel 1417.
25+
- `currentColorName()`: The current CheerLights color name (e.g. "red").
26+
- `currentColorHex()`: The current CheerLights color as a hex value (e.g. 0xFF0000).
27+
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0).
Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,78 @@
11
/*
2-
'CheerLightsExample' demonstrates how to use the CheerLights library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
2+
'CheerLightsExample' demonstrates how to use the CheerLights Arduino library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
33
44
To learn more about CheerLights, visit https://cheerlights.com.
55
*/
66

7-
#include <CheerLights.h>
8-
#include "secrets.h"
9-
#include <Adafruit_NeoPixel.h> // For controlling NeoPixels
7+
// Include the Adafruit NeoPixel library and initialize the strip
8+
#include <Adafruit_NeoPixel.h>
109

1110
#define LED_PIN 6
1211
#define NUM_LEDS 8
1312

14-
CheerLights CheerLights;
1513
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
1614

15+
// Include the correct WiFi library based on the board
16+
#if defined(ESP8266)
17+
#include <ESP8266WiFi.h>
18+
#elif defined(ESP32)
19+
#include <WiFi.h>
20+
#elif defined(ARDUINO_SAMD_MKR1000)
21+
// For Arduino MKR1000 using WiFi101 library
22+
#include <WiFi101.h>
23+
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
24+
// For Arduino MKR WiFi 1010 using WiFiNINA library
25+
#include <WiFiNINA.h>
26+
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
27+
// For Arduino Uno WiFi Rev2
28+
#include <WiFiNINA.h>
29+
#elif defined(ARDUINO_ARCH_SAMD)
30+
// For other SAMD boards
31+
#include <WiFiNINA.h>
32+
#else
33+
#include <WiFi.h>
34+
#endif
35+
36+
// Include the CheerLights library and instantiate the CheerLights object
37+
#include <CheerLights.h>
38+
CheerLights CheerLights;
39+
40+
// Include the secrets file that contains the WiFi credentials
41+
#include "secrets.h"
42+
1743
unsigned long previousMillis = 0;
18-
const long interval = 15000; // 15 seconds
44+
const long updateInterval = 15000;
1945

2046
void setup() {
2147
Serial.begin(115200);
2248
CheerLights.begin(SECRET_SSID, SECRET_PASSWORD);
2349
strip.begin();
24-
strip.show(); // Initialize all pixels to 'off'
50+
51+
// Get the current CheerLights color and set the LEDs to that color
52+
CheerLights.getCurrentColor();
53+
Serial.print("Current CheerLights Color: ");
54+
Serial.println(CheerLights.currentColorName());
55+
setLEDColors(CheerLights.currentRed(), CheerLights.currentGreen(), CheerLights.currentBlue());
2556
}
2657

2758
void loop() {
2859
unsigned long currentMillis = millis();
2960

30-
if (currentMillis - previousMillis >= interval) {
61+
// Update the LEDs every updateInterval milliseconds
62+
if (currentMillis - previousMillis >= updateInterval) {
3163
previousMillis = currentMillis;
3264

33-
// Get the current CheerLights color
65+
// Get the current CheerLights color and set the LEDs to that color
66+
CheerLights.getCurrentColor();
3467
Serial.print("Current CheerLights Color: ");
35-
Serial.println(CheerLights.getCurrentColor());
36-
37-
// Show the color as a hex value
38-
Serial.print("Color Hex: 0x");
39-
Serial.println(CheerLights.showColorHex(), HEX);
40-
41-
// Show the RGB values
42-
uint8_t red = CheerLights.showRed();
43-
uint8_t green = CheerLights.showGreen();
44-
uint8_t blue = CheerLights.showBlue();
45-
46-
Serial.print("Red: ");
47-
Serial.print(red);
48-
Serial.print(" Green: ");
49-
Serial.print(green);
50-
Serial.print(" Blue: ");
51-
Serial.println(blue);
52-
53-
// Show the color name
54-
Serial.print("CheerLights Color Name: ");
55-
Serial.println(CheerLights.showColorName());
56-
57-
// Set all pixels to the CheerLights color using the RGB values
58-
for (int i = 0; i < NUM_LEDS; i++) {
59-
strip.setPixelColor(i, strip.Color(red, green, blue));
60-
}
61-
strip.show();
68+
Serial.println(CheerLights.currentColorName());
69+
setLEDColors(CheerLights.currentRed(), CheerLights.currentGreen(), CheerLights.currentBlue());
6270
}
63-
64-
// Other non-blocking code can go here
6571
}
72+
73+
void setLEDColors(uint8_t red, uint8_t green, uint8_t blue) {
74+
for (int i = 0; i < NUM_LEDS; i++) {
75+
strip.setPixelColor(i, strip.Color(red, green, blue));
76+
}
77+
strip.show();
78+
}

keywords.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
CheerLights KEYWORD1
2-
getCurrentColor KEYWORD2
3-
showColorName KEYWORD2
4-
showColorHex KEYWORD2
5-
showRed KEYWORD2
6-
showGreen KEYWORD2
7-
showBlue KEYWORD2
8-
begin KEYWORD2
1+
CheerLights KEYWORD1
2+
getCurrentColor KEYWORD2
3+
currentColorName KEYWORD2
4+
currentColorHex KEYWORD2
5+
currentRed KEYWORD2
6+
currentGreen KEYWORD2
7+
currentBlue KEYWORD2
8+
begin KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CheerLights
2-
version=1.0.1
2+
version=1.0.2
33
author=Hans Scharler <hscharler@gmail.com>
44
maintainer=Hans Scharler <hscharler@gmail.com>
55
sentence=Fetch and use the latest CheerLights color.

src/CheerLights.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "CheerLights.h"
22

33
#define MIN_UPDATE_INTERVAL 5000
4+
#define TIMEOUT 5000
45

56
CheerLights::CheerLights() {
67
_colorName = "black";
@@ -83,7 +84,7 @@ void CheerLights::_fetchColor() {
8384
// Wait for response
8485
unsigned long timeout = millis();
8586
while (client.connected() && !client.available()) {
86-
if (millis() - timeout > 5000) { // 5 seconds timeout
87+
if (millis() - timeout > TIMEOUT) {
8788
Serial.println(F(">>> Client Timeout!"));
8889
client.stop();
8990
return;
@@ -97,7 +98,6 @@ void CheerLights::_fetchColor() {
9798
line.trim();
9899

99100
if (!headersEnd) {
100-
// Check for end of headers
101101
if (line.length() == 0) {
102102
headersEnd = true;
103103
}
@@ -155,22 +155,22 @@ String CheerLights::getCurrentColor() {
155155
return _colorName;
156156
}
157157

158-
String CheerLights::showColorName() {
158+
String CheerLights::currentColorName() {
159159
return _colorName;
160160
}
161161

162-
uint32_t CheerLights::showColorHex() {
162+
uint32_t CheerLights::currentColorHex() {
163163
return _colorHex;
164164
}
165165

166-
uint8_t CheerLights::showRed() {
166+
uint8_t CheerLights::currentRed() {
167167
return (_colorHex >> 16) & 0xFF;
168168
}
169169

170-
uint8_t CheerLights::showGreen() {
170+
uint8_t CheerLights::currentGreen() {
171171
return (_colorHex >> 8) & 0xFF;
172172
}
173173

174-
uint8_t CheerLights::showBlue() {
174+
uint8_t CheerLights::currentBlue() {
175175
return _colorHex & 0xFF;
176176
}

src/CheerLights.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,16 @@
44
#include <Arduino.h>
55
#include <WiFiClient.h>
66

7-
// Include the correct WiFi library based on the board
8-
#if defined(ESP8266)
9-
#include <ESP8266WiFi.h>
10-
#elif defined(ESP32)
11-
#include <WiFi.h>
12-
#elif defined(ARDUINO_SAMD_MKR1000)
13-
// For Arduino MKR1000 using WiFi101 library
14-
#include <WiFi101.h>
15-
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
16-
// For Arduino MKR WiFi 1010 using WiFiNINA library
17-
#include <WiFiNINA.h>
18-
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
19-
// For Arduino Uno WiFi Rev2
20-
#include <WiFiNINA.h>
21-
#elif defined(ARDUINO_ARCH_SAMD)
22-
// For other SAMD boards
23-
#include <WiFiNINA.h>
24-
#else
25-
// Default to WiFi.h
26-
#include <WiFi.h>
27-
#endif
28-
297
class CheerLights {
308
public:
319
CheerLights();
3210
void begin(const char* ssid, const char* password);
3311
String getCurrentColor();
34-
String showColorName();
35-
uint32_t showColorHex();
36-
uint8_t showRed();
37-
uint8_t showGreen();
38-
uint8_t showBlue();
12+
String currentColorName();
13+
uint32_t currentColorHex();
14+
uint8_t currentRed();
15+
uint8_t currentGreen();
16+
uint8_t currentBlue();
3917

4018
private:
4119
void _connectToWiFi();

0 commit comments

Comments
 (0)