// Libraries
#include <canoneos.h>
#include <usbhub.h>
#include <Bounce.h>
// Pindefinitionen
// die Encoderpins sind hier vertauscht als im Bauplan, da ich sie falschherum (an den Encodern direkt !) gelötet habe :)
#define blende_encoderPinA A1 // Blende, A0
#define blende_encoderPinB A0 // Blende, A1
#define iso_encoderPinA A3 // ISO, A2
#define iso_encoderPinB A2 // ISO, A3
#define shutter_encoderPinA A5 // Verschlußzeit, A4
#define shutter_encoderPinB A4 // Verschlußzeit, A5
#define fokus_encoderPinA 3 // Fokus, D2
#define fokus_encoderPinB 2 // Fokus, D3
// Buttonpins
#define zoomsw 4 // Zoomschalter (Zoom 5x), D4
#define recled 5 // Record-LED D5
#define recbtn 6 // Roter Drucktaster (Record), D6
#define fokuspeedswI 7 // Fokuspeed-Umschalter I D7
#define fokuspeedswII 8 // Fokuspeed-Umschalter II D8
// Positionen
volatile int fokus_encoderPos = 0;
volatile int iso_encoderPos = 0;
volatile int blende_encoderPos = 0;
volatile int shutter_encoderPos = 0;
volatile int zoomswPos = 0;
volatile int recbtnPos = 0;
volatile int fokuspeedswIPos = 0;
volatile int fokuspeedswIIPos = 0;
// Schrittgrößen für Fokusencoder
volatile int fokus_forward_step = 2; // Fokus vorwärts, geht von 1-3
volatile int fokus_backward_step = 0x8002; // Fokus rückwärts, geht von 0x8001 - 0x8003, .......
// Modis
volatile int iso_mode = 0;
volatile int aperture_mode = 0;
volatile char recording_mode = 0;
volatile char zoom_mode = 0;
volatile char shutter_mode = 0;
volatile int fokuspeedswI_mode = 0;
volatile int fokuspeedswII_mode = 0;
// BOUNCE Umschalter und Taster
Bounce bouncer_recbtn = Bounce( recbtn,10 );
Bounce bouncer_zoomsw = Bounce( zoomsw,10 );
Bounce bouncer_fokuspeedswI = Bounce( fokuspeedswI,10 );
Bounce bouncer_fokuspeedswII = Bounce( fokuspeedswII,10 );
int fokus_encoderPinALast = LOW;
int iso_encoderPinALast = LOW;
int blende_encoderPinALast = LOW;
int shutter_encoderPinALast = LOW; // Verschlußzeit
int n = LOW;
// Initialisierung Encoder, Umschalter, Taster und LED
void initEncoder() {
// Encoder
pinMode(fokus_encoderPinA, INPUT);
digitalWrite(fokus_encoderPinA, HIGH); // turn on pullup resistor
pinMode(fokus_encoderPinB, INPUT);
digitalWrite(fokus_encoderPinB, HIGH);
pinMode(iso_encoderPinA, INPUT);
digitalWrite(iso_encoderPinA, HIGH);
pinMode(iso_encoderPinB, INPUT);
digitalWrite(iso_encoderPinB, HIGH);
pinMode(blende_encoderPinA, INPUT);
digitalWrite(blende_encoderPinA, HIGH);
pinMode(blende_encoderPinB, INPUT);
digitalWrite(blende_encoderPinB, HIGH);
pinMode(shutter_encoderPinA, INPUT);
digitalWrite(shutter_encoderPinA, HIGH);
pinMode(shutter_encoderPinB, INPUT);
digitalWrite(shutter_encoderPinB, HIGH);
// Umschalter und Taster
pinMode(zoomsw, INPUT);
digitalWrite(zoomsw, HIGH);
pinMode(fokuspeedswI, INPUT);
digitalWrite(fokuspeedswI, HIGH);
pinMode(fokuspeedswII, INPUT);
digitalWrite(fokuspeedswII, HIGH);
pinMode(recbtn, INPUT);
digitalWrite(recbtn, HIGH);
// LED
pinMode(recled, OUTPUT);
digitalWrite(recled, LOW);
fokus_encoderPinALast = digitalRead(fokus_encoderPinA);
iso_encoderPinALast = digitalRead(iso_encoderPinA);
blende_encoderPinALast = digitalRead(blende_encoderPinA);
shutter_encoderPinALast = digitalRead(shutter_encoderPinA);
}
// READ ENCODER & BUTTONS
void readEncoder() {
static int8_t last[5]; //bei 4 Encodern muss das [5] sein
int8_t neu = 0;
int8_t diff = 0;
// Encoder - FOKUS
neu = 0;
if (digitalRead(fokus_encoderPinA) == HIGH) {
neu = 3;
}
if (digitalRead(fokus_encoderPinB) == HIGH) {
neu ^= 1; // convert gray to binary
}
diff = last[0] - neu; // difference last - neu
if( diff & 1 ){ // bit 0 = value (1)
last[0] = neu; // store neu as next last
fokus_encoderPos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Encoder - ISO
neu = 0;
if (digitalRead(iso_encoderPinA) == HIGH) {
neu = 3;
}
if (digitalRead(iso_encoderPinB) == HIGH) {
neu ^= 1; // convert gray to binary
}
diff = last[1] - neu; // difference last - neu
if( diff & 1 ){ // bit 0 = value (1)
last[1] = neu; // store neu as next last
iso_encoderPos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Encoder - Blende
neu = 0;
if (digitalRead(blende_encoderPinA) == HIGH) {
neu = 3;
}
if (digitalRead(blende_encoderPinB) == HIGH) {
neu ^= 1; // convert gray to binary
}
diff = last[2] - neu; // difference last - neu
if( diff & 1 ){ // bit 0 = value (1)
last[2] = neu; // store neu as next last
blende_encoderPos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Encoder - Verschlußzeit
neu = 0;
if (digitalRead(shutter_encoderPinA) == HIGH) {
neu = 3;
}
if (digitalRead(shutter_encoderPinB) == HIGH) {
neu ^= 1; // convert gray to binary
}
diff = last[3] - neu; // difference last - neu
if( diff & 1 ){ // bit 0 = value (1)
last[3] = neu; // store neu as next last
shutter_encoderPos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Button Debouncing
// read recbtn with arduion debounce library
bouncer_recbtn.update();
int value_recbtn = bouncer_recbtn.read();
if ( value_recbtn == LOW) {
recbtnPos = 1;
}
else {
recbtnPos = 0;
}
// read zoomsw with arduion debounce library
bouncer_zoomsw.update();
int value_zoomsw = bouncer_zoomsw.read();
if ( value_zoomsw == LOW) {
zoomswPos = 1;
}
else {
zoomswPos = 0;
}
// read fokuspeedswI with arduion debounce library
bouncer_fokuspeedswI.update();
int value_fokuspeedswI = bouncer_fokuspeedswI.read();
if ( value_fokuspeedswI == LOW) {
fokuspeedswIPos = 1;
}
else {
fokuspeedswIPos = 0;
}
// read fokuspeedswII with arduion debounce library
bouncer_fokuspeedswII.update();
int value_fokuspeedswII = bouncer_fokuspeedswII.read();
if ( value_fokuspeedswII == LOW) {
fokuspeedswIIPos = 1;
}
else {
fokuspeedswIIPos = 0;
}
}
class CamStateHandlers :
public EOSStateHandlers
{
enum CamStates {
stInitial, stDisconnected, stConnected };
CamStates stateConnected;
public:
CamStateHandlers() :
stateConnected(stInitial) {
};
virtual void OnDeviceDisconnectedState(PTP *ptp);
virtual void OnDeviceInitializedState(PTP *ptp);
};
CamStateHandlers CamStates;
USB Usb;
USBHub Hub1(&Usb);
CanonEOS Eos(&Usb, &CamStates);
// MAIN-SETUP
void setup() {
//Serial.begin( 115200 );
//Serial.println("Start");
initEncoder();
if (Usb.Init() == -1)
//Serial.println("OSC did not start.");
delay(1000);
TCNT2 = 128;
TCCR2A = 0;
TCCR2B = (1<<CS22);
TIMSK2 |= (1<<TOIE2);
sei();
}
ISR(TIMER2_OVF_vect) {
readEncoder();
}
// MAIN-LOOP
void loop() {
Usb.Task();
}
// Camstatehandler -Disconnected
void CamStateHandlers::OnDeviceDisconnectedState(PTP *ptp) {
if (stateConnected == stConnected || stateConnected == stInitial) {
stateConnected = stDisconnected;
Notify(PSTR("Camera disconnected.\r\n"));
}
}
// Camstatehandler -Modes
void CamStateHandlers::OnDeviceInitializedState(PTP *ptp) {
if (stateConnected == stDisconnected || stateConnected == stInitial) {
stateConnected = stConnected;
fokus_encoderPos = 0;
iso_encoderPos = 0;
blende_encoderPos = 0;
shutter_encoderPos = 0;
}
if (stateConnected == stConnected) {
// Rec-Mode
if (recbtnPos > 0) {
if (recording_mode == 0) {
Eos.SetProperty(EOS_DPC_EVFRecordStatus, 4);
delay(100);
recbtnPos = 0;
recording_mode = 1;
digitalWrite(recled, HIGH);
}
else {
Eos.SetProperty(EOS_DPC_EVFRecordStatus, 0);
delay(100);
recbtnPos = 0;
recording_mode = 0;
digitalWrite(recled, LOW);
}
}
// Zoom-Mode 5x
if (zoom_mode == 0 and zoomswPos == 1) {
((CanonEOS*)ptp)->Zoom(5);
delay(100);
zoom_mode = 1;
//Serial.println("Zoom = AN");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("=========");
//fokus_forward_step = 3;
//fokus_backward_step = 0x8003;
}
// Zoom-Mode 1x
if (zoom_mode == 1 and zoomswPos == 0) {
((CanonEOS*)ptp)->Zoom(1);
delay(100);
zoom_mode = 0;
//Serial.println("Zoom = AUS");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("==========");
//fokus_forward_step = 2;
//fokus_backward_step = 0x8002;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// FokuspeedswI (Umschalter auf I ist feinste Fokusierung)
if (fokuspeedswI_mode == 0 and fokuspeedswIPos == 1) {
//delay(100);
fokuspeedswI_mode = 1;
fokus_forward_step = 1;
fokus_backward_step = 0x8001;
//Serial.println("Fokus-SW I = An");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("===============");
}
if (fokuspeedswI_mode == 1 and fokuspeedswIPos == 0) {
//delay(100);
fokuspeedswI_mode = 0;
fokus_forward_step = 2;
fokus_backward_step = 0x8002;
//Serial.println("Fokus-SW I = Aus");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("================");
}
// FokuspeedswII (Umschalter auf II ist gröbste Fokusierung)
if (fokuspeedswII_mode == 0 and fokuspeedswIIPos == 1) {
//delay(100);
fokuspeedswII_mode = 1;
fokus_forward_step = 3;
fokus_backward_step = 0x8003;
//Serial.println("Fokus-SW II = An");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("===============");
}
if (fokuspeedswII_mode == 1 and fokuspeedswIIPos == 0) {
//delay(100);
fokuspeedswII_mode = 0;
fokus_forward_step = 2;
fokus_backward_step = 0x8002;
//Serial.println("Fokus-SW II = Aus");
//Serial.println(fokus_forward_step);
//Serial.println(fokus_backward_step);
//Serial.println("================");
}
// mein ISO Auto, 100, 200, 400, 800, 1600, 3200, 6400, 12800(H)
uint8_t iso_values[9] = { 0, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78, 0x80 };
if (iso_encoderPos > 0) {
if (iso_mode < 8) {
iso_mode++;
Eos.SetProperty(EOS_DPC_Iso, iso_values[iso_mode]);
}
delay(50);
}
else if (iso_encoderPos < 0) {
if (iso_mode > 0) {
iso_mode--;
Eos.SetProperty(EOS_DPC_Iso, iso_values[iso_mode]);
}
delay(50);
}
iso_encoderPos = 0;
// meine BLENDE 3.5, 4, 4.5, 5, 5.6, 6.3, 7.1, 8, 9, 10, 11, 13, 14, 16, 18, 20, 22
uint8_t aperture_values[17] = {
0x25, 0x28, 0x2b, 0x2d, 0x30, 0x33, 0x35, 0x38, 0x3b, 0x3d, 0x40, 0x43, 0x45, 0x48, 0x4b, 0x4d, 0x4f };
if (blende_encoderPos > 0) {
if (aperture_mode < 16) { //orig. war 22
aperture_mode++;
Eos.SetProperty(EOS_DPC_Aperture,
aperture_values[aperture_mode]);
}
delay(50);
}
else if (blende_encoderPos < 0) {
if (aperture_mode > 0) {
aperture_mode--;
Eos.SetProperty(EOS_DPC_Aperture,
aperture_values[aperture_mode]);
}
delay(50);
}
blende_encoderPos = 0;
// meine Verschlußzeiten 30, 40, 50, 60, 80, 100, 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000
uint8_t shutter_values[22] = {
0x60, 0x63, 0x65, 0x68, 0x6b, 0x6d, 0x70, 0x73, 0x75, 0x78, 0x7b, 0x7d, 0x80, 0x83, 0x85, 0x88, 0x8B, 0x8d, 0x90, 0x93, 0x95, 0x98 };
if (shutter_encoderPos > 0) {
if (shutter_mode < 21) {
shutter_mode++;
Eos.SetProperty(EOS_DPC_ShutterSpeed,
shutter_values[shutter_mode]);
}
delay(50);
}
else if (shutter_encoderPos < 0) {
if (shutter_mode > 0) {
shutter_mode--;
Eos.SetProperty(EOS_DPC_ShutterSpeed,
shutter_values[shutter_mode]);
}
delay(50);
}
shutter_encoderPos = 0;
// Fokus Funktion
while (fokus_encoderPos != 0) {
if (fokus_encoderPos > 0) {
fokus_encoderPos = 0;
((CanonEOS*)ptp)->MoveFocus(fokus_forward_step); // vorwärts
//Serial.println(fokus_forward_step);
delay(50);
}
else if (fokus_encoderPos < 0) {
fokus_encoderPos = 0;
((CanonEOS*)ptp)->MoveFocus(fokus_backward_step); //rückwärts
//Serial.println(fokus_backward_step);
delay(50);
}
}
fokus_encoderPos = 0;
}
}