summaryrefslogtreecommitdiffstats
path: root/src/lmsensor.cpp
blob: 89a79c368deb2db28eadd8ca407b881d9c811d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************
                          lmsensor.cpp  -  description
                             -------------------
    begin                : Mon Aug 6 2001
    copyright            : (C) 2001 by 
    email                : 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "lmsensor.h"

#include "lmsensorschip.h"

#include "stdio.h"

LMSensor::LMSensor(SensorsList *parent): Sensor(parent)
{
  feature= -1;
}


LMSensor::~LMSensor(){
}


bool LMSensor::init(const sensors_feature_data **data, int *nr1,int *nr2)
{
double min,max;

 const sensors_chip_name *chip_name= getChipName();

 if (strstr((*data)->name, "temp"))
 {
    setType(lmTemp);
    max= 65;
    min= 0;
 }
 else if (strstr((*data)->name, "fan"))
 {
    setType(lmFan);
    max= 10000;
    min= 3000;
 }
 else
 {
    setType(lmVoltage);
    max= 16;
    min= -16;
 }

 feature= (*data)->number;

 QString str;
 str.sprintf("%s.%s", chip_name->prefix, (*data)->name );
 setName( str.latin1() );

 char *label;
 sensors_get_label(*chip_name,feature,&label);
 setDescription(QString(label));

 bool valid= false;
 while( (*data= sensors_get_all_features(*chip_name, nr1, nr2)) && (*data)->mapping!=SENSORS_NO_MAPPING) {
   str= (*data)->name;
   if(str.find("_min")>=0 || str.find("_low")>=0) {
       sensors_get_feature(*chip_name, (*data)->number, &valMin);
       valid= true;
   } else
       if(str.find("_max")>=0 || str.find("_over")>=0  || str.find("_high")>=0) {
          sensors_get_feature(*chip_name, (*data)->number, &valMax);
          valid= true;
       }
 }

 if(valid) {
    double newVal;
    valid= (sensors_get_feature(*chip_name, feature, &newVal)==0);
    if(valid) {
        if(min>max) {
          double pivot= valMin;
          min= max;
          max= pivot;
        }
        setValueMax(max,dgCelsius);
        setValueMin(min,dgCelsius);
        setValue((max+min)/2,dgCelsius);
        readConfig();
        updateValue();
        setValueIdeal(getValue());
    }
 }
 return valid;
}

void LMSensor::updateValue()
{
 setValue( readSensorValue(), dgCelsius );
}

double LMSensor::readSensorValue()
{
 double newVal;
 const sensors_chip_name *chip_name= getChipName();
 sensors_get_feature(*chip_name, feature, &newVal);
 return newVal;
}

#define ABSOLUTE_VALUE(n)  ( (n)>=0 ? (n) : -(n) )
#define TRUNCATE_VALUE(n)  ( (double)(int)(n)    )

double LMSensor::calculateIdealValue()
{
 double value= readSensorValue();

 if(getType()==lmVoltage) {
     double decimals= 10;
     double module = ABSOLUTE_VALUE(value);
     if(module>3.0) {
       if( ABSOLUTE_VALUE( 12.0-value)<2.0 ) return  12.0;
       if( ABSOLUTE_VALUE(-12.0-value)<2.0 ) return -12.0;
       if( ABSOLUTE_VALUE(  5.0-value)<1.0 ) return   5.0;
       if( ABSOLUTE_VALUE( -5.0-value)<1.0 ) return  -5.0;
       if( ABSOLUTE_VALUE(  3.3-value)<0.3 ) return   3.3;
       if( ABSOLUTE_VALUE( -3.3-value)<0.3 ) return  -3.3;
       if(module>4.0) decimals= 1;
     }
     return TRUNCATE_VALUE((value * decimals)) / decimals;
  } else {
     return value;
  }
}

const sensors_chip_name *LMSensor::getChipName()
{
  return ((LMSensorsChip *)parent())->getChipName();
}