<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>temperature | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/temperature/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Mon, 27 Apr 2020 12:57:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>temperature | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Here&#8217;s the Raspberry Pi temperature code</title>
		<link>https://learncgames.com/heres-the-raspberry-pi-temperature-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=heres-the-raspberry-pi-temperature-code</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 24 Apr 2020 23:00:19 +0000</pubDate>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[raspberry-pi]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[temperature]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=455</guid>

					<description><![CDATA[<p>To add temperature to the frame caption, I first do a check to see that it is running on a Raspberry Pi. This function does that. void SetPiFlag() { struct utsname buffer; tempFlag = 0; if (uname(&#38;buffer) != 0) return; if (strcmp(buffer.nodename,&#34;raspberrypi&#34;) !=0) return; if (strcmp(buffer.machine,&#34;armv7l&#34;) != 0) return; piFlag=1; } Note you have to [&#8230;]</p>
The post <a href="https://learncgames.com/heres-the-raspberry-pi-temperature-code/">Here’s the Raspberry Pi temperature code</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p>To add temperature to the frame caption, I first do a check to see that it is running on a Raspberry Pi. This function does that.</p>
<pre><code class="language-c" data-line="">void SetPiFlag() {
	struct utsname buffer;
	tempFlag = 0;
    if (uname(&amp;buffer) != 0) return;
    if (strcmp(buffer.nodename,&quot;raspberrypi&quot;) !=0) return;
	if (strcmp(buffer.machine,&quot;armv7l&quot;) != 0) return;
	piFlag=1;
}</code></pre>
<p>Note you have to add this include</p>
<pre><code class="language-c" data-line="">#include &lt;sys/utsname.h&gt;</code></pre>
<p>Now you need to call this function to return the temperature as a float.</p>
<pre><code class="language-c" data-line="">float ReadPiTemperature() {
    char buffer[10]; 
	char * end; 
	if (!piFlag) return 0.0f;
	if (SDL_GetTicks() - tempCount &lt; 1000) {	
		return lastTemp;
	}
	tempCount = SDL_GetTicks() ;
	FILE * temp = fopen(&quot;/sys/class/thermal/thermal_zone0/temp&quot;,&quot;rt&quot;); 
	int numread = fread(buffer,10,1,temp); 
	fclose(temp); 
	lastTemp = strtol(buffer,&amp;end,10)/1000.0f;
	return lastTemp;
}</code></pre>
<p>Things to notice.</p>
<ol>
<li>If it&#8217;s not running on a Pi it always returns 0.0C.</li>
<li>No matter how often it&#8217;s called, it only reads the temperature once a second.</li>
<li>In between reads it caches the temperature in a variable lastTemp</li>
</ol>
<p>When I first wrote this, it was reading the temperature on every frame. It actually changes that quickly but that made it hard to read. So once a second is fine.</p>The post <a href="https://learncgames.com/heres-the-raspberry-pi-temperature-code/">Here’s the Raspberry Pi temperature code</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">455</post-id>	</item>
		<item>
		<title>Asteroids now runs on a Pi 4</title>
		<link>https://learncgames.com/asteroids-now-runs-on-a-pi-4/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asteroids-now-runs-on-a-pi-4</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Thu, 23 Apr 2020 23:00:14 +0000</pubDate>
				<category><![CDATA[bugs]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[raspberry-pi]]></category>
		<category><![CDATA[temperature]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=447</guid>

					<description><![CDATA[<p>I was interested in seeing what frame rate I got out of it and how much it warmed the PI. The change to get the texture loaded was to split the five image files (four x asteroid + player ship) into two rows each. I added this code into the DrawPlayerShip function. if (Player.dir &#62;= [&#8230;]</p>
The post <a href="https://learncgames.com/asteroids-now-runs-on-a-pi-4/">Asteroids now runs on a Pi 4</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignleft size-full wp-image-451" src="https://learncgames.com/wp-content/uploads/2020/04/temperature-1.png" alt="Asteroids with built in temperature for Raspberry Pi" width="465" height="158" srcset="https://learncgames.com/wp-content/uploads/2020/04/temperature-1.png 465w, https://learncgames.com/wp-content/uploads/2020/04/temperature-1-300x102.png 300w" sizes="(max-width: 465px) 100vw, 465px" />I was interested in seeing what frame rate I got out of it and how much it warmed the PI.</p>
<p>The change to get the texture loaded was to split the five image files (four x asteroid + player ship) into two rows each.</p>
<p>I added this code into the DrawPlayerShip function.</p>
<pre><code class="language-cpp" data-line="">    if (Player.dir &gt;= 12) {
	  	spriterect.y = SHIPHEIGHT;
		spriterect.x -= SHIPWIDTH*12;
	}</code></pre>
<p>So for directions 0-11, it uses the top row and 12-23 the 2nd row. There&#8217;s similar code in the DrawAsteroids function.</p>
<p>I&#8217;m getting about 55 fps, twice the frame rate of the 3B+.  Sustained play over five minutes got the temperature up to 51C, but if I start the game and let asteroids drift about for a while it settles somewhere around 48-50C.</p>
<p>I have a fan fitted plugged into the 3.3V that runs all the time but is almost inaudible. There&#8217;s also a 5V setting that can be used for extra cooling but you only need that when temperatures get up to the 80C mark. There&#8217;s also 3 copper heat sinks stuck on three chips on the motherboard.</p>
<p>It&#8217;s very playable at 55 fps. This isn&#8217;t full screen BTW but on a 1024 x 768 playing area. There&#8217;s just one last change I&#8217;ve added. I combined the uname code to detect if it is running on a Pi and in that case display the temperature  on the Window caption. If you look closely at the image above you&#8217;ll see it says 43.82 C. I use a counter and check against so it only reads the temperature once a second and caches the result.</p>The post <a href="https://learncgames.com/asteroids-now-runs-on-a-pi-4/">Asteroids now runs on a Pi 4</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">447</post-id>	</item>
		<item>
		<title>How to read a Raspberry PI temperature in C code</title>
		<link>https://learncgames.com/how-to-read-a-raspberry-pi-temperature-in-c-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-read-a-raspberry-pi-temperature-in-c-code</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sun, 19 Apr 2020 23:00:31 +0000</pubDate>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[raspberry-pi]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[temperature]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=420</guid>

					<description><![CDATA[<p>Reading the temperature of a Raspberry PI can be done in a couple of ways. This command: vcgencmd measure_temp Outputs something like temp=32.7&#8217;C. My 3B+ PI has heatsinks and a cooling fan so runs cool. Even with asteroids it only peaked at 50.5C. Well below the throtle back temperature of 85C. Another way (I suspect [&#8230;]</p>
The post <a href="https://learncgames.com/how-to-read-a-raspberry-pi-temperature-in-c-code/">How to read a Raspberry PI temperature in C code</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img decoding="async" class="alignleft size-medium wp-image-423" src="https://learncgames.com/wp-content/uploads/2020/04/raspberrypi-300x194.jpg" alt="Raspberry Pi" width="300" height="194" srcset="https://learncgames.com/wp-content/uploads/2020/04/raspberrypi-300x194.jpg 300w, https://learncgames.com/wp-content/uploads/2020/04/raspberrypi-768x497.jpg 768w, https://learncgames.com/wp-content/uploads/2020/04/raspberrypi.jpg 800w" sizes="(max-width: 300px) 100vw, 300px" />Reading the temperature of a Raspberry PI can be done in a couple of ways. This command:</p>
<pre><code class="language-bash" data-line="">vcgencmd measure_temp</code></pre>
<p>Outputs something like temp=32.7&#8217;C. My 3B+ PI has heatsinks and a cooling fan so runs cool. Even with asteroids it only peaked at 50.5C. Well below the throtle back temperature of 85C.</p>
<p>Another way (I suspect they are the same) is to do</p>
<pre><code class="language-bash" data-line="">cat /sys/class/thermal/thermal_zone0/temp</code></pre>
<p>Which outputs values like 32705. Just divide by 1000 to get the temperature in Celsius.</p>
<p>But how do we do it in code? Well I&#8217;ve tested this code below and it seems to work.</p>
<pre><code class="language-java" data-line="">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;strings.h&gt;

int main(void) {
  char buffer[10];
  char * end;
  FILE * temp = fopen(&quot;/sys/class/thermal/thermal_zone0/temp&quot;,&quot;rt&quot;);
  int numread = fread(buffer,10,1,temp);
  fclose(temp);
  printf(&quot;Temp = %5.2fC\n&quot;,strtol(buffer,&amp;end,10)/1000.0);
}</code></pre>
<p>It reads the device as a string then converts to a long using strtol and divides that by 1000 then prints it. Output is something like:</p>
<pre>Temp (C) = 34.88C</pre>
<p>Now it just needs combined with the code to detect that it&#8217;s running on a Pi and you&#8217;re good.</p>The post <a href="https://learncgames.com/how-to-read-a-raspberry-pi-temperature-in-c-code/">How to read a Raspberry PI temperature in C code</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">420</post-id>	</item>
	</channel>
</rss>
