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:
47
50
52 void Init(const Config& cfg)
53 {
54 config_ = cfg;
55 // Init GPIO
57 clk_.pull = DSY_GPIO_NOPULL;
58 clk_.pin = cfg.clk;
59 dsy_gpio_init(&clk_);
61 latch_.pull = DSY_GPIO_NOPULL;
62 latch_.pin = cfg.latch;
63 dsy_gpio_init(&latch_);
64 for(size_t i = 0; i < num_parallel; i++)
65 {
66 data_[i].mode = DSY_GPIO_MODE_INPUT;
67 data_[i].pull = DSY_GPIO_NOPULL;
68 data_[i].pin = cfg.data[i];
69 dsy_gpio_init(&data_[i]);
70 }
71 // Init States
72 for(size_t i = 0; i < kTotalStates; i++)
73 {
74 states_[i] = false;
75 }
76 }
77
79 void Update()
80 {
81 dsy_gpio_write(&clk_, 0);
82 dsy_gpio_write(&latch_, 1);
84 dsy_gpio_write(&latch_, 0);
86 for(size_t i = 0; i < 8 * num_daisychained; i++)
87 {
88 dsy_gpio_write(&clk_, 0);
90 for(size_t j = 0; j < num_parallel; j++)
91 {
92 idx = (8 * num_daisychained - 1) - i;
93 idx += (8 * num_daisychained * j);
94 states_[idx] = dsy_gpio_read(&data_[j]);
95 }
96 dsy_gpio_write(&clk_, 1);
98 }
99 }
100
107 inline bool State(int index) const { return states_[index]; }
108
109 inline const Config& GetConfig() const { return config_; }
110
111 private:
112 static constexpr int kTotalStates = 8 * num_daisychained * num_parallel;
113 Config config_;
114 bool states_[kTotalStates];
115 dsy_gpio clk_;
116 dsy_gpio latch_;
117 dsy_gpio data_[num_parallel];
118};
119
120} // namespace daisy
121
122#endif
Definition leddriver.h:33
Device Driver for CD4021 shift register.
Definition sr_4021.h:38
const Config & GetConfig() const
Definition sr_4021.h:109
ShiftRegister4021()
Definition sr_4021.h:48
~ShiftRegister4021()
Definition sr_4021.h:49
bool State(int index) const
Definition sr_4021.h:107
void Update()
Definition sr_4021.h:79
void Init(const Config &cfg)
Definition sr_4021.h:52
static void DelayTicks(uint32_t delay_ticks)
void dsy_gpio_write(const dsy_gpio *p, uint8_t state)
void dsy_gpio_init(const dsy_gpio *p)
uint8_t dsy_gpio_read(const dsy_gpio *p)
@ DSY_GPIO_MODE_INPUT
Definition gpio.h:158
@ DSY_GPIO_MODE_OUTPUT_PP
Definition gpio.h:159
@ DSY_GPIO_NOPULL
Definition gpio.h:168
Hardware defines and helpers for daisy field platform.
Definition index.h:2
Definition sr_4021.h:42
dsy_gpio_pin latch
Definition sr_4021.h:44
dsy_gpio_pin data[num_parallel]
Definition sr_4021.h:45
dsy_gpio_pin clk
Definition sr_4021.h:43
Definition daisy_core.h:205
Definition gpio.h:175
dsy_gpio_mode mode
Definition gpio.h:177
dsy_gpio_pin pin
Definition gpio.h:176
dsy_gpio_pull pull
Definition gpio.h:178