-
Notifications
You must be signed in to change notification settings - Fork 6
/
arduino-code.ino
551 lines (425 loc) · 11.8 KB
/
arduino-code.ino
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
Not the prettiest code, but it works!
Details:
- This code was made for a TTGO LCD 240*135 ESP32 board.
- You can type hex into the serial terminal and it will send the commands to the lens (useful for debugging)
- The lens is connected to the board using a SPI interface
Huge thanks to http://jp79dsfr.free.fr/ for their incredible reverse engineering of the Canon lens protocol.
Check out the document they made here: http://jp79dsfr.free.fr/_Docs%20et%20infos/Photo%20Tech%20_%20Canon%20EOS-EF%20Protocol.pdf
Read my blog post for more details on the project: https://joshuabird.com/blog/post/3d-printed-film-video-camera
Joshua Bird
*/
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
/*
PIN DEFINITIONS
*/
#define STEP_PIN 27
#define EN_PIN 33
#define HSPI_MISO 13
#define HSPI_MOSI 25
#define HSPI_SCLK 15
#define HSPI_CS 39
#define TRIGGER_PIN 17
#define TOP_BUT_PIN 0
#define BOT_BUT_PIN 35
#define SENSOR_PIN 26
#define BAT_PIN 2
#define TFT_WIDTH 135
#define TFT_HEIGHT 240
/*
GLOBAL VARIABLES
*/
int fps = 18;
const float shutterAngle = 150.0;
const int microsteps = 8;
int lowerBound = 500;
int upperBound = 2000;
int selected = 0;
bool selectedChanged = false;
bool changeSelected = false;
uint8_t minAv, maxAv;
uint8_t av;
uint16_t focalLength;
bool isLensInit = false;
int handleTriggerInterrupt = 0;
enum RecordingState {
shouldInitRecording,
speedingUp,
recording,
slowingDown,
shouldDeinitRecording,
waiting,
};
RecordingState recordingState = waiting;
float curRps = 0;
bool sensorIsLow = true;
int sprocketCount = 0;
int prevSprocketCount = 0;
int sprocketSkips = 0;
bool error = false;
bool errorOverride = false;
int timer1 = 0;
int timer2 = 0;
const int accel = 35;
int absolutePosition = 0;
SPIClass * lens_spi = NULL;
static const int spiClk = 500000;
SPISettings spiSettings = SPISettings(spiClk, MSBFIRST, SPI_MODE3);
TFT_eSPI tft = TFT_eSPI();
/*
INTERRUPT HANDLING
*/
void IRAM_ATTR toggleSelectedInterrupt() {
if (!debounce()) return;
selected++;
selected %= 7;
selectedChanged = true;
}
void IRAM_ATTR changeSelectedInterrupt() {
if (!debounce()) return;
changeSelected = true;
}
void IRAM_ATTR triggerInterrupt() {
if (!debounce()) return;
handleTriggerInterrupt = millis();
}
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
pinMode(TRIGGER_PIN, INPUT_PULLDOWN);
pinMode(TOP_BUT_PIN, INPUT_PULLUP);
pinMode(BOT_BUT_PIN, INPUT_PULLUP);
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(TOP_BUT_PIN, toggleSelectedInterrupt, FALLING);
attachInterrupt(BOT_BUT_PIN, changeSelectedInterrupt, RISING);
attachInterrupt(TRIGGER_PIN, triggerInterrupt, CHANGE);
digitalWrite(STEP_PIN, LOW);
digitalWrite(EN_PIN, HIGH);
Serial.begin(2000000);
tft.init();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
lens_spi = new SPIClass(HSPI);
lens_spi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS);
tft.fillScreen(TFT_BLACK);
displayParams();
delay(500);
}
int displayCount = 0;
void loop() {
// Handle interrupts
if (handleTriggerInterrupt != 0 && millis() - handleTriggerInterrupt >= 20) {
if (digitalRead(TRIGGER_PIN) == HIGH && recordingState == waiting) {
recordingState = shouldInitRecording;
}
else if (digitalRead(TRIGGER_PIN) == LOW && recordingState != waiting) {
recordingState = slowingDown;
}
handleTriggerInterrupt = 0;
}
// Do stuff depending on recording state
switch(recordingState) {
case shouldInitRecording: {
digitalWrite(EN_PIN, LOW);
curRps = 0.1;
stopDown(av);
tft.fillScreen(TFT_MAGENTA);
tft.setTextColor(TFT_WHITE, TFT_MAGENTA);
tft.drawString("RECORD", 0, 0, 4);
recordingState = speedingUp;
return;
}
case speedingUp: {
float maxRps = fps/5.0;
recordingState = recording;
changeMotorSpeed(0, maxRps, accel, true);
return;
}
case recording: {
runMotor(fps/5.0, 200*microsteps/5);
checkFilmJam();
return;
}
case slowingDown: {
changeMotorSpeed(curRps, 0, accel, false);
recordingState = shouldDeinitRecording;
returnHome();
return;
}
case shouldDeinitRecording: {
digitalWrite(EN_PIN, HIGH);
openUp();
if (error) {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("ERROR:", 0, 0, 4);
tft.drawString("film jam", 0, 26, 4);
char buf[50];
dtostrf(sprocketSkips, 4, 0, buf);
strcat(buf," skips");
tft.drawString(buf, 0, 78, 4);
}
else {
tft.fillScreen(TFT_BLACK);
displayParams();
}
delay(100);
recordingState = waiting;
break;
}
case waiting: {
break;
}
}
// Update screen stuff
if (selectedChanged) {
displayParams();
selectedChanged = false;
}
if (changeSelected) {
changeSelectedHandler();
displayParams();
changeSelected = false;
}
// Try to connect to lens
if (!isLensInit) {
isLensInit = tryInitLens();
if (isLensInit) displayParams();
}
// Check if lens status has changed every once in a while
if (millis() - timer1 > 100) {
timer1 = millis();
bool newIsLensInit = checkIsLensInit();
if (newIsLensInit != isLensInit) {
isLensInit = newIsLensInit;
displayParams();
}
updateFocalLength();
}
// Update battery reading once in a while?
if (millis() - timer2 > 10000) {
timer2 = millis();
// Update battery
//displayParams();
}
// Read commands from serial
while (Serial.available() > 0) {
uint8_t incomingByte1 = Serial.read();
if (incomingByte1 == 10) break;
uint16_t firstByte = charToHex(incomingByte1) << 4;
uint8_t secondByte = charToHex(Serial.read());
uint16_t dataToSend = firstByte | secondByte;
sendData(dataToSend);
}
}
/**
* @param accel rps/s
*/
void changeMotorSpeed(float startRps, float endRps, float accel, bool shouldCheckFilmJam) {
if (startRps == endRps) return;
int stepCount = 0;
bool speedingUp = startRps < endRps;
if (!speedingUp) accel *= -1;
curRps = startRps+0.1;
int prevFrameTime = 0;
while ((speedingUp && curRps < endRps) || (!speedingUp && curRps > endRps)) {
int delayTime = 1000000/(200*microsteps*curRps*2); // In microseconds
while (micros() - prevFrameTime < delayTime*2) delayMicroseconds(1);
prevFrameTime = micros();
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delayTime);
digitalWrite(STEP_PIN, LOW);
if (shouldCheckFilmJam) readSensor();
stepCount++;
absolutePosition += 1;
absolutePosition %= 200*microsteps/5;
if (stepCount % 200 == 0 && shouldCheckFilmJam) {
checkFilmJam();
if (recordingState == slowingDown) return;
}
curRps += accel * delayTime*1e-6*2;
}
curRps = endRps;
}
void runMotor(float rps, int steps) {
int delayTime = 1000000/(200*microsteps*rps*2);
int prevFrameTime = 0;
for (int i=0; i < steps; i++) {
while (micros() - prevFrameTime < delayTime*2) delayMicroseconds(1);
prevFrameTime = micros();
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delayTime);
digitalWrite(STEP_PIN, LOW);
readSensor();
absolutePosition += 1;
absolutePosition %= 200*microsteps/5;
}
}
void returnHome() {
runMotor(0.5, 200*microsteps/5 - absolutePosition);
}
void checkFilmJam() {
sprocketSkips += 2-min(sprocketCount-prevSprocketCount, 2);
if (sprocketCount == prevSprocketCount && !errorOverride) {
error = true;
recordingState = slowingDown;
}
prevSprocketCount = sprocketCount;
}
bool checkIsLensInit() {
sendData(0x00);
sendData(0x0a);
return sendData(0x00) == 0xaa;
}
void updateFocalLength() {
if (!isLensInit) return;
sendData(0xa0);
uint16_t result1 = sendData(0x00) << 8;
uint8_t result2 = sendData(0x00);
uint16_t result = result1 | result2;
// If focal len has changed
if (result != focalLength) {
focalLength = result;
setMinMaxAv();
av = max(av, minAv);
av = min(av, maxAv);
displayParams();
}
}
float avToFStop(int av) {
return sqrt(pow(2,(av/8.0 - 1)));
}
void changeSelectedHandler() {
switch(selected) {
case 0: // Nothing selected
break;
case 1: // FPS
fps %= 48;
fps += 2;
break;
case 3: // F-Stop
av += 4;
if (av > maxAv) av = minAv;
break;
case 4: // Error override
errorOverride = !errorOverride;
break;
}
}
void stopDown(int av) {
int deltaAv = av - minAv;
sendData(0x12);
sendData(0xff+deltaAv); // Overflows if av is pos, subtracts from 0xFF if av is neg
sendData(0x00);
}
void setMinMaxAv() {
sendData(0xb0);
sendData(0x00);
minAv = sendData(0x00);
maxAv = sendData(0x00);
}
bool tryInitLens() {
if (!checkIsLensInit()) return false;
setMinMaxAv();
openUp();
av = minAv;
// enable IS
sendData(0x00);
sendData(0x0a);
sendData(0x00);
sendData(0x91);
sendData(0xb9);
sendData(0x00);
sendData(0x00);
sendData(0x00);
updateFocalLength();
return true;
}
void openUp() {
sendData(0x12);
sendData(0x80);
sendData(0x00);
}
float batVoltage() {
float value = analogRead(BAT_PIN);
return value * 0.0071746 / 6;
}
void displayParams() {
displayParam(0, 0, "FPS", fps, 2, 0, selected==1);
displayParam(1, 0, "SEC LEFT", (144.0-sprocketCount/2)/fps, 2, 1, selected==2);
displayParam(0, 1, "F-STOP", avToFStop(av), 3, 1, selected==3);
displayParam(1, 1, "ERR OVRD", errorOverride ? "ON" : "OFF", selected==4);
isLensInit
? displayParam(0, 2, "FOCL LEN", focalLength*2.5, 2, 0, selected==5)
: displayParam(0, 2, "FOCL LEN", "ERR", selected==5);
displayParam(1, 2, "BAT VOLT", batVoltage(), 3, 1, selected==6);
}
void displayParam(int x, int y, char* paramName, float param, int paramWidth, int paramPrec, boolean selected) {
int margin = 2;
int screenX = x*(TFT_WIDTH/2);
int screenY = y*(TFT_HEIGHT/3);
tft.fillRect(screenX, screenY, (TFT_WIDTH/2), (TFT_HEIGHT/3), TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
if (selected) {
tft.fillRect(screenX, screenY, (TFT_WIDTH/2), (TFT_HEIGHT/3), TFT_MAGENTA);
tft.setTextColor(TFT_WHITE, TFT_MAGENTA);
}
tft.drawString(paramName, screenX+margin, screenY, 2);
char buf[10];
dtostrf(param, paramWidth, paramPrec, buf);
tft.drawString(buf, screenX+margin, screenY+18, 6);
}
void displayParam(int x, int y, char* paramName, const char* param, boolean selected) {
int margin = 6;
int screenX = x*(TFT_WIDTH/2);
int screenY = y*(TFT_HEIGHT/3);
tft.fillRect(screenX, screenY, (TFT_WIDTH/2), (TFT_HEIGHT/3), TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
if (selected) {
tft.fillRect(screenX, screenY, (TFT_WIDTH/2), (TFT_HEIGHT/3), TFT_MAGENTA);
tft.setTextColor(TFT_WHITE, TFT_MAGENTA);
}
tft.drawString(paramName, screenX+margin, screenY, 2);
tft.drawString(param, screenX+margin, screenY+18+10, 4);
}
void readSensor() {
int value = digitalRead(SENSOR_PIN);
//Serial.println(value);
if (sensorIsLow && value == HIGH) {
sensorIsLow = false;
}
else if (!sensorIsLow && value == LOW) {
sensorIsLow = true;
sprocketCount++;
}
}
uint8_t charToHex(uint8_t val) {
if (val >= 48 && val <= 57) {
return val - 48;
}
else {
return val - 87;
}
}
uint8_t sendData(uint16_t dataToSend) {
uint8_t result;
lens_spi->beginTransaction(spiSettings);
result = lens_spi->transfer(dataToSend);
lens_spi->endTransaction();
delay(1);
Serial.print("sent: ");
Serial.print(dataToSend, HEX);
Serial.print(" received: ");
Serial.println(result, HEX);
return result;
}
int lastPushTime = 0;
boolean debounce() {
int now = millis();
int timeGap = now - lastPushTime;
lastPushTime = now;
return timeGap > 5;
}