-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <Arduino.h> | ||
|
||
class Led { | ||
const uint8_t pin; | ||
uint8_t brightness; | ||
int16_t fadeAmount; | ||
unsigned long lastDebounceTime; | ||
|
||
public: | ||
Led(uint8_t pin) | ||
: pin(pin), brightness(255), fadeAmount(5), lastDebounceTime(0) { | ||
pinMode(pin, OUTPUT); | ||
} | ||
auto on() -> void { analogWrite(pin, brightness); } | ||
auto off() -> void { analogWrite(pin, 0); } | ||
|
||
auto fade() -> void { | ||
analogWrite(pin, brightness); | ||
brightness = brightness + fadeAmount; | ||
if (brightness == 0 || brightness >= 255) { | ||
fadeAmount = -fadeAmount; | ||
} | ||
delay(30); | ||
} | ||
|
||
auto set_brightness(uint8_t value) -> void { brightness = value; } | ||
auto get_brightness() -> uint8_t { return brightness; } | ||
auto set_fade_amount(int16_t value) -> void { fadeAmount = value; } | ||
auto get_fade_amount() -> int16_t { return fadeAmount; } | ||
}; | ||
|
||
class Button { | ||
const uint8_t pin; | ||
bool state; | ||
|
||
public: | ||
Button(uint8_t pin) : pin(pin), state(false) { pinMode(pin, INPUT_PULLUP); } | ||
|
||
auto is_pressed() -> bool { return !digitalRead(pin); } | ||
}; | ||
|
||
class handson_xiao_board { | ||
Led board_led; | ||
Button board_button; | ||
|
||
public: | ||
handson_xiao_board(uint8_t led, uint8_t button) | ||
: board_led(led), board_button(button) {}; | ||
|
||
auto led() -> Led& { return board_led; } | ||
auto button() -> Button& { return board_button; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,32 @@ | ||
// /Users/<username>/.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h | ||
#include <Arduino.h> | ||
// define led according to pin diagram in article | ||
#include <vector> | ||
|
||
class handson_xiao_board { | ||
const uint8_t led; | ||
const uint8_t button; | ||
bool buttonState; | ||
uint8_t brightness; | ||
int16_t fadeAmount; | ||
unsigned long lastDebounceTime; | ||
const unsigned long debounceDelay = 50; // 50ms | ||
#include "handson_xiao_board.hpp" | ||
|
||
public: | ||
handson_xiao_board(uint8_t led, uint8_t button) | ||
: led(led), | ||
button(button), | ||
buttonState(false), | ||
brightness(0), | ||
fadeAmount(5), | ||
lastDebounceTime(0) { | ||
Serial.begin(115200); | ||
pinMode(led, OUTPUT); | ||
pinMode(button, INPUT_PULLUP); | ||
} | ||
|
||
auto pressed() -> bool { | ||
bool currentState = !digitalRead(button); | ||
if (currentState != buttonState) { | ||
lastDebounceTime = millis(); | ||
} | ||
if ((millis() - lastDebounceTime) > debounceDelay) { | ||
buttonState = currentState; | ||
} | ||
return buttonState; | ||
} | ||
auto led_fade() -> void { | ||
analogWrite(led, brightness); | ||
brightness = brightness + fadeAmount; | ||
if (brightness == 0 || brightness >= 255) { | ||
fadeAmount = -fadeAmount; | ||
} | ||
delay(30); | ||
} | ||
auto led_off() -> void { analogWrite(led, 0); } | ||
auto led_on() -> void { analogWrite(led, brightness); } | ||
auto set_brightness(uint8_t value) -> void { brightness = value; } | ||
auto get_brightness() -> uint8_t { return brightness; } | ||
auto set_fade_amount(int16_t value) -> void { fadeAmount = value; } | ||
}; | ||
|
||
handson_xiao_board xiao(D0, D1); | ||
handson_xiao_board board(D0, D1); | ||
|
||
void setup() { | ||
xiao.set_brightness(255); | ||
Serial.begin(115200); | ||
Serial.printf("Brightness: %d\n", xiao.get_brightness()); | ||
board.led().set_brightness(150); | ||
std::vector<bool> pattern = {1, 0, 1, 0, 1, 1, 0, 1}; | ||
for (auto &&led : pattern) { | ||
if (led) { | ||
board.led().on(); | ||
Serial.printf("LED ON\n"); | ||
delay(1000); | ||
} else { | ||
board.led().off(); | ||
Serial.printf("LED OFF\n"); | ||
delay(1000); | ||
} | ||
} | ||
board.led().off(); | ||
} | ||
|
||
void loop() { | ||
xiao.led_on(); | ||
delay(1000); | ||
xiao.led_off(); | ||
delay(1000); | ||
if (board.button().is_pressed()) { | ||
board.led().fade(); | ||
Serial.printf("%d\n", board.led().get_brightness()); | ||
} else { | ||
board.led().off(); | ||
} | ||
} |