Console-based podcaster – updated

It has been a while since I first posted my version of bashpodder (a console-based podcaster in a single bash script)

Since then a bunch of small fixes have been added to the script to better handle ‘odd’ rss feeds and also to clean up the output.

The configuration of the script now sits in the file ~/.bashpodder  Inside this file you can specify where the logs go to and where the files download to (makes it easier to run the script automatically)

Thanks to Mobilediesel it now also doesn’t create the temporary xslt file and there is the option to use ‘-a feedurl [items]’ to add a feed to the config file.  The [items] parameter is optional and when used will cause bashpodder to mark all items in the feed as already downloaded except the last [items] items.  This is useful when added a new feed so that it doesn’t try to download every item in the feed since day one 🙂

The updated script can be downloaded as bashpodder.sh and an example config file as .bashpodder

For more details on the script see the original post here

MusicXML/MIDI for Arduino

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 the XML and outputs an array where each note is stored in two uint16 values – 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.

The script is as follows (or download from here)

#!/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, ">".$name.".h");
print OUT "PROGMEM prog_uint16_t ".$name."Tune[] = {\n";

my $xmldoc = XML::Mini::Document->new();
$xmldoc->fromFile($ARGV[0]);
my $xmlHash = $xmldoc->toHash();
my $measures = $xmlHash->{'score-partwise'}->{'part'}->{'measure'};
foreach my $measure (@$measures) {
  my $notes=$measure->{'note'};
  foreach (@$notes) {
    my $note = "";
    if ($_->{'pitch'}) {
      if (defined $_->{'accidental'}) {
        if ($_->{'accidental'} eq "sharp") {
          $_->{'pitch'}->{'step'}=$_->{'pitch'}->{'step'}."S";
        } elsif ($_->{'accidental'} eq "flat") {
          $_->{'pitch'}->{'step'} =~ tr/A-F/FA-E/;
          $_->{'pitch'}->{'step'}=$_->{'pitch'}->{'step'}."S";
        }
      }
      $note = "NOTE_".$_->{'pitch'}->{'step'}.$_->{'pitch'}->{'octave'};
    } else {
      $note = "NOTE_00";
    }
    my $duration = $_->{'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;

To actually use this generated include file I created a function ‘playMelody’ which is called regularly during the running of a sketch.

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 ‘tone’ 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’s okay to call too often)

boolean playMelody()
{
  boolean retVal=true;
  if (millis() > time) {
    int toneVal = 0;
    int duration = 0;
    if (tuneStep >= SmoothCriminalTuneSteps) {
      retVal=false;
      tuneStep=0;
    }
    toneVal=pgm_read_word_near(SmoothCriminalTune+(tuneStep*2));
    duration = pgm_read_word_near(SmoothCriminalTune+(tuneStep*2)+1);
    tuneStep++;
    if (toneVal) tone(speakerPin,toneVal,duration*2);
    time=millis()+(duration*2)+5;
  }
  return retVal;
}

I have a sample sketch which uses this to play a short melody loop as music.zip

Peggy 2 multi-game sketch v0.2

I’ve done a lot more work on the multi-game sketch for my Peggy2 – mostly to add functionality and fix up a few ‘issues’
See the original post here 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’ve added a two player version of pong (use up/down buttons to control player two – I’ve made a separate plug-in controller for player two to make things easier to use)
I’ve fixed up the ‘breakout’ clone so it actually can be considered a ‘game’ 🙂
I also added a ‘demo’ 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)
The other main thing is that it now has sound!
With the release of arduino-0018 they added a function ‘tone’ 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.
There is intro music at the game select screen, music when displaying the score and various bips and beeps while playing the games.
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)
Adding the music and extra games meant I was hitting the space limits in the AVR so there is a bit of ‘dodgyness’ in the code so that it would fit.

See the original post here for more details on what is included

Peggy 2 multi-game sketch v0.1

I had such fun writing the simple ‘snake’ 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 ‘Tone’ library as I have started adding sound to the sketch – connect speaker to ADC5 to hear it)

Starting peggy2_game

Game menu

When you turn on the peggy2 a menu is presented displaying the available games – you can select from them using the up/down buttons and select with the ‘select’ or ‘any’ 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.

Playing the games

Snake

SnakeLevel select

When the game first starts you can choose which level to start at.  There are four different boards available – these boards are re-used in a loop just at a higher speed for later levels.

Eat the ‘apples’ to get points, don’t hit yourself or walls

Arrow buttons control movement of the snake.

Score screen

Pong

Pong game

Hit the ball back, get a point if the AI misses, AI gets the point if you miss

Left/right buttons control movement of the bottom paddle (one player only for the moment)

Break

Breakout game

Not much here yet, just displays a bouncing ball + blocks to hit

Left/right buttons control movement of the paddle

Race

Race game

Avoid the walls.  One point every 25 blocks driven past, walls get narrower every 25 points

Left/right buttons control movement of the car.

peggy2_games.zip