mqtt_mux: add publish example

This commit is contained in:
occheung 2020-09-25 10:24:18 +08:00
parent f993ee5e28
commit 93f3b2dfb3
2 changed files with 32 additions and 11 deletions

View File

@ -96,10 +96,10 @@ fn main() -> ! {
.pll1_r_ck(400.mhz()) .pll1_r_ck(400.mhz())
.freeze(vos, &dp.SYSCFG); .freeze(vos, &dp.SYSCFG);
unsafe { // unsafe {
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM); // logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
} // }
logger::init(); // logger::init();
let mut delay = cp.SYST.delay(ccdr.clocks); let mut delay = cp.SYST.delay(ccdr.clocks);
@ -111,9 +111,11 @@ fn main() -> ! {
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF); let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG); let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
trace!("Flashing configuration bitstream to iCE40 HX8K on Humpback."); gpiob.pb3.into_alternate_af0().set_speed(Speed::VeryHigh);
delay.delay_ms(1000_u16);
// trace!("Flashing configuration bitstream to iCE40 HX8K on Humpback.");
// delay.delay_ms(1000_u16);
/*
// Using SPI_1 alternate functions (af5) // Using SPI_1 alternate functions (af5)
let fpga_sck = gpiob.pb3.into_alternate_af5(); let fpga_sck = gpiob.pb3.into_alternate_af5();
let fpga_sdo = gpiob.pb4.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(); flash_ice40_fpga(fpga_cfg_spi, fpga_ss, fpga_creset, fpga_cdone, delay).unwrap();
*/
// Configure ethernet IO // Configure ethernet IO
{ {
let _rmii_refclk = gpioa.pa1.into_alternate_af11().set_speed(Speed::VeryHigh); let _rmii_refclk = gpioa.pa1.into_alternate_af11().set_speed(Speed::VeryHigh);
@ -217,6 +219,11 @@ fn main() -> ! {
parts.spi1, parts.spi2, parts.spi3, parts.spi4, parts.spi5, parts.spi6, parts.spi7 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(); urukul.reset().unwrap();
info!("Test value: {}", urukul.test().unwrap()); info!("Test value: {}", urukul.test().unwrap());
@ -275,11 +282,16 @@ fn main() -> ! {
if connection && !has_subscribed && tick { if connection && !has_subscribed && tick {
match client.subscribe("Urukul/Control/#", &[]) { match client.subscribe("Urukul/Control/#", &[]) {
Ok(()) => has_subscribed = true, Ok(()) => has_subscribed = true,
Err(minimq::Error::NotReady) => {}, Err(minimq::Error::NotReady) => warn!("Minimq is not ready"),
e => warn!("{:?}", e), 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 // Reset tick flag
tick = false; tick = false;
} }

View File

@ -278,6 +278,15 @@ impl<SPI, E> MqttMux<SPI> where SPI: Transfer<u8, Error = E> {
MqttCommand::Profile(prof) => self.urukul.set_profile(prof), MqttCommand::Profile(prof) => self.urukul.set_profile(prof),
} }
} }
pub fn get_switch_status_message(&mut self, channel: u8) -> Result<&str, Error<E>> {
self.urukul.get_channel_switch_status(channel.into()).map(
|stat| if stat {
"on"
} else {
"off"
})
}
} }
// Read message parameter separator (optional comma and whitespace) // Read message parameter separator (optional comma and whitespace)