我尝试为 Arduino 构建一个库(file.cpp + file.h),但出现此错误:
在示例_MPL3115A2.ino:2 中包含的文件中:/Applications/Arduino.app/Contents/Resources/Java/libraries/MPL3115A2/MPL3115A2.h:8: 错误:ISO C++ 禁止声明没有类型EXAMPLE_MPL3115A2.ino 的“getPressure”:在函数 'void loop()' 中:EXAMPLE_MPL3115A2:18:错误:在 '.' 之前预期的 unqualified-id 令牌
你能解释一下错误在哪里吗?(我不太懂C++)
这是我的MPL3115A2.h
#ifndef MPL3115A2_h
#define MPL3115A2_h
#include "Arduino.h"
class MPL3115A2{
public:
getPressure();
private:
int m_i2c_address;
byte _p_msb, _p_csb, _plsb;
byte _tempp, _tempa, _tempt, _decimalPress, _decimalAlt, _decimalTemp;
unsigned long _ptot;
};
#endif
我的MPL3115A2.cpp
#include "Arduino.h"
#include "MPL3115A2.h"
#include "I2C.h"
MPL3115A2::MPL3115A2()
{
m_i2c_address = 0x60;
p_msb = _p_msb;
p_csb = _p_csb;
p_lsb = _p_lsb;
tempp = _tempp;
tempa = _tempa;
tempt = _tempt;
decimalPress = _decimalPress;
decimalAlt = _decimalAlt;
decimalTemp = _decimalTemp;
ptot = _ptot;
}
float MPL3115A2::getPressure()
{
I2c.write(m_i2c_address,0x26,0x00);
I2c.write(m_i2c_address,0x26,0x01);
delay(100);
I2c.read(m_i2c_address,0x1,3);
I2c.end();
while(I2c.available()){
p_msb=I2c.receive();
p_csb=I2c.receive();
p_lsb=I2c.receive();}
ptot=p_msb;
ptot=(ptot<<8)+p_csb;
ptot=(ptot<<2)+(p_lsb>>6);
tempp=(p_lsb<<2);
if(tempp==192){
decimalPress=75;
}else if(tempp==128){
decimalPress=25;
}else if(tempp==64){
decimalPress=5;
}else{tempp=0;}
Serial.print(ptot);
Serial.print(".");
Serial.println(decimalPress);
}
#endif
和我的example.pde
#include <Arduino.h>
#include <MPL3115A2.h>
#include <I2C.h>
void setup(){
Serial.begin(9600);
I2c.begin();
}
//LOOP
void loop(){
Serial.print("Pressure is: ");
MPL3115A2.getPressure(); //Expressed in Pa
Serial.print("Altimetry is: ");
//MPL3115A2.getAltimetry(); //Expressed in m
Serial.print("Temperature is: ");
// MPL3115A2.getTemperature(); //Expressed in C
delay(2000);
}
对不起,很长的帖子!感谢大家的帮助!