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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use rppal::i2c;
use std::time::Duration;
use async_std::task;
pub const PCA9685_MODE1: u8 = 0x00;
pub const PCA9685_MODE2: u8 = 0x01;
pub const PCA9865_SUBADR1: u8 = 0x02;
pub const PCA9865_SUBADR2: u8 = 0x03;
pub const PCA9865_SUBADR3: u8 = 0x04;
pub const PCA9685_ALLCALLADR: u8 = 0x05;
pub const PCA9685_LED0_ON_L: u8 = 0x06;
pub const PCA9685_LED0_ON_H: u8 = 0x07;
pub const PCA9685_LED0_OFF_L: u8 = 0x08;
pub const PCA9685_LED0_OFF_H: u8 = 0x09;
pub const PCA9685_ALLLED_ON_L: u8 = 0xFA;
pub const PCA9685_ALLLED_ON_H: u8 = 0xFB;
pub const PCA9685_ALLLED_OFF_L: u8 = 0xFC;
pub const PCA9685_ALLLED_OFF_H: u8 = 0xFD;
pub const PCA9685_PRESCALE: u8 = 0xFE;
pub const PCA9685_TESTMODE: u8 = 0xFF;
pub const MODE1_ALLCAL: u8 = 0x01;
pub const MODE1_SUB3: u8 = 0x02;
pub const MODE1_SUB2: u8 = 0x04;
pub const MODE1_SUB1: u8 = 0x08;
pub const MODE1_SLEEP: u8 = 0x10;
pub const MODE1_AI: u8 = 0x20;
pub const MODE1_EXTCLK: u8 = 0x40;
pub const MODE1_RESTART: u8 = 0x80;
pub const MODE2_OUTNE_0: u8 = 0x01;
pub const MODE2_OUTNE_1: u8 = 0x02;
pub const MODE2_OUTDRV: u8 = 0x04;
pub const MODE2_OCH: u8 = 0x08;
pub const MODE2_INVRT: u8 = 0x10;
pub const FREQUENCY_OSCILLATOR: u32 = 25000000;
pub const PCA9685_PRESCALE_MIN: u8 = 3;
pub const PCA9685_PRESCALE_MAX: u8 = 255;
pub struct PCA9685 {
address: u8,
bus: i2c::I2c,
oscillator_freq: u32,
}
impl PCA9685 {
pub fn new(address: u8, bus: i2c::I2c) -> Result<PCA9685, i2c::Error> {
let mut dev = PCA9685 {
address,
bus,
oscillator_freq: FREQUENCY_OSCILLATOR,
};
if let Err(e) = dev.bus.set_slave_address(dev.address as u16) {
return Err(e);
}
Ok(dev)
}
pub async fn begin(&mut self, prescale: u8 ) -> Result<(), i2c::Error> {
match prescale {
p if p == 0 => {
return self.set_pwm_freq(1000.0).await;
}
p if p < PCA9685_PRESCALE_MIN => panic!("Prescale is less than it should be!"),
p if p > PCA9685_PRESCALE_MAX => panic!("Prescale is greater than it should be!"),
_ => {
if let Err(e) = self.set_ext_clock(prescale).await {
return Err(e);
}
}
};
self.set_osc_frequency(self.oscillator_freq);
self.reset().await
}
pub async fn reset(&mut self) -> Result<(), rppal::i2c::Error> {
let res = self.bus.write(&vec![PCA9685_MODE1, MODE1_RESTART]);
if let Err(e) = res {
return Err(e);
}
task::sleep(Duration::from_millis(10)).await;
Ok(())
}
pub async fn sleep(&mut self) -> Result<(), rppal::i2c::Error> {
let mut buf = vec![0 as u8];
if let Err(e) = self.bus.write_read(&vec![PCA9685_MODE1], &mut buf) {
return Err(e);
}
let awake = buf.get(0).unwrap();
let sleep = awake | MODE1_SLEEP;
if let Err(e) = self.bus.write(&vec![PCA9685_MODE1, sleep]) {
return Err(e);
}
task::sleep(Duration::from_millis(5)).await;
Ok(())
}
pub async fn wakeup(&mut self) -> Result<(), rppal::i2c::Error> {
let mut buf = vec![0 as u8];
if let Err(e) = self.bus.write_read(&vec![PCA9685_MODE1], &mut buf) {
return Err(e);
}
let sleep = buf.get(0).unwrap();
let awake = sleep & !MODE1_SLEEP;
if let Err(e) = self.bus.write(&vec![PCA9685_MODE1, awake]) {
return Err(e);
}
Ok(())
}
pub fn write_micros(&mut self, channel: u8, micros: u16) -> Result<(), i2c::Error> {
let pulse = micros;
let (prescale, result) = self.get_prescale();
if let Err(e) = result {
return Err(e);
}
let prescale = prescale + 1;
let pulselength = 1_000_000 * prescale;
let pulselength = pulselength / self.oscillator_freq as usize;
let pulse = pulse / pulselength as u16;
if let Err(e) = self.set_pwm(channel, 0, pulse as u16) {
return Err(e);
}
Ok(())
}
pub async fn set_pwm_freq(&mut self, freq: f32) -> Result<(), rppal::i2c::Error> {
match freq {
f if f < 1.0 => panic!("Frequency Cannot Be Lower than 1!"),
f if f > 3052.0 => panic!("Datasheet Max is 3052hz"),
_ => {},
}
let prescale_val = ((self.oscillator_freq as f32 / (freq * 4096.0)) + 0.5) - 1.0;
let prescale = match prescale_val {
p if p < PCA9685_PRESCALE_MIN as f32 => PCA9685_PRESCALE_MIN as f32,
p if p > PCA9685_PRESCALE_MAX as f32 => PCA9685_PRESCALE_MAX as f32,
_ => prescale_val
};
let mut buf = vec![0 as u8];
if let Err(e) = self.bus.write_read(&vec![PCA9685_MODE1], &mut buf) {
return Err(e);
}
let oldmode = buf.get(0).unwrap();
let newmode = (oldmode * !MODE1_RESTART) | MODE1_SLEEP;
if let Err(e) = self.bus.write(&vec![
PCA9685_MODE1, newmode
]) {
return Err(e);
}
if let Err(e) = self.bus.write(&vec![
PCA9685_PRESCALE, prescale as u8
]) {
return Err(e);
}
if let Err(e) = self.bus.write(&vec![
PCA9685_MODE1, *oldmode
]) {
return Err(e);
}
task::sleep(Duration::from_millis(5)).await;
Ok(())
}
pub fn set_pin(&mut self, channel: u8, mut on_tick: u16, invert: bool) -> Result<usize,rppal::i2c::Error> {
if on_tick > 4095 {
on_tick = 4095;
}
if invert {
if on_tick == 0 {
self.set_pwm(channel, 4096, 0)
} else if on_tick == 4095 {
self.set_pwm(channel, 0, 4096)
} else {
self.set_pwm(channel, 0, 4095-on_tick)
}
} else {
if on_tick == 4095 {
self.set_pwm(channel, 4096, 0)
} else if on_tick == 0 {
self.set_pwm(channel, 0, 4096)
} else {
self.set_pwm(channel, 0, on_tick)
}
}
}
pub fn set_pwm(&mut self, channel: u8, on: u16, off: u16) -> Result<usize, rppal::i2c::Error> {
let on_bytes = on.to_be_bytes();
let off_bytes = off.to_be_bytes();
if channel > 15 {
panic!("There are only 16 channels on the chip.");
}
self.bus.write(&vec![
PCA9685_LED0_ON_L + 4 * channel,
on_bytes[1],
on_bytes[0],
off_bytes[1],
off_bytes[0],
])
}
pub fn get_osc_frequency(&self) -> u32 {
self.oscillator_freq
}
pub fn set_osc_frequency(&mut self, freq: u32) {
self.oscillator_freq = freq;
}
pub fn get_prescale(&mut self) -> (usize, i2c::Result<()>) {
let mut buf = vec![0 as u8];
let result = self.bus.write_read( &vec![PCA9685_PRESCALE],&mut buf[..]);
((*buf.get(0).unwrap()).into(), result)
}
pub async fn set_ext_clock(&mut self, prescale: u8) -> Result<(), rppal::i2c::Error> {
let mut buf = vec![0 as u8];
let result = self.bus.write_read(&vec![PCA9685_MODE1], &mut buf);
if let Err(e) = result {
return Err(e);
}
let oldmode = buf.get(0).unwrap();
let newmode = (oldmode & !MODE1_RESTART) | MODE1_SLEEP;
if let Err(e) = self.bus.write(&vec![
PCA9685_MODE1,
newmode
]){
return Err(e);
}
if let Err(e) = self.bus.write(&vec![
PCA9685_PRESCALE,
prescale
]) {
return Err(e);
}
task::sleep(Duration::from_millis(5)).await;
if let Err(e) = self.bus.write(&vec![
PCA9685_MODE1,
(newmode & !MODE1_SLEEP) | MODE1_RESTART | MODE1_AI
]) {
return Err(e);
}
Ok(())
}
pub fn set_output_mode(&mut self, totempole: bool) -> Result<(), rppal::i2c::Error> {
let mut buf = vec![0 as u8];
if let Err(e) = self.bus.write_read(
&vec![PCA9685_MODE2], &mut buf
) {
return Err(e);
}
let oldmode = buf.get(0).unwrap();
let newmode: u8;
if totempole {
newmode = oldmode | MODE2_OUTDRV;
} else {
newmode = oldmode & !MODE2_OUTDRV;
}
if let Err(e) = self.bus.write(&vec![
PCA9685_MODE2, newmode
]) {
return Err(e);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}