Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. from gpiozero import InternalDevice
  2. import os
  3.  
  4. class GPUTemperature(InternalDevice):
  5. """
  6. Extends :class:`InternalDevice` to provide a device which is active when
  7. the GPU temperature exceeds the *threshold* value.
  8. Modified from :class:`CPUTemperature`
  9.  
  10. :param str sensor_command:
  11. The command from which to read the temperature. This defaults to the
  12. run `vcgencmd measure_temp`. Whatever message return is specified is
  13. expected to contain a single line containing the
  14. temperature in degrees celsius.
  15.  
  16. :param float min_temp:
  17. The temperature at which :attr:`value` will read 0.0. This defaults to
  18. 0.0.
  19.  
  20. :param float max_temp:
  21. The temperature at which :attr:`value` will read 1.0. This defaults to
  22. 100.0.
  23.  
  24. :param float threshold:
  25. The temperature above which the device will be considered "active".
  26. This defaults to 80.0.
  27. """
  28. def __init__(self, sensor_command='vcgencmd measure_temp', min_temp=0.0,
  29. max_temp=100.0, threshold=80.0):
  30. self.sensor_command = sensor_command
  31. super(GPUTemperature, self).__init__()
  32. self.min_temp = min_temp
  33. self.max_temp = max_temp
  34. self.threshold = threshold
  35. self._fire_events()
  36.  
  37. def __repr__(self):
  38. return '<gpiozero.GPUTemperature temperature=%.2f>' % self.temperature
  39.  
  40. @property
  41. def temperature(self):
  42. """
  43. Returns the current GPU temperature in degrees celsius.
  44. """
  45. with os.popen(self.sensor_command) as f:
  46. return float(f.readline().replace('temp=','').replace("'C\n",'').strip())
  47.  
  48. @property
  49. def value(self):
  50. """
  51. Returns the current GPU temperature as a value between 0.0
  52. (representing the *min_temp* value) and 1.0 (representing the
  53. *max_temp* value). These default to 0.0 and 100.0 respectively, hence
  54. :attr:`value` is :attr:`temperature` divided by 100 by default.
  55. """
  56. temp_range = self.max_temp - self.min_temp
  57. return (self.temperature - self.min_temp) / temp_range
  58.  
  59. @property
  60. def is_active(self):
  61. """
  62. Returns ``True`` when the GPU :attr:`temperature` exceeds the
  63. :attr:`threshold`.
  64. """
  65. return self.temperature > self.threshold
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement