This commit is contained in:
Alexey 2026-05-20 17:23:01 +03:00
commit e3c5d3df57
8 changed files with 627 additions and 0 deletions

77
src/bin/roulette.rs Normal file
View file

@ -0,0 +1,77 @@
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use microbit::{Board, display::blocking::Display, hal::Timer};
use panic_halt as _;
const MAX: usize = 4;
#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum Direction {
#[default] Right,
Down,
Left,
Up,
}
impl Direction {
fn next(&mut self) { *self = match self {
Self::Right => Self::Down,
Self::Down => Self::Left,
Self::Left => Self::Up,
Self::Up => Self::Right,
}; }
fn advance(&mut self, x: &mut usize, y: &mut usize) {
if *self == Self::Right && *x == MAX ||
*self == Self::Down && *y == MAX ||
*self == Self::Left && *x == 0 ||
*self == Self::Up && *y == 0
{
self.next();
}
match *self {
Self::Right => {
*x += 1;
},
Self::Down => {
*y += 1;
},
Self::Left => {
*x -= 1;
},
Self::Up => {
*y -= 1;
},
}
}
}
#[entry]
fn main() -> ! {
let board = Board::take().unwrap();
let mut timer = Timer::new(board.TIMER0);
let mut display = Display::new(board.display_pins);
let mut direction = Direction::default();
let mut x = 0;
let mut y = 0;
let mut display_out = [
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
];
loop {
display.clear();
display.show(&mut timer, display_out, 100);
display_out[y][x] = 0;
direction.advance(&mut x, &mut y);
display_out[y][x] = 1;
}
}

55
src/bin/turn.rs Normal file
View file

@ -0,0 +1,55 @@
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use embedded_hal::digital::InputPin;
use microbit::{Board, display::blocking::Display, hal::Timer};
use panic_halt as _;
const NEUTRAL: [[u8; 5]; 5] = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
];
const RIGHT: [[u8; 5]; 5] = [
[0, 0, 1, 0, 0],
[0, 1, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
];
const LEFT: [[u8; 5]; 5] = [
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 1, 0],
[0, 0, 1, 0, 0],
];
#[entry]
fn main() -> ! {
let board = Board::take().unwrap();
let mut timer = Timer::new(board.TIMER0);
let mut display = Display::new(board.display_pins);
let mut button_a = board.buttons.button_a;
let mut button_b = board.buttons.button_b;
loop {
display.clear();
let a_pressed = button_a.is_low().unwrap();
let b_pressed = button_b.is_low().unwrap();
let display_out = match (a_pressed, b_pressed) {
(false, false) | (true, true) => NEUTRAL,
(true, false) => RIGHT,
(false, true) => LEFT,
};
display.show(&mut timer, display_out, 10);
}
}

32
src/bin/uart.rs Normal file
View file

@ -0,0 +1,32 @@
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use embedded_io::{Read, Write};
use heapless::Vec;
use microbit::{hal::uarte::{self, Baudrate, Parity}};
use panic_halt as _;
#[entry]
fn main() -> ! {
let board = microbit::Board::take().unwrap();
let tx_buf = cortex_m::singleton!(TX_BUF: [u8; 1] = [0u8; 1]).unwrap();
let rx_buf = cortex_m::singleton!(RX_BUF: [u8; 1] = [0u8; 1]).unwrap();
let serial = uarte::Uarte::new(
board.UARTE0,
board.uart.into(),
Parity::EXCLUDED,
Baudrate::BAUD115200,
);
let (_tx, mut rx) = serial.split(tx_buf, rx_buf).unwrap();
let mut buffer: Vec<u8, 32> = Vec::new();
loop {
rx.read(buffer.as_mut_slice()).unwrap();
}
}