12

簡単に言えば、CPUの温度をチェックできるアプリケーションを作成しようとしています。libsensors(3)のマニュアルページを使用して、少なくともlibsensors_version番号を取得することができました。今のところ、これが私のコードです:

#include <sensors/sensors.h>
#include "SensorData.h"
#include <string>
#include <sstream>


using namespace std;

SensorData::SensorData()
{
   sensors_init(NULL);
}

SensorData::~SensorData()
{
    sensors_cleanup();
}

string SensorData::GetVersion()
{
    ostringstream Converter;
    Converter<<"Version: "<<libsensors_version;
    return Converter.str();
}

void SensorData::FetchTemp()
{
    //sensors_get_value()
}

マニュアルページで、sensor_get_valueが期待していることを知っています

const sensors_chip_name *name
int subfeat_nr
double *value 

それに渡されます。問題は、それらが正確に何であるかわからないことです。ドキュメント内のほぼすべての関数にこの問題があります。彼らは皆、私が供給方法を知らない漠然としたものを期待しています。

それで、ここに質問の大部分があります:誰かが私が見ることができるこのライブラリの実用的な例を持っていますか?または、少なくとも、これらの関数に必要な値を与える方法を知っている人はいますか?

編集:

このライブラリについては誰もよく知らないようですが、温度を取得する別の方法を知っている人はいますか?

4

3 に答える 3

16

You can find out how to use the API by browsing the source code. The code for the sensors program isn't too complex to follow.

To get you started, here's a quick function that:

  • enumerates all the chips
  • enumerates all their features
  • prints the values of their readable subfeatures

You can just add it to your existing skeleton class as-is.

(This code is for demo purposes only, not tested thoroughly at all.)

void SensorData::FetchTemp()
{
    sensors_chip_name const * cn;
    int c = 0;
    while ((cn = sensors_get_detected_chips(0, &c)) != 0) {
        std::cout << "Chip: " << cn->prefix << "/" << cn->path << std::endl;

        sensors_feature const *feat;
        int f = 0;

        while ((feat = sensors_get_features(cn, &f)) != 0) {
            std::cout << f << ": " << feat->name << std::endl;

            sensors_subfeature const *subf;
            int s = 0;

            while ((subf = sensors_get_all_subfeatures(cn, feat, &s)) != 0) {
                std::cout << f << ":" << s << ":" << subf->name
                          << "/" << subf->number << " = ";
                double val;
                if (subf->flags & SENSORS_MODE_R) {
                    int rc = sensors_get_value(cn, subf->number, &val);
                    if (rc < 0) {
                        std::cout << "err: " << rc;
                    } else {
                        std::cout << val;
                    }
                }
                std::cout << std::endl;
            }
        }
    }
}
于 2011-12-19T17:40:30.337 に答える
2

Gnome パネルの Sensors アプレットは、libsensors (およびその他のバックエンド) で動作します。完全なソースは、Sourceforge ( http://sensors-applet.sourceforge.net/index.php?content=source ) から入手できます。

… 特に、libsensors プラグインはかなり読みやすいように見えます… これは、そのコードへの直接の使用可能な gitweb リンクであると思います: http://sensors-applet.git.sourceforge.net/git/gitweb.cgi?p=sensors -applet/sensors-applet;a=blob;f=plugins/libsensors/libsensors-plugin.c;h=960c19f4c36902dee4e20b690f2e3dfe6c715279;hb=HEAD

于 2011-12-19T17:55:11.877 に答える