Big Mouth Billy Bass automation library

Dependents:   BillyBass_with_SD

Revision:
3:6c91a6232c4a
Parent:
0:84aaade0de8f
Child:
4:f009306756b3
--- a/billybass.hpp	Tue Jun 18 06:12:48 2013 +0000
+++ b/billybass.hpp	Tue Jun 18 13:11:07 2013 +0000
@@ -6,29 +6,42 @@
 #include <list>
 #include <vector>
 #include <cmath>
-
-extern AnalogOut speaker;
-extern Serial pc;
+#include "mbed_debug.h"
 
 class BillyBass
 {
 public:
 
-    BillyBass(PinName tailPin, PinName mouthPin, PinName bodyPin) : 
-        tail(tailPin), mouth(mouthPin), body(bodyPin)
-    {
-        tail.write(0);
-        mouth.write(0);
-        body.write(0);
+    BillyBass(PinName tailPin, PinName mouthPin, PinName bodyPin, bool _inverted=false) :
+        tail(tailPin), mouth(mouthPin), body(bodyPin), inverted(!!_inverted) {
+        relax();
         if (numFish < MAX_FISH) fish[numFish++] = this;
-        // else error
+        else fprintf(stderr, "Too many fish!\r\n");
     }
 
     // if *_pName, it will get the string name of the output
     DigitalOut *outputNamed(char const *_outputName, char const **_pName = 0);
 
-    static BillyBass *bassNumber(unsigned which) { return (which >= numFish) ? 0 : fish[which]; }
-    static unsigned getNumFish() { return numFish; }
+    static BillyBass *bassNumber(unsigned which) {
+        return (which >= numFish) ? 0 : fish[which];
+    }
+
+    static unsigned getNumFish() {
+        return numFish;
+    }
+
+    int onState() const {
+        return !inverted;
+    }
+    int offState() const {
+        return inverted;
+    }
+
+    void relax() {
+        tail.write(offState());
+        mouth.write(offState());
+        body.write(offState());
+    }
 
 protected:
     static BillyBass* fish[ MAX_FISH ];
@@ -40,6 +53,7 @@
     DigitalOut tail;
     DigitalOut mouth;
     DigitalOut body;
+    int inverted;
 };
 
 #endif