Added a way to consume the Interface object, and easy single-function way to create a network interface with mac and IP
This commit is contained in:
parent
3cad9cac57
commit
1d533b2104
1 changed files with 20 additions and 2 deletions
|
|
@ -1,7 +1,14 @@
|
||||||
use bus::{ActiveBus};
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
|
use embedded_hal::spi::FullDuplex;
|
||||||
|
use embedded_nal::Ipv4Addr;
|
||||||
|
|
||||||
|
use crate::{MacAddress,Mode};
|
||||||
|
|
||||||
|
use bus::{ActiveBus,FourWire,ActiveFourWire};
|
||||||
use device::Device;
|
use device::Device;
|
||||||
use network::Network;
|
use network::{Network,Manual};
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
|
use uninitialized_device::{UninitializedDevice,InitializeError};
|
||||||
|
|
||||||
pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
|
pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
|
||||||
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
|
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
|
||||||
|
|
@ -11,8 +18,19 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> {
|
||||||
fn new(device: Device<SpiBus, NetworkImpl>) -> Self {
|
fn new(device: Device<SpiBus, NetworkImpl>) -> Self {
|
||||||
Self { device: RefCell::new(device) }
|
Self { device: RefCell::new(device) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn release(self) -> Device<SpiBus, NetworkImpl> {
|
||||||
|
self.device.into_inner()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin> Interface<ActiveFourWire<Spi, ChipSelect>, Manual> {
|
||||||
|
pub fn setup(spi: Spi, cs: ChipSelect, mac: MacAddress, ip: Ipv4Addr) -> Result<Self, InitializeError<<ActiveFourWire<Spi, ChipSelect> as ActiveBus>::Error>> {
|
||||||
|
Ok(UninitializedDevice::new(FourWire::new(cs).activate(spi)).initialize_manual(mac, ip, Mode::default())?.into_interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<SpiBus: ActiveBus, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>> for Interface<SpiBus, NetworkImpl> {
|
impl<SpiBus: ActiveBus, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>> for Interface<SpiBus, NetworkImpl> {
|
||||||
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> {
|
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> {
|
||||||
Interface::<SpiBus, NetworkImpl>::new(device)
|
Interface::<SpiBus, NetworkImpl>::new(device)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue