development environment
Operating system: ubuntu20 four point three
Hardware: Intel J1900 integrated industrial control screen
Development environment: QT6 twenty-two
preface
In embedded program development, code is needed to complete the brightness control of the display screen. For example, when the user does not operate for a period of time, the backlight brightness is reduced, and when the key is pressed, the backlight brightness is increased. There are two methods to adjust the backlight brightness under uBuntu. SeeTwo methods of setting uBuntu backlight brightness in console command mode
1, Backlight control scheme selection
The backlight brightness is controlled by writing the / sys/class/backlight/xxx/brightness file. The backlight brightness is consistent with the brightness scroll bar on the uBuntu desktop, which is very intuitive. Therefore, this method is selected to complete the program code.
Due to the permission of / sys/class/backlight/xxx/brightness, ordinary users need to execute
sudo sh -c 'echo 5000 > brightness'
2, Modify the sudoers file and add permissions to execute sudo without entering a password
To use sudo, you need to enter a password. It is inconvenient to use Qt to complete the password input, so you need to modify the sudoers file. Users running the program do not need to enter a password when using sudo
The sudoers file is located in the / etc directory
Switch to the etc directory and view the name and group of the current user
developer@ubuntu:/home$ cd /etc developer@ubuntu:/etc$ whoami developer developer@ubuntu:/etc$ groups developer developer : developer adm cdrom sudo dip plugdev lpadmin lxd sambashare developer@ubuntu:/etc$
You can see that the current user name is developer and the group is developer adm
Add the developer group to the sudoers file and set it to use sudo without entering a password
1: View sudoers file permissions
2: Modify sudoers file permissions to 744
3: Open sudoers files using familiar file editing tools
developer@ubuntu:/etc$ ls -l sudoers -r--r----- 1 root root 755 Jan 12 23:08 sudoers developer@ubuntu:/etc$ sudo chmod 744 sudoers [sudo] password for developer: developer@ubuntu:/etc$ sudo gedit sudoers
Note: if sudo cannot be used due to careless correction of sudoers file content or wrong permission setting, you can enter it on the console
pkexec visudo
Modify the sudoers file, save ctrl+o and exit ctrl+x
4: Add at the end of sudoers file
# Allow members of group developer to execute any command without password %developer ALL=(ALL) NOPASSWD:ALL
%Developer: developer team
First ALL: ALL hosts
ALL in parentheses: ALL users
NOPASSWD: no password required
Last ALL: ALL commands
5: Change sudoers file permissions to 440
developer@ubuntu:/etc$ sudo chmod 440 sudoers developer@ubuntu:/etc$ ls -l sudoers -r--r----- 1 root root 865 Jan 12 23:09 sudoers
At this point, the developer workgroup users have been modified without a password when using sudo
Using apt to test, there is no need to enter a password
developer@ubuntu:/etc$ sudo apt update Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease Hit:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease Hit:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease Hit:4 http://mirrors.aliyun.com/ubuntu focal-proposed InRelease Hit:5 http://mirrors.aliyun.com/ubuntu focal-backports InRelease Reading package lists... Done Building dependency tree Reading state information... Done 7 packages can be upgraded. Run 'apt list --upgradable' to see them. developer@ubuntu:/etc$
3, Qt adjustment of backlight brightness
The UI part is completed using QML, and the backlight is set through the Slider component
function __setBacklightBrightness(brightness) { if (brightness < QmlGlobalParas.minBrightness) { brightness = QmlGlobalParas.minBrightness } CfSystemProxy.setBacklightBrightness(brightness) } __workBrightnessSlider.onValueChanged: { __setBacklightBrightness(__workBrightnessSlider.position) } __idleBrightnessSlider.onValueChanged: { __setBacklightBrightness(__idleBrightnessSlider.position) }
Note: CfSystemProxy is an interface proxy class for QML in our program, which is registered in QML as a singleton for calling
/* Register CfSystemInfo as singleton mode */ qmlRegisterSingletonType<CfSystemProxy>("CfSystemProxy", 1, 0, "CfSystemProxy", CfSystemProxy::getInstance);
CfSysProxy.h correlation
Q_INVOKABLE virtual float getBacklightBrightness() override; Q_INVOKABLE virtual void setBacklightBrightness(const float brightness) override;
CfSysProxy.cpp correlation
float CfSystemProxy::getBacklightBrightness() { return m_systemCtrl->getBacklightBrightness(); } void CfSystemProxy::setBacklightBrightness(const float brightness) { QString file = QCoreApplication::applicationDirPath() + "/system.ini"; QSettings setting(file, QSettings::IniFormat); setting.beginGroup("system"); setting.setValue("Brightness", brightness); setting.endGroup(); m_systemCtrl->setBacklightBrightness(brightness); }
The bottom layer uses C + +. As long as the system function is used, the console command can be called
void linuxCmd(const QString &command) { system(command.toLatin1().data()); } /* * input parameter * brightness: Backlight brightness, range 0.3-1.0 */ void CfLinuxCtrl::setBacklightBrightness(const float brightness) { uint32_t brightnessVal = 0; m_brightness = brightness >= 0.3 ? brightness : 0.3; QDir dir("/sys/class/backlight/intel_backlight"); if (dir.exists()) { if (0 == m_maxBrightness) { QFile maxBrightnessFile(dir.path() + "/max_brightness"); if (maxBrightnessFile.open(QFile::ReadOnly)) { char buf[1024]; qint64 lineLenght = maxBrightnessFile.readLine(buf, sizeof(buf)); if (-1 != lineLenght) { m_maxBrightness = QString(buf).toInt(); } maxBrightnessFile.close(); } } if (m_maxBrightness > 0) { brightnessVal = m_maxBrightness * brightness; QString cmd = "sudo sh -c \'echo " + QString::number(brightnessVal) + " > " + dir.path() + "/brightness\'"; linuxCmd(cmd); } } /* * Use xrandr to set the backlight brightness, discard m_brightness = brightness; QString cmd = "xrandr --output " + m_monitorName + " --brightness " + QString::number(brightness, 'f', 1); linuxCmd(cmd); */ }