diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/buzzer/buzzer.cpp | 37 | ||||
| -rw-r--r-- | lib/buzzer/buzzer.h | 14 | ||||
| -rw-r--r-- | lib/melodies/melodies.cpp | 60 | ||||
| -rw-r--r-- | lib/melodies/melodies.h | 10 | ||||
| -rw-r--r-- | lib/rgb/rgb.cpp | 25 |
5 files changed, 130 insertions, 16 deletions
diff --git a/lib/buzzer/buzzer.cpp b/lib/buzzer/buzzer.cpp new file mode 100644 index 0000000..a83ec22 --- /dev/null +++ b/lib/buzzer/buzzer.cpp @@ -0,0 +1,37 @@ +#include <Arduino.h> +#include "settings.h" +#include "buzzer.h" + +#define BUZZER_CHANNEL 3 +#define BUZZER_RESOLUTION 8 + +BUZZER::BUZZER() +{ + ledcSetup(BUZZER_CHANNEL, 2000, BUZZER_RESOLUTION); + ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL); +} + +void BUZZER::playTone(uint16_t frequency, uint32_t duration) +{ + ledcWriteTone(BUZZER_CHANNEL, frequency); + if (duration > 0) + { + delay(duration); + stop(); + } +} + +void BUZZER::playMelody(const uint16_t melody[][2], size_t length) +{ + + for (size_t i = 0; i < length; i++) + { + playTone(melody[i][0], melody[i][1]); + delay(50); + } +} + +void BUZZER::stop() +{ + ledcWriteTone(BUZZER_CHANNEL, 0); +}
\ No newline at end of file diff --git a/lib/buzzer/buzzer.h b/lib/buzzer/buzzer.h new file mode 100644 index 0000000..f71a90a --- /dev/null +++ b/lib/buzzer/buzzer.h @@ -0,0 +1,14 @@ +#include <Arduino.h> +#include "settings.h" + +#pragma once + +class BUZZER +{ +public: + BUZZER(); + + void playTone(uint16_t frequency, uint32_t duration = 0); + void stop(); + void playMelody(const uint16_t melody[][2], size_t length); +}; diff --git a/lib/melodies/melodies.cpp b/lib/melodies/melodies.cpp new file mode 100644 index 0000000..73d343d --- /dev/null +++ b/lib/melodies/melodies.cpp @@ -0,0 +1,60 @@ +#include <Arduino.h> +#include "melodies.h" + +const uint16_t melody[][2] = { + {262, 300}, // C4, + {294, 300}, // D4, + {330, 300}, // E4, + {349, 300}, // F4, + {392, 300}, // G4, + {440, 300}, // A4, + {494, 300}, // B4, + {523, 300} // C5, +}; + + +const uint16_t surprise[][2] = { + {523, 300}, // C5 + {587, 300}, // D5 + {659, 300}, // E5 + {698, 300}, // F5 + {784, 300}, // G5 + {880, 300}, // A5 + {987, 300}, // B5 + {1047, 300} // C6 +}; + +const uint16_t alaarm[][2] = { + {880, 100}, // A5 + {987, 100}, // B5 + {1047, 100}, // C6 + {880, 100}, // A5 + {987, 100}, // B5 + {1047, 100}, // C6 + {880, 100}, // A5 + {987, 100}, // B5 + {1047, 100} // C6 +}; + +const uint16_t anger[][2] = { + {440, 100}, // A4 + {523, 100}, // C5 + {587, 100}, // D5 + {659, 100}, // E5 + {698, 100}, // F5 + {784, 100}, // G5 + {880, 100}, // A5 + {987, 100}, // B5 + {1047, 100} // C6 +}; + +const uint16_t happy[][2] = { + {262, 300}, // C4 + {294, 300}, // D4 + {330, 300}, // E4 + {349, 300}, // F4 + {392, 300}, // G4 + {440, 300}, // A4 + {494, 300}, // B4 + {523, 300} // C5 +}; diff --git a/lib/melodies/melodies.h b/lib/melodies/melodies.h new file mode 100644 index 0000000..029c8fd --- /dev/null +++ b/lib/melodies/melodies.h @@ -0,0 +1,10 @@ +#include <Arduino.h> + +#pragma once + +const uint16_t happy[][2]; +const uint16_t anger[][2]; +const uint16_t alaarm[][2]; +const uint16_t surprise[][2]; + + diff --git a/lib/rgb/rgb.cpp b/lib/rgb/rgb.cpp index 174a9e5..249e7f0 100644 --- a/lib/rgb/rgb.cpp +++ b/lib/rgb/rgb.cpp @@ -3,15 +3,12 @@ #include "rgb.h" #include <math.h> +#define RED_CHANNEL 0 +#define GREEN_CHANNEL 1 +#define BLUE_CHANNEL 2 -// Kanały PWM (muszą być unikalne) -#define RED_CHANNEL 0 -#define GREEN_CHANNEL 1 -#define BLUE_CHANNEL 2 - -// Częstotliwość PWM i rozdzielczość -#define PWM_FREQ 5000 -#define PWM_RES 10 // 10-bit (0–1023) +#define PWM_FREQ 5000 +#define PWM_RES 10 int RGB::scaleColor(uint8_t val) { @@ -44,7 +41,6 @@ void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor = } } - void RGB::fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration) { int startR = currentR; @@ -77,8 +73,8 @@ void RGB::blink(int cycles, int interval) void RGB::breathe(int cycles, int period) { - const int steps = 128; - int stepDelay = period / (steps * 2); + const int steps = 128; + int stepDelay = period / (steps * 2); uint8_t baseR = currentR; uint8_t baseG = currentG; @@ -93,7 +89,6 @@ void RGB::breathe(int cycles, int period) delay(stepDelay); } - for (int i = steps; i >= 0; --i) { float scale = (float)i / steps; @@ -105,7 +100,6 @@ void RGB::breathe(int cycles, int period) setColorRGB(baseR, baseG, baseB); } - void RGB::rainbowCycle(int duration, float speed) { // speed (0.1 – slow, 1.0 – normal, 2.0 – fast) @@ -114,9 +108,9 @@ void RGB::rainbowCycle(int duration, float speed) for (int i = 0; i < steps; i++) { float angle = i * speed * 0.024; - int red = sin(angle) * 127 + 128; + int red = sin(angle) * 127 + 128; int green = sin(angle + 2) * 127 + 128; - int blue = sin(angle + 4) * 127 + 128; + int blue = sin(angle + 4) * 127 + 128; setColorRGB(red, green, blue, false); delay(duration / steps); @@ -124,4 +118,3 @@ void RGB::rainbowCycle(int duration, float speed) setColorRGB(currentR, currentG, currentB, false); } - |