Big Mouth Billy Bass automation library

Dependents:   BillyBass_with_SD

Files at this revision

API Documentation at this revision

Comitter:
bikeNomad
Date:
Tue Jun 18 00:07:47 2013 +0000
Parent:
0:84aaade0de8f
Child:
2:eaba75af0f0d
Commit message:
don't store songs in list.

Changed in this revision

config.hpp Show annotated file Show diff for this revision Revisions of this file
song.cpp Show annotated file Show diff for this revision Revisions of this file
song.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/config.hpp	Mon Jun 17 22:17:59 2013 +0000
+++ b/config.hpp	Tue Jun 18 00:07:47 2013 +0000
@@ -22,6 +22,9 @@
         BASS_DIRECTORY_LENGTH + 1 \
         + MAX_BASENAME_LENGTH + 1
 
+#define MAX_ACTIONS_LINES_PER_SONG 60
+#define MAX_ACTIONS_PER_SONG MAX_ACTIONS_LINES_PER_SONG*2
+
 // Sample configuration
 typedef int16_t Sample_t;   // 16-bit raw, LE samples
 const float SAMPLE_RATE_HZ = 8000.0;
--- a/song.cpp	Mon Jun 17 22:17:59 2013 +0000
+++ b/song.cpp	Tue Jun 18 00:07:47 2013 +0000
@@ -15,8 +15,6 @@
 char const *Song::sampleExtension = "raw";
 // class static
 unsigned const Song::NO_FISH = MAX_FISH + 1;
-// class static
-std::list<Song*> Song::songs;
 
 // _name is relative to BASS_DIRECTORY
 // class static
@@ -31,11 +29,11 @@
         pc.printf("parseFilename(%s) failed\r\n", _name);
         goto on_error;
     }
+    pc.printf("parsed filename OK\r\n");
     if (! s->readActions()) {
         pc.printf("readActions(%s) failed\r\n", _name);
         goto on_error;
     }
-    songs.push_back(s);
 
     return s;
 
@@ -44,36 +42,6 @@
     return 0;
 }
 
-// class static
-void Song::clearAllSongs()
-{
-    for (std::list<Song *>::const_iterator song = songs.begin(); song != songs.end(); song++)
-        delete *song;
-    songs.clear();
-}
-
-// class static
-unsigned Song::readAllSongs(DIR *bassDir)
-{
-    if (!bassDir) return 0;
-    clearAllSongs();
-
-    while (dirent *dir = bassDir->readdir()) {
-        pc.printf("Reading %s\r\n", dir->d_name);
-        unsigned namelen = strlen(dir->d_name);
-        if (namelen > 9 && !strcasecmp(dir->d_name + namelen - 3, sampleExtension)) {
-            Song *song = Song::newSong(dir->d_name);
-            if (!song) {
-                pc.printf("ERROR reading %s\r\n", dir->d_name);
-            } else {
-                song->print(pc);
-            }
-        }
-    }
-
-    return songs.size();
-}
-
 bool Song::parseFilename(char const *_name)
 {
     if (strlen(_name) > MAX_BASENAME_LENGTH)
@@ -117,11 +85,14 @@
 
 bool Song::readActions()
 {
+    pc.printf("reading actions of %s\r\n", getTextFileName());
+
     FILE *txtfile = fopen(getTextFileName(), "r");
     if (!txtfile)  {
         pc.printf("can't open %s\r\n", getTextFileName());
         return false;
-    }
+    } else
+        pc.printf("opened %s OK\r\n", getTextFileName());
     bool retval = false;
 
     BillyBass *bass = BillyBass::bassNumber(whichFish);
@@ -131,16 +102,18 @@
     }
 
     // read actions from file
+    static char textFileBuffer[ 2048 ];
+    memset(textFileBuffer, 0, sizeof(textFileBuffer));
+    int nread = fread(textFileBuffer, 1, sizeof(textFileBuffer), txtfile);
+    pc.printf("Read %d\r\n", nread);
+    if (nread <= 0 || nread == sizeof(textFileBuffer)) {
+        goto done;
+    }
     unsigned line = 1;
-    char actionLine[ MAX_ACTION_LINE_LENGTH + 1 ];
-    while (true) {
-        if (!fgets(actionLine, sizeof(actionLine), txtfile))
-            break;
-
-        // delete trailing whitespace
-        char *p = actionLine + strlen(actionLine) - 1;
-        while (isspace(*p)) *p-- = 0;
-
+    for (char *actionLine = strtok(textFileBuffer, "\r\n");
+            actionLine;
+            actionLine = strtok(0, "\r\n"), line++) {
+        char *p;
         float startTime = strtof(actionLine, &p);
         if (p == actionLine)
             goto done;
@@ -150,29 +123,23 @@
         if (q == p)
             goto done;
 
-        while (isspace(*q)) q++;
-
         char const *outName;
-        DigitalOut *out = bass->outputNamed(q, &outName);
+        DigitalOut *out = bass->outputNamed(++q, &outName);
         if (!out) {
             pc.printf("%s line %d: bad outname \"%s\"\r\n", getTextFileName(), line, q);
             goto done;
         }
         pc.printf("%d add %f %f %s\r\n", line, startTime, endTime, outName);
 
-        actions.push_back(Action(startTime, true, out, outName));
-        actions.push_back(Action(endTime, false, out, outName));
-
-        line++;
+        // actions.push_back(Action(startTime, true, out, outName));
+        // actions.push_back(Action(endTime, false, out, outName));
     }
 
     std::sort(actions.begin(), actions.end()); // sort actions by time
     retval = true;
 
 done:
-    if (!retval) {
-        pc.printf("Error reading action from \"%s\"\r\n", actionLine);
-    }
+
     fclose(txtfile);
     return retval;
 }
--- a/song.hpp	Mon Jun 17 22:17:59 2013 +0000
+++ b/song.hpp	Tue Jun 18 00:07:47 2013 +0000
@@ -10,20 +10,16 @@
 class Song
 {
 public:
-
     typedef std::vector<Action> actions_t;
 
     // _name is relative to BASS_DIRECTORY
     // return a pointer to a fully read-in Song if valid
     // also adds new song to songs
     static Song *newSong(char const *_name);
-    static std::list<Song*> songs;
-    static unsigned readAllSongs(DIR *bassDir);
-    static void clearAllSongs();
 
-    Song() : sequenceNumber(0), whichFish(NO_FISH), basename(0), extension(0)  {
+    Song() : sequenceNumber(0), whichFish(NO_FISH), basename(0), extension(0) {
         fullname[0] = 0;
-        actions.reserve(60);
+        actions.reserve(MAX_ACTIONS_PER_SONG);
     }
 
     bool isValid() const {