-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathled_blaster.cpp
51 lines (39 loc) · 969 Bytes
/
led_blaster.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
#define LED_PIN LED_BUILTIN
#define LED_ON_MS_NORMAL 15
#define LED_OFF_MS_NORMAL 450
#define LED_ON_MS_FAST 6
#define LED_OFF_MS_FAST 408
#include <Arduino.h>
#include "led_blaster.h"
namespace LedBlaster {
uint16_t led_on_ms;
uint16_t led_off_ms;
void setup() {
pinMode(LED_PIN, OUTPUT);
enable_fast_mode(false);
}
void enable_fast_mode(bool fast_mode_enabled) {
if (fast_mode_enabled) {
led_on_ms = LED_ON_MS_FAST;
led_off_ms = LED_OFF_MS_FAST;
} else {
led_on_ms = LED_ON_MS_NORMAL;
led_off_ms = LED_OFF_MS_NORMAL;
}
}
void emit_0() {
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(led_on_ms);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(led_off_ms);
}
void emit_1() {
delayMicroseconds(led_on_ms + led_off_ms);
}
void emit_byte(uint8_t serial_byte) {
emit_0();
for (int i = 0; i < 8; i++) {
bitRead(serial_byte, i) ? emit_1() : emit_0();
}
}
}