@@ -73,6 +73,81 @@ def value(self):
7373 return True
7474
7575
76+ class CPUTemperature (InternalDevice ):
77+ """
78+ Extends :class:`InternalDevice` to provide a device which is active when
79+ the CPU temperature exceeds the *threshold* value.
80+
81+ The following example plots the CPU's temperature on an LED bar graph::
82+
83+ from gpiozero import LEDBarGraph, CPUTemperature
84+ from signal import pause
85+
86+ # Use minimums and maximums that are closer to "normal" usage so the
87+ # bar graph is a bit more "lively"
88+ temp = CPUTemperature(min_temp=50, max_temp=90)
89+ graph = LEDBarGraph(5, 6, 13, 19, 25, pwm=True)
90+ graph.source = temp.values
91+ pause()
92+
93+ :param str sensor_file:
94+ The file from which to read the temperature. This defaults to the
95+ sysfs file :file:`/sys/class/thermal/thermal_zone0/temp`. Whatever
96+ file is specified is expected to contain a single line containing the
97+ temperature in milli-degrees celsius.
98+
99+ :param float min_temp:
100+ The temperature at which :attr:`value` will read 0.0. This defaults to
101+ 0.0.
102+
103+ :param float max_temp:
104+ The temperature at which :attr:`value` will read 1.0. This defaults to
105+ 100.0.
106+
107+ :param float threshold:
108+ The temperature above which the device will be considered "active".
109+ This defaults to 80.0.
110+ """
111+ def __init__ (self , sensor_file = '/sys/class/thermal/thermal_zone0/temp' ,
112+ min_temp = 0.0 , max_temp = 100.0 , threshold = 80.0 ):
113+ self .sensor_file = sensor_file
114+ super (CPUTemperature , self ).__init__ ()
115+ self .min_temp = min_temp
116+ self .max_temp = max_temp
117+ self .threshold = threshold
118+ self ._fire_events ()
119+
120+ def __repr__ (self ):
121+ return '<gpiozero.CPUTemperature temperature=%.2f>' % self .temperature
122+
123+ @property
124+ def temperature (self ):
125+ """
126+ Returns the current CPU temperature in degrees celsius.
127+ """
128+ with io .open (self .sensor_file , 'r' ) as f :
129+ return float (f .readline ().strip ()) / 1000
130+
131+ @property
132+ def value (self ):
133+ """
134+ Returns the current CPU temperature as a value between 0.0
135+ (representing the *min_temp* value) and 1.0 (representing the
136+ *max_temp* value). These default to 0.0 and 100.0 respectively, hence
137+ :attr:`value` is :attr:`temperature` divided by 100 by default.
138+ """
139+ temp_range = self .max_temp - self .min_temp
140+ return (self .temperature - self .min_temp ) / temp_range
141+
142+ @property
143+ def is_active (self ):
144+ """
145+ Returns ``True`` when the CPU :attr:`temperature` exceeds the
146+ :attr:`threshold`.
147+ """
148+ return self .temperature > self .threshold
149+
150+
76151class TimeOfDay (InternalDevice ):
77152 """
78153 Extends :class:`InternalDevice` to provide a device which is active when
0 commit comments