Ned Konz / Mbed 2 deprecated BillyBass_with_SD

Dependencies:   SDFileSystem mbed BillyBass

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "billybass.hpp"
00002 #include "SDFileSystem.h"
00003 #include "song.hpp"
00004 #include "player.hpp"
00005 #include "action.hpp"
00006 #include <ctype.h>
00007 
00008 //                 tailPin,  mouthPin, bodyPin   inverted
00009 // BillyBass testBass(LED_RED, LED_GREEN, LED_BLUE, true);
00010 
00011 //                 tailPin,        mouthPin,      bodyPin
00012 BillyBass bass1(BASS1_TAIL,     BASS1_MOUTH,   BASS1_BODY);
00013 //                 tailPin,        mouthPin,      bodyPin
00014 BillyBass bass2(BASS2_TAIL,     BASS2_MOUTH,   BASS2_BODY);
00015 DigitalIn button1(BASS1_BUTTON);    // J3/4
00016 DigitalIn button2(BASS2_BUTTON);   // J3/2
00017 
00018 // Analog:
00019 // GND   J3/14
00020 // VrefH J3/16
00021 AnalogOut speaker(PTE30);              // J10/11
00022 
00023 //                 MOSI,    MISO,    SCLK,    CS, name
00024 SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCLK, SD_CS, SD_NAME);
00025 Serial pc(USBTX, USBRX);
00026 
00027 Timer t;
00028 
00029 void serialIrqHandler()
00030 {
00031     int c = pc.getc();
00032     bool isLower = islower(c);
00033     c = toupper(c);
00034     DigitalOut *out = 0;
00035     float elapsed = t.read();
00036     t.reset();
00037     pc.printf("%d %f\r\n", (int)!isLower, elapsed);
00038 
00039     switch (c) {
00040         case 'H':
00041         case 'B':
00042             out = bass1.outputNamed("body");
00043             break;
00044         case 'T':
00045             out = bass1.outputNamed("tail");
00046             break;
00047         case 'M':
00048             out = bass1.outputNamed("mouth");
00049         default:
00050             break;
00051     }
00052     if (out) {
00053         if (isLower)
00054             out->write(0);
00055         else
00056             out->write(1);
00057     }
00058 }
00059 
00060 int main()
00061 {
00062     SongPlayer player;
00063     pc.baud(SERIAL_BAUD);
00064     t.start();
00065     pc.attach(serialIrqHandler);
00066     button1.mode(PullUp);
00067     button2.mode(PullUp);
00068 
00069     fprintf(stderr, "*** REBOOT ***\r\n");
00070 
00071     for (;;) {
00072         // read the directory
00073         DIR *bassDir = 0;
00074         while (!bassDir) {
00075             if ((bassDir = opendir(BASS_DIRECTORY)) != 0)
00076                 break;
00077             pc.printf("Error opening " BASS_DIRECTORY "\r\n");
00078             wait(1.0);
00079         }
00080 
00081         while (dirent *d = readdir(bassDir)) {
00082             Song *song = Song::newSong(d->d_name);
00083             if (song) {
00084                 fprintf(stderr, "Waiting to play %s\r\n", song->getSampleFileName());
00085                 while (!(!button1 || !button2))
00086                     wait(0.1);
00087                 player.playEntireSong(song);
00088                 fprintf(stderr, "total length: %f done: %u\r\n", player.timeInSong, player.actionsDone);
00089             }
00090         }
00091         closedir(bassDir);
00092         fprintf(stderr, "Done.\r\n");
00093     }
00094 }
00095