| 1 | // Scaffold phase of the Arduino -> Rust rewrite: later phases consume the |
| 2 | // reserved Board peripherals/pins, so allow the not-yet-read fields for now. |
| 3 | #![allow(dead_code)] |
| 4 | |
| 5 | mod apps; |
| 6 | mod board; |
| 7 | mod drivers; |
| 8 | mod ui; |
| 9 | |
| 10 | use core::cell::RefCell; |
| 11 | |
| 12 | use esp_idf_hal::i2c::{config::Config as I2cConfig, I2cDriver}; |
| 13 | use esp_idf_hal::peripherals::Peripherals; |
| 14 | use esp_idf_hal::units::FromValueType; |
| 15 | |
| 16 | use board::board::{Board, Cc1101Pins, NrfPins, SharedI2c}; |
| 17 | use drivers::buttons::Buttons; |
| 18 | use drivers::display::Display; |
| 19 | |
| 20 | fn main() { |
| 21 | // Required once so esp-idf-sys runtime patches link. See |
| 22 | // https://github.com/esp-rs/esp-idf-template/issues/71 |
| 23 | esp_idf_svc::sys::link_patches(); |
| 24 | esp_idf_svc::log::EspLogger::initialize_default(); |
| 25 | |
| 26 | log::info!("ORION-RF booting"); |
| 27 | |
| 28 | let peripherals = Peripherals::take().expect("take peripherals"); |
| 29 | let pins = peripherals.pins; |
| 30 | |
| 31 | // Shared I2C0 bus (OLED + PN532). Leaked to 'static: the display is on for |
| 32 | // the whole program, so this bus never needs to be dropped or re-created. |
| 33 | let i2c = I2cDriver::new( |
| 34 | peripherals.i2c0, |
| 35 | pins.gpio8, // SDA |
| 36 | pins.gpio9, // SCL |
| 37 | &I2cConfig::new().baudrate(400u32.kHz().into()), |
| 38 | ) |
| 39 | .expect("i2c0 init"); |
| 40 | let i2c_bus: SharedI2c = Box::leak(Box::new(RefCell::new(i2c))); |
| 41 | |
| 42 | let mut display = Display::new(i2c_bus); |
| 43 | |
| 44 | // Buttons by physical label (active-low, internal pull-ups). |
| 45 | let mut buttons = Buttons::new( |
| 46 | pins.gpio4.degrade_input_output(), // UP |
| 47 | pins.gpio5.degrade_input_output(), // DOWN |
| 48 | pins.gpio6.degrade_input_output(), // SELECT |
| 49 | pins.gpio7.degrade_input_output(), // BACK |
| 50 | pins.gpio1.degrade_input_output(), // RIGHT |
| 51 | pins.gpio2.degrade_input_output(), // LEFT |
| 52 | ); |
| 53 | |
| 54 | // Everything the radio/NFC phases will consume, owned up front. |
| 55 | let mut board = Board { |
| 56 | i2c: i2c_bus, |
| 57 | modem: peripherals.modem, |
| 58 | spi2: peripherals.spi2, |
| 59 | spi3: peripherals.spi3, |
| 60 | nrf: NrfPins { |
| 61 | sck: pins.gpio18.degrade_input_output(), |
| 62 | miso: pins.gpio16.degrade_input_output(), |
| 63 | mosi: pins.gpio17.degrade_input_output(), |
| 64 | ce1: pins.gpio10.degrade_input_output(), |
| 65 | csn1: pins.gpio11.degrade_input_output(), |
| 66 | ce2: pins.gpio12.degrade_input_output(), |
| 67 | csn2: pins.gpio13.degrade_input_output(), |
| 68 | }, |
| 69 | cc1101: Cc1101Pins { |
| 70 | sck: pins.gpio15.degrade_input_output(), |
| 71 | miso: pins.gpio41.degrade_input_output(), |
| 72 | mosi: pins.gpio35.degrade_input_output(), |
| 73 | cs: pins.gpio40.degrade_input_output(), |
| 74 | gdo0: pins.gpio39.degrade_input_output(), |
| 75 | gdo2: pins.gpio42.degrade_input_output(), |
| 76 | }, |
| 77 | }; |
| 78 | |
| 79 | ui::widgets::splash(&mut display); |
| 80 | esp_idf_hal::delay::FreeRtos::delay_ms(1500); |
| 81 | |
| 82 | log::info!("ORION-RF ready"); |
| 83 | ui::menu::run(&mut display, &mut buttons, &mut board); |
| 84 | } |