Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from gpiozero import InternalDevice
- import os
- class GPUTemperature(InternalDevice):
- """
- Extends :class:`InternalDevice` to provide a device which is active when
- the GPU temperature exceeds the *threshold* value.
- Modified from :class:`CPUTemperature`
- :param str sensor_command:
- The command from which to read the temperature. This defaults to the
- run `vcgencmd measure_temp`. Whatever message return is specified is
- expected to contain a single line containing the
- temperature in degrees celsius.
- :param float min_temp:
- The temperature at which :attr:`value` will read 0.0. This defaults to
- 0.0.
- :param float max_temp:
- The temperature at which :attr:`value` will read 1.0. This defaults to
- 100.0.
- :param float threshold:
- The temperature above which the device will be considered "active".
- This defaults to 80.0.
- """
- def __init__(self, sensor_command='vcgencmd measure_temp', min_temp=0.0,
- max_temp=100.0, threshold=80.0):
- self.sensor_command = sensor_command
- super(GPUTemperature, self).__init__()
- self.min_temp = min_temp
- self.max_temp = max_temp
- self.threshold = threshold
- self._fire_events()
- def __repr__(self):
- return '<gpiozero.GPUTemperature temperature=%.2f>' % self.temperature
- @property
- def temperature(self):
- """
- Returns the current GPU temperature in degrees celsius.
- """
- with os.popen(self.sensor_command) as f:
- return float(f.readline().replace('temp=','').replace("'C\n",'').strip())
- @property
- def value(self):
- """
- Returns the current GPU temperature as a value between 0.0
- (representing the *min_temp* value) and 1.0 (representing the
- *max_temp* value). These default to 0.0 and 100.0 respectively, hence
- :attr:`value` is :attr:`temperature` divided by 100 by default.
- """
- temp_range = self.max_temp - self.min_temp
- return (self.temperature - self.min_temp) / temp_range
- @property
- def is_active(self):
- """
- Returns ``True`` when the GPU :attr:`temperature` exceeds the
- :attr:`threshold`.
- """
- return self.temperature > self.threshold
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement