Soldered BMP280 Arduino Library 1.0.0
Soldered Arduino library for BMP280 sensor breakout.
Loading...
Searching...
No Matches
Device.h
Go to the documentation of this file.
1/*
2 Device is an I2C/SPI compatible base class library.
3
4 Copyright (C) Martin Lindupp 2019
5
6 V1.0.0 -- Initial release
7 V1.0.1 -- Added ESP32 HSPI support
8 V1.0.2 -- Modification to allow external creation of HSPI object on ESP32
9 V1.0.3 -- Addition of SPI write and read byte masks
10 V1.0.4 -- Modification to allow user-defined pins for I2C operation on the ESP8266
11 V1.0.5 -- Modification to allow user-defined pins for I2C operation on the ESP32
12 V1.0.6 -- Initialise "device" constructor member variables in the same order they are declared
13 V1.0.7 -- Allow for additional TwoWire instances
14 V1.0.8 -- Fixed uninitialised "Wire" pointer for ESP8266/ESP32 with user defined I2C pins
15
16 The MIT License (MIT)
17 Permission is hereby granted, free of charge, to any person obtaining a copy
18 of this software and associated documentation files (the "Software"), to deal
19 in the Software without restriction, including without limitation the rights
20 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 copies of the Software, and to permit persons to whom the Software is
22 furnished to do so, subject to the following conditions:
23 The above copyright notice and this permission notice shall be included in all
24 copies or substantial portions of the Software.
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 SOFTWARE.
32*/
33
34#ifndef Device_h
35#define Device_h
36
37#include <Arduino.h>
38#include <SPI.h>
39#include <Wire.h>
40
42// Device Communications
44
45#if defined ARDUINO_ESP8266_GENERIC || defined ARDUINO_ESP32_DEV
52#else
54{
57};
58#endif
59
61// Device Class definition
63
64class Device
65{
66 public:
67 Device(TwoWire &twoWire); // Device object for I2C operation
68#ifdef ARDUINO_ESP8266_GENERIC
69 Device(uint8_t sda, uint8_t scl,
70 TwoWire &twoWire); // Device object for ESP8266 I2C operation with user-defined pins
71#endif
72 Device(uint8_t cs); // Device object for SPI operation
73#ifdef ARDUINO_ESP32_DEV
74 Device(uint8_t sda, uint8_t scl, TwoWire &twoWire); // Device object for ESP32 I2C operation with user-defined pins
75 Device(uint8_t cs, uint8_t spiPort,
76 SPIClass &spiClass); // Device object for ESP32 HSPI operation with supplied SPI object
77#endif
78 void setClock(uint32_t clockSpeed); // Set the I2C/SPI clock speed
79 protected:
80 void initialise(); // Initialise communications
81 void setI2CAddress(uint8_t addr); // Set the Device I2C address
82 void writeByte(uint8_t subAddress, uint8_t data); // I2C and SPI write byte wrapper function
83 uint8_t readByte(uint8_t subAddress); // I2C and SPI read byte wrapper function
84 void readBytes(uint8_t subAddress, uint8_t *dest, uint16_t count); // I2C and SPI read bytes wrapper function
85 private:
86 Comms comms; // Communications bus: I2C or SPI
87 uint8_t address; // The device I2C address
88 uint8_t cs; // The SPI chip select pin
89#ifdef ARDUINO_ESP32_DEV
90 uint8_t spiPort; // SPI port type VSPI or HSPI
91#endif
92 TwoWire *i2c; // Pointer to the Wire class
93 SPIClass *spi; // Pointer to the SPI class
94 uint32_t spiClockSpeed; // The SPI clock speed
95 const uint8_t WRITE_MASK = 0x7F; // Sub-address write mask for SPI communications
96 const uint8_t READ_MASK = 0x80; // Sub-address read mask for SPI communications
97#if defined ARDUINO_ESP8266_GENERIC || defined ARDUINO_ESP32_DEV
98 uint8_t sda, scl; // Software I2C SDA and SCL pins
99#endif
100};
101#endif
Comms
Definition Device.h:47
@ SPI_COMMS
Definition Device.h:49
@ I2C_COMMS
Definition Device.h:48
@ I2C_COMMS_DEFINED_PINS
Definition Device.h:50
Definition Device.h:65
uint8_t address
Definition Device.h:87
void setI2CAddress(uint8_t addr)
Definition Device.cpp:120
uint8_t sda
Definition Device.h:98
uint8_t readByte(uint8_t subAddress)
Definition Device.cpp:146
const uint8_t READ_MASK
Definition Device.h:96
const uint8_t WRITE_MASK
Definition Device.h:95
void setClock(uint32_t clockSpeed)
Definition Device.cpp:68
void readBytes(uint8_t subAddress, uint8_t *dest, uint16_t count)
Definition Device.cpp:170
uint32_t spiClockSpeed
Definition Device.h:94
uint8_t scl
Definition Device.h:98
uint8_t spiPort
Definition Device.h:90
Device(TwoWire &twoWire)
Definition Device.cpp:40
Device(uint8_t sda, uint8_t scl, TwoWire &twoWire)
void initialise()
Definition Device.cpp:84
TwoWire * i2c
Definition Device.h:92
SPIClass * spi
Definition Device.h:93
void writeByte(uint8_t subAddress, uint8_t data)
Definition Device.cpp:125
uint8_t cs
Definition Device.h:88
Comms comms
Definition Device.h:86