AceSPI  0.3
Unified interface for selecting hardware or software SPI implementations on Arduino platforms
SimpleSpiInterface.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_SPI_SIMPLE_SPI_INTERFACE_H
26 #define ACE_SPI_SIMPLE_SPI_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h>
30 
31 namespace ace_spi {
32 
35  public:
44  uint8_t latchPin,
45  uint8_t dataPin,
46  uint8_t clockPin
47  ) :
48  mLatchPin(latchPin),
49  mDataPin(dataPin),
50  mClockPin(clockPin)
51  {}
52 
54  void begin() const {
55  pinMode(mLatchPin, OUTPUT);
56  pinMode(mDataPin, OUTPUT);
57  pinMode(mClockPin, OUTPUT);
58  }
59 
61  void end() const {
62  pinMode(mLatchPin, INPUT);
63  pinMode(mDataPin, INPUT);
64  pinMode(mClockPin, INPUT);
65  }
66 
68  void beginTransaction() const {
69  digitalWrite(mLatchPin, LOW);
70  }
71 
73  void endTransaction() const {
74  digitalWrite(mLatchPin, HIGH);
75  }
76 
78  void transfer(uint8_t value) const {
79  shiftOut(mDataPin, mClockPin, MSBFIRST, value);
80  }
81 
83  void transfer16(uint16_t value) const {
84  uint8_t msb = (value & 0xff00) >> 8;
85  uint8_t lsb = (value & 0xff);
86  shiftOut(mDataPin, mClockPin, MSBFIRST, msb);
87  shiftOut(mDataPin, mClockPin, MSBFIRST, lsb);
88  }
89 
91  void send8(uint8_t value) const {
93  transfer(value);
95  }
96 
98  void send16(uint16_t value) const {
100  transfer16(value);
101  endTransaction();
102  }
103 
105  void send16(uint8_t msb, uint8_t lsb) const {
107  shiftOut(mDataPin, mClockPin, MSBFIRST, msb);
108  shiftOut(mDataPin, mClockPin, MSBFIRST, lsb);
109  endTransaction();
110  }
111 
112  // Use default copy constructor and assignment operator.
113  SimpleSpiInterface(const SimpleSpiInterface&) = default;
114  SimpleSpiInterface& operator=(const SimpleSpiInterface&) = default;
115 
116  private:
117  uint8_t const mLatchPin;
118  uint8_t const mDataPin;
119  uint8_t const mClockPin;
120 };
121 
122 } // ace_spi
123 
124 #endif
ace_spi::SimpleSpiInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Convenience method to send 16 bits a single transaction.
Definition: SimpleSpiInterface.h:105
ace_spi::SimpleSpiInterface::transfer16
void transfer16(uint16_t value) const
Transfer 16 bits.
Definition: SimpleSpiInterface.h:83
ace_spi::SimpleSpiInterface::send8
void send8(uint8_t value) const
Convenience method to send 8 bits a single transaction.
Definition: SimpleSpiInterface.h:91
ace_spi::SimpleSpiInterface::transfer
void transfer(uint8_t value) const
Transfer 8 bits.
Definition: SimpleSpiInterface.h:78
ace_spi::SimpleSpiInterface::beginTransaction
void beginTransaction() const
Begin SPI transaction.
Definition: SimpleSpiInterface.h:68
ace_spi::SimpleSpiInterface::send16
void send16(uint16_t value) const
Convenience method to send 16 bits a single transaction.
Definition: SimpleSpiInterface.h:98
ace_spi::SimpleSpiInterface::end
void end() const
Reset the various pins.
Definition: SimpleSpiInterface.h:61
ace_spi::SimpleSpiInterface::begin
void begin() const
Initialize the various pins.
Definition: SimpleSpiInterface.h:54
ace_spi::SimpleSpiInterface::SimpleSpiInterface
SimpleSpiInterface(uint8_t latchPin, uint8_t dataPin, uint8_t clockPin)
Constructor.
Definition: SimpleSpiInterface.h:43
ace_spi::SimpleSpiInterface::endTransaction
void endTransaction() const
End SPI transaction.
Definition: SimpleSpiInterface.h:73
ace_spi::SimpleSpiInterface
Software SPI using shiftOut().
Definition: SimpleSpiInterface.h:34