-
Notifications
You must be signed in to change notification settings - Fork 105
/
BME280Spi.cpp
134 lines (104 loc) · 3.32 KB
/
BME280Spi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
BME280Spi.cpp
This code records data from the BME280Spi sensor and provides an API.
This file is part of the Arduino BME280Spi library.
Copyright (C) 2016 Tyler Glenn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written: Dec 18 2016. - Happy Holidays!
Last Updated: Oct 07 2017.
This header must be included in any derived code or copies of the code.
Based on the data sheet provided by Bosch for the BME280Spi environmental sensor,
calibration code based on algorithms providedBosch, some unit conversations courtesy
of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation
courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu.
*/
#include "Arduino.h"
#include "BME280Spi.h"
#include <SPI.h>
/****************************************************************/
BME280Spi::BME280Spi
(
const Settings& settings
)
:BME280(settings),
m_settings(settings)
{
}
/****************************************************************/
bool BME280Spi::Initialize()
{
pinMode(m_settings.spiCsPin, OUTPUT);
digitalWrite(m_settings.spiCsPin, HIGH);
return BME280::Initialize();
}
/****************************************************************/
void BME280Spi::setSettings
(
const Settings& settings
)
{
m_settings = settings;
BME280::setSettings(settings);
}
/****************************************************************/
const BME280Spi::Settings& BME280Spi::getSettings() const
{
return m_settings;
}
/****************************************************************/
bool BME280Spi::ReadRegister
(
uint8_t addr,
uint8_t data[],
uint8_t len
)
{
SPI.beginTransaction(SPISettings(500000,MSBFIRST,SPI_MODE0));
// bme280 uses the msb to select read and write
// combine the addr with the read/write bit
uint8_t readAddr = addr | BME280_SPI_READ;
//select the device
digitalWrite(m_settings.spiCsPin, LOW);
// transfer the addr
SPI.transfer(readAddr);
// read the data
for(int i = 0; i < len; ++i)
{
// transfer 0x00 to get the data
data[i] = SPI.transfer(0);
}
// de-select the device
digitalWrite(m_settings.spiCsPin, HIGH);
SPI.endTransaction();
return true;
}
/****************************************************************/
bool BME280Spi::WriteRegister
(
uint8_t addr,
uint8_t data
)
{
SPI.beginTransaction(SPISettings(500000,MSBFIRST,SPI_MODE0));
// bme280 uses the msb to select read and write
// combine the addr with the read/write bit
uint8_t writeAddr = addr & ~0x80;
// select the device
digitalWrite(m_settings.spiCsPin, LOW);
// transfer the addr and then the data to spi device
SPI.transfer(writeAddr);
SPI.transfer(data);
// de-select the device
digitalWrite(m_settings.spiCsPin, HIGH);
SPI.endTransaction();
return true;
}