-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndicator.cpp
179 lines (144 loc) · 4.63 KB
/
Indicator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Created by Misha on 6/25/2020.
//
#include <Arduino.h>
#include "Tone.h"
#include "Indicator.h"
Indicator::Indicator(uint8_t r_pin, uint8_t g_pin, uint8_t b_pin, uint8_t buzz_pin, uint8_t loud_buzz_pin) {
this->r_pin = r_pin;
this->g_pin = g_pin;
this->b_pin = b_pin;
this->buzz_pin = buzz_pin;
this->loud_buzz_pin = loud_buzz_pin;
pr = pb = pg = br = bg = bb = 0;
pulse_duration = pulse_interval = 0;
yell_duration = yell_start = 0;
pulse_on = pulsed = yelling = false;
pulse_ref = 0;
}
void Indicator::start() {
pinMode(r_pin, OUTPUT);
pinMode(g_pin, OUTPUT);
pinMode(b_pin, OUTPUT);
pinMode(buzz_pin, OUTPUT);
pinMode(loud_buzz_pin, OUTPUT);
digitalWrite(loud_buzz_pin, HIGH);
analogWrite(r_pin, PWM_MAX);
analogWrite(g_pin, PWM_MAX);
analogWrite(b_pin, PWM_MAX);
}
void Indicator::setPassiveRGB(uint8_t r, uint8_t g, uint8_t b) {
pr = ((uint16_t) r) << 4U;
pg = ((uint16_t) g) << 4U;
pb = ((uint16_t) b) << 4U;
analogWrite(r_pin, constrain(PWM_MAX - r, 0, PWM_MAX));
analogWrite(g_pin, constrain(PWM_MAX - g, 0, PWM_MAX));
analogWrite(b_pin, constrain(PWM_MAX - b, 0, PWM_MAX));
}
void Indicator::setBlinkRGB(uint8_t r, uint8_t g, uint8_t b) {
br = ((uint16_t) r) << 4U;
bg = ((uint16_t) g) << 4U;
bb = ((uint16_t) b) << 4U;
}
void Indicator::cycle() {
for (int r = 0; r < PWM_MAX; r += 64)
for (int g = 0; g < PWM_MAX; g += 64)
for (int b = 0; b < PWM_MAX; b += 64) {
setPassiveRGB(r, g, b);
delay(1);
}
}
void Indicator::update() {
if(yelling && millis() - yell_start > yell_duration){
yelling = false;
digitalWrite(loud_buzz_pin, HIGH);
}
if (pulse_on) {
unsigned long t = (millis() - pulse_ref) % (pulse_duration + pulse_interval);
if (t < pulse_duration && !pulsed) {
beep(pulse_duration);
pulsed = true;
analogWrite(r_pin, constrain(PWM_MAX - br, 0, PWM_MAX));
analogWrite(g_pin, constrain(PWM_MAX - bg, 0, PWM_MAX));
analogWrite(b_pin, constrain(PWM_MAX - bb, 0, PWM_MAX));
} else if (t >= pulse_duration && pulsed) {
pulsed = false;
analogWrite(r_pin, constrain(PWM_MAX - pr, 0, PWM_MAX));
analogWrite(g_pin, constrain(PWM_MAX - pg, 0, PWM_MAX));
analogWrite(b_pin, constrain(PWM_MAX - pb, 0, PWM_MAX));
}
}
}
void Indicator::beep(unsigned int duration) const {
tone(buzz_pin, F_BEEP, duration);
}
void Indicator::boop(unsigned int duration) const {
tone(buzz_pin, F_BOOP, duration);
}
void Indicator::silence() const {
noTone(buzz_pin);
}
void Indicator::setPulse(unsigned int duration, unsigned int interval) {
this->pulse_duration = duration;
this->pulse_interval = interval;
this->pulse_on = true;
this->pulsed = false;
pulse_ref = millis();
}
void Indicator::disablePulse() {
this->pulse_on = false;
}
//TODO integrate led blinking into bitstring methods
void Indicator::beepstring(uint8_t bitstring, int bitrate) {
unsigned int duration = 4 * 1000 / bitrate / 5;
unsigned int wait = 1000 / bitrate;
for (unsigned int i = 0; i < sizeof(uint8_t) * 8; i++) {
if (bitstring & 0x80U)
beep(duration);
else
boop(duration);
delay(wait);
bitstring <<= 1U;
}
}
void Indicator::beepstring(uint16_t bitstring, int bitrate) {
unsigned int duration = 4 * 1000 / bitrate / 5;
unsigned int wait = 1000 / bitrate;
for (unsigned int i = 0; i < sizeof(uint16_t) * 8; i++) {
if (bitstring & 0x8000U)
beep(duration);
else
boop(duration);
delay(wait);
bitstring <<= 1U;
}
}
void Indicator::beepstring(uint32_t bitstring, int bitrate) {
unsigned int duration = 4 * 1000 / bitrate / 5;
unsigned int wait = 1000 / bitrate;
for (unsigned int i = 0; i < sizeof(uint32_t) * 8; i++) {
if (bitstring & 0x80000000UL)
beep(duration);
else
boop(duration);
delay(wait);
bitstring <<= 1U;
}
}
void Indicator::beepstring(uint64_t bitstring, int bitrate) {
unsigned int duration = 4 * 1000 / bitrate / 5;
unsigned int wait = 1000 / bitrate;
for (unsigned int i = 0; i < sizeof(uint64_t) * 8; i++) {
if (bitstring & 0x8000000000000000UL)
beep(duration);
else
boop(duration);
delay(wait);
bitstring <<= 1U;
}
}
void Indicator::yell(unsigned int duration) {
yell_start = millis();
digitalWrite(loud_buzz_pin, LOW);
yelling = true;
}