// INCLUDES
#include <canoneos.h>
#include <usbhub.h>
#include <Bounce.h>
//DEFINES ENCODER
#define encoder0PinA 3 // Fokus, Digitalport 3
#define encoder0PinB 2 // Fokus, Digitalport 2
#define encoder1PinA A3 // ISO, Analogport 3
#define encoder1PinB A2 // ISO, Analogport 2
#define encoder2PinA A0 // Blende, Analogport 0
#define encoder2PinB A1 // Blende, Analogport 1
#define encoder3PinA A4 // Verschlußzeit, Analogport 4 ================
#define encoder3PinB A5 // Verschlußzeit, Analogport 5 ========================
//DEFINES SWITCHE/BUTTONS
#define sw0Pin 4 // left switch -zoom5x, Digitalport 4
//#define sw1Pin 5 // right switch, Digitalport 5, ohne funktion
#define but0Pin 6 // red button (Record), Digitalport 6
//#define but1Pin 7 // green button, Digiport 7, ohne unktion
//SET (grundstellungen)
volatile int encoder0Pos = 0;
volatile int encoder1Pos = 0;
volatile int encoder2Pos = 0;
volatile int encoder3Pos = 0; //==================
volatile int sw0Pos = 0;
//volatile int sw1Pos = 0;
volatile int but0Pos = 0;
//volatile int but1Pos = 0;
volatile int ffstep = 2; // Schrittgröße Fokus vorwärts / orig. war 2
volatile int bfstep = 0x8002; // Schrittgröße Fokus rückw. / orig. war 0x8002;
volatile int iso_mode = 0;
volatile int aperture_mode = 0;
volatile char recording_mode = 0;
volatile char zoom_mode = 0;
volatile char shutterspeed_mode = 0; //===================
//BOUNCE schalter und taster
Bounce bouncer_but0 = Bounce( but0Pin,10 );
//Bounce bouncer_but1 = Bounce( but1Pin,10 );
Bounce bouncer_sw0 = Bounce( sw0Pin,10 );
//Bounce bouncer_sw1 = Bounce( sw1Pin,10 );
int encoder0PinALast = LOW;
int encoder1PinALast = LOW;
int encoder2PinALast = LOW;
int encoder3PinALast = LOW; //====================
int n = LOW;
// INIT ENCODER/SWITCH/BUTTON
void initEncoder() {
// init drehencoder
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
pinMode(encoder1PinA, INPUT);
digitalWrite(encoder1PinA, HIGH); // turn on pullup resistor
pinMode(encoder1PinB, INPUT);
digitalWrite(encoder1PinB, HIGH); // turn on pullup resistor
pinMode(encoder2PinA, INPUT);
digitalWrite(encoder2PinA, HIGH); // turn on pullup resistor
pinMode(encoder2PinB, INPUT);
digitalWrite(encoder2PinB, HIGH); // turn on pullup resistor
//================================================
pinMode(encoder3PinA, INPUT);
digitalWrite(encoder3PinA, HIGH); // turn on pullup resistor
pinMode(encoder3PinB, INPUT);
digitalWrite(encoder3PinB, HIGH); // turn on pullup resistor
//================================================
// Init taster/buttons
pinMode(sw0Pin, INPUT);
digitalWrite(sw0Pin, HIGH); // turn on pullup resistor
// pinMode(sw1Pin, INPUT);
// digitalWrite(sw1Pin, HIGH); // turn on pullup resistor
pinMode(but0Pin, INPUT);
digitalWrite(but0Pin, HIGH); // turn on pullup resistor
// pinMode(but1Pin, INPUT);
// digitalWrite(but1Pin, HIGH); // turn on pullup resistor
encoder0PinALast = digitalRead(encoder0PinA);
encoder1PinALast = digitalRead(encoder1PinA);
encoder2PinALast = digitalRead(encoder2PinA);
encoder3PinALast = digitalRead(encoder3PinA); //===============================
}
// READ ENCODER
void readEncoder() {
static int8_t last[5]; //================== orig. war 4
int8_t neu = 0;
int8_t diff = 0;
// drehencoder funktion from Peter Dannegger
http://www.mikrocontroller.net/topic/112603
// Encoder 0
neu = 0;
if (digitalRead(encoder0PinA) == HIGH) {
neu = 3;
}
if (digitalRead(encoder0PinB) == 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
encoder0Pos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Encoder 1
neu = 0;
if (digitalRead(encoder1PinA) == HIGH) {
neu = 3;
}
if (digitalRead(encoder1PinB) == 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
encoder1Pos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
// Encoder 2
neu = 0;
if (digitalRead(encoder2PinA) == HIGH) {
neu = 3;
}
if (digitalRead(encoder2PinB) == 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
encoder2Pos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
//================================================================================
// Encoder 3
neu = 0;
if (digitalRead(encoder3PinA) == HIGH) {
neu = 3;
}
if (digitalRead(encoder3PinB) == 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
encoder3Pos += (diff & 2) - 1; // bit 1 = direction (+/-)
}
//==============================================================================
// read but0 with arduion debounce library
bouncer_but0.update();
int value_but0 = bouncer_but0.read();
if ( value_but0 == LOW) {
but0Pos = 1;
}
else {
but0Pos = 0;
}
// read but1 with arduion debounce library
/*
bouncer_but1.update();
int value_but1 = bouncer_but1.read();
if ( value_but1 == LOW) {
but1Pos = 1;
}
else {
but1Pos = 0;
}
*/
// read sw0 with arduion debounce library
bouncer_sw0.update();
int value_sw0 = bouncer_sw0.read();
if ( value_sw0 == LOW) {
sw0Pos = 1;
}
else {
sw0Pos = 0;
}
// read sw1 with arduion debounce library
/*
bouncer_sw1.update();
int value_sw1 = bouncer_sw1.read();
if ( value_sw1 == LOW) {
sw1Pos = 1;
}
else {
sw1Pos = 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();
}
void CamStateHandlers::OnDeviceDisconnectedState(PTP *ptp) {
if (stateConnected == stConnected || stateConnected ==
stInitial) {
stateConnected = stDisconnected;
Notify(PSTR("Camera disconnected.\r\n"));
}
}
void CamStateHandlers::OnDeviceInitializedState(PTP *ptp) {
if (stateConnected == stDisconnected || stateConnected ==
stInitial) {
stateConnected = stConnected;
encoder0Pos = 0;
encoder1Pos = 0;
encoder2Pos = 0;
encoder3Pos = 0; //==============
}
if (stateConnected == stConnected) {
// Rec-Mode
if (but0Pos > 0) {
if (recording_mode == 0) {
Eos.SetProperty(EOS_DPC_EVFRecordStatus, 4);
delay(100);
but0Pos = 0;
recording_mode = 1;
}
else {
Eos.SetProperty(EOS_DPC_EVFRecordStatus, 0);
delay(100);
but0Pos = 0;
recording_mode = 0;
}
}
// zoom field: 1x, 5x
if (zoom_mode == 0 and sw0Pos == 1) {
((CanonEOS*)ptp)->Zoom(5);
delay(100);
zoom_mode = 1;
ffstep = 1;
bfstep = 0x8001;
}
if (zoom_mode == 1 and sw0Pos == 0) {
((CanonEOS*)ptp)->Zoom(1);
delay(100);
zoom_mode = 0;
ffstep = 2;
bfstep = 0x8002;
}
// ISO auto, 100,160,320,640,1250,2500
// 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}; // orig war 0, 0x48, 0x4D, 0x55, 0x5D, 0x65, 0x6D };
if (encoder1Pos > 0) {
if (iso_mode < 8) {
iso_mode++;
Eos.SetProperty(EOS_DPC_Iso, iso_values[iso_mode]);
}
delay(50);
}
else if (encoder1Pos < 0) {
if (iso_mode > 0) {
iso_mode--;
Eos.SetProperty(EOS_DPC_Iso, iso_values[iso_mode]);
}
delay(50);
}
encoder1Pos = 0;
// orig. BLENDE f1.2,1.4,1.6,1.8,2.0,....f16
// 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 }; // orig war [23] ... 0x0d, 0x10, 0x13, 0x15, 0x18, 0x1b, 0x1d, 0x20, 0x23, 0x25, 0x28, 0x2b, 0x2d, 0x30, 0x33, 0x35, 0x38, 0x3b, 0x3d, 0x40, 0x43, 0x45, 0x48 };
if (encoder2Pos > 0) {
if (aperture_mode < 16) { //orig. war 22
aperture_mode++;
Eos.SetProperty(EOS_DPC_Aperture,
aperture_values[aperture_mode]);
}
delay(50);
}
else if (encoder2Pos < 0) {
if (aperture_mode > 0) {
aperture_mode--;
Eos.SetProperty(EOS_DPC_Aperture,
aperture_values[aperture_mode]);
}
delay(50);
}
encoder2Pos = 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 shutterspeed_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 (encoder3Pos > 0) {
if (shutterspeed_mode < 21) {
shutterspeed_mode++; Serial.println("shutter forward"); // debugging
Eos.SetProperty(EOS_DPC_ShutterSpeed,
shutterspeed_values[shutterspeed_mode]);
}
delay(50);
}
else if (encoder3Pos < 0) {
if (shutterspeed_mode > 0) {
shutterspeed_mode--;
Eos.SetProperty(EOS_DPC_ShutterSpeed,
shutterspeed_values[shutterspeed_mode]);
}
delay(50);
}
encoder3Pos = 0;
//================================================================
// FOCUS ACTION
while (encoder0Pos != 0) {
if (encoder0Pos > 0) {
encoder0Pos = 0;
Serial.println("Focus forward"); // debugging
Serial.println(ffstep); // debugging
((CanonEOS*)ptp)->MoveFocus(ffstep); // vorwärts
delay(10);
}
else if (encoder0Pos < 0) {
encoder0Pos = 0;
Serial.println("Focus backward"); // debugging
Serial.println(bfstep); // debugging
((CanonEOS*)ptp)->MoveFocus(bfstep); //rückwärts
delay(10);
}
}
encoder0Pos = 0;
}
}