124 lines
3.3 KiB
Rust
124 lines
3.3 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
|
|
// pick a panicking behavior
|
|
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
|
|
// extern crate panic_abort; // requires nightly
|
|
// extern crate panic_itm; // logs messages over ITM; requires ITM support
|
|
// extern crate panic_semihosting; // logs messages to the host stderr; requires a debugger
|
|
|
|
use metro_m0 as hal;
|
|
|
|
use cortex_m::asm;
|
|
use hal::clock::GenericClockController;
|
|
use hal::delay::Delay;
|
|
use hal::entry;
|
|
use hal::pac::{CorePeripherals, Peripherals};
|
|
use hal::prelude::*;
|
|
|
|
macro_rules! write_rgb_zero {
|
|
($pin:expr) => {
|
|
$pin.set_high().unwrap();
|
|
for _ in 0..4 {
|
|
// for _ in 0..12 {
|
|
asm::nop();
|
|
}
|
|
|
|
$pin.set_low().unwrap();
|
|
|
|
for _ in 0..48 {
|
|
asm::nop();
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! write_rgb_one {
|
|
($pin:expr) => {
|
|
$pin.set_high().unwrap();
|
|
for _ in 0..20 {
|
|
// for _ iQn 0..28 {
|
|
asm::nop();
|
|
}
|
|
|
|
$pin.set_low().unwrap();
|
|
for _ in 0..28 {
|
|
asm::nop();
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! write_rgb {
|
|
($pin:expr, $uint8:expr) => {
|
|
for i in (0..8).rev() {
|
|
if ($uint8 >> i) & 1 == 1 {
|
|
write_rgb_one!($pin);
|
|
} else {
|
|
write_rgb_zero!($pin);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let mut peripherals = Peripherals::take().unwrap();
|
|
let core = CorePeripherals::take().unwrap();
|
|
let mut clocks = GenericClockController::with_external_32kosc(
|
|
peripherals.GCLK,
|
|
&mut peripherals.PM,
|
|
&mut peripherals.SYSCTRL,
|
|
&mut peripherals.NVMCTRL,
|
|
);
|
|
let mut pins = hal::Pins::new(peripherals.PORT);
|
|
let mut col1 = pins.d6.into_open_drain_output(&mut pins.port);
|
|
let mut col2 = pins.d7.into_open_drain_output(&mut pins.port);
|
|
let row1 = pins.d11.into_pull_down_input(&mut pins.port);
|
|
let row2 = pins.d12.into_pull_down_input(&mut pins.port);
|
|
|
|
// let mut red_led = pins.d13.into_push_pull_output(&mut pins.port);
|
|
// let mut assert_pin = pins.d12.into_push_pull_output(&mut pins.port);
|
|
// let switch = pins.d3.into_pull_down_input(&mut pins.port);
|
|
|
|
let mut rgb_meme = pins.neopixel.into_push_pull_output(&mut pins.port);
|
|
|
|
let mut delay = Delay::new(core.SYST, &mut clocks);
|
|
|
|
loop {
|
|
delay.delay_us(255u8);
|
|
|
|
// lol(&mut rgb_meme, &mut delay, 20, 0, 0);
|
|
|
|
col1.set_high().unwrap();
|
|
if row1.is_high().unwrap() {
|
|
lol(&mut rgb_meme, &mut delay, 0, 0, 1);
|
|
} else if row2.is_high().unwrap() {
|
|
lol(&mut rgb_meme, &mut delay, 1, 1, 0);
|
|
}
|
|
col1.set_low().unwrap();
|
|
|
|
col2.set_high().unwrap();
|
|
if row1.is_high().unwrap() {
|
|
lol(&mut rgb_meme, &mut delay, 0, 1, 0);
|
|
} else if row2.is_high().unwrap() {
|
|
lol(&mut rgb_meme, &mut delay, 1, 0, 0);
|
|
}
|
|
col2.set_low().unwrap();
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
fn lol(
|
|
pin: &mut metro_m0::gpio::Pa30<metro_m0::gpio::Output<metro_m0::gpio::PushPull>>,
|
|
delay: &mut hal::delay::Delay,
|
|
r: u8,
|
|
g: u8,
|
|
b: u8,
|
|
) {
|
|
pin.set_low().unwrap();
|
|
delay.delay_us(60u8);
|
|
|
|
write_rgb!(pin, g);
|
|
write_rgb!(pin, r);
|
|
write_rgb!(pin, b);
|
|
}
|