我正在为我的DHT传感器创建一个库。我有两个名称为AM_2301.cpp
和AM_2301.h
的文件:
AM_2301.h
:
#ifndef AM2301_h
#define AM2301_h
#include "Arduino.h"
struct Two_float {
float temp;
float humidity;
};
extern int pinn;
class AM_2301
{
public:
AM_2301(int pin);
void begin();
struct Two_float read();
private:
int _pin;
};
#endif
AM_2301.cpp
:
#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"
//#define pinn 3
int pinn;
DHT dht(pinn, DHT21);
AM_2301::AM_2301(int pin)
{
pinMode(pin, OUTPUT);
Serial.print("pinn: ");
Serial.println(pinn);
_pin = pin;
pinn = pin;
//DHT dht(pinn, DHT21);
}
void AM_2301::begin(){
dht.begin();
}
struct Two_float AM_2301::read()
{
struct Two_float output;
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
t = 0;
h = 0;
}
output = {t, h};
return output;
}
和main.cpp
:
#include "Arduino.h"
#include "AM_2301.h"
int pin =3 ;
AM_2301 AM(pin);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
AM.begin();
}
void loop() {
struct Two_float val;
val = AM.read();
Serial.print("Temp: ");
Serial.print(val.temp);
Serial.print(" , ");
Serial.print("Humidity: ");
Serial.println(val.humidity);
delay(2000);
// put your main code here, to run repeatedly:
}
但问题是我想在构造函数中声明一个引脚号,并且该引脚转到AM_2301.cpp
中的另一个构造函数但我不知道如何实现它。我想让dht
对象成为我班级中所有其他函数的全局对象。
答案 0 :(得分:2)
最后我根据paddy的answer找到了一个解决方案我必须使用来自我的静态实例的指针。以下是修改后的AM_2301.cpp
:
#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"
static DHT *dht = NULL; //define the pointer
AM_2301::AM_2301(int pin)
{
pinMode(pin, OUTPUT);
Serial.print("_pin: ");
Serial.println(_pin);
_pin = pin;
dht = new DHT(_pin, DHT21); //pass the initialization
}
void AM_2301::begin(){
dht->begin();
}
struct Two_float AM_2301::read()
{
struct Two_float output;
float h = dht->readHumidity();
float t = dht->readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
t = 0;
h = 0;
}
output = {t, h};
return output;
}
答案 1 :(得分:2)
您需要将 <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/movieImage"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginRight="5px"
android:src="@drawable/fruits" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/movieImage"
android:orientation="vertical">
//below text is to appear beneath the first image
<TextView
android:id="@+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Name"
android:textSize="20sp" />
//below layout to appear beneath the above text
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="@id/movieImage"
android:orientation="horizontal">
<ImageView
android:id="@+id/img1"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_toEndOf="@id/movieImage"
android:layout_marginRight="5px"
android:src="@drawable/thumbsupr" />
<TextView
android:id="@+id/exp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textSize="20sp"
android:layout_toEndOf="@id/img1" />
<ImageView
android:id="@+id/img2"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/exp2"
android:layout_marginRight="5px"
android:src="@drawable/thumbsdownr" />
<TextView
android:id="@+id/exp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="20sp"
android:layout_toEndOf="@id/img2" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
对象作为DHT
类的一部分,然后使用member initializer list对其进行初始化。
AM_2301.h
AM_2301
AM_2301.cpp
#ifndef AM2301_h
#define AM2301_h
#include "Arduino.h"
struct Two_float {
float temp;
float humidity;
};
class AM_2301
{
public:
AM_2301(int pin);
void begin();
struct Two_float read();
private:
DHT dht; // add a DHT object
};
#endif