Add support for setting the screen brightness on Raspberry Pis

This commit is contained in:
Michael Zanetti 2019-07-01 15:37:23 +02:00
parent 58d4be66f5
commit f01e7ca627
8 changed files with 88 additions and 10 deletions

View File

@ -388,9 +388,9 @@ void NymeaConnection::updateActiveBearers()
{
NymeaConnection::BearerTypes availableBearerTypes;
QList<QNetworkConfiguration> configs = m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active);
qDebug() << "Network configuations:" << configs.count();
// qDebug() << "Network configuations:" << configs.count();
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...
#if defined(Q_OS_IOS)

View File

@ -19,3 +19,13 @@ void PlatformHelper::setScreenTimeout(int 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(bool canControlScreen READ canControlScreen CONSTANT)
Q_PROPERTY(int screenTimeout READ screenTimeout WRITE setScreenTimeout NOTIFY screenTimeoutChanged)
Q_PROPERTY(int screenBrightness READ screenBrightness WRITE setScreenBrightness NOTIFY screenBrightnessChanged)
public:
enum HapticsFeedback {
@ -40,12 +41,16 @@ public:
virtual bool canControlScreen() const;
virtual int screenTimeout() const;
virtual void setScreenTimeout(int screenTimeout);
virtual int screenBrightness() const;
virtual void setScreenBrightness(int percent);
Q_INVOKABLE virtual void vibrate(HapticsFeedback feedbackType) = 0;
signals:
void permissionsRequestFinished();
void screenTimeoutChanged();
void screenBrightnessChanged();
};
#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)
{
Q_UNUSED(feedbyckType)

View File

@ -25,6 +25,8 @@ public:
virtual bool canControlScreen() const override;
virtual int screenTimeout() const 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;

View File

@ -7,8 +7,8 @@
RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
{
m_sysFsFile.setFileName("/sys/class/backlight/rpi_backlight/bl_power");
bool available = m_sysFsFile.open(QFile::ReadWrite | QFile::Text);
m_powerFile.setFileName("/sys/class/backlight/rpi_backlight/bl_power");
bool available = m_powerFile.open(QFile::ReadWrite | QFile::Text);
if (!available) {
return;
@ -16,6 +16,14 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
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();
foreach (QWindow *w, qApp->topLevelWindows()) {
@ -33,7 +41,7 @@ RaspberryPiHelper::RaspberryPiHelper(QObject *parent) : QObject(parent)
bool RaspberryPiHelper::active() const
{
return m_sysFsFile.isOpen();
return m_powerFile.isOpen();
}
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)
{
if (m_screenOffTimer.interval() == 0) {
@ -88,8 +108,8 @@ bool RaspberryPiHelper::eventFilter(QObject *watched, QEvent *event)
void RaspberryPiHelper::screenOn()
{
qDebug() << "Turning screen on";
int ret = m_sysFsFile.write("0\n");
m_sysFsFile.flush();
int ret = m_powerFile.write("0\n");
m_powerFile.flush();
if (ret < 0) {
qWarning() << "Failed to power on screen";
}
@ -98,8 +118,8 @@ void RaspberryPiHelper::screenOn()
void RaspberryPiHelper::screenOff()
{
qDebug() << "Turning screen off";
int ret = m_sysFsFile.write("1\n");
m_sysFsFile.flush();
int ret = m_powerFile.write("1\n");
m_powerFile.flush();
if (ret < 0) {
qWarning() << "Failed to power off screen";
}

View File

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

View File

@ -89,6 +89,7 @@ Page {
checked: settings.showConnectionTabs
onClicked: settings.showConnectionTabs = checked
}
CheckDelegate {
id: screenOffCheck
Layout.fillWidth: true
@ -97,6 +98,7 @@ Page {
checked: PlatformHelper.screenTimeout > 0
onClicked: PlatformHelper.screenTimeout = (checked ? 15000 : 0)
}
ItemDelegate {
Layout.fillWidth: true
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 {