PlantCtrl/next/src/main.rs

98 lines
2.7 KiB
Rust

use esp_idf_hal::{
adc::{
attenuation,
oneshot::{config::AdcChannelConfig, AdcChannelDriver, AdcDriver},
},
delay::Delay,
gpio::{AnyInputPin, IOPin, InputOutput, PinDriver, Pull},
peripherals::Peripherals,
sys::{esp_timer_get_time, vTaskDelay},
};
use next::sipo::ShiftRegister8;
use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!");
let peripherals = Peripherals::take().unwrap();
let mut s0 = PinDriver::input_output(peripherals.pins.gpio10.downgrade()).unwrap();
s0.set_low();
//s0 = 11
//s1 = 8
//s2 = 22
//s3 = 21
let mut counter_unit1 = PcntDriver::new(
peripherals.pcnt0,
Some(peripherals.pins.gpio5),
Option::<AnyInputPin>::None,
Option::<AnyInputPin>::None,
Option::<AnyInputPin>::None,
).unwrap();
println!("Channel config start");
counter_unit1.channel_config(
PcntChannel::Channel0,
PinIndex::Pin0,
PinIndex::Pin1,
&PcntChannelConfig {
lctrl_mode: PcntControlMode::Keep,
hctrl_mode: PcntControlMode::Keep,
pos_mode: PcntCountMode::Increment,
neg_mode: PcntCountMode::Hold,
counter_h_lim: i16::MAX,
counter_l_lim: 0,
},
).unwrap();
println!("Setup filter");
//TODO validate filter value! currently max allowed value
//counter_unit1.set_filter_value(1023).unwrap();
//counter_unit1.filter_enable().unwrap();
counter_unit1.counter_pause().unwrap();
let delay = Delay::new(1);
loop {
s0.set_low().unwrap();
delay.delay_ms(100);
let sensor0 = measure(&mut counter_unit1, &delay);
s0.set_high().unwrap();
delay.delay_ms(100);
let sensor1 = measure(&mut counter_unit1, &delay);
println!(
"Sensor a {}hz sensor b {}hz",
sensor0,
sensor1
);
}
}
fn measure (counter_unit1: &mut PcntDriver, delay: &Delay) -> u32{
counter_unit1.counter_clear().unwrap();
counter_unit1.counter_resume().unwrap();
delay.delay_ms(100);
counter_unit1.counter_pause().unwrap();
let sensor0 = counter_unit1.get_counter_value().unwrap() as u32 * 10;
return sensor0
}