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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! This crate provides an interface, `Multiplexer` that makes it trivially easy
//! to select channels on any given 74HC4051 or 74HC4067 series analog multiplexer.
//! Internally it keeps track of each multiplexer's state, allowing you to
//! check what channel is presently active or to enable/disable the multiplexer
//! at will.
//!
//! # Example using a 74HC4067 with a Blue Pill (stm32f104) board
//!
//! ```
//! // NOTE: This is pseudocode. It's just meant to get the concept across :)
//! use analog_multiplexer::Multiplexer; // Important part
//!
//! use stm32f1xx_hal::gpio::State;
//! // The pins we're using:
//! use stm32f1xx_hal::gpio::gpiob::{PB0, PB5, PB12, PB13, PB14, PB15};
//! use stm32f1xx_hal::{adc}; // So we can read an analog pin (PB0)
//!
//! fn main() {
//! // stm32f1xx_hal boilerplate...
//! let device = pac::Peripherals::take().unwrap();
//! let mut flash = device.FLASH.constrain();
//! let mut rcc = device.RCC.constrain();
//! let mut _afio = device.AFIO.constrain(&mut rcc.apb2);
//! let _clocks = rcc
//! .cfgr
//! .use_hse(8.mhz())
//! .sysclk(72.mhz())
//! .pclk1(36.mhz())
//! .freeze(&mut flash.acr);
//! // Setup ADC (we're using ADC1 for this example since we're reading PB0)
//! let adc1 = adc::Adc::adc1(device.ADC1, &mut rcc.apb2, _clocks);
//! // Setup GPIOB (so we can access the ADC via PB0)
//! let mut gpiob = device.GPIOB.split(&mut rcc.apb2);
//! // Configure PB0 as an analog input (all channels lead to this analog input pin!)
//! let analog_pin = gpiob.pb0.into_analog(&mut gpiob.crl);
//! // Setup PB12-PB15 for accessing S0-S3 on the 74HC4067 multiplexer
//! let s0 = gpiob
//! .pb12
//! .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
//! let s1 = gpiob
//! .pb13
//! .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
//! let s2 = gpiob
//! .pb14
//! .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
//! let s3 = gpiob
//! .pb15
//! .into_push_pull_output_with_state(&mut gpiob.crh, State::Low);
//! // NOTE: On some multiplexers the S0-S3 pins are labeled A, B, C, D
//! // Enable pin... If you want to be able to enable/disable the multiplexer on-the-fly
//! let en = gpiob
//! .pb5
//! .into_push_pull_output_with_state(&mut gpiob.crl, State::Low);
//! // TIP: Just run a wire from EN to GND to keep it enabled all the time
//! // Multiplexer pins are given as a tuple in the order S0-S3 then enable pin (EN):
//! let pins = (s0,s1,s2,s3,en); // For 16-channel
//! // let pins = (s0,s1,s2,en); // For 8-channel
//! let mut multiplexer = Multiplexer::new(pins); // The important part!
//! multiplexer.enable(); // Make sure it's enabled (if using EN pin)
//! loop {
//! for chan in 0..multiplexer.num_channels {
//! multiplexer.set_channel(chan); // Change the channel
//! let data: u16 = adc1.read(&mut *analog_pin).unwrap();
//! // Do something with the data here
//! }
//! }
//! }
//!
//! ```
//!
//! **NOTE:** There's a working Blue Pill/RTIC example in the `examples` directory.
//!
extern crate embedded_hal as hal;
use Infallible;
use OutputPin;
/// Provides an interface for setting the active channel
/// and enabling/disabling an 8-channel (74HC4051) or
/// 16-channel (74HC4067) analog multiplexer. It also
/// keeps track of which channel is currently active
/// (`active_channel`) and provides a convenient
/// `num_channels` field that can be used to iterate
/// over all the multiplexer's channels.
/// A trait so we can support both 8-channel and 16-channel
/// multiplexers simultaneously by merely instantiating them
/// with a 5 (16-channel) or 4 (8-channel) member tuple of
/// `OutputPin`s.
/// A 5-pin implementation to support 16-channel multiplexers (e.g. 74HC4067)
/// A 4-pin implementation to support 8-channel multiplexers (e.g. 74HC4051)
/// A DummyPin for when you've got your EN (enable) pin run to GND
/// (the analog multiplexer is always enabled)
;