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

<channel>
	<title>veys.com &#187; code</title>
	<atom:link href="http://veys.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://veys.com</link>
	<description>if I only had a tagline.</description>
	<lastBuildDate>Tue, 27 Sep 2011 21:35:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>An open source LED grid clock</title>
		<link>http://veys.com/2011/09/26/an-open-source-led-grid-clock/</link>
		<comments>http://veys.com/2011/09/26/an-open-source-led-grid-clock/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 04:50:19 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[gadget]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://veys.com/?p=382</guid>
		<description><![CDATA[So your analog wall clock breaks&#8230; What do you do about it? Make a digital clock, of course. Lumitock is an &#8220;LED grid&#8221; clock, where the time is represented by the number of lit LEDs within a segment of the clock. Lumitock is based on the Arduino platform, specifically using an Arduino Pro Mini 328 [...]]]></description>
			<content:encoded><![CDATA[<p>So your analog wall clock breaks&#8230;  What do you do about it?  Make a digital clock, of course.  Lumitock is an &#8220;LED grid&#8221; clock, where the time is represented by the number of lit LEDs within a segment of the clock.</p>
<p>Lumitock is based on the Arduino platform, specifically using an Arduino Pro Mini 328 (socketed, I have commitment issues).  It also uses a <a href="http://www.maxim-ic.com/datasheet/index.mvp/id/3128">DS1337C RTC</a> for time-keeping, and <a href="http://www.ti.com/product/tlc5940">TLC5940 LED drivers</a> to keep track of all that light.  I used <a href="http://batchpcb.com">BatchPCB</a> to fab some boards for it, and they turned out great.  Very solid, and such an easy service when time isn&#8217;t much of a factor.</p>
<p>It is open hardware and software, <a href="http://github.com/nickveys/lumitock">available on github</a>.  The Arduino project as well as Eagle CAD project files are included.</p>
<p><strong>Beware!</strong>  The <a href="https://github.com/nickveys/lumitock/commits/version-1">current version (tag version-1)</a> has a fairly nasty issue with the PCB layout, the TLC5940 sockets were drawn as .4&#8243; wide, not .3&#8243;.  Not fun to solder that up, you can see the kludgey fixes in the images.  I will be updating the PCB as well as making a SMT-only version to slim things up.</p>
<p><a href="http://www.youtube.com/watch?v=MHyRmk73WY8"><img src="http://img.youtube.com/vi/MHyRmk73WY8/2.jpg"></a></p>
<p><a href="http://www.youtube.com/watch?v=MHyRmk73WY8">Click here</a> to view the video on YouTube.</p>

<h2>Images</h2>

<a href='http://veys.com/2011/09/26/an-open-source-led-grid-clock/img_2490/' title='IMG_2490'><img width="150" height="150" src="http://veys.com/wp-content/uploads/2011/09/IMG_2490-150x150.jpg" class="attachment-thumbnail" alt="IMG_2490" title="IMG_2490" /></a>
<a href='http://veys.com/2011/09/26/an-open-source-led-grid-clock/img_2495/' title='IMG_2495'><img width="150" height="150" src="http://veys.com/wp-content/uploads/2011/09/IMG_2495-150x150.jpg" class="attachment-thumbnail" alt="IMG_2495" title="IMG_2495" /></a>
<a href='http://veys.com/2011/09/26/an-open-source-led-grid-clock/img_2501/' title='IMG_2501'><img width="150" height="150" src="http://veys.com/wp-content/uploads/2011/09/IMG_2501-150x150.jpg" class="attachment-thumbnail" alt="IMG_2501" title="IMG_2501" /></a>

]]></content:encoded>
			<wfw:commentRss>http://veys.com/2011/09/26/an-open-source-led-grid-clock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Migrating multi-project Subversion repositories to Git</title>
		<link>http://veys.com/2010/07/24/migrating-multi-project-subversion-repositories-to-git/</link>
		<comments>http://veys.com/2010/07/24/migrating-multi-project-subversion-repositories-to-git/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 00:11:04 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn2git]]></category>
		<category><![CDATA[svnadmin]]></category>
		<category><![CDATA[svndumpfilter]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://veys.com/?p=375</guid>
		<description><![CDATA[So I finally gave in and submitted to the version control Gods, Git is the way to go.  Of course it would happen that Git repositories are typically a single-project-per-repo situation, and I have a couple globbed together Subversion repositories with many projects in them.  How do I separate out my histories from the individual [...]]]></description>
			<content:encoded><![CDATA[<p>So I finally gave in and submitted to the version control Gods, Git is the way to go.  Of course it would happen that Git repositories are typically a single-project-per-repo situation, and I have a couple globbed together Subversion repositories with many projects in them.  How do I separate out my histories from the individual projects, maintain tags and branches, and not lose that precious history that nobody really ever looks at anyway?</p>
<p>Well, luckily there is a nifty script called <a title="svn2git" href="http://github.com/nirvdrum/svn2git">svn2git</a> that gets us the bulk work of migrating history of a single Subversion repo to a new Git repo.  Coupled with a few admin tools that come with Subversion, we got it covered.</p>
<h2>The current situation</h2>
<p>I have a couple repositories, looking like the following:</p>
<pre>/var/lib/svn/repo1
  /project1
    /trunk
    /tags
    /branches
  /project2
    /trunk
    /tags
    /branches
/var/lib/svn/repo2
  /project3
    /trunk
    /tags
    /branches
  /project4
    /trunk
    /tags
    /branches</pre>
<h2>The desired situation</h2>
<p>I&#8217;d like them all separated, with all history!!!  Resulting in the following structure:</p>
<pre>/var/lib/git/project1.git
/var/lib/git/project2.git
/var/lib/git/project3.git
/var/lib/git/project4.git</pre>
<h2>Step 1 &#8211; Dump your projects from the Subversion repository</h2>
<p>Subversion ships with a tool, svnadmin, that will help dump a repository.  It must operate on the local filesystem of the Subversion repository, not a working copy.  As a result, it only dumps full repositories, hence the problem in general.</p>
<p>Be not afraid though!  Subversion also ships with svndumpfilter, the key to our salvation.</p>
<p>For my example above, to dump project1, project2, project3 and project4 into separated Subversion repositories, I would:</p>
<pre>mkdir svn-convert &amp;&amp; cd svn-convert
svnadmin create project1.svn
svnadmin create project2.svn
svnadmin create project3.svn
svnadmin create project4.svn
svnadmin dump /var/lib/svn/repo1 | svndumpfilter include project1 | svnadmin load project1.svn
svnadmin dump /var/lib/svn/repo1 | svndumpfilter include project2 | svnadmin load project2.svn
svnadmin dump /var/lib/svn/repo2 | svndumpfilter include project3 | svnadmin load project3.svn
svnadmin dump /var/lib/svn/repo2 | svndumpfilter include project4 | svnadmin load project4.svn</pre>
<p>And viola, separated Subversion repositories.</p>
<h2>Step 2 &#8211; Convert via svn2git</h2>
<p>First, download and install <a href="http://github.com/nirvdrum/svn2git">svn2git</a>, it&#8217;s a Ruby script, the instructions are good, it&#8217;ll work for you.</p>
<p>Next, create a text file, &#8216;authors.txt&#8217; with contents like the following:</p>
<pre>name1 = Full Name &lt;email@example.com&gt;
name2 = Another Name &lt;email2@example.com&gt;</pre>
<p>Substituting the names of users who have committed to the Subversion repositories of course.  This will map the Subversion users to Git authors.</p>
<p>Next, we make a place for the new repository and run the conversion utility.  It will init the Git repository and create the necessary history.</p>
<pre>mkdir project1.git &amp;&amp; cd project1.git
svn2git file:///home/nick/svn-convert/project1.svn \
    --trunk project1/trunk --branches project1/branches \
    --tags project1/tags --authors ../authors.txt</pre>
<p>Repeated for each repository with appropriate substitutions, and you&#8217;ve got yourself some Git repositories.</p>
<p>Quick verification should show you you&#8217;re in business:</p>
<pre>$ git tag
v1.0
v1.0.1
v1.0.2
v1.0.3</pre>
<pre>$ git branch
* master
  ticket102
  ticket87</pre>
<p>All that is left is to put them in a safe place and start cloning!</p>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2010/07/24/migrating-multi-project-subversion-repositories-to-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time to broadcast, iPhone style</title>
		<link>http://veys.com/2009/06/27/time-to-broadcast-iphone-style/</link>
		<comments>http://veys.com/2009/06/27/time-to-broadcast-iphone-style/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 22:40:56 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[broadcast]]></category>
		<category><![CDATA[bsd]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[sockets]]></category>
		<category><![CDATA[udp]]></category>

		<guid isPermaLink="false">http://veys.com/?p=346</guid>
		<description><![CDATA[Ran into a need to dynamically determine the current UDP broadcast address for the WiFi interface on the ole&#8217; iPhone. Since NSHost appears to be a private API even w/the 3.0 software, it seems one must go lower. I wrapped it up in a neat little bundle that seems fairly usable if not verbose and [...]]]></description>
			<content:encoded><![CDATA[<p>Ran into a need to dynamically determine the current UDP broadcast address for the WiFi interface on the ole&#8217; iPhone.  Since NSHost appears to be a private API even w/the 3.0 software, it seems one must go lower.  I wrapped it up in a neat little bundle that seems fairly usable if not verbose and full of magic (but understandable) numbers.</p>
<p>A few things of note. en0 is the WiFi interface.  There are others.  Instrument the following code w/some debug to get them all out.  The ip/netmask methods return nil when the WiFi interface is not active.  I would also be shocked if there were no corner cases I am ignoring&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;arpa/inet.h&gt;</span>
<span style="color: #6e371a;">#include &lt;net/if.h&gt;</span>
<span style="color: #6e371a;">#include &lt;sys/ioctl.h&gt;</span>
<span style="color: #6e371a;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #6e371a;">#include &lt;errno.h&gt;</span>
<span style="color: #6e371a;">#include &lt;ifaddrs.h&gt;</span>
<span style="color: #6e371a;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>kWifiInterface <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en0&quot;</span>;
&nbsp;
<span style="color: #a61390;">@implementation</span> NetUtil
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>broadcastAddressForAddress<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ipAddress withMask<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>netmask <span style="color: #002200;">&#123;</span>
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> ipAddress, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IP address cannot be nil&quot;</span><span style="color: #002200;">&#41;</span>;
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> netmask, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Netmask cannot be nil&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>ipChunks <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ipAddress componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;.&quot;</span><span style="color: #002200;">&#93;</span>;
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>ipChunks count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">4</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IP does not have 4 octets!&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>nmChunks <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>netmask componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;.&quot;</span><span style="color: #002200;">&#93;</span>;
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>nmChunks count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">4</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Netmask does not have 4 octets!&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    NSUInteger ipRaw <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    NSUInteger nmRaw <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
    NSUInteger shift <span style="color: #002200;">=</span> <span style="color: #2400d9;">24</span>;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>NSUInteger i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; <span style="color: #2400d9;">4</span>; <span style="color: #002200;">++</span>i, shift <span style="color: #002200;">-=</span> <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        ipRaw |<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ipChunks objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span> &lt;&lt; shift;
        nmRaw |<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>nmChunks objectAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span> &lt;&lt; shift;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    NSUInteger bcRaw <span style="color: #002200;">=</span> ~nmRaw | ipRaw;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d.%d.%d.%d&quot;</span>, <span style="color: #002200;">&#40;</span>bcRaw <span style="color: #002200;">&amp;</span> 0xFF000000<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">24</span>,
            <span style="color: #002200;">&#40;</span>bcRaw <span style="color: #002200;">&amp;</span> 0x00FF0000<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">16</span>, <span style="color: #002200;">&#40;</span>bcRaw <span style="color: #002200;">&amp;</span> 0x0000FF00<span style="color: #002200;">&#41;</span> &gt;&gt; <span style="color: #2400d9;">8</span>, bcRaw <span style="color: #002200;">&amp;</span> 0x000000FF<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ipAddressForInterface<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ifName <span style="color: #002200;">&#123;</span>
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> ifName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Interface name cannot be nil&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>addrs <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>getifaddrs<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addrs<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failed to enumerate interfaces: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span><span style="color: #a61390;">strerror</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">errno</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* walk the linked-list of interfaces until we find the desired one */</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>addr <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>curAddr <span style="color: #002200;">=</span> addrs;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>curAddr <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>AF_INET <span style="color: #002200;">==</span> curAddr<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>curName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>curAddr<span style="color: #002200;">-</span>&gt;ifa_name<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>ifName isEqualToString<span style="color: #002200;">:</span>curName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #a61390;">char</span><span style="color: #002200;">*</span> cstring <span style="color: #002200;">=</span> inet_ntoa<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>curAddr<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr<span style="color: #002200;">&#41;</span>;
                addr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>cstring<span style="color: #002200;">&#93;</span>;
                <span style="color: #a61390;">break</span>;
            <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span>
        curAddr <span style="color: #002200;">=</span> curAddr<span style="color: #002200;">-</span>&gt;ifa_next;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/* clean up, return what we found */</span>
    freeifaddrs<span style="color: #002200;">&#40;</span>addrs<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> addr;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ipAddressForWifi <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>NetUtil ipAddressForInterface<span style="color: #002200;">:</span>kWifiInterface<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>netmaskForInterface<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ifName <span style="color: #002200;">&#123;</span>
    NSAssert<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> ifName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Interface name cannot be nil&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">struct</span> ifreq ifr;
    <span style="color: #a61390;">strncpy</span><span style="color: #002200;">&#40;</span>ifr.ifr_name, <span style="color: #002200;">&#91;</span>ifName UTF8String<span style="color: #002200;">&#93;</span>, IFNAMSIZ<span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> fd <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">==</span> fd<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failed to open socket to get netmask&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">==</span> ioctl<span style="color: #002200;">&#40;</span>fd, SIOCGIFNETMASK, <span style="color: #002200;">&amp;</span>ifr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failed to read netmask: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span><span style="color: #a61390;">strerror</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">errno</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        close<span style="color: #002200;">&#40;</span>fd<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    close<span style="color: #002200;">&#40;</span>fd<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>cstring <span style="color: #002200;">=</span> inet_ntoa<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>ifr.ifr_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>cstring<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>netmaskForWifi <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>NetUtil netmaskForInterface<span style="color: #002200;">:</span>kWifiInterface<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://veys.com/2009/06/27/time-to-broadcast-iphone-style/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>NSURLConnection + startImmediately:NO == boom?</title>
		<link>http://veys.com/2008/08/17/nsurlconnection-startimmediatelyno-boom/</link>
		<comments>http://veys.com/2008/08/17/nsurlconnection-startimmediatelyno-boom/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 00:59:47 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[nsurlconnection]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://www.veys.com/?p=320</guid>
		<description><![CDATA[Having issues creating NSURLConnections using initWithRequest:delegate:startImmediately? NSURLConnection *c = &#91;&#91;NSURLConnection alloc&#93; initWithRequest:&#91;NSURLRequest requestWithURL:url&#93; delegate:self startImmediately:NO&#93;; Apparently when not using the simpler initWithRequest:delegate:, or even startImmediately:YES, the connection does not get scheduled in the current run loop. And again apparently, this causes unhappiness to occur when you eventually get around to calling start. Simple fix, just [...]]]></description>
			<content:encoded><![CDATA[<p>Having issues creating NSURLConnections using initWithRequest:delegate:startImmediately?</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>
                                                     delegate<span style="color: #002200;">:</span>self
                                             startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Apparently when not using the simpler initWithRequest:delegate:, or even startImmediately:YES, the connection does not get scheduled in the current run loop.  And again apparently, this causes unhappiness to occur when you eventually get around to calling start.</p>
<p>Simple fix, just stuff it in the current run loop before calling start and everyone gets along just fine.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>c scheduleInRunLoop<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSRunLoop</span> currentRunLoop<span style="color: #002200;">&#93;</span> forMode<span style="color: #002200;">:</span>NSDefaultRunLoopMode<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>c start<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If there is something I am doing wrong or something I can do to prevent this, I&#8217;d like to know.  Alas, the API is fairly brief on NSURLConnection, I don&#8217;t think I&#8217;m missing anything.  This seems consistent with Cocoa, Cocoa Touch.</p>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2008/08/17/nsurlconnection-startimmediatelyno-boom/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python SubWCRev</title>
		<link>http://veys.com/2007/10/26/python-subwcrev/</link>
		<comments>http://veys.com/2007/10/26/python-subwcrev/#comments</comments>
		<pubDate>Sat, 27 Oct 2007 06:52:19 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[windows sucks]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=64</guid>
		<description><![CDATA[Fired out a little Python script for exercise&#8230; pysubwcrev is a Python version of TortoiseSVN&#8217;s SubWCRev app. SubWCRev is a windows-only console app, pysubwcrev is a command-line argument compatible replacement that is Python-based, and therefore runs on any platform with an available Python interpreter and pysvn. Currently only Linux is tested. The code is hosted @ github. Currently no [...]]]></description>
			<content:encoded><![CDATA[<p>Fired out a little Python script for exercise&#8230;</p>
<p>pysubwcrev is a Python version of <a class="external text" title="http://tortoisesvn.net" rel="nofollow" href="http://tortoisesvn.net/">TortoiseSVN&#8217;s</a> <a class="external text" title="http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html" rel="nofollow" href="http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html">SubWCRev</a> app. SubWCRev is a windows-only console app, pysubwcrev is a command-line argument compatible replacement that is Python-based, and therefore runs on any platform with an available Python interpreter and pysvn. Currently only Linux is tested.</p>
<p>The code is hosted @ <a class="external text" title="http://github.com/nickveys/pysubwcrev" rel="nofollow" href="http://github.com/nickveys/pysubwcrev">github</a>. Currently no packaged release exists but it is (should be?) feature complete.</p>
<p>Note: This is just a hack at playing w/Python, jabs and criticism w/the style can come in the form of patches.</p>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2007/10/26/python-subwcrev/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditional Jumping in PIC16 Assembly</title>
		<link>http://veys.com/2003/02/27/conditional-jumping-in-pic16-asm/</link>
		<comments>http://veys.com/2003/02/27/conditional-jumping-in-pic16-asm/#comments</comments>
		<pubDate>Fri, 28 Feb 2003 03:51:09 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[pic16]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=237</guid>
		<description><![CDATA[PIC Microcontrollers have a funky way of handling conditionals. I&#8217;d like to present a set of macros I&#8217;ve made to make this easier to use, as well as explain the basics behind the technique in general. Most MCU&#8217;s I&#8217;ve worked with before PICs had nice simple conditional statements&#8230; The mnemonic was usually to the effect [...]]]></description>
			<content:encoded><![CDATA[<p>PIC Microcontrollers have a funky way of handling conditionals. I&#8217;d like to present a set of macros I&#8217;ve made to make this easier to use, as well as explain the basics behind the technique in general.</p>
<p>Most MCU&#8217;s I&#8217;ve worked with before PICs had nice simple conditional statements&#8230; The mnemonic was usually to the effect of &#8220;branch if x to destination&#8221;. Not so on the PICs.</p>
<p>On a PIC, we have to do an operation between the data to test, then either skip the next instruction or not, based on the results of this test.</p>
<p>Take the instruction &#8220;BTFSS&#8221; meaning &#8220;Bit-test F, Skip if Set&#8221;. This instruction takes a register and a bit number, and will jump over the next instruction if that bit number in the register is set. For example:</p>
<p>We have 2 registers, REG1 and REG2. We want to know if the value in REG1 equals the value in REG2. A quick, simple way to do this is first to load one into W, then subtract the other from it, with the result going in to W as not to destroy one of the variables. This sets the &#8220;ZERO&#8221; bit in the STATUS register to either 1 if the operation resulted in a 0, or 0 otherwise. We know if the operation resulted in 0 the two values are equal, so we can code like so:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">TESTIFZERO
  movf REG1<span style="color: #339933;">,</span> w
  subwf REG2<span style="color: #339933;">,</span> w
  btfss STATUS<span style="color: #339933;">,</span> Z
    <span style="color: #000000; font-weight: bold;">goto</span> NOTEQUAL
EQUAL
  <span style="color: #666666; font-style: italic;">; they were equal</span>
  <span style="color: #000000; font-weight: bold;">goto</span> DONETESTING
NOTEQUAL
  <span style="color: #666666; font-style: italic;">; they weren't equal</span></pre></div></div>

<p>So we can test for equality.</p>
<p>We can also macro-ize it for ease of use&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">BEQ <span style="color: #000000; font-weight: bold;">macro</span> REG1<span style="color: #339933;">,</span> REG2<span style="color: #339933;">,</span> DEST	<span style="color: #666666; font-style: italic;">; branch if REG1 == REG2</span>
  movf REG2<span style="color: #339933;">,</span> W			<span style="color: #666666; font-style: italic;">; W &amp;lt;- REG2</span>
  subwf REG1<span style="color: #339933;">,</span> W			<span style="color: #666666; font-style: italic;">; W &amp;lt;- REG1 - REG2</span>
  btfsc STATUS<span style="color: #339933;">,</span> Z		<span style="color: #666666; font-style: italic;">; if result was nonzero: skip out</span>
    <span style="color: #000000; font-weight: bold;">goto</span> DEST			<span style="color: #666666; font-style: italic;">; otherwise jump</span>
  <span style="color: #000000; font-weight: bold;">endm</span></pre></div></div>

<p>This gives us a macro we can call with 2 registers and a destination, and have it jump there if the condition ends up being true, and just pass on through if it&#8217;s not true. Much, much easier to use.</p>
<p>The other tests: inequality, less than, greater than, less than or equal to, greater than or equal to and so on follow a similar pattern. They are all covered in the &#8220;conditionals.inc&#8221; file I use quite often. I have both register-register comparisons and register-literal comparisons in there. Feel free to grab a copy and use it in your next project.</p>
<h3>Downloads</h3>
<ul>
<li><a href="http://www.veys.com/wp-content/uploads/2008/07/conditionals.zip">conditionals</a> (zip)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2003/02/27/conditional-jumping-in-pic16-asm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PIC buttons (interrupt-based)</title>
		<link>http://veys.com/2002/08/23/pic-buttons-interrupt-based/</link>
		<comments>http://veys.com/2002/08/23/pic-buttons-interrupt-based/#comments</comments>
		<pubDate>Sat, 24 Aug 2002 05:08:56 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[interrupts]]></category>
		<category><![CDATA[legacy articles]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=124</guid>
		<description><![CDATA[Previously, in PIC buttons (polling) we saw how to poll for the state of a line connect to a button, that is all fine and good but really that is not the best way to do them. The &#8220;real&#8221; way to interface with external components like that is through interrupts, a slick feature. Interrupts provide [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_250" class="wp-caption alignright" style="width: 160px"><a href="http://www.veys.com/wp-content/uploads/2008/07/interrupt_schematic.gif" rel="lightbox[124]"><img class="size-thumbnail wp-image-250" title="Schematic" src="http://www.veys.com/wp-content/uploads/2008/07/interrupt_schematic-150x150.gif" alt="Schematic" width="150" height="150" /></a><p class="wp-caption-text">Schematic</p></div>
<p>Previously, in <a href="http://veys.com/2002/08/22/pic-buttons-polling/" title="PIC buttons (polling)">PIC buttons (polling)</a> we saw how to poll for the state of a line connect to a button, that is all fine and good but really that is not the best way to do them. The &#8220;real&#8221; way to interface with external components like that is through interrupts, a slick feature.</p>
<p>Interrupts provide you with lots of freedom in your code. They allow you to sit back, relax, and be told when an event occurs, and not be forced to sit and wait for it to happen.</p>
<p>For this program, the schematic and circuit are practically the same, the only thing that changed location is the button.</p>
<p>Instead of looping over and over again, we simply wait, using a goto $ we are essentially goto&#8217;ing the same address over and over, &#8220;goto here, goto here, goto here&#8230;&#8221; ad nauseum. A common technique is also to use a SLEEP command, which puts the PIC in a low power mode and halts the program counter. Same effect to the user though. You of course could do &#8216;real&#8217; work too instead of just burn cycles.</p>
<p>Once the button is pressed and released, the PIC will generate an interrupt, forcing the program to goto memory location 4. This is labeled in the code as ISR (Interrupt Service Routine).</p>
<p>For this program we are using the RB0/INT Interrupt. This interrupt occurs when there is a low-high change in PORTB,0. It can also be configured for high-low as well.</p>
<p>To enable this, we set INTCON,INTE. This bit says we want to know if a change occurs. To enable interrupts in general, we must then set INTCON,GIE. This lets all enabled interrupts occur.</p>
<p>We then wait for the interrupt. Once it occurs, GIE is automatically cleared so we can&#8217;t have them inside each other, and blink the LED a couple times. We then clear the interrupt flag, saying we&#8217;ve handled the interrupt (INTCON,INTF). We then re-enable interrupts and return from the interrupt: retfie.</p>
<p>It should be noted that if we were using more than one interrupt type, we would have needed to check the flag bits to find out which one interrupted us. We then handle it, and clear its flag. The PIC is somewhat crippled in this manner. Any and all interrupts generated and thrown into address 4 and &#8220;we&#8221; have to figure out which one occured. Many higher end MCU&#8217;s will have a table of addresses to jump to for each particular interrupt type, we then code in each location the correct routine and the processor knows which to call based on what happens.</p>
<h3>Downloads</h3>
<ul>
<li><a href="http://www.veys.com/wp-content/uploads/2008/07/interrupts.zip">Source Code</a> (zip)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2002/08/23/pic-buttons-interrupt-based/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PIC buttons (polling)</title>
		<link>http://veys.com/2002/08/22/pic-buttons-polling/</link>
		<comments>http://veys.com/2002/08/22/pic-buttons-polling/#comments</comments>
		<pubDate>Fri, 23 Aug 2002 05:07:52 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[legacy articles]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=122</guid>
		<description><![CDATA[Polling for button input, how useful! This is pretty brief and gives a good idea how to let buttons control your programs execution. In this tutorial I&#8217;ve switched from using an oscillator to using a crystal. This changes the design a bit. Using the 2 OSC pins, OSC1/OSC2 (CLKOUT/CLKIN) they hook to the crystal in [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_243" class="wp-caption alignright" style="width: 160px"><a href="http://www.veys.com/wp-content/uploads/2008/07/polling_schematic.gif" rel="lightbox[122]"><img class="size-thumbnail wp-image-243" title="Schematic" src="http://www.veys.com/wp-content/uploads/2008/07/polling_schematic-150x150.gif" alt="" width="150" height="150" /></a><p class="wp-caption-text">Schematic</p></div>
<p>Polling for button input, how useful! This is pretty brief and gives a good idea how to let buttons control your programs execution.</p>
<p>In this tutorial I&#8217;ve switched from using an oscillator to using a crystal. This changes the design a bit. Using the 2 OSC pins, OSC1/OSC2 (CLKOUT/CLKIN) they hook to the crystal in parallel. Then the two sides of the crystal are connected to ground via two capacitors, in this case 18pF. The speed and capacitance needed varies, and can be seen in most any PIC MCU datasheet as to how to lay it out. It will also be in my schematic.</p>
<p>Please note this is essentially NO DIFFERENT than the oscillator. It is simply a different means of providing a clock signal. You can swap in the oscillator to the normal CLKIN pin and it will work just fine.</p>
<p>For this program, we&#8217;ll start the processor, and wait for a button to be pressed. After it has been pressed we&#8217;ll essentially execute the LED blinker from before.</p>
<p>Polling is extremely simple. Polling is the act of checking something for a certain state to occur. In our case, we poll PORTB,4 waiting for it to go low. Once it does, we continue on and start blinking.</p>
<div id="attachment_242" class="wp-caption alignright" style="width: 160px"><a href="http://www.veys.com/wp-content/uploads/2008/07/polling_circuit.jpg" rel="lightbox[122]"><img class="size-thumbnail wp-image-242" title="Circuit Image" src="http://www.veys.com/wp-content/uploads/2008/07/polling_circuit-150x150.jpg" alt="Circuit Image" width="150" height="150" /></a><p class="wp-caption-text">Circuit Image</p></div>
<p>Another topic that needs to be covered is that of Pull-Up resistors. In order for a button to be able to change from 0 to 1 (Ground to 5V) we need a way to protect everything from a short circuit. We do this with a Pull-Up (or Pull-Down in some cases) resistor. For our case, we connect PORTB,4 to +5V via a 10k resistor. So when we fire up the program, the PIC sees PORTB,4 as HIGH. We then connect our button to PORTB,4 on one side, and Ground on the other. Now, when we press the button, PORTB,4 is connected to ground and is now LOW. A short circuit between +5 and Ground would occur if not for the resistor, which being 10k limits the current to a measly 500uA, nothing to worry about. Once the button is released, Ground is disconnected and the pin returns to a HIGH state.</p>
<p>Reading the code, you may also notice the _BANK macro has changed a bit. I&#8217;ve simply modified it to encompass all possible BANK configs instead of using 4 different macros for each. Reading through it can give you a little insight as to how the conditional assembler works. It&#8217;s a bit like C/C++ and a bit like BASIC. Quite nice and handy.</p>
<p>A final new bit is the _MCLRE_OFF in the __CONFIG line at the beginning. This frees us from having to pull _MCLR high to keep from a RESET condition. Just keeps our parts count down. <img src='http://veys.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Now for the new section of code.</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">  btfsc PORTB<span style="color: #339933;">,</span><span style="color: #0000ff;">4</span>
    <span style="color: #000000; font-weight: bold;">goto</span> $<span style="color: #339933;">-</span><span style="color: #0000ff;">1</span></pre></div></div>

<p>That&#8217;s polling, yup, that&#8217;s it. All it is doing is a bit test on PORTB,4, waiting for it to become clear (Ground). The goto line is telling it to go 1 instruction back ($ means the address of the current instruction, $-1 means one before, the btfsc). Once that pin becomes 0 it skips the loop forcing it to check, and lets it continue on down to the blinker!</p>
<h3>Downloads</h3>
<ul>
<li><a href="http://www.veys.com/wp-content/uploads/2008/07/poll.zip">Source Code</a> (zip)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2002/08/22/pic-buttons-polling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PIC LED blinker (busy-wait)</title>
		<link>http://veys.com/2002/08/21/pic-led-blinker-busy-wait/</link>
		<comments>http://veys.com/2002/08/21/pic-led-blinker-busy-wait/#comments</comments>
		<pubDate>Thu, 22 Aug 2002 04:18:18 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[blinker]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[led]]></category>
		<category><![CDATA[legacy articles]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=120</guid>
		<description><![CDATA[Beyond all doubt, the #1 beginning program in microcontrollers is the LED blinker. It&#8217;s super simple, and teaches the concept of pin voltages and busy-waits. Here is a busy-wait LED blinker program, and a walkthrough building it in MPLab. First, the delay. This is a busy-wait delay program, busy-wait means you just burn instruction cycles [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_226" class="wp-caption alignright" style="width: 160px"><a href="http://www.veys.com/wp-content/uploads/2008/07/pic16_blinker_schem.jpg" rel="lightbox[120]"><img class="size-thumbnail wp-image-226  " title="LED blinker #1 circuit schematic" src="http://www.veys.com/wp-content/uploads/2008/07/pic16_blinker_schem-150x150.jpg" alt="LED blinker #1 circuit schematic" width="150" height="150" /></a><br />
<p class="wp-caption-text">Schematic</p></div>
<p>Beyond all doubt, the #1 beginning program in microcontrollers is the LED blinker. It&#8217;s super simple, and teaches the concept of pin voltages and busy-waits.  Here is a busy-wait LED blinker program, and a walkthrough building it in MPLab.</p>
<p>First, the delay. This is a busy-wait delay program, busy-wait means you just burn instruction cycles for the delay, keeping the MCU &#8220;busy&#8221;. There&#8217;s a tiny bit of math behind them. First, the clock speed is 20MHz, the instruction frequency is (clock/4) so our instructions are executing at 5MHz. This gives us a period of 200ns per cycle.</p>
<p>I created two delay functions, for versatility, and for LCD stuff which will come later but it&#8217;s handy here. One is a &#8220;DELAY_US&#8221; which will delay a specified amount of microseconds. This is done by wasting 5 cycles (5*200ns = 1us) a specified amount of times (less than or equal to 255us, since I only made an 8-bit &#8220;delay&#8221; variable). We can learn the cycle times from the data sheet, and make it work from there.</p>
<p>Next, I created a &#8220;DELAY_MS&#8221; which delays a specified amount of milliseconds. Same 8-bit limitation of max 255ms, but that&#8217;s enough to have fun with&#8230; It simply calls DELAY_US a few times with specified amounts of delay, adding up to 1000us (=1ms) and repeats as many times as we tell it to.</p>
<p>These delays are used to make the LED blinking visible, otherwise it would blink faster than we could see (if at all, it takes a cycle or two for the pin to change state so it might not even change if we just toggle it every other cycle).</p>
<p>Here&#8217;s a little more PIC architecture information. The PIC&#8217;s data registers are broken into &#8220;banks&#8221; (bank 0, 1, 2, 3). Meaning you cannot get at them all at the same time, although some are mapped to all banks so you CAN get at them, important ones. We usually hang out in bank 0&#8230; This usually isn&#8217;t a problem, just something you need to remember. The data sheet illustrates it pretty well. When it comes up I&#8217;ll clarify things about it.</p>
<p>Also, for the I/O pins. Since they&#8217;re bi-directional, you need to choose which direction to set them to. Input or output. This is done by setting the tri-state register for the given port, for example PORTA&#8217;s tris register is called TRISA (not tough!). You set the direction of a specific bit, by setting the bit of the TRIS register to either 0 or 1, 0 meaning OUTPUT and 1 meaning INPUT. Not tough to remember: 0 = Out and 1 = In.</p>
<h3>Source Code</h3>
<p>Ok, first you need to make sure you have MPLab from Microchip.com</p>
<p>Once you have MPLab, download the LED Blinker (busy-wait) source code.</p>
<p>MPLab is a nice IDE, you&#8217;ll need to create a &#8220;project&#8221; and then pick your chip, and add the asm file to it (called a &#8220;node&#8221;). All code is going to be indented 2 spaces, labels will not be indented at all, assembler directives are either 1 or 2 spaces in&#8230;</p>
<p>First few lines are kinda simple, the title directive just sets a title for your project&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">title</span>  <span style="color: #7f007f;">&quot;LED Blinker Tutorial 1&quot;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">LIST</span> R=<span style="color: #00007f; font-weight: bold;">DEC</span>
  <span style="color: #000000; font-weight: bold;">INCLUDE</span> <span style="color: #7f007f;">&quot;p16f628.inc&quot;</span>
 __CONFIG _CP_OFF &amp; _WDT_OFF &amp; _HS_OSC &amp; _PWRTE_ON &amp; _LVP_OFF &amp; _MCLRE_ON</pre></div></div>

<p>LIST R = DEC sets the default &#8220;radix&#8221; for the program, meaning the number base. So if I put 100 in a line somewhere, it means 100 DECIMAL. If I changed it to LIST R = HEX, then if I put 100 in somewhere, it means 0&#215;100, TOTALLY different. I find DEC easier to work with, and you can still use 0x whatever and it means HEX so you get the best of both worlds.</p>
<p>Next we INCLUDE the &#8220;p16f628.inc&#8221; which will give us nice little names for our registers so we don&#8217;t need to remember their addresses, how nice of Microchip.</p>
<p>Then the __CONFIG line, arguably the ugliest line of code while still being fairly simple. You can see the list of __CONFIG&#8217;s in the &#8220;p16f628.inc&#8221; file, they&#8217;re simply setting the configuration of certain chip features, the ones I have listed do this: _CP_OFF copyprotect OFF, you can set the copyprotect bits to make your chip unreadable, this is only good for a production product, don&#8217;t use it. _WDT_OFF, watchdog timer off, watchdog timer is there for mission-critical applications, it&#8217;s constantly running and needs to be reset constantly, if it isn&#8217;t reset it will reset the chip (assuming your program has locked up), we don&#8217;t need it here. _HS_OSC specifies a high speed oscillator, it&#8217;s in the data sheet. _PWRTE_ON turns on the power up timer delay, it&#8217;ll make the MCU wait a bit before executing to make sure voltage is stable, oscillator is stable, etc&#8230; _LVP_OFF &#8211; low voltage programming, dun&#8217;need it&#8230; _MCLRE_ON makes us hold MCLR high, instead of letting the chip do it&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">; Variable declarations</span>
<span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
 CBLOCK <span style="color: #0000ff;">0x20</span>
DELAY<span style="color: #339933;">,</span>DELAYTMP			<span style="color: #666666; font-style: italic;">; delay function variables...</span>
 ENDC</pre></div></div>

<p>Now to the variable declarations, the CBLOCK directive lets us just list out our variable names, and the assembler will assign addresses for us, this is handy. The 0&#215;20 is the starting address of general-purpose registers in BANK 0. We list em out, then end it with ENDC.</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">; Macro declarations</span>
<span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
BANK0 <span style="color: #000000; font-weight: bold;">macro</span>           		<span style="color: #666666; font-style: italic;">; Switch to BANK0</span>
  bcf STATUS<span style="color: #339933;">,</span>RP1
  bcf STATUS<span style="color: #339933;">,</span>RP0
  <span style="color: #000000; font-weight: bold;">endm</span>
&nbsp;
BANK1 <span style="color: #000000; font-weight: bold;">macro</span>           		<span style="color: #666666; font-style: italic;">; Switch to BANK1</span>
  bcf STATUS<span style="color: #339933;">,</span>RP1
  <span style="color: #00007f; font-weight: bold;">bsf</span> STATUS<span style="color: #339933;">,</span>RP0
  <span style="color: #000000; font-weight: bold;">endm</span>
&nbsp;
DELAY_MILLI <span style="color: #000000; font-weight: bold;">macro</span> TIME
  movlw TIME
  movwf DELAY
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_MS
  <span style="color: #000000; font-weight: bold;">endm</span>
&nbsp;
DELAY_MICRO <span style="color: #000000; font-weight: bold;">macro</span> TIME
  movlw TIME
  movwf DELAY
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_US
  <span style="color: #000000; font-weight: bold;">endm</span></pre></div></div>

<p>Next the Macro&#8217;s&#8230; Macro&#8217;s are one of the coolest features of MPLab, it&#8217;s kinda like an inline C function, and kinda like a #define. When called, the code is dumped into where it was called from, but you can use variables in it, and even arguments to customize it&#8217;s compiling, stuff I&#8217;ll show in later programs.</p>
<p>Anyways, you make one by saying: NAME macro in the first column, then code. End it w/a endm. The ones I have are fairly simple and re-usable. BANK0 sets the bank bits to get us into BANK 0, go figure. BANK1 sets them to get us into BANK1, crazy!</p>
<p>DELAY_MILLI takes the TIME argument and loads it into W, next it moves W to the register labeled DELAY. Then it calls our illustrious DELAY_MS function which will be explained in detail down below&#8230; DELAY_MICRO does the same damn thing with DELAY_US!</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">; Program code</span>
<span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
  <span style="color: #000000; font-weight: bold;">PAGE</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">org</span> <span style="color: #0000ff;">0</span>
  <span style="color: #000000; font-weight: bold;">goto</span> MAIN
&nbsp;
 <span style="color: #000000; font-weight: bold;">org</span> <span style="color: #0000ff;">4</span>
ISR
  <span style="color: #666666; font-style: italic;">; interrupt handler</span>
  retfie</pre></div></div>

<p>PAGE is a pagebreak for printing, though it doesn&#8217;t work for me. <img src='http://veys.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>org 0 tells the assembler to start assembly at address 0, our first instruction is to jump (goto) the label MAIN.</p>
<p>org 4 starts us in the Interrupt address of PIC&#8217;s&#8230; This is kind of strange and will be explained later on, for now just accept it as fact&#8230;</p>
<p>And if you&#8217;re wondering if we lost space for 3 instructions between 0 and 4, you&#8217;re right. <img src='http://veys.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">; Subroutines</span>
<span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
DELAY_US			<span style="color: #666666; font-style: italic;">; busy wait of DELAY us</span>
				<span style="color: #666666; font-style: italic;">; 200ns instruction period assumed</span>
  <span style="color: #00007f; font-weight: bold;">nop</span>				<span style="color: #666666; font-style: italic;">; (1)</span>
  <span style="color: #00007f; font-weight: bold;">nop</span>				<span style="color: #666666; font-style: italic;">; (2)</span>
  decfsz DELAY<span style="color: #339933;">,</span>f		<span style="color: #666666; font-style: italic;">; test DELAY count (3)</span>
    <span style="color: #000000; font-weight: bold;">goto</span> DELAY_US		<span style="color: #666666; font-style: italic;">; loop if not done (4,5)</span>
  return			<span style="color: #666666; font-style: italic;">; gtfo (4,5)</span>
&nbsp;
DELAY_MS			<span style="color: #666666; font-style: italic;">; busy wait of DELAY ms</span>
				<span style="color: #666666; font-style: italic;">; dependant upon DELAY_US being accurate</span>
  movf DELAY<span style="color: #339933;">,</span>w
  movwf DELAYTMP		<span style="color: #666666; font-style: italic;">; save DELAY time</span>
DELAY_MS_LOOP			<span style="color: #666666; font-style: italic;">; inner loop</span>
  movlw <span style="color: #0000ff;">245</span>			<span style="color: #666666; font-style: italic;">; load 245 (1)</span>
  movwf DELAY			<span style="color: #666666; font-style: italic;">; into DELAY (2)</span>
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_US			<span style="color: #666666; font-style: italic;">; wait 245us (3-249)</span>
  movlw <span style="color: #0000ff;">245</span>			<span style="color: #666666; font-style: italic;">; load 245 (250)</span>
  movwf DELAY			<span style="color: #666666; font-style: italic;">; into DELAY (251)</span>
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_US			<span style="color: #666666; font-style: italic;">; wait 245us (252-498)</span>
  movlw <span style="color: #0000ff;">245</span>			<span style="color: #666666; font-style: italic;">; load 245 (499)</span>
  movwf DELAY			<span style="color: #666666; font-style: italic;">; into DELAY (500)</span>
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_US			<span style="color: #666666; font-style: italic;">; wait 245us (501-747)</span>
  movlw <span style="color: #0000ff;">246</span>			<span style="color: #666666; font-style: italic;">; load 246 (748)</span>
  movwf DELAY			<span style="color: #666666; font-style: italic;">; into DELAY (749)</span>
  <span style="color: #00007f; font-weight: bold;">call</span> DELAY_US			<span style="color: #666666; font-style: italic;">; wait 246us (750-997)</span>
  decfsz DELAYTMP<span style="color: #339933;">,</span>f		<span style="color: #666666; font-style: italic;">; test DELAYTMP count (998)</span>
    <span style="color: #000000; font-weight: bold;">goto</span> DELAY_MS_LOOP		<span style="color: #666666; font-style: italic;">; loop if not done (999,1000)</span>
  return			<span style="color: #666666; font-style: italic;">; gtfo (999,1000)</span></pre></div></div>

<p>Next we have our subroutines, the delays, I have these before the main lines of code just out of habit, it&#8217;s not required.</p>
<p>DELAY_US&#8230; Pretty simple really, we start out by burning 2 cycles, so we&#8217;ve waited 400ns so far, next we decrement our counter, and test if it&#8217;s zero, if it isn&#8217;t, we goto DELAY_US, looping again, if not, we return. The test itself takes one cycle (600ns so far) and either the goto or return take 2 cycles (1000ns = 1us) so we have our microsecond delay!</p>
<p>DELAY_MS works on the same principle, it&#8217;s accurate enough for this! <img src='http://veys.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">; Mainline of code</span>
<span style="color: #666666; font-style: italic;">;----------------------------------------------------------------------------------------</span>
MAIN
  BANK1
  bcf TRISA<span style="color: #339933;">,</span><span style="color: #0000ff;">2</span>			<span style="color: #666666; font-style: italic;">; PORT A, bit 2 is our output pin.</span>
  BANK0
&nbsp;
LOOP_BEGIN
  <span style="color: #00007f; font-weight: bold;">bsf</span> PORTA<span style="color: #339933;">,</span><span style="color: #0000ff;">2</span>  			<span style="color: #666666; font-style: italic;">; set her.</span>
  DELAY_MILLI <span style="color: #0000ff;">250</span>		<span style="color: #666666; font-style: italic;">; wait 1/4 sec</span>
  bcf PORTA<span style="color: #339933;">,</span><span style="color: #0000ff;">2</span>			<span style="color: #666666; font-style: italic;">; clear her!</span>
  DELAY_MILLI <span style="color: #0000ff;">250</span>		<span style="color: #666666; font-style: italic;">; wait 1/4 sec!!!!</span>
  <span style="color: #000000; font-weight: bold;">goto</span> LOOP_BEGIN		<span style="color: #666666; font-style: italic;">; forever... :o</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">end</span></pre></div></div>

<p>MAIN is our label for the beginning of the code, jumped to by the first line up @ org 0. BANK1 gets us into BANK 1 so we can set our bit direction, we clear bit 2 of TRISA making bit 2 of PORTA our output pin, then we hop back to BANK 0&#8230;</p>
<p>Then our introductory programming teachers worst nightmare, a purposely created infinite loop. We label the beginning, then set our pin high, shutting off the LED (as you&#8217;ll see in the wiring diagram). We wait 250ms via our handy delay function, then clear the bit, turning the LED on, we wait again and loop ad nauseum.</p>
<p>&#8216;end&#8217; tells the assembler to give up&#8230;</p>
<h3>Building</h3>
<p>Alright, so we have our program, we run the assembler by clicking the weird funnel icon or by going to Project -&gt; Build Node (or All). It&#8217;ll crunch and come up with no errors (of course).  Then just toss it into the programmer and feed the chip your tasty code.</p>
<h3>Schematic</h3>
<div id="attachment_225" class="wp-caption alignright" style="width: 160px"><a href="http://www.veys.com/wp-content/uploads/2008/07/pic16_blinker.jpg" rel="lightbox[120]"><img class="size-thumbnail wp-image-225" title="The assembled circuit" src="http://www.veys.com/wp-content/uploads/2008/07/pic16_blinker-150x150.jpg" alt="The assembled circuit" width="150" height="150" /></a><p class="wp-caption-text">The assembled circuit</p></div>
<p>Wire up the circuit as in the schematic at the top of the page.  Hopefully it illustrates to you why the LED is on when the bit is off, and off when the bit is on&#8230; The LED is a typical ~2V yellow LED&#8230;  Wired up it should look something like the image.</p>
<h3>Running</h3>
<p>Hook up +5V and Gnd, and fire it up! If everything is set up correctly you&#8217;ll get a steady blinking LED!</p>
<h3>Something to try</h3>
<p>Connect a momentary switch to the _MCLR line, wired to Ground on the other side. Pushing the button will reset the chip, releasing it will start it over from the beginning of the program, of course it will do the same stuff, but this demonstrates how the reset buttons work. I also highly recommend changing the code to add a more interesting blink pattern, longer/shorter delays, and other stuff to get used to modifying code&#8230;</p>
<h3>Downloads</h3>
<ul>
<li><a href="http://www.veys.com/wp-content/uploads/2008/07/busyblinker.zip">Source Code</a> (zip)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2002/08/21/pic-led-blinker-busy-wait/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  veys.com/category/code/feed/ ) in 2.52592 seconds, on Feb 6th, 2012 at 8:32 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 6th, 2012 at 8:34 am UTC -->
