libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
sr_4021.h
Go to the documentation of this file.
1#pragma once
2#ifndef DEV_SR_4021_H
3#define DEV_SR_4021_H
4#include "per/gpio.h"
5#include "sys/system.h"
6
7namespace daisy
8{
36template <size_t num_daisychained = 1, size_t num_parallel = 1>
38{
39 public:
54
57
59 void Init(const Config& cfg)
60 {
61 config_ = cfg;
62 // Init GPIO
63 clk_.Init(cfg.clk, GPIO::Mode::OUTPUT);
64 latch_.Init(cfg.latch, GPIO::Mode::OUTPUT);
65 for(size_t i = 0; i < num_parallel; i++)
66 {
67 data_[i].Init(cfg.data[i], GPIO::Mode::INPUT);
68 }
69 // Init States
70 for(size_t i = 0; i < kTotalStates; i++)
71 {
72 states_[i] = false;
73 }
74 }
75
77 void Update()
78 {
80 clk_.Write(false);
81 latch_.Write(true);
83 latch_.Write(false);
85 for(size_t i = 0; i < 8 * num_daisychained; i++)
86 {
87 clk_.Write(false);
89 for(size_t j = 0; j < num_parallel; j++)
90 {
91 idx = (8 * num_daisychained - 1) - i;
92 idx += (8 * num_daisychained * j);
93 states_[idx] = data_[j].Read();
94 }
95 clk_.Write(true);
97 }
98 }
99
106 inline bool State(int index) const { return states_[index]; }
107
108 inline const Config& GetConfig() const { return config_; }
109
110 private:
111 static constexpr int kTotalStates = 8 * num_daisychained * num_parallel;
112 Config config_;
113 bool states_[kTotalStates];
114 GPIO clk_;
115 GPIO latch_;
116 GPIO data_[num_parallel];
117};
118
119} // namespace daisy
120
121#endif
General Purpose I/O control.
Definition gpio.h:22
void Write(bool state)
Changes the state of the GPIO hardware when configured as an OUTPUT.
bool Read()
Reads the state of the GPIO.
void Init()
Initialize the GPIO using the internal Config struct.
Definition leddriver.h:33
Device Driver for CD4021 shift register.
Definition sr_4021.h:38
const Config & GetConfig() const
Definition sr_4021.h:108
ShiftRegister4021()
Definition sr_4021.h:55
~ShiftRegister4021()
Definition sr_4021.h:56
bool State(int index) const
Definition sr_4021.h:106
void Update()
Definition sr_4021.h:77
void Init(const Config &cfg)
Definition sr_4021.h:59
static void DelayTicks(uint32_t delay_ticks)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition sr_4021.h:42
Pin data[num_parallel]
Definition sr_4021.h:45
Pin latch
Definition sr_4021.h:44
uint32_t delay_ticks
Definition sr_4021.h:52
Pin clk
Definition sr_4021.h:43