FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
sipo.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
24#ifndef SIPO_HH
25#define SIPO_HH
26
27#include "../gpio.h"
28
34namespace devices
35{
57 template<board::DigitalPin CLOCK_, board::DigitalPin LATCH_, board::DigitalPin DATA_> class SIPO
58 {
59 public:
61 static constexpr const board::DigitalPin CLOCK = CLOCK_;
63 static constexpr const board::DigitalPin LATCH = LATCH_;
65 static constexpr const board::DigitalPin DATA = DATA_;
66
68 SIPO(const SIPO&) = delete;
69 SIPO& operator=(const SIPO&) = delete;
71
79 SIPO() = default;
80
88 void init()
89 {
90 clock_.set_mode(gpio::PinMode::OUTPUT, false);
91 latch_.set_mode(gpio::PinMode::OUTPUT, true);
92 data_.set_mode(gpio::PinMode::OUTPUT, false);
93 }
94
102 template<typename T> void output(T data)
103 {
104 uint8_t* pdata = (uint8_t*) data;
105 latch_.clear();
106 for (uint8_t i = 0; i < sizeof(T); ++i) bit_bang_data(pdata[i]);
107 latch_.set();
108 }
109
111 // Specialized output for most common types
112 void output(uint8_t data) INLINE
113 {
114 latch_.clear();
115 bit_bang_data(data);
116 latch_.set();
117 }
118
119 void output(uint16_t data) INLINE
120 {
121 latch_.clear();
122 bit_bang_data(bits::HIGH_BYTE(data));
123 bit_bang_data(bits::LOW_BYTE(data));
124 latch_.set();
125 }
127
128 private:
129 void bit_bang_data(uint8_t data)
130 {
131 // Start with MSB
132 uint8_t mask = 0x80;
133 do
134 {
135 if (data & mask)
136 data_.set();
137 else
138 data_.clear();
139 clock_.set();
140 mask >>= 1;
141 clock_.clear();
142 } while (mask);
143 }
144
148 };
149}
150
151#endif /* SIPO_HH */
This template class supports one SIPO chip, connected to the MCU through 3 pins.
Definition: sipo.h:58
static constexpr const board::DigitalPin CLOCK
The output pin that will send the clock signal to the chip.
Definition: sipo.h:61
static constexpr const board::DigitalPin LATCH
The output pin that will tell the chip to copy its shift register content to its output pins.
Definition: sipo.h:63
void output(T data)
Handles output and latching of all bits in data to the SIPO chip.
Definition: sipo.h:102
static constexpr const board::DigitalPin DATA
The output pin that will send serial data to the chip.
Definition: sipo.h:65
void init()
Initialize (direction, value) all used pins.
Definition: sipo.h:88
SIPO()=default
Create a new SIPO handler, according to pins defined by class template parameters.
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
static constexpr uint8_t HIGH_BYTE(uint16_t value)
Extract the high byte (aka Most Significant Byte, MSB) of a uint16_t value.
Definition: bits.h:269
static constexpr uint8_t LOW_BYTE(uint16_t value)
Extract the low byte (aka Least Significant Byte, LSB) of a uint16_t value.
Definition: bits.h:261
DigitalPin
Defines all available digital input/output pins of the target MCU.
Definition: empty.h:56
Defines all API for all external devices supported by FastArduino.
typename FastPinType< DPIN_ >::TYPE FAST_PIN
Useful alias type to the FastPin type matching a given board::DigitalPin.
Definition: gpio.h:694
@ OUTPUT
Digital pin is configured as output.