<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Steven Johnston</title>
	<atom:link href="http://stevenjohnston.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://stevenjohnston.co.uk</link>
	<description></description>
	<lastBuildDate>Fri, 24 May 2013 16:34:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='stevenjohnston.co.uk' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Steven Johnston</title>
		<link>http://stevenjohnston.co.uk</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://stevenjohnston.co.uk/osd.xml" title="Steven Johnston" />
	<atom:link rel='hub' href='http://stevenjohnston.co.uk/?pushpress=hub'/>
		<item>
		<title>PIR module for .NET Gadgeteer (Motion Sensor)</title>
		<link>http://stevenjohnston.co.uk/2013/05/24/pir-module-for-net-gadgeteer-motion-sensor/</link>
		<comments>http://stevenjohnston.co.uk/2013/05/24/pir-module-for-net-gadgeteer-motion-sensor/#comments</comments>
		<pubDate>Fri, 24 May 2013 16:30:59 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Drivers]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[PIR]]></category>
		<category><![CDATA[Motion sensor]]></category>
		<category><![CDATA[PIR sensor]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=442</guid>
		<description><![CDATA[(Module: http://gadgeteering.net/module/ghi-pir-sensor) We met up last night to have a look at a .NET Gadgeteer module, picking a nice easy &#8230;<p><a href="http://stevenjohnston.co.uk/2013/05/24/pir-module-for-net-gadgeteer-motion-sensor/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=442&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>(Module: <a title="http://gadgeteering.net/module/ghi-pir-sensor" href="http://gadgeteering.net/module/ghi-pir-sensor">http://gadgeteering.net/module/ghi-pir-sensor</a>)</p>
<p>We met up last night to have a look at a .NET Gadgeteer module, picking a nice easy module to look at to begin with for: <a title="http://www.meetup.com/GadgeteerSouthCoast/" href="http://www.meetup.com/GadgeteerSouthCoast/">http://www.meetup.com/GadgeteerSouthCoast/</a>. Despite being called the GHI PIR Sensor it is found in the toolbox as a “Motion Sensor”</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image25.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb25.png?w=276&#038;h=451" width="276" height="451"></a> </p>
<p>Here is the view from the top of the module:</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/wp_20130524_012.jpg"><img title="WP_20130524_012" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="WP_20130524_012" src="http://stevenjohnston.files.wordpress.com/2013/05/wp_20130524_012_thumb.jpg?w=578&#038;h=485" width="578" height="485"></a> </p>
<p>There is just the one event which is triggered whenever the sensor spots an IR source (Heat/person), called Motion_Sensed. There is a property that can be checked to see if the sensor can still see the source once it was triggered. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image26.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb26.png?w=479&#038;h=279" width="479" height="279"></a> </p>
<p>I would have expected to see a data sheen in codeplex, <a title="http://gadgeteer.codeplex.com/SourceControl/latest" href="http://gadgeteer.codeplex.com/SourceControl/latest">http://gadgeteer.codeplex.com/SourceControl/latest</a>, but there is nothing. Next stop is the module manufacture website/forum. In this case it is GHI and I am not the first to ask questions about the PIR module:</p>
<ul>
<li>GHI Forum: <a title="http://www.ghielectronics.com/community/forum/topic?id=6930" href="http://www.ghielectronics.com/community/forum/topic?id=6930">http://www.ghielectronics.com/community/forum/topic?id=6930</a></li>
<li>Datasheet: <a title="http://elecfreaks.com/store/download/datasheet/sensor/DYP-ME003/Specification.pdf" href="http://elecfreaks.com/store/download/datasheet/sensor/DYP-ME003/Specification.pdf">http://elecfreaks.com/store/download/datasheet/sensor/DYP-ME003/Specification.pdf</a></li>
<li>Info: <a title="http://www.elecfreaks.com/398.html" href="http://www.elecfreaks.com/398.html">http://www.elecfreaks.com/398.html</a></li>
</ul>
<p>Based on this information I have labelled the POTS and jumper below. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/pir.png"><img title="PIR" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="PIR" src="http://stevenjohnston.files.wordpress.com/2013/05/pir_thumb.png?w=594&#038;h=370" width="594" height="370"></a> </p>
<h3>Motion_Sensed event</h3>
<p>First lets see how often this event gets triggered. First add code for the event:</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:853e5f69-ecf5-47cd-ad6d-3a6c9c421ea1" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">startTime = <span style="color:#2b91af;">DateTime</span>.Now;</li>
<li>motion_Sensor.Motion_Sensed += <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span>.<span style="color:#2b91af;">Motion_SensorEventHandler</span>(motion_Sensor_Motion_Sensed);</li>
</ol></div>
</p></div>
</p></div>
<p>And here is what we will execute every time it is triggered:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:30d24092-e419-479d-a779-ea7b4ec18de4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> motion_Sensor_Motion_Sensed(GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span> sender, GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span>.<span style="color:#2b91af;">Motion_SensorState</span> state)</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Time span = &quot;</span> + (<span style="color:#2b91af;">DateTime</span>.UtcNow &#8211; startTime).ToString());</li>
<li style="background:#f3f3f3;">    startTime = <span style="color:#2b91af;">DateTime</span>.UtcNow;</li>
<li>    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;TRIGGER&quot;</span>);</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>By setting the <strong>time</strong> POT to min (anti-clockwise) so the events will be triggered as often as possible. (The distance POT is at the mid point). We get the following output. </p>
<p><em>Using mainboard GHI Electronics FEZHydra version 1.2<br />Program Started<br />Sensor = False<br />Time span = 00:00:09.9934515<br />TRIGGER<br />The thread &#8216;&lt;No Name&gt;&#8217; (0&#215;3) has exited with code 0 (0&#215;0).<br />Time span = 00:00:07.8206963<br />TRIGGER<br />Time span = 00:00:07.7379289<br />TRIGGER<br />Time span = 00:00:06.4709773<br />TRIGGER<br />Time span = 00:00:08.7667315<br />TRIGGER<br />Time span = 00:00:07.6156723<br />TRIGGER<br />Time span = 00:00:06.2445184<br />TRIGGER<br />Time span = 00:00:06.6116992</em>
<p>By setting the time POT to max (Clockwise) we get the following output:
<p><em>Time span = 00:03:42.0452275<br />TRIGGER<br />Time span = 00:03:58.3361766<br />TRIGGER<br />Time span = 00:03:41.4596557<br />TRIGGER<br />Time span = 00:03:40.7306982</em>
<p>So the time range is around 7sec (min) to 230sec (max) which is in keeping with the data sheet, which I think says 5 sec – 300 sec : </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image27.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb27.png?w=859&#038;h=122" width="859" height="122"></a> </p>
<p>Changing the distance POT certainly did change range or sensitivity but it was not so easy to work out the exact trigger distances. however the datasheet shows a range or around 5 –7m and it certainly gets triggered within those ranges. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image28.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb28.png?w=679&#038;h=438" width="679" height="438"></a>
</p>
<h3>SensorStillActive property</h3>
<p>There is a property that is readable and is of type Boolean, but what can we use it for?</p>
<p>If we run in with the time POT on minimum, distance on 50% and the jumper set to “Repeatedly trigger” we get the following output:</p>
<p><em>TRIGGER<br />State changed to: False @ 00:00:00.0011277<br />State changed to: True @ 00:00:02.2871399<br />Time span = 00:00:09.5215143<br />TRIGGER<br />State changed to: False @ 00:00:00.0650803<br />State changed to: True @ 00:00:02.4655180</em>
<p>And if we set the jumper to “Single trigger” we get the following:</p>
<p><em>TRIGGER<br />State changed to: False @ 00:00:00.0010278<br />State changed to: True @ 00:00:08.3824435<br />Time span = 00:00:11.9519795<br />TRIGGER<br />State changed to: False @ 00:00:00.0300698<br />State changed to: True @ 00:00:03.0303744<br />Time span = 00:00:06.5282752<br />TRIGGER<br />State changed to: False @ 00:00:00.0019021<br />State changed to: True @ 00:00:02.5016538<br />Time span = 00:00:04.7239091<br />TRIGGER<br />State changed to: False @ 00:00:00.0771596<br />State changed to: True @ 00:00:02.9764812<br />State changed to: False @ 00:00:06.4768678<br />Time span = 00:00:06.4945881<br />TRIGGER<br />State changed to: True @ 00:00:07.4823129<br />Time span = 00:00:11.0135769</em>
<p>So I am unsure what the jumper does, but the event is triggered when the SensorStillActive goes to false, it is held false for around 3 seconds then returns to true. The trigger happens every 6sec as expected. I suspect this will get held to false as long as the sensor can still detect an IR source, however my one always stops sensing despite being waved around at a source. Here is what the datasheet has to say about the jumper:</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image29.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb29.png?w=794&#038;h=428" width="794" height="428"></a> </p>
<p>&nbsp;</p>
<p>Have some source code:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67dcd7ae-8a4e-4e50-aa4e-bbb4653f9da8" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections;</li>
<li><span style="color:#0000ff;">using</span> System.Threading;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Controls;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Media;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Touch;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Networking;</li>
<li><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> CompassDriverExample</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#2b91af;">DateTime</span> startTime;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">bool</span> sensorState; </li>
<li style="background:#f3f3f3;">           </li>
<li>        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">            startTime = <span style="color:#2b91af;">DateTime</span>.Now;</li>
<li>            sensorState = motion_Sensor.SensorStillActive;</li>
<li style="background:#f3f3f3;">            motion_Sensor.Motion_Sensed += <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span>.<span style="color:#2b91af;">Motion_SensorEventHandler</span>(motion_Sensor_Motion_Sensed);</li>
<li>            GT.<span style="color:#2b91af;">Timer</span> tmr = <span style="color:#0000ff;">new</span> GT.<span style="color:#2b91af;">Timer</span>(50, GT.<span style="color:#2b91af;">Timer</span>.<span style="color:#2b91af;">BehaviorType</span>.RunContinuously);</li>
<li style="background:#f3f3f3;">            tmr.Tick += <span style="color:#0000ff;">new</span> GT.<span style="color:#2b91af;">Timer</span>.<span style="color:#2b91af;">TickEventHandler</span>(tmr_Tick);</li>
<li>            tmr.Start();</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        </li>
<li>        <span style="color:#0000ff;">void</span> tmr_Tick(GT.<span style="color:#2b91af;">Timer</span> timer)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">if</span> (motion_Sensor.SensorStillActive != sensorState)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;State changed to: &quot;</span> + motion_Sensor.SensorStillActive + <span style="color:#a31515;">&quot; @ &quot;</span>+ (<span style="color:#2b91af;">DateTime</span>.UtcNow &#8211; startTime).ToString());</li>
<li style="background:#f3f3f3;">                sensorState = motion_Sensor.SensorStillActive;</li>
<li>            }</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> motion_Sensor_Motion_Sensed(GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span> sender, GTM.GHIElectronics.<span style="color:#2b91af;">Motion_Sensor</span>.<span style="color:#2b91af;">Motion_SensorState</span> state)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Time span = &quot;</span> + (<span style="color:#2b91af;">DateTime</span>.UtcNow &#8211; startTime).ToString());</li>
<li>            startTime = <span style="color:#2b91af;">DateTime</span>.UtcNow;</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;TRIGGER&quot;</span>);</li>
<li>        }</li>
<li style="background:#f3f3f3;">    }</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/442/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/442/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=442&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/05/24/pir-module-for-net-gadgeteer-motion-sensor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb25.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/wp_20130524_012_thumb.jpg" medium="image">
			<media:title type="html">WP_20130524_012</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb26.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/pir_thumb.png" medium="image">
			<media:title type="html">PIR</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb27.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb28.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb29.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>I want to edit the .NET Gadgeteer drivers for a particular module!</title>
		<link>http://stevenjohnston.co.uk/2013/05/23/i-want-to-edit-the-net-gadgeteer-drivers-for-a-particular-module/</link>
		<comments>http://stevenjohnston.co.uk/2013/05/23/i-want-to-edit-the-net-gadgeteer-drivers-for-a-particular-module/#comments</comments>
		<pubDate>Thu, 23 May 2013 15:52:51 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Drivers]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[.Net Gadgeteer drivers]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[edit]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=426</guid>
		<description><![CDATA[When you drag a module from the toolbox onto the designer it references the correct DLL’s and adds import statements &#8230;<p><a href="http://stevenjohnston.co.uk/2013/05/23/i-want-to-edit-the-net-gadgeteer-drivers-for-a-particular-module/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=426&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When you drag a module from the toolbox onto the designer it references the correct DLL’s and adds import statements to the program.cs. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image3.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb3.png?w=488&#038;h=226" width="488" height="226"></a> </p>
<p>Fig 1: Adding a compass module to the designer. </p>
<p>This way you are ready to just use the module in your code. This is the normal way to to things… However what if you want to make a change to the driver for a particular module, for example to:</p>
<ul>
<li>add an event, method or property,</li>
<li>perhaps fix an annoying bug,</li>
<li>add extra debug, or work out how the hardware works,</li>
<li>load a different driver, such as the GPS/Bluetooth module drivers from Codeplex.com ,</li>
<li>copy the driver for a similar piece of hardware,</li>
<li>you want to compile a driver for a different version of NetMF (e.g. upgrade 4.1 –&gt; 4.2)</li>
<li>you cant be bothered to get WIX setup and working just to make a small change. </li>
</ul>
<p>The correct way to change a driver is to get the source code VS solution, edit it and then rebuild. This will build an installer (msi) which can be installed and distributed with the changes intact. This process is fine for the final edits and distribution of a driver, but what if you just want to try a few changes? This is a long process as each change results in a build, reinstall , test.</p>
<p>This post is designed to show how to simply edit drivers or include different drivers. It is NOT a substitute for building the install files which should be distributed with each hardware module. </p>
<h3>Example</h3>
<p>Lets say we want to make an edit to the compass driver, perhaps it does not function as we expected. Here is a summary of what to do:</p>
<ol>
<li>Get the source code. (Either the existing source, or substitute with a different one)</li>
<li>Remove the existing driver from your solution.</li>
<li>Add the (new) source code to your solution.</li>
<li>Add the module to your project.</li>
<li>Edit the driver and test.</li>
</ol>
<h3>Get the source code. </h3>
<p>There are multiple drivers for some modules, and the alternatives can often be better. (Well at least until the manufacturer incorporates the changes into the distributed driver.)</p>
<p>If you are looking for other drivers try: <a title="http://www.codeplex.com/site/search?query=.net%20gadgeteer" href="http://www.codeplex.com/site/search?query=.net%20gadgeteer">http://www.codeplex.com/site/search?query=.net%20gadgeteer</a></p>
<p>The source for the distributed drivers lives here: <a title="http://gadgeteer.codeplex.com/SourceControl/latest" href="http://gadgeteer.codeplex.com/SourceControl/latest">http://gadgeteer.codeplex.com/SourceControl/latest</a></p>
<p>I use SVN to get the latest source, but if you just want the source then click the download button. It will give you a zip, be sure to expand it to somewhere sensible, we will be using it later. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image4.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb4.png?w=711&#038;h=266" width="711" height="266"></a> </p>
<h3>Remove the existing driver from your solution.</h3>
<p>This step is important as it can drive you crazy later by swapping your new driver with the old one when you least suspect it. </p>
<p>Here is some background information first:</p>
<p>If we look a the code behind the designer we can see what happens when the compass module is added.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image5.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb5.png?w=352&#038;h=324" width="352" height="324"></a> </p>
<p>Click “Show all files” if you cannot see the “Program.gadgeteer.cs” file. This file is generated EVERY time you save the designer (the place you drag modules onto). Inside this file there is the following code. (For this example project)</p>
<p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9b3915de-aa4d-4469-be73-871354738d6f" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">namespace</span> CompassDriverExample {</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">using</span> Gadgeteer;</li>
<li>    <span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li style="background:#f3f3f3;">    </li>
<li>    </li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span> : Gadgeteer.<span style="color:#2b91af;">Program</span> {</li>
<li>        </li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> Gadgeteer.Modules.Seeed.<span style="color:#2b91af;">Compass</span> compass;</li>
<li>        </li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main() {</li>
<li>            <span style="color:#008000;">// Important to initialize the Mainboard first</span></li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">Program</span>.Mainboard = <span style="color:#0000ff;">new</span> LoveElectronics.Gadgeteer.<span style="color:#2b91af;">ArgonR1</span>();</li>
<li>            <span style="color:#2b91af;">Program</span> p = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Program</span>();</li>
<li style="background:#f3f3f3;">            p.InitializeModules();</li>
<li>            p.ProgramStarted();</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Starts Dispatcher</span></li>
<li>            p.Run();</li>
<li style="background:#f3f3f3;">        }</li>
<li>        </li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> InitializeModules() {</li>
<li>            <span style="color:#0000ff;">this</span>.compass = <span style="color:#0000ff;">new</span> GTM.Seeed.<span style="color:#2b91af;">Compass</span>(8);</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>The first line of importance creates a compass variable of type Gadgeteer.Modules.Seeed.Compass.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c0d3a770-de39-4f69-886a-2b5fd37e3585" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">private</span> Gadgeteer.Modules.Seeed.<span style="color:#2b91af;">Compass</span> compass;</li>
</ol></div>
</p></div>
</p></div>
<p>The second line of importance is the one that creates an instance of the compass object and specifies the socket number.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:01061939-435c-495f-9319-38c115a5e47c" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">this</span>.compass = <span style="color:#0000ff;">new</span> GTM.Seeed.<span style="color:#2b91af;">Compass</span>(8);</li>
</ol></div>
</p></div>
</p></div>
<p>In order to create the compass object it needs to know about it. This is achieved automatically by adding a reference to the compiled DLL, when you dragged in a new module. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image6.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb6.png?w=238&#038;h=432" width="238" height="432"></a> </p>
<p>Here are the things you need to change to remove the old driver:</p>
<p>1) Delete the module from the designer. </p>
<p>- This will remove it from the generated code and will stop the auto generated code swapping back to the old driver when you save.</p>
<p>2) Check that the references on your project no longer reference the DLL for the old driver. (highlighted above)</p>
<p>or </p>
<p>You could just start with a blank project. </p>
<p>The reason I show all this is that later on you will need to instantiate the object yourself. If you don&#8217;t know what to write you can copy the auto generated code … more later. </p>
<h3>Add the (new) source code to your solution.</h3>
<p>At this point you have a blank project or one that does NOT reference/use the module that you plan to edit the driver for. First we need to add the source code, this is the code that you downloaded earlier or can be a different driver that you wish to try.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image7.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb7.png?w=574&#038;h=270" width="574" height="270"></a> </p>
<p>In Visual studio right click on the solution and add an existing project by navigating to the correct project. (This can be a little confusing as it tends to be a long way down the folder structure, and you need to be sure of the version you are using, v4.2)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image8.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb8.png?w=618&#038;h=134" width="618" height="134"></a> </p>
<p>Inside the Compass –&gt; software folder you are looking for the compass.csproj file. (I would expect to see 2 versions, one for 4.1 and one for 4.2, but this may not be the case if an old driver template was used.)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image9.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb9.png?w=621&#038;h=163" width="621" height="163"></a>&nbsp;</p>
<p><em><strong>A little note:</strong> In this particular case there is just a driver for the 4.1 version of NetMF, yet when you install the latest SDK the compass is available in 4.2. This means that the manufacturer has updated the driver but not uploaded the changes to Codeplex. Be sure to encourage manufacturers to keep the source updated. This could also be the case if the Codeplex source code has different methods/signatures. Since my project is v4.2 I will just choose to update the target framework in the project properties.(Good example of how to upgrade versions)</em></p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image10.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb10.png?w=446&#038;h=315" width="446" height="315"></a>&nbsp;</p>
<h3>Add the module to your project</h3>
<p>At this stage you should have multiple projects in your solution. Your project as well as the project for the module you are wanting to edit. So now we have all the source code in place. Lets use it. </p>
<p>First we need to let our project know about the new project by adding a reference, but instead of referencing the DLL we reference the project with the source code. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image11.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb11.png?w=371&#038;h=368" width="371" height="368"></a> </p>
<p>It is under projects not the .NET tab:</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image12.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb12.png?w=488&#038;h=206" width="488" height="206"></a> </p>
<p>You will then see a reference to the Compass project, note that this is different to the reference show above when you let the designer import it. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image13.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb13.png?w=387&#038;h=442" width="387" height="442"></a> </p>
<p>Now we are ready to use the module. Remember all the automatically generated code that the designer does for you, well now you have to do it yourself. Instead of adding to code to the generated file (you will loose it , when it auto generates) we add it to the main program.cs file. (only 2 lines)</p>
<p>First create a private variable for the compass then in the program started create an instance, on socket 8. Be sure not to use the socket in the designer – you will get a warning if you do so fear not. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:57d3b093-4c49-4a42-8c45-812f5b8da118" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections;</li>
<li><span style="color:#0000ff;">using</span> System.Threading;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Controls;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Media;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Touch;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Networking;</li>
<li><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> CompassDriverExample</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> Gadgeteer.Modules.Seeed.<span style="color:#2b91af;">Compass</span> compass; <span style="color:#008000;">//Declare the compass</span></li>
<li>             </li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.compass = <span style="color:#0000ff;">new</span> GTM.Seeed.<span style="color:#2b91af;">Compass</span>(8);<span style="color:#008000;">// create an instance on socket 8</span></li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<h3>Edit the driver and test.</h3>
<p>If you have made it this far then you have a solution that builds a module from source and instantiates the module, ready for use. Have a go to see if it works as expected and all the methods are there. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image14.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb14.png?w=848&#038;h=278" width="848" height="278"></a> </p>
<p>Now down to the best bit, you can now just change the module source and deploy as you normally would. The compass project will be rebuild every time. For example you may want to know exactly what the ‘Gain’ value is and how it is set. The module has a setgain method which takes a Gain property. The property ranges from Gain1 to Gian8, if you hold your mouse over the property it will display a tooltip, but lets go look at the source.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image15.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb15.png?w=700&#038;h=413" width="700" height="413"></a> </p>
<p>If you right click on a method (and have the source available) you can select “Go to definition” and it will take you to the code – got to love Visual Studio. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image16.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb16.png?w=542&#038;h=387" width="542" height="387"></a> </p>
<p>The method source simply writes a value to a register, where the value is of type ‘Gain’ (Right click on ‘Gain’ and go to definition)</p>
<p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:93e0f644-7a20-4c2d-85d1-9c90291159a4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> SetGain(<span style="color:#2b91af;">Gain</span> gain)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            Write(<span style="color:#2b91af;">Register</span>.CRB, (<span style="color:#0000ff;">byte</span>)gain);</li>
<li style="background:#f3f3f3;">        }</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Here are the values :</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:60790097-8718-496c-9e4c-abc01ba041f3" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> <span style="color:#2b91af;">Gain</span> : <span style="color:#0000ff;">byte</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 0.88 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain1 = 0&#215;00,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 1.2 Ga (default)</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            Gain2 = 0&#215;20,</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 1.9 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain3 = 0&#215;40,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 2.5 Ga</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            Gain4 = 0&#215;60,</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 4.0 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain5 = 0&#215;80,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 4.7 Ga</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            Gain6 = 0xA0,</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 5.6 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain7 = 0xC0,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 8.1 Ga</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            Gain8 = 0xE0,</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        }</li>
</ol></div>
</p></div>
</p></div>
<p>If you rename one of these values, it will have an immediate effect on your code. For example:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c0b58aba-7e43-4f39-8e47-6506c4adb4f1" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> <span style="color:#2b91af;">Gain</span> : <span style="color:#0000ff;">byte</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 0.88 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain1RenamedToSomethingBetter = 0&#215;00,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
</ol></div>
</p></div>
</p></div>
<p>Will immediately change in your program.cs</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image17.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb17.png?w=586&#038;h=331" width="586" height="331"></a></p>
<p>Job done, you are now able to edit the driver and have the changes take effect immediately. Compile is same as always and the solution will deploy your latest code to the hardware when you deploy. This way you can edit and change code until you are satisfied. Once that is done you can then go back to the Compass SOLUTION and build an installer…..</p>
<h3>Advanced – where did these numbers come from?</h3>
<p>But you have to wonder what these values are and what do they mean? Here is where we need the datasheet. A few folders up from the source code there is a ‘Hardware’ folder that has a datasheet. I you have a custom modules or there is not datasheet you can do very well by searching for the chip number. If stuck pester the manufacturer to add a link to the datasheet for the module.&nbsp; </p>
<p>This compass module is a HMC5883 and there is a data sheet in the Hardware folder (in the downloaded zip from codeplex) :</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image18.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb18.png?w=628&#038;h=142" width="628" height="142"></a> </p>
<p> These can be hard to read, but we can use the bits we are interested in. For example searching the datasheet for ‘Gain’ shows that there is a gain control and that there are 3 bits dedicated to changing it. </p>
<p>&nbsp;<a href="http://stevenjohnston.files.wordpress.com/2013/05/image19.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb19.png?w=635&#038;h=385" width="635" height="385"></a></p>
<p>Further on in the datasheet there is a section about ‘configuration register B’ this is for setting the gain. Remember that the gain method just writes a byte (8 bits) to a register (Register B), table 10 lets us know that the register is 1 byte (8 bits) – same as our code. So now we just need to check the values that are to be written. Table 10 on page 13 shows that bits 7,6,5 are for setting the gain and 4-0 must be set to zero. Table 12 shows the different gain values and shows how this changes the output. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image20.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb20.png?w=611&#038;h=657" width="611" height="657"></a> </p>
<p>For example the default gain is 1024 counts/Gauss and this is done by setting the 8 bits of the register to [0,0,1,0,0,0,0,0] but if we look at the code the values are written in hex not binary. </p>
<p>If you are not comfortable converting, believe it or not <em>calculator</em> is your friend! Change calculator into programmer mode: </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image21.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb21.png?w=656&#038;h=376" width="656" height="376"></a> </p>
<p>Select binary and then input the binary you want to convert, in this case [0,0,1,0,0,0,0,0]&nbsp; (note you do not need to leading zeros)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image22.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb22.png?w=509&#038;h=471" width="509" height="471"></a> </p>
<p>If you then select hex, it will convert the value. (Answer 20)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image23.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb23.png?w=507&#038;h=469" width="507" height="469"></a> </p>
<p>Since the compiler would not know if we meant 20 (twenty) or 20 (thirty two in decimal,or binary [0,0,1,0,0,0,0,0]) there is a prefix added to a number to let it know that it is hex, the prefix is ‘0x’. And sure enough if we look at the code the default gain is ‘0&#215;20’ which is the correct value according to the data sheet. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3cd2f6be-bc73-4814-b593-0981991a9c70" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> <span style="color:#2b91af;">Gain</span> : <span style="color:#0000ff;">byte</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 0.88 Ga</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">            Gain1RenamedToSomethingBetter = 0&#215;00,</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> +/- 1.2 Ga (default)</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            Gain2 = 0&#215;20,</li>
</ol></div>
</p></div>
</p></div>
<p>Have a look around the datasheet, perhaps there are hardware features that are not implemented in code, so you can improve the performance, enhance the driver, solve bugs or trouble shoot issues. </p>
<p>For example register <strong>A</strong> can be used to set the sensor rate (default 15Hz) as well as a bias for different axis, but if you right click on the register name and select ‘find all references’, it shows that the driver never uses it….</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image24.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb24.png?w=724&#038;h=433" width="724" height="433"></a> </p>
<p>Happy Gadgeteering…. check <a href="http://www.gadgeteering.net">www.gadgeteering.net</a> for a complete list of hardware.</p>
<h3>Tips</h3>
<p>If you are using the express version of Visual Studio it only supports one language. So if you have a VB project and import a C# project, you wont get any debug and things will look a bit strange. </p>
<p>Remember that since you are compiling from source you can now add a break point inside the driver! (F9) This is a fantastic way to halt execution and inspect what is going on, with out having to write tonnes of output lines……</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/426/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=426&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/05/23/i-want-to-edit-the-net-gadgeteer-drivers-for-a-particular-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb11.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb15.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb16.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb17.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb18.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb19.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb20.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb21.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb22.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb23.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb24.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Gadgeteer is causing me to get a BSOD</title>
		<link>http://stevenjohnston.co.uk/2013/05/02/net-gadgeteer-is-causing-me-to-get-a-bsod/</link>
		<comments>http://stevenjohnston.co.uk/2013/05/02/net-gadgeteer-is-causing-me-to-get-a-bsod/#comments</comments>
		<pubDate>Thu, 02 May 2013 10:02:30 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[BSOD]]></category>
		<category><![CDATA[Drivers]]></category>
		<category><![CDATA[GHI]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=374</guid>
		<description><![CDATA[There is a case, where you can get a blue screen of death (BSOD) where your computer just dies/reboots, usually &#8230;<p><a href="http://stevenjohnston.co.uk/2013/05/02/net-gadgeteer-is-causing-me-to-get-a-bsod/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=374&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>There is a case, where you can get a blue screen of death (BSOD) where your computer just dies/reboots, usually during a NetMF or .NET Gadgeteer deploy and especially if you hit the reset button (on the device) or attempt to pull out the USB cable. </p>
<p>This problem is to do with the USB driver and it has been addressed, so why are you still getting it? Well you are using the wrong driver <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . The old drivers run in kernel mode and hence can cause a BSOD, but this has since been switched to WinUSB which runs in user mode, so cannot BSOD. There are some issues with WinUSB and virtualised installs and not all hardware supports it. </p>
<p>The solution: swap the driver. Note this does not work for all hardware. If you cannot swap the driver for various known reasons. Learn to recognise the symptoms to avoid a BSOD. Usually Visual studio just sits on the deployment stage, if you do almost anything it will BSOD. However if you click ‘Build –&gt; Cancel’ and wait 2 sec it will cancel and you can reset/redeploy. (Note on VS Express you don&#8217;t have this menu item.)</p>
<p>GHI have a great article on this <a title="http://www.ghielectronics.com/docs/109/usb-drivers-choices-including-winusb" href="http://www.ghielectronics.com/docs/109/usb-drivers-choices-including-winusb">http://www.ghielectronics.com/docs/109/usb-drivers-choices-including-winusb</a></p>
<p>The steps below let you check to see what driver you are using and how to swap it. (Use the GHI description for more info or if you get stuck)</p>
<h3>What driver am I currently using?</h3>
<p>During a deploy I hit the reset button on my spider today and it rebooted my laptop, strange as I thought I was using the WinUSB driver. Open Device Manager and look for the Gadgeteer device. In the case of a Spider mainboard it will appear as a .NET debugable device. Here is the device driver information for the old USB drivers, so turns out I am running the wrong one, hence the BSOD. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/driverthatbsod.png"><img title="DriverThatBSOD" style="display:inline;border-width:0;" border="0" alt="DriverThatBSOD" src="http://stevenjohnston.files.wordpress.com/2013/05/driverthatbsod_thumb.png?w=386&#038;h=434" width="386" height="434"></a> </p>
<h3>Swapping the driver.</h3>
<p>If you click “Update Driver” you can navigate to the GHI drivers directory and select the WinUSB driver. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb.png?w=434&#038;h=129" width="434" height="129"></a> </p>
<p>Once that has completed you will see that new drivers have been loaded. Horary! The spider mainboard also now appears under a different section in device manager.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image1.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb1.png?w=416&#038;h=277" width="416" height="277"></a> </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/05/image2.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb2.png?w=401&#038;h=451" width="401" height="451"></a> </p>
<p>(WinUSB drivers)</p>
<p>&nbsp;</p>
<h3>Notes:</h3>
<p>GHI firmware from 14Feb (listed as updated 18 Feb) 2013</p>
<p>VS 2010</p>
<p>Spider mainboard. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/374/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/374/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=374&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/05/02/net-gadgeteer-is-causing-me-to-get-a-bsod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/driverthatbsod_thumb.png" medium="image">
			<media:title type="html">DriverThatBSOD</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>LED Strip with RGB slider</title>
		<link>http://stevenjohnston.co.uk/2013/04/17/led-strip-with-rgb-slider/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/17/led-strip-with-rgb-slider/#comments</comments>
		<pubDate>Wed, 17 Apr 2013 14:59:58 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=354</guid>
		<description><![CDATA[I swapped the ColourSense (http://gadgeteering.net/module/ghi-colorsense)&#160; module for a triple slider (http://gadgeteering.net/module/soldermonkey-triple-slider-module).<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=354&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I swapped the ColourSense (<a title="http://gadgeteering.net/module/ghi-colorsense" href="http://gadgeteering.net/module/ghi-colorsense">http://gadgeteering.net/module/ghi-colorsense</a>)&nbsp; module for a triple slider (<a title="http://gadgeteering.net/module/soldermonkey-triple-slider-module" href="http://gadgeteering.net/module/soldermonkey-triple-slider-module">http://gadgeteering.net/module/soldermonkey-triple-slider-module</a>). </p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:420152de-f61e-40e6-8c99-3bf5b6a57c80" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div><span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='529' height='328' src='http://www.youtube.com/embed/SyfZuZHKsVs?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span></div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/354/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=354&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/17/led-strip-with-rgb-slider/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>
	</item>
		<item>
		<title>Colour sensor + LED strip video</title>
		<link>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-led-strip-video/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-led-strip-video/#comments</comments>
		<pubDate>Mon, 15 Apr 2013 18:02:56 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=348</guid>
		<description><![CDATA[[UPDATE] RGB Slider &#8211; LED Strip with RGB slider&#160; As requested here is an example of my colour sensor in &#8230;<p><a href="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-led-strip-video/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=348&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>[UPDATE] RGB Slider &#8211; <a title="LED Strip with RGB slider" href="http://stevenjohnston.co.uk/2013/04/17/led-strip-with-rgb-slider/">LED Strip with RGB slider</a>&nbsp;</p>
<p>As requested here is an example of my colour sensor in continuous mode setting the colours of the LED strip. The code below uses the tweaks shown in these two posts: </p>
<p>1) <a title="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/" href="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/">http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/</a></p>
<p>2) <a title="http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/" href="http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/">http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/</a></p>
<h3>Video</h3>
<p>The colour changes are much better that this video captures but it is hard to record them (Rather than looking at the ‘white’ over saturated LED, look at the surrounding colour!) In the first few seconds of the video I reset the entire 5m of LEDs and you can see the delay in sending the messages all the way to the last LED. Running the SPI bus at 14MHz I get a refresh of about 5hz (5 full refreshes per second)</p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:4c4d95ea-4f46-4968-8812-4d26337fd301" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div><span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='529' height='328' src='http://www.youtube.com/embed/Fo0yq2GQGaU?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span></div>
</div>
<h3>Source code</h3>
<p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:85df8299-90f0-4bd7-a63f-fd5bf7e66b52" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections;</li>
<li><span style="color:#0000ff;">using</span> System.Threading;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Controls;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Media;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Touch;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Networking;</li>
<li><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Interfaces;</li>
<li><span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Modules.GHIElectronics;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> MagicLights</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">FEZtive</span> feztive;</li>
<li>        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Calibrate the colour sensor</span></li>
<li>            colorSense.SetIntegrationTime(<span style="color:#2b91af;">ColorSense</span>.<span style="color:#2b91af;">TimingRegister</span>.FallingEdge_FreeRunning_400ms);<span style="color:#008000;">//Set the time span for integration</span></li>
<li style="background:#f3f3f3;">            colorSense.SetGain(<span style="color:#2b91af;">ColorSense</span>.<span style="color:#2b91af;">GainRegister</span>.Gain16x_PreScalerDivide4);<span style="color:#008000;">//Set Gain and PreScalar. </span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Init the LED strip (test other SPI bus speeds)</span></li>
<li>            feztive = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">FEZtive</span>(9);</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//feztive.Initialize(160, 500);//OK</span></li>
<li>            <span style="color:#008000;">//feztive.Initialize(160, 1000);//OK</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//feztive.Initialize(160, 2000);//OK</span></li>
<li>            <span style="color:#008000;">//feztive.Initialize(160, 4000);//OK</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//feztive.Initialize(160, 8000);//OK</span></li>
<li>            <span style="color:#008000;">//feztive.Initialize(160, 16000);//Some LEDS are the wrong colour</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//feztive.Initialize(160, 12000);//OK</span></li>
<li>            <span style="color:#008000;">//feztive.Initialize(160, 14000);//OK</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//feztive.Initialize(160, 15000);//Some LEDS are the wrong colour</span></li>
<li>            <span style="color:#008000;">//feztive.Initialize(160, 14500);//Some LEDS are the wrong colour</span></li>
<li style="background:#f3f3f3;">            feztive.Initialize(160, 14000);<span style="color:#008000;">//OK</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Show some colours</span></li>
<li>            feztive.SetAll(feztive.Black);</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Set every LED to Red</span></li>
<li>            feztive.SetAll(feztive.Red);</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Set every LED to Red</span></li>
<li>            feztive.SetAll(feztive.Red);</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Set every LED to Green</span></li>
<li>            feztive.SetAll(feztive.Green);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#008000;">//Toggle the colour sensor white LEDs</span></li>
<li style="background:#f3f3f3;">            button.ButtonPressed += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonEventHandler</span>(button_ButtonPressed);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Threads: <a href="http://blogs.msdn.com/b/netmfteam/archive/2011/01/17/threads-and-thread-priorities-in-netmf.aspx" rel="nofollow">http://blogs.msdn.com/b/netmfteam/archive/2011/01/17/threads-and-thread-priorities-in-netmf.aspx</a></span></li>
<li>            <span style="color:#008000;">//Dont block the main thread/dispatcher          </span></li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">Thread</span> t = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Thread</span>(ContiniousReader);</li>
<li>            t.Start();</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//Some variables for debug</span></li>
<li>        <span style="color:#0000ff;">uint</span> r = 0, g = 0, b = 0, c = 0;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> ContiniousReader()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Loop forever &#8211; seperate threads!</span></li>
<li>            <span style="color:#0000ff;">while</span> (<span style="color:#0000ff;">true</span>)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#2b91af;">ColorSense</span>.<span style="color:#2b91af;">ColorChannels</span> cChan = colorSense.ReadColorChannels();</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (cChan.Red &gt; r) { r = cChan.Red; }</li>
<li>                <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;RED: &quot;</span> + cChan.Red + <span style="color:#a31515;">&quot; MAX&quot;</span> + r);<span style="color:#008000;">//DEBUG &#8211; check for saturation</span></li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (cChan.Green &gt; g) { g = cChan.Green; }</li>
<li>                <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;GREEN: &quot;</span> + cChan.Green + <span style="color:#a31515;">&quot; MAX&quot;</span> + g);</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (cChan.Blue &gt; b) { b = cChan.Blue; }</li>
<li>                <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;BLUE: &quot;</span> + cChan.Blue + <span style="color:#a31515;">&quot; MAX&quot;</span> + b);</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (cChan.Clear &gt; c) { c = cChan.Clear; }</li>
<li>                <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;CLEAR: &quot;</span> + cChan.Clear + <span style="color:#a31515;">&quot; MAX&quot;</span> + c);</li>
<li style="background:#f3f3f3;">                GTM.GHIElectronics.<span style="color:#2b91af;">Color</span> newColour = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">Color</span>(</li>
<li>                  normaliseColourChannel(cChan.Red),</li>
<li style="background:#f3f3f3;">                  normaliseColourChannel(cChan.Green),</li>
<li>                  normaliseColourChannel(cChan.Blue));</li>
<li style="background:#f3f3f3;">                feztive.SetAll(newColour);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            }</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Take values in the range 0-65k and scale to 0-127       </span></li>
<li>        <span style="color:#0000ff;">byte</span> normaliseColourChannel(<span style="color:#0000ff;">uint</span> value)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">var</span> res = ((value / 65536.0) * 127.0);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> (<span style="color:#0000ff;">byte</span>)res;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">bool</span> ledStatus = <span style="color:#0000ff;">false</span>;<span style="color:#008000;">//Last known state</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> button_ButtonPressed(<span style="color:#2b91af;">Button</span> sender, <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonState</span> state)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            ledStatus = !ledStatus;</li>
<li>            colorSense.ToggleOnboardLED(ledStatus);</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/348/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=348&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-led-strip-video/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>
	</item>
		<item>
		<title>Colour sensor on .NET Gadgeteer (TCS3414)</title>
		<link>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/#comments</comments>
		<pubDate>Mon, 15 Apr 2013 16:21:58 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=341</guid>
		<description><![CDATA[Last post I was looking at the individually addressable LEDs and used the ‘ColorSense’ module to detect colours. (But did &#8230;<p><a href="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=341&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Last post I was looking at the <a href="http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/">individually addressable LEDs</a> and used the ‘ColorSense’ module to detect colours. (But did not get very good results)</p>
<p>This post addresses the issues with the colour sensor. </p>
<p>I am using the ColorSense REV 1.2 which has a TCS3414 digital colour sensor and 4 white LEDs to illuminate any sources. Each sensor has 16 photodiodes, 4 per channel (R,G,B,Clear)&nbsp; each channel has a 16 bit ADC and we are communicating to the module using I2C. The original driver form GHI can be found in Codeplex: <a title="http://gadgeteer.codeplex.com/SourceControl/changeset/view/24955#333941" href="http://gadgeteer.codeplex.com/SourceControl/changeset/view/24955#333941">http://gadgeteer.codeplex.com/SourceControl/changeset/view/24955#333941</a></p>
<p>There are a few settings you may wish to change in order to calibrate she sensor for the task you have in mind. (you may even want to change these values during operation)</p>
<h3>1) Integration time.</h3>
<p>You can change the length of time that the sensor takes per integration cycle. There are 3 settings and 12ms is the default. (10ms / 400ms being the other options.) The code to enable changes to this is shown below. (If you are getting saturated, lower the integration time)</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:61e1bb2f-84b3-4470-b1d3-0a8c11324cab" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">TimingRegister</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#008000;">//http://www.ghielectronics.com/downloads/man/ColorSense_TCS3404_TCS3414-A.pdf</span></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;81;</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_12ms = 0&#215;00;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_100ms = 0&#215;01;</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_400ms = 0&#215;02;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3fcdce4f-4686-4d69-aba7-e7bd4cbf1147" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> SetIntegrationTime(<span style="color:#0000ff;">byte</span> value)</li>
<li style="background:#f3f3f3;">{</li>
<li>    softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">TimingRegister</span>.RegisterAddress, value});</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<h3>2) Setting the Gain and the prescalar.</h3>
<p>The gain and prescalar are set in a single operation. The gain provides “gain control for all four parallel ADC channels” and can be used to ensure the ADC range is modified to avoid saturation. I.e. if the output is always low, then increasing the gain will improve the sensitivity and the channel outputs will increase. The gain can be changed to 4 discrete multipliers, 1x, 4x, 16x and 64x – 64x would make the sensor very sensitive. (Saturation can be avoided by lowering the integration time)</p>
<p>The prescalar divides the ADC output, thus reducing the sensitivity of the ADC. There are 7 different prescalars -&nbsp; divide by 1,2,4,8,16,32,64.</p>
<p>I have put the gain and prescalar options into a struct with a supporting method to change them:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46f290d4-d990-4078-824f-f5c7bab46209" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">GainRegister</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;87;</li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//Reserved bit (7 = high)</span></li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Reserved = 0&#215;80;</li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//Gain &#8211; This field switches the common analog gain of the four ADC channels. Four gain modes are provide</span></li>
<li>    <span style="color:#008000;">//bit 5:4</span></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x = 0&#215;00;</li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x = 0&#215;10;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x = 0&#215;20;</li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x = 0&#215;30;</li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//Prescaler. This field controls a 6-bit digital prescaler and divider. The prescaler reduces the </span></li>
<li>    <span style="color:#008000;">//sensitivity of each ADC integrator as shown below:</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//Bit 2:0</span></li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide1 = 0&#215;00;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide2 = 0&#215;01;</li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide4 = 0&#215;02;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide8 = 0&#215;03;</li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide16 = 0&#215;04;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide32 = 0&#215;05;</li>
<li>    <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide64 = 0&#215;06;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide1);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide1);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide1);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide1);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide2);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide4);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide8);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide16);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide32);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide64);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide2);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide4);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide8);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide16);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide32);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide64);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide2);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide4);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide8);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide16);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide32);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide64);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide2);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide4);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide8);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide16);</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide64);</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide64);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:731116d8-e8ba-4779-bb03-c2aa66892353" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> SetGain(<span style="color:#0000ff;">byte</span> value)</li>
<li style="background:#f3f3f3;">{</li>
<li>    softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">GainRegister</span>.RegisterAddress,value });</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<h3>3) Enable disable</h3>
<p>It may also be useful to enable/disable the module. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:492354a8-3cb1-4056-8159-7a7d69464c1e" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">ControlRegister</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;80;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> EnableADC_PowerOn = 0&#215;03;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> DisableADC_PowerOff = 0&#215;00;</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:314f3748-df21-4fd9-bd37-0a4059d234bb" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> EnableSensor(<span style="color:#0000ff;">bool</span> enable)</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">if</span> (enable)</li>
<li style="background:#f3f3f3;">    {</li>
<li>        softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">ControlRegister</span>.RegisterAddress, <span style="color:#2b91af;">ControlRegister</span>.EnableADC_PowerOn });</li>
<li style="background:#f3f3f3;">    }</li>
<li>    <span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>        softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">ControlRegister</span>.RegisterAddress, <span style="color:#2b91af;">ControlRegister</span>.DisableADC_PowerOff });</li>
<li style="background:#f3f3f3;">    }</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<h3>Example</h3>
<p>For the task I have in mind setting the following provides me with RGB values between 0-65k and rarely gets saturated (About as sensitive as I dare push it) . Note there are public methods on this module to change these parameters. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9e234b6e-468e-4942-9cd5-d26333ea22be" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">this</span>.EnableSensor(<span style="color:#0000ff;">true</span>);<span style="color:#008000;">//Power sensor on</span></li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">this</span>.SetIntegrationTime(<span style="color:#2b91af;">TimingRegister</span>.FallingEdge_FreeRunning_100ms);<span style="color:#008000;">//Set the time span for integration</span></li>
<li><span style="color:#0000ff;">this</span>.SetGain(<span style="color:#2b91af;">GainRegister</span>.Gain64x_PreScalerDivide2);<span style="color:#008000;">//Set Gain and PreScalar.</span></li>
</ol></div>
</p></div>
</p></div>
<h3>Complete code below </h3>
</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a4fb905a-5e89-4472-8972-35bbdc0dedba" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li><span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GTI = Gadgeteer.Interfaces;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> Gadgeteer.Modules.GHIElectronics</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//http://www.ghielectronics.com/downloads/man/ColorSense_TCS3404_TCS3414-A.pdf    </span></li>
<li>    <span style="color:#008000;">//ADDRESS REGISTER NAME REGISTER FUNCTION &#8211; Set MSB high ( OR register address with 0&#215;80)</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">// COMMAND Specifies register address</span></li>
<li>    <span style="color:#008000;">//00h CONTROL Control of basic functions</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//01h TIMING Integration time/gain control</span></li>
<li>    <span style="color:#008000;">//02h INTERRUPT Interrupt control</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//03h INT SOURCE Interrupt source</span></li>
<li>    <span style="color:#008000;">//04h ID Part number/ Rev ID</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//07h GAIN ADC gain control</span></li>
<li>    <span style="color:#008000;">//08h LOW_THRESH_LOW_BYTE Low byte of low interrupt threshold</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//09h LOW_THRESH_HIGH_BYTE High byte of low interrupt threshold</span></li>
<li>    <span style="color:#008000;">//0Ah HIGH_THRESH_LOW_BYTE Low byte of high interrupt threshold</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//0Bh HIGH_THRESH_HIGH_BYTE High byte of high interrupt threshold</span></li>
<li>    <span style="color:#008000;">//0Fh  SMBus block read (10h through 17h)</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//10h DATA1LOW Low byte of ADC green channel</span></li>
<li>    <span style="color:#008000;">//11h DATA1HIGH High byte of ADC green channel</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//12h DATA2LOW Low byte of ADC red channel</span></li>
<li>    <span style="color:#008000;">//13h DATA2HIGH High byte of ADC red channel</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//14h DATA3LOW Low byte of ADC blue channel</span></li>
<li>    <span style="color:#008000;">//15h DATA3HIGH High byte of ADC blue channel</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">//16h DATA4LOW Low byte of ADC clear channel</span></li>
<li>    <span style="color:#008000;">//17h DATA4HIGH High byte of ADC clear channel</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>    <span style="color:#808080;">///</span><span style="color:#008000;"> A ColorSense module for Microsoft .NET Gadgeteer</span></li>
<li style="background:#f3f3f3;">    <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">ColorSense</span> : GTM.<span style="color:#2b91af;">Module</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> GTI.<span style="color:#2b91af;">DigitalOutput</span> LEDControl;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> GTI.<span style="color:#2b91af;">SoftwareI2C</span> softwareI2C;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span>[] readRegisterData = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[2];</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">byte</span> colorAddress = 0&#215;39;<span style="color:#008000;">//Address for the Colour sense module on I2C</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Note: A constructor summary is auto-generated by the doc builder.</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;socketNumber&quot;&gt;</span><span style="color:#008000;">The socket that this module is plugged in to.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> ColorSense(<span style="color:#0000ff;">int</span> socketNumber)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">Socket</span> socket = <span style="color:#2b91af;">Socket</span>.GetSocket(socketNumber, <span style="color:#0000ff;">true</span>, <span style="color:#0000ff;">this</span>, <span style="color:#0000ff;">null</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            socket.EnsureTypeIsSupported(<span style="color:#0000ff;">new</span> <span style="color:#0000ff;">char</span>[] { <span style="color:#a31515;">&#039;X&#039;</span>, <span style="color:#a31515;">&#039;Y&#039;</span> }, <span style="color:#0000ff;">this</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            LEDControl = <span style="color:#0000ff;">new</span> GTI.<span style="color:#2b91af;">DigitalOutput</span>(socket, <span style="color:#2b91af;">Socket</span>.<span style="color:#2b91af;">Pin</span>.Three, <span style="color:#0000ff;">false</span>, <span style="color:#0000ff;">this</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            softwareI2C = <span style="color:#0000ff;">new</span> GTI.<span style="color:#2b91af;">SoftwareI2C</span>(socket, <span style="color:#2b91af;">Socket</span>.<span style="color:#2b91af;">Pin</span>.Five, <span style="color:#2b91af;">Socket</span>.<span style="color:#2b91af;">Pin</span>.Four, <span style="color:#0000ff;">this</span>);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#008000;">// Send COMMAND to access control register for chip power-up</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Send to power-up chip</span></li>
<li>            <span style="color:#0000ff;">this</span>.EnableSensor(<span style="color:#0000ff;">true</span>);<span style="color:#008000;">//Power sensor on</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.SetIntegrationTime(<span style="color:#2b91af;">TimingRegister</span>.FallingEdge_FreeRunning_100ms);<span style="color:#008000;">//Set the time span for integration</span></li>
<li>            <span style="color:#0000ff;">this</span>.SetGain(<span style="color:#2b91af;">GainRegister</span>.Gain64x_PreScalerDivide2);<span style="color:#008000;">//Set Gain and PreScalar. </span></li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> Turn the sensor on/off</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;enable&quot;&gt;&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> EnableSensor(<span style="color:#0000ff;">bool</span> enable)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">if</span> (enable)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">ControlRegister</span>.RegisterAddress, <span style="color:#2b91af;">ControlRegister</span>.EnableADC_PowerOn });</li>
<li>            }</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">else</span></li>
<li>            {</li>
<li style="background:#f3f3f3;">                softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">ControlRegister</span>.RegisterAddress, <span style="color:#2b91af;">ControlRegister</span>.DisableADC_PowerOff });</li>
<li>            }</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> The TIMING register controls the synchronization and integration time of the ADC channels. The Timing</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Register settings apply to all four ADC channels.</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> SetIntegrationTime(<span style="color:#0000ff;">byte</span> value)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">TimingRegister</span>.RegisterAddress, value});</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> <a href="http://www.ghielectronics.com/downloads/man/ColorSense_TCS3404_TCS3414-A.pdf" rel="nofollow">http://www.ghielectronics.com/downloads/man/ColorSense_TCS3404_TCS3414-A.pdf</a></span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> The Gain register provides a common gain control adjustment for all four parallel ADC output channels. Two</span></li>
<li>        <span style="color:#008000;">//gain bits [5:4] in the Gain Register allow the relative gain to be adjusted from 1 to 64 in 4 increments. The</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//advantage of the gain adjust is to extend the dynamic range of the light input up to a factor of 64 before analog</span></li>
<li>        <span style="color:#008000;">//or digital saturation occurs. If analog saturation has occurred, lowering the gain sensitivity will likely prevent</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//analog saturation especially when the integration time is relatively short. For longer integration times, the 16-bit</span></li>
<li>        <span style="color:#008000;">//output could be in digital saturation (64K). If lowering the gain to 1 does not prevent digital saturation from</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//occurring, the use of PRESCALER can be useful.</span></li>
<li>        <span style="color:#008000;">//The PRESCALER is 3 bits [2:0] in the gain register that divides down the output count (i.e. shifts the LSB of the</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//count value to the right). The PRESCALER adjustment range is divide by 1 to 64 in multiples of 2.</span></li>
<li>        <span style="color:#008000;">//The most sensitive gain setting of the device would be when GAIN is set to 11b (64), and PRESCALER is set</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//to 000b (divide by 1). The least sensitive part setting would be GAIN 00 (1) and PRESCALER 110 (divide by</span></li>
<li>        <span style="color:#008000;">//64). If the part continues to be in digital saturation at the least sensitive setting, the integration time can be</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//lowered (see Timing Register section).</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;</span><span style="color:#008000;">GainRegister value </span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> SetGain(<span style="color:#0000ff;">byte</span> value)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            softwareI2C.Write(colorAddress, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { <span style="color:#2b91af;">GainRegister</span>.RegisterAddress,value });</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> Toggles the on-board LEDs to the passed in state.</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;LEDState&quot;&gt;</span><span style="color:#008000;">State to set the LEDs to.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> ToggleOnboardLED(<span style="color:#0000ff;">bool</span> LEDState)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            LEDControl.Write(LEDState);</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Reads the current color from the sensor and returns the results</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;returns&gt;</span><span style="color:#008000;">Returns an instance of the ColorChannels structure, holding the current measurement of color values.</span><span style="color:#808080;">&lt;/returns&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#2b91af;">ColorChannels</span> ReadColorChannels()</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">ColorChannels</span> returnData;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#0000ff;">byte</span>[] TransmitBuffer = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[1];</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            TransmitBuffer[0] = 0&#215;90; <span style="color:#008000;">// Send COMMAND to access Green Color Channel register for chip</span></li>
<li style="background:#f3f3f3;">            returnData.Green = readWord(0&#215;90, TransmitBuffer)[0];</li>
<li>            TransmitBuffer[0] = 0&#215;91;</li>
<li style="background:#f3f3f3;">            returnData.Green |= (<span style="color:#0000ff;">uint</span>)readWord(0&#215;91, TransmitBuffer)[0] &lt;&lt; 8;</li>
<li>            <span style="color:#008000;">//returnData.Green = 256 * (uint)readWord(TransmitBuffer)[0] + returnData.Green;</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            TransmitBuffer[0] = 0&#215;92; <span style="color:#008000;">// Send COMMAND to access Red Color Channel register for chip</span></li>
<li style="background:#f3f3f3;">            returnData.Red = readWord(0&#215;92, TransmitBuffer)[0];</li>
<li>            TransmitBuffer[0] = 0&#215;93;</li>
<li style="background:#f3f3f3;">            returnData.Red |= (<span style="color:#0000ff;">uint</span>)readWord(0&#215;93, TransmitBuffer)[0] &lt;&lt; 8;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            TransmitBuffer[0] = 0&#215;94; <span style="color:#008000;">// Send COMMAND to access Blue Color Channel register for chip</span></li>
<li>            returnData.Blue = readWord(0&#215;94, TransmitBuffer)[0];</li>
<li style="background:#f3f3f3;">            TransmitBuffer[0] = 0&#215;95;</li>
<li>            returnData.Blue |= (<span style="color:#0000ff;">uint</span>)readWord(0&#215;95, TransmitBuffer)[0] &lt;&lt; 8;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            TransmitBuffer[0] = 0&#215;96; <span style="color:#008000;">// Send COMMAND to access Clear Channel register for chip</span></li>
<li style="background:#f3f3f3;">            returnData.Clear = readWord(0&#215;96, TransmitBuffer)[0];</li>
<li>            TransmitBuffer[0] = 0&#215;97;</li>
<li style="background:#f3f3f3;">            returnData.Clear |= (<span style="color:#0000ff;">uint</span>)readWord(0&#215;97, TransmitBuffer)[0] &lt;&lt; 8;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> returnData;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">byte</span>[] readWord(<span style="color:#0000ff;">byte</span> address, <span style="color:#0000ff;">byte</span>[] CommandBytes)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            softwareI2C.WriteRead(colorAddress, CommandBytes, readRegisterData);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">return</span> readRegisterData;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> Structure to hold color data</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">ColorChannels</span></li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> Intensity of green-filtered channel</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">uint</span> Green;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> Intensity of red-filtered channel</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">uint</span> Red;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> Intensity of blue-filtered channel</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">uint</span> Blue;</li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>            <span style="color:#808080;">///</span><span style="color:#008000;"> Intensity of non-filtered channel</span></li>
<li style="background:#f3f3f3;">            <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">uint</span> Clear;</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> Enable / disable</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">ControlRegister</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;80;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> EnableADC_PowerOn = 0&#215;03;</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> DisableADC_PowerOff = 0&#215;00;</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> Integration time</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">TimingRegister</span></li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;81;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_12ms = 0&#215;00;</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_100ms = 0&#215;01;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> FallingEdge_FreeRunning_400ms = 0&#215;02;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> ADC Gain control- Gain = ADC adjustment, PreScaler = divides down the digital output</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">struct</span> <span style="color:#2b91af;">GainRegister</span></li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> RegisterAddress = 0&#215;87;</li>
<li>            <span style="color:#008000;">//Reserved bit (7 = high)</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Reserved = 0&#215;80;</li>
<li>            <span style="color:#008000;">//Gain &#8211; This field switches the common analog gain of the four ADC channels. Four gain modes are provide</span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//bit 5:4</span></li>
<li>            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x = 0&#215;00;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x = 0&#215;10;</li>
<li>            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x = 0&#215;20;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x = 0&#215;30;</li>
<li>            <span style="color:#008000;">//Prescaler. This field controls a 6-bit digital prescaler and divider. The prescaler reduces the </span></li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//sensitivity of each ADC integrator as shown below:</span></li>
<li>            <span style="color:#008000;">//Bit 2:0</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide1 = 0&#215;00;</li>
<li>            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide2 = 0&#215;01;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide4 = 0&#215;02;</li>
<li>            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide8 = 0&#215;03;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide16 = 0&#215;04;</li>
<li>            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide32 = 0&#215;05;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> PreScalerDivide64 = 0&#215;06;</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide1);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide1);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide1);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide1 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide1);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide2);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide4);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide8);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide16);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide32);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain1x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain1x | PreScalerDivide64);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide2);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide4);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide8);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide16);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide32);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain4x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain4x | PreScalerDivide64);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide2);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide4);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide8);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide16);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide32);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain16x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain16x | PreScalerDivide64);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide2 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide2);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide4 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide4);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide8 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide8);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide16 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide16);</li>
<li>            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide32 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide64);</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">byte</span> Gain64x_PreScalerDivide64 = (<span style="color:#0000ff;">byte</span>)(Reserved | Gain64x | PreScalerDivide64);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/341/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/341/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=341&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>
	</item>
		<item>
		<title>Individually addressable multicolour LED&#8217;s on .NET Gadgeteer</title>
		<link>http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/#comments</comments>
		<pubDate>Thu, 11 Apr 2013 19:25:26 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=334</guid>
		<description><![CDATA[Who does not like LED’s? They are fun and amazing, especially if you by a 5m strip of individually addressable &#8230;<p><a href="http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=334&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Who does not like LED’s? They are fun and amazing, especially if you by a 5m strip of individually addressable multicolour LED’s. I had a whole pile of important work to get done today but for some reason the FEZtive kit I ordered ages ago was just begging to be used. More importantly I found a power supply that will give 10A@5v so I had to have a play. </p>
<p>The first hurdle was finding out how to power the module 160&#215;3 LEDs (RGB) is a lot to power and GHI recommend a good power supply which is attached to the FEZtive module board part A, this way you are not drawing power through the .NET Gadgeteer mainboard. In the picture below you can see my power connector. (Not I have not split the PCB as I only need one connector for now and this way I don&#8217;t loose the other half.)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_005.jpg"><img title="WP_20130411_005" style="display:inline;border-width:0;" border="0" alt="WP_20130411_005" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_005_thumb.jpg?w=674&#038;h=381" width="674" height="381"></a></p>
<p>Next I need to connect the connect the LED strip to the PCB, my strip had a connector on each end and as I planed to run this as a single strip, I cut the connector off the end and attached it to the PCB. This way I can disconnect the LED strip. Now I have power, data (Gadgeteer cable) and a connector for the lights. </p>
<p>&nbsp;<a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_006.jpg"><img title="WP_20130411_006" style="display:inline;border-width:0;" border="0" alt="WP_20130411_006" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_006_thumb.jpg?w=676&#038;h=382" width="676" height="382"></a> </p>
<p>Connect it all up and we are ready for some software….</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_007.jpg"><img title="WP_20130411_007" style="display:inline;border-width:0;" border="0" alt="WP_20130411_007" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_007_thumb.jpg?w=669&#038;h=378" width="669" height="378"></a> </p>
<p>There is a great example of how the use the module here : <a title="http://wiki.tinyclr.com/index.php?title=FEZtive_Module" href="http://wiki.tinyclr.com/index.php?title=FEZtive_Module">http://wiki.tinyclr.com/index.php?title=FEZtive_Module</a>&nbsp;</p>
<p>At the time of writing the FEZtive module is not part of the main GHI installer (I guess it will be in time) so you need to download the project from the link above. Be sure to add this project to your solution (or new project),</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/image4.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb4.png?w=671&#038;h=382" width="671" height="382"></a>&nbsp;</p>
<p>Next add a reference from your project to the FEZtive project you just downloaded. (I am using NetMF 4.2 so add a reference to the _42 project.)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/image5.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb5.png?w=649&#038;h=384" width="649" height="384"></a> </p>
<p>Since you cannot drag and drop the module from the designer (not yet) you need to create the object on the socket that you are using, I have a Spider with SPI on socket 9. Here is the example code from: <a title="http://wiki.tinyclr.com/index.php?title=FEZtive_Module" href="http://wiki.tinyclr.com/index.php?title=FEZtive_Module">http://wiki.tinyclr.com/index.php?title=FEZtive_Module</a>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fc9a3bec-62bf-4830-b238-91fcff4edeb8" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#008000;">// This method is run when the mainboard is powered up or reset.</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#2b91af;">FEZtive</span> feztive;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li>        feztive = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">FEZtive</span>(9);</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Initialize the module to use 80 LEDs, with an SPI clock rate of 4Mhz</span></li>
<li>        feztive.Initialize(160, 4000);</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Set every LED to Black (off)</span></li>
<li>        feztive.SetAll(feztive.Black);</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Set every LED to Red</span></li>
<li>        feztive.SetAll(feztive.Red);</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Set every LED to Red</span></li>
<li>        feztive.SetAll(feztive.Red);</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">// Set every LED to Green</span></li>
<li>        feztive.SetAll(feztive.Green);</li>
</ol></div>
</p></div>
</p></div>
<p>Fantastic this works! (It is hard to take a picture, they look better in real life)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_012.jpg"><img title="WP_20130411_012" style="display:inline;border-width:0;" border="0" alt="WP_20130411_012" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_012_thumb.jpg?w=666&#038;h=376" width="666" height="376"></a> </p>
<p>&nbsp;</p>
<h3>The FEZtive part B</h3>
<p>The LED’s are connected by 4 lines (Gnd, CI, DI, +5v) and you don&#8217;t need anything on the ‘end’ of the LED strip, as it is just one big long SPI bus. So in theory you can cut the strip (there are marked points to cut after each LED- see the white line) into different sizes. The PCB that my kit came with has 2 parts and A and a B, each with 5 areas to connect an LED strip. Think of these PCBs as U-turns in the LED strip. You can cut it into 5 x 1m strips and then zigzag them between part A and part B, note that this will make the order of the LEDs into a long snake, so if you are writing text or showing a picture you will need to calculate which LED is where. (Yes you can link multiple together – I only have one)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_0174.jpg"><img title="WP_20130411_017" style="display:inline;border-width:0;" border="0" alt="WP_20130411_017" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_017_thumb4.jpg?w=678&#038;h=383" width="678" height="383"></a> </p>
<h3>How does it work?</h3>
<p>If you look at the GHI driver for this module you can see that there is a large array (one for each LED) which contains a custom ‘colour’ object. There are a series of public methods that let you manipulate this array (change the colour) and then there is a draw method that will ‘write’ the entire array of ‘colour’ objects to the SPI bus. </p>
<p>There are some interesting effects of this. Imaging you have a LONG strip, say 5m and decide to set the colour to blue, you can see the new colour advancing along the strip. It is easier to see if you slow the clock speed down. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3554bae6-f0ba-4f01-80ec-a9182cf9cf03" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>feztive = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">FEZtive</span>(9);</li>
<li style="background:#f3f3f3;">feztive.Initialize(160, 500);</li>
</ol></div>
</p></div>
</p></div>
<p>This got me thinking, how is it working? Imagine you built a new (very large) HD TV out of this would it work? Well not exactly, you would get rather ugly ‘scan’ lines and the refresh would be poor. </p>
<h3>Change the colour</h3>
<p>I have a .NET Gadgeteer colour sensor and setup a button that would read the sensor and set all LEDs to that colour. Handy to test and a little more fun as you can point it at an object, press a button and all the LEDs change colour. </p>
<p>Here is a bit of code:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1f95da6a-58d8-4c74-b122-9627892ba1e5" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> button_ButtonPressed(<span style="color:#2b91af;">Button</span> sender, <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonState</span> state)</li>
<li style="background:#f3f3f3;">{</li>
<li>   <span style="color:#008000;">// colorSense.ToggleOnboardLED(true);</span></li>
<li style="background:#f3f3f3;">   <span style="color:#2b91af;">ColorSense</span>.<span style="color:#2b91af;">ColorChannels</span> cChan = colorSense.ReadColorChannels();            </li>
<li>   GTM.GHIElectronics.<span style="color:#2b91af;">Color</span> newColour = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">Color</span>((<span style="color:#0000ff;">byte</span>)(cChan.Red / 2), (<span style="color:#0000ff;">byte</span>)(cChan.Green / 2), (<span style="color:#0000ff;">byte</span>)(cChan.Blue / 2));</li>
<li style="background:#f3f3f3;">   <span style="color:#008000;">//  colorSense.ToggleOnboardLED(false);</span></li>
<li>   feztive.SetAll(newColour);</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>Something is a bit strange here, the colour sensor returns a uint for each of RGB as well as an intensity. The FEZtive LED strip takes a value between 0-127 for each of the RGB. For now I halved the channels, but this is not correct and for extreme lighting conditions it will produce strange results. I plan to normalise these values, but so I can test a bit more this will do for now. I could not find a datasheet for the Colour sensor module and the source code does not give away much. humph! – GHI? The driver reads 2 bytes fore each channel 2^16 colour options per channel, but I noticed that with a bright light the values rarely top 10,000. (e.g RED: 9715, GREEN: 12368, BLUE: 11918) [<strong>UPDATE</strong> : <a title="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/" href="http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/">http://stevenjohnston.co.uk/2013/04/15/colour-sensor-on-net-gadgeteer-tcs3414/</a>]</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:304d55d7-eea5-47ce-acd7-24a14fd4b1cc" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">public</span> <span style="color:#2b91af;">ColorChannels</span> ReadColorChannels()</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#2b91af;">ColorChannels</span> returnData;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">byte</span>[] TransmitBuffer = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[1];</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    TransmitBuffer[0] = 0&#215;90; <span style="color:#008000;">// Send COMMAND to access Green Color Channel register for chip</span></li>
<li style="background:#f3f3f3;">    returnData.Green = readWord(0&#215;90, TransmitBuffer)[0];</li>
<li>    TransmitBuffer[0] = 0&#215;91;</li>
<li style="background:#f3f3f3;">    returnData.Green |= (<span style="color:#0000ff;">uint</span>)readWord(0&#215;91, TransmitBuffer)[0] &lt;&lt; 8;</li>
</ol></div>
</p></div>
</p></div>
<h3>The LDP8806</h3>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_0175.jpg"><img title="WP_20130411_017" style="display:inline;border-width:0;" border="0" alt="WP_20130411_017" src="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_017_thumb5.jpg?w=592&#038;h=438" width="592" height="438"></a> </p>
<p>Each multicolour led on the strip has a processor next to it, yes every one! This is the LDP8806 – anyone got a data sheet?</p>
<p>Something is interesting here, you just tell the Gadgeteer initialiser how many LEDs there are, and then ram data down the SPI bus. If you say there are only 10 LEDs then only the first 10 in your strip will work. This means that the data is fed to the first led in the strip, the colour is set and then all subsequent data is passed down the SPI bus, to the next LED and so on.&nbsp; </p>
<p>There is a series of zeros that are passed into SPI before and after each call, and this depends on the length of the strip. (presumably to set the LED into a state where it can obtain a new colour). I think this explains why each value is limited to a range of 0-127, the high bit is being used to command each LED. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b3d7a806-9a62-4265-a916-8ff1e323bf59" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>_zeros = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[3 * ((numLEDS + 63) / 64)];</li>
</ol></div>
</p></div>
</p></div>
<p>Ok, enough speculation, here is a much better explanation: <a title="https://github.com/adafruit/LPD8806/blob/master/LPD8806.cpp" href="https://github.com/adafruit/LPD8806/blob/master/LPD8806.cpp">https://github.com/adafruit/LPD8806/blob/master/LPD8806.cpp</a></p>
<h3>Can it go faster?</h3>
<p>For a bit of fun I decided to see what SPI clock rates would work with <strong>my</strong> 5m strip. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:08a18447-182b-401b-b79f-a0b067ef4294" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#2b91af;">FEZtive</span> feztive;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li>    feztive = <span style="color:#0000ff;">new</span> GTM.GHIElectronics.<span style="color:#2b91af;">FEZtive</span>(9);</li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 500);<span style="color:#008000;">//OK</span></li>
<li>    feztive.Initialize(160, 1000);<span style="color:#008000;">//OK</span></li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 2000);<span style="color:#008000;">//OK</span></li>
<li>    feztive.Initialize(160, 4000);<span style="color:#008000;">//OK</span></li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 8000);<span style="color:#008000;">//OK</span></li>
<li>    feztive.Initialize(160, 16000);<span style="color:#008000;">//Some LEDS are the wrong colour</span></li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 12000);<span style="color:#008000;">//OK</span></li>
<li>    feztive.Initialize(160, 14000);<span style="color:#008000;">//OK</span></li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 15000);<span style="color:#008000;">//Some LEDS are the wrong colour</span></li>
<li>    feztive.Initialize(160, 14500);<span style="color:#008000;">//Some LEDS are the wrong colour</span></li>
<li style="background:#f3f3f3;">    feztive.Initialize(160, 14000);<span style="color:#008000;">//OK</span></li>
</ol></div>
</p></div>
</p></div>
<p>So it would look like 14MHz is as fast as this will run without errors. It is not exactly scientific, so the plan is to run at at this speed for a bit and see how it copes. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/334/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/334/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=334&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/11/individually-addressable-multicolour-leds-on-net-gadgeteer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_005_thumb.jpg" medium="image">
			<media:title type="html">WP_20130411_005</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_006_thumb.jpg" medium="image">
			<media:title type="html">WP_20130411_006</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_007_thumb.jpg" medium="image">
			<media:title type="html">WP_20130411_007</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_012_thumb.jpg" medium="image">
			<media:title type="html">WP_20130411_012</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_017_thumb4.jpg" medium="image">
			<media:title type="html">WP_20130411_017</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/wp_20130411_017_thumb5.jpg" medium="image">
			<media:title type="html">WP_20130411_017</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Gadgeteer digital camera with a SerCam</title>
		<link>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-digital-camera-with-a-sercam/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-digital-camera-with-a-sercam/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 16:10:13 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=312</guid>
		<description><![CDATA[[UPDATE:&#160; C# and VB solutions are at the end, the first part is ONLY an explanation. You do not need &#8230;<p><a href="http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-digital-camera-with-a-sercam/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=312&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>[UPDATE:&nbsp; C# and VB solutions are at the end, the first part is ONLY an explanation. You do not need to follow the steps, just look at the code at the very end if you are after a fix. ]</em></p>
<p>I have had a few people mention that they hit issues when swapping from the GHI Camera module to a GHI SerCam module. So here is an explanation and example:</p>
<p>First remember that things are constantly being updated so make sure you have the updates! (<a href="http://stevenjohnston.co.uk/2012/11/20/help-visual-studio-wont-deploy-my-net-gadgeteer-project/">Updates/Debug help</a>)</p>
<p>I am using NetMF QFE2, version 4.2 with the GHI software last updated Feb 18, 2013, (I also have no idea if the source code on codeplex has been updated to reflect this release.)</p>
<p>(Expect this post to get out of date, it is here to help those who need a workaround NOW – solution at the bottom)</p>
<h3>Issue 1</h3>
<p>1) The <strong>NullReferenceException:</strong></p>
<p>Create a new project and call the take picture method on the camera. </p>
<p>&nbsp; </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b6eeb1ab-07fd-4a99-b715-0a200e3978a8" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> button_ButtonPressed(<span style="color:#2b91af;">Button</span> sender, <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonState</span> state)</li>
<li style="background:#f3f3f3;">{</li>
<li>    serCam.TakePicture();</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>When you run the code it throws an exception. The exception is in the SerCam.dll and you do not have the source code in your project so you cant see what is going on. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/image.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb.png?w=518&#038;h=235" width="518" height="235"></a> </p>
<p>I downloaded the latest GHI source from codeplex (<a title="http://gadgeteer.codeplex.com/" href="http://gadgeteer.codeplex.com/">http://gadgeteer.codeplex.com/</a>) </p>
<p>Link the downloaded code into your project:</p>
<p>1) Delete the reference to the GHI SerCam.dll</p>
<p>2) Add the 4.2 SerCam project to your solution (codeplex source folder : \Main\Modules\GHIElectronics\SerCam\Software\SerCam\SerCam_42).</p>
<p>3) Add a reference from your project to the codeplex SerCam. </p>
<p>Right now you are good to go, when it deploys it will now halt execution at the code that is causing the problem :</p>
<p>&nbsp;<a href="http://stevenjohnston.files.wordpress.com/2013/04/image1.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb1.png?w=545&#038;h=589" width="545" height="589"></a> </p>
<p>The problem here is that the code assumes that there is at least one delegate to call when the StartDataCaptured event is triggered. A simple work around (if you don&#8217;t want to change the source) is to ensure that all events have something to call, even if it is empty. Then when you run the code you should not get the NullReferenceException. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8bbf40a6-406a-4893-8991-3cb7ab518de4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li style="background:#f3f3f3;">{            </li>
<li>    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">    serCam.StartDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">StartDataCapturedEventHandler</span>(serCam_StartDataCaptured);</li>
<li>    serCam.FinishDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">FinishDataCapturedEventHandler</span>(serCam_FinishDataCaptured);</li>
<li style="background:#f3f3f3;">    serCam.OnDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">DataCapturedEventHandler</span>(serCam_OnDataCaptured);</li>
<li>    serCam.TakePicture();</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">void</span> serCam_OnDataCaptured(SerCam sender, <span style="color:#0000ff;">byte</span>[] data)</li>
<li>{            </li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">void</span> serCam_FinishDataCaptured(SerCam sender)</li>
<li>{</li>
<li style="background:#f3f3f3;">}</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">void</span> serCam_StartDataCaptured(SerCam sender, <span style="color:#0000ff;">int</span> sizeImage)</li>
<li>{</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<h3>Issue 2</h3>
<p>The methods are not the same as the camera I am used to, where is the Picture object, how do i get it and why is it not returned to me?</p>
<p>There are 3 events on the SerCam, one that is triggered at the start of a picture capture (StartDataCaptured), one that is triggered once the picture has been captured (FinishDataCaptured) and one that is triggered when <strong>some</strong> data is available (OnDataCaptured). <strong>The key here is that you only get the picture back in chunks, </strong>handy if memory is an issue. You have to take these chunks and piece them together. If we take a picture and set a breakpoint in the ReadFrameBuffer method (the one that triggers the OnDataCaptured event)&nbsp; we can see that the picture is split into blocks, each block is returned as it is read. There are 95 blocks in this particular picture, so we have to put them all back together to reconstruct the picture for the display. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/image2.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb2.png?w=534&#038;h=134" width="534" height="134"></a> </p>
<p>&lt;snip&gt;</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2013/04/image3.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb3.png?w=539&#038;h=164" width="539" height="164"></a> </p>
<p>&nbsp;</p>
<h3>Solution/workaround </h3>
<p>Make sure there is code to execute for every event and rebuild the picture. </p>
<p>1) First create a Byte array to store the reconstructed picture in your program, I will call it rawPicData:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b94cfc91-9efd-4178-b340-64de496df5fd" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">namespace</span> SerCamExample</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">byte</span>[] rawPicData; </li>
<li>        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li style="background:#f3f3f3;">        {            </li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
</ol></div>
</p></div>
</p></div>
<p>3) When we start to take a picture lets reset the rawPicData array. That way we know it is initialised and is reset for every picture. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ef9fe371-3425-4b5f-bb3b-f46614ae2e5a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> serCam_StartDataCaptured(<span style="color:#2b91af;">SerCam</span> sender, <span style="color:#0000ff;">int</span> sizeImage)</li>
<li style="background:#f3f3f3;">{</li>
<li>    rawPicData = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { };</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>4) Every time there is a block (chunk) of picture data ready, we read it and append it to the end of the picture array (Not the optimal solution, it will be a bit slow, but it is one line). I added a debug line so you can see that something is happening. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6edaec6c-1087-4f84-8d6b-46512c5e3e7f" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> serCam_OnDataCaptured(<span style="color:#2b91af;">SerCam</span> sender, <span style="color:#0000ff;">byte</span>[] data)</li>
<li style="background:#f3f3f3;">{</li>
<li>    rawPicData = Microsoft.SPOT.Hardware.<span style="color:#2b91af;">Utility</span>.CombineArrays(rawPicData, data);</li>
<li style="background:#f3f3f3;">    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Working&#8230;&quot;</span>);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
</p>
</p>
<p>5) Finally when the camera has finished taking a picture and you have read all the blocks, the FinishedDataCaptured event will be triggered. Now you can take the byte array and convert it into a picture object. You can only do this once you have ALL the blocks and you should check for an exception, in case of an error.&nbsp; In this case it is Jpeg</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c834e2d9-6c2f-4528-b003-7363eb01e0af" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">var</span> picB = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Bitmap</span>(rawPicData, <span style="color:#2b91af;">Bitmap</span>.<span style="color:#2b91af;">BitmapImageType</span>.Jpeg);</li>
</ol></div>
</p></div>
</p></div>
<p>You can then display the picture:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:20e30c89-8524-4de7-998d-39f55b4c3aae" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li> display_T35.SimpleGraphics.DisplayImage(picB, 0, 0);</li>
</ol></div>
</p></div>
</p></div>
<h3>Source Code</h3>
<p>If you read nothing above and just want a working example here it is:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4387d346-1aa9-4de8-84b9-6ce7425f1743" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections;</li>
<li><span style="color:#0000ff;">using</span> System.Threading;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Controls;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Media;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Touch;</li>
<li><span style="color:#0000ff;">using</span> Gadgeteer.Networking;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li><span style="color:#0000ff;">using</span> GTM = Gadgeteer.Modules;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Modules.GHIElectronics;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> SerCamExample</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">byte</span>[] rawPicData; </li>
<li>        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li style="background:#f3f3f3;">        {            </li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li style="background:#f3f3f3;">            serCam.StartDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">StartDataCapturedEventHandler</span>(serCam_StartDataCaptured);</li>
<li>            serCam.FinishDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">FinishDataCapturedEventHandler</span>(serCam_FinishDataCaptured);</li>
<li style="background:#f3f3f3;">            serCam.OnDataCaptured += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SerCam</span>.<span style="color:#2b91af;">DataCapturedEventHandler</span>(serCam_OnDataCaptured);           </li>
<li>            button.ButtonPressed += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonEventHandler</span>(button_ButtonPressed);        </li>
<li style="background:#f3f3f3;">        }</li>
<li>        </li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> serCam_OnDataCaptured(<span style="color:#2b91af;">SerCam</span> sender, <span style="color:#0000ff;">byte</span>[] data)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            rawPicData = Microsoft.SPOT.Hardware.<span style="color:#2b91af;">Utility</span>.CombineArrays(rawPicData, data);</li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Working&#8230;&quot;</span>);</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> serCam_FinishDataCaptured(<span style="color:#2b91af;">SerCam</span> sender)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">var</span> picB = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Bitmap</span>(rawPicData, <span style="color:#2b91af;">Bitmap</span>.<span style="color:#2b91af;">BitmapImageType</span>.Jpeg);</li>
<li>            display_T35.SimpleGraphics.DisplayImage(picB, 0, 0);</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> serCam_StartDataCaptured(<span style="color:#2b91af;">SerCam</span> sender, <span style="color:#0000ff;">int</span> sizeImage)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            rawPicData = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { };</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">void</span> button_ButtonPressed(<span style="color:#2b91af;">Button</span> sender, <span style="color:#2b91af;">Button</span>.<span style="color:#2b91af;">ButtonState</span> state)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            serCam.TakePicture();</li>
<li style="background:#f3f3f3;">        }        </li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<p>And in VB (Thanks to Sue)</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0a359f35-478a-419d-ad68-c49d8bd0d547" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">Imports</span> GT = Gadgeteer</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">Imports</span> GTM = Gadgeteer.Modules</li>
<li><span style="color:#0000ff;">Imports</span> Gadgeteer.Modules.GHIElectronics</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">Partial</span> <span style="color:#0000ff;">Public</span> <span style="color:#0000ff;">Class</span> <span style="color:#2b91af;">Program</span></li>
<li>    <span style="color:#008000;">&#039; Dim datajpg As Byte() = {8}</span></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">Dim</span> datajpg() <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">Byte</span> = <span style="color:#0000ff;">New</span> <span style="color:#0000ff;">Byte</span>() {}</li>
<li>    <span style="color:#0000ff;">Dim</span> index <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">Integer</span></li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">&#039; This is run when the mainboard is powered up or reset. </span></li>
<li>    <span style="color:#0000ff;">Public</span> <span style="color:#0000ff;">Sub</span> ProgramStarted()       </li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">&#039; Use Debug.Print to show messages in Visual Studio&#039;s &quot;Output&quot; window during debugging.</span></li>
<li>        <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>)</li>
<li style="background:#f3f3f3;">        serCam.SetImageSize(serCam.Camera_Resolution.SIZE_QVGA)</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">Private</span> <span style="color:#0000ff;">Sub</span> button_ButtonPressed(sender <span style="color:#0000ff;">As</span> Gadgeteer.Modules.GHIElectronics.Button, state <span style="color:#0000ff;">As</span> Gadgeteer.Modules.GHIElectronics.Button.ButtonState) <span style="color:#0000ff;">Handles</span> button.ButtonPressed</li>
<li>        serCam.TakePicture()</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Taken picture&quot;</span>)</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">Private</span> <span style="color:#0000ff;">Sub</span> serCam_FinishDataCaptured(sender <span style="color:#0000ff;">As</span> Gadgeteer.Modules.GHIElectronics.SerCam) <span style="color:#0000ff;">Handles</span> serCam.FinishDataCaptured</li>
<li>        <span style="color:#0000ff;">Dim</span> picture <span style="color:#0000ff;">As</span> <span style="color:#2b91af;">Bitmap</span> = <span style="color:#0000ff;">New</span> <span style="color:#2b91af;">Bitmap</span>(datajpg, <span style="color:#2b91af;">Bitmap</span>.<span style="color:#2b91af;">BitmapImageType</span>.Jpeg)</li>
<li style="background:#f3f3f3;">        display_T35.SimpleGraphics.DisplayImage(picture, 0, 0)</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">Private</span> <span style="color:#0000ff;">Sub</span> serCam_OnDataCaptured(sender <span style="color:#0000ff;">As</span> Gadgeteer.Modules.GHIElectronics.SerCam, data() <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">Byte</span>) <span style="color:#0000ff;">Handles</span> serCam.OnDataCaptured</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        datajpg = Microsoft.SPOT.Hardware.<span style="color:#2b91af;">Utility</span>.CombineArrays(datajpg, data)</li>
<li>    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>    <span style="color:#0000ff;">Private</span> <span style="color:#0000ff;">Sub</span> serCam_StartDataCaptured(sender <span style="color:#0000ff;">As</span> Gadgeteer.Modules.GHIElectronics.SerCam, sizeImage <span style="color:#0000ff;">As</span> <span style="color:#0000ff;">Integer</span>) <span style="color:#0000ff;">Handles</span> serCam.StartDataCaptured</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">&#039; At present need an empty sub here to stop a Null Reference error occurring.</span></li>
<li>        <span style="color:#008000;">&#039; Hopefully this will be fixed soon (11/4/2013)</span></li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Sub</span></li>
<li><span style="color:#0000ff;">End</span> <span style="color:#0000ff;">Class</span></li>
</ol></div>
</p></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/312/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=312&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-digital-camera-with-a-sercam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2013/04/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Gadgeteer Led Matrix Part 2 &#8211; the code</title>
		<link>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-led-matrix-part-2-the-code/</link>
		<comments>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-led-matrix-part-2-the-code/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 10:03:51 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LED Matrix]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=296</guid>
		<description><![CDATA[I was asked to show the source behind the example shown in the .NET Gadgeteer LED Matrix blog post. (as &#8230;<p><a href="http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-led-matrix-part-2-the-code/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=296&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was asked to show the source behind the example shown in the <a href="http://stevenjohnston.co.uk/2012/07/19/net-gadgeteer-led-matrix/">.NET Gadgeteer LED Matrix</a> blog post. </p>
<p> (as is, no warranty, and I am sure there are bugs etc blah blah)</p>
<p>The code is supplied below, it has a display buffer that you fill with text and it will scroll based on the timer ( turn off if annoying) . This is a class that you can just add to your project and call the methods to display text. </p>
<p>1) Add code to project (new class)</p>
<p>2) Create instance, supplying an array of your hardware modules to the constructor</p>
<p>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6a8dc1bd-f77c-4e8c-9e34-a8f2be6a1f13" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#2b91af;">LedDisplayArray</span> ledArray;</li>
<li style="background:#f3f3f3;">ledArray = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">LedDisplayArray</span>(<span style="color:#0000ff;">new</span> <span style="color:#2b91af;">LEDMatrix</span>[] { lEDMatrix, lEDMatrix1 ,lEDMatrix2,lEDMatrix3});</li>
</ol></div>
</p></div>
</p></div>
<p>3) Call DrawString with the text you need to display. </p>
<p>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8c344049-1a4c-4527-ab21-1471420e2915" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">string</span> displayText =   <span style="color:#a31515;">&quot; 1 2 3 4 5 6 7 8 9 0 :  !  o   &quot;</span>;<span style="color:#008000;">//Example text</span></li>
<li style="background:#f3f3f3;">ledArray.clear();<span style="color:#008000;">//Reset/clear any previous text</span></li>
<li>ledArray.DrawString(displayText);<span style="color:#008000;">//Display the text</span></li>
<li style="background:#f3f3f3;">ledArray.Scroll(); <span style="color:#008000;">//Start scrolling</span></li>
</ol></div>
</p></div>
</p></div>
<p>NOTE: You will have to create a lookup table for each character/digit you want to display. I have only added a few. </p>
<p>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9cde6f32-954b-479e-87be-7bae4970cea0" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> System.Collections;</li>
<li><span style="color:#0000ff;">using</span> Gadgeteer;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Gadgeteer.Modules.GHIElectronics;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Hardware;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> LedArray</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">LedDisplayArray</span></li>
<li>    {</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//The array with the chars that we will display</span></li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">byte</span>[] displayArray = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { };</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//An array of the physical matrix modules, so we can add more</span></li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#2b91af;">LEDMatrix</span>[] displays;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">int</span> destinationIndex = 0;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//A list of all possible characters (you have to make your own, examples below)</span></li>
<li>        <span style="color:#2b91af;">Hashtable</span> AsciiLookup = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Hashtable</span>();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#2b91af;">Timer</span> timer = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Timer</span>(400);</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> arrayOffset = 1;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">//Constructor, give it all your physical displays</span></li>
<li>        <span style="color:#0000ff;">public</span> LedDisplayArray(<span style="color:#2b91af;">LEDMatrix</span>[] displays)</li>
<li style="background:#f3f3f3;">        {</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Setup characters/digits &#8211; add more</span></li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;1&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0&#215;02, 0xFE, 0&#215;42 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;2&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xF2, 0&#215;92, 0x9E });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;3&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;92, 0&#215;92 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;4&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;10, 0xF0 });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;5&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0x9E, 0&#215;92, 0xF2 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;6&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0x9E, 0&#215;92, 0xFE });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;7&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;80, 0&#215;80 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;8&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;92, 0xFE });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;9&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;90, 0xF0 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;0&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0xFE, 0&#215;82, 0xFE });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039; &#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0&#215;00 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;:&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0&#215;28 });</li>
<li>            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;!&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0&#215;20, 0&#215;10, 0xD7, 0xF8, 0xD7, 0&#215;10, 0&#215;20 });</li>
<li style="background:#f3f3f3;">            AsciiLookup.Add((<span style="color:#0000ff;">int</span>)<span style="color:#a31515;">&#039;o&#039;</span>, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { 0&#215;06, 0&#215;63, 0&#215;63, 0x0B, 0&#215;63, 0&#215;63, 0&#215;06 });</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//All hardware</span></li>
<li>            <span style="color:#0000ff;">this</span>.displays = displays;</li>
<li style="background:#f3f3f3;">            displayArray = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[displays.Length * 8];</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//scroller</span></li>
<li>            timer.Tick += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Timer</span>.<span style="color:#2b91af;">TickEventHandler</span>(timer_Tick);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">void</span> timer_Tick(<span style="color:#2b91af;">Timer</span> timer)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#008000;">//move array</span></li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            displayArray = <span style="color:#2b91af;">Utility</span>.CombineArrays(displayArray, displayArray.Length &#8211; arrayOffset, arrayOffset, displayArray, 0, displayArray.Length &#8211; arrayOffset);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            Draw();<span style="color:#008000;">//redraw</span></li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Scroll()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//start timer</span></li>
<li>            timer.Start();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> Draw()</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#008000;">//pad array to display size &#8211; minimum</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">if</span> (displayArray.Length &lt; displays.Length * 8)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                displayArray = <span style="color:#2b91af;">Utility</span>.CombineArrays(displayArray, <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[(displays.Length * 8) - displayArray.Length]);</li>
<li>            }</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//For each matrix.</span></li>
<li>            <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> ctr = 0; ctr &lt; displays.Length; ctr++)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#008000;">//The text can be bigger than we can display, get the bit we want to display</span></li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">byte</span>[] currentDisplayData = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { };</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                <span style="color:#008000;">//if the text is small, just display it. </span></li>
<li>                <span style="color:#0000ff;">if</span> ((displayArray.Length &gt;= (ctr + 1) * 8))</li>
<li style="background:#f3f3f3;">                {</li>
<li>                    <span style="color:#008000;">//display all text</span></li>
<li style="background:#f3f3f3;">                    currentDisplayData = <span style="color:#2b91af;">Utility</span>.ExtractRangeFromArray(displayArray, ctr * 8, 8);</li>
<li>                }</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">else</span></li>
<li>                {</li>
<li style="background:#f3f3f3;">                    <span style="color:#008000;">//Start of text</span></li>
<li>                    currentDisplayData = <span style="color:#2b91af;">Utility</span>.ExtractRangeFromArray(displayArray, ctr * 8,</li>
<li style="background:#f3f3f3;">                                                                       displayArray.Length &#8211; (ctr * 8));</li>
<li>                    <span style="color:#008000;">//wrap round and get beginning text if at end. </span></li>
<li style="background:#f3f3f3;">                    currentDisplayData = <span style="color:#2b91af;">Utility</span>.CombineArrays(currentDisplayData,</li>
<li>                                                               <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[8 - currentDisplayData.Length]);</li>
<li style="background:#f3f3f3;">                }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                <span style="color:#008000;">//Display the text that we have decided should be shown </span></li>
<li>                displays[ctr].DrawBitmap(currentDisplayData);</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> DrawString(<span style="color:#0000ff;">string</span> str)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//Given a string , put it in the draw buffer</span></li>
<li>            <span style="color:#0000ff;">char</span>[] characterArray = str.ToCharArray();</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//get each char and look it up &#8211; be sure to add all the chars you will need</span></li>
<li>            <span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">char</span> c <span style="color:#0000ff;">in</span> characterArray)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#0000ff;">this</span>.AddCharacter((<span style="color:#0000ff;">byte</span>[])<span style="color:#0000ff;">this</span>.AsciiLookup[(<span style="color:#0000ff;">int</span>)c]);</li>
<li style="background:#f3f3f3;">            }</li>
<li>            <span style="color:#008000;">//Draw it</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.Draw();</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> AddCharacter(<span style="color:#0000ff;">byte</span>[] characterBytes)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//add characters to the diaplay array</span></li>
<li>            displayArray = <span style="color:#2b91af;">Utility</span>.CombineArrays(characterBytes, displayArray);</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">internal</span> <span style="color:#0000ff;">void</span> clear()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">//remove all text</span></li>
<li>            displayArray = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[] { };</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/296/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=296&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2013/04/10/net-gadgeteer-led-matrix-part-2-the-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Gadgeteer temperature logger example</title>
		<link>http://stevenjohnston.co.uk/2012/12/12/net-gadgeteer-temperature-logger-example/</link>
		<comments>http://stevenjohnston.co.uk/2012/12/12/net-gadgeteer-temperature-logger-example/#comments</comments>
		<pubDate>Wed, 12 Dec 2012 15:40:29 +0000</pubDate>
		<dc:creator>sjj698</dc:creator>
				<category><![CDATA[.NET Gadgeteer]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[data logger]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[humidity]]></category>
		<category><![CDATA[temperature]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">https://stevenjohnston.wordpress.com/?p=235</guid>
		<description><![CDATA[An example can go a long way, so rather than a 2 hour presentation with nothing but slides, I decided &#8230;<p><a href="http://stevenjohnston.co.uk/2012/12/12/net-gadgeteer-temperature-logger-example/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=235&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>An example can go a long way, so rather than a 2 hour presentation with nothing but slides, I decided that the NxtGenUG Christmas special should include an end-to-end .NET Gadgeteer example. The example below includes the steps to build your own data logger to log both temperature and humidity. The coding examples were purposely kept short and do not include all the error checking that should be included, the aim was to show a wide variety of .NET Gadgeteer capabilities resulting in a final working prototype – within the 2 hour limit (including all coding and hardware building)! </p>
<p> [Note: I assume you have the latest SDK’s installed and that the firmware has been updated. I am using Visual Studio Ultimate, but the following will also work in the free version of VS.]</p>
<p>1) Lets start with a new project..</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image13.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb13.png?w=587&#038;h=381" width="587" height="381"></a> </p>
<p>I have updated to the latest SDK/Drivers and firmware so we are currently targeting v4.2</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image91.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image9_thumb.png?w=590&#038;h=409" width="590" height="409"></a> </p>
</p>
<p>2) There are currently a wide variety of sensors, actuators and displays available (over 130 different modules)- Lets start with a simple temperature sensor, this module uses the SHT10 temp and humidity sensor. To create a basic prototype using this we will need a mainboard ( GHI Spider) and a power module. This is achieved by dragging the modules from the toolbox on the right into the designer (main panel). By right clicking and selecting connect all modules you should end up with the following in the designer.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image191.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image19_thumb1.png?w=416&#038;h=395" width="416" height="395"></a> </p>
<p>3) To begin with we will code the bare minimum so we can see the development cycle and get a feel for the environment. Notice that dragging modules on to the designer instantiates them, so they are ready to be used in your code. First we add some code to the MeasurementCompleted event and then put the sensor into continuous measurement mode. Each time the sensor acquires a new measurement our code will just print the readings out to the debug console. (Remember to turn this on if you are using VS Express – Debug-&gt; Windows-&gt; Output, See <a title="http://wp.me/p2pktY-3q" href="http://wp.me/p2pktY-3q">http://wp.me/p2pktY-3q</a>). As you type VS will prompt you with intellisense and auto complete assistance, be sure to use these as it saves time. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:85c85e8d-6aa9-4796-bcde-528ac72a2f53" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>{        </li>
<li style="background:#f3f3f3;">      <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li>      {                       </li>
<li style="background:#f3f3f3;">          <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li>          temperatureHumidity.MeasurementComplete += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">TemperatureHumidity</span>.<span style="color:#2b91af;">MeasurementCompleteEventHandler</span>(temperatureHumidity_MeasurementComplete);</li>
<li style="background:#f3f3f3;">          temperatureHumidity.StartContinuousMeasurements();</li>
<li>      }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>      <span style="color:#0000ff;">void</span> temperatureHumidity_MeasurementComplete(<span style="color:#2b91af;">TemperatureHumidity</span> sender, <span style="color:#0000ff;">double</span> temperature, <span style="color:#0000ff;">double</span> relativeHumidity)</li>
<li style="background:#f3f3f3;">      {</li>
<li>          <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature + <span style="color:#a31515;">&quot; RH: &quot;</span> + relativeHumidity);</li>
<li style="background:#f3f3f3;">      }</li>
</ol></div>
</p></div>
</p></div>
<p>4) Next we will deploy the solution (hit F5/ Green arrow) this will compile the code and flash your mainboard with the solution. Note we are using the USB cable to power hardware as well as debug inside VS. Once the program has been deployed it will start and you can see the debug output in VS. </p>
</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image22.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image22_thumb.png?w=473&#038;h=158" width="473" height="158"></a></p>
<p>5) This is all very well but the Debug output is not a great place to view data, so let us add a display. There are a few to choose from but lets start with the T35 from GHI. Dragging and dropping the display in the designer and selecting connect all modules will result in the layout below. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image5.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb5.png?w=480&#038;h=343" width="480" height="343"></a> </p>
<p>6)I prefer to have a separate method to configure the display but feel free to have the code inside the program started method. We will need a font, one of the two supplied with the template. There are 2 modes of operation for the display, WPF and simple graphics. Many examples include the simple graphics option so we will use the WPF as a demo, suffice to say that this is a very basic interface and WPF is very powerful, so worth looking at further. The hardware display module has a WPFWindow property, this is the root of all the graphics that we will add. First we will add some text to the main canvas.</p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bf04aae2-fdc8-4594-bca1-3bba6ef6366d" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#2b91af;">Font</span> baseFont;</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">Window</span> window;</li>
<li><span style="color:#2b91af;">Canvas</span> canvas = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Canvas</span>();</li>
<li style="background:#f3f3f3;"><span style="color:#2b91af;">Text</span> txtMsg;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> setupWindow()</li>
<li>{</li>
<li style="background:#f3f3f3;">    baseFont = <span style="color:#2b91af;">Resources</span>.GetFont(<span style="color:#2b91af;">Resources</span>.<span style="color:#2b91af;">FontResources</span>.NinaB);</li>
<li>    window = display_TE35.WPFWindow;</li>
<li style="background:#f3f3f3;">    window.Child = canvas;</li>
<li>    txtMsg = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Text</span>(baseFont, <span style="color:#a31515;">&quot;Starting&#8230;&quot;</span>);</li>
<li style="background:#f3f3f3;">    canvas.Children.Add(txtMsg);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
</p>
<p>7) Each time the sensor returns a new reading we can use it to change the text content on the canvas. Note that we do not need to refresh the text, we are using the same text object. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:96f47a9d-d18e-45aa-ac7d-eca4207676d9" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> temperatureHumidity_MeasurementComplete(<span style="color:#2b91af;">TemperatureHumidity</span> sender, <span style="color:#0000ff;">double</span> temperature, <span style="color:#0000ff;">double</span> relativeHumidity)</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature + <span style="color:#a31515;">&quot; RH: &quot;</span> + relativeHumidity);</li>
<li style="background:#f3f3f3;">    txtMsg.TextContent =  <span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature + <span style="color:#a31515;">&quot; RH: &quot;</span> + relativeHumidity;                        </li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<p>8) This text is a bit long, and has a few too many decimal places, so lets shorten the text. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8643ab8b-ed0c-49a5-906b-f4446106e720" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> temperatureHumidity_MeasurementComplete(<span style="color:#2b91af;">TemperatureHumidity</span> sender, <span style="color:#0000ff;">double</span> temperature, <span style="color:#0000ff;">double</span> relativeHumidity)</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature + <span style="color:#a31515;">&quot; RH: &quot;</span> + relativeHumidity);</li>
<li style="background:#f3f3f3;">    txtMsg.TextContent = <span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature.ToString(<span style="color:#a31515;">&quot;F&quot;</span>) + <span style="color:#a31515;">&quot;           RH: &quot;</span> + relativeHumidity.ToString(<span style="color:#a31515;">&quot;F&quot;</span>);                        </li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<p>9) The text is also rather close to the top of the screen, so we can nudge the canvas down a bit by adding a margin. </p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:28cf4bde-4fbd-42d4-88cf-f67953166dd1" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>canvas.SetMargin(5);</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>10) If all went well we are left with a user interface that updates, but is perhaps not beautiful. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image6.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb6.png?w=442&#038;h=332" width="442" height="332"></a> </p>
<p>&nbsp;</p>
<p>11) Imagine that you are happy with this prototype and show it to friends/colleagues, who like the prototype but think the display is not very good and suggest adding a larger display. (Substituting hardware is something that is often required but can be very difficult to achieve, particularly when working with embedded systems). Lets go to the designer and deleted the T35 display and drag and drop the CP7 display into the solution. Connecting up the new display is the same as the old one and hitting build will result in an error. The error occurs because the display variable names are different (I just used the default names) – correcting this will let the solution compile and deploy. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image7.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb7.png?w=385&#038;h=309" width="385" height="309"></a> </p>
<p>12) With minimal effort the new display should look like the picture below. (Still not a fantastic interface) Remember that you need to build different prototype versions, experiment with hardware and test your ideas. The quicker you can develop new versions the quicker you can test you ideas and bring them into reality.&nbsp; </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/picture000.jpg"><img title="picture000" style="display:inline;border-width:0;" border="0" alt="picture000" src="http://stevenjohnston.files.wordpress.com/2012/12/picture000_thumb.jpg?w=411&#038;h=309" width="411" height="309"></a></p>
<p>&nbsp;</p>
<p>13) The next step is to do something about this interface, it is lacking a bit of readability. The first step is to make the font bigger. This is not as simple as you imagine as there are just 2 default fonts in the template and they do not come in different sizes. Basically you would not want to waste all your precious embedded device memory on fonts! So you can add just the fonts (and character sets) that you need! There is a tool to import fonts called TFConvert which is command line driven (TFConvert more info: <a title="http://msdn.microsoft.com/en-us/library/cc533019.aspx" href="http://msdn.microsoft.com/en-us/library/cc533019.aspx">http://msdn.microsoft.com/en-us/library/cc533019.aspx</a>). Like most things someone has written a tool to improve it :- <a title="http://informatix.miloush.net/microframework/Utilities/TinyFontTool.aspx" href="http://informatix.miloush.net/microframework/Utilities/TinyFontTool.aspx">http://informatix.miloush.net/microframework/Utilities/TinyFontTool.aspx</a></p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image8.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb8.png?w=449&#038;h=487" width="449" height="487"></a> </p>
<p>Once you have a new .tinyfnt file simply import is as a resource in VS (Add resource –&gt; File-&gt; *.tinyfnt) and it will become available in your code. </p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image9.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb9.png?w=418&#038;h=195" width="418" height="195"></a> </p>
<p>&nbsp;</p>
<p>14) Lets just swap the font for the new font, intellisense will auto complete the FontResources, to show all the fonts you now have (should be 3).</p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:51a40b0b-9c72-428c-bbab-f5c181d8d095" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> setupWindow()</li>
<li style="background:#f3f3f3;">{</li>
<li>    baseFont = <span style="color:#2b91af;">Resources</span>.GetFont(<span style="color:#2b91af;">Resources</span>.<span style="color:#2b91af;">FontResources</span>.SegoeUILarge);</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>15) Simply writing text to the display is a bit lazy, remember this is a WPFWindow, we have a canvas and a whole collection of WPF components that we could add. Most things in WPF end up in a stack panel of some sort, think of it as a bag that lets you throw things into and can display them either as a row or a column. I will add a stack panel to the canvas and then add the text for humidity and temperature as two separate text objects. Feel free to explore some of the methods at this point. For example you can catch touch events on WPF components such as a text object. I have added the complete code below as a reference. </p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5f0997a2-e896-4e23-b433-507220ffa122" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">namespace</span> TemperatureLogger</li>
<li style="background:#f3f3f3;">{</li>
<li>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> <span style="color:#2b91af;">Program</span></li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#2b91af;">Font</span> baseFont;</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Window</span> window;</li>
<li>        <span style="color:#2b91af;">Canvas</span> canvas = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Canvas</span>();</li>
<li style="background:#f3f3f3;">        <span style="color:#2b91af;">Text</span> txtMsgTemp;</li>
<li>        <span style="color:#2b91af;">Text</span> txtMsgRH;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> setupWindow()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            baseFont = <span style="color:#2b91af;">Resources</span>.GetFont(<span style="color:#2b91af;">Resources</span>.<span style="color:#2b91af;">FontResources</span>.SegoeUILarge);</li>
<li>            window = display_CP7.WPFWindow;</li>
<li style="background:#f3f3f3;">            window.Child = canvas;</li>
<li>            txtMsgTemp = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Text</span>(baseFont, <span style="color:#a31515;">&quot;Starting&#8230;&quot;</span>);</li>
<li style="background:#f3f3f3;">            txtMsgRH = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">Text</span>(baseFont, <span style="color:#a31515;">&quot;Starting&#8230;&quot;</span>);</li>
<li>            canvas.SetMargin(5);</li>
<li style="background:#f3f3f3;">            txtMsgTemp.TextWrap = <span style="color:#0000ff;">true</span>;</li>
<li>            <span style="color:#2b91af;">StackPanel</span> stack = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">StackPanel</span>();</li>
<li style="background:#f3f3f3;">            stack.Children.Add(txtMsgTemp);</li>
<li>            stack.Children.Add(txtMsgRH);</li>
<li style="background:#f3f3f3;">            canvas.Children.Add(stack);</li>
<li>             </li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">void</span> ProgramStarted()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Program Started&quot;</span>);</li>
<li>            setupWindow();</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            temperatureHumidity.MeasurementComplete += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">TemperatureHumidity</span>.<span style="color:#2b91af;">MeasurementCompleteEventHandler</span>(temperatureHumidity_MeasurementComplete);</li>
<li style="background:#f3f3f3;">            temperatureHumidity.StartContinuousMeasurements();</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">void</span> temperatureHumidity_MeasurementComplete(<span style="color:#2b91af;">TemperatureHumidity</span> sender, <span style="color:#0000ff;">double</span> temperature, <span style="color:#0000ff;">double</span> relativeHumidity)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#2b91af;">Debug</span>.Print(<span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature + <span style="color:#a31515;">&quot; RH: &quot;</span> + relativeHumidity);</li>
<li style="background:#f3f3f3;">            txtMsgTemp.TextContent = <span style="color:#a31515;">&quot;Temp : &quot;</span> + temperature.ToString(<span style="color:#a31515;">&quot;F&quot;</span>);</li>
<li>            txtMsgRH.TextContent = <span style="color:#a31515;">&quot;RH: &quot;</span> + relativeHumidity.ToString(<span style="color:#a31515;">&quot;F&quot;</span>);</li>
<li style="background:#f3f3f3;">        }</li>
<li>    }</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>16) Text that updates is lovely but sometimes you just need to draw something! This is the only code that I will not manually type but it is short and simple enough to read through and understand. There is a basic graphing class that will draw grid lines and plot data points. Source code copied from from <a title="http://gadgeteering.net/sites/default/files/users/nvillar/files/SimpleGraph.cs" href="http://gadgeteering.net/sites/default/files/users/nvillar/files/SimpleGraph.cs">http://gadgeteering.net/sites/default/files/users/nvillar/files/SimpleGraph.cs</a> . Code listed below for completeness. </p>
<p>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7d7e9821-76b0-46ff-ad59-e31ee0349327" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">using</span> System;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Media;</li>
<li><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Shapes;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> Microsoft.SPOT.Presentation.Controls;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> GT = Gadgeteer;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">namespace</span> TemperatureLogger</li>
<li>{</li>
<li style="background:#f3f3f3;">    <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>    <span style="color:#808080;">///</span><span style="color:#008000;"> This is a SimpleLine graph implemented as a WPF control.</span></li>
<li style="background:#f3f3f3;">    <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>    <span style="color:#0000ff;">class</span> SimpleGraph : Canvas</li>
<li style="background:#f3f3f3;">    {</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span>[] PointsX;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span>[] PointsY;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        Pen GraphPen;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> PointIndex;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">double</span> MinYValue;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">double</span> MaxYValue;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> DataPoints;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> Pen GridPen;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> VerticalGridSpacing = 0;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> HorizontalGridSpacing = 0;</li>
<li>        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">bool</span> ShowGrid = <span style="color:#0000ff;">false</span>;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;height&quot;&gt;</span><span style="color:#008000;">Height of the graph, in pixels.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;width&quot;&gt;</span><span style="color:#008000;">Width of the graph, in pixels.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;minYValue&quot;&gt;</span><span style="color:#008000;">The smallest expected data value.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;maxYValue&quot;&gt;</span><span style="color:#008000;">The largest expected data value.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;dataPoints&quot;&gt;</span><span style="color:#008000;">The number of data points.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;param name=&quot;lineThickness&quot;&gt;</span><span style="color:#008000;">The thickness of the graph line.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;param name=&quot;lineColor&quot;&gt;</span><span style="color:#008000;">The color of the graph line.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> SimpleGraph(<span style="color:#0000ff;">uint</span> height, <span style="color:#0000ff;">uint</span> width, <span style="color:#0000ff;">double</span> minYValue, <span style="color:#0000ff;">double</span> maxYValue, <span style="color:#0000ff;">int</span> dataPoints, <span style="color:#0000ff;">ushort</span> lineThickness, Color lineColor)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">this</span>.Width = (<span style="color:#0000ff;">int</span>)width + 1; <span style="color:#008000;">// Extra pixels to accomodate a border (only visible when grid lines are on)</span></li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.Height = (<span style="color:#0000ff;">int</span>)height + 1;</li>
<li>            <span style="color:#0000ff;">this</span>.MinYValue = minYValue;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.MaxYValue = maxYValue;</li>
<li>            <span style="color:#0000ff;">this</span>.DataPoints = dataPoints;</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            PointsX = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">int</span>[dataPoints];</li>
<li style="background:#f3f3f3;">            PointsY = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">int</span>[dataPoints];</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            GraphPen = <span style="color:#0000ff;">new</span> Pen(lineColor, lineThickness);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            Clear();</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// Plot a new value onto the graph. When there is no more room for new values, the graph will scroll.</span></li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Plot(<span style="color:#0000ff;">double</span> value)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            AddValue(value);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#0000ff;">this</span>.Invalidate();</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li>        <span style="color:#008000;">/// Clear the graph.</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Clear()</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i &lt; DataPoints; i++)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                PointsX[i] = i * (<span style="color:#0000ff;">int</span>)((<span style="color:#0000ff;">double</span>)<span style="color:#0000ff;">this</span>.Width / DataPoints);</li>
<li style="background:#f3f3f3;">                PointsY[i] = 0;</li>
<li>            }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            PointIndex = 0;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">this</span>.Invalidate();</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// Display grid lines in the background of the graph.</span></li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;/summary&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;param name=&quot;verticalLineSpacing&quot;&gt;</span><span style="color:#008000;">Spacing of vertical grid lines, in pixels.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;param name=&quot;horizontalLineSpacing&quot;&gt;</span><span style="color:#008000;">Spacing of horizontal grid lines, in pixels.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li style="background:#f3f3f3;">        <span style="color:#008000;">/// </span><span style="color:#808080;">&lt;param name=&quot;lineColor&quot;&gt;</span><span style="color:#008000;">Grid line color.</span><span style="color:#808080;">&lt;/param&gt;</span></li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> DisplayGridLines(<span style="color:#0000ff;">int</span> verticalLineSpacing, <span style="color:#0000ff;">int</span> horizontalLineSpacing, GT.Color lineColor)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            VerticalGridSpacing = verticalLineSpacing;</li>
<li style="background:#f3f3f3;">            HorizontalGridSpacing = horizontalLineSpacing;</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            GridPen = <span style="color:#0000ff;">new</span> Pen(lineColor, 1);</li>
<li>            ShowGrid = <span style="color:#0000ff;">true</span>;</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> HideGrid()</li>
<li>        {</li>
<li style="background:#f3f3f3;">            ShowGrid = <span style="color:#0000ff;">false</span>;</li>
<li>        }</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnRender(DrawingContext dc)</li>
<li style="background:#f3f3f3;">        {</li>
<li>            <span style="color:#0000ff;">base</span>.OnRender(dc);</li>
<li style="background:#f3f3f3;">&nbsp;</li>
<li>            <span style="color:#0000ff;">if</span> (ShowGrid)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                <span style="color:#0000ff;">if</span> (VerticalGridSpacing &gt; 0)</li>
<li style="background:#f3f3f3;">                {</li>
<li>                    <span style="color:#008000;">// Draw vertical grid lines</span></li>
<li style="background:#f3f3f3;">                    <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> x = 0; x &lt; <span style="color:#0000ff;">this</span>.Width; x += VerticalGridSpacing)</li>
<li>                    {</li>
<li style="background:#f3f3f3;">                        dc.DrawLine(GridPen, x, 0, x, <span style="color:#0000ff;">this</span>.Height);</li>
<li>                    }</li>
<li style="background:#f3f3f3;">                }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">                <span style="color:#0000ff;">if</span> (HorizontalGridSpacing &gt; 0)</li>
<li>                {</li>
<li style="background:#f3f3f3;">                    <span style="color:#008000;">// Draw horizontal grid lines</span></li>
<li>                    <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> y = 0; y &lt; <span style="color:#0000ff;">this</span>.Height; y += HorizontalGridSpacing)</li>
<li style="background:#f3f3f3;">                    {</li>
<li>                        dc.DrawLine(GridPen, 0, y, <span style="color:#0000ff;">this</span>.Width, y);</li>
<li style="background:#f3f3f3;">                    }</li>
<li>                }</li>
<li style="background:#f3f3f3;">            }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#008000;">// Draw the graph lines</span></li>
<li>            <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 1; i &lt; PointIndex; i++)</li>
<li style="background:#f3f3f3;">            {</li>
<li>                dc.DrawLine(GraphPen, PointsX[i-1], PointsY[i-1], PointsX[i], PointsY[i]);</li>
<li style="background:#f3f3f3;">                dc.DrawLine(GraphPen, PointsX[i - 1], PointsY[i - 1] &#8211; 1, PointsX[i], PointsY[i] &#8211; 1);</li>
<li>            }</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> AddValue(<span style="color:#0000ff;">double</span> value)</li>
<li>        {</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">int</span> yPos = (<span style="color:#0000ff;">int</span>)(<span style="color:#0000ff;">this</span>.Height &#8211; (((<span style="color:#0000ff;">double</span>)(value &#8211; MinYValue) / (MaxYValue &#8211; MinYValue)) * <span style="color:#0000ff;">this</span>.Height));</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">            <span style="color:#0000ff;">if</span> (PointIndex &lt; PointsY.Length)</li>
<li>            {</li>
<li style="background:#f3f3f3;">                PointsY[PointIndex] = yPos;</li>
<li>                PointIndex++;  </li>
<li style="background:#f3f3f3;">            }</li>
<li>            <span style="color:#0000ff;">else</span></li>
<li style="background:#f3f3f3;">            {</li>
<li>                Array.Copy(PointsY, 1, PointsY, 0, DataPoints &#8211; 1);</li>
<li style="background:#f3f3f3;">                PointsY[DataPoints - 1] = yPos;</li>
<li>            }</li>
<li style="background:#f3f3f3;">        }</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">    }</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<p>17) Import the simple graphing class and create a graph object. </p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c5981860-4f2d-4daf-a712-24adab5776f6" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li> <span style="color:#2b91af;">SimpleGraph</span> tempGraph;</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>Select the colours, size and grid lines. Then add the graph object to the stack panel that we created earlier. </p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:83bb260b-74cb-4954-a0e8-4d4a94338015" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>tempGraph = <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">SimpleGraph</span>(200, 300, 0,50, 300, 1, <span style="color:#2b91af;">Colors</span>.Blue);                    </li>
<li style="background:#f3f3f3;">tempGraph.DisplayGridLines(60, 20, GT.<span style="color:#2b91af;">Color</span>.LightGray);</li>
<li>&nbsp;</li>
<li style="background:#f3f3f3;">stack.Children.Add(tempGraph);</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>Then every time the sensor event triggers add the data to the graph. </p>
<p>&nbsp;</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9b6c6605-fb91-4926-ad24-d55e684c37ba" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>tempGraph.Plot(temperature);</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<p>The result should be a nice stack of text , text, graph! (Axis, units, colour, background, buttons are still required!) Hopefully that demonstrates how quite sophisticated GUIs can be generated using .NET Gadgeteer and very few lines of code.</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/wp_000674.jpg"><img title="WP_000674" style="display:inline;border-width:0;" border="0" alt="WP_000674" src="http://stevenjohnston.files.wordpress.com/2012/12/wp_000674_thumb.jpg?w=522&#038;h=392" width="522" height="392"></a> </p>
<p>18) Clearly we are making a sensor logger here, assuming the display now meets all you requirements, lets log the data to an SD card. First step is to add an SD module to the designer. (Drag, drop)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image10.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb10.png?w=525&#038;h=327" width="525" height="327"></a>&nbsp;</p>
<p>&nbsp;</p>
<p>19) First be sure to check you have an SD card, that it is formatted etc! We will just mount the storage that is in the SD module. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:883c7a66-c590-4ed7-a2fc-4d1c5e5c8292" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#008000;">//SDCard</span></li>
<li style="background:#f3f3f3;">sdCard.MountSDCard();</li>
<li>storage =  sdCard.GetStorageDevice(); <span style="color:#008000;">//Do checks here</span></li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<p>20) Every time the sensor gets some data, append it to the end of a file. Be sure to consider if this is a wise move, especially if you are polling the sensor frequently.&nbsp; ( I will add a fs.flush() for the demo to show that data is written). There is also a storage FileWrite method that is worth considering, I did not use it here as I plan to flush the FileStream, to show that data ends up on the card without having to wait for a flush. </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7914fe2a-8efe-48d7-97f3-8059a6ef6d84" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">byte</span>[] data = <span style="color:#2b91af;">Encoding</span>.UTF8.GetBytes(msg + <span style="color:#a31515;">&quot;&#092;n&quot;</span>);</li>
<li style="background:#f3f3f3;"><span style="color:#0000ff;">using</span> (<span style="color:#2b91af;">FileStream</span> fs = storage.Open(<span style="color:#a31515;">&quot;data.log&quot;</span>, <span style="color:#2b91af;">FileMode</span>.Append, <span style="color:#2b91af;">FileAccess</span>.Write))</li>
<li>{</li>
<li style="background:#f3f3f3;">    fs.Write(data, 0, data.Length);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>21) We have a data display, and logger, but nothing is complete without some sort of internet capability. Lets make our sensor available across the internet! .NET Gadgeteer easily lets you set up a web server. Lets add an Ethernet connection in the designer:</p>
<p>&nbsp;<a href="http://stevenjohnston.files.wordpress.com/2012/12/image14.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb14.png?w=697&#038;h=469" width="697" height="469"></a> </p>
<p>You could just as easily add a WiFi module:</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image15.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb15.png?w=704&#038;h=447" width="704" height="447"></a></p>
<p>Note: If you use a WiFi module here is a handy way to setup an adhoc network on your Win8 laptop/PC </p>
<p>Setup wireless network (from <a title="http://www.addictivetips.com/windows-tips/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8/" href="http://www.addictivetips.com/windows-tips/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8/">http://www.addictivetips.com/windows-tips/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8/</a>)</p>
<p>&gt; netsh wlan set hostednetwork mode=allow ssid=MyNetwork key=MyNetwork</p>
<p>&gt; netsh wlan start hostednetwork</p>
<p>(Be sure to run as admin)</p>
<p><a href="http://stevenjohnston.files.wordpress.com/2012/12/image12.png"><img title="image" style="display:inline;border-width:0;" border="0" alt="image" src="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb12.png?w=542&#038;h=121" width="542" height="121"></a> </p>
<p>Note: Here is a good webserver example <a title="http://mikedodaro.net/2011/10/15/gadgeteer-web-service-surveillance-camera/" href="http://mikedodaro.net/2011/10/15/gadgeteer-web-service-surveillance-camera/">http://mikedodaro.net/2011/10/15/gadgeteer-web-service-surveillance-camera/</a></p>
<p>&nbsp;</p>
<p>22) Be sure to add more error checking than is shown here,&nbsp; to your networking capabilities. </p>
<p>&nbsp;</p>
<p>23) Lets start a webserver when we get an IP address. First register to get notifications when the IP changes, enable DHCP and then open the network.</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f039c597-52af-468b-93e5-e1f8f81230ff" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li>ethernet_J11D.Interface.NetworkAddressChanged += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">NetworkInterfaceExtension</span>.<span style="color:#2b91af;">NetworkAddressChangedEventHandler</span>(Interface_NetworkAddressChanged);</li>
<li style="background:#f3f3f3;">ethernet_J11D.Interface.NetworkInterface.EnableDhcp();</li>
<li>ethernet_J11D.Interface.Open();</li>
</ol></div>
</p></div>
</p></div>
<p>As an aside I noticed that it can take the WiFi module a while to start-up so I added the initialisation into a delayed timer. </p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e8189e62-d863-413f-962b-161c8a0276e4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#008000;">//WiFi connect</span></li>
<li style="background:#f3f3f3;">GT.<span style="color:#2b91af;">Timer</span> wifiTmr = <span style="color:#0000ff;">new</span> GT.<span style="color:#2b91af;">Timer</span>(15000);</li>
<li>wifiTmr.Tick += <span style="color:#0000ff;">new</span> GT.<span style="color:#2b91af;">Timer</span>.<span style="color:#2b91af;">TickEventHandler</span>(wifiTmr_Tick);</li>
<li style="background:#f3f3f3;">wifiTmr.Start();</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>24) Again it is wise to add some sanity checks here, but we will assume that our networking is perfect , that we get an IP address and simply start a webserver on port 80. The great thing about the webserver is that you can setup events on the url, here we add an event that will be triggered when the url <a href="http://&lt;IP&gt;/Temp">http://&lt;IP&gt;/Temp</a> is requested.&nbsp; (Simple eh!)</p>
<p>&nbsp;</p>
</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:926dcfe0-8da0-4ac3-a6cd-517a2e555820" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> Interface_NetworkAddressChanged(<span style="color:#0000ff;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</li>
<li style="background:#f3f3f3;">{</li>
<li>   <span style="color:#2b91af;">WebServer</span>.StartLocalServer(ethernet_J11D.Interface.NetworkInterface.IPAddress, 80);</li>
<li style="background:#f3f3f3;">   <span style="color:#2b91af;">WebEvent</span> we = <span style="color:#2b91af;">WebServer</span>.SetupWebEvent(<span style="color:#a31515;">&quot;Temp&quot;</span>);</li>
<li>   we.WebEventReceived += <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">WebEvent</span>.<span style="color:#2b91af;">ReceivedWebEventHandler</span>(we_WebEventReceived);</li>
<li style="background:#f3f3f3;">}</li>
</ol></div>
</p></div>
</p></div>
</p>
<p>&nbsp;</p>
<p>It is always polite to return a response,&nbsp; we can simply return a webpage (basic!) </p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:15b63bf3-ca98-4d38-9807-de3de2e68755" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;">
<div style="background:#fff;overflow:auto;">
<ol style="background:#ffffff;margin:0;padding:0 0 0 5px;white-space:nowrap;">
<li><span style="color:#0000ff;">void</span> we_WebEventReceived(<span style="color:#0000ff;">string</span> path, <span style="color:#2b91af;">WebServer</span>.<span style="color:#2b91af;">HttpMethod</span> method, <span style="color:#2b91af;">Responder</span> responder)</li>
<li style="background:#f3f3f3;">{</li>
<li>    </li>
<li style="background:#f3f3f3;">    responder.Respond(<span style="color:#a31515;">&quot;&lt;html&gt;&lt;body&gt; Temperature : &quot;</span>+ txtMsgTemp.TextContent+ <span style="color:#a31515;">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span>);</li>
<li>}</li>
</ol></div>
</p></div>
</p></div>
<p>&nbsp;</p>
<h3>Summary </h3>
<p>In just 2 hours we have built an example data logger, with a graphical frontend, storage capabilities as well as making the datalogger internet capable! Aside from the SimpleGraph capabilities we have assembled all the hardware and typed all the code to make the prototype work, including importing a custom font. Hopefully this demonstrates the power of .NET Gadgeteer for rapid prototype creations! A few shortcuts were taken and it is highly recommended to add some error checking.</p>
<p>Note: Throughout this presentation I have purposely iterated through the development cycle to simulate the process you will encounter developing a prototype. Each change resulted in a build, compile and deploy (F5 in VS) and then a simple test to show the expected output. We will have also experienced the debug breakpoint feature which is very powerful. It is important to realise that we can halt execution on the actual hardware and inspect the objects that are in scope, this is probably one of the most valuable debug features and is a fantastic capability for embedded devices!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/stevenjohnston.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/stevenjohnston.wordpress.com/235/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=stevenjohnston.co.uk&#038;blog=35589610&#038;post=235&#038;subd=stevenjohnston&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://stevenjohnston.co.uk/2012/12/12/net-gadgeteer-temperature-logger-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/21b86213a078a688ab933f8c013e6502?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sjj698</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb13.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image9_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image19_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image22_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/picture000_thumb.jpg" medium="image">
			<media:title type="html">picture000</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb9.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/wp_000674_thumb.jpg" medium="image">
			<media:title type="html">WP_000674</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb10.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb14.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb15.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://stevenjohnston.files.wordpress.com/2012/12/image_thumb12.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
