Skip to content

Instantly share code, notes, and snippets.

@JonasVautherin
Created September 29, 2014 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonasVautherin/0665d9f920aecfaa29b9 to your computer and use it in GitHub Desktop.
Save JonasVautherin/0665d9f920aecfaa29b9 to your computer and use it in GitHub Desktop.
#include <gphoto2/gphoto2.h>
#include <iostream>
#include <string.h>
#include <unistd.h>
class CanonTrigger
{
private:
Camera* m_camera;
GPContext* m_context;
public:
CanonTrigger()
{
gp_camera_new(&m_camera);
m_context = createContext();
}
~CanonTrigger()
{
gp_camera_exit(m_camera, m_context);
gp_camera_unref(m_camera);
gp_context_unref(m_context);
}
GPContext* createContext()
{
GPContext* context;
// TODO: see context.c for optional parts (LOGGING)
context = gp_context_new();
return context;
}
// TODO: puts the camera in a busy state (it is not possible to change the oesviewfinder config after this)
// NOTE: gphoto2 has the same problem (with --set-config autofocusdrive=1)
/**
* \brief Runs autofocus.
*
* NOTE: * Only works while in preview mode.
* * Breaks the eosviewfinder anyway...
*/
bool autofocus()
{
// TODO: return false if not in preview mode?
// => save the mode as a member
return toggleConfig("autofocusdrive", "1");
}
bool setPreviewMode(bool enabled)
{
if (enabled)
{
return toggleConfig("eosviewfinder", "1");
}
else
{
return toggleConfig("eosviewfinder", "0");
}
}
bool toggleConfig(const std::string key, const std::string value)
{
CameraWidget* widget = NULL;
CameraWidget* child = NULL;
CameraWidgetType type;
int ret;
int val;
// "Retrieve a configuration window for the camera" (from API documentation)
ret = gp_camera_get_config(m_camera, &widget, m_context);
if (ret < GP_OK)
{
std::cout << " - camera_get_config failed (returned " << ret << ")." << std::endl;
return false;
}
if (!lookupWidget(widget, key, &child))
{
std::cout << " - could not retrieve widget \"" << key << "\"." << std::endl;
return false;
}
// Check that this is a toggle
ret = gp_widget_get_type(child, &type);
if (ret < GP_OK)
{
std::cout << " - gp_widget_get_type failed (returned " << ret << ")." << std::endl;
gp_widget_free(widget);
return false;
}
switch (type)
{
case GP_WIDGET_TOGGLE:
break;
default:
std::cout << " - Widget has bad type: " << type << std::endl;
gp_widget_free(widget);
return false;
}
// Is this really useful?
ret = gp_widget_get_value(child, &val);
if (ret < GP_OK)
{
std::cout << " - Could not get widget value." << std::endl;
gp_widget_free(widget);
return false;
}
else
{
std::cout << " - key \"" << key << "\" has value \"" << val << "\"" << std::endl;
}
std::cout << " - set value of " << key << " to " << value << std::endl;
ret = gp_widget_set_value(child, value.c_str());
if (ret < GP_OK)
{
std::cout << " Could not set widget value to " << value << "..." << std::endl;
gp_widget_free(widget);
return false;
}
ret = gp_camera_set_config(m_camera, widget, m_context);
if (ret < GP_OK)
{
std::cout << " - Could not set config tree to autofocus." << std::endl;
gp_widget_free(widget);
return false;
}
gp_widget_free(widget);
return true;
}
static bool lookupWidget(CameraWidget* widget, const std::string key, CameraWidget** child)
{
int ret;
if (gp_widget_get_child_by_name(widget, key.c_str(), child) < GP_OK
&& gp_widget_get_child_by_label(widget, key.c_str(), child) < GP_OK)
{
return false;
}
return true;
}
};
int main()
{
CanonTrigger canon_trigger;
canon_trigger.setPreviewMode(true);
sleep(2);
canon_trigger.setPreviewMode(false);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment