Skip to content

Commit cd78fa8

Browse files
1 feature wrapper (#12)
1 parent 0c76941 commit cd78fa8

File tree

8 files changed

+549
-0
lines changed

8 files changed

+549
-0
lines changed

.gitignore

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,87 @@
1+
# Developer files
2+
test-bin
3+
*.iml
4+
.idea
5+
.directory
6+
7+
### macOS ###
8+
# General
9+
.DS_Store
10+
.AppleDouble
11+
.LSOverride
12+
13+
# Icon must end with two \r
14+
Icon
15+
16+
17+
# Thumbnails
18+
._*
19+
20+
# Files that might appear in the root of a volume
21+
.DocumentRevisions-V100
22+
.fseventsd
23+
.Spotlight-V100
24+
.TemporaryItems
25+
.Trashes
26+
.VolumeIcon.icns
27+
.com.apple.timemachine.donotpresent
28+
29+
# Directories potentially created on remote AFP share
30+
.AppleDB
31+
.AppleDesktop
32+
Network Trash Folder
33+
Temporary Items
34+
.apdisk
35+
36+
### macOS Patch ###
37+
# iCloud generated files
38+
*.icloud
39+
40+
### Windows ###
41+
# Windows thumbnail cache files
42+
Thumbs.db
43+
Thumbs.db:encryptable
44+
ehthumbs.db
45+
ehthumbs_vista.db
46+
47+
# Dump file
48+
*.stackdump
49+
50+
# Folder config file
51+
[Dd]esktop.ini
52+
53+
# Recycle Bin used on file shares
54+
$RECYCLE.BIN/
55+
56+
# Windows Installer files
57+
*.cab
58+
*.msi
59+
*.msix
60+
*.msm
61+
*.msp
62+
63+
# Windows shortcuts
64+
*.lnk
65+
66+
### VisualStudioCode ###
67+
.vscode/*
68+
!.vscode/settings.json
69+
!.vscode/tasks.json
70+
!.vscode/launch.json
71+
!.vscode/extensions.json
72+
!.vscode/*.code-snippets
73+
74+
# Local History for Visual Studio Code
75+
.history/
76+
77+
# Built Visual Studio Code Extensions
78+
*.vsix
79+
80+
### VisualStudioCode Patch ###
81+
# Ignore all local history of files
82+
.history
83+
.ionide
84+
185
# Prerequisites
286
*.d
387

Lantern.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "Lantern.h"
2+
3+
Lantern::Lantern() {
4+
_pin = 8; // default is set to #8, integrated neopixel.
5+
_numPixels = 1; // assumes single LED.
6+
_brightness = 50; // default is set to ~1/5th brightness. Max is 255.
7+
_pixels = Adafruit_NeoPixel(_numPixels, _pin, NEO_GRB + NEO_KHZ800);
8+
}
9+
10+
Lantern::Lantern(uint8_t pin, uint8_t numPixels, uint8_t brightness) {
11+
_pin = pin;
12+
_numPixels = numPixels;
13+
_brightness = brightness;
14+
_pixels = Adafruit_NeoPixel(_numPixels, _pin, NEO_GRB + NEO_KHZ800);
15+
}
16+
17+
void Lantern::begin() {
18+
_pixels.setBrightness(_brightness);
19+
_pixels.begin();
20+
}
21+
22+
void Lantern::setBrightness(uint8_t brightness) {
23+
_pixels.setBrightness(brightness);
24+
}
25+
26+
uint32_t Lantern::color(uint8_t red, uint8_t green, uint8_t blue) {
27+
// The red, green, and blue values are bitwise ANDed with 0xFF to ensure they
28+
// fit within the range of 0-255, and then left-shifted and combined using
29+
// bitwise OR operations.
30+
31+
// a 32-bit color is composed of 8-bit red, green, and blue values
32+
// | RED | GREEN | BLUE |
33+
// | 32 -> 24 | 24 -> 16 | 16 -> 8 |
34+
return _pixels.Color(red, green, blue);
35+
}
36+
37+
void Lantern::setColor(float seconds, uint8_t red, uint8_t green, uint8_t blue) {
38+
setColor(seconds, color(red, green, blue));
39+
}
40+
41+
void Lantern::setColor(float duration, uint32_t color) {
42+
_pixels.setPixelColor(0, color);
43+
_pixels.show();
44+
wait(duration);
45+
}
46+
47+
static void Lantern::wait(float seconds) {
48+
static const float THREE_DAYS = 259200;
49+
// Bounds check
50+
if (seconds < 0) {
51+
Serial.println("Can't wait negative seconds, waiting default of 0.25 seconds");
52+
seconds = 0.25;
53+
} else if (seconds > THREE_DAYS) {
54+
Serial.println("Are you sure you want to keep this color for longer than 3 days? That's a long time!");
55+
}
56+
57+
delay(int(seconds * 1000));
58+
}

Lantern.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @file Lantern.h
3+
* @author Ryan ([email protected])
4+
* @brief Wrapper library for Adafruit_NeoPixel
5+
* @version 0.1
6+
* @date 2024-02-19
7+
*
8+
* @copyright Copyright (c) 2024
9+
*
10+
*/
11+
#ifndef Lantern_h
12+
#define Lantern_h
13+
14+
#include "Arduino.h"
15+
#include "Adafruit_NeoPixel.h"
16+
17+
class Lantern {
18+
public:
19+
20+
/**
21+
* @brief Constructs a new Lantern object with safe default values. Uses
22+
* integrated neopixel.
23+
*
24+
*/
25+
Lantern();
26+
27+
/**
28+
* @brief Construct a new Lantern object with specified brightness.
29+
*
30+
* @param pin output pin on the adafruit flora.
31+
* @param numLed number of LED lights.
32+
* @param brightness takes input from 0-255 and controls brightness from
33+
* darkest `0` to brightest `255`.
34+
*/
35+
Lantern(uint8_t pin, uint8_t numLed, uint8_t brightness);
36+
37+
/**
38+
* @brief Starts or restarts LED at current brightness.
39+
*
40+
* To change brightness, use `setBrightness(int brightness)`.
41+
*/
42+
void begin();
43+
44+
/**
45+
* @brief Sets the brightness of the LED.
46+
*
47+
* @param brightness takes input from 0-255 and controls brightness from
48+
* darkest `0` to brightest `255`.
49+
*/
50+
void setBrightness(uint8_t brightness);
51+
52+
/**
53+
* @brief Packs red, green, blue values into `uint32` to play nicely with
54+
* Adafruit color functions.
55+
*
56+
* @param red
57+
* @param green
58+
* @param blue
59+
* @return uint32_t
60+
*/
61+
uint32_t color(uint8_t red, uint8_t green, uint8_t blue);
62+
63+
/**
64+
* @brief Set the color using just seconds, red, green, and blue
65+
*
66+
* @param seconds duration to be set
67+
* @param red how red your color will be
68+
* @param green how green your color will be
69+
* @param blue how blue your color will be
70+
*/
71+
void setColor(float seconds, uint8_t red, uint8_t green, uint8_t blue);
72+
73+
/**
74+
* @brief Set the color using Adafruit's `Color()` function.
75+
*
76+
* @param seconds how long the light will stay that color
77+
* @param color packed RGB color value for the light
78+
*/
79+
void setColor(float seconds, uint32_t color);
80+
81+
/**
82+
* @brief Wait for the amount of seconds before continuing. Used to hold
83+
* the current color value for some time.
84+
*
85+
* It just calls delay in units of seconds instead of microseconds.
86+
*
87+
* @param seconds
88+
*/
89+
static void wait(float seconds);
90+
91+
private:
92+
int _pin;
93+
int _numPixels;
94+
int _brightness;
95+
Adafruit_NeoPixel _pixels;
96+
};
97+
98+
#endif

examples/act1-lights/act1-lights.ino

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Activity 1: Get those lights on!
3+
*
4+
*/
5+
#include <Lantern.h>
6+
7+
Lantern lantern;
8+
9+
/**
10+
* @brief This function only runs once to "setup" the Lantern.
11+
*
12+
*/
13+
void setup() {
14+
lantern.begin();
15+
}
16+
17+
/**
18+
* @brief This function repeats itself, like a loop!
19+
*
20+
*/
21+
void loop() {
22+
checkLight();
23+
}
24+
25+
/**
26+
* @brief Sets color values for our lantern. Used to check if our Lantern is
27+
* ready to accept instructions.
28+
*
29+
* Not getting the color you want? Check out this website to learn how to pick
30+
* your color using just Red, Green, and Blue!
31+
* - [RGB Color Picker](https://www.w3schools.com/colors/colors_rgb.asp)
32+
*/
33+
void checkLight() {
34+
// Set color values here! Colors range from 0 to 255
35+
int red = 0;
36+
int green = 150;
37+
int blue = 0;
38+
float seconds = 0.5;
39+
40+
lantern.setColor(seconds, lantern.color(red, green, blue));
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Activity 2: Why repeat when you can loop?
3+
*/
4+
5+
#include <Lantern.h>
6+
7+
Lantern lantern;
8+
9+
void setup() {
10+
lantern.begin();
11+
}
12+
13+
void loop() {
14+
redlight(1);
15+
yellowlight(1);
16+
greenlight(1);
17+
}
18+
19+
void redlight(int seconds) {
20+
int Red = 150;
21+
int Green = 0;
22+
int Blue = 0;
23+
24+
lantern.setColor(seconds, lantern.color(Red, Green, Blue));
25+
}
26+
27+
void yellowlight(int seconds) {
28+
int Red = 150;
29+
int Green = 150;
30+
int Blue = 0;
31+
32+
lantern.setColor(seconds, lantern.color(Red, Green, Blue));
33+
}
34+
35+
void greenlight(int seconds) {
36+
int Red = 0;
37+
int Green = 150;
38+
int Blue = 0;
39+
40+
lantern.setColor(seconds, lantern.color(Red, Green, Blue));
41+
}

0 commit comments

Comments
 (0)