Skip to content

Commit fcb3511

Browse files
committed
feat(gpio): Implement camera wake-up GPIO functionality in GPhotoCameraWorker
1 parent 6d2cd33 commit fcb3511

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

qml/Application.qml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ ApplicationWindow {
8282
collageMenu.printer : printer
8383
libcamera: libcamera
8484

85+
// Set up camera wake-up GPIO when settings change
86+
onSnapshotMenuChanged: {
87+
if (snapshotMenu && snapshotMenu.cameraRenderer) {
88+
var cameraDevice = snapshotMenu.cameraRenderer.camera
89+
if (cameraDevice && applicationSettings.gpioCameraWakeupEnabled &&
90+
applicationSettings.gpioCameraWakeupLine >= 0) {
91+
// This would require exposing setCameraWakeupGpio from C++
92+
// For now, this is a placeholder for future implementation
93+
console.log("Camera wake-up GPIO configured: line " + applicationSettings.gpioCameraWakeupLine)
94+
}
95+
}
96+
}
97+
8598
settingsMenu.switchPrinter.onCheckedChanged:
8699
{
87100
applicationSettings.printEnable = settingsMenu.switchPrinter.checked
@@ -209,6 +222,21 @@ ApplicationWindow {
209222
applicationSettings.gpioInvertPwm = settingsMenu.switchInvertPwm.checked
210223
}
211224

225+
settingsMenu.switchCameraWakeup.onCheckedChanged:
226+
{
227+
applicationSettings.gpioCameraWakeupEnabled = settingsMenu.switchCameraWakeup.checked
228+
}
229+
230+
settingsMenu.comboBoxCameraWakeupLine.onCurrentValueChanged:
231+
{
232+
applicationSettings.gpioCameraWakeupLine = Number(settingsMenu.comboBoxCameraWakeupLine.currentValue)
233+
}
234+
235+
settingsMenu.spinBoxCameraWakeupDelay.onValueChanged:
236+
{
237+
applicationSettings.gpioCameraWakeupDelayMs = settingsMenu.spinBoxCameraWakeupDelay.value
238+
}
239+
212240
mainMenu.printerBusy: printer ? printer.busy : false
213241
}
214242

@@ -238,6 +266,9 @@ ApplicationWindow {
238266
property int gpioLedBrightnessLine: 18
239267
property int gpioPwmFrequency: 1000
240268
property bool gpioInvertPwm: true
269+
property bool gpioCameraWakeupEnabled: false
270+
property int gpioCameraWakeupLine: -1
271+
property int gpioCameraWakeupDelayMs: 100
241272

242273
Component.onCompleted:
243274
{
@@ -257,6 +288,8 @@ ApplicationWindow {
257288
// GPIO settings
258289
flow.settingsMenu.switchEnableGpio.checked = gpioEnabled
259290
flow.settingsMenu.switchInvertPwm.checked = gpioInvertPwm
291+
flow.settingsMenu.switchCameraWakeup.checked = gpioCameraWakeupEnabled
292+
flow.settingsMenu.spinBoxCameraWakeupDelay.value = gpioCameraWakeupDelayMs
260293
}
261294

262295
onPrinterNameChanged:

qml/SettingsMenu.qml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ SettingsMenuForm {
135135
if (brightnessIdx >= 0)
136136
comboBoxLedBrightnessLine.currentIndex = brightnessIdx
137137

138+
// Camera wake-up line
139+
var wakeupIdx = comboBoxCameraWakeupLine.indexOfValue(applicationSettings.gpioCameraWakeupLine)
140+
if (wakeupIdx >= 0)
141+
comboBoxCameraWakeupLine.currentIndex = wakeupIdx
142+
138143
// PWM frequency
139144
spinBoxPwmFrequency.value = applicationSettings.gpioPwmFrequency
140145

@@ -148,6 +153,7 @@ SettingsMenuForm {
148153
var lines = GPIO.availableLines(chipPath)
149154
comboBoxLedEnableLine.model = lines
150155
comboBoxLedBrightnessLine.model = lines
156+
comboBoxCameraWakeupLine.model = lines
151157
}
152158

153159
comboBoxGpioChip.onCurrentValueChanged: {

qml/SettingsMenuForm.ui.qml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Item {
3737
property alias comboBoxLedBrightnessLine: comboBoxLedBrightnessLine
3838
property alias spinBoxPwmFrequency: spinBoxPwmFrequency
3939
property alias switchInvertPwm: switchInvertPwm
40+
property alias switchCameraWakeup: switchCameraWakeup
41+
property alias comboBoxCameraWakeupLine: comboBoxCameraWakeupLine
42+
property alias spinBoxCameraWakeupDelay: spinBoxCameraWakeupDelay
4043
property var libcamera
4144

4245
ColumnLayout {
@@ -621,6 +624,62 @@ Item {
621624
id: switchInvertPwm
622625
}
623626
}
627+
628+
ToolSeparator {
629+
visible: switchEnableGpio.checked
630+
orientation: Qt.Horizontal
631+
Layout.fillWidth: true
632+
}
633+
634+
RowLayout {
635+
visible: switchEnableGpio.checked
636+
spacing: 10
637+
Label {
638+
text: qsTr("Enable Camera Wake-up:")
639+
}
640+
Item {
641+
Layout.fillWidth: true
642+
}
643+
Switch {
644+
id: switchCameraWakeup
645+
}
646+
}
647+
648+
RowLayout {
649+
visible: switchEnableGpio.checked && switchCameraWakeup.checked
650+
spacing: 10
651+
Label {
652+
text: qsTr("Camera Wake-up Line:")
653+
}
654+
Item {
655+
Layout.fillWidth: true
656+
}
657+
ComboBox {
658+
id: comboBoxCameraWakeupLine
659+
Layout.preferredWidth: 300
660+
textRole: "text"
661+
valueRole: "value"
662+
}
663+
}
664+
665+
RowLayout {
666+
visible: switchEnableGpio.checked && switchCameraWakeup.checked
667+
spacing: 10
668+
Label {
669+
text: qsTr("Wake-up Delay (ms):")
670+
}
671+
Item {
672+
Layout.fillWidth: true
673+
}
674+
SpinBox {
675+
id: spinBoxCameraWakeupDelay
676+
from: 0
677+
to: 5000
678+
stepSize: 10
679+
value: 100
680+
editable: true
681+
}
682+
}
624683
}
625684
}
626685
}

src/gphotocamera.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
#include <QDebug>
33
#include <QString>
44
#include <QVideoFrame>
5+
#include <QCoreApplication>
6+
#include <QSettings>
57
#include <gphoto2/gphoto2-camera.h>
68
#include <gphoto2/gphoto2-context.h>
79
#include <gphoto2/gphoto2-list.h>
810
#include <gphoto2/gphoto2-port.h>
11+
#include <thread>
12+
#include <chrono>
913

1014
namespace {
1115
constexpr auto capturingFailLimit = 10;
@@ -85,10 +89,60 @@ GPhotoCameraWorker::GPhotoCameraWorker()
8589

8690
mKeepAliveTimer.setInterval(1000 * 60); // Check every minute
8791
mKeepAliveTimer.setSingleShot(true);
92+
93+
// Trigger camera wake-up GPIO on startup before camera enumeration
94+
triggerCameraWakeup();
8895
}
8996
GPhotoCameraWorker::~GPhotoCameraWorker() {}
9097

98+
void GPhotoCameraWorker::triggerCameraWakeup() {
99+
// Read GPIO settings from QSettings
100+
QSettings settings;
101+
102+
bool gpioEnabled = settings.value("gpioEnabled", false).toBool();
103+
bool cameraWakeupEnabled = settings.value("gpioCameraWakeupEnabled", false).toBool();
104+
105+
if (!gpioEnabled || !cameraWakeupEnabled) {
106+
return;
107+
}
108+
109+
QString gpioChip = settings.value("gpioChip", "/dev/gpiochip0").toString();
110+
int wakeupLine = settings.value("gpioCameraWakeupLine", 17).toInt();
111+
int delayMs = settings.value("gpioCameraWakeupDelayMs", 100).toInt();
112+
113+
qDebug() << "Triggering camera wake-up GPIO on" << gpioChip << "line" << wakeupLine;
114+
115+
try {
116+
// Create GPIO instance for wake-up
117+
mCameraWakeupGpio = std::make_unique<GPIO>();
118+
mCameraWakeupGpio->setChipPath(gpioChip);
119+
mCameraWakeupGpio->setLine(wakeupLine);
120+
mCameraWakeupGpio->setMode(GPIO::Output);
121+
mCameraWakeupGpio->setEnabled(true);
122+
123+
// Trigger GPIO (set high)
124+
mCameraWakeupGpio->setValue(1.0);
125+
126+
// Wait for delay
127+
if (delayMs > 0) {
128+
std::this_thread::sleep_for(std::chrono::milliseconds(delayMs));
129+
}
130+
131+
// Release GPIO (set low)
132+
mCameraWakeupGpio->setValue(0.0);
133+
mCameraWakeupGpio->setEnabled(false);
134+
mCameraWakeupGpio.reset();
135+
136+
qDebug() << "Camera wake-up GPIO trigger completed";
137+
} catch (const std::exception &e) {
138+
qWarning() << "Failed to trigger camera wake-up GPIO:" << e.what();
139+
}
140+
}
141+
91142
void GPhotoCameraWorker::startCamera(const QString &cameraName) {
143+
// Trigger camera wake-up GPIO before starting camera
144+
triggerCameraWakeup();
145+
92146
if (mCameraStarted) {
93147
stopCamera();
94148
}

src/gphotocamera.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <gphoto2/gphoto2-port.h>
1515
#include <gphoto2/gphoto2-camera.h>
1616

17+
#include "gpio.h"
18+
1719

1820
class GPhotoCameraWorker;
1921

@@ -85,10 +87,14 @@ public slots:
8587
CameraFilePtr mPreviewFile;
8688
bool mCameraStarted = false;
8789
uint32_t mCapturingFailCount = 0;
90+
91+
// GPIO wake-up members
92+
std::unique_ptr<GPIO> mCameraWakeupGpio;
8893

8994
void waitForOperationCompleted();
9095
QVariant parameter(const QString &name);
9196
bool setParameter(const QString &name, const QVariant &value);
97+
void triggerCameraWakeup();
9298
protected slots:
9399
// check and set capture parameters to keep camera alive
94100
void checkCaptureParameter();

0 commit comments

Comments
 (0)