Qt Linux - Lesson 003. Checking the battery level of the laptop using libacpi

Qt, Battery, libacpi

On the forum we to talk about checking the battery level of the laptop under Linux using the Qt library. In fact, Qt does not currently provide a class that would allow you to check the battery charge. But such functionality can be implemented with the help of third-party libraries, for example libacpi.

Let's try to create an application that will take at least some data, and it will look like this:

libacpi

The library itself is written in C and has the following functions and data structures of interest:

  • global_t - A global data structure that stores the number of fans, batteries and thermal zones, as well as a structure with a power adapter.
  • init_acpi_batt(global_t *globals) - Initializing battery data
  • init_acpi_acadapt(global_t *globals) - Initializing adapter data
  • init_acpi_fan(global_t *globals) - Initializing fan data
  • init_acpi_thermal(global_t *globals) - Initialization of data on thermal zones
  • read_acpi_batt(int num) - Battery data reading
  • read_acpi_zone(int num, global_t *globals) - Reading of data on thermal zones
  • read_acpi_fan(int num) - Read fan data
  • battery_t - Battery data structure
  • fans_t - Fan data structure
  • thermal_t - Structure of data on the thermal zone
  • battery_t batteries [MAX_ITEMS] - Array of batteries, it stores the data when called read_acpi_batt
  • thermal_t thermals [MAX_ITEMS] - Array of thermal zones, it stores data when called read_acpi_zone
  • fan_t fans [MAX_ITEMS] - Array of fans, it stores data when called read_acpi_fan

Using the Library

To use a library in a project on Qt, you must:

  1. Install the library from the repository:
    sudo apt-get install libacpi-dev
    
  2. Connect the library to the project pro file:
    LIBS += -lacpi
    
  3. Connect the library header file to the source file where it will receive battery charge data:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    #include <libacpi.h>
    
    #ifdef __cplusplus
    }
    #endif

Main application window

The main application window is the Widget class, which is made using a ui file and whose interface is made through a graphical designer. It has ScrollArea, which contains an object for the vertical placement of elements, widgets, which will be created dynamically, depending on what the library could find. And also there is a button for starting the data reading by the library.

widget.h

In the header file, vectors with widgets for batteries, fans and thermal zones are declared. Also, a pointer to the widget with the power adapter is declared. All widgets are custom classes. A little further on is an example of the code of one of the widgets.

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include "batterywidget.h"
#include "adapterwidget.h"
#include "fanwidget.h"
#include "thermalwidget.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QVector<BatteryWidget*> m_butteryWidgetList;    // Vector widget with batteries
    QVector<FanWidget*> m_fanWidgetList;            // Vector of widgets with fans
    QVector<ThermalWidget*> m_thermalWidgetList;    // Vector of widgets with thermal zones
    AdapterWidget* m_adapterWidget;                 // Widget for power adapter is always one
};

#endif // WIDGET_H

widget.cpp

Since the library is written in C, it has a procedural style of code execution. And also there is one little trick. The matter is that in library there are some global variables in which the data are written down. These are arrays with fans, heat zones and batteries.

That is, when you call a function read_acpi_batt(1) , then you write down the data structure battery_t into the cell of the array batteries[1].

Whereas to create a global structure that contains data on the number of all the above listed structures, you need to allocate memory.

#include "widget.h"
#include "ui_widget.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <libacpi.h>

#ifdef __cplusplus
}
#endif

#include <QFileDialog>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    m_adapterWidget(nullptr)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    // We allocate memory for the global data structure of the library
    global_t *global = static_cast<global_t*>(malloc(sizeof(global_t)));

    // Initialize the structure for
    init_acpi_batt(global);     // Batteries
    init_acpi_acadapt(global);  // Power adapter
    init_acpi_fan(global);      // Fans
    init_acpi_thermal(global);  // Thermal zones

    // Battery
    battery_t *binfo;

    // If there were batteries, then we read the data in the loop and add widgets
    for(int i=0; i < global->batt_count; i++)
    {
        read_acpi_batt(i);      // Read the data
        // Which are read into a special array of batteries - it is in the library itself
        binfo = &batteries[i];

        BatteryWidget* butteryWidget;
        // Create a widget
        if (m_butteryWidgetList.count() <= i)
        {
            butteryWidget = new BatteryWidget(this);
            butteryWidget->setTitle(QString(binfo->name));
            m_butteryWidgetList.append(butteryWidget);
            ui->verticalLayout->addWidget(butteryWidget);
        }
        else
        {
            // Or we take it from the list of widgets
            butteryWidget = m_butteryWidgetList.at(i);
        }

        // Set
        butteryWidget->setPercentage(binfo->percentage);        // Percent charge
        butteryWidget->setChargeTime(binfo->charge_time);       // Time to full charge
        butteryWidget->setRemainingTime(binfo->remaining_time); // Time to full discharge

        // Determine the charge status
        switch (binfo->charge_state)
        {
        case C_CHARGE:
            butteryWidget->setChargeState("Charging");
            break;
        case C_DISCHARGE:
            butteryWidget->setChargeState("Discharging");
        case C_CHARGED:
            butteryWidget->setChargeState("Charged");
        case C_NOINFO:
            butteryWidget->setChargeState("No info");
        default:
            butteryWidget->setChargeState("Error");
            break;
        }
    }

    // Adapter
    // Configuring the Widget for the Power Adapter
    adapter_t *ac = &global->adapt;

    if (!m_adapterWidget)
    {
        m_adapterWidget = new AdapterWidget(this);
        ui->verticalLayout->addWidget(m_adapterWidget);
    }

    // Here we check only whether it is connected or not
    switch (ac->ac_state)
    {
    case P_AC:
        m_adapterWidget->setStatus("Runs on AC");
        break;
    case P_BATT:
        m_adapterWidget->setStatus("Runs on battery");
    default:
        m_adapterWidget->setStatus("Error");
        break;
    }

    // Fan
    fan_t *finfo;

    // If there are fans, then we read the data in the loop and add widgets
    for(int i=0; i < global->fan_count; i++)
    {
        read_acpi_fan(i);   // Read the data
        // Which are read into a special array of fans - it is in the library itself
        finfo = &fans[i];
        FanWidget* fanWidget;

        // Create a widget
        if (m_fanWidgetList.count() <= i)
        {
            fanWidget = new FanWidget(this);
            fanWidget->setTitle(QString(finfo->name));
            m_fanWidgetList.append(fanWidget);
            ui->verticalLayout->addWidget(fanWidget);
        }
        else
        {
            // Or we take it from the list of widgets
            fanWidget = m_fanWidgetList.at(i);
        }

        // Set the status, whether it is on or not
        switch (finfo->fan_state)
        {
        case F_ON:
            fanWidget->setStatus("ON");
            break;
        case F_OFF:
            fanWidget->setStatus("OFF");
        default:
            fanWidget->setStatus("Error");
            break;
        }
    }

    // Thermal
    thermal_t *tinfo;

    // If there are thermal zones, then we read the data in the cycle and add widgets
    for(int i=0; i < global->fan_count; i++)
    {
        read_acpi_zone(i, global);  // Read the data
        // Which are read into a special array of thermals - it is in the library itself
        tinfo = &thermals[i];
        ThermalWidget* thermalWidget;

        // Create a widget
        if (m_thermalWidgetList.count() <= i)
        {
            thermalWidget = new ThermalWidget(this);
            thermalWidget->setTitle(QString(tinfo->name));
            m_thermalWidgetList.append(thermalWidget);
            ui->verticalLayout->addWidget(thermalWidget);
        }
        else
        {
            // Or we take it from the list of widgets
            thermalWidget = m_thermalWidgetList.at(i);
        }

        thermalWidget->setTemperature(tinfo->temperature);  // Set the temperature
        thermalWidget->setFrequency(tinfo->frequency);      // And frequency
    }

    free(global);
}

Widgets

The program contains widgets for all the above structures. They serve only for displaying information and dynamic addition. Here is an example widget that displays information about the battery.

batterywidget.h

#ifndef BATTERYWIDGET_H
#define BATTERYWIDGET_H

#include <QWidget>

namespace Ui {
class BatteryWidget;
}

class BatteryWidget : public QWidget
{
    Q_OBJECT

public:
    explicit BatteryWidget(QWidget *parent = 0);
    ~BatteryWidget();

    void setTitle(QString title);               // Battery name (as specified in Linux OS)
    void setPercentage(int percentage);         // Percent charge
    void setChargeTime(int chargeTime);         // Time to full charge in minutes
    void setRemainingTime(int remainingTime);   // Time to full discharge in minutes
    void setChargeState(QString chargeState);   // State

private:
    Ui::BatteryWidget *ui;
};

#endif // BATTERYWIDGET_H

batterywidget.cpp

#include "batterywidget.h"
#include "ui_batterywidget.h"

BatteryWidget::BatteryWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BatteryWidget)
{
    ui->setupUi(this);
}

BatteryWidget::~BatteryWidget()
{
    delete ui;
}

void BatteryWidget::setTitle(QString title)
{
    ui->label_title->setText(title);
}

void BatteryWidget::setPercentage(int percentage)
{
    ui->label_percentage->setText(QString::number(percentage));
}

void BatteryWidget::setChargeTime(int chargeTime)
{
    ui->label_charge_time->setText(QString("%1:%2").arg(chargeTime / 60).arg(chargeTime % 60));
}

void BatteryWidget::setRemainingTime(int remainingTime)
{
    ui->label_remaining_time->setText(QString("%1:%2").arg(remainingTime / 60).arg(remainingTime % 60));
}

void BatteryWidget::setChargeState(QString chargeState)
{
    ui->label_charge_state->setText(chargeState);
}

The result of the library

The result of the library I would call satisfactory, although it determines the percentage of battery charge, as well as the time for a full charge of the battery. But the time until the discharge, she did not determine. And also did not find any fans and thermal zones. I believe that on my laptop model this data is simply not available. Also, there were occasional drops of the application when trying to access certain parameters of structures.

The screenshot that appears at the beginning of the article displays the data that the library could find in my laptop. There, of course, not all the data are given, if you look at the library header file, you will find that the structures provide a richer selection of parameters. But to demonstrate the launch of the library I consider it sufficient.

Try to run the project at home, and if possible, write, if you have a laptop in this notebook will find thermal zones and fans.

Application project

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate

Comments

Only authorized users can post comments.
Please, Log in or Sign up
How to become an author?

Contribute to the evolution of the EVILEG community.

Learn how to become a site author.

Learn it
Donate

Good day, Dear Users!!!

I am Evgenii Legotckoi, developer of EVILEG. And it is my hobby project, which helps to learn programming another programmers and developers

If the site helped you, and you want also support the development of the site, than you can donate by following ways

PayPalYandex.Money
Timeweb

Let me recommend you the excellent hosting on which EVILEG is located.

For many years, Timeweb has been proving his stability.

For projects on Django I recommend VDS hosting

View Hosting Timeweb
MN
May 25, 2020, 11:33 a.m.
Mitja Nagibin

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
f
May 25, 2020, 5:05 a.m.
falcon

C++ - Test 001. The first program and data types

  • Result:66points,
  • Rating points-1
jm
May 25, 2020, 3:30 a.m.
just maks

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
Last comments
May 26, 2020, 6:51 a.m.
Evgenij Legotskoj

Qt/C++ - Lesson 004. QSqlTableModel – How to present the table from database?

У вас база данных не открылась Исправьте путь к базе данных на свой корректный в следующих методах void DataBase::connectToDataBase() bool DataBase::openDataBase()
T1
T1
May 26, 2020, 6:22 a.m.
Tima 1

Qt/C++ - Lesson 004. QSqlTableModel – How to present the table from database?

полностью повторил структору проекта. В форму дабавил tableView. Но при запуске получаю форму только с пустым tableView. Можете подсказать в чем пробелма?
May 26, 2020, 6:02 a.m.
Evgenij Legotskoj

Qt/C++ - Lesson 004. QSqlTableModel – How to present the table from database?

Потому что это файл который нужно создать, а не библиотека. В статье есть содержание этого файла. Добавляйте в проект. Копируйте содержимое из статьи.
T1
May 26, 2020, 6 a.m.
Tima 1

Qt/C++ - Lesson 004. QSqlTableModel – How to present the table from database?

не удается подключиить библеотеку include "database.h" выдает ошибку. Можете помочь?
Now discuss on the forum
May 26, 2020, 5:16 a.m.
BlinCT

Отсутствие драйвера SQLite в пакете Qt 4 на Linux

Вот честно непонимаю почему до сих пор используют qt4, там же столько всего отсутствует, много фишек и возможностей нету там. То есть используя такое старье приходится много писать самому а не и…
DK
May 26, 2020, 2:24 a.m.
Dzhon Kofi

Disable autoscroll

такие естественные решения все перепробовал. Получилось вчера так: const int maximumScroll = ui->_samples->verticalScrollBar()->maximum();const int sliderPos = ui->_samp…
May 26, 2020, 12:43 a.m.
Ruslan Polupan

Посоветуйте новичку (базы данных и Qt, что учить)

Без БД сейчас практически никуда. Поэтому SQL надо знать. SQLite самы простой вариант, но имхо лучще начать с бд клиент-сервер. Настроить сервер. Подключаться клиентом. Просто это помогает понят…
EJ
May 25, 2020, 2:42 p.m.
Esteban José María

Компиляция пустого проекта Qt Android

qt 5.12.8 BUILD SUCCESSFUL in 42s 28 actionable tasks: 28 executed Android package built successfully in 68.251 ms. Ну, буду разбираться по-тихоньку. :)
s
May 25, 2020, 1:24 p.m.
sander-007

Использование файлов в памяти (memory file mapping)

Добрый вечер, проблемы работы с файлом Exel нет вообще. Весь смысл в том чтобы не создавать на диске физический файл (требования безопасности), дабы потом не чистить. А так вопрос только в этом …
About
Services
© EVILEG 2015-2020
Recommend hosting TIMEWEB