我正在使用DHT11 library,其中基于esp32 implementation的参数gpio
对于new必须实现InputPin
和OutputPin
.
所以我创建了一个函数,它返回这两个特征中的一个超级特征的TraitObject.但当我想取消对它的引用以实际调用new
时,我得到了错误doesn't have a size known at compile-time
.
这是我从以上提到的trait 中得出的超级trait :
trait InputOutputPin: InputPin<Error = EspError> + OutputPin<Error = EspError> {}
impl<T: InputPin<Error = EspError> + OutputPin<Error = EspError>> InputOutputPin for T {}
这是根据给定的管脚编号创建GPIO的函数:
fn get_gpio(pin: &u8) -> Result<&dyn InputOutputPin, &'static str>{
match pin {
33 => Ok(&Peripherals::take().unwrap().pins.gpio33.into_input_output().unwrap()),
32 => Ok(&Peripherals::take().unwrap().pins.gpio32.into_input_output().unwrap()),
27 => Ok(&Peripherals::take().unwrap().pins.gpio27.into_input_output().unwrap()),
26 => Ok(&Peripherals::take().unwrap().pins.gpio26.into_input_output().unwrap()),
25 => Ok(&Peripherals::take().unwrap().pins.gpio25.into_input_output().unwrap()),
_ => Err("Pin not configurable for dht")
}
}
下面是函数结果的赋值方式:
let gpio = Self::get_gpio(pin).unwrap();
let dht = Dht11::new(*gpio);
我所要做的就是根据所 Select 的管脚编号创建一个DHT11对象,但是the esp32 library通过使用makro将每个GPIO实现为其自己的 struct . 我错过了什么,或者有没有一个明显的、更好的方法来做到这一点.