From 93f3b2dfb30694f9e7605b98d4543b66ed8daa25 Mon Sep 17 00:00:00 2001 From: occheung Date: Fri, 25 Sep 2020 10:24:18 +0800 Subject: [PATCH] mqtt_mux: add publish example --- src/main.rs | 34 +++++++++++++++++++++++----------- src/mqtt_mux.rs | 9 +++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f96fad..069124a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,10 +96,10 @@ fn main() -> ! { .pll1_r_ck(400.mhz()) .freeze(vos, &dp.SYSCFG); - unsafe { - logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM); - } - logger::init(); + // unsafe { + // logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM); + // } + // logger::init(); let mut delay = cp.SYST.delay(ccdr.clocks); @@ -109,11 +109,13 @@ fn main() -> ! { let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD); let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE); let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF); - let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG); - - trace!("Flashing configuration bitstream to iCE40 HX8K on Humpback."); - delay.delay_ms(1000_u16); + let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG); + + gpiob.pb3.into_alternate_af0().set_speed(Speed::VeryHigh); + // trace!("Flashing configuration bitstream to iCE40 HX8K on Humpback."); + // delay.delay_ms(1000_u16); +/* // Using SPI_1 alternate functions (af5) let fpga_sck = gpiob.pb3.into_alternate_af5(); let fpga_sdo = gpiob.pb4.into_alternate_af5(); @@ -136,7 +138,7 @@ fn main() -> ! { ); flash_ice40_fpga(fpga_cfg_spi, fpga_ss, fpga_creset, fpga_cdone, delay).unwrap(); - +*/ // Configure ethernet IO { let _rmii_refclk = gpioa.pa1.into_alternate_af11().set_speed(Speed::VeryHigh); @@ -215,7 +217,12 @@ fn main() -> ! { let mut urukul = Urukul::new( parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7 - ); + ); + + unsafe { + logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM); + } + logger::init(); urukul.reset().unwrap(); info!("Test value: {}", urukul.test().unwrap()); @@ -275,11 +282,16 @@ fn main() -> ! { if connection && !has_subscribed && tick { match client.subscribe("Urukul/Control/#", &[]) { Ok(()) => has_subscribed = true, - Err(minimq::Error::NotReady) => {}, + Err(minimq::Error::NotReady) => warn!("Minimq is not ready"), e => warn!("{:?}", e), }; } + if connection && tick && (time % 3000) == 0 { + info!("Feedback from print publish: {:?}", client + .publish("Channel1/Switch", mqtt_mux.get_switch_status_message(1).unwrap().as_bytes(), QoS::AtMostOnce, &[])); + } + // Reset tick flag tick = false; } diff --git a/src/mqtt_mux.rs b/src/mqtt_mux.rs index ff36a75..b8ed0dc 100644 --- a/src/mqtt_mux.rs +++ b/src/mqtt_mux.rs @@ -278,6 +278,15 @@ impl MqttMux where SPI: Transfer { MqttCommand::Profile(prof) => self.urukul.set_profile(prof), } } + + pub fn get_switch_status_message(&mut self, channel: u8) -> Result<&str, Error> { + self.urukul.get_channel_switch_status(channel.into()).map( + |stat| if stat { + "on" + } else { + "off" + }) + } } // Read message parameter separator (optional comma and whitespace)