<?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>Dual Elephants &#187; Peggy 2</title>
	<atom:link href="http://www.adebenham.com/tag/peggy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adebenham.com</link>
	<description>Always keep a spare for redundancy</description>
	<lastBuildDate>Sun, 21 Feb 2010 22:18:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MusicXML/MIDI for Arduino</title>
		<link>http://www.adebenham.com/2010/02/musicxmlmidi-for-arduino/</link>
		<comments>http://www.adebenham.com/2010/02/musicxmlmidi-for-arduino/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 00:47:49 +0000</pubDate>
		<dc:creator>cjd</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Peggy 2]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[music]]></category>

		<guid isPermaLink="false">http://www.adebenham.com/?p=49</guid>
		<description><![CDATA[For a recent project I wanted to add music.
Being that I am not musically talented in any way I figured the best way to do this was to generate the music from a midi file.
To help with this I wrote a converter that would take a MusicXML file and output an include file.
It simply reads [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project I wanted to add music.</p>
<p>Being that I am not musically talented in any way I figured the best way to do this was to generate the music from a midi file.</p>
<p>To help with this I wrote a converter that would take a MusicXML file and output an include file.</p>
<p>It simply reads the XML and outputs an array where each note is stored in two uint16 values &#8211; the first is the note name as a defined in pitches.h (ie NOTE_C4), the second is the duration in milliseconds.  The entire array is stored in PROGMEM so as to not use up too much ram on the AVR.</p>
<p>The script is as follows (or download from <a href="/files/arduino/convert_xml.pl">here</a>)</p>
<pre>#!/usr/bin/perl
# Convert a single-channel musicxml file to include file for arduino
# Pass the xml filename on the commandline and a .h file will be generated
# with the same name (replacing .xml with .h)

# Needs XML::Mini perlmodule (libxml-mini-perl package in debian/ubuntu)
use XML::Mini::Document;
my $size = 0;

my $name = $ARGV[0];
$name =~ s/\.xml$//;
open(OUT, "&gt;".$name.".h");
print OUT "PROGMEM prog_uint16_t ".$name."Tune[] = {\n";

my $xmldoc = XML::Mini::Document-&gt;new();
$xmldoc-&gt;fromFile($ARGV[0]);
my $xmlHash = $xmldoc-&gt;toHash();
my $measures = $xmlHash-&gt;{'score-partwise'}-&gt;{'part'}-&gt;{'measure'};
foreach my $measure (@$measures) {
  my $notes=$measure-&gt;{'note'};
  foreach (@$notes) {
    my $note = "";
    if ($_-&gt;{'pitch'}) {
      if (defined $_-&gt;{'accidental'}) {
        if ($_-&gt;{'accidental'} eq "sharp") {
          $_-&gt;{'pitch'}-&gt;{'step'}=$_-&gt;{'pitch'}-&gt;{'step'}."S";
        } elsif ($_-&gt;{'accidental'} eq "flat") {
          $_-&gt;{'pitch'}-&gt;{'step'} =~ tr/A-F/FA-E/;
          $_-&gt;{'pitch'}-&gt;{'step'}=$_-&gt;{'pitch'}-&gt;{'step'}."S";
        }
      }
      $note = "NOTE_".$_-&gt;{'pitch'}-&gt;{'step'}.$_-&gt;{'pitch'}-&gt;{'octave'};
    } else {
      $note = "NOTE_00";
    }
    my $duration = $_-&gt;{'duration'};
    print OUT $note.", ".$duration.", ";
    $size++;
  }
  print OUT "\n";
}
print OUT "};\n";
print OUT "byte ".$name."TuneSteps = ".$size.";\n";
print "total memory usage:".(($size*4)+1)." bytes\n";
close OUT;
</pre>
<p>To actually use this generated include file I created a function &#8216;playMelody&#8217; which is called regularly during the running of a sketch.</p>
<p>Each time it runs it checks if it is time to play the next note, if so then it reads the next note from the array,calls &#8216;tone&#8217; to play it and then sets a variable to say when the next note should be played.  If it is not yet time to play the next note then it quickly returns (there is not much latency added by calling the function so it&#8217;s okay to call too often)</p>
<pre><span style="color: #cc6600;">boolean</span> playMelody()
{
  <span style="color: #cc6600;">boolean</span> retVal=<span style="color: #cc6600;">true</span>;
  <span style="color: #cc6600;">if</span> (<span style="color: #cc6600;">millis</span>() &gt; time) {
    <span style="color: #cc6600;">int</span> toneVal = 0;
    <span style="color: #cc6600;">int</span> duration = 0;
    <span style="color: #cc6600;">if</span> (tuneStep &gt;= SmoothCriminalTuneSteps) {
      retVal=<span style="color: #cc6600;">false</span>;
      tuneStep=0;
    }
    toneVal=pgm_read_word_near(SmoothCriminalTune+(tuneStep*2));
    duration = pgm_read_word_near(SmoothCriminalTune+(tuneStep*2)+1);
    tuneStep++;
    <span style="color: #cc6600;">if</span> (toneVal) <span style="color: #cc6600;">tone</span>(speakerPin,toneVal,duration*2);
    time=<span style="color: #cc6600;">millis</span>()+(duration*2)+5;
  }
  <span style="color: #cc6600;">return</span> retVal;
}
</pre>
<p>I have a sample sketch which uses this to play a short melody loop as <a href="/files/arduino/music.zip">music.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adebenham.com/2010/02/musicxmlmidi-for-arduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peggy 2 multi-game sketch v0.2</title>
		<link>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-2/</link>
		<comments>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-2/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 00:28:23 +0000</pubDate>
		<dc:creator>cjd</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Peggy 2]]></category>

		<guid isPermaLink="false">http://www.adebenham.com/?p=48</guid>
		<description><![CDATA[I&#8217;ve done a lot more work on the multi-game sketch for my Peggy2 &#8211; mostly to add functionality and fix up a few &#8216;issues&#8217;
See the original post  for more details on what is included
Download it from here When compiled it uses 13808 bytes so there only a bit of room to spare.
I&#8217;ve added a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve done a lot more work on the multi-game sketch for my Peggy2 &#8211; mostly to add functionality and fix up a few &#8216;issues&#8217;<br />
See the original post <a href="http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-1/">here</a> for more details on what is included<br />
Download it from <a href="/files/arduino/peggy2_games_0.2.zip">here</a> When compiled it uses 13808 bytes so there only a bit of room to spare.<br />
I&#8217;ve added a two player version of pong (use up/down buttons to control player two &#8211; I&#8217;ve made a separate plug-in controller for player two to make things easier to use)<br />
I&#8217;ve fixed up the &#8216;breakout&#8217; clone so it actually can be considered a &#8216;game&#8217; <img src='http://www.adebenham.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I also added a &#8216;demo&#8217; game which just lights up all leds with a nice grayscale pattern than can be moved around via the direction buttons (press select to stop the motion)<br />
The other main thing is that it now has sound!<br />
With the release of arduino-0018 they added a function &#8216;tone&#8217; to generate square waves on a digital pin. I connected a piezo speaker (from a headphone) to ADC5 and generated the tones on that pin.<br />
There is intro music at the game select screen, music when displaying the score and various bips and beeps while playing the games.<br />
To make the music I wrote a small perl script to convert MusicXML files to a suitable include file (will be documented in a separate post)<br />
Adding the music and extra games meant I was hitting the space limits in the AVR so there is a bit of &#8216;dodgyness&#8217; in the code so that it would fit.</p>
<p>See the original post <a href="http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-1/">here</a> for more details on what is included</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peggy 2 multi-game sketch v0.1</title>
		<link>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-1/</link>
		<comments>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-1/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 22:26:30 +0000</pubDate>
		<dc:creator>cjd</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Peggy 2]]></category>

		<guid isPermaLink="false">http://www.adebenham.com/?p=17</guid>
		<description><![CDATA[I had such fun writing the simple &#8217;snake&#8217; game for my peggy 2 that I wrote a bunch of other games for it as well.
At the moment there is Snake, Breakout, Pong and Race.
You can download the sketch as peggy2_games_0.1.zip
When compiled it takes up 11432 bytes (it includes the &#8216;Tone&#8217; library as I have started [...]]]></description>
			<content:encoded><![CDATA[<p>I had such fun writing the simple &#8217;snake&#8217; game for my peggy 2 that I wrote a bunch of other games for it as well.</p>
<p>At the moment there is Snake, Breakout, Pong and Race.</p>
<p>You can download the sketch as <a href="/files/arduino/peggy2_games_0.1.zip">peggy2_games_0.1.zip</a></p>
<p>When compiled it takes up 11432 bytes (it includes the &#8216;Tone&#8217; library as I have started adding sound to the sketch &#8211; connect speaker to ADC5 to hear it)</p>
<h2>Starting peggy2_game</h2>
<p><a href="http://www.adebenham.com/wp-content/uploads/2010/02/game_menu.jpg"><img class="aligncenter size-full wp-image-19" title="Game menu" src="http://www.adebenham.com/wp-content/uploads/2010/02/game_menu.jpg" alt="Game menu" width="300" height="327" /></a></p>
<p>When you turn on the peggy2 a menu is presented displaying the available games &#8211; you can select from them using the up/down buttons and select with the &#8217;select&#8217; or &#8216;any&#8217; button on the left of the peggy2.  Only three game names fit on the screen at once so the entire display scrolls up/down when changing game (and highlights the current selection)  To change games press the reset button.</p>
<h2>Playing the games</h2>
<h3>Snake</h3>
<p style="text-align: center;"><a href="http://www.adebenham.com/wp-content/uploads/2010/02/snake.jpg"><img class="alignnone size-full wp-image-21" title="Snake" src="http://www.adebenham.com/wp-content/uploads/2010/02/snake.jpg" alt="Snake" width="300" height="309" /></a><a href="http://www.adebenham.com/wp-content/uploads/2010/02/level_select.jpg"><img class="alignnone size-full wp-image-20" title="Level select" src="http://www.adebenham.com/wp-content/uploads/2010/02/level_select.jpg" alt="Level select" width="300" height="307" /></a></p>
<p>When the game first starts you can choose which level to start at.  There are four different boards available &#8211; these boards are re-used in a loop just at a higher speed for later levels.</p>
<p>Eat the &#8216;apples&#8217; to get points, don&#8217;t hit yourself or walls</p>
<p>Arrow buttons control movement of the snake.</p>
<p><a href="http://www.adebenham.com/wp-content/uploads/2010/02/score.jpg"><img class="aligncenter size-full wp-image-22" title="Score screen" src="http://www.adebenham.com/wp-content/uploads/2010/02/score.jpg" alt="Score screen" width="300" height="299" /></a></p>
<h3>Pong</h3>
<p><a href="http://www.adebenham.com/wp-content/uploads/2010/02/pong.jpg"><img class="aligncenter size-full wp-image-23" title="Pong game" src="http://www.adebenham.com/wp-content/uploads/2010/02/pong.jpg" alt="Pong game" width="300" height="292" /></a></p>
<p>Hit the ball back, get a point if the AI misses, AI gets the point if you miss</p>
<p>Left/right buttons control movement of the bottom paddle (one player only for the moment)</p>
<h3>Break</h3>
<p><a href="http://www.adebenham.com/wp-content/uploads/2010/02/break.jpg"><img class="aligncenter size-full wp-image-24" title="Breakout game" src="http://www.adebenham.com/wp-content/uploads/2010/02/break.jpg" alt="Breakout game" width="300" height="284" /></a></p>
<p>Not much here yet, just displays a bouncing ball + blocks to hit</p>
<p>Left/right buttons control movement of the paddle</p>
<h3>Race</h3>
<p><a href="http://www.adebenham.com/wp-content/uploads/2010/02/race.jpg"><img class="aligncenter size-full wp-image-25" title="Race game" src="http://www.adebenham.com/wp-content/uploads/2010/02/race.jpg" alt="Race game" width="300" height="280" /></a></p>
<p>Avoid the walls.  One point every 25 blocks driven past, walls get narrower every 25 points</p>
<p>Left/right buttons control movement of the car.</p>
<p>peggy2_games.zip</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adebenham.com/2010/02/peggy-2-multi-game-sketch-v0-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peggy 2 Character display</title>
		<link>http://www.adebenham.com/2010/01/peggy-2-character-display/</link>
		<comments>http://www.adebenham.com/2010/01/peggy-2-character-display/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 03:48:31 +0000</pubDate>
		<dc:creator>cjd</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Peggy 2]]></category>

		<guid isPermaLink="false">http://www.adebenham.com/?p=15</guid>
		<description><![CDATA[As mentioned in the  post I wrote a bunch of small utility functions to display a string on the peggy 2.
Each character is stored as a 24-bit number (held in a 32bit uint32) where each 4 bits indicates the on/off status for a row of 4 pixels. This results in allowing for a 4&#215;6 pixel [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned in the <a href="http://www.adebenham.com/2010/01/peggy-2-snake/">Peggy 2 Snake</a> post I wrote a bunch of small utility functions to display a string on the peggy 2.</p>
<p>Each character is stored as a 24-bit number (held in a 32bit uint32) where each 4 bits indicates the on/off status for a row of 4 pixels. This results in allowing for a 4&#215;6 pixel character</p>
<p>For example the character for the number &#8216;0&#8242; is stored as 0&#215;699996 which when spread out looks like:</p>
<pre>0<strong>11</strong>0
<strong>1</strong>00<strong>1</strong>
<strong>1</strong>00<strong>1</strong>
<strong>1</strong>00<strong>1</strong>
<strong>1</strong>00<strong>1</strong>
0<strong>11</strong>0</pre>
<p>By doing this I can store 0-9 and A-Z in only 144bytes.<br />
To save RAM I put this in program memory using: PROGMEM prog_uint32_t chars[37] ﻿</p>
<p>To write a character to the display I use a function &#8220;show_char&#8221; (note I use a special function &#8217;setPixel&#8217; to set the individual pixels to a certain brightness level &#8211; but if you are not using grayscale then the call can be replaced by standard frame.SetPixel calls)</p>
<pre>void show_char(char Character, byte x, byte y)
{
  byte charNum = 36;
  if ((Character &gt;= '0') &amp;&amp; (Character &lt;= '9')) {
    charNum=Character-48;
  }
  if ((Character &gt;= 'A') &amp;&amp; (Character &lt;= 'Z')) {
    charNum=Character-55;
  }
  unsigned long w;
  w = pgm_read_dword_near(chars + charNum);
  for (byte i=0; i&lt;6; i++) {
    byte offset = 4*(5-i);
    byte b = w &gt;&gt; offset; // get four bits
    b=b &amp; 0xF;
    if (b &amp; B1000) setPixel(x,y+i,7);
    if (b &amp; B0100) setPixel(x+1,y+i,7);
    if (b &amp; B0010) setPixel(x+2,y+i,7);
    if (b &amp; B0001) setPixel(x+3,y+i,7);
  }
}
</pre>
<p>To make things a bit easier to display a string I also created a quick wrapper function &#8216;write_string&#8217;</p>
<pre>void write_string(char string[], byte x, byte y)
{
  byte i=0;
  while (string[i] != '\0') {
    show_char(string[i], x+(5*i), y);
    i++;
  }
}
</pre>
<p>Full usage/source can be found in the peggy 2 snake code</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adebenham.com/2010/01/peggy-2-character-display/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peggy 2 Snake</title>
		<link>http://www.adebenham.com/2010/01/peggy-2-snake/</link>
		<comments>http://www.adebenham.com/2010/01/peggy-2-snake/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 03:32:30 +0000</pubDate>
		<dc:creator>cjd</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Peggy 2]]></category>

		<guid isPermaLink="false">http://www.adebenham.com/?p=13</guid>
		<description><![CDATA[For Christmas my wife gave me a peggy 2 and I finally got around to building it.
It has 24&#215;25 White LEDs with a row of 25 Red LEDs across the bottom.
By having that bottom row a different colour I can use that section for displaying score or other information
The first application I wrote for it [...]]]></description>
			<content:encoded><![CDATA[<p>For Christmas my wife gave me a <a href="http://evilmadscience.com/tinykitlist/75">peggy 2</a> and I finally got around to building it.</p>
<div id="attachment_14" class="wp-caption aligncenter" style="width: 209px"><a href="http://www.adebenham.com/wp-content/uploads/2010/01/Peggy2_snake.jpg"><img class="size-medium wp-image-14" title="Peggy2_snake" src="http://www.adebenham.com/wp-content/uploads/2010/01/Peggy2_snake-199x300.jpg" alt="Peggy 2 running Snake" width="199" height="300" /></a><p class="wp-caption-text">Peggy 2 running Snake</p></div>
<p>It has 24&#215;25 White LEDs with a row of 25 Red LEDs across the bottom.</p>
<p>By having that bottom row a different colour I can use that section for displaying score or other information</p>
<p>The first application I wrote for it the good old game of &#8216;Snake&#8217;.  After writing a quick version of the game I realised it needed a bit more &#8216;flair&#8217; and so added the ability to display an intro screen and score screen.  To make this easier I wrote a bunch of utility functions to display characters that are stored in an array. I&#8217;ll describe how this works in a separate post as <a href="http://www.adebenham.com/2010/01/peggy-2-character-display/">Peggy 2 Character display</a>.</p>
<p>The game has multiple levels (where walls are put up in different spots and the speed is increased).  All up the program takes up 5258 bytes so fits on an atmega168 with room to spare.</p>
<p>Source code is available as <a href="/files/arduino/peggy2_snake.zip">peggy2_snake.zip</a></p>
<p>If you don&#8217;t have all the leds soldered on you can change gameMinX, gameMaxX, gameMinY, gameMaxY and score* to set the area used to play/display score</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adebenham.com/2010/01/peggy-2-snake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
