diff --git a/Cargo.lock b/Cargo.lock index 2f27a7d..c1c5591 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,9 +221,12 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" name = "bike" version = "0.1.0" dependencies = [ + "az", "cortex-m", "cortex-m-rt", + "embedded-alloc", "embedded-hal", + "fixed", "fugit", "lights-core", "panic-halt", @@ -801,6 +804,16 @@ dependencies = [ "serde 1.0.188", ] +[[package]] +name = "embedded-alloc" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddae17915accbac2cfbc64ea0ae6e3b330e6ea124ba108dada63646fd3c6f815" +dependencies = [ + "critical-section", + "linked_list_allocator", +] + [[package]] name = "embedded-dma" version = "0.2.0" @@ -2194,6 +2207,12 @@ dependencies = [ "fixed", ] +[[package]] +name = "linked_list_allocator" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" + [[package]] name = "linux-raw-sys" version = "0.4.8" diff --git a/bike-lights/bike/Cargo.toml b/bike-lights/bike/Cargo.toml index 80fc201..e62c730 100644 --- a/bike-lights/bike/Cargo.toml +++ b/bike-lights/bike/Cargo.toml @@ -6,10 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cortex-m = "0.7.7" -cortex-m-rt = "0.7.3" -embedded-hal = "0.2.7" -fugit = "0.3.7" -panic-halt = "0.2.0" -rp-pico = "0.8.0" -lights-core = { path = "../core" } +az = { version = "1" } +cortex-m-rt = { version = "0.7.3" } +cortex-m = { version = "0.7.7" } +embedded-alloc = { version = "0.5.1" } +embedded-hal = { version = "0.2.7" } +fixed = { version = "1" } +fugit = { version = "0.3.7" } +lights-core = { path = "../core" } +panic-halt = { version = "0.2.0" } +rp-pico = { version = "0.8.0" } diff --git a/bike-lights/bike/src/main.rs b/bike-lights/bike/src/main.rs index 824e3b4..4e45da7 100644 --- a/bike-lights/bike/src/main.rs +++ b/bike-lights/bike/src/main.rs @@ -1,10 +1,134 @@ #![no_main] #![no_std] +extern crate alloc; + +use az::*; +use core::cell::RefCell; +use cortex_m::delay::Delay; +use embedded_alloc::Heap; +use embedded_hal::{blocking::spi::Write, digital::v2::OutputPin}; +use fixed::types::I16F16; +use fugit::RateExtU32; +use lights_core::{ + BodyPattern, DashboardPattern, Event, TRANS_PRIDE_BODY, TRANS_PRIDE_DASHBOARD, UI, +}; use panic_halt as _; -use rp_pico::entry; +use rp_pico::{ + entry, + hal::{ + clocks::init_clocks_and_plls, + pac::{CorePeripherals, Peripherals}, + spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, + watchdog::Watchdog, + Clock, Sio, + }, + Pins, +}; + +#[global_allocator] +static HEAP: Heap = Heap::empty(); + +const LIGHT_SCALE: I16F16 = I16F16::lit("256.0"); +const GLOBAL_BRIGHTESS: u8 = 1; + +struct BikeUI> { + spi: RefCell>, +} + +impl> BikeUI { + fn new(spi: Spi) -> Self { + Self { + spi: RefCell::new(spi), + } + } +} + +impl> UI for BikeUI { + fn check_event(&self) -> Option { + None + } + + fn update_lights(&self, dashboard_lights: DashboardPattern, body_lights: BodyPattern) { + let mut lights: [u8; 20] = [0; 20]; + lights[16] = 0xff; + lights[17] = 0xff; + lights[18] = 0xff; + lights[19] = 0xff; + for (idx, rgb) in dashboard_lights.iter().enumerate() { + lights[(idx + 1) * 4 + 0] = 0xe0 + GLOBAL_BRIGHTESS; + lights[(idx + 1) * 4 + 1] = (I16F16::from(rgb.r) * LIGHT_SCALE).saturating_as(); + lights[(idx + 1) * 4 + 2] = (I16F16::from(rgb.b) * LIGHT_SCALE).saturating_as(); + lights[(idx + 1) * 4 + 3] = (I16F16::from(rgb.g) * LIGHT_SCALE).saturating_as(); + } + let mut spi = self.spi.borrow_mut(); + spi.write(lights.as_slice()); + } +} #[entry] fn main() -> ! { - loop {} + { + use core::mem::MaybeUninit; + const HEAP_SIZE: usize = 8096; + static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; + unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } + } + + let mut pac = Peripherals::take().unwrap(); + let core = CorePeripherals::take().unwrap(); + let sio = Sio::new(pac.SIO); + let mut watchdog = Watchdog::new(pac.WATCHDOG); + + let pins = Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS, + ); + + let clocks = init_clocks_and_plls( + 12_000_000u32, + pac.XOSC, + pac.CLOCKS, + pac.PLL_SYS, + pac.PLL_USB, + &mut pac.RESETS, + &mut watchdog, + ) + .ok() + .unwrap(); + + let mut delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); + let mut spi_clk = pins.gpio10.into_function(); + let mut spi_sdo = pins.gpio11.into_function(); + let spi = Spi::<_, _, _, 8>::new(pac.SPI1, (spi_sdo, spi_clk)); + let mut spi = spi.init( + &mut pac.RESETS, + clocks.peripheral_clock.freq(), + 1_u32.MHz(), + embedded_hal::spi::MODE_1, + ); + + let ui = BikeUI::new(spi); + + // let app = App::new(Box::new(ui)); + // app.tick(TRANS_PRIDE_DASHBOARD, TRANS_PRIDE_BODY); + ui.update_lights(TRANS_PRIDE_DASHBOARD, TRANS_PRIDE_BODY); + + /* + spi.write(&[ + 0, 0, 0, 0, 0xe1, 0x80, 0x00, 0x00, 0xe1, 0x00, 0x80, 0x00, 0xe1, 0x00, 0x00, 0x80, 0xff, + 0xff, 0xff, 0xff, + ]); + */ + + let mut led_pin = pins.led.into_push_pull_output(); + + loop { + led_pin.set_high().unwrap(); + delay.delay_ms(500); + led_pin.set_low().unwrap(); + delay.delay_ms(500); + } } diff --git a/bike-lights/core/src/lib.rs b/bike-lights/core/src/lib.rs index c8523eb..361d39b 100644 --- a/bike-lights/core/src/lib.rs +++ b/bike-lights/core/src/lib.rs @@ -4,7 +4,7 @@ extern crate alloc; use alloc::boxed::Box; use az::*; use core::{clone::Clone, cmp::PartialEq, ops::Sub, option::Option}; -use fixed::types::{I16F0, I16F16, I48F16, I8F8, U128F0, U16F0}; +use fixed::types::{I48F16, I8F8, U128F0, U16F0}; mod patterns; pub use patterns::*; diff --git a/bike-lights/core/src/patterns.rs b/bike-lights/core/src/patterns.rs index e566a5d..0c8f609 100644 --- a/bike-lights/core/src/patterns.rs +++ b/bike-lights/core/src/patterns.rs @@ -46,12 +46,12 @@ pub const PRIDE_YELLOW: RGB = RGB { pub const PRIDE_GREEN: RGB = RGB { r: I8F8::lit("0"), g: I8F8::lit("0.5"), - b: I8F8::lit("0.14"), + b: I8F8::lit("0.05"), }; pub const PRIDE_INDIGO: RGB = RGB { - r: I8F8::lit("0.14"), - g: I8F8::lit("0.25"), + r: I8F8::lit("0.04"), + g: I8F8::lit("0.15"), b: I8F8::lit("0.55"), }; @@ -62,15 +62,15 @@ pub const PRIDE_VIOLET: RGB = RGB { }; pub const TRANS_BLUE: RGB = RGB { - r: I8F8::lit("0.36"), - g: I8F8::lit("0.81"), + r: I8F8::lit("0.06"), + g: I8F8::lit("0.41"), b: I8F8::lit("0.98"), }; pub const TRANS_PINK: RGB = RGB { r: I8F8::lit("0.96"), - g: I8F8::lit("0.66"), - b: I8F8::lit("0.72"), + g: I8F8::lit("0.16"), + b: I8F8::lit("0.32"), }; pub const OFF_DASHBOARD: DashboardPattern = [RGB_OFF; 3];