ネットワークアップデート機能とか、Pachubeへの情報登録とかの処理を追加しています

Dependencies:   Terminal EthernetNetIf Pachube TextLCD mbed ConfigFile FirmwareUpdater

Files at this revision

API Documentation at this revision

Comitter:
abe00makoto
Date:
Tue May 17 13:49:41 2011 +0000
Child:
1:1eb67d074bed
Commit message:
sbm-20 gm-tube support
pachube support
network update suport

Changed in this revision

ConfigFile.lib Show annotated file Show diff for this revision Revisions of this file
EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
FATFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
FirmwareUpdater.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
Pachube.lib Show annotated file Show diff for this revision Revisions of this file
Terminal.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
appconf/appconf.cpp Show annotated file Show diff for this revision Revisions of this file
appconf/appconf.h Show annotated file Show diff for this revision Revisions of this file
geigercounter.cpp Show annotated file Show diff for this revision Revisions of this file
geigercounter.h Show annotated file Show diff for this revision Revisions of this file
geigercounter_ci_3bg.h Show annotated file Show diff for this revision Revisions of this file
geigercounter_sbm_20.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
trans.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FATFileSystem.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_unsupported/code/fatfilesystem/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FirmwareUpdater.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/FirmwareUpdater/#851bd91fa0ae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pachube.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/Pachube/#19accbe9a05e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Terminal.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Terminal/#85184c13476c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mamezu/code/TextLCD/#ffeb3e6c1367
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/appconf/appconf.cpp	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,103 @@
+/**
+ * =============================================================================
+ * Application configuration for 'Expansion Board One' example no.2
+ * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/
+ * =============================================================================
+ * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * =============================================================================
+ */
+
+#include "appconf.h"
+#include "ConfigFile.h"
+
+#define KEY_PACHUBE_APIKEY   "PACHUBE_APIKEY"
+#define KEY_PACHUBE_FEEDID   "PACHUBE_FEEDID"
+//#define KEY_PACHUBE_INTERVAL "PACHUBE_INTERVAL"
+
+/**
+ * Initialize configuration.
+ *
+ * @param p A pointer to a application config.
+ */
+void appconf_init(appconf_t *p) {
+    memset(p->apikey, 0, sizeof(p->apikey));
+    memset(p->feedid, 0, sizeof(p->feedid));
+//    p->interval = 0;
+}
+
+/**
+ * Read configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_read(char *filename, appconf_t *p) {
+    ConfigFile cf;
+    if (!cf.read(filename)) {
+        return -1;
+    }
+    if (!cf.getValue(KEY_PACHUBE_APIKEY, p->apikey, sizeof(p->apikey))) {
+        return -2;
+    }    
+    if (!cf.getValue(KEY_PACHUBE_FEEDID, p->feedid, sizeof(p->feedid))) {
+        return -3;
+    }
+/*
+    char buf[64];
+    if (!cf.getValue(KEY_PACHUBE_INTERVAL, buf, sizeof(buf))) {
+        return -4;
+    } else {
+        if (sscanf(buf, "%d", &(p->interval)) != 1) {
+            return -5;
+        }
+    }
+*/
+    return 0;
+}
+
+/**
+ * Write configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_write(char *filename, appconf_t *p) {
+    ConfigFile cf;
+    if (!cf.setValue(KEY_PACHUBE_APIKEY, p->apikey)) {
+        return -1;
+    }
+    if (!cf.setValue(KEY_PACHUBE_FEEDID, p->feedid)) {
+        return -2;
+    }
+/*
+    char buf[64];
+    snprintf(buf, sizeof(buf) - 1, "%d", p->interval);
+    if (!cf.setValue(KEY_PACHUBE_INTERVAL, buf)) {
+        return -3;
+    }
+*/
+    if (!cf.write(filename)) {
+        return -4;
+    }
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/appconf/appconf.h	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,57 @@
+/**
+ * =============================================================================
+ * Application configuration for 'Expansion Board One' example no.2
+ * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/
+ * =============================================================================
+ * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ * =============================================================================
+ */
+
+typedef struct {
+    char apikey[256];
+    char feedid[128];
+//    int interval;
+} appconf_t;
+
+/**
+ * Initialize configuration.
+ *
+ * @param p A pointer to a application config.
+ */
+void appconf_init(appconf_t *p);
+
+/**
+ * Read configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_read(char *filename, appconf_t *p);
+
+/**
+ * Write configuration.
+ *
+ * @param filename Filename.
+ * @param p A pointer to a application config.
+ * @return Return zero if it succeed.
+ */
+int appconf_write(char *filename, appconf_t *p);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geigercounter.cpp	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,16 @@
+#include "geigercounter.h"
+
+
+
+float Geigercounter::getcps()
+{
+    if(_geigerrun)_stoptime=time(NULL);
+    int seconds=_stoptime-_starttime;
+    float cps=(float)_count/(float)seconds;
+    return cps;
+}
+
+float Geigercounter::getcpm()
+{
+  return this->getcps()*60.0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geigercounter.h	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,73 @@
+#ifndef MBED_GEIGERCOUNTER_H
+#define MBED_GEIGERCOUNTER_H
+#include "mbed.h"
+
+class Geigercounter {
+public:
+    Geigercounter(PinName pin,PinName spin) : _interrupt(pin),_speaker(spin),_speakerstop(){        
+        _interrupt.rise(this, &Geigercounter::increment); 
+        
+        //speaker setup sound off
+        _speaker.period(1.0/2000.0);
+        _speaker=0.0;
+        _speakeron=true;
+    }
+
+    void start(){
+      _count=0;
+      _starttime=time(NULL);
+      _geigerrun=true;
+    }
+    
+    void stop(){
+      _geigerrun=false;
+      _stoptime=time(NULL);
+      
+    }
+    
+    void soundon(){
+      _speakeron=true;
+    }
+    
+    void soundoff(){
+     _speakeron=false;
+    }
+     
+    
+    float getcps();
+    float getcpm();    
+    
+    int read() {
+        return _count;
+    }
+
+private:
+    void _speakeroff(){
+      _speaker=0;
+    }
+    void increment() {
+      if(_geigerrun){
+      
+        _count++;
+        
+        //sound
+        if(_speakeron){
+          _speaker=0.5;
+          _speakerstop.attach(this,&Geigercounter::_speakeroff,0.02);
+        }
+      }
+    }
+
+    InterruptIn _interrupt;
+    PwmOut _speaker;
+    Timeout _speakerstop;
+    time_t       _starttime;
+    time_t       _stoptime;
+    volatile int _count;
+    volatile bool _geigerrun;
+    volatile bool _speakeron;
+
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geigercounter_ci_3bg.h	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,21 @@
+#ifndef MBED_GEIGERCOUNTER_CI_3BG_H
+#define MBED_GEIGERCOUNTER_CI_3BG_H
+#include "geigercounter.h"
+
+
+class Geigercounter_CI_3BG:public Geigercounter{
+
+public:
+
+Geigercounter_CI_3BG(PinName pin,PinName spin):Geigercounter(pin,spin){}
+
+float getusv(){
+  //si-3bg is maybe 200 cps/R/h = 200 cps/ 10000uSv/h = 0.02 cps/uSv/h  
+  //usv=cpm/60.0/0.02;
+   return getcpm()/1.7;
+}
+
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geigercounter_sbm_20.h	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,20 @@
+#ifndef MBED_GEIGERCOUNTER_CI_3BG_H
+#define MBED_GEIGERCOUNTER_CI_3BG_H
+#include "geigercounter.h"
+
+
+class Geigercounter_SBM_20:public Geigercounter{
+
+public:
+
+Geigercounter_SBM_20(PinName pin,PinName spin):Geigercounter(pin,spin){}
+
+float getusv(){
+
+   return (float)getcpm()/132.0;
+}
+
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,250 @@
+#include "mbed.h"
+#include "geigercounter_sbm_20.h"
+#include "trans.h"
+#include "TextLCD.h"
+
+
+#include "mbed.h"
+#include "PachubeV2CSV.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "appconf.h"
+#include "FirmwareUpdater.h"
+
+
+
+ extern "C" void mbed_reset();
+ 
+/*
+ * Definitions for a configuration file.
+ */
+#define CONFIG_FILENAME "/local/PACHUBE.CFG"
+const int PACHUBE_CODE_OK = 200;
+
+LocalFileSystem localfs("local");
+
+EthernetNetIf netif;
+
+FirmwareUpdater fwup("http://mbed.org/media/uploads/abe00makoto", "geiger", true);
+
+TextLCD  output( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
+Geigercounter_SBM_20 geiger(p18,p22);//Geiger pin,Spekaer pin
+Trans trans(p21,0.16/1000.0,0.76); //Trans pin,period,duty
+
+static appconf_t appconf;
+
+
+/**
+ * Convert double to char.
+ *
+ * @param val Value.
+ * @param buf A pointer to a buffer.
+ * @param bufsiz The buffer size.
+ */
+void convertDoubleToChar(double val, char *buf, size_t bufsiz) {
+    snprintf(buf, bufsiz, "%f", val);
+}
+
+/**
+ * Post to the feed on Pachube.
+ *
+ * @param web Pointer to a Pachube object.
+ * @param feed_id Feed ID.
+ * @param stream_no Stream number.
+ * @param value value.
+ *
+ * @return Pachube code.
+ */
+int web_post(PachubeV2CSV *web, int feed_id, int stream_no, double value) {
+    char value_text[16];
+    convertDoubleToChar(value, value_text, sizeof(value_text));
+    char stream_no_text[8];
+    stream_no_text[0] = "0123456789"[stream_no];
+    stream_no_text[1] = '\0';
+    return web->updateDataStream(feed_id, stream_no_text, std::string(value_text));
+}
+
+
+
+//firm ware updater
+void check_newfirm() {
+    if (fwup.exist() == 0) {
+        output.printf("Found a new firmware.\n");
+        if (fwup.execute() == 0) {
+            output.printf("Update succeed.\n");
+            wait(10);
+            output.printf("Resetting this system.\n");
+            wait(10);
+            fwup.reset();
+        } else {
+            output.printf("Update failed!\n");
+            
+        }
+    }else{
+       output.printf("not found update.\n");
+    }
+    wait(5);
+}
+
+void print_geigerdata()
+{
+    output.cls();
+    output.printf("%.3fuSv/h\n",geiger.getusv());
+    output.printf("%.3fCPM",geiger.getcpm());
+}
+
+
+
+
+int main() {
+  
+  bool etherconnect;
+  output.cls();
+  output.printf("Hello.");
+  wait(1);
+  
+  
+  /*
+   * Initialize ethernet interface.
+   */
+  output.cls();
+  output.locate(0, 0);
+  output.printf("Initializing...");
+  output.locate(0, 1);
+  output.printf("Ethernet: ");
+  
+  EthernetErr ethErr = netif.setup();
+  if (ethErr) {
+      output.printf("[NG]");
+      //error("Ethernet setup failed. Done with code %d.\n", ethErr);
+      etherconnect=false;
+  }
+  else{
+      etherconnect=true;
+      output.printf("[OK]");
+  }
+  
+  wait(2);
+  if(etherconnect){    
+    //firmware update
+    output.cls();
+    check_newfirm();
+  } 
+  
+    /*
+     * Read configuration variables from a file.
+     */
+  output.cls();
+  output.locate(0, 0);
+  output.printf("cfg file Reading...");
+  output.locate(0, 1);
+  output.printf("Setup: ");
+  appconf_init(&appconf);
+  if (appconf_read(CONFIG_FILENAME, &appconf) != 0) {
+      output.printf("[NG]");
+      error("Failure to read a configuration file.\n");
+  }
+  else{
+    output.printf("[OK]");
+    wait(3);
+  }
+    
+  /*
+   * Initialize objects.
+   */
+  PachubeV2CSV web(appconf.apikey);
+  const int feed_id = atoi(appconf.feedid);
+      
+  
+  
+  //20 minitues loop
+  for(int min=0;min<(20/5);min++){
+    trans.on();
+    wait(0.5);
+    geiger.start();  
+    //5 minitues geiger run
+    for(int i=0;i<5*60;i++){
+      print_geigerdata();
+      wait(1); 
+    }
+    geiger.stop();
+    trans.off();
+    output.cls();
+    print_geigerdata();
+    wait(1);
+    /*
+     * Check the pachube feautures.
+     */
+    if(etherconnect){
+  
+        if (web_post(&web, feed_id, 0, geiger.getusv()) != PACHUBE_CODE_OK) {
+          output.printf("Checking Pachube status:x");
+          wait(10);
+        }
+
+        if (web_post(&web, feed_id, 1, geiger.getcpm()) != PACHUBE_CODE_OK){
+          output.printf("Checking Pachube status:x");
+          wait(10);
+        }
+    }
+  }
+  
+  if(!etherconnect){
+    output.cls();
+    output.printf("please!!\n net connect\n");
+    wait(10);
+    output.printf("mbed reset!!\n");
+    wait(10);
+    mbed_reset();
+  }
+  
+  
+  //
+  //  night running mode. 
+  //
+  output.cls();
+  output.printf("sound off mode..");
+  geiger.soundoff();
+  
+  int errcnt=0;
+  while(1){
+    
+    for(int min=0;min<(20/5);min++){
+      trans.on();
+      wait(1);
+      geiger.start();
+      for(int i=0;i<5*60;i++){
+        print_geigerdata();
+        wait(1); 
+      }
+  
+      geiger.stop();
+      trans.off();
+    
+      /*
+       * Post.
+       */
+      if(etherconnect){
+        if (web_post(&web, feed_id, 0, geiger.getusv()) != PACHUBE_CODE_OK) {
+          output.printf("Checking Pachube status:x");
+          errcnt++;
+          wait(10);
+        }
+
+        if (web_post(&web, feed_id, 1, geiger.getcpm()) != PACHUBE_CODE_OK){
+          output.printf("Checking Pachube status:x");
+          errcnt++;
+          wait(10);
+        }
+      }//if(etherconncet)
+    }//for
+    
+    //if firmware exist.reset. 
+    if(etherconnect){
+      if(fwup.exist()==0)mbed_reset();
+    }
+    
+    //many error!! reset
+    if(errcnt>20)mbed_reset();
+  }//while
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trans.h	Tue May 17 13:49:41 2011 +0000
@@ -0,0 +1,27 @@
+#ifndef MBED_VOLTAGECONVERTER_H
+#define MBED_VOLTAGECONVERTER_H
+#include "mbed.h"
+
+class Trans
+{
+
+public: 
+ Trans(PinName pin,float period,float duty): _trans(pin){
+   _trans.period(period); 
+   _trans=0;
+   _duty=duty;   
+ }
+ void off(){
+   _trans=0;
+ }
+ void on(){
+   _trans=_duty;
+ }
+ 
+private:
+  PwmOut _trans;
+  float _duty;
+  
+  
+};
+#endif
\ No newline at end of file