<?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>SQUARISM &#187; C/C++</title>
	<atom:link href="http://squarism.com/category/development/cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://squarism.com</link>
	<description>until lambs become lions</description>
	<lastBuildDate>Mon, 07 May 2012 17:52:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Arduino command protocol</title>
		<link>http://squarism.com/2010/02/28/arduino-command-protocol/</link>
		<comments>http://squarism.com/2010/02/28/arduino-command-protocol/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 16:40:57 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/?p=354</guid>
		<description><![CDATA[UPDATE: Use CmdMessenger instead of Messenger. Here&#8217;s my IRC Arduino Bot. It uses a regular Arduino 328 and an Ethernet Shield both from sparkfun. As for software, I&#8217;m using the Ethernet2 library (see my previous post about this), the WString library and a homerolled IRC protocol parser. The breadboard&#8217;s power is connected to arbitrary pin [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> Use <a href="http://arduino.cc/playground/Code/CmdMessenger">CmdMessenger</a> instead of Messenger.</p>
<p>Here&#8217;s my IRC Arduino Bot.  It uses a regular <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=666">Arduino 328</a> and an <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9026">Ethernet Shield</a> both from sparkfun.  As for software, I&#8217;m using the Ethernet2 library (see <a href="http://squarism.com/2010/02/06/ethernet2-arduino-library-fix-on-0017/">my previous post</a> about this), the <a href="http://www.arduino.cc/en/Tutorial/TextString">WString library</a> and a homerolled <a href="http://www.ietf.org/rfc/rfc1459.txt">IRC protocol</a> parser.  The breadboard&#8217;s power is connected to arbitrary pin 5 and some resistors to keep the LED from burning out.</p>
<p><img src="http://squarism.com/wp-content/uploads/2010/02/arduino_irc_light-580x435.png" alt="arduino_irc_light" title="arduino_irc_light" width="580" height="435" class="aligncenter size-large wp-image-355" /></p>
<p>Basically, my bot joins an IRC channel and then listens for PRIVMSG commands starting with a password.  It takes those commands and controls an LED.  For example, I&#8217;d send this privately to the Arduino:<br />
<code>command password LEDON</code></p>
<p>And then the red LED comes on.  I tell it &#8220;LEDOFF&#8221; and it turns off.  Ok, it&#8217;s not a new RFC spec worthy of IEEE recognition and international adoption.  But it got the job done in a human-readable manner.  Previously on my facebook status light project, I had done much of the processing on my laptop and only send hex codes to the Arduino to light up LEDs.  The difference now is that the Arduino is doing the processing and no computer is needed.</p>
<p>While I was working on this little project, I had the bot join the channel and announce itself.<br />
<img src="http://squarism.com/wp-content/uploads/2010/02/irc_log.png" alt="irc_log" title="irc_log" width="556" height="372" class="aligncenter size-full wp-image-356" /></p>
<p>At one point, I was working on code and then my bot would disconnect.  I checked the serial monitor and the server seemed to drop me after a few minutes.  The channel would say that I timed out.  I realized that I wasn&#8217;t responding to the PING from the server.  So I threw in some code that checks for anything from the server that starts with &#8220;PING :&#8221;.  I then respond with &#8220;PONG&#8221;.  I remember seeing PING?/PONG! messages in mIRC back in the day.  Now it makes sense why mIRC was doing that in the console window.</p>
<p>It works great and I was excited about how much this little board could do in 14KB.  And then I kept testing it.  After about 7 or 8 &#8220;turn on&#8221; and &#8220;turn off&#8221; commands, the Arduino wouldn&#8217;t do anything anymore.  It&#8217;s like it just froze.  If I typed 5 commands, it&#8217;d stay connected for a long time.  But every time I&#8217;d send it 7 to 8 commands, it would lock up.  And by lock up, I mean the commands wouldn&#8217;t work anymore and it would time out from the server.  WTF.  So close!</p>
<p>So I figure that it&#8217;s something to do with pointers and memory.  I really don&#8217;t have a solid grasp on pointers and C.  I got a lot of this working by iterative experimentation over many days.  So I was looking for a better way to send human readable commands to my bot.  By human readable I mean something that works like a unix command &#8220;command arg1 arg2&#8243;.  Of course this human readable bit introduces strings which is tricky enough in C (for me) and even worse on the Arduino.  I figured this was a problem that someone smarter than me had solved.</p>
<p>I found a library called <a href="http://www.arduino.cc/playground/Code/Messenger">Messenger</a>.  It&#8217;s pretty simple to install, just throw it in your ~/Documents/Arduino/libraries folder on Mac and um &#8230; the equivalent on Windows?  There are examples in the Messenger folder that you can checkout.  HOWEVER the whole point of me posting this big long thing is the following.</p>
<p>The example checkString really threw me for a loop.  It did exactly what I need it do to in a much cleaner way.  I uploaded to the Arduino and then broke out to a shell.</p>
<p><code>$ screen /dev/tty.usbserial-A9005bCr 115200</code></p>
<p>Substitute your virtual usb device for the /dev/tty path.  Note that the sketch uses 115k serial speed.  You won&#8217;t see anything when you type but if you hit &#8220;enter&#8221; (to clear the buffer) &#8220;on[enter]&#8221; in screen LED 13 will turn on.  Type &#8220;off[enter]&#8221; ([enter] means the enter key) and it will turn off.  Great!  Exactly what I need.  But then I tried typing &#8220;on&#8221; then &#8220;off&#8221; then &#8220;muffins&#8221; then &#8220;on&#8221; and the light stayed off.  Any garbage gets the Arduino stuck like my sketch.  Ok, is what I&#8217;m trying to do impossible or is this just coincidence?</p>
<p>I modified the checkString example to look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This example demonstrates Messenger's checkString method</span>
<span style="color: #666666; font-style: italic;">// It turns on the LED attached to pin 13 if it receives &quot;on&quot;</span>
<span style="color: #666666; font-style: italic;">// It turns it off if it receives &quot;off&quot;</span>
&nbsp;
&nbsp;
#include <span style="color: #339933;">&lt;</span>Messenger.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Instantiate Messenger object with the message function and the default separator </span>
<span style="color: #666666; font-style: italic;">// (the space character)</span>
Messenger message <span style="color: #339933;">=</span> Messenger<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Define messenger function</span>
<span style="color: #000066; font-weight: bold;">void</span> messageCompleted<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// This loop will echo each element of the message separately</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">checkString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;on&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      digitalWrite<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> message.<span style="color: #006633;">checkString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;off&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      digitalWrite<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>       <span style="color: #666666; font-style: italic;">// ADD THIS</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// ADD THIS</span>
    <span style="color: #009900;">&#125;</span>                <span style="color: #666666; font-style: italic;">// ADD THIS</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Initiate Serial Communication</span>
  Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">115200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  message.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span>messageCompleted<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  pinMode<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">13</span>,OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The following line is the most effective way of </span>
  <span style="color: #666666; font-style: italic;">// feeding the serial data to Messenger</span>
  <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> Serial.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> message.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span> Serial.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I added the break and it&#8217;s able to deal with garbage.  I tested more than 20 commands with banging on the keyboard in between and it seems pretty solid.  Now I just need to integrate this with my IRC bot and I might have something that can stay online for a while.</p>
<p>By the way, after you use screen to connect to a serial port, hit &#8220;Ctrl+A, k&#8221; to kill the window and break out of screen.</p>
<p><strong>Update</strong>: People have asked for the code.  It&#8217;s posted after the break.<br />
<span id="more-354"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// UNSTABLE IRC BOT</span>
<span style="color: #666666; font-style: italic;">// TODO: Integrate with Messenger.h for stability</span>
#include <span style="color: #339933;">&lt;</span>Ethernet2.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
#include <span style="color: #339933;">&lt;</span>WString.<span style="color: #006633;">h</span><span style="color: #339933;">&gt;</span>
&nbsp;
#define STRING_BUFFER_SIZE <span style="color: #cc66cc;">128</span>
<span style="color: #003399;">String</span> buffer <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span>STRING_BUFFER_SIZE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// string variable to hold server name detection</span>
<span style="color: #003399;">String</span> hostString <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">byte</span> mac<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// CHANGE ME</span>
<span style="color: #000066; font-weight: bold;">byte</span> ip<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// arduino IP: 1.2.3.4</span>
<span style="color: #000066; font-weight: bold;">byte</span> gateway<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// your router IP: 1.2.3.4</span>
<span style="color: #000066; font-weight: bold;">byte</span> subnet<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">255</span>, <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// your network mask</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// irc.freenode.net</span>
<span style="color: #000066; font-weight: bold;">byte</span> ircServer<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">74</span>, <span style="color: #cc66cc;">208</span>, <span style="color: #cc66cc;">174</span>, <span style="color: #cc66cc;">239</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// IP of freenode.net</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> responseWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// default: 1000</span>
<span style="color: #000066; font-weight: bold;">int</span> joinWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// default: 1000</span>
<span style="color: #000066; font-weight: bold;">int</span> pingCheckThrottle <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// check for PING event every 10 seconds once connected</span>
<span style="color: #000066; font-weight: bold;">int</span> reconnectWait <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// CHANGE ME</span>
<span style="color: #000066; font-weight: bold;">char</span> ircNick<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DuinoFace&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span> channel<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#freebsToTheDee&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">boolean</span> isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
Client client<span style="color: #009900;">&#40;</span>ircServer, <span style="color: #cc66cc;">6667</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// turn on when debugging</span>
<span style="color: #000066; font-weight: bold;">boolean</span> isDebugOn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// logged in (in channel) status light</span>
<span style="color: #000066; font-weight: bold;">int</span> ledPin <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//router time?</span>
  delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// opens serial port, sets data rate to 9600 bps</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Say hello:</span>
  debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;String Library version: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">version</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  Ethernet.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span>mac, ip, gateway, subnet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;client.connect()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>client.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;client.connect() failed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// status light</span>
  pinMode<span style="color: #009900;">&#40;</span>ledPin, OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>client.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> client.<span style="color: #006633;">connected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// parse the incoming buffer</span>
    readIRCRequest<span style="color: #009900;">&#40;</span>client<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>client.<span style="color: #006633;">connected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Disconnected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay<span style="color: #009900;">&#40;</span>reconnectWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reconnecting...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">int</span> readIRCRequest<span style="color: #009900;">&#40;</span>Client client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">char</span> inChar <span style="color: #339933;">=</span> client.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>inChar <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>inChar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Buffer: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>isLoggedIn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;NOTICE * :*** No Ident response&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        buffer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
      <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:End of /MOTD command.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        join<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
      <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:End of /NAMES list.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        say<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        isLoggedIn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      checkCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
      checkPing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">//delay(pingCheckThrottle);</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// clear out buffer for next line read</span>
    buffer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/* We need to send something that looks like this
 *   NICK yaytofu
 *   USER yaytofu 0 * yaytofu
 * Where yaytofu is our nickname
 */</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> msg <span style="color: #339933;">=</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;NICK &quot;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">//if (debug) Serial.println(msg);</span>
  debug<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* wait for a response */</span>
&nbsp;
  msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;USER &quot;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; 0 * &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  msg.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ircNick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* wait for a response */</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> join<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  delay<span style="color: #009900;">&#40;</span>joinWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JOIN...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>strcat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JOIN &quot;</span>, channel<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  delay<span style="color: #009900;">&#40;</span>responseWait<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> say<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//if (debug) Serial.println(&quot;Sending something to the channel.&quot;);</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sending something to channel.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG #freebsToTheDee :Arduino with Ethernet Shield is here.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> say<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sending something to channel.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG #freebsToTheDee :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> checkPing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PING :&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GOT PING!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// parse and store hostname in string</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hostString.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      hostString <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// respond to ping</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PONG!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PONG &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    client.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>hostString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> checkCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// :nick!~nick@static-1-13-5-1.domain.com PRIVMSG #freebsToTheDee :wut</span>
  <span style="color: #666666; font-style: italic;">// state codes 0=DNS   1=PRIVMSG   2=channel  3=message</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buffer.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> buffer.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PRIVMSG&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">int</span> state <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// split on spaces, TODO: state check</span>
    <span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>bufferArray <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">getChars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">char</span> test <span style="color: #339933;">=</span> bufferArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>test <span style="color: #339933;">==</span> <span style="color: #0000ff;">' '</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;found space at:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        debug<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        spaces<span style="color: #009900;">&#91;</span>state<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
        state<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #003399;">String</span> dnsName <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> privMsg <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> channel <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span>, spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> cmd <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>spaces<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</span>, buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dnsName:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>dnsName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;privMsg:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>privMsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;channel:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>channel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cmd:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    debug<span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setStatusLight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isLoggedIn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Setting status light to ON.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    digitalWrite<span style="color: #009900;">&#40;</span>ledPin, HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    debug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Setting status light to OFF.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    digitalWrite<span style="color: #009900;">&#40;</span>ledPin, LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// No carriage return</span>
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// No carriage return overloaded</span>
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> debugNoLine<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Carriage return</span>
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Carriage return overloaded</span>
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> <span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isDebugOn<span style="color: #009900;">&#41;</span> Serial.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Be sure to change the two sections of CHANGE ME.  Also please note that like I said in my post, I never integrated these two source files together.  So the IRC bot is unstable after about 8 commands.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2010/02/28/arduino-command-protocol/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Parabola</title>
		<link>http://squarism.com/2007/04/15/parabola/</link>
		<comments>http://squarism.com/2007/04/15/parabola/#comments</comments>
		<pubDate>Mon, 16 Apr 2007 00:44:24 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[GameDev]]></category>

		<guid isPermaLink="false">http://squarism.com/2007/04/15/parabola/</guid>
		<description><![CDATA[My goal was to create the curves above. I knew if I could draw it then I could move a box or game object along that path. It took me about two weeks of casual time and many math questions posted to yahoo answers. The problem is, implementing a math formula in C++. It&#8217;s just [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://squarism.com/2007/04/15/parabola/parabola-sketch/"><img src="http://squarism.com/wp-content/uploads/2007/04/parabola_sketch-300x185.jpg" alt="parabola sketch" title="parabola sketch" width="300" height="185" class="alignnone size-medium wp-image-1293" /></a> </p>
<p>My goal was to create the curves above.  I knew if I could draw it then I could move a box or game object along that path.  It took me about two weeks of casual time and many math questions posted to yahoo answers.</p>
<p>The problem is, implementing a math formula in C++.  It&#8217;s just not as pretty as the equation and any algebra tricks are hard to express in code.  Not to mention remembering algebra period.  ?</p>
<p>Eventually, I ended up with this.  I specified the starting point, the ending point and how tall I want the curve.  A series of horrible equations builds the rest into a vector of x,y point structs.  There&#8217;s an LOD thing too that says how pretty the curve should be.</p>
<p><a href="http://squarism.com/2007/04/15/parabola/parabola3/"><img src="http://squarism.com/wp-content/uploads/2007/04/parabola3-300x273.png" alt="parabola3" title="parabola3" width="300" height="273" class="alignnone size-medium wp-image-1294" /></a> </p>
<p>Of course, it&#8217;s pixilated and kinda ugly.  I tried anti-aliasing it but it doesn&#8217;t look much better.  Also, I might have some math issues because in some places there seems to be small humps.  I might be running into precision problems again.  ?</p>
<p>Overall, I&#8217;m pretty exhausted.  I don&#8217;t know what my next project will be, I don&#8217;t know if I want to make more of a game that&#8217;s interactive instead of these little graphics tests.</p>
<p>Also, C++ is pretty ugly imo.  It&#8217;s used everywhere, I understand but I might pick up an actionscript book and see how much of this graphics stuff could be wrapped up in flash.  I bet you can do some cool low-level drawing in flash; and then it&#8217;d be more &#8216;portable&#8217; than an OSX app.</p>
<p>Anyway &#8230; it&#8217;s a thing of goddamn beauty for now.  Except <a href="http://wiki.ubernub.com/parabola">the code</a>, which is ugly, untidy and probably doesn&#8217;t compile by itself.  I&#8217;ll update it so it&#8217;s stand-alone.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2007/04/15/parabola/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>lstat test</title>
		<link>http://squarism.com/2007/04/10/lstat-test/</link>
		<comments>http://squarism.com/2007/04/10/lstat-test/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 22:38:13 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://squarism.com/2007/04/10/lstat-test/</guid>
		<description><![CDATA[Following an interview question that was extremely hard I went to `man lstat&#8217; and tried to code up a test just based on system documentation. It was not entirely successful, however after a tip-off from an online resource I came up with this. File: lstat.c #include #include #include #include int main(int c, char** argv) { [...]]]></description>
			<content:encoded><![CDATA[<p>Following an interview question that was extremely hard I went to `man lstat&#8217; and tried to code up a test just based on system documentation.  It was not entirely successful, however after a tip-off from an online resource I came up with this.</p>
<p><span id="more-76"></span><br />
<em>File: lstat.c</em></p>
<pre>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

int main(int c, char** argv) {
        struct stat buf;
        lstat(argv[1],&#038;buf);
        printf("%s is %d bytes\n", argv[1],buf.st_size);

        return 0;
}
</pre>
<p>When compile like this:<br />
<code>g++ -o lstat lstat.c</code></p>
<p>It looks like this where yep.txt is a dummy file.<br />
<code>user@box ~/c $ ls -l /tmp/yep.txt<br />
ls -l /tmp/yep.txt<br />
-rw-r--r-- 1 user users 17 Apr  9 23:16 /tmp/yep.txt</p>
<p>user@box ~/c $ ./lstat /tmp/yep.txt<br />
/tmp/yep.txt is 17 bytes</code></p>
<p>So all this does is really use stat since yep.txt is a file and not a symbolic link.  This stupid test just prints out the file size which you can see by the real `ls&#8217; above.</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2007/04/10/lstat-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animated Box</title>
		<link>http://squarism.com/2007/04/01/animated-box/</link>
		<comments>http://squarism.com/2007/04/01/animated-box/#comments</comments>
		<pubDate>Sun, 01 Apr 2007 16:11:29 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[GameDev]]></category>

		<guid isPermaLink="false">http://squarism.com/2007/04/01/animated-box/</guid>
		<description><![CDATA[I&#8217;m very, very early on in the effort of this graphics/game test. But essentially I wanted to write down where I&#8217;m at with Xcode, OpenGL and learning C++. Learning C++ by starting with OpenGL is very stupid. I admit this. It&#8217;s the wrong way to start out. It&#8217;s like learning how to walk by jumping [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m very, very early on in the effort of this graphics/game test.  But essentially I wanted to write down where I&#8217;m at with Xcode, OpenGL and learning C++.</p>
<p>Learning C++ by starting with OpenGL is very stupid.  I admit this.  It&#8217;s the wrong way to start out.  It&#8217;s like learning how to walk by jumping out of a Dodge Viper.  But I want to get beyond the Hello World books and after trying for two or three years in my limited spare time, I&#8217;m finding that being thrown into the fire is somewhat motivating.  I learned Java the same way (not that I&#8217;m a master of that either), I gave myself a goal that I really wanted to accomplish and the rest just fell into place because I couldn&#8217;t think about anything else.</p>
<p>Such is my animated box.  I want to move a box in a really smart way.  Not just some Box.setX(i++); Box.setY(j++) in the main() method but something smarter that would enable me to move two, four or one-thousand boxes in the future.</p>
<p><a href="http://squarism.com/2007/04/01/animated-box/animated-box-2/"><img src="http://squarism.com/wp-content/uploads/2007/04/animated_box.gif" alt="animated box" title="animated box" width="250" height="272" class="alignnone size-medium wp-image-1288" /></a> </p>
<p>More coming&#8230;</p>
<p><a href="http://squarism.com/2007/04/01/animated-box/xcode-glclearcolor/"><img src="http://squarism.com/wp-content/uploads/2007/04/xcode_glclearcolor-224x300.png" alt="xcode glclearcolor" title="xcode glclearcolor" width="224" height="300" class="alignnone size-medium wp-image-1289" /></a> </p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2007/04/01/animated-box/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Chapter 4</title>
		<link>http://squarism.com/2007/01/21/chapter-4/</link>
		<comments>http://squarism.com/2007/01/21/chapter-4/#comments</comments>
		<pubDate>Sun, 21 Jan 2007 22:08:33 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/2007/01/21/chapter-4/</guid>
		<description><![CDATA[Going through the Oreilly book Practical C++ Programming, which may be an arguable title, and I&#8217;m just trying to go through all of the examples and save them somewhere so I know where I left off the next time I&#8217;m feeling recklessly productive. Specifically, Chapter 4 asks Exercise 4-2: Write a program to print a [...]]]></description>
			<content:encoded><![CDATA[<p>Going through the Oreilly book <em>Practical C++ Programming</em>, which may be an arguable title, and I&#8217;m just trying to go through all of the examples and save them somewhere so I know where I left off the next time I&#8217;m feeling recklessly productive.</p>
<p>Specifically, Chapter 4 asks</p>
<blockquote><p>
<strong>Exercise 4-2:</strong> Write a program to print a block E using asterisks (*), where the E is 7 characters high and 5 characters wide.
</p></blockquote>
<p>Of course the easy way would be to just print out a bunch of strings.  But I consider that cheap.  So I played around with struct and typedefs but I don&#8217;t know what the hell I&#8217;m doing with those so I instead forced myself to learn about passing by value vs. passing by reference.</p>
<p>First, the function makeE() creates an integer array which in my world of goals could be a game model, or x,y,z points for some game object.  Then printE() prints out the array, but only an array of 7.  If I wanted to do this whole thing much better I&#8217;d use a vector.</p>
<p>It&#8217;s rough shifting gears back to C++.  I don&#8217;t know if I&#8217;ll ever get really good with it and be able to get to the fun stuff.  You can see below that another version just printed out way too many *&#8217;s.</p>
<p><a href="http://squarism.com/2007/01/21/chapter-4/e-messedup/"><img src="http://squarism.com/wp-content/uploads/2007/01/e_messedup-300x102.png" alt="e messedup" title="e messedup" width="300" height="102" class="alignnone size-medium wp-image-1286" /></a> </p>
<pre>
/*
 *  4-2.cpp
 *  Practical C++ Exercises
 *
 *  Created by Chris on 1/21/07.
 *  Copyright 2007 hexameter.com. All rights reserved.
 *
 */
// write a program to print a block E using *, where e is 7 high and 5 wide.
#include &lt;iostream&gt;

void makeE(int int_array[])
{
	int_array[0]=5;
	int_array[1]=1;
	int_array[2]=1;
	int_array[3]=5;
	int_array[4]=1;
	int_array[5]=1;
	int_array[6]=5;
}

void printE(int e[])
{

	for (int i = 0; i < 7 ; i++)
	{
		for (int j=0; j<e[i]; j++)
		{
			std::cout << "*";
		}
		std::cout << "\n";
	}
}

int main()
{
	int e_array[7];

	makeE(e_array);
	printE(e_array);
	return(0);
}
</pre>
<p>But then finally, it worked suddenly after I made some adjustments.</p>
<p><a href="http://squarism.com/2007/01/21/chapter-4/e-success/"><img src="http://squarism.com/wp-content/uploads/2007/01/e_success-300x92.png" alt="e success" title="e success" width="300" height="92" class="alignnone size-medium wp-image-1287" /></a> </p>
<p>This is all just beginner stuff.  :(</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2007/01/21/chapter-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>distcc is cool.</title>
		<link>http://squarism.com/2004/11/18/distcc-is-cool/</link>
		<comments>http://squarism.com/2004/11/18/distcc-is-cool/#comments</comments>
		<pubDate>Thu, 18 Nov 2004 22:11:17 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/2004/11/18/distcc-is-cool/</guid>
		<description><![CDATA[Distcc is a distributed C/C++ compiler. You can easily use a bunch of machines&#8217; power to compile stuff. The setup isn&#8217;t bad at all. It&#8217;s just a non-root network service. Gentoo.org has some nice instructions on setting up distcc for builds. Here, we have 2x Pentium 3 machines and 1x dual xeon helping me build [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://distcc.samba.org/">Distcc</a> is a distributed C/C++ compiler.  You can easily use a bunch of machines&#8217; power to compile stuff.  The setup isn&#8217;t bad at all.  It&#8217;s just a non-root network service.</p>
<p>Gentoo.org has some <a href="http://www.gentoo.org/doc/en/distcc.xml">nice instructions</a> on setting up distcc for builds.  Here, we have 2x Pentium 3 machines and 1x dual xeon helping me build <a href="http://www.nethack.org/">nethack</a>.  Don&#8217;t have numbers on how much faster it was but it seemed faster.  Most makes/compiles are annoyingly slow, this seemed ok.</p>
<p><a href="http://squarism.com/archives/distcc_full.html"><img alt="distcc_thumb.jpg" src="http://squarism.com/wp-content/photos/distcc_thumb.jpg" width="239" height="267" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2004/11/18/distcc-is-cool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some OpenGL progress</title>
		<link>http://squarism.com/2004/11/08/some-opengl-progress/</link>
		<comments>http://squarism.com/2004/11/08/some-opengl-progress/#comments</comments>
		<pubDate>Mon, 08 Nov 2004 16:24:42 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/2004/11/08/some-opengl-progress/</guid>
		<description><![CDATA[I finally got the FTGL library built. It was quite annoying to do. In the end, I had a libfreetype.a file (looks like an assembly binary) and a libftgl.a file. I added those to the XCode project. Then I double clicked on the target icon to pull up a properties pane where I could add [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got the FTGL library built.  It was quite annoying to do.  In the end, I had a libfreetype.a file (looks like an assembly binary) and a libftgl.a file.  I added those to the XCode project.  Then I double clicked on the target icon to pull up a properties pane where I could add the Freetype2 headers I installed from source.</p>
<p>I made freetype2&#8242;s prefix path /sw/freetype2 with `configure &#8211;prefix=/sw/freetype2&#8242;.  With that path, I included /sw/freetype2 and /sw/freetype2/include in the XCode project.</p>
<p>I&#8217;ll post screenshots of this whole process because I couldn&#8217;t find jack on the Internet about this.</p>
<p>After futzing around with some FTGL demos that didn&#8217;t work I finally modified an example to the point that it loaded a true type font and displayed the string &#8220;hi&#8221; on the screen.  I was very happy.  I was then able to move the texture and change the alpha channel to make it fade away.  What I&#8217;m looking for is a little popup of arbitrary text to appear over someone&#8217;s head when they get hit to represent damage.</p>
<p>Right now, I&#8217;m stuck trying to figure out how to trigger this whole drawing thing off of a keystroke instead of just being called with by the main function.  I&#8217;ll post the entire example with pics soon.  [placeholder]</p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2004/11/08/some-opengl-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RPG project</title>
		<link>http://squarism.com/2004/09/28/rpg-project/</link>
		<comments>http://squarism.com/2004/09/28/rpg-project/#comments</comments>
		<pubDate>Wed, 29 Sep 2004 02:48:48 +0000</pubDate>
		<dc:creator>Dillon</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://squarism.com/2004/09/28/rpg-project/</guid>
		<description><![CDATA[A few ideas detailing a first bold attempt at game programming. Of note, the game will try to be of a Final Fantasy 1 feel while having attributes and statistics similar to Final Fantasy 7 and 11. Features and goals include: - Some sort of world map - Possibly a shader to make the world [...]]]></description>
			<content:encoded><![CDATA[<p>A few ideas detailing a first bold attempt at game programming.</p>
<p>Of note, the game will try to be of a Final Fantasy 1 feel while having attributes and statistics similar to Final Fantasy 7 and 11.  Features and goals include:</p>
<p>- Some sort of world map<br />
- Possibly a shader to make the world look like a snow globe (see Doom3&#8242;s glass post-processing effect)<br />
- Special effects based on hits / actions<br />
- Eventually some sort of story and plot</p>
<p>These are on the upper end of the TODO list.  Read on for more immediate tasks.<br />
<span id="more-44"></span><br />
Java has been fun to learn but it is limited in the ways of graphics.  I&#8217;ve read that there are OpenGL bindings for Java but it doesn&#8217;t quite seem right.  Java is interpreted.  All the 3d books published implement their concepts in C/C++.  Learning a new language isn&#8217;t exactly what I call easy.  Certainly graphics programming is an advanced topic.  So for the short-term, I&#8217;m going to try to keep my goals and features realistic:</p>
<p>- Get a basic encounter working: 1) party runs into a &#8216;bad guy&#8217;  2) players hit &#8216;bad guy&#8217; and HP gets subtracted<br />
- Write a character class/object then write a series of actions to act upon that object<br />
- Implement basic game rule formulas (like evasion calculations, turn order and random damage)<br />
- Get something at least somewhat playable working in text mode<br />
- After a semblance of a framework and design is becoming clear, try to get a UI or a graphical menu working</p>
<p>I would like to start this project in XCode.  Objective-c is completely new to me and it seems different than even C/C++ (I&#8217;m not very good in any of these).  The going will be slow and the results will likely not even be fun to play.  It&#8217;s just a better project than the examples in my <a href="http://www.oreilly.com/catalog/pcp3/">intro to C books.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://squarism.com/2004/09/28/rpg-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

