Station API

Dependents:   GMCStation

Revision:
2:a9d1a9c92927
Parent:
1:a22e390c70b3
Child:
4:ed5993d82ceb
--- a/GMCounter.h	Mon Dec 12 02:33:21 2011 +0000
+++ b/GMCounter.h	Mon Dec 12 11:41:24 2011 +0000
@@ -31,16 +31,18 @@
 class GMCounter {
 public:
     /**
-     * creates GM Counter object
+     * Constructor
      *
      * @param gmcPin pin for GMC input
      * @param buzzerPin pin for buzzer output
      * @param ledPin pin for LED output
+     * @param cpm2usv conversion ratio of CPM to uSv
+     * @param buzzerEnabled if true enable buzzer
+     * @param ledEnabled if true enable LED
      */
-    GMCounter(PinName gmcPin, PinName buzzerPin = NC, PinName ledPin = NC) :
-            interrupt(gmcPin), buzzer(buzzerPin), led(ledPin),
-            counter(0), index(0), index60(0),
-            elapsed(0), buzzerEnabled(buzzerPin != NC), ledEnabled(ledPin != NC) {
+    GMCounter(PinName gmcPin, PinName buzzerPin, PinName ledPin, float cpm2usv = 1.0F / 120, bool buzzerEnabled = true, bool ledEnabled = true)
+            : interrupt(gmcPin), buzzer(buzzerPin), led(ledPin), cpm2usv(cpm2usv), buzzerEnabled(buzzerEnabled), ledEnabled(ledEnabled),
+            counter(0), index(0), index60(0), elapsed(0) {
         memset(count, 0, sizeof(count));
         memset(count60, 0, sizeof(count60));
         ticker.attach(this, &GMCounter::tickerHandler, 1);
@@ -49,6 +51,41 @@
     }
 
     /**
+     * creates an GMCounter object
+     *
+     * @param gmcPin pin for GMC input
+     * @param buzzerPin pin for buzzer output
+     * @param ledPin pin for LED output
+     * @param filename name of the config file
+     * @param verbose if true display debug info
+     *
+     * @returns GMCounter object
+     */
+    static GMCounter create(PinName gmcPin, PinName buzzerPin, PinName ledPin, char *filename, bool verbose = false) {
+        bool buzzerEnabled = true, ledEnabled = true;
+        float cpm2usv = 1.0F / 120;
+
+        if (filename) {
+            char path[32];
+            LocalFileSystem local("local");
+            sprintf(path, "/local/%s", filename);
+            if (FILE *fp = fopen(path, "r")) {
+                Utils::fgetValues(fp, "set-buzzer:%d", &buzzerEnabled);
+                Utils::fgetValues(fp, "set-led:%d", &ledEnabled);
+                Utils::fgetValues(fp, "cpm-to-usv:%f", &cpm2usv);
+                fclose(fp);
+                if (verbose) {
+                    printf("set-buzzer:%d\n", buzzerEnabled);
+                    printf("set-led:%d\n", ledEnabled);
+                    printf("cpm-to-usv:%f\n", cpm2usv);
+                }
+            }
+        }
+
+        return GMCounter(gmcPin, buzzerPin, ledPin, cpm2usv, buzzerEnabled, ledEnabled);
+    }
+
+    /**
      * gets CPM (Counts Per Minute) value
      *
      * @returns counts during the last 60 seconds
@@ -83,6 +120,24 @@
     }
 
     /**
+     * Returns radiation
+     *
+     * @returns radiation in uSv
+     */
+    float getRadiation() {
+        return getCPM() * cpm2usv;
+    }
+
+    /**
+     * Returns average radiation
+     *
+     * @returns average radiation during last 60 minutes in uSv
+     */
+    float getAverageRadiation() {
+        return getAverageCPM() * cpm2usv;
+    }
+
+    /**
      * returns buzzer status
      *
      * @returns buzzer status
@@ -114,20 +169,30 @@
         ledEnabled = enable;
     }
 
+    /**
+     * sets CPM to uSv conversion ration
+     *
+     * @param cpm2usv conversion ratio uSv/CPM
+     */
+    void setConversionRatio(float cpm2usv) {
+        this->cpm2usv = cpm2usv;
+    }
+
 private:
     InterruptIn interrupt;
     DigitalOut buzzer;
     DigitalOut led;
     Timeout timeout;
     Ticker ticker, ticker60;
+    float cpm2usv;
+    bool buzzerEnabled;
+    bool ledEnabled;
     unsigned int counter;
     unsigned int count[61];
     unsigned int count60[61];
     int index;
     int index60;
     float elapsed;
-    bool buzzerEnabled;
-    bool ledEnabled;
 
     void interruptHandler() {
         counter++;