onyx/src/main.rs

124 lines
3.3 KiB
Rust
Raw Normal View History

2020-01-25 20:49:11 +00:00
#![no_std]
#![no_main]
// pick a panicking behavior
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
2020-01-25 22:53:06 +00:00
// 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
2020-01-25 20:49:11 +00:00
2020-01-25 22:53:06 +00:00
use metro_m0 as hal;
2020-02-06 23:42:45 +00:00
use cortex_m::asm;
2020-01-25 22:53:06 +00:00
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::entry;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
2020-01-25 20:49:11 +00:00
2020-02-06 23:42:45 +00:00
macro_rules! write_rgb_zero {
2020-02-07 00:21:26 +00:00
($pin:expr) => {
2020-02-06 23:42:45 +00:00
$pin.set_high().unwrap();
2020-02-07 00:21:26 +00:00
for _ in 0..4 {
// for _ in 0..12 {
asm::nop();
}
2020-02-06 23:42:45 +00:00
$pin.set_low().unwrap();
2020-02-07 00:21:26 +00:00
for _ in 0..48 {
asm::nop();
}
2020-02-06 23:42:45 +00:00
};
}
macro_rules! write_rgb_one {
2020-02-07 00:21:26 +00:00
($pin:expr) => {
2020-02-06 23:42:45 +00:00
$pin.set_high().unwrap();
2020-02-07 00:21:26 +00:00
for _ in 0..20 {
// for _ iQn 0..28 {
asm::nop();
}
2020-02-06 23:42:45 +00:00
$pin.set_low().unwrap();
2020-02-07 00:21:26 +00:00
for _ in 0..28 {
asm::nop();
}
2020-02-06 23:42:45 +00:00
};
}
2020-02-07 00:21:26 +00:00
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);
}
}
};
}
2020-02-06 23:42:45 +00:00
2020-01-25 20:49:11 +00:00
#[entry]
fn main() -> ! {
2020-01-25 22:53:06 +00:00
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);
2020-02-07 00:21:26 +00:00
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);
2020-01-25 20:49:11 +00:00
2020-02-06 23:42:45 +00:00
let mut rgb_meme = pins.neopixel.into_push_pull_output(&mut pins.port);
2020-01-25 22:53:06 +00:00
let mut delay = Delay::new(core.SYST, &mut clocks);
2020-01-26 18:28:35 +00:00
2020-01-25 20:49:11 +00:00
loop {
2020-02-06 23:42:45 +00:00
delay.delay_us(255u8);
2020-02-07 00:21:26 +00:00
// 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);
2020-01-26 18:28:35 +00:00
}
2020-02-07 00:21:26 +00:00
col1.set_low().unwrap();
2020-02-06 23:42:45 +00:00
2020-02-07 00:21:26 +00:00
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();
2020-01-25 20:49:11 +00:00
}
2020-01-24 19:10:30 +00:00
}
2020-02-06 23:42:45 +00:00
#[no_mangle]
fn lol(
pin: &mut metro_m0::gpio::Pa30<metro_m0::gpio::Output<metro_m0::gpio::PushPull>>,
delay: &mut hal::delay::Delay,
2020-02-07 00:21:26 +00:00
r: u8,
g: u8,
b: u8,
2020-02-06 23:42:45 +00:00
) {
pin.set_low().unwrap();
delay.delay_us(60u8);
2020-02-07 00:21:26 +00:00
write_rgb!(pin, g);
write_rgb!(pin, r);
write_rgb!(pin, b);
2020-02-06 23:42:45 +00:00
}