Last active
March 5, 2019 17:40
-
-
Save sajattack/0696c9d55b0adb76346fdeb189dba5a8 to your computer and use it in GitHub Desktop.
siddacious' requested demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![no_std] | |
| #![no_main] | |
| extern crate cortex_m; | |
| extern crate trellis_m4 as hal; | |
| extern crate panic_halt; | |
| extern crate smart_leds; | |
| extern crate smart_leds_trait; | |
| extern crate ws2812_timer_delay as ws2812; | |
| extern crate embedded_hal; | |
| use hal::prelude::*; | |
| use hal::{entry, Peripherals, CorePeripherals}; | |
| use hal::{clock::GenericClockController, delay::Delay, timer::TimerCounter}; | |
| use smart_leds_trait::SmartLedsWrite; | |
| use smart_leds_trait::Color; | |
| use smart_leds::brightness; | |
| use smart_leds::colors::{BLUE, RED}; | |
| use embedded_hal::digital::InputPin; | |
| #[entry] | |
| fn main() -> ! { | |
| let mut peripherals = Peripherals::take().unwrap(); | |
| let core = CorePeripherals::take().unwrap(); | |
| let mut clocks = GenericClockController::with_internal_32kosc( | |
| peripherals.GCLK, | |
| &mut peripherals.MCLK, | |
| &mut peripherals.OSC32KCTRL, | |
| &mut peripherals.OSCCTRL, | |
| &mut peripherals.NVMCTRL, | |
| ); | |
| let mut pins = hal::Pins::new(peripherals.PORT); | |
| let gclk0 = clocks.gclk0(); | |
| let timer_clock = clocks.tc2_tc3(&gclk0).unwrap(); | |
| let mut timer = TimerCounter::tc3_(&timer_clock, peripherals.TC3, &mut peripherals.MCLK); | |
| timer.start(3_800_000u32.hz()); | |
| let mut neopixel_pin = pins.neopixel.into_push_pull_output(&mut pins.port); | |
| let mut neopixel = ws2812::Ws2812::new(timer, &mut neopixel_pin); | |
| let mut delay = Delay::new(core.SYST, &mut clocks); | |
| const NUM_LEDS: usize = 32; | |
| let mut data = [Color::default(); NUM_LEDS]; | |
| let sda = pins.sda.into_pull_up_input(&mut pins.port); | |
| let scl = pins.scl.into_pull_up_input(&mut pins.port); | |
| loop { | |
| if sda.is_low() { | |
| data[0] = RED; | |
| neopixel.write(brightness(data.iter().cloned(), 32)).unwrap(); | |
| delay.delay_ms(250u8); | |
| data[0] = Color::default(); | |
| neopixel.write(brightness(data.iter().cloned(), 32)).unwrap(); | |
| delay.delay_ms(250u8); | |
| } | |
| if scl.is_low() { | |
| data[1] = BLUE; | |
| data[2] = BLUE; | |
| neopixel.write(brightness(data.iter().cloned(), 32)).unwrap(); | |
| delay.delay_ms(500u16); | |
| data[1] = Color::default(); | |
| data[2] = Color::default(); | |
| neopixel.write(brightness(data.iter().cloned(), 32)).unwrap(); | |
| delay.delay_ms(500u16); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment