Merge PR #216: Add support for setting the screen brightness on Raspberry Pis

This commit is contained in:
Jenkins 2019-07-05 01:25:33 +02:00
commit 4f4bb116a1
8 changed files with 88 additions and 10 deletions

View File

@ -388,9 +388,9 @@ void NymeaConnection::updateActiveBearers()
{ {
NymeaConnection::BearerTypes availableBearerTypes; NymeaConnection::BearerTypes availableBearerTypes;
QList<QNetworkConfiguration> configs = m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active); QList<QNetworkConfiguration> configs = m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active);
qDebug() << "Network configuations:" << configs.count(); // qDebug() << "Network configuations:" << configs.count();
foreach (const QNetworkConfiguration &config, configs) { foreach (const QNetworkConfiguration &config, configs) {
qDebug() << "Active network config:" << config.name() << config.bearerTypeFamily() << config.bearerTypeName(); // qDebug() << "Active network config:" << config.name() << config.bearerTypeFamily() << config.bearerTypeName();
// NOTE: iOS doesn't correctly report bearer types. It'll be Unknown all the time. Let's hardcode it to WiFi for that... // NOTE: iOS doesn't correctly report bearer types. It'll be Unknown all the time. Let's hardcode it to WiFi for that...
#if defined(Q_OS_IOS) #if defined(Q_OS_IOS)

View File

@ -19,3 +19,13 @@ void PlatformHelper::setScreenTimeout(int screenTimeout)
{ {
Q_UNUSED(screenTimeout) Q_UNUSED(screenTimeout)
} }
int PlatformHelper::screenBrightness() const
{
return 0;
}
void PlatformHelper::setScreenBrightness(int percent)
{
Q_UNUSED(percent)
}

View File

@ -14,6 +14,7 @@ class PlatformHelper : public QObject
Q_PROPERTY(QString machineHostname READ machineHostname CONSTANT) Q_PROPERTY(QString machineHostname READ machineHostname CONSTANT)
Q_PROPERTY(bool canControlScreen READ canControlScreen CONSTANT) Q_PROPERTY(bool canControlScreen READ canControlScreen CONSTANT)
Q_PROPERTY(int screenTimeout READ screenTimeout WRITE setScreenTimeout NOTIFY screenTimeoutChanged) Q_PROPERTY(int screenTimeout READ screenTimeout WRITE setScreenTimeout NOTIFY screenTimeoutChanged)
Q_PROPERTY(int screenBrightness READ screenBrightness WRITE setScreenBrightness NOTIFY screenBrightnessChanged)
public: public:
enum HapticsFeedback { enum HapticsFeedback {
@ -40,12 +41,16 @@ public:
virtual bool canControlScreen() const; virtual bool canControlScreen() const;
virtual int screenTimeout() const; virtual int screenTimeout() const;
virtual void setScreenTimeout(int screenTimeout); virtual void setScreenTimeout(int screenTimeout);
virtual int screenBrightness() const;
virtual void setScreenBrightness(int percent);
Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbackType) = 0; Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbackType) = 0;
signals: signals:
void permissionsRequestFinished(); void permissionsRequestFinished();
void screenTimeoutChanged(); void screenTimeoutChanged();
void screenBrightnessChanged();
}; };
#endif // PLATFORMHELPER_H #endif // PLATFORMHELPER_H

View File

@ -67,6 +67,19 @@ void PlatformHelperGeneric::setScreenTimeout(int timeout)
} }
} }
int PlatformHelperGeneric::screenBrightness() const
{
return m_piHelper->screenBrightness();
}
void PlatformHelperGeneric::setScreenBrightness(int percent)
{
if (m_piHelper->screenBrightness() != percent) {
m_piHelper->setScreenBrightness(percent);
emit screenTimeoutChanged();
}
}
void PlatformHelperGeneric::vibrate(PlatformHelper::HapticsFeedback feedbyckType) void PlatformHelperGeneric::vibrate(PlatformHelper::HapticsFeedback feedbyckType)
{ {
Q_UNUSED(feedbyckType) Q_UNUSED(feedbyckType)

View File

@ -25,6 +25,8 @@ public:
virtual bool canControlScreen() const override; virtual bool canControlScreen() const override;
virtual int screenTimeout() const override; virtual int screenTimeout() const override;
virtual void setScreenTimeout(int timeout) override; virtual void setScreenTimeout(int timeout) override;
virtual int screenBrightness() const override;
virtual void setScreenBrightness(int percent) override;
Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbyckType) override; Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbyckType) override;

View File

@ -7,8 +7,8 @@
RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent) RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
{ {
m_sysFsFile.setFileName("/sys/class/backlight/rpi_backlight/bl_power"); m_powerFile.setFileName("/sys/class/backlight/rpi_backlight/bl_power");
bool available = m_sysFsFile.open(QFile::ReadWrite | QFile::Text); bool available = m_powerFile.open(QFile::ReadWrite | QFile::Text);
if (!available) { if (!available) {
return; return;
@ -16,6 +16,14 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
qDebug() << "Raspberry Pi detected. Enabling backlight control"; qDebug() << "Raspberry Pi detected. Enabling backlight control";
m_brightnessFile.setFileName("/sys/class/backlight/rpi_backlight/brightness");
if (!m_brightnessFile.open(QFile::ReadWrite | QFile::Text)) {
qWarning() << "Failed to open brightness file";
}
QByteArray currentBrightness = m_brightnessFile.readLine();
m_currentBrightness = currentBrightness.trimmed().toInt() * 100 / 255;
qDebug() << "Current brightness is:" << currentBrightness << m_currentBrightness;
screenOn(); screenOn();
foreach (QWindow *w, qApp->topLevelWindows()) { foreach (QWindow *w, qApp->topLevelWindows()) {
@ -33,7 +41,7 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
bool RaspberryPiHelper::active() const bool RaspberryPiHelper::active() const
{ {
return m_sysFsFile.isOpen(); return m_powerFile.isOpen();
} }
int RaspberryPiHelper::screenTimeout() const int RaspberryPiHelper::screenTimeout() const
@ -53,6 +61,18 @@ void RaspberryPiHelper::setScreenTimeout(int timeout)
} }
} }
int RaspberryPiHelper::screenBrightness() const
{
return m_currentBrightness;
}
void RaspberryPiHelper::setScreenBrightness(int percent)
{
m_currentBrightness = percent;
m_brightnessFile.write(QString("%1\n").arg(percent * 255 / 100).toUtf8());
m_brightnessFile.flush();
}
bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event) bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event)
{ {
if (m_screenOffTimer.interval() == 0) { if (m_screenOffTimer.interval() == 0) {
@ -88,8 +108,8 @@ bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event)
void RaspberryPiHelper::screenOn() void RaspberryPiHelper::screenOn()
{ {
qDebug() << "Turning screen on"; qDebug() << "Turning screen on";
int ret = m_sysFsFile.write("0\n"); int ret = m_powerFile.write("0\n");
m_sysFsFile.flush(); m_powerFile.flush();
if (ret < 0) { if (ret < 0) {
qWarning() << "Failed to power on screen"; qWarning() << "Failed to power on screen";
} }
@ -98,8 +118,8 @@ void RaspberryPiHelper::screenOn()
void RaspberryPiHelper::screenOff() void RaspberryPiHelper::screenOff()
{ {
qDebug() << "Turning screen off"; qDebug() << "Turning screen off";
int ret = m_sysFsFile.write("1\n"); int ret = m_powerFile.write("1\n");
m_sysFsFile.flush(); m_powerFile.flush();
if (ret < 0) { if (ret < 0) {
qWarning() << "Failed to power off screen"; qWarning() << "Failed to power off screen";
} }

View File

@ -15,6 +15,9 @@ public:
int screenTimeout() const; int screenTimeout() const;
void setScreenTimeout(int timeout); void setScreenTimeout(int timeout);
int screenBrightness() const;
void setScreenBrightness(int percent);
bool eventFilter(QObject *watched, QEvent *event) override; bool eventFilter(QObject *watched, QEvent *event) override;
private slots: private slots:
@ -23,7 +26,10 @@ private slots:
private: private:
QTimer m_screenOffTimer; QTimer m_screenOffTimer;
QFile m_sysFsFile; QFile m_powerFile;
QFile m_brightnessFile;
int m_currentBrightness = 255;
}; };
#endif // RASPBERRYPIHELPER_H #endif // RASPBERRYPIHELPER_H

View File

@ -89,6 +89,7 @@ Page {
checked: settings.showConnectionTabs checked: settings.showConnectionTabs
onClicked: settings.showConnectionTabs = checked onClicked: settings.showConnectionTabs = checked
} }
CheckDelegate { CheckDelegate {
id: screenOffCheck id: screenOffCheck
Layout.fillWidth: true Layout.fillWidth: true
@ -97,6 +98,7 @@ Page {
checked: PlatformHelper.screenTimeout > 0 checked: PlatformHelper.screenTimeout > 0
onClicked: PlatformHelper.screenTimeout = (checked ? 15000 : 0) onClicked: PlatformHelper.screenTimeout = (checked ? 15000 : 0)
} }
ItemDelegate { ItemDelegate {
Layout.fillWidth: true Layout.fillWidth: true
Layout.preferredHeight: screenOffCheck.height Layout.preferredHeight: screenOffCheck.height
@ -118,6 +120,26 @@ Page {
} }
} }
} }
ItemDelegate {
Layout.fillWidth: true
visible: PlatformHelper.canControlScreen
topPadding: 0
contentItem: RowLayout {
Label {
Layout.fillWidth: true
text: qsTr("Screen brightness")
}
Slider {
Layout.fillWidth: true
value: PlatformHelper.screenBrightness
onMoved: PlatformHelper.screenBrightness = value
from: 0
to: 100
stepSize: 1
}
}
}
} }
Component { Component {