<?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>objitsu.com</title>
	<atom:link href="http://objitsu.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://objitsu.com/blog</link>
	<description>The Adventures of Emacs the Viking</description>
	<lastBuildDate>Sat, 21 Aug 2010 17:59:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Linrad SDR Patch</title>
		<link>http://objitsu.com/blog/2010/08/21/linrad-sdr-patch/</link>
		<comments>http://objitsu.com/blog/2010/08/21/linrad-sdr-patch/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 17:59:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[hacking!]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=662</guid>
		<description><![CDATA[A friend of mine recently downloaded and tried to build Linrad (http://www.sm5bsz.com/linuxdsp/linrad.htm) but it failed with an error saying that a buffer overflow would occur. He is using Ubuntu 10.04 and the reason for this failure would appear to be &#8216;gcc&#8217; being strict and be glad it is! The essence of the problem is that [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine recently downloaded and tried to build Linrad (http://www.sm5bsz.com/linuxdsp/linrad.htm) but it failed with an error saying that a buffer overflow would occur. He is using Ubuntu 10.04 and the reason for this failure would appear to be &#8216;gcc&#8217; being strict and be glad it is!</p>
<p>The essence of the problem is that the initial array is letting the compiler set the size from the *first* occurrence of a string assignment, in this case ten characters long, but the line that blows chunks, line 369 is trying to write past the end of the buffer. That&#8217;s a no-no in anybodies book, for the morbidly curious here it is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>362
363
364
365
366
367
368
369
370
371
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>serport_number <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  sprintf<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;/dev/ttyS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  sprintf<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span>serport_number<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
  <span style="color: #009900;">&#123;</span>
  sprintf<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;/dev/ttyUSB&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  sprintf<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">11</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span>serport_number<span style="color: #339933;">-</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I fixed the code from this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>31
32
33
34
35
36
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifdef BSD</span>
<span style="color: #993333;">char</span> serport_name<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/dev/ttyd?&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> saved_euid<span style="color: #339933;">;</span>
<span style="color: #339933;">#else</span>
<span style="color: #993333;">char</span> serport_name<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/dev/ttyS?&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p>to this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>31
32
33
34
35
36
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifdef BSD</span>
<span style="color: #993333;">char</span> serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/dev/ttyd?&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">extern</span> <span style="color: #993333;">int</span> saved_euid<span style="color: #339933;">;</span>
<span style="color: #339933;">#else</span>
<span style="color: #993333;">char</span> serport_name<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/dev/ttyS?&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p>which is a brute-force and ignorance inspired solution. I&#8217;ve used 100 as I don&#8217;t think this would ever be too small for the intended use of that buffer considering the zero minutes I spent looking at the rest of the code!</p>
<p>After saving this file (lxsys.c) the project will then build, compile and install. I also generated a patch file which, once I get my request for membership to the Linrad group approved I will be submitting to them but in the meantime, here is the contents of the patch file:</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;"><span style="color: #440088;">32c32</span>
<span style="color: #991111;">&lt; char serport_name<span style="">&#91;</span><span style="">&#93;</span>=&quot;/dev/ttyd?&quot;;</span>
<span style="color: #888822;">---
<span style="color: #00b000;">&gt; char serport_name<span style="">&#91;</span><span style="">100</span><span style="">&#93;</span>=&quot;/dev/ttyd?&quot;;</span></span>
<span style="color: #440088;">35c35</span>
<span style="color: #991111;">&lt; char serport_name<span style="">&#91;</span><span style="">&#93;</span>=&quot;/dev/ttyS?&quot;;</span>
<span style="color: #888822;">---
<span style="color: #00b000;">&gt; char serport_name<span style="">&#91;</span><span style="">100</span><span style="">&#93;</span>=&quot;/dev/ttyS?&quot;;</span></span></pre></div></div>

<p>Cut and paste it, save it as &#8216;linrad.patch&#8217;, then AFTER you run run the configure script but BEFORE you type &#8216;make&#8217;, enter the following in a shell assuming that you have saved the patch file in the same folder as the file &#8216;lxsys.c&#8217; which is the source of the error:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">patch</span> lxys.c linrad.patch
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Job done, enjoy <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/08/21/linrad-sdr-patch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drupal Droppings: Fix that string!</title>
		<link>http://objitsu.com/blog/2010/06/21/drupal-droppings-fix-that-string/</link>
		<comments>http://objitsu.com/blog/2010/06/21/drupal-droppings-fix-that-string/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 17:43:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=650</guid>
		<description><![CDATA[how to painlessly alter a string without modifying any code *if* the module author has followed the rules with t()...]]></description>
			<content:encoded><![CDATA[<p>This is so obvious that I am going to blog it anyway just in case it helps somebody else!</p>
<p>We are using the node_limit module and we do not like the message that it displays when you exceed the limit of comments for your account.</p>
<p>We wanted to change that message and soon deep meaningful discussions about form_alter hooks, nodeapi hooks, $SESSION variables etc followed blah blah yadda yadda you know what I mean if you do any serious hacking in a crowd during the day sort of thing.</p>
<p>After a few minutes, we noticed a comment in the source code for the t() function which suddenly made us all go very quiet, here&#8217;s the top part of the t() function which got us all excited and humbled by Drupal-s magnificence&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> t<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$langcode</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$language</span><span style="color: #339933;">;</span>
  static <span style="color: #000088;">$custom_strings</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$langcode</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$langcode</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$langcode</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$language</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">language</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// First, check for an array of customized strings. If present, use the array</span>
  <span style="color: #666666; font-style: italic;">// *instead of* database lookups. This is a high performance way to provide a</span>
  <span style="color: #666666; font-style: italic;">// handful of string replacements. See settings.php for examples.</span>
  <span style="color: #666666; font-style: italic;">// Cache the $custom_strings variable to improve performance.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$custom_strings</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$langcode</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$custom_strings</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$langcode</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'locale_custom_strings_'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$langcode</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>So we steamed of over to the settings.php file and lo and behold, right there at the bottom of the file, a place not oft visited during the day we found this, clearly commented but also commented out,</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * String overrides:
 *
 * To override specific strings on your site with or without enabling locale
 * module, add an entry to this list. This functionality allows you to change
 * a small number of your site's default English language interface strings.
 *
 * Remove the leading hash signs to enable.
 */</span>
<span style="color: #666666; font-style: italic;"># $conf['locale_custom_strings_en'] = array(
</span><span style="color: #666666; font-style: italic;">#   'forum'      =&gt; 'Discussion board',
</span><span style="color: #666666; font-style: italic;">#   '@count min' =&gt; '@count minutes',
</span><span style="color: #666666; font-style: italic;"># );</span></pre></div></div>

<p>For example, lets say there is a module &#8216;M&#8217; that detected a condition and does this :-</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">drupal_set_message<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Your input sucks, do it again you idiot!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This might have been funny at the time but if you wanted to change that to something different without touching the code, which may not even be available for modification for many reasons, all you have to do is modify the $conf variable like so :-</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$conf</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'locale_custom_strings_en'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">&quot;Your input sucks, do it again you idiot!&quot;</span> <span style="color: #339933;">=&gt;</span>
      <span style="color: #0000ff;">&quot;Please check your input and try again.&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So, all we had to do was add a new entry, the key being the existing string and the value being what we wanted to say instead.<br />
Works like a charm!<br />
 <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/06/21/drupal-droppings-fix-that-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Drupal Droppings: variable_get tamed!</title>
		<link>http://objitsu.com/blog/2010/06/01/drupal-droppings-variable_get-tamed/</link>
		<comments>http://objitsu.com/blog/2010/06/01/drupal-droppings-variable_get-tamed/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 17:38:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[hacking!]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=641</guid>
		<description><![CDATA[A more concise way to use variable_get and improve coding quality at the same time!]]></description>
			<content:encoded><![CDATA[<p>In Drupal there is the function called variable_get() which is the<br />
means by which a simple persistence mechanism has been provided for<br />
modules. The name that you give to your variable should always be<br />
prefixed (under normal circumstances) with the name of the module. So<br />
for example, saving a variable for module &#8216;foo&#8217; would probably look<br />
like this :-</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">variable_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo_value'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you are a good little coder and you are writing a nice tidy little<br />
back-end administration page to support some module features then you<br />
ought to be always supplying your calls to variable_get() with a<br />
suitable default value because it is possible that a module can be<br />
called upon to do its job without the user ever visting the back end<br />
administration pages.</p>
<p>One thing that you can do is to use variable_set() to set defaults for<br />
everything in the module.install file, that way every variable that<br />
your module uses will have a sensible value set for it by the time the<br />
real code is called upon to do its thing. If you haven&#8217;t done that<br />
then the only other way to ensure that you always get a sensible value<br />
is to always supply a default value to the variable_get() call.</p>
<p>Sometimes this can get a little tedious especially if, like me, you<br />
also always use named constants to refer to things to avoid repeating<br />
yourself in the code. This is a good practice but sometimes typing<br />
long names can be a real pain. Yeah, I know, editors can help but<br />
wouldn&#8217;t it be nice if there was a cleaner way to always call<br />
variable_get() with a property name and have it always return either<br />
the current value or the default value and at the same time minimise<br />
the typing and thus opportunity for error ?</p>
<h3>Here it is!</h3>
<p>Step one. Make your variable name the same as the constant name.<br />
Step two. Define this function somewhere in your module.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> _vg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	? <span style="color: #990000;">constant</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> variable_get<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that we have to use the strtoupper() function on the constant name in order to conform to the convention that all constants are defined in upper case. This is an interesting &#8216;quirk&#8217; in PHP I guess because by default the constants are case sensitive. However, if you stick the the rule of *always* defining your constants in upper case then this function is for you!</p>
<p>All you have to do now is make sure you define your constants and then<br />
you can just use _vg() with the name of the constant&#8230; if the<br />
constant is defined then it will use the current value of the constant<br />
as the default value, otherwise it will use whatever value is<br />
currently set for that constant.</p>
<p>This means that instead of typing this all the time :-</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FOO_HIT_COUNTER_VALUE'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// longhand</span>
<span style="color: #000088;">$thing</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo_hit_counter_value'</span><span style="color: #339933;">,</span> FOO_HIT_COUNTER_VALUE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// shorter</span>
<span style="color: #000088;">$thing</span> <span style="color: #339933;">=</span> _vg<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo_hit_counter_value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Having used this pattern for a while I have noticed that I make less<br />
mistakes and that it becomes easier on the brain to sit down and<br />
actually declare a shed load of constants in the .module file and<br />
coding becomes more disciplined.</p>
<p>Enjoy.<br />
 <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/06/01/drupal-droppings-variable_get-tamed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Line Pinguino &#8212; The Sequel!</title>
		<link>http://objitsu.com/blog/2010/05/01/command-line-pinguino-the-sequel/</link>
		<comments>http://objitsu.com/blog/2010/05/01/command-line-pinguino-the-sequel/#comments</comments>
		<pubDate>Sat, 01 May 2010 21:39:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Pinguino]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=632</guid>
		<description><![CDATA[Talk about bad timing&#8230; not less than two days after my original posting, does a new release of Pinguino IDE arrive! LOL. It now includes a command line option but it still does not provide you with the means to upload the HEX file on a successful build. I have spoken with Jean-Pierre and we [...]]]></description>
			<content:encoded><![CDATA[<p>Talk about bad timing&#8230; not less than two days after <a href="http://objitsu.com/blog/2010/04/30/comamnd-line-pinguino/">my original posting</a>,  does a new release of Pinguino IDE arrive! LOL. <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It now includes a command line option but it still does not provide you with the means to upload the HEX file on a successful build. I have spoken with Jean-Pierre and we have both agreed that the way forward is a separation of concerns; to produce separate modules for editing, building and uploading. I will leave that to JP and his team as I have other things to do.</p>
<p>But&#8230; being a sad Unix hacker I decided that I still prefer using Emacs and I still want auto-uploading but this time I decided to get it <strong>without modifying pinguinobeta8.py</strong> in any way what-so-ever!</p>
<p>I have created a real simple bash script (Cygwin compatible in theory but I have not tested it as I don&#8217;t use/want/like Windows) that takes the first parameter and attempts to use the now present command line compile option and if that works it will upload it for me. If it fails then it dumps out the output it captured during the build which will contain the errors messages at the bottom.</p>
<p>For some reason it also seems to echo out the processed lines of the source file which is a little busy on the screen, it would be nice to be able to turn that off.</p>
<p>Here is the bash script, save it in a file called &#8216;pgmake&#8217; then do
<pre>chmod +x pgmake</pre>
<p>. This file must be in the same folder as the Pinguino IDE as it makes a relative reference to the tools folder.</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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># This uses the Pinguino 8 release to command line compile a file and</span>
<span style="color: #666666; font-style: italic;"># if that succeeds it will prompt the user and then upload the HEX</span>
<span style="color: #666666; font-style: italic;"># file to the Pinguino</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># If the build fails we display the output from the build.</span>
<span style="color: #666666; font-style: italic;">#</span>
upload<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;Press RETURN to upload the HEX file $1.HEX&quot;</span>
    .<span style="color: #000000; font-weight: bold;">/</span>tools<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>docker <span style="color: #660033;">-v</span> 04d8 <span style="color: #c20cb9; font-weight: bold;">write</span> $1.hex
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>pinguinobeta8.py <span style="color: #660033;">-nogui</span> $1.pde <span style="color: #000000; font-weight: bold;">&amp;&gt;</span> $1.log <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> upload $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> $1.log</pre></td></tr></table></div>

<p>The usage is as follows and please note that you only need specify the basic part of the filename, i.e. omit the PDE extension. This is a bare-bones, no error checking just get the bleeding job done script.</p>
<p>So, for example, to compile and upload a file called ERIC,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>pgmake eric
Press RETURN to upload the HEX <span style="color: #c20cb9; font-weight: bold;">file</span> eric.HEX
Processing device 009
erasing section <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2000</span>, 7fff<span style="color: #7a0874; font-weight: bold;">&#93;</span>
writing section <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2000</span>, 7fff<span style="color: #7a0874; font-weight: bold;">&#93;</span>
$</pre></div></div>

<p>Cheers. <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/05/01/command-line-pinguino-the-sequel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Line Pinguino</title>
		<link>http://objitsu.com/blog/2010/04/30/comamnd-line-pinguino/</link>
		<comments>http://objitsu.com/blog/2010/04/30/comamnd-line-pinguino/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 09:35:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Pinguino]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hacking!]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=618</guid>
		<description><![CDATA[Using the Pinguino IDE for command line compilation.]]></description>
			<content:encoded><![CDATA[<p>I have been working with PIC-s for a while and a few months back I was informed of the existence of &#8216;Pinguino&#8217;. You can find it at <a href="http://www.hackinglab.org/" target="_blank">this page</a>. I also heartily recommend you read <a href="http://jpmandon.blogspot.com/" target="_blank">the blog site</a> as well to get more background information on it.</p>
<p>Jean-Pierre has written a really compact and useful IDE in Python which does a wonderful job of hiding all the dirty details on firing of SDCC for a compile and link and using the upload tool. That is a big help for people that may not be familiar with such things. The IDE is really good but I couldn&#8217;t figure out how to make it use Courier which I pretty much always like for coding for the font and also it seems to have the odd issue with cut and paste getting out of synch with what is really showing on the screen. I am sure these will all be ironed out over time. As it stands, I  can&#8217;t think of a better way for people to enter into the world of PIC development!</p>
<p>However, I have used Emacs for over twenty three years and I kind of get used to being able to do everything with it and it started to become apparent that the best thing I could do would be to modify the IDE so that it accepted just a single command line argument which would allow me to run the &#8216;compile&#8217; command from within Emacs and keep on hacking!</p>
<h3>Some Python hacking&#8230;</h3>
<p>It&#8217;s been a long time! I chose not to follow Python because I just didn&#8217;t like the indeting-is-the-association thing. Anyway, I modified the main code to look like this, just a simple call to the command line basher&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    wx.<span style="color: black;">InitAllImageHandlers</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame_1 = Pinguino<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, -<span style="color: #ff4500;">1</span>, <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># OBJITSU modifications start here!</span>
    frame_1.<span style="color: black;">cliRequest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">SetTopWindow</span><span style="color: black;">&#40;</span>frame_1<span style="color: black;">&#41;</span>
    frame_1.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>All it does is call the <strong>cliRequest()</strong> function which is as follows: it checks for the -c or -C and if either is present it compiles a single file and exists. Sure, you could go mad and allow this and that but I am a strong follower of <a href="http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html" target="_blank">doing the simplest thing.</a>. C2.COM is an awesome site; regular reading def. boosts brain-cells IMHO.</p>
<p>Here is the implementation of the cliRequest() function, it just uses the standard <strong>getopt</strong> module and drives the desired functionality&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">#</span>
    <span style="color: #808080; font-style: italic;"># This checks to see if a filename was given on the command line with</span>
    <span style="color: #808080; font-style: italic;"># the -c option. If it was then we just do the usual thing and exit</span>
    <span style="color: #808080; font-style: italic;"># without bothering to fire up the UI.</span>
    <span style="color: #808080; font-style: italic;">#</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> cliRequest<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        args, opts = <span style="color: #dc143c;">getopt</span>.<span style="color: #dc143c;">getopt</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>,<span style="color: #483d8b;">'C:c:'</span>,<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> opt, arg <span style="color: #ff7700;font-weight:bold;">in</span> args:
            <span style="color: #ff7700;font-weight:bold;">if</span> opt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'-C'</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">cliBuild</span><span style="color: black;">&#40;</span>arg,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> opt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'-c'</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">cliBuild</span><span style="color: black;">&#40;</span>arg,<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> cliBuild<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,filename,upload<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">commandLine</span>=<span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">buildHexImage</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> upload == <span style="color: #008000;">True</span>:
                <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reset the Pinguino, press RETURN to upload HEX...&quot;</span><span style="color: black;">&#41;</span>
                rawfilename,extension=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">splitext</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">cleanUpload</span><span style="color: black;">&#40;</span> rawfilename<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre></div></div>

<p>I also had to re-factor some of the internal code to de-couple the editor component from the pre-process, compile, link and upload code. For completeness I may as well throw in the actual function that the re-factored code calls and the new code calls so you can read it. It&#8217;s been a while since I cut Python code so it may appear a liitle &#8216;idiosyncratic&#8217; in places! LOL <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">#</span>
    <span style="color: #808080; font-style: italic;"># This wil pre-process, comile and link the 'filename' as though</span>
    <span style="color: #808080; font-style: italic;"># it had come from the UI</span>
    <span style="color: #808080; font-style: italic;">#</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buildHexImage<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,filename<span style="color: black;">&#41;</span>:
        filename,extension=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">splitext</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>filename+<span style="color: #483d8b;">&quot;.hex&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">os</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>filename+<span style="color: #483d8b;">&quot;.hex&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;/source/user.c&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">os</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;/source/user.c&quot;</span><span style="color: black;">&#41;</span>
        retour=<span style="color: #008000;">self</span>.<span style="color: black;">preprocess</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> retour==<span style="color: #483d8b;">&quot;error&quot;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        retour=<span style="color: #008000;">self</span>.<span style="color: black;">cleanCompile</span><span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> retour<span style="color: #66cc66;">!</span>=<span style="color: #ff4500;">0</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">displaymsg</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;error while compiling file &quot;</span>+filename,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            retour=<span style="color: #008000;">self</span>.<span style="color: black;">link</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;/source/main.hex&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">True</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">displaymsg</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;error while linking &quot;</span>+filename+<span style="color: #483d8b;">&quot;.o&quot;</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">cp</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;/source/main.hex&quot;</span>,filename+<span style="color: #483d8b;">&quot;.hex&quot;</span><span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">displaymsg</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;compilation done&quot;</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">os</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;/source/main.hex&quot;</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">os</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>filename+<span style="color: #483d8b;">&quot;.c&quot;</span><span style="color: black;">&#41;</span>         
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span></pre></div></div>

<p>The file is available <a href="http://www.italentshare.com/pinguinoforum/viewtopic.php?f=5&#038;t=26" target="_blank">at the Pinguino forums</a> shown somewhere near the bottom left of the post.</p>
<h3>Using it&#8230;</h3>
<p>It has some limitations; it must be run from the Pinguino installation folder as I am no Python export and couldn&#8217;t make it run from /usr/local/bin. I don&#8217;t care so long as I can use it from Emacs! LOL If somebody cares to polish it so it could be installed from /usr/local/bin and still load the button icons etc then that would be nice.</p>
<h4>Straight compile to a HEX file</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>pgb7cc.py <span style="color: #660033;">-c</span> filename.pde
compilation <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<h4>Straight compile to a HEX file</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>pgb7cc.py <span style="color: #660033;">-c</span> filename.pde
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>sean<span style="color: #000000; font-weight: bold;">/</span>pinguino<span style="color: #000000; font-weight: bold;">/</span>pinguino beta7 linux <span style="color: #000000; font-weight: bold;">/</span>source<span style="color: #000000; font-weight: bold;">/</span>user.c:<span style="color: #000000;">24</span>: syntax error: token -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">'void'</span> ; column <span style="color: #000000;">12</span>
&nbsp;
error <span style="color: #000000; font-weight: bold;">while</span> compiling <span style="color: #c20cb9; font-weight: bold;">file</span> ir-jammer-<span style="color: #000000;">1</span></pre></div></div>

<p>and finally a good build and upload&#8230;</p>
<h4>Compile to a HEX file and Upload</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>pgb7cc.py <span style="color: #660033;">-C</span> ir-jammer-1.pde 
compilation <span style="color: #000000; font-weight: bold;">done</span>
Reset the Pinguino, press RETURN to upload HEX...
Processing device 004
erasing section <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2000</span>, 7fff<span style="color: #7a0874; font-weight: bold;">&#93;</span>
writing section <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2000</span>, 7fff<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>The upload will only happen if the build succeeds, otherwise it just halts there and then with the errors fed to stdout.</p>
<p>All in all I have really enjoyed re-jigging the IDE to allow command line operation and Jean-Pierre has inspired me to now go and write another tool-set but I think I may use one of Gambit Scheme or PicoLisp or Erlang or&#8230; or&#8230; or&#8230; I love/hate software! LOL</p>
<p>I hope you found this useful.<br />
 <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/04/30/comamnd-line-pinguino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang BulkSMS Module</title>
		<link>http://objitsu.com/blog/2010/04/24/erlang-bulksms-module/</link>
		<comments>http://objitsu.com/blog/2010/04/24/erlang-bulksms-module/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 21:40:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=610</guid>
		<description><![CDATA[A simple Erlang driver for using BulkSMS.co.uk]]></description>
			<content:encoded><![CDATA[<p>I recently bought some BulkSMS credits for a project I am working on in Erlang so here is a simple driver for it.</p>
<p>It supports only what I needed, just the ability to send SMS messages, get quotes for sending and to get the credit balance. There is no support for batch sending or any of the other parts of the API. If I need them, I will add them.</p>
<p>So, here it is, warts and all.<br />
It does work!</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">%%%-------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%% File        : bulksms.erl</span>
<span style="color: #666666; font-style: italic;">%%% Author      : Sean Charles &lt;sean@objitsu.com&gt;</span>
<span style="color: #666666; font-style: italic;">%%% Description : A simple driver to the BulkSMS message interface </span>
<span style="color: #666666; font-style: italic;">%%%             : supports sending SMS messages, getting quotes and</span>
<span style="color: #666666; font-style: italic;">%%%             : a credit balance.</span>
<span style="color: #666666; font-style: italic;">%%%             :</span>
<span style="color: #666666; font-style: italic;">%%%             : NOTE: send_batch is not implemented.</span>
<span style="color: #666666; font-style: italic;">%%%             :</span>
<span style="color: #666666; font-style: italic;">%%% Created     : 19 Apr 2010 by Sean Charles &lt;sean@objitsu.com&gt;</span>
<span style="color: #666666; font-style: italic;">%%%</span>
<span style="color: #666666; font-style: italic;">%%% Published under a 'do what you want with it but a credit would</span>
<span style="color: #666666; font-style: italic;">%%% be nice' licence.</span>
<span style="color: #666666; font-style: italic;">%%%</span>
<span style="color: #666666; font-style: italic;">%%%-------------------------------------------------------------------</span>
&nbsp;
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>bulksms<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">compile</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>send_sms<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> quote_sms<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> get_credits<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">BULKSERVER</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;http://www.bulksms.co.uk:5567/eapi/&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">MULTIPART</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">GETCREDITS</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;http://www.bulksms.co.uk:5567/eapi/user/get_credits/1/1.1&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">SENDSMS</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">QUOTESMS</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;http://www.bulksms.co.uk:5567/eapi/submission/quote_sms/2/2.0&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%% Define the routing_group setting</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">ROUTING_GROUP</span><span style="color: #6bb810;">,</span><span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%% These will be part of the session start request</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">USERNAME</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;--your-bulksms-account-name-here--&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">PASSWORD</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;--password-here--&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% Send an SMS to one or more devices. The MSISDN string is assumed to</span>
<span style="color: #666666; font-style: italic;">%% be a string() containing one or more comma separated numbers.</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
&nbsp;
<span style="color: #ff3c00;">send_sms</span><span style="color: #109ab8;">&#40;</span> <span style="color: #45b3e6;">MSISDN</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Msg</span> <span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span>
    <span style="color: #ff3c00;">execute</span><span style="color: #109ab8;">&#40;</span>
	   ?<span style="color: #6941fd;">SENDSMS</span><span style="color: #6bb810;">,</span>
	   <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>message<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Msg</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
	    <span style="color: #109ab8;">&#123;</span>msisdn<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">MSISDN</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% @doc This takes the same parameters as the send function but</span>
<span style="color: #666666; font-style: italic;">%% returns a quote of the number of credits that would be required to</span>
<span style="color: #666666; font-style: italic;">%% send the supplied data for real.</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
&nbsp;
<span style="color: #ff3c00;">quote_sms</span><span style="color: #109ab8;">&#40;</span> <span style="color: #45b3e6;">MSISDN</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Msg</span> <span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span>
    <span style="color: #ff3c00;">execute</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">QUOTESMS</span><span style="color: #6bb810;">,</span>
	    <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>message<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Msg</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
	     <span style="color: #109ab8;">&#123;</span>msisdn<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">MSISDN</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% @doc This answers the number of credits remaining on the account.</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% @spec get_credits() -&gt; {ok, float()} | {error, Reason}</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
&nbsp;
<span style="color: #ff3c00;">get_credits</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span>
    <span style="color: #186895;">case</span> <span style="color: #ff3c00;">execute</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">GETCREDITS</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span>
	<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">-&gt;</span>
	    <span style="color: #45b3e6;">List</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">string</span>:<span style="color: #ff3c00;">tokens</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Data</span><span style="color: #6bb810;">,</span><span style="color: #ff7800;">&quot;|&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
	    <span style="color: #45b3e6;">Code</span> <span style="color: #014ea4;">=</span> <span style="color: #ff3c00;">list_to_integer</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">List</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
	    <span style="color: #45b3e6;">Desc</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">List</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
	    <span style="color: #186895;">case</span> <span style="color: #45b3e6;">Code</span> <span style="color: #186895;">of</span> 
		<span style="color: #ff9600;">0</span> <span style="color: #6bb810;">-&gt;</span> 
		    <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Credits</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">_</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">string</span>:<span style="color: #ff3c00;">to_float</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">List</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
		    <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Credits</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">;</span>
		<span style="color: #45b3e6;">_</span> <span style="color: #6bb810;">-&gt;</span>
		    <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Code</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Desc</span><span style="color: #109ab8;">&#125;</span>
	    <span style="color: #186895;">end</span><span style="color: #6bb810;">;</span>
	<span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">-&gt;</span>
	    <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span>
    <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% @doc Execute a specific API command to the BulkSMS server.</span>
<span style="color: #666666; font-style: italic;">%% </span>
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
&nbsp;
<span style="color: #ff3c00;">execute</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">URL</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
    <span style="color: #45b3e6;">POSTData</span> <span style="color: #014ea4;">=</span> <span style="color: #ff3c00;">post_data</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;DATA: ~s~n&quot;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">POSTData</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #45b3e6;">Response</span> <span style="color: #014ea4;">=</span> http<span style="color: #ff4e18;">c</span>:<span style="color: #ff3c00;">request</span><span style="color: #109ab8;">&#40;</span>
	     post<span style="color: #6bb810;">,</span>
	   <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">URL</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span>?<span style="color: #6941fd;">MULTIPART</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">POSTData</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
	   <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span>
	   <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>full_result<span style="color: #6bb810;">,</span>false<span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #186895;">case</span> <span style="color: #45b3e6;">Response</span> <span style="color: #186895;">of</span>
	<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span><span style="color: #ff9600;">200</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
	    <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">;</span>
	<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Error</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
	    <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Error</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span>
    <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%% @doc Converts a {k,v} list into a URL string and also adds in</span>
<span style="color: #666666; font-style: italic;">%% essential fields such as the username and password.</span>
<span style="color: #666666; font-style: italic;">%%</span>
<span style="color: #666666; font-style: italic;">%%--------------------------------------------------------------------</span>
&nbsp;
<span style="color: #ff3c00;">post_data</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span>
    <span style="color: #45b3e6;">AllParams</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">flatten</span><span style="color: #109ab8;">&#40;</span>
		  <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>username<span style="color: #6bb810;">,</span>?<span style="color: #6941fd;">USERNAME</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
		   <span style="color: #109ab8;">&#123;</span>password<span style="color: #6bb810;">,</span>?<span style="color: #6941fd;">PASSWORD</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
		   <span style="color: #109ab8;">&#123;</span>routing_group<span style="color: #6bb810;">,</span>?<span style="color: #6941fd;">ROUTING_GROUP</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
		   <span style="color: #666666; font-style: italic;">%%{test_always_succeed,1},</span>
		   <span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #45b3e6;">Data</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">flatten</span><span style="color: #109ab8;">&#40;</span>
	     <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">io_lib</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;&amp;~s=~s&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">K</span><span style="color: #6bb810;">,</span><span style="color: #ff3c00;">to_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">V</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span>
	      <span style="color: #014ea4;">||</span> <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">K</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">V</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">&lt;-</span> <span style="color: #45b3e6;">AllParams</span> <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #ff4e18;">string</span>:<span style="color: #ff3c00;">sub_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Data</span><span style="color: #6bb810;">,</span><span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
&nbsp;
<span style="color: #ff3c00;">to_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">when</span> <span style="color: #ff3c00;">is_atom</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">atom_to_list</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">to_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">when</span> <span style="color: #ff3c00;">is_integer</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span> <span style="color: #ff4e18;">io_lib</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;~B&quot;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">to_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">when</span> <span style="color: #ff3c00;">is_list</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">X</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">X</span><span style="color: #6bb810;">.</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/04/24/erlang-bulksms-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang, Nitrogen and automatic rebuild.</title>
		<link>http://objitsu.com/blog/2010/03/16/erlang-nitrogen-and-automatic-rebuild/</link>
		<comments>http://objitsu.com/blog/2010/03/16/erlang-nitrogen-and-automatic-rebuild/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:33:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[nitrogen]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=603</guid>
		<description><![CDATA[I came across Nitrogen about a year ago and loved it and it has come on very well since then. Finally I have been presented with an opportunity to use it for a freelance project (currently under an NDA) and the first thing that kind of caused me a little upset was the fact that [...]]]></description>
			<content:encoded><![CDATA[<p>I came across Nitrogen about a year ago and loved it and it has come on very well since then.</p>
<p>Finally I have been presented with an opportunity to use it for a freelance project (currently under an NDA) and the first thing that kind of caused me a little upset was the fact that unlike PHP for example, it didn&#8217;t seem to automatically rebuild and reload any modules that you have edited.</p>
<p>It is possible that I missed something in the documentation but I am pretty sure I didn&#8217;t too&#8230; however, I remember there being a single point of entry that all requests go through&#8230; the perfect spot to use the Erlang make module.</p>
<p>And here is my solution. All you do is modify your existing YOURAPP_app.erl file like this :-</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">request</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Module</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
    <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;request-pinchpoint: ~w~n&quot;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Module</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    <span style="color: #ff4e18;">make</span>:<span style="color: #ff3c00;">all</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>all<span style="color: #6bb810;">,</span>load<span style="color: #6bb810;">,</span>debug_info<span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
    nitrogen:<span style="color: #ff3c00;">request</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Module</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>Once you have done that, rebuild it from the command line for the final time (yes!) and then run it again&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">make</span>
erl <span style="color: #660033;">-make</span>
$ .<span style="color: #000000; font-weight: bold;">/</span>start.sh
Starting Nitrogen.
Erlang R13B04 <span style="color: #7a0874; font-weight: bold;">&#40;</span>erts-5.7.5<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">source</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>smp:<span style="color: #000000;">2</span>:<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>rq:<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>async-threads:<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>hipe<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>kernel-poll:<span style="color: #c20cb9; font-weight: bold;">false</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
Eshell V5.7.5  <span style="color: #7a0874; font-weight: bold;">&#40;</span>abort with ^G<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#40;</span>nitrogen<span style="color: #000000; font-weight: bold;">@</span>localhost<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span> 
&nbsp;
<span style="color: #660033;">---</span>
Nitrogen is now running on inets.
Serving files from: .<span style="color: #000000; font-weight: bold;">/</span>wwwroot
Template root is at: .<span style="color: #000000; font-weight: bold;">/</span>
Open your browser to: http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8000</span>
<span style="color: #660033;">---</span></pre></div></div>

<p>Now go and edit a module file beneath the src/ tree and then refresh the page&#8230; here is what you should see if for example you modifed the default home page module in web_index.erl&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">request-pinchpoint: web_index
Recompile: .<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>pages<span style="color: #000000; font-weight: bold;">/</span>web_index</pre></div></div>

<p>That&#8217;s it. Happy hacking.</p>
<p>PS: You can remove the pinch-point print statement if it annoys you too much, I just like to see / know what&#8217;s going on sometimes!</p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/03/16/erlang-nitrogen-and-automatic-rebuild/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iPhone and Gambit Scheme</title>
		<link>http://objitsu.com/blog/2010/01/28/iphone-and-gambit-scheme/</link>
		<comments>http://objitsu.com/blog/2010/01/28/iphone-and-gambit-scheme/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 17:56:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Gambit]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=595</guid>
		<description><![CDATA[James Long has written some awesome stuff allowing you to code iPhone applications with Gambit Scheme. I will leave the details for james to explain, which he does excellently here. However&#8230; the Xcode project as downloaded does not work, the first gotcha was explained to me by James after I mailed him a sorry tale [...]]]></description>
			<content:encoded><![CDATA[<p>James Long has written some awesome stuff allowing you to code iPhone applications with Gambit Scheme. I will leave the details for james to explain, which he does excellently <a href="http://jlongster.com/blog/2009/06/17/write-apps-iphone-scheme/" target="_blank">here.</a></p>
<p>However&#8230; the Xcode project as downloaded does not work, the first gotcha was explained to me by James after I mailed him a sorry tale of hours spent poring over assembly code and some hair-pulling by me.</p>
<p>The fix is incredibly simple and I will simply put the email response from James here so you can see the fix:</p>
<pre>
I know exactly what it is, and I am so sorry you spent so much
time trying to figure it out.  If you look in lib/init.scm, at the top
you'll see a section for "debugger".  I think this should be the code:
</pre>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>include <span style="color: #ff0000;">&quot;util/remote-debugger/debuggee.scm&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>rdi<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">-</span>host<span style="color: #66cc66;">!</span> <span style="color: #ff0000;">&quot;localhost:20000&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>thread<span style="color: #66cc66;">-</span>start<span style="color: #66cc66;">!</span>
 <span style="color: #66cc66;">&#40;</span>make<span style="color: #66cc66;">-</span>thread
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>##repl<span style="color: #66cc66;">-</span>debug<span style="color: #66cc66;">-</span>main<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<pre>
Comment out all of these lines, and it should work.
The problem is that you aren't running the debugger for the
app to connect to, and the debugging libraries for this are
extremely rough, and don't even handle the case where there
isn't a debugger to connect to.  It's partially my fault for not
noting this somewhere.

It comes from the call to `___setup` because that's where
Gambit evaluates all of the code at the top level, and that code
is executed immediately.

I am sorry again you had to spend so much time on it.  I'm
currently reworking my site and am hoping to write more articles
about using Gambit on the iPhone and will update everything to
make it better.

Let me know if that works!
- James
</pre>
<p>That was a great help but there is one more file that needs to be changed, it is the lib/config.scm file, and you need to change it to whatever path is appropriate for your system, this is what it contains as downloaded :-</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">load</span>
<span style="color: #ff0000;">&quot;/Users/james/projects/scheme/gambit-iphone-example/lib/apps/app5&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Once you do that, you can build and run and it just works as expected.</p>
<p>I want to say a big thanks to James for responding so quickly and with accurate information to boot. I shall be in the corner coding scheme on my iPhone if anybody wants me&#8230; <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/01/28/iphone-and-gambit-scheme/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An in-depth analysis of &#8220;ffmpeg and non-monotone timestamps&#8221;</title>
		<link>http://objitsu.com/blog/2010/01/21/an-in-depth-analysis-of-ffmpeg-and-non-monotone-timestamps/</link>
		<comments>http://objitsu.com/blog/2010/01/21/an-in-depth-analysis-of-ffmpeg-and-non-monotone-timestamps/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:33:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=581</guid>
		<description><![CDATA[Ever been faced with "ffmpeg and non monotone timestamps"... WTF? Me too so I wanted to know more about the cause. No solution. Sorry!]]></description>
			<content:encoded><![CDATA[<p>Having built the latest version of ffmpeg and then tried to run this comamnd line on an FLV file grabbed with flvstreamer, I was presented with this error:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>scharles<span style="color: #7a0874; font-weight: bold;">&#93;</span>vrec $ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> VREC_2.flv <span style="color: #660033;">-acodec</span> copy <span style="color: #660033;">-vcodec</span> copy <span style="color: #660033;">-f</span> flv test.flv
FFmpeg version git-svn-r21326, Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>-<span style="color: #000000;">2010</span> Fabrice Bellard, et al.
  built on Jan <span style="color: #000000;">19</span> <span style="color: #000000;">2010</span> <span style="color: #000000;">16</span>:<span style="color: #000000;">35</span>:<span style="color: #000000;">26</span> with <span style="color: #c20cb9; font-weight: bold;">gcc</span> 4.0.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Apple Inc. build <span style="color: #000000;">5493</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  configuration: 
  libavutil     50. 7. <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">/</span> 50. 7. <span style="color: #000000;">0</span>
  libavcodec    52.48. <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">/</span> 52.48. <span style="color: #000000;">0</span>
  libavformat   52.47. <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">/</span> 52.47. <span style="color: #000000;">0</span>
  libavdevice   52. 2. <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">/</span> 52. 2. <span style="color: #000000;">0</span>
  libswscale     0. 8. <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">/</span>  0. 8. <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>flv <span style="color: #000000; font-weight: bold;">@</span> 0x1002600<span style="color: #7a0874; font-weight: bold;">&#93;</span>Estimating duration from bitrate, this may be inaccurate
&nbsp;
Seems stream <span style="color: #000000;">0</span> codec frame rate differs from container frame rate: <span style="color: #000000;">1000.00</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1000</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">24.83</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">149</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Input <span style="color: #666666; font-style: italic;">#0, flv, from 'VREC_2.flv':</span>
  Duration: 00:<span style="color: #000000;">24</span>:<span style="color: #000000;">28.79</span>, start: <span style="color: #000000;">0.000000</span>, bitrate: <span style="color: #000000;">128</span> kb<span style="color: #000000; font-weight: bold;">/</span>s
    Stream <span style="color: #666666; font-style: italic;">#0.0: Video: vp6f, yuv420p, 480x360, 24.83 tbr, 1k tbn, 1k tbc</span>
    Stream <span style="color: #666666; font-style: italic;">#0.1: Audio: mp3, 44100 Hz, 2 channels, s16, 128 kb/s</span>
Output <span style="color: #666666; font-style: italic;">#0, flv, to 'test.flv':</span>
    Stream <span style="color: #666666; font-style: italic;">#0.0: Video: 0x0004, yuv420p, 480x360, q=2-31, 1k tbn, 1k tbc</span>
    Stream <span style="color: #666666; font-style: italic;">#0.1: Audio: 0x0002, 44100 Hz, 2 channels, 128 kb/s</span>
Stream mapping:
  Stream <span style="color: #666666; font-style: italic;">#0.0 -&gt; #0.0</span>
  Stream <span style="color: #666666; font-style: italic;">#0.1 -&gt; #0.1</span>
Press <span style="color: #7a0874; font-weight: bold;">&#91;</span>q<span style="color: #7a0874; font-weight: bold;">&#93;</span> to stop encoding
<span style="color: #7a0874; font-weight: bold;">&#91;</span>flv <span style="color: #000000; font-weight: bold;">@</span> 0x1007600<span style="color: #7a0874; font-weight: bold;">&#93;</span>st:<span style="color: #000000;">0</span> error, non monotone timestamps <span style="color: #000000;">1169932</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000;">1201</span>
av_interleaved_write_frame<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>: Error <span style="color: #000000; font-weight: bold;">while</span> opening <span style="color: #c20cb9; font-weight: bold;">file</span></pre></div></div>

<p>In the ffmpeg source, in the file ./libavformat/utils.c at line 2633 we have the following piece of code, which is responsible for the above error and subsequent bail-out of the operation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>2633
2634
2635
2636
2637
2638
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">&amp;&amp;</span> st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">!=</span> AV_NOPTS_VALUE <span style="color: #339933;">&amp;&amp;</span> st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">&gt;=</span> pkt<span style="color: #339933;">-&gt;</span>dts<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        av_log<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span> AV_LOG_ERROR<span style="color: #339933;">,</span>
               <span style="color: #ff0000;">&quot;st:%d error, non monotone timestamps %&quot;</span>PRId64<span style="color: #ff0000;">&quot; &gt;= %&quot;</span>PRId64<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
               st<span style="color: #339933;">-&gt;</span>index<span style="color: #339933;">,</span> st<span style="color: #339933;">-&gt;</span>cur_dts<span style="color: #339933;">,</span> pkt<span style="color: #339933;">-&gt;</span>dts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Also worth pointing out here is the comment in the definition of the AVPacket structure, which is what pkt is a pointer to at this point, file is ./libavcodec/avcodec.h and this is the full structure dump of AVPacket for FFmpeg version git-svn-r21326, Copyright (c) 2000-2010 Fabrice Bellard, et al. built on Jan 19 2010 16:35:26 with gcc 4.0.1 (Apple Inc. build 5493) by me&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> AVPacket <span style="color: #009900;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/**                                                                                                    
     * Presentation timestamp in AVStream-&gt;time_base units; the time at which                              
     * the decompressed packet will be presented to the user.                                              
     * Can be AV_NOPTS_VALUE if it is not stored in the file.                                              
     * pts MUST be larger or equal to dts as presentation cannot happen before                             
     * decompression, unless one wants to view hex dumps. Some formats misuse                              
     * the terms dts and pts/cts to mean something different. Such timestamps                              
     * must be converted to true pts/dts before they are stored in AVPacket.                               
     */</span>
    int64_t pts<span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/**                                                                                                    
     * Decompression timestamp in AVStream-&gt;time_base units; the time at which                             
     * the packet is decompressed.                                                                         
     * Can be AV_NOPTS_VALUE if it is not stored in the file.                                              
     */</span>
    int64_t dts<span style="color: #339933;">;</span>
    uint8_t <span style="color: #339933;">*</span>data<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span>   size<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span>   stream_index<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span>   flags<span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/**                                                                                                    
     * Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.                                 
     * Equals next_pts - this_pts in presentation order.                                                   
     */</span>
    <span style="color: #993333;">int</span>   duration<span style="color: #339933;">;</span>
    <span style="color: #993333;">void</span>  <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>destruct<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> AVPacket <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">void</span>  <span style="color: #339933;">*</span>priv<span style="color: #339933;">;</span>
    int64_t pos<span style="color: #339933;">;</span>                            <span style="color: #666666; font-style: italic;">///&lt; byte position in stream, -1 if unknown                    </span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/**                                                                                                    
     * Time difference in AVStream-&gt;time_base units from the pts of this                                   
     * packet to the point at which the output from the decoder has converged                              
     * independent from the availability of previous frames. That is, the                                  
     * frames are virtually identical no matter if decoding started from                                   
     * the very first frame or from this keyframe.                                                         
     * Is AV_NOPTS_VALUE if unknown.                                                                       
     * This field is not the display duration of the current packet.                                       
     *                                                                                                     
     * The purpose of this field is to allow seeking in streams that have no                               
     * keyframes in the conventional sense. It corresponds to the                                          
     * recovery point SEI in H.264 and match_time_delta in NUT. It is also                                 
     * essential for some types of subtitle streams to ensure that all                                     
     * subtitles are correctly displayed after seeking.                                                    
     */</span>
    int64_t convergence_duration<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> AVPacket<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>At first sight (and somebody that knows ffmpeg better please correct me) it would appear that we have two timestamps being maintained, one is the presentation timestamp and one is the decompression timestamp. With that in mind, lets go back to the C code that blew the chunks&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>2633
2634
2635
2636
2637
2638
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">&amp;&amp;</span> st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">!=</span> AV_NOPTS_VALUE <span style="color: #339933;">&amp;&amp;</span> st<span style="color: #339933;">-&gt;</span>cur_dts <span style="color: #339933;">&gt;=</span> pkt<span style="color: #339933;">-&gt;</span>dts<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        av_log<span style="color: #009900;">&#40;</span>s<span style="color: #339933;">,</span> AV_LOG_ERROR<span style="color: #339933;">,</span>
               <span style="color: #ff0000;">&quot;st:%d error, non monotone timestamps %&quot;</span>PRId64<span style="color: #ff0000;">&quot; &gt;= %&quot;</span>PRId64<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
               st<span style="color: #339933;">-&gt;</span>index<span style="color: #339933;">,</span> st<span style="color: #339933;">-&gt;</span>cur_dts<span style="color: #339933;">,</span> pkt<span style="color: #339933;">-&gt;</span>dts<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As always, it helps to translate the stuff into English first and then try to see what it thinks it was doing&#8230; my real issue is <i>with the English</i>, &#8220;non monotone timestamps&#8221; may mean something to the gurus that maintain ffmpeg but to us users it doesn&#8217;t really mean anything&#8230; a classic case of writing error messages that mean nothing to the average user. The <a href="http://dictionary.reference.com/browse/monotone">definition of monotone</a> is such that it would appear to have absolutely nothing to do with time at all, and everything to do with sound. Interesting error message!</p>
<p><i>[UPDATE: 24/1/2010: Whilst reading "A Mathematical Theory of Communication" by C.E.Shannon I saw the term "monotonic function" on the very first page and a connection was made. Not being a working mathematician I looked it up and then realised that my ultimate conclusion to this problem was at least correct!  So, I will concede that the still somewhat obtuse error is kind of on track but I would suggest that it be changed to "non monotonic" or even better, something a little more verbose that leaves no guessing as to the cause of the error... a few more bytes of RAM is not going to kill ffmpeg methinks.]</i></p>
<p>So, with all that in our heads and knowing that &#8220;st&#8221; is a pointer to an AVStream structure and &#8220;pkt&#8221; is a pointer to an AVPacket structure let&#8217;s go&#8230;</p>
<p>1] if  the current stream decompression timestamp value is not zero<br />
2] and the current stream decompression timestamp value is not AV_NOPTS_VALUE<br />
3] and the current stream decompression timestamp value is bigger than<br />
4]     the current packet decompression timestamp value</p>
<p>[1] => on the principle of early bailing, this test will ensure that the remainder of the expression is NOT evaluated if the current value is 0, i.e. we have not presumably decoded any data yet.</p>
<p>[2] assuming [1] is true (we have data) then we check to see if the &#8216;dts&#8217; value has not been set to the value of AV_NOPTS_VALUE, which immunises the code from failing at this point. Question: Where, when and why would this value be set ? A quick look:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*c&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-iH</span> <span style="color: #ff0000;">&quot; = AV_NOPTS_VALUE&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \; <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> <span style="color: #660033;">-l</span>
<span style="color: #000000;">66</span></pre></div></div>

<p>Shows 66 assigments so it looks like it gets used to initialise a lot of things in a lot of places&#8230; but when does it get replaced with something else? Sigh. Another unknown.</p>
<p>[3] assuming that [1] and [2] are both true (ignoring the reasons why&#8230;) then we get to the real crux of the test for generating the error condition:  &#8220;Has the accumulated time exceeded the packet time, is this packet &#8216;late&#8217; with respect to the stream timestamp ?&#8221;</p>
<p>Given that we now know that timestamps are expected to be monotonic in the positive direction then it would indeed appear to indicate that somehow, the previous packet(s) have caused the current stream decompression time to have exceeded the time at which this packet thinks it was due. Why? I haven&#8217;t a clue. But it means that for some reason, during the decoding of the packets in my errant FLV file, there is something causing ffmpeg to think that at some point a packet has somehow fallen behind its decompression time and I have no idea why. The FLV is recorded from a live RTMP stream which may be part of the reason, the start time values seem to be the actual time of day rather than based at 00:00:00. Maybe that is the problem. Maybe I should try to &#8216;re-base&#8217; or &#8216;re-stripe&#8217; the internal time code before trying to add the GOP data ? Anybody, help!</p>
<p>I think. (The cause of all my problems.)</p>
<p>If anybody cares to expand / enlighten / correct me then please do as I still need to be able to deal with this file!</p>
<p>Cheers! <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/01/21/an-in-depth-analysis-of-ffmpeg-and-non-monotone-timestamps/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>XUGGLER and Java 1.6 on a Mac</title>
		<link>http://objitsu.com/blog/2010/01/06/xuggler-and-java-1-6-on-a-mac/</link>
		<comments>http://objitsu.com/blog/2010/01/06/xuggler-and-java-1-6-on-a-mac/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 13:26:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[xuggler]]></category>

		<guid isPermaLink="false">http://objitsu.com/blog/?p=542</guid>
		<description><![CDATA[You may have upgraded your Mac to Java 1.6 but you may *not be using it* as your default compiler!]]></description>
			<content:encoded><![CDATA[<p>I am currently investigating various open source API solutions for dealing with video in real-time for water-marking, captions etc etc and I have discovered xuggler.com. At first sight it seems awesome and so I downloaded and installed the kit and started my first program only to be greeted by this error :-</p>
<pre>
[scharles]xuggler $ javac -cp /usr/local/xuggler/share/java/jars/xuggle-xuggler.jar RW1.java
RW1.java:3: cannot access com.xuggle.mediatool.IMediaReader
bad class file: /usr/local/xuggler/share/java/jars/xuggle-xuggler.jar(com/xuggle/mediatool/IMediaReader.class)
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
import com.xuggle.mediatool.IMediaReader;
                            ^
1 error
[scharles]xuggler $ 
</pre>
<p>Having used Java since it came out, I have seen this error more than once(!) and I knew what the trouble was instantly but to remedy the situation took me a little longer to figure out but in the end it is quite simple.</p>
<h3>You What ?</h3>
<p>The cause of the error is simple, the JAR files supplied by Xuggler are compiled with Java version 1.6 but my Mac was still using 1.5 as the default, confirmed by asking the compiler to show its mettle :-</p>
<pre>
[scharles]xuggler $ javac -version
javac 1.5.0_22
</pre>
<p>So how does one sort this out on a Mac ? Easy peasy lemon squeazy&#8230; Inside the Applications folder is the Utilities folder. Inside there is the Java Preferences folder and upon launching it, I was presented with this which showed me that although I had 1.6 installed I was still using &#8216;J2SE 5.0&#8242; aka 1.5!<br />
<div id="attachment_548" class="wp-caption aligncenter" style="width: 681px"><a href="http://objitsu.com/blog/wp-content/uploads/2010/01/Picture-12.png"><img src="http://objitsu.com/blog/wp-content/uploads/2010/01/Picture-12.png" alt="Java 1.5 Active" title="Java 1.5 Active" width="671" height="515" class="size-full wp-image-548" /></a><p class="wp-caption-text">Java 1.5 Active</p></div></p>
<h3>And the solution is&#8230;.</h3>
<p>All we need to do then is drag the &#8216;J2SE 5.0&#8242; down to second position in both lists to keep things balanced and to avoid confusion about who is running what, like so.<br />
<div id="attachment_549" class="wp-caption aligncenter" style="width: 681px"><a href="http://objitsu.com/blog/wp-content/uploads/2010/01/Picture-13.png"><img src="http://objitsu.com/blog/wp-content/uploads/2010/01/Picture-13.png" alt="Java 1.6 Active" title="Java 1.6 Active" width="671" height="514" class="size-full wp-image-549" /></a><p class="wp-caption-text">Java 1.6 Active</p></div></p>
<p>Then issuing the version command this time shows&#8230;</p>
<pre>
[scharles]xuggler $ javac -version
javac 1.6.0_17
</pre>
<p>And lo and behold my xuggler programs start to build with no problems, so there you go, a simple solution to a somewhat mystifying initial error message.</p>
<p>Once I get my head around using xuggler and have to things that I want to have a pop at, firstly I want to use Clojure as the application language as I love LISP anyway and Clojure is perfect on the JVM, (IMHO!) and secondly I want to see if I can use the real-time aspects of xuggler <b>without red5</b> being required on the scene. I have used and played with red5 before and to be honest I just didn&#8217;t get along with it!! Maybe it has improved in the last eighteen months.</p>
<p>I also want to maybe modify the source code to the awesome <a href="http://savannah.nongnu.org/projects/flvstreamer" target="_blank">flvstreamer </a>application and adding some options to it if I need to although off the top of my head I think it can actually forward data. Whatever. We&#8217;ll see&#8230;</p>
<p>Enjoy. <img src='http://objitsu.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://objitsu.com/blog/2010/01/06/xuggler-and-java-1-6-on-a-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
