<?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>omino code blog &#187; code</title>
	<atom:link href="http://omino.com/blog/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://omino.com/blog</link>
	<description>We need code. Lots of code.</description>
	<lastBuildDate>Tue, 06 Jan 2009 15:55:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>simple browser tricks</title>
		<link>http://omino.com/blog/2008/simple-browser-tricks/</link>
		<comments>http://omino.com/blog/2008/simple-browser-tricks/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 01:45:54 +0000</pubDate>
		<dc:creator>David Van Brink</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://omino.com/blog/?p=107</guid>
		<description><![CDATA[// Support function for saShowCode wordpress plugin function showCodeShowHide(id,shown) { var outer = document.getElementById("outer_" + id); if(shown == null) { shown = outer.shown; if(shown == null) shown = true; shown = !shown; } outer.shown = shown; var outerStyle = outer.style; var iframe = document.getElementById("iframe_" + id); var iframeStyle = iframe.style; if(shown) { iframeStyle.visibility = "visible"; [...]]]></description>
			<content:encoded><![CDATA[<p>In a detour to a detour to some longer term goal, I needed to do some minimal HTML/JavaScript coding. The regularly scheduled hardware and WBL tinkering &#8212; including soldering, LEDs, and wireless sensors &#8212; will resume shortly.</p>
<p><span id="more-107"></span></p>
<p>Specifically, in WordPress, I wanted to hide &lt;embed> tags behind a &#8220;click-to-show&#8221; link, to reduce browser-clobbering in some of my heavier posts.</p>
<p>It turns out that such manipulations are easy and fun!</p>
<h3>getElementById &#038; innerHTML</h3>
<p>Modern HTML lets you add an <b>onclick=&#8221;<i>some JavaScript code</i>&#8220;</b> to any element. That JavaScript code can then modify another element. Here&#8217;s a bit of HTML:</p>
<pre>
&lt;span
onclick='document.getElementById("sbt_target1").innerHTML="Ah, you clicked the Span.";'
style='cursor:pointer' >
   Click this Span
&lt;/span>

&lt;a onclick='document.getElementById("sbt_target1").innerHTML="You clicked the Anchor!";'>
   Click this Anchor
&lt;/a>

&lt;span id="sbt_target1">... this text will be changed ...&lt;/span>
</pre>
<p>And here&#8217;s the code, live. Try it out.</p>
<blockquote style="border : 1px gray solid ; margin:3px ; padding:3px"><p>
<span onclick='document.getElementById("sbt_target1").innerHTML="Ah, you clicked the Span.";' style='cursor:pointer' ><br />
   Click this Span<br />
</span></p>
<p><a onclick='document.getElementById("sbt_target1").innerHTML="The Anchor got clicked!";'><br />
   Click this Anchor<br />
</a><br />
<span id="sbt_target1">&#8230; this text will be changed &#8230;</span>
</p></blockquote>
<h3>A Self-Modifying Element</h3>
<p>The next obvious silly trick is to have an element set its parent&#8217;s inner HTML, obliterating itself.</p>
<pre>
&lt;span id="sbt_target2">&lt;a onclick='document.getElementById("sbt_target2").innerHTML="Gone!";'>
  Click me and I'll disappear!
&lt;/a>&lt;/span>
</pre>
<p>And&#8230; here it is, self-destroying code, live. Single-use only.</p>
<blockquote style="border : 1px gray solid ; margin:3px ; padding:3px"><p>
<span id="sbt_target2"><a onclick='document.getElementById("sbt_target2").innerHTML="&lt;h1&gt;Gone!&lt;/h1&gt;";'>Click me and I&#8217;ll disappear!</a></span>
</p></blockquote>
<h3>WordPress Plugin</h3>
<p>So, sure, I know there&#8217;s one or two of these floating around out there, but I&#8217;m addicted to rolling my own. A WordPress plugin has the opportunity to filter post&#8217;s contents. To put my QuickTime movies behind a &#8220;show-me click&#8221;, my plugin looks for <b>&lt;embed></b> elements and replaces them with a self-modifying span, like the second example above.</p>
<p>It uses a global variable to count invocations, to generate unique element ID&#8217;s in case there&#8217;s more than one embed. Here it is in action (with a spoiler from the next post, about wireless doohickies):</p>
<p><span id="embed_4_1">
    <div
       onclick='document.getElementById("embed_4_1").innerHTML = "<embed src=\"http://omino.com/blog/content/2008/wblXBee/theLedBlinks.mov\" controller=\"true\" width=\"480\" height=\"376\" autoplay=\"true\" />";'
       style="
        color : white ;
        border : 4px #888 solid ;
        margin : 4px ;
        background : #ddd ;
        background-image : url('http://omino.com/blog/wp-content/plugins/tvStatic128Dark.gif') ;
        cursor:pointer ;
        height:360px ; width:464px ; ">
        <div style="word-wrap : break-word ; font-size:130% ; position : relative ; top : 35% ; text-align : center ; vertical-align : middle">
            <b>theLedBlinks.mov</b>
            <br /><br />
            <font size="-1" color="#999">click for<br />http://omino.com/blog/content/2008/wblXBee/theLedBlinks.mov</font>
        </div>
    </div>
</span></p>
<p>(I confess, I&#8217;m particularly proud of the tv-snow animated GIF, made in After Effects.)</p>
<p>Lastly, here&#8217;s the plugin code, for amusement purposes only.</p>
<p>    <div
        id="outer_showcode_3_1"
        style="border:1px grey dashed ;
            margin:2px ; padding:2px ;
            background-color:#ffffe0 ;
            position:relative ;
            height:340px ; width:800px" >
    <div style=" width:100% ; height:20px ; position:relative " >
        <a href="/src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php" target="_blank"><img src="/img/src_window.gif" alt="/src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php in new window"></a>
        <a href="/src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php.dl"><img src="/img/src_download.gif" alt="download /src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php to file"></a>
        <a href='javascript:showCodeShowHide("showcode_3_1");'><img src="/img/src_showhide.png" alt="hide /src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php"></a>
    </div>
    <iframe src="/src/saInfrastructure/wpOverlay/wp-content/plugins/saEmbed_wp.php"
        id="iframe_showcode_3_1"
        frameborder="0"
        style="position:relative ; height:320px ; width:100%" >
    </iframe>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://omino.com/blog/2008/simple-browser-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://omino.com/blog/content/2008/wblXBee/theLedBlinks.mov" length="3877129" type="video/quicktime" />
		</item>
		<item>
		<title>Code Velocity Profile</title>
		<link>http://omino.com/blog/2008/code-velocity-profile/</link>
		<comments>http://omino.com/blog/2008/code-velocity-profile/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 16:17:42 +0000</pubDate>
		<dc:creator>David Van Brink</dc:creator>
				<category><![CDATA[broad generalities]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://omino.com/blog/?p=96</guid>
		<description><![CDATA[A body of code is never finished The authors have in mind some &#8220;next steps&#8221; The code implicitly supports these particular next steps, and similar ones The code implicitly hinders wildly different next steps Adoption by users implicitly supports the current state and, depending on apparent paradigm, supports the intended next steps Adoption by users [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>A body of code is never finished</li>
<li>The authors have in mind some &#8220;next steps&#8221;</li>
<li>The code implicitly supports these particular next steps, and similar ones</li>
<li>The code implicitly hinders wildly different next steps</li>
<li>Adoption by users implicitly supports the current state and, depending on apparent paradigm, supports the intended next steps</li>
<li>Adoption by users resulting in persistent manifestations of the current state &#8212; that is, saved documents &#8212; explicitly hinders wildly different next steps</li>
<li>Intermittent development may distract from those next steps, but they are still implied by the code (and the current state is required by extant persistent documents)</li>
<li>Development by new authors may distract from those next step, but they are still implied by the code (and the current state is required by extant persistent documents)</li>
<li>If you accelerate for six months towards the wrong star, it takes 18 months to turn your spaceship around, decelerate, and return to your starting point at a stop. Less if you don&#8217;t mind overshooting.
</ol>
<p>Seems about right.</p>
<p>Compelling charts and graphs omitted.</p>
]]></content:encoded>
			<wfw:commentRss>http://omino.com/blog/2008/code-velocity-profile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Gotcha</title>
		<link>http://omino.com/blog/2008/java-gotcha/</link>
		<comments>http://omino.com/blog/2008/java-gotcha/#comments</comments>
		<pubDate>Tue, 27 May 2008 23:46:40 +0000</pubDate>
		<dc:creator>David Van Brink</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://omino.com/blog/?p=94</guid>
		<description><![CDATA[I was working on some code, and finding that it broke a bunch of unit tests. By the advanced method of &#8220;commenting out the new stuff&#8221;, I found that the line in red below appeared to be causing the problem. If I commented out the line in red, everything worked! Put it back in, it [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on some code, and finding that it broke a bunch of unit tests. By the advanced method of &#8220;commenting out the new stuff&#8221;, I found that the line in red below appeared to be causing the problem.</p>
<p>If I commented out the line in red, everything worked! Put it back in, it all breaks!</p>
<pre>
for(IDocumentUrl doc : docs)
{
<span style="background:#ff6666">	@SuppressWarnings("unused") AltNode docUrlNode = new AltNode(DOCUMENT_URL);</span>
//	docUrlNode.setValue(DISPLAY_NAME,doc.getTitle());
//	docUrlNode.setValue(TYPE,doc.getType().name());
//	docUrlNode.setValue(URL,doc.getUrl());
//	cardNode.addChild(docUrlNode);
}
</pre>
<p>What&#8217;s the deal?</p>
<p>The line in red is just allocating a harmless object. In fact&#8230; I replaced it with:</p>
<pre>
for(IDocumentUrl doc : docs)
{
<span style="background:#ff6666">	@SuppressWarnings("unused") Integer x = 3;</span>
}
</pre>
<p>With the same symptoms. Comment out the line in red, and all is well.</p>
<p>Ah. Java <em>does</em> do some optimization! Removing the contents of the iterator loop allowed Java to omit the entire loop. Turns out <strong>docs</strong> was null; removing the contents of the iterator loop avoided an exception.</p>
<p>All fixed:</p>
<pre>
if(docs != null)
  for(IDocumentUrl doc : docs)
  {
    @SuppressWarnings("unused") AltNode docUrlNode = new AltNode(DOCUMENT_URL);
    docUrlNode.setValue(DISPLAY_NAME,doc.getTitle());
    docUrlNode.setValue(TYPE,doc.getType().name());
    docUrlNode.setValue(URL,doc.getUrl());
    cardNode.addChild(docUrlNode);
  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://omino.com/blog/2008/java-gotcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Retro USB MIDI Device VI</title>
		<link>http://omino.com/blog/2008/retro-usb-midi-device-vi/</link>
		<comments>http://omino.com/blog/2008/retro-usb-midi-device-vi/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 03:54:09 +0000</pubDate>
		<dc:creator>David Van Brink</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[wbl weird blinking lights]]></category>

		<guid isPermaLink="false">http://omino.com/blog/2008/retro-usb-midi-device-vi/</guid>
		<description><![CDATA[First Working Code After much tinkering and poring over data dumps and specs, I have flashed the PICDEM FS USB board such that, when plugged into the Mac, it shows up as a usable MIDI input. (Doesn&#8217;t yet show up properly on Windows XP, though.) This, using a Pic 18F4550. From reading and googling the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>First Working Code</strong></p>
<p>After much tinkering and poring over data dumps and specs, I have flashed the PICDEM FS USB board such that, when plugged into the Mac, it shows up as a usable MIDI input. (Doesn&#8217;t yet show up properly on Windows XP, though.) This, using a Pic 18F4550.</p>
<p>From reading and googling the subject, it seems that most USB developers start by creating the descriptors, and committing them to ROM.</p>
<p>I took a different approach. I wanted to have a flexible and repeatable way to create new MIDI devices as needed. Who knows, I might do more than one. And this way others might benefit from it too, potentially. As part of <code>main</code>, I call a setup method which <em>dynamically</em> creates the all the descriptors, strings, &#038; so on. I donated 256 bytes of RAM (out of 2k) to hold the descriptors. And the code to poke it all in chews up a K or so of program space. But, for now at least, the freewheeling flexibility is worth it.</p>
<pre>
    UsbMidiSetup(0x0832,0x401c,5,5,
        "omino.com products",
        "omino 8038 controller"
        );
</pre>
<p>The above C call creates in memory the device and configuration descriptors for a Streaming Audio (MIDI) USB device with Vendor ID 0832, Product ID 401C, five MIDI input cables, five MIDI output cables, for a company named &#8220;omino.com products&#8221;. It shows up in the MIDI patchbay as an &#8220;omino 8038 controller&#8221;.</p>
<p><a href="http://omino.com/blog/content/2008/usb/showsUp1.png"><img src="http://omino.com/blog/content/2008/usb/showsUp1.png" width="360" /></a></p>
<p>Below the cut, some trivia about the Pic 18F4550, the PICDEM FS USB demo board, and their development tools.</p>
<p><span id="more-84"></span></p>
<p><strong>Notes</strong></p>
<p><strong>MCC18</strong>. Microchip&#8217;s C compiler, MCC18, has a few quirks and extensions. Data may be in program memory (ROM) or general purpose registers (RAM). These are separate address spaces; to access program memory, the variable must be marked <code>rom</code></p>
<p>The Pic 18-family instruction set is <em>much</em> more compact for accessing absolute RAM locations than stack-offsets. To leverage this, variables may be marked <code>overlay</code>, which is almost like statics or globals, except that the fixed location might be shared among multiple routines that don&#8217;t call each other. Obviously, this can&#8217;t be used if recursion happens.</p>
<pre>
void copyRomStrToRam(const rom char *src,char *dst,unsigned char len)
{
	overlay unsigned char i;
	for(i = 0; i < len; i++)
		dst[i] = src[i];
}
</pre>
<p><strong>Interrupt Vectors on the 18F4550</strong>. Their examples have interrupt vectors at 0x808 and 0x818. I tried to emulate that, but things kept failing. The interrupt vectors are <em>really</em> at 0x8 and 0x18... but the examples all assume you've installed their boot loader. The boot loader is a super-reduced USB stack that fits in 2K, 0-0x800, and lets you reflash the ROM over USB. Handy, but I wasn't using it, didn't have it installed. Now I poke the isr vector jumps in both locations, superstitiously, and all is well.</p>
<pre>
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void) { _asm goto _startup _endasm }

#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
void _high_ISR (void) { _asm goto isr _endasm }
#pragma code _HIGH_INTERRUPT_VECTOR_0 = 0x000008
void _high_ISR_0 (void) { _asm goto isr _endasm }

#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void) { _asm goto isr _endasm }
#pragma code _LOW_INTERRUPT_VECTOR_0 = 0x000018
void _low_ISR_0 (void) { _asm goto isr _endasm }
</pre>
<p><strong>Mac OS X and USB Stickiness</strong>. While iterating on the design, it seemed like my changes to the device would be ignored. I'd flash a new program, reset the device, and watch it show up again, reenumerated, on the Mac. But it didn't change... Turns out if it has the same product and vendor ID, the Mac <em>remembers</em> what it was from last time. Workaround: bump the product ID each time. (By the way, I'm using made-up vendor and product IDs just now.)</p>
<p><strong>Convenient Timer Numbers</strong>. The typical clock setup with the 18F4550, the one on the demo board, is a 20MHz crystal divided on-chip down to 4Mhz, and back up to 48MHz and 96MHz. This (it turns out) gives the CPU 12 Mips. I used Timer 0 to divide this by 46875, yielding a tickrate of exactly 256Hz. This is so handy; each tick, I increment a 16-bit word. The low byte is 256ths -- good enough for musical timing -- and the high byte is seconds. Might go a bit higher (look at all those 5's that divide out from 46875) and use the IRQ to do LED brightness, as well. 12 Mips seems infinite.</p>
<p><strong>Which Ports Are Available</strong>. What a puzzle. There are 5 8-bit ports, PORTA throught PORTE. But not every port has all 8 bits implemented. And some bits are preallocated to certain functions, like USB. Others happen to be used on this board for various things. And others... don't happen to show up on the easy connector on the demo board.</p>
<p><a href="/blog/content/2008/usb/picdemFsUsb.jpg"><img src="/blog/content/2008/usb/picdemFsUsb.jpg" width="560" /></a></p>
<p>For reference, here's the ones I'm using. RC6 and RC7 (after cutting jumpers JP3 through JP6 on the back, since I don't intend to use the RS232 port, though they could be easily reconnected). And RC1, RC2, RA4, and RA5.</p>
<p>Using for what? Ah. Some pictures of a prototype MIDI controller, next up.</p>
]]></content:encoded>
			<wfw:commentRss>http://omino.com/blog/2008/retro-usb-midi-device-vi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dear Plugin Developer</title>
		<link>http://omino.com/blog/2007/dear-plugin-developer/</link>
		<comments>http://omino.com/blog/2007/dear-plugin-developer/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 17:13:25 +0000</pubDate>
		<dc:creator>David Van Brink</dc:creator>
				<category><![CDATA[broad generalities]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://omino.com/blog/2007/dear-plugin-developer/</guid>
		<description><![CDATA[Dear Plugin Developer, Always code defensively. Assume the rest of the system is insane, and your module a small island of clarity, nay, robustness, in a sea of unreason. We do NOT guarantee to call any interfaces or API methods in any particular order. We do NOT guarantee even to set only legal parameters! (This [...]]]></description>
			<content:encoded><![CDATA[<p>Dear Plugin Developer,</p>
<p>Always code defensively. Assume the rest of the system is insane, and your module a small island of clarity, nay, robustness, in a sea of unreason.</p>
<p>We do NOT guarantee to call any interfaces or API methods in any particular order. We do NOT guarantee even to set only legal parameters! (This is because &#8220;we&#8221; are not always GooEdit, &#8220;we&#8221; are scripting clients, other editing hosts, anyone with a compiler might eventually call your plugin, joining the ranks of &#8220;we&#8221; to your &#8220;they&#8221;.)</p>
<p>On the flip side, we bulletproof GooEdit against plugins throwing exceptions, returning absurd results, and so on.</p>
<p>So!</p>
<p>The interface you were asking about is ISetMoreInfo. And sometimes the value will be &#8220;null&#8221;.</p>
<p> &#8211;> Goo</p>
]]></content:encoded>
			<wfw:commentRss>http://omino.com/blog/2007/dear-plugin-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
