Big Mouth Billy Bass automation library

Dependents:   BillyBass_with_SD

Revision:
7:dba9221acf48
Parent:
6:ea8136eb6976
Child:
8:ad0c038ebfc1
--- a/song.cpp	Thu Jun 20 03:04:36 2013 +0000
+++ b/song.cpp	Thu Jun 20 04:10:22 2013 +0000
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <algorithm>
+#include <math.h>
 
 #include "billybass.hpp"
 #include "song.hpp"
@@ -81,6 +82,24 @@
     return false;
 }
 
+bool Song::addAction(float _time, int _state, DigitalOut* _out, char _code)
+{
+    Action *priorAction = (numActions > 0) ? actions + numActions - 1 : 0;
+    if (priorAction ) {
+        if (priorAction->output == _out) {
+            if (priorAction->actionTime >= _time && priorAction->desiredState != _state)
+                return true;
+            if (priorAction->actionTime < _time && priorAction->desiredState == _state) {
+                priorAction->actionTime = _time;
+                return true;
+            }
+        }
+    }
+    if (numActions >= MAX_ACTIONS_PER_SONG) return false;
+    actions[numActions++].set(_time, _state, _out, _code);
+    return true;
+}
+
 bool Song::readActions()
 {
     fprintf(stderr, "reading actions of %s\r\n", getTextFileName());
@@ -122,18 +141,32 @@
             goto done;
 
         char const *outName;
-        DigitalOut *out = bass->outputNamed(++q, &outName);
+        float onDelay, offDelay, minOnTime;
+        DigitalOut *out = bass->outputNamed(++q, &outName, &onDelay, &offDelay, &minOnTime);
         if (!out) {
             fprintf(stderr, "%s line %d: bad outname \"%s\"\r\n", getTextFileName(), line, q);
             goto done;
         }
-        // fprintf(stderr, "%d add %f %f %s\r\n", line, startTime, endTime, outName);
+
+        startTime -= onDelay;
+        startTime -= remainder(startTime, SECONDS_PER_CHUNK);
+        if (startTime < 0.0) startTime = 0.0;
+
+        endTime -= offDelay;
+        endTime += remainder(endTime, SECONDS_PER_CHUNK);
+        if (endTime < startTime + minOnTime)
+            endTime = startTime + minOnTime;
+
+        fprintf(stderr, "%d %f %f %s\r\n", line, startTime, endTime, outName);
 
         addAction(startTime, bass->onState(), out, toupper(outName[0]));
         addAction(endTime, bass->offState(), out, outName[0]);
     }
     fprintf(stderr, "Added %d actions\r\n", numActions);
     qsort(actions, numActions, sizeof(Action), &Action::compare);
+    for (int i = 0; i < numActions; i++ ) {
+        fprintf(stderr, "%f %c\r\n", actions[i].actionTime, actions[i].code);
+    }
     retval = true;
 
 done: