AceSPI  0.2.0
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 send8(uint8_t value) const {
69  digitalWrite(mLatchPin, LOW);
70  shiftOut(mDataPin, mClockPin, MSBFIRST, value);
71  digitalWrite(mLatchPin, HIGH);
72  }
73 
75  void send16(uint16_t value) const {
76  uint8_t msb = (value & 0xff00) >> 8;
77  uint8_t lsb = (value & 0xff);
78  send16(msb, lsb);
79  }
80 
82  void send16(uint8_t msb, uint8_t lsb) const {
83  digitalWrite(mLatchPin, LOW);
84  shiftOut(mDataPin, mClockPin, MSBFIRST, msb);
85  shiftOut(mDataPin, mClockPin, MSBFIRST, lsb);
86  digitalWrite(mLatchPin, HIGH);
87  }
88 
89  // Use default copy constructor and assignment operator.
90  SimpleSpiInterface(const SimpleSpiInterface&) = default;
91  SimpleSpiInterface& operator=(const SimpleSpiInterface&) = default;
92 
93  private:
94  uint8_t const mLatchPin;
95  uint8_t const mDataPin;
96  uint8_t const mClockPin;
97 };
98 
99 } // ace_spi
100 
101 #endif
ace_spi::SimpleSpiInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Send 2 bytes as 16-bit stream, including latching LOW and HIGH.
Definition: SimpleSpiInterface.h:82
ace_spi::SimpleSpiInterface::send8
void send8(uint8_t value) const
Send 8 bits, including latching LOW and HIGH.
Definition: SimpleSpiInterface.h:68
ace_spi::SimpleSpiInterface::send16
void send16(uint16_t value) const
Send 16 bits, including latching LOW and HIGH.
Definition: SimpleSpiInterface.h:75
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
Software SPI using shiftOut().
Definition: SimpleSpiInterface.h:34