SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
IoExpanderSPI.hpp
Go to the documentation of this file.
1
29#pragma once
30
31#include "../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_IO_EXPANDER_SPI
33
34#include "IoExpanderBase.hpp"
35
45{
46public:
52 IoExpanderSPI(): _mosi(-1), _sck(-1), _miso(-1), _order(0), _cs(-1) {}
53
57 ~IoExpanderSPI() = default;
58
75 bool beginSPI(IoExpanderBase &base, int mosi, int miso, int sck, int cs)
76 {
77 _base = &base;
78 _mosi = mosi;
79 _miso = miso;
80 _sck = sck;
81 _cs = cs;
82
83 if (_mosi == -1 || sck == -1) {
84 SENSORLIB_LOG_E("MOSI and SCK pins must be specified");
85 return false;
86 }
87
88 _base->pinMode(_mosi, OUTPUT);
89 _base->pinMode(_sck, OUTPUT);
90
91 if (_cs != -1) {
92 _base->pinMode(_cs, OUTPUT);
93 _base->digitalWrite(_cs, HIGH);
94 }
95
96 if (_miso != -1) {
97 _base->pinMode(_miso, INPUT);
98 }
99 _base->digitalWrite(_mosi, HIGH);
100 _base->digitalWrite(_sck, HIGH);
101 return true;
102 }
103
110 void setBitOrder(uint8_t order)
111 {
112 _order = order & 1;
113 }
114
121 uint8_t transfer8(uint8_t val)
122 {
123 return transferDataBits(val, 8);
124 }
125
134 uint16_t transfer9(uint16_t val)
135 {
136 return transferDataBits(val, 9);
137 }
138
149 uint32_t transferDataBits(uint32_t val, uint32_t bits)
150 {
151 if (!_base) {
152 SENSORLIB_LOG_E("SPI not initialized (call beginSPI first)");
153 return 0;
154 }
155 uint32_t out = 0;
156 if (_cs != -1) _base->digitalWrite(_cs, LOW);
157 for (uint32_t i = 0; i < bits; i++) {
158 _base->digitalWrite(_sck, LOW);
159 out <<= 1;
160 if (_miso != -1 && _base->digitalRead(_miso)) out |= 0x01;
161 bool bitVal;
162 if (_order == 0) // MSB first
163 bitVal = (val >> (bits - 1 - i)) & 1;
164 else // LSB first
165 bitVal = (val >> i) & 1;
166 _base->digitalWrite(_mosi, bitVal ? HIGH : LOW);
167 _base->digitalWrite(_sck, HIGH);
168 }
169 if (_cs != -1) _base->digitalWrite(_cs, HIGH);
170 return out;
171 }
172
173private:
174 int _mosi;
175 int _sck;
176 int _miso;
177 int _order;
178 int _cs;
179 IoExpanderBase *_base;
180};
181
182#endif
@license MIT License
#define SENSORLIB_LOG_E(...)
Abstract base class for GPIO expander devices.
bool digitalRead(uint8_t pin)
Read the digital value from an input pin.
void pinMode(uint8_t pin, uint8_t mode)
Set the direction of a GPIO pin.
void digitalWrite(uint8_t pin, bool value)
Write a digital value to an output pin.
Software SPI master class for driving GPIO expanders via bit-banging.
bool beginSPI(IoExpanderBase &base, int mosi, int miso, int sck, int cs)
Initialize the software SPI interface.
uint32_t transferDataBits(uint32_t val, uint32_t bits)
Transfer an arbitrary number of bits over SPI.
void setBitOrder(uint8_t order)
Set the bit order for SPI transfers.
~IoExpanderSPI()=default
Destroy the IoExpanderSPI object.
uint8_t transfer8(uint8_t val)
Transfer 8 bits over SPI.
uint16_t transfer9(uint16_t val)
Transfer 9 bits over SPI.
IoExpanderSPI()
Construct a new IoExpanderSPI object.
uint8_t i