-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCamsble.ino
More file actions
413 lines (369 loc) · 10.3 KB
/
Camsble.ino
File metadata and controls
413 lines (369 loc) · 10.3 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// MODE 0 输入触发模式 1 Schedule模式 2 B门模式
#include <Preferences.h>
#include <Ticker.h>
#include "Button2.h"
Preferences preferences;
#define shutterS1 10
#define shutterS2 5
#define BUTTON_A_PIN 1
#define BUTTON_B_PIN 0
#define INPUT_PIN 3
#define MAXPAGE 2
//------------------------------- GLOBAL -_,-
int mode;
int triggerTimes;
int triggerDelay;
int interVal;
int interValSwitch;
int bShutter;
int inputMode;
int hasScreen = 0;
int leftSec = 0;
int shutterCount = 0;
int isBLEEnable = 0;
byte counter = 0;
int changeInterValStep = 0;
Ticker delayTimer;
Ticker triggerTimer;
Ticker scheduleTimer;
Ticker batteryTimer;
Ticker cinterValTimer;
Button2 buttonA, buttonB;
//EasyButton inputScreen(7,35,false,true);
#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include "images.h"
#include "fonts.h"
SSD1306Wire display(0x3c, 7, 6);
OLEDDisplayUi ui ( &display );
// -------------------------
#include <NeoPixelBus.h>
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(1, 8);
#define colorSaturation 128
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor purple(90, 28, 117);
RgbColor yellow(117, 116, 19);
RgbColor white(colorSaturation);
RgbColor black(0);
//-------------------
void setup() {
//setCpuFrequencyMhz(80);
Serial.println("initScreen Done");
strip.Begin();
strip.SetPixelColor(0, blue);
strip.Show();
Serial.println("Setup...");
Serial.begin(115200);
pinMode(shutterS1,OUTPUT);
pinMode(shutterS2,OUTPUT);
pinMode(INPUT_PIN,INPUT_PULLUP);
digitalWrite(shutterS1,HIGH);
digitalWrite(shutterS2,HIGH);
preferences.begin("camsble", false);
mode = preferences.getInt("mode", 0);
triggerTimes = preferences.getInt("triggerTimes", 1);
triggerDelay = preferences.getInt("triggerDelay", 0);
interVal = preferences.getInt("interVal", 5000);
interValSwitch = preferences.getInt("interValSwitch", 1);
bShutter = preferences.getInt("bShutter", 0);
inputMode = preferences.getInt("inputMode", 0);
buttonA.begin(BUTTON_A_PIN,INPUT_PULLUP,false);
buttonA.setClickHandler(handler);
buttonB.begin(BUTTON_B_PIN,INPUT_PULLUP,false);
buttonB.setClickHandler(handler);
buttonA.setDoubleClickHandler(handler);
buttonB.setDoubleClickHandler(handler);
buttonA.setLongClickDetectedHandler(handler);
buttonB.setLongClickDetectedHandler(handler);
buttonA.setPressedHandler(pressed);
buttonA.setReleasedHandler(released);
buttonB.setPressedHandler(pressed);
buttonB.setReleasedHandler(released);
//attachInterrupt(INPUT_PIN,inputTrigger,FALLING);
setInputInterrupt(inputMode);
//initBLE();
readBattery();
batteryTimer.attach_ms(60000, readBattery);
cinterValTimer.attach_ms(150,changeInterval);
initScreen();
Serial.println("Setup Done");
Serial.println(mode);
}
void unPressShutter(){
digitalWrite(shutterS1,HIGH);
digitalWrite(shutterS2,HIGH);
if(mode == 0){
strip.SetPixelColor(0, blue);
strip.Show();
}
if(mode == 1){
strip.SetPixelColor(0, purple);
strip.Show();
}
}
void triggerShutter(){
//Serial.println("Shutter Triggered");
strip.SetPixelColor(0, red);
strip.Show();
digitalWrite(shutterS1,LOW);
digitalWrite(shutterS2,LOW);
triggerTimer.once_ms(200,unPressShutter);
bleTriggerShutter();
shutterCount++;
}
void holdShutter(){
strip.SetPixelColor(0, red);
strip.Show();
digitalWrite(shutterS1,LOW);
digitalWrite(shutterS2,LOW);
bleTriggerShutter();
strip.SetPixelColor(0, red);
strip.Show();
}
int triggerCount = 0;
long debouncing_time = 250; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
void inputTrigger(){
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
if(mode == 0){
triggerCount++;
if(triggerCount >= triggerTimes){
delayTimer.once_ms(triggerDelay,triggerShutter);
triggerCount = 0;
}
}
last_micros = micros();
}
}
unsigned long time_now = 0;
bool isHolding = false;
void loop() {
//Serial.println("loop");
tickScreen();
buttonA.loop();
buttonB.loop();
if(mode == 0){ //输入触发模式
if(inputMode == 2){ //LOW
if(digitalRead(INPUT_PIN) == LOW){
if(!isHolding){
holdShutter();
isHolding = true;
}
}
else{
if(isHolding){
unPressShutter();
isHolding =false;
}
}
}
if(inputMode == 3){ //LOW
if(digitalRead(INPUT_PIN) == HIGH){
if(!isHolding){
holdShutter();
isHolding = true;
}
}
else{
if(isHolding){
unPressShutter();
isHolding =false;
}
}
}
}
if(mode == 1){ //Schedule模式
//更新倒计时
if(interValSwitch == 0){
leftSec = interVal/1000;
}
else{
leftSec = ((interVal - (millis() - time_now))+999)/1000;
// Serial.println(leftSec);
if(leftSec < 3){
strip.SetPixelColor(0, yellow);
strip.Show();
}
if(millis() - time_now > interVal){
time_now = millis();
triggerShutter();
}
}
}
if(mode == 2){ //无线遥控模式
if(bShutter == 1){
digitalWrite(shutterS1,LOW);
strip.SetPixelColor(0, red);
strip.Show();
}
else{
digitalWrite(shutterS1,HIGH);
strip.SetPixelColor(0, green);
strip.Show();
}
}
//Serial.print(digitalRead(6));
//Serial.println(digitalRead(7));
}
//utils
void changeModeUni(int newmode){
preferences.putInt("mode", newmode);
Serial.println(String("Change Mode to:") + newmode);
mode = newmode;
shutterCount = 0;
if(mode == 0){
strip.SetPixelColor(0, blue);
strip.Show();
}
if(mode == 1){
strip.SetPixelColor(0, purple);
strip.Show();
}
ui.transitionToFrame(newmode);
}
unsigned long last_battery_time = 0;
String batteryStatus = "100%";
void readBattery(){
uint8_t percentage = 100;
float voltage = analogRead(4) / 4096.0 * 5.9; // LOLIN D32 (no voltage divider need already fitted to board.or NODEMCU ESP32 with 100K+100K voltage divider
Serial.println("Voltage = " + String(voltage));
percentage = 2808.3808 * pow(voltage, 4) - 43560.9157 * pow(voltage, 3) + 252848.5888 * pow(voltage, 2) - 650767.4615 * voltage + 626532.5703;
if (voltage > 4.19) percentage = 100;
else if (voltage <= 3.50) percentage = 0;
batteryStatus = String(percentage)+"%";
}
void handler(Button2& btn) {
Serial.println("Button Click");
switch (btn.getType()) {
case single_click:
if (btn == buttonA) {
switch(mode){
case 0:
triggerTimes = triggerTimes - 1;
if(triggerTimes < 1) triggerTimes = 1;
preferences.putInt("triggerTimes", triggerTimes);
break;
case 1:
interValSwitch = 0;
preferences.putInt("interValSwitch", 0);
break;
case 2:
inputTrigger();
break;
}
} else if (btn == buttonB) {
switch(mode){
case 0:
triggerTimes = triggerTimes + 1;
preferences.putInt("triggerTimes", triggerTimes);
break;
case 1:
interValSwitch = 1;
preferences.putInt("interValSwitch", 1);
break;
case 2:
inputTrigger();
break;
}
}
break;
case double_click:
if (btn == buttonA) {
prevPage();
} else if (btn == buttonB) {
nextPage();
}
break;
case triple_click:
Serial.print("triple ");
changeBLE();
break;
case long_click:
Serial.println("long click");
if (btn == buttonA) {
switch(mode){
case 0:
inputMode = inputMode - 1;
if(inputMode <0) inputMode = 4;
changeInterrupt(inputMode);
break;
case 1:
changeInterValStep = -1000;
break;
case 2:
changeBLE();
}
} else if (btn == buttonB) {
switch(mode){
case 0:
inputMode = inputMode + 1;
if(inputMode >4 ) inputMode = 0;
changeInterrupt(inputMode);
break;
case 1:
changeInterValStep = 1000;
break;
case 2:
changeBLE();
}
}
break;
}
}
void prevPage(){
Serial.println("prev Page");
int newmode = mode-1;
if(newmode<0) newmode = MAXPAGE;
changeModeUni(newmode);
}
void nextPage(){
Serial.println("next Page");
int newmode = mode+1;
if(newmode>MAXPAGE) newmode = 0;
changeModeUni(newmode);
}
void changeBLE(){
Serial.println("Change BLE");
if(isBLEEnable == 0) initBLE();
else deinitBLE();
}
void pressed(Button2& btn) {
counter++;
if (counter == 2) {
changeBLE();
}
}
void changeInterrupt(int inputMode){ //0 FALLING //1 RISING //2 LOW //3 HIGH //4 CHANGE
Serial.print("Change Interrupt to:");
Serial.println(inputMode);
detachInterrupt(INPUT_PIN);
unPressShutter();
isHolding =false;
setInputInterrupt(inputMode);
preferences.putInt("inputMode", inputMode);
}
void setInputInterrupt(int input){
if(inputMode == 0){
attachInterrupt(INPUT_PIN,inputTrigger,FALLING);
}
if(inputMode == 1){
attachInterrupt(INPUT_PIN,inputTrigger,RISING);
}
if(inputMode == 4){
attachInterrupt(INPUT_PIN,inputTrigger,CHANGE);
}
}
void changeInterval(){
int lastInterVal = interVal;
interVal = interVal + changeInterValStep;
if(lastInterVal != interVal){
preferences.putInt("interVal", interVal);
}
}
/////////////////////////////////////////////////////////////////
void released(Button2& btn) {
counter--;
changeInterValStep = 0;
}