<?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>Bret Kuhns &#187; Programming</title>
	<atom:link href="http://www.bretkuhns.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bretkuhns.com/blog</link>
	<description>Programming, Cars, and Life</description>
	<lastBuildDate>Sun, 19 Dec 2010 19:05:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CakePHP JSON Response Data in Headers</title>
		<link>http://www.bretkuhns.com/blog/2010/12/cakephp-json-response-data-in-headers/</link>
		<comments>http://www.bretkuhns.com/blog/2010/12/cakephp-json-response-data-in-headers/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 15:39:28 +0000</pubDate>
		<dc:creator>Bret Kuhns</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.bretkuhns.com/blog/?p=170</guid>
		<description><![CDATA[I recently utilized CakePHP&#8217;s ability to automatically adjust view parameters based on the requested extension in the URI. This makes it really easy to specify a JSON formatted response by just tacking &#8220;.json&#8221; to the end of the URI. When I discovered this handy little feature of Cake&#8217;s, I was praising the framework for such [...]]]></description>
			<content:encoded><![CDATA[<p>I recently utilized <a href="http://www.cakephp.org/">CakePHP</a>&#8217;s ability to automatically adjust view parameters based on the requested extension in the URI. This makes it really easy to specify a <a href="http://www.json.org/">JSON</a> formatted response by just tacking &#8220;.json&#8221; to the end of the URI. When I discovered this handy little feature of Cake&#8217;s, I was praising the framework for such a quick and useful feature. Until things went wrong, of course. </p>
<p>I do most of my development in Chromium, but noticed when I was testing my code for the new JSON responses, data wasn&#8217;t showing up properly in Firefox. I didn&#8217;t think much of it and figured I would return to the issue once I could focus on browser compatibility. I eventually, however, found myself at that point in development. I discovered that returning only a sliver of the normal response data lead Firefox to respond properly. I also found Chromium was hitting it&#8217;s own, significantly higher, limit. Firefox was capping the JSON response at 4096 bytes, whereas Chromium was somewhere in the area of 305kB. The Chromium limit was practically reasonable for my application, but there was no way I could dodge Firefox&#8217;s measly 4kB cap. But wait, there&#8217;s no way Firefox limits <i>all</i> AJAX responses to 4kB, right? Gmail can&#8217;t possibly operate on an army of tiny 4kB responses, so what am I doing wrong?</p>
<p>Courtesy of Firebug, I compared responses and noticed something peculiar, my server&#8217;s JSON responses contained the entire data contents in the headers of the response itself. After some Googling, it seems response header fields have a size limit. In Firefox that limit is, you guessed it, 4096 bytes. As I found, CakePHP&#8217;s automatic handling of JSON extensions takes the view variables and places them directly in the headers. Not only that, but my JSON views were returning the json_encode()&#8217;ed result of my data, so the response contained the same data in two places. Without much time available for finding a proper way to make Cake stop adding data in the headers, I decided to go back and do it the ol&#8217; fashioned way, by myself. I got rid of Cake&#8217;s router line for Router::parseExtensions(&#8216;json&#8217;); left my controller action alone, and changed my view to:</p>
<pre class="brush: php;">&lt;?php
$this-&gt;layout = 'ajax';
Configure::write('debug', 0);
header('conten-type:text/x-json');
header('cache-control:no-store,no-cache,max-age=0,must-revalidate');
echo json_encode($results);
?&gt;</pre>
<p>And with that, both Firefox and Chromium now accept responses greater than 4kB and 305kB respectively. I&#8217;m still sprinting to finish this project, so I haven&#8217;t had time to investigate if the core Cake team knows about this issue. I plan to revisit after project launch and see if I can learn more about this strange behavior.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bretkuhns.com/blog/2010/12/cakephp-json-response-data-in-headers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Ternary Reference Assignment</title>
		<link>http://www.bretkuhns.com/blog/2010/11/php-ternary-reference-assignment/</link>
		<comments>http://www.bretkuhns.com/blog/2010/11/php-ternary-reference-assignment/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 13:26:53 +0000</pubDate>
		<dc:creator>Bret Kuhns</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.bretkuhns.com/blog/?p=144</guid>
		<description><![CDATA[I&#8217;m a fan of CakePHP, but some times it can do some goofy things. One such example is the afterFind() Model callback, whose $results parameter may contain data in two different formats depending on how the find results were retrieved elsewhere in the code. This breaks the blackbox concept of functions in which the function [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a fan of <a href="http://www.cakephp.org/">CakePHP</a>, but some times it can do some goofy things. One such example is the <a href="http://book.cakephp.org/view/1050/afterFind">afterFind()</a> Model callback, whose $results parameter may contain data in two different formats depending on how the find results were retrieved elsewhere in the code. This breaks the blackbox concept of functions in which the function shouldn&#8217;t care less of what happens outside it&#8217;s scope. Because of this oddity, you have to support either the model data existing in the $results array under a key named after the model, or the data existing directly in the $results array as simple key/value pairs. I attempted to make this easy to deal with by doing the following:</p>
<pre class="brush: php;">$res = ($primary)
	? &amp;$result['MyModel']
	: &amp;$result;</pre>
<p>But for whatever reason, PHP fails with an &#8220;unexpected &#038;&#8221; error after the ternary `?` operator. The ternary syntax is just a shorthand I use for a really easy if-then, so I decided to expand it and see what happens.</p>
<pre class="brush: php;">if($primary) {
	$res = &amp;$result['MyModel'];
} else {
	$res = &amp;$result;
}</pre>
<p>This code runs without a hitch. So then what&#8217;s happening? Your guess is as good as mine. I honestly can&#8217;t discern what&#8217;s making PHP break on the ternary version. Although the ternary syntax is rather simple and can be considered synonymous to the if-then code, PHP seems to handle them differently and consequently breaks reference assignment when using ternary.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bretkuhns.com/blog/2010/11/php-ternary-reference-assignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flashing OpenLog Firmware in Ubuntu Linux</title>
		<link>http://www.bretkuhns.com/blog/2010/03/flashing-openlog-firmware-in-ubuntu-linux/</link>
		<comments>http://www.bretkuhns.com/blog/2010/03/flashing-openlog-firmware-in-ubuntu-linux/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 18:36:37 +0000</pubDate>
		<dc:creator>Bret Kuhns</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[OpenLog]]></category>
		<category><![CDATA[sparkfun]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.bretkuhns.com/blog/?p=85</guid>
		<description><![CDATA[I recently hit a bug in the Sparkfun OpenLog v1.1 firmware that left the device useless. I found out the hard way that version 1.1 only supports up to 255 log files. Once it hits this limit the firmware doesn&#8217;t know what to do with itself and loops endlessly. This even prevents you from entering command [...]]]></description>
			<content:encoded><![CDATA[<p>I recently hit a bug in the Sparkfun <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=9530">OpenLog </a>v1.1 firmware that left the device useless. I found out the hard way that version 1.1 only supports up to 255 log files. Once it hits this limit the firmware doesn&#8217;t know what to do with itself and loops endlessly. This even prevents you from entering command mode where you could otherwise reset the log number. <a href="http://github.com/nseidle">Nate Seidle</a> at SparkFun quickly released an update, v1.2, to correct this problem I was having. But now I had to figure out how to flash the firmware on my OpenLog. It turns out the process is extremely easy in Ubuntu, but the GitHub documentation targets mostly Windows, so I decided to document the process for Ubuntu users from start to finish here.<span id="more-85"></span></p>
<p>I expect that you have the Arduino IDE <a href="http://www.arduino.cc/playground/Learning/Linux">installed and running properly</a>. This means the avrdude and avr-gcc libraries necessary to flash OpenLog will already be installed. Next step is to pull the firmware source code from the <a href="http://github.com/nseidle/OpenLog">GitHub repository</a>. If you don&#8217;t have git installed, run the following command:</p>
<pre class="brush: plain; gutter: false;">$ sudo apt-get install git-core</pre>
<p>Now you can run the git command to pull the source code over the read-only HTTP path. This is generally the easiest path to use to avoid issues with proxies or closed/blocked ports on your network.</p>
<pre class="brush: plain; gutter: false;">$ git clone http://github.com/nseidle/OpenLog.git</pre>
<p>Once the command has completed, you&#8217;ll have an OpenLog directory in your currently active directory. Time to compile the source code to make the required hex file that we&#8217;ll write to OpenLog.</p>
<pre class="brush: plain; gutter: false;">$ cd OpenLog/Code/</pre>
<pre class="brush: plain; gutter: false;">$ make</pre>
<p style="text-align: left;">You should see several notices about compiling various .c files, linking main.elf, and most importantly loading main.hex. Now we need to get that hex file onto OpenLog. Grab your trusty <a href="http://www.sparkfun.com/commerce/product_info.php?products_id=8772">FTDI breakout board</a> (you should never leave home without it) and wire it to OpenLog as shown below.</p>
<p style="text-align: left; padding-top: 0;"><a href="http://www.bretkuhns.com/blog/wp-content/uploads/2010/03/FTDI-Connections.jpg"><img class="aligncenter size-medium wp-image-86" title="FTDI to OpenLog connection diagram." src="http://www.bretkuhns.com/blog/wp-content/uploads/2010/03/FTDI-Connections-300x96.jpg" alt="" width="300" height="96" /></a></p>
<p style="text-align: left; padding-top: 0;">Before connecting the FTDI to your computer, we want to make sure we have the correct device path for the next step. Look at the list of all tty-capable USB devices. It&#8217;s okay if none show up right now.</p>
<p style="text-align: left; padding-top: 0;">
<pre class="brush: plain; gutter: false;">$ ls /dev/ttyUSB*</pre>
<p style="text-align: left; padding-top: 0;">Now connect the FTDI board and run the command again. The new ttyUSB# device listed is your FTDI board. I&#8217;ll use ttyUSB0 in the command below, so you will need to change the zero if your # is different.</p>
<p style="text-align: left; padding-top: 0;">I hope you have an extra wire at hand. The Linux build of avrdude unfortunately doesn&#8217;t have the &#8220;DTR wiggle&#8221; necessary to automatically reset OpenLog&#8217;s ATMega328p, so we have to do it ourselves. Grab a wire and connect one end to ground. If necessary, the microSD socket&#8217;s case can be used as a ground if you don&#8217;t have OpenLog on a breadboard. You&#8217;ll need to tap the other end of the wire onto the GPIO pin 6 on OpenLog. This is the square pin pad near the &#8220;O&#8221; in &#8220;OpenLog&#8221; on the silkscreen (the bottom-left most pin in the image featured above). Timing here is of the essence; run the command below, hit enter on your keyboard, then immediately tap the other end of your grounded wire to pin 6. Don&#8217;t leave the wire sitting on pin 6, it&#8217;s just a quick tap to momentarily ground it and force a reset.</p>
<p style="text-align: left; padding-top: 0;">
<div id="_mcePaste">
<pre class="brush: plain; gutter: false;">$ avrdude -p atmega328p -P /dev/ttyUSB0 -c stk500v1 -b 57600 -U flash:w:main.hex</pre>
</div>
<p>Don&#8217;t forget to change the ttyUSB# number if necessary. If your timing was good enough, you should see avrdude show a progress bar as it flashes the hex firmware onto OpenLog. If not, you&#8217;ll likely see errors like &#8220;programmer not responding&#8221; or &#8220;out of sync&#8221;. Try try again. Tapping enter and grounding pin 6 should be almost instantaneous, with the enter key coming ever so slightly first.</p>
<p>After a few seconds, the updated firmware will now be on your OpenLog chip and you&#8217;ll be on your merry way. I hope this helps someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bretkuhns.com/blog/2010/03/flashing-openlog-firmware-in-ubuntu-linux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Recursive Strings</title>
		<link>http://www.bretkuhns.com/blog/2009/11/php-recursive-strings/</link>
		<comments>http://www.bretkuhns.com/blog/2009/11/php-recursive-strings/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 00:58:29 +0000</pubDate>
		<dc:creator>Bret Kuhns</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.bretkuhns.com/blog/?p=1</guid>
		<description><![CDATA[I needed to construct a recursive string of regular expression patterns. Parts of a string may be built from another string which may also be built of other strings. If you&#8217;re familiar, think of production rules in a context-free grammar. If you&#8217;re not, then just take for granted this recursive string idea would be useful. [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to construct a recursive string of regular expression patterns. Parts of a string may be built from another string which may also be built of other strings. If you&#8217;re familiar, think of <a href="http://en.wikipedia.org/wiki/Production_%28computer_science%29">production rules</a> in a <a href="http://en.wikipedia.org/wiki/Context-free_grammar">context-free grammar</a>. If you&#8217;re not, then just take for granted this recursive string idea would be useful. I initially decided to build all my patterns into a single array, giving me a nice data collection to access them. I was also under the assumption that my recursive idea would work best with an array as the interpreter would have to account for all strings in the array at once. This would prevent order issues if one string relied on another but wasn&#8217;t yet declared. Unfortunately, PHP simply doesn&#8217;t seem to work as I had hoped.<span id="more-1"></span></p>
<p>Consider the following snippet of code:</p>
<pre class="brush: php;">&lt;?php
$a = array(
     0 =&gt; &quot;test {$a[1]}&quot;,
     1 =&gt; &quot;blah {$a[2]}&quot;,
     2 =&gt; &quot;argh&quot;
);
echo $a[0].&quot;\n&quot;;	// outputs: test

$s2 = &quot;argh&quot;;
$s1 = &quot;blah {$s2}&quot;;
$s0 = &quot;test {$s1}&quot;;

echo $s0.&quot;\n&quot;;	// outputs: test blah argh
?&gt;</pre>
<p>At first, we attempt to use an array to handle the string recursion, then we use string variables.We can see from the output that the array code simply doesn&#8217;t work. It appears you can&#8217;t access parts of the array from within itself. At the moment, it looks like I&#8217;m stuck avoiding arrays for this. If anyone out there knows a way around this, please let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bretkuhns.com/blog/2009/11/php-recursive-strings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

