trainning_template

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
brainliang
Date:
Tue Dec 01 05:21:19 2020 +0000
Parent:
4:fe1e9f9c7b33
Commit message:
trainning_template

Changed in this revision

converters.h Show annotated file Show diff for this revision Revisions of this file
example.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sensors.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/converters.h	Tue Dec 01 05:21:19 2020 +0000
@@ -0,0 +1,45 @@
+#pragma once
+
+#include <cstdlib>
+#include <string>
+
+namespace std{
+static inline int stoi(const string &s) {
+    return atoi(s.c_str());
+}
+static inline long stol(const string &s) {
+    return atol(s.c_str());
+}
+static inline float stof(const string &s) {
+    return float(atof(s.c_str()));
+}
+#define _c_to_string(fmt,val) { \
+    char buf[16];\
+    snprintf(buf, sizeof(buf), fmt, val);\
+    return string(buf);\
+}
+static inline string to_string(bool v) {
+    _c_to_string("%d", (int)v);
+}
+static inline string to_string(char v) {
+    _c_to_string("%c", v);
+}
+static inline string to_string(int v) {
+    _c_to_string("%d", v);
+}
+static inline string to_string(long v) {
+    _c_to_string("%ld", v);
+}
+static inline string to_string(float v) {
+    _c_to_string("%f", v);
+}
+}
+
+inline bool  _p(bool x) { return x; }
+inline char  _p(char x) { return x; }
+inline int   _p(int x) { return x; }
+inline long  _p(long x) { return x; }
+inline float _p(float x) { return x; }
+inline void* _p(void* x) { return x; }
+inline const char* _p(const std::string &x) { return x.c_str(); }
+
--- a/example.cpp	Thu Oct 18 16:23:13 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,98 +0,0 @@
-#include "sensors.h"
-#include "mbed.h"
-
-Serial usb2pc(PA_2, PA_3, 9600);  
-DigitalOut myled(PC_13);
-
-int main()
-{
-
-    
-    usb2pc.printf("starting\r\n");
-    sr501 x(PB_1);
-    BH1750 y(PB_7,PB_6);
-    mq z(PA_7,PA_6);
-    dht11 w(PA_5);
-    DS18B20 v(PC_14);
-    YL u(PA_1,PA_0);
-    BMP180 t(PB_9,PB_8);
-    GP2Y1010 s(PA_8,PB_0);
-    
-    while(1)
-    {
-        /*sr501
-        if(x.operator ==(true))
-        {
-            usb2pc.printf("get\r\n");
-            while(x.read()){usb2pc.printf("aaaaaaaaaaaaa\r\n");wait(0.1);myled=1;}
-            x.reset();
-        }
-        
-        else
-        {
-            usb2pc.printf("no\r\n");
-        }
-        wait(0.1);
-        */
-        /*BH1750
-        float light=y.getlightdata();
-        usb2pc.printf("Light intensity: %.4f Lux\r\n",light);
-        */
-        /*mq
-        if(z.operator==(true))
-        {
-            usb2pc.printf("mq on\r\n");
-            while(!z.read()){usb2pc.printf("%.3f\r\n",z.getairdata());wait(0.1);myled=1;}
-            myled = 1; // LED is ON
-            wait(0.2); // 200 ms
-            z.reset();
-        }
-        else
-        {
-            usb2pc.printf("mq off\r\n");
-            myled = 0; // LED is OFF
-            wait(1.0); // 1 sec
-        }
-        */
-        /*dht11
-        if(w.getdata())
-        {
-            usb2pc.printf("Humidity (%):%.3f\r\n",w.gethumidity());
-            usb2pc.printf("Temperature (oC): %.3f\r\n",w.gettemperature());
-        }
-        wait(2);
-        */
-        /*DS18B20
-        if(v.getdata())
-        {
-            usb2pc.printf("Temperature (oC): %.3f\r\n",v.gettemperature());
-        }
-        wait(2);
-        */
-        /*YL-38
-        if(u.operator==(true))
-        {
-            usb2pc.printf("YL on\r\n");
-            while(!u.read()){usb2pc.printf("%.3f\r\n",u.getairdata());wait(0.1);myled=1;}
-            myled = 1; // LED is ON
-            wait(0.2); // 200 ms
-            u.reset();
-        }
-        else
-        {
-            usb2pc.printf("YL off\r\n");
-            myled = 0; // LED is OFF
-            wait(1.0); // 1 sec
-        }
-        */
-        /*bmp180
-        usb2pc.printf("Temperature is  %.1f C\r\n", (float)t.BMP180GetTemperature()); 
-        usb2pc.printf("Pressure is  %.3f kPa\r\n", (float)t.BMP180GetPressure()/1000.0);
-        wait(1.0); 
-        */
-        /*GP2Y1010
-        usb2pc.printf("Dust Density: %.3f \r\n",s.getairdata()) ;
-        wait(1.0); 
-        */
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Dec 01 05:21:19 2020 +0000
@@ -0,0 +1,33 @@
+#define HIGH 1
+#define LOW 0
+#include "mbed.h"
+#include <string>
+typedef bool boolean;
+typedef std::string String;
+#include "sensors.h"
+#include "converters.h"
+
+int gasOK;
+boolean item;
+
+Serial Serial_2(PA_2,PA_3);
+YL analog_PC_15(PC_15,PA_0);
+DigitalOut myDigitalOutPC_13(PC_13);
+
+int main() {
+
+
+Serial_2.baud(9600);
+
+item = 0;
+while (true) {
+item = !item;
+myDigitalOutPC_13.write(item);
+wait_ms(1000);
+if (analog_PC_15==true) {
+gasOK = analog_PC_15.read();
+Serial_2.printf("gas: %d\n",_p(gasOK));
+}
+}
+
+}
\ No newline at end of file
--- a/sensors.cpp	Thu Oct 18 16:23:13 2018 +0000
+++ b/sensors.cpp	Tue Dec 01 05:21:19 2020 +0000
@@ -1,12 +1,220 @@
 #include "sensors.h"
 #include "mbed.h"
 
-extern Serial usb2pc;  
-extern DigitalOut myled;
+//extern Serial usb2pc;  
+// extern DigitalOut myled;
+
+//==============================================YL-38
+YL::YL(PinName dio)
+    : status(false), signal(dio),signallevel(PC_13)
+{
+    signal.fall(this, &YL::triggered);
+//    usb2pc.printf("YL start!\r\n");
+}
+//--------
+YL::YL(PinName dio,PinName aio)
+    : status(false), signal(dio),signallevel(aio)
+{
+    signal.fall(this, &YL::triggered);
+//    usb2pc.printf("YL start!\r\n");
+}
+//--------
+void YL::triggered()                                     //触发中断!!
+{
+//    usb2pc.printf("YL Triggered!\r\n");
+    status = true;
+}
+//--------
+bool YL::operator==(const bool &target)
+{
+    if(status == target)
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+//--------
+float YL::getairdata()
+{
+    return signallevel.read();
+}  
+//-----------
+void YL::reset()
+{
+    status = false;
+}
+int YL::read()
+{
+    return signal.read();
+}
+
+//==============================================MQ-2
+mq::mq(PinName dio)
+    : status(false), signal(dio),signallevel(PC_13)
+{
+    signal.fall(this, &mq::triggered);
+//    usb2pc.printf("mq start!\r\n");
+}
+//--------
+mq::mq(PinName dio,PinName aio)
+    : status(false), signal(dio),signallevel(aio)
+{
+    signal.fall(this, &mq::triggered);
+//    usb2pc.printf("mq start!\r\n");
+}
+//--------
+void mq::triggered()                                     //触发中断!!
+{
+//    usb2pc.printf("mq Triggered!\r\n");
+    status = true;
+}
+//--------
+bool mq::operator==(const bool &target)
+{
+    if(status == target)
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+//--------
+float mq::getairdata()
+{
+    return signallevel.read();
+}  
+//-----------
+void mq::reset()
+{
+    status = false;
+}
+int mq::read()
+{
+    return signal.read();
+}
+
+
+//===========================================DHT11
+dht11::dht11(PinName pin)
+    :datapin(pin)
+{
+    starttime.start();
+//    usb2pc.printf("dht11 start!\r\n");
+}
+//----------
+int dht11::getdata()
+{
+    int timeout=10000;
+    uint8_t data[5];
+    uint8_t bit=7;
+    uint8_t count=0;
+        
+    for(int i=0;i<5;i++)
+    {
+        data[i]=0;
+    }
+    if(starttime.read_ms()<1500){while(starttime.read_ms()<1500){}starttime.stop();}
+        
+    datapin.output();
+    datapin=0;
+    wait_ms(19);
+    datapin=1;
+    wait_us(30);
+    datapin.input();
+        
+    while(!datapin)
+    {
+//        if(timeout--==0){usb2pc.printf("timeout!no reset\r\n");return 0;}
+        if(timeout--==0){return 0;}
+    }
+    timeout=10000;
+    while(datapin)
+    {
+//        if(timeout--==0){usb2pc.printf("timeout!no respanse\r\n");return 0;}
+        if(timeout--==0){return 0;}
+    }
+    timeout=10000;
+    for(int i=0;i<40;i++)
+    {
+        while(!datapin)
+        {
+//            if(timeout--==0){usb2pc.printf("timeout!\r\n");return 0;}
+            if(timeout--==0){return 0;}
+        }
+        timer.start();
+        timeout=10000;
+        while(datapin)
+        {
+//            if(timeout--==0){usb2pc.printf("timeout!n\r\n");return 0;}
+            if(timeout--==0){return 0;}
+        }
+        timeout=10000;
+        long t=timer.read_us();
+        timer.stop();
+        timer.reset();
+            
+        if(bit==0)
+        {
+            if(t>40){data[count]|=(1<<bit);}
+            bit=7;
+            count++;
+        }
+        else
+        {
+            if(t>40)
+            {
+                data[count]|=(1<<bit);
+            }
+            bit--;
+        }
+    }
+    datapin=1;
+    if(data[4]==data[0]+data[1]+data[2]+data[3])
+    {
+        //usb2pc.printf("Humidity (%):%f\r\n",(float)data[0]);
+        //usb2pc.printf("Temperature (oC): %f\r\n",(float)data[2]);
+        H=data[0]+data[1]/10.0;
+        T=data[2]+data[3]/10.0;
+        return 1;
+    }
+//    else{usb2pc.printf("error!\r\ndata0:%d\tdata1:%d\r\ndata2:%d\tdata3:%d\r\ndata4:%d\r\n",(int)data[0],(int)data[1],(int)data[2],(int)data[3],(int)data[4]);return 0;}
+       else{return 0;}
+}  
+//-------
+float dht11::gethumidity()
+{
+    return H;
+}
+//-------
+float dht11::gettemperature()
+{
+    return T;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 //============================================================SR501
 void sr501::triggered()                                     //触发中断!!
 {
-    usb2pc.printf("sr501 Triggered!\r\n");
+//    usb2pc.printf("sr501 Triggered!\r\n");
     status = true;
 }
 //--------
@@ -14,7 +222,7 @@
     : status(false), signal(pSignal)
 {
     signal.rise(this, &sr501::triggered);
-    usb2pc.printf("sr501 start!\r\n");
+//    usb2pc.printf("sr501 start!\r\n");
 }
 //-------
 bool sr501::operator==(const bool &target)
@@ -50,7 +258,7 @@
         status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false);
         wait_ms(10);
     }
-    usb2pc.printf("BH1750 start with default mode!\r\n");
+//    usb2pc.printf("BH1750 start with default mode!\r\n");
 }
 //--------
 BH1750::BH1750(PinName sda,PinName scl,char mode[])           //启动光强!!!!!(自定义设置)
@@ -62,7 +270,7 @@
         status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false);
         wait_ms(10);
     }
-    usb2pc.printf("BH1750 start with customize mode!\r\n");    
+//    usb2pc.printf("BH1750 start with customize mode!\r\n");    
 }
 //--------
 float BH1750::getlightdata()                                //读取光强(lux)
@@ -76,152 +284,17 @@
     }
     else
     {
-        usb2pc.printf("BH1750 read fail!\r\n");
+//        usb2pc.printf("BH1750 read fail!\r\n");
         return -1;
     }
 }
-//==============================================MQ-2
-mq::mq(PinName dio)
-    : status(false), signal(dio),signallevel(PC_13)
-{
-    signal.fall(this, &mq::triggered);
-    usb2pc.printf("mq start!\r\n");
-}
-//--------
-mq::mq(PinName dio,PinName aio)
-    : status(false), signal(dio),signallevel(aio)
-{
-    signal.fall(this, &mq::triggered);
-    usb2pc.printf("mq start!\r\n");
-}
-//--------
-void mq::triggered()                                     //触发中断!!
-{
-    usb2pc.printf("mq Triggered!\r\n");
-    status = true;
-}
-//--------
-bool mq::operator==(const bool &target)
-{
-    if(status == target)
-    {
-        return true;
-    }
-    else
-    {
-        return false;
-    }
-}
-//--------
-float mq::getairdata()
-{
-    return signallevel.read();
-}  
-//-----------
-void mq::reset()
-{
-    status = false;
-}
-int mq::read()
-{
-    return signal.read();
-}
-//===========================================DHT11
-dht11::dht11(PinName pin)
-    :datapin(pin)
-{
-    starttime.start();
-    usb2pc.printf("dht11 start!\r\n");
-}
-//----------
-int dht11::getdata()
-{
-    int timeout=10000;
-    uint8_t data[5];
-    uint8_t bit=7;
-    uint8_t count=0;
-        
-    for(int i=0;i<5;i++)
-    {
-        data[i]=0;
-    }
-    if(starttime.read_ms()<1500){while(starttime.read_ms()<1500){}starttime.stop();}
-        
-    datapin.output();
-    datapin=0;
-    wait_ms(19);
-    datapin=1;
-    wait_us(30);
-    datapin.input();
-        
-    while(!datapin)
-    {
-        if(timeout--==0){usb2pc.printf("timeout!no reset\r\n");return 0;}
-    }
-    timeout=10000;
-    while(datapin)
-    {
-        if(timeout--==0){usb2pc.printf("timeout!no respanse\r\n");return 0;}
-    }
-    timeout=10000;
-    for(int i=0;i<40;i++)
-    {
-        while(!datapin)
-        {
-            if(timeout--==0){usb2pc.printf("timeout!\r\n");return 0;}
-        }
-        timer.start();
-        timeout=10000;
-        while(datapin)
-        {
-            if(timeout--==0){usb2pc.printf("timeout!n\r\n");return 0;}
-        }
-        timeout=10000;
-        long t=timer.read_us();
-        timer.stop();
-        timer.reset();
-            
-        if(bit==0)
-        {
-            if(t>40){data[count]|=(1<<bit);}
-            bit=7;
-            count++;
-        }
-        else
-        {
-            if(t>40)
-            {
-                data[count]|=(1<<bit);
-            }
-            bit--;
-        }
-    }
-    datapin=1;
-    if(data[4]==data[0]+data[1]+data[2]+data[3])
-    {
-        //usb2pc.printf("Humidity (%):%f\r\n",(float)data[0]);
-        //usb2pc.printf("Temperature (oC): %f\r\n",(float)data[2]);
-        H=data[0]+data[1]/10.0;
-        T=data[2]+data[3]/10.0;
-        return 1;
-    }
-    else{usb2pc.printf("error!\r\ndata0:%d\tdata1:%d\r\ndata2:%d\tdata3:%d\r\ndata4:%d\r\n",(int)data[0],(int)data[1],(int)data[2],(int)data[3],(int)data[4]);return 0;}
-}  
-//-------
-float dht11::gethumidity()
-{
-    return H;
-}
-//-------
-float dht11::gettemperature()
-{
-    return T;
-}
+
 //============================================DS18B20
 DS18B20::DS18B20(PinName pin)
     :datapin(pin)
 {
-        if(start()){usb2pc.printf("DS18B20 started\r\n");}
+//        if(start()){usb2pc.printf("DS18B20 started\r\n");}
+        if(start()){}
 }
 //-----------
 int DS18B20::start()
@@ -320,54 +393,11 @@
 float DS18B20::gettemperature()
 {
     if(getdata()){return T;}
-    else{usb2pc.printf("get temperature fail!\r\n");return 0;}
-}
-//==============================================YL-38
-YL::YL(PinName dio)
-    : status(false), signal(dio),signallevel(PC_13)
-{
-    signal.fall(this, &YL::triggered);
-    usb2pc.printf("YL start!\r\n");
-}
-//--------
-YL::YL(PinName dio,PinName aio)
-    : status(false), signal(dio),signallevel(aio)
-{
-    signal.fall(this, &YL::triggered);
-    usb2pc.printf("YL start!\r\n");
-}
-//--------
-void YL::triggered()                                     //触发中断!!
-{
-    usb2pc.printf("YL Triggered!\r\n");
-    status = true;
+    //else{usb2pc.printf("get temperature fail!\r\n");return 0;}
+    else{return 0;}
 }
-//--------
-bool YL::operator==(const bool &target)
-{
-    if(status == target)
-    {
-        return true;
-    }
-    else
-    {
-        return false;
-    }
-}
-//--------
-float YL::getairdata()
-{
-    return signallevel.read();
-}  
-//-----------
-void YL::reset()
-{
-    status = false;
-}
-int YL::read()
-{
-    return signal.read();
-}
+// ==================================================
+
 //=============================================
 BMP180::BMP180(PinName sda,PinName scl)  
     :i2c(sda,scl)  
@@ -376,17 +406,17 @@
     uint8_t c = readByte(BMP180_ADDRESS, BMP180_WHO_AM_I);   
     if(c == 0x55) {
  
-    usb2pc.printf("BMP-180 is 0x%x\r\n", c);
-    usb2pc.printf("BMP-180 should be 0x55\r\n");
-    usb2pc.printf("BMP-180 online...\r\n");
+//    usb2pc.printf("BMP-180 is 0x%x\r\n", c);
+//    usb2pc.printf("BMP-180 should be 0x55\r\n");
+//    usb2pc.printf("BMP-180 online...\r\n");
    
     BMP180Calibration();
-    usb2pc.printf("BMP-180 calibration complete...\r\n");
+//    usb2pc.printf("BMP-180 calibration complete...\r\n");
    }
    else 
    {
-    usb2pc.printf("BMP-180 is 0x%x\r\n", c);
-    usb2pc.printf("BMP-180 should be 0x55\r\n");
+//    usb2pc.printf("BMP-180 is 0x%x\r\n", c);
+//    usb2pc.printf("BMP-180 should be 0x55\r\n");
     while(1); // idle here forever
    }
 }