Advertisement
turlando

Untitled

Oct 16th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Device::SerialPort;
  5.  
  6. if ($#ARGV + 1 != 2) {
  7. print "Usage: $0 port filename\n";
  8. print "Example: $0 /dev/ttyASM0 money.txt\n";
  9. exit 1;
  10. }
  11.  
  12. my $file = $ARGV[0];
  13. my $dev = $ARGV[1];
  14.  
  15. if (!-e $file || !-e $dev) {
  16. print "File or brain not found.";
  17. exit 1;
  18. }
  19.  
  20. require "notes.pl";
  21.  
  22. my $arduino = DeviceSerialPort->new($dev);
  23. $arduino->baudrate(9600);
  24. $arduino->databits(8);
  25. $arduino->parity("none");
  26. $arduino->stopbits(1);
  27.  
  28. open NOTES, "$file";
  29.  
  30. while (<NOTES>) {
  31. chomp; # no newline
  32. s/#.*//; # no comments
  33. s/^\s+//; # no leading white
  34. s/\s+$//; # no trailing white
  35. next unless length; # anything left?
  36. if ($_ =~ m/^TEMPO/) {
  37. my $tempo = split(/\s+/, $_, -1);
  38. } else {
  39. my @tone = split(/\s+/, $_);
  40. }
  41. my $note = $notes{$tone[0]};
  42. my $duration = $tone[1]*$tempo;
  43. print "Tempo is $tempo.";
  44. print "Playing $tone[0] (\@$note Hz) for $tone[1] units ($duration ms).";
  45. while ($note > 255) {
  46. $arduino->write(chr(255));
  47. $note -= 255;
  48. }
  49. $arduino->write($note);
  50. $arduino->write(";");
  51. sleep($duration);
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement