Initial commit
This commit is contained in:
commit
4baf810992
6 changed files with 699 additions and 0 deletions
115
src/main.rs
Normal file
115
src/main.rs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use embedded_graphics::{Drawable, mono_font::{MonoTextStyle, ascii::FONT_10X20}, pixelcolor::Rgb565, prelude::*, primitives::{Polyline, PrimitiveStyle, Rectangle, StyledDrawable}, text::{Alignment, Text}};
|
||||
use embedded_hal::{delay::DelayNs, digital::{InputPin, OutputPin}};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use microbit::Board;
|
||||
use mipidsi::{Builder, interface::SpiInterface, models, options::{ColorInversion, Orientation, TearingEffect}};
|
||||
use nrf52833_hal::{Timer, gpio::Level, spi::{self, Spi}, spim};
|
||||
use panic_rtt_target as _;
|
||||
use rtt_target::rtt_init_print;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
rtt_init_print!();
|
||||
let board = Board::take().unwrap();
|
||||
|
||||
let mut button_a = board.buttons.button_a;
|
||||
let mut button_b = board.buttons.button_b;
|
||||
|
||||
let mut timer0 = Timer::new(board.TIMER0);
|
||||
let mut timer1 = Timer::new(board.TIMER1);
|
||||
|
||||
let scl = board.edge.e08.into_push_pull_output(Level::Low);
|
||||
let sda = board.edge.e09.into_push_pull_output(Level::Low);
|
||||
let mut res = board.pins.p0_17.into_push_pull_output(Level::High);
|
||||
let dc = board.pins.p0_01.into_push_pull_output(Level::Low);
|
||||
let cs = board.pins.p0_13.into_push_pull_output(Level::Low);
|
||||
let mut bl = board.edge.e16.into_push_pull_output(Level::High);
|
||||
|
||||
res.set_high();
|
||||
bl.set_high();
|
||||
|
||||
let spi_pins = spi::Pins {
|
||||
sck: Some(scl.degrade()),
|
||||
mosi: Some(sda.degrade()),
|
||||
miso: None,
|
||||
};
|
||||
|
||||
let bus = Spi::new(board.SPI0, spi_pins, spi::Frequency::M8, spim::MODE_3);
|
||||
let spi = ExclusiveDevice::new(bus, cs, &mut timer0).unwrap();
|
||||
|
||||
let mut buffer = [0u8; 512];
|
||||
|
||||
let interface = SpiInterface::new(spi, dc, &mut buffer);
|
||||
|
||||
let mut display = Builder::new(models::ST7789, interface)
|
||||
.display_size(240, 320)
|
||||
.invert_colors(ColorInversion::Inverted)
|
||||
.reset_pin(res)
|
||||
.orientation(Orientation::new().flip_vertical().flip_horizontal())
|
||||
.init(&mut timer1).unwrap();
|
||||
|
||||
display.set_tearing_effect(TearingEffect::HorizontalAndVertical);
|
||||
|
||||
display.clear(Rgb565::BLACK).unwrap();
|
||||
display.fill_solid(&Rectangle::with_center(Point::new(32, 32), Size::new(16, 16)), Rgb565::GREEN);
|
||||
display.fill_solid(&Rectangle::with_center(Point::new(240-32, 32), Size::new(16, 16)), Rgb565::BLUE);
|
||||
display.fill_solid(&Rectangle::with_center(Point::new(240-32, 320-32), Size::new(16, 16)), Rgb565::RED);
|
||||
display.fill_solid(&Rectangle::with_center(Point::new(32, 320-32), Size::new(16, 16)), Rgb565::YELLOW);
|
||||
|
||||
let points = [
|
||||
Point::new(0, 0),
|
||||
Point::new(239, 0),
|
||||
Point::new(239, 319),
|
||||
Point::new(0, 319),
|
||||
Point::new(0, 0),
|
||||
];
|
||||
|
||||
let line_style = PrimitiveStyle::with_stroke(Rgb565::WHITE, 1);
|
||||
|
||||
let text_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
||||
|
||||
Text::with_alignment("Hello, gryadki!", Point::new(120, 38), text_style, Alignment::Center).draw(&mut display).unwrap();
|
||||
|
||||
Polyline::new(&points)
|
||||
.into_styled(line_style)
|
||||
.draw(&mut display);
|
||||
|
||||
let mut rectangle = Rectangle::new(
|
||||
Point::new(120-8, 200),
|
||||
Size::new(16, 8),
|
||||
);
|
||||
|
||||
let speed = 4;
|
||||
let update_size = Size::new(speed as u32, 8);
|
||||
let (min_x, max_x) = (1, 239-16);
|
||||
|
||||
let clean_style = PrimitiveStyle::with_fill(Rgb565::BLACK);
|
||||
let player_style = PrimitiveStyle::with_fill(Rgb565::CYAN);
|
||||
|
||||
loop {
|
||||
if button_a.is_low().unwrap() {
|
||||
Rectangle::new(rectangle.top_left, update_size).draw_styled(&clean_style, &mut display);
|
||||
rectangle.translate_mut(Point::new(speed, 0));
|
||||
}
|
||||
|
||||
if button_b.is_low().unwrap() {
|
||||
Rectangle::new(
|
||||
Point::new(
|
||||
rectangle.top_left.x + rectangle.size.width as i32 - speed,
|
||||
rectangle.top_left.y,
|
||||
),
|
||||
update_size,
|
||||
).draw_styled(&clean_style, &mut display);
|
||||
rectangle.translate_mut(Point::new(-speed, 0));
|
||||
}
|
||||
|
||||
rectangle.top_left.x = rectangle.top_left.x.clamp(min_x, max_x);
|
||||
|
||||
rectangle.draw_styled(&player_style, &mut display);
|
||||
timer1.delay_ms(33);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue