-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathledstrip_BT_esp32.ino.bak
136 lines (121 loc) · 3.64 KB
/
ledstrip_BT_esp32.ino.bak
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
#include <ArduinoJson.h>
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
StaticJsonDocument<1024> doc;
String inputStr;
#define LED_BUILTIN 2
#define MODE_STATIC 1
#define MODE_RAINBOW 2
struct Effect {
//uint16_t pixels;
uint8_t mode;
bool onOff;
uint8_t brightness;
float speed;
//float periods;
//uint8_t fading;
//RgbColor color;
//float phase;
//uint16_t checksum;
} effect;
// fade LED PIN
#define LED_PIN 4
// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0 0
// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT 13
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ 5000
// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(LED_BUILTIN, OUTPUT);
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
effect.mode = MODE_STATIC;
effect.onOff = true;
effect.brightness = 128;
effect.speed = 1;
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
inputStr = SerialBT.readStringUntil('\n');
Serial.println(inputStr);
//char s[100];
//S.toCharArray(s, 99);
/*for (int i=0; i < sizeof(S)+5; i++) {
Serial.println(s[i], DEC);
}*/
//char c = SerialBT.read();
const char *s = inputStr.c_str();
char c = s[0];
//Serial.write(c);
switch (c) {
case 'a':
digitalWrite(LED_BUILTIN, HIGH);
break;
case 'b':
digitalWrite(LED_BUILTIN, LOW);
break;
case '*':
//if (digitalRead(LED_BUILTIN)) doc["led"] = "true"; else doc["led"] = "false";
if (effect.onOff) doc["onOff"] = "true"; else doc["onOff"] = "false";
doc["brightness"] = effect.brightness;
doc["speed"] = effect.speed;
serializeJson(doc, SerialBT); SerialBT.write('\n');
break;
default:
parseEffect();
updateEffect();
}
}
if (effect.mode > 1 && effect.onOff == true) updateEffect();
delay(20);
}
// https://arduinojson.org/v5/api/jsonobject/containskey/
#define IFKEY(a) if (doc.containsKey(a))
#define IF(a,b) IFKEY(a) if (!strcmp(doc[a],b))
void parseEffect() {
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, inputStr);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
IFKEY("brightness") effect.brightness = doc["brightness"];
IFKEY("onOff") effect.onOff = doc["onOff"];
IFKEY("mode") effect.mode = doc["mode"];
IFKEY("speed") effect.speed = doc["speed"];
}
void updateEffect() {
if (effect.onOff) {
digitalWrite(LED_BUILTIN, HIGH);
switch (effect.mode) {
case MODE_STATIC:
ledcAnalogWrite(LEDC_CHANNEL_0, effect.brightness);
break;
case MODE_RAINBOW:
//updateRainbow();
uint8_t a = ((millis() >> 4) * effect.speed);
ledcAnalogWrite(LEDC_CHANNEL_0, a);
break;
}
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}