-
Notifications
You must be signed in to change notification settings - Fork 134
/
SPIShiftRegisterOut.ipp
39 lines (33 loc) · 1.11 KB
/
SPIShiftRegisterOut.ipp
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
#ifdef ARDUINO // I'm too lazy to mock the SPI library
#include "SPIShiftRegisterOut.hpp"
#include "ExtendedInputOutput.hpp"
#include <SPI.h>
using namespace ExtIO;
template <uint8_t N>
SPIShiftRegisterOut<N>::SPIShiftRegisterOut(pin_t latchPin, uint8_t bitOrder)
: ShiftRegisterOutBase<N>(latchPin, bitOrder) {}
template <uint8_t N>
void SPIShiftRegisterOut<N>::begin() {
ExtIO::pinMode(this->latchPin, OUTPUT);
SPI.begin();
update();
}
template <uint8_t N>
void SPIShiftRegisterOut<N>::update() {
if (!this->dirty)
return;
SPISettings settings = {SPI_MAX_SPEED, this->bitOrder, SPI_MODE0};
SPI.beginTransaction(settings);
ExtIO::digitalWrite(this->latchPin, LOW);
const uint8_t bufferLength = this->buffer.getBufferLength();
if (this->bitOrder == LSBFIRST)
for (uint8_t i = 0; i < bufferLength; i++)
SPI.transfer(this->buffer.getByte(i));
else
for (int8_t i = bufferLength - 1; i >= 0; i--)
SPI.transfer(this->buffer.getByte(i));
ExtIO::digitalWrite(this->latchPin, HIGH);
SPI.endTransaction();
this->dirty = false;
}
#endif