gpt4 book ai didi

Traits collecting different pins and timers(收集不同管脚和计时器的特征)

转载 作者:bug小助手 更新时间:2023-10-26 20:14:43 26 4
gpt4 key购买 nike



I am using avr-hal to control the rgb for a LED on the digital output on a arduino uno and use set_duty for a variety of TimerPWM and PDs to change the colors. Since this is working fine and has the same function call everywhere I would now like to combine the functionality for every pin into a single function.
But I am not able to find a trait combining the arbitrary Timers and PDs into one type, just having pin.set_duty(duty:u8) available.

我正在使用AVR-HAL来控制Arduino uno上数字输出上LED的RGB,并使用SET_DUTY来改变各种定时器和PD的颜色。因为这运行得很好,并且在任何地方都有相同的函数调用,所以我现在想将每个管脚的功能组合到一个函数中。但我找不到将任意定时器和PD组合成一种类型的特征,只有Pin.set_Duty(Duty:U8)可用。


How could one combine the two implementations of Blinks into fitting for both types: Pin<PwmOutput<Timer0Pwm>, PD6>and Pin<PwmOutput<Timer2Pwm>, PD3>

如何将闪烁的两种实现结合在一起以适应两种类型:Pin ,pd6>和Pin ,pd3>


#![no_std]
#![no_main]

use arduino_hal::hal::port::{PD3, PD6};
use arduino_hal::pac::TC0;
use arduino_hal::port::mode::{Analog, Input, Output, PwmOutput};
use arduino_hal::port::Pin;
use arduino_hal::prelude::*;
use arduino_hal::simple_pwm::{IntoPwmPin, Timer0Pwm, Timer2Pwm};
use arduino_hal::{DefaultClock, Peripherals, Pins, Usart};
use panic_halt as _;

trait Blinks {
fn blink(&mut self);
}

impl Blinks for Pin<PwmOutput<Timer0Pwm>, PD6> {
fn blink(&mut self) {
self.set_duty(200);
arduino_hal::delay_ms(100);
self.set_duty(0);
}
}

impl Blinks for Pin<PwmOutput<Timer2Pwm>, PD3> {
fn blink(&mut self) {
self.set_duty(200);
arduino_hal::delay_ms(100);
self.set_duty(0);
}
}

#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);

let mut pwm_timer0 = arduino_hal::simple_pwm::Timer0Pwm::new(
dp.TC0,
arduino_hal::simple_pwm::Prescaler::Prescale64,
);
let mut pwm_timer2 = arduino_hal::simple_pwm::Timer2Pwm::new(
dp.TC2,
arduino_hal::simple_pwm::Prescaler::Prescale64,
);

let mut red = pins.d6.into_output().into_pwm(&mut pwm_timer0);
let mut blue = pins.d3.into_output().into_pwm(&mut pwm_timer2);

red.enable();
blue.enable();

loop {
red.blink();
blue.blink();
}
}

I already tried to implement the trait for for Pin<PinOps + arduino_hal::prelude::_embedded_hal_Pwm>but then I am depending on the trait Dynamic and would loose the ability to set duty directly via set_duty(duty:u8).

我已经尝试为For Pin 实现了特征,但随后我依赖于特征动态,并且无法通过SET_DUTY(任务:U8)直接设置任务。


I also tried relying on PwmPinOps but then I am tightly bound to the timers again:

我也试过依赖PwmPinOps,但随后我又被计时器紧紧绑住了:


impl<TC, P> Blinks for P
where
P: PwmPinOps<TC, Duty = u8>,
{
fn blink(&mut self) {
self.set_duty(200);
arduino_hal::delay_ms(100);
self.set_duty(0);
}
}

// error[E0207]: the type parameter `TC` is not constrained by the impl // trait, self type, or predicates
// --> src/main.rs:87:6
// |
// 87 | impl<TC, P> Blinks for P
// | ^^ unconstrained type parameter



更多回答

Since set_duty is from the PwmPinOps trait it seems logical to use that trait as bound when implementing, can't test right now since I don't have access to a rust compiler.

由于Set_Duty来自PwmPinOps特性,因此在实现时使用该特性作为绑定似乎是合乎逻辑的,现在无法进行测试,因为我无法访问RUST编译器。

I did try this, but then TC gets in my way. See my last edit

我试过了,但TC挡住了我的路。查看我的上一次编辑

优秀答案推荐

The impl that the set_duty method itself belongs to is defined like this:

SET_DUTY方法本身所属的Impl定义如下:


impl<TC, PIN: PwmPinOps<TC>> Pin<mode::PwmOutput<TC>, PIN> {
// ...
}

This generic impl matches any Pin whose mode is a PwmOutput. You could implement your Blinks trait for the same set of types, like this:

此泛型Iml与模式为PwmOutput的任何管脚匹配。您可以为同一组类型实现您的Binks特征,如下所示:


impl<TC, PIN: PwmPinOps<TC>> Blinks for Pin<mode::PwmOutput<TC>, PIN> {
fn blink(&mut self) {
self.set_duty(200);
arduino_hal::delay_ms(100);
self.set_duty(0);
}
}

This trait would therefore be implemented for any pin type that could support the set_duty method, which should include both Pin<PwmOutput<Timer0Pwm>, PD6> and Pin<PwmOutput<Timer2Pwm>, PD3>, so your red and blue variables should both still support it.

因此,可以为任何可以支持SET_DUTY方法的管脚类型实现此特性,该方法应该同时包括Pin ,pd6>和Pin ,pd3>,因此您的红色和蓝色变量应该仍然支持它。


更多回答

Thank you! This fixed my problem and also gave much valuable Information how to tackle this situation in the future.

谢谢大家!这解决了我的问题,也给了很多有价值的信息,如何在未来解决这种情况。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com