Comunidad Gambas-es
Monitorizar la temperatura del hardware mediante las funciones del API de libsensors - Versión para impresión

+- Comunidad Gambas-es (https://gambas-es.org)
+-- Foro: Gambas (https://gambas-es.org/forum-3.html)
+--- Foro: Aplicaciones/Fragmentos de Código (https://gambas-es.org/forum-8.html)
+--- Tema: Monitorizar la temperatura del hardware mediante las funciones del API de libsensors (/thread-363.html)



Monitorizar la temperatura del hardware mediante las funciones del API de libsensors - vuott - 20-04-2021

Hola os dejo este mi codigo para monitorizar la temperatura del hardware mediante las funciones del API de libsensors:

Código:
Library "libsensors:5.0.0"
 
Public Struct sensors_bus_id
  type As Short
  nr As Short
End Struct

Public Struct sensors_chip_name
  prefix As Pointer
  bus As Struct Sensors_bus_id
  addr As Integer
  path As Pointer
End Struct

Public Struct sensors_feature
  name As Pointer
  number As Integer
  type As Integer
  first_subfeature As Integer
  padding1 As Integer
End Struct

Public Struct sensors_subfeature
  name As Pointer
  number As Integer
  type As Integer
  mapping As Integer
  flags As Integer
End Struct

Private Const SENSORS_FEATURE_TEMP As Integer = 2
Private Const SENSORS_SUBFEATURE_TEMP_INPUT As Integer = 512

' int sensors_init(FILE *input)
' Load the configuration file and the detected chips list.
Private Extern sensors_init(input_ As Pointer) As Integer

' const sensors_chip_name *sensors_get_detected_chips(const sensors_chip_name *match, int *nr)
' This function returns all detected chips that match a given chip name, one by one.
Private Extern sensors_get_detected_chips(match_ As Pointer, nr As Pointer) As Pointer

' int sensors_snprintf_chip_name(char *str, size_t size, const sensors_chip_name *chip)
' Prints a chip name from its internal representation.
Private Extern sensors_snprintf_chip_name(str_ As Pointer, size As Integer, chip As Sensors_chip_name) As Integer

' const char* sensors_get_adapter_name (const sensors_bus_id * bus)
' Returns the adapter name of a bus number, as used within the sensors_chip_name structure.
Private Extern sensors_get_adapter_name(bus As Sensors_bus_id) As String

' const sensors_feature * sensors_get_features(const sensors_chip_name *name, int *nr)
' This returns all main features of a specific chip. nr is an internally used variable.
Private Extern sensors_get_features(name As Sensors_chip_name, nr As Pointer) As Pointer

' char *sensors_get_label(const sensors_chip_name *name, const sensors_feature *feature)
' Look up the label for a given feature.
Private Extern sensors_get_label(name As Sensors_chip_name, feature As Sensors_feature) As Pointer

' const sensors_subfeature * sensors_get_subfeature(const sensors_chip_name *name, const sensors_feature *feature, sensors_subfeature_type type)
' Returns the subfeature of the given type for a given main feature.
Private Extern sensors_get_subfeature(name As Sensors_chip_name, feature As Sensors_feature, type As Integer) As Sensors_subfeature

' int sensors_get_value(const sensors_chip_name *name, int subfeat_nr, double *value)
' Read the value of a subfeature of a certain chip.
Private Extern sensors_get_value(name As Sensors_chip_name, subfeat_nr As Integer, value As Pointer) As Integer

' void sensors_cleanup(void)
' Cleans everything up.
Private Extern sensors_cleanup()


Public Sub Main()

 Dim p As Pointer
 Dim scn As Sensors_chip_name
 Dim chn As Integer
 
 If sensors_init(Null) <> 0 Then
   Chiusura()
   Error.Raise("Errore !")
 Endif
 
' Devuelve en la variable "chn" el número de chips encontrados:
 p = sensors_get_detected_chips(Null, VarPtr(chn))
 
 While p > 0
' Utiliza el Puntero, para no incurrir en errores encontrados en las pruebas, y lo asigna a la Estructura:
   scn = p
   If Mostra(scn) > 0 Then Print
   p = sensors_get_detected_chips(Null, VarPtr(chn))
 Wend 
 
' Cerrando:
 Chiusura()
 
End 

Private Procedure Chiusura()
 
' Libera la memoria ocupada anteriormente por la libreria "libsensors":
 sensors_cleanup()

End

Private Procedure Mostra(nome As Sensors_chip_name) As Integer
 
 Dim buf As New Byte[128]
 Dim chip As New String[2]
 Dim adap As String
 Dim p As Pointer
 Dim sft As Sensors_feature
 Dim subf As Sensors_subfeature
 Dim i, c As Integer
 Dim f As Float
 
 sensors_snprintf_chip_name(buf.Data, buf.Count, nome)
 chip[0] = String@(buf.Data)
 adap = sensors_get_adapter_name(nome.bus)
 chip[1] = adap
 
 p = sensors_get_features(nome, VarPtr(i))
 
 While p > 0
' Utiliza el Puntero, para no incurrir en errores encontrados en las pruebas, y lo asigna a la Estructura:
   sft = p
   If sft.type == SENSORS_FEATURE_TEMP Then 
     subf = sensors_get_subfeature(nome, sft, SENSORS_SUBFEATURE_TEMP_INPUT) 
     sensors_get_value(nome, subf.number, VarPtr(f))
     If chip.Count > 0 Then
       Print chip[0]
       Print "Adapter: " & chip[1]
       chip.Clear
     Endif
     Print String@(sensors_get_label(nome, sft)); ": "; "\e[1m"; f; "\e[0m"; "°C"
     Inc c
   Endif 
   p = sensors_get_features(nome, VarPtr(i))
 Wend 
 
 Return c
 
End



RE: Controlar la temperatura del hardware mediante las funciones del API de libsensors - gambafeliz - 21-04-2021

Cuando dices para controlar la temperatura de Hardware

Te refieres por ejemplo a una placa base de mi computadora por ejemplo la que escribe ahora en esta pregunta o es para algún hardware especifico?

Por cierto, vaya tela, -código aventurero- Smile


RE: Controlar la temperatura del hardware mediante las funciones del API de libsensors - tincho - 21-04-2021

Hola Vuott, gracias por compartir este código, por lo que puedo ver sirve para "leer" la temperatura del hardwre pero no para "actuar" sobre este para que descienda.
En control industrial de procesos de sistemas dinámicos esta implícita la posibilidad de actuar sobre las variables (temperatura) con algún elemento de salida (ventilador)
Sin la posibilidad de cambiar las variables físicas o químicas del sistema decimos que solo medimos, censamos o monitotrizamos
https://en.wikipedia.org/wiki/Control_theory

Propongo el titulo: Monitorizar la temperatura del hardware mediante las funciones del API de libsensors

Saludos.


RE: Monitorizar la temperatura del hardware mediante las funciones del API de libsensors - vuott - 21-04-2021

(21-04-2021, 15:08)tincho escribió: Propongo el titulo: Monitorizar la temperatura del hardware mediante las funciones del API de libsensors

ok