initial commit
commit
d102145db3
|
@ -0,0 +1,14 @@
|
|||
### Rust ###
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# Compiled files
|
||||
*.hex
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "rusted_mario_avr"
|
||||
version = "0.1.0"
|
||||
authors = ["kirbylife <kirbylife@protonmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
ruduino = "0.2.5"
|
||||
avr-std-stub = "1.0"
|
|
@ -0,0 +1,9 @@
|
|||
The MIT License (MIT)
|
||||
Copyright (c) 2017 AVR-Rust contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
# blink
|
||||
|
||||
An small Hello World Rust application for the AVR.
|
||||
|
||||
The program itself toggles a LED on PORTB periodically.
|
||||
|
||||
Designed for the ATmega328p.
|
||||
|
||||
[How to set up a cross compiler](https://github.com/avr-rust/rust)
|
||||
|
||||
# Usage
|
||||
|
||||
There are a few environment variables that need to be set first:
|
||||
|
||||
```bash
|
||||
# Needed until https://github.com/japaric/xargo/pull/205 goes through,
|
||||
# to tell it where to find avr-atmega328p.json:
|
||||
export RUST_TARGET_PATH=`pwd`
|
||||
|
||||
# Likely needed if you've just compiled avr-rust from source:
|
||||
export XARGO_RUST_SRC=/path_to_avr_rust/src
|
||||
# (e.g. :if you built from sources and typed `git clone https://github.com/avr-rust/rust.git` in `~/avr-rust`, this would be `~/avr-rust/rust/src)
|
||||
```
|
||||
|
||||
Now to build, run:
|
||||
|
||||
```bash
|
||||
rustup run avr-toolchain xargo build --target avr-atmega328p --release
|
||||
|
||||
```
|
||||
There should now be an ELF file at `target/atmega328p/release/blink.elf`.
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"arch": "avr",
|
||||
"cpu": "atmega328p",
|
||||
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
|
||||
"env": "",
|
||||
"executables": true,
|
||||
"linker": "avr-gcc",
|
||||
"linker-flavor": "gcc",
|
||||
"linker-is-gnu": true,
|
||||
"llvm-target": "avr-unknown-unknown",
|
||||
"no-compiler-rt": true,
|
||||
"os": "unknown",
|
||||
"position-independent-executables": false,
|
||||
"exe-suffix": ".elf",
|
||||
"eh-frame-header": false,
|
||||
"pre-link-args": {
|
||||
"gcc": [
|
||||
"-Os",
|
||||
"-mmcu=atmega328p"
|
||||
]
|
||||
},
|
||||
"late-link-args": {
|
||||
"gcc": [
|
||||
"-lc",
|
||||
"-lgcc"
|
||||
]
|
||||
},
|
||||
"target-c-int-width": "16",
|
||||
"target-endian": "little",
|
||||
"target-pointer-width": "16",
|
||||
"vendor": "unknown"
|
||||
}
|
|
@ -0,0 +1,455 @@
|
|||
#![feature(llvm_asm, lang_items, core_intrinsics)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
/*
|
||||
PORTC{0..5} Analog
|
||||
PORTC6 Reset
|
||||
|
||||
PORTB{0..5} Digital {8..13}
|
||||
PORTD{0..7} Digital {0..7}
|
||||
|
||||
PORTB5 LED_BUILTIN
|
||||
*/
|
||||
|
||||
extern crate avr_std_stub;
|
||||
extern crate ruduino;
|
||||
|
||||
// use core::ptr::{read, read_volatile, write_volatile};
|
||||
use ruduino::cores::current as avr_core;
|
||||
use ruduino::legacy::serial;
|
||||
use ruduino::Register;
|
||||
|
||||
use avr_core::{DDRB, DDRC, DDRD, PINB, PINC, PIND, PORTB, PORTC, PORTD};
|
||||
|
||||
const BLACK: u8 = 0u8;
|
||||
const RED: u8 = 1u8;
|
||||
const GREEN: u8 = 2u8;
|
||||
const YELLOW: u8 = 3u8;
|
||||
|
||||
const CPU_FREQUENCY_HZ: u64 = 16_000_000;
|
||||
const BAUD: u64 = 9600;
|
||||
const UBRR: u16 = (CPU_FREQUENCY_HZ / 16 / BAUD - 1) as u16;
|
||||
|
||||
const HIGH: u8 = 0xFF;
|
||||
const LOW: u8 = 0x00;
|
||||
|
||||
const OUTPUT: u8 = 0xFF;
|
||||
const INPUT: u8 = 0x00;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct Position {
|
||||
x: i8,
|
||||
y: i8,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct Size {
|
||||
width: i8,
|
||||
height: i8,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct Player {
|
||||
position: Position,
|
||||
size: Size,
|
||||
force: i8,
|
||||
speed: f32,
|
||||
powered: bool,
|
||||
jumping: bool,
|
||||
}
|
||||
impl Player {
|
||||
fn new() -> Self {
|
||||
Player {
|
||||
position: Position { x: 2, y: 30 },
|
||||
size: Size {
|
||||
width: 3,
|
||||
height: 6,
|
||||
},
|
||||
force: 0,
|
||||
speed: 0.0,
|
||||
powered: false,
|
||||
jumping: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn collide(&self, wall: &Wall) -> Option<Direction> {
|
||||
let is_colliding = self.position.x < wall.position.x + wall.size.width
|
||||
&& self.position.x + self.size.width > wall.position.x
|
||||
&& self.position.y < wall.position.y + wall.size.height
|
||||
&& self.position.y + self.size.height > wall.position.y;
|
||||
|
||||
if is_colliding {
|
||||
if self.position.y < wall.position.y {
|
||||
Some(Direction::Up)
|
||||
} else if self.position.x < wall.position.x {
|
||||
Some(Direction::Left)
|
||||
} else if self.position.x + self.size.width > wall.position.x + wall.size.width {
|
||||
Some(Direction::Right)
|
||||
} else {
|
||||
Some(Direction::Down)
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum WallType {
|
||||
Solid,
|
||||
Breakable,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Wall {
|
||||
position: Position,
|
||||
size: Size,
|
||||
wall_type: WallType,
|
||||
visible: bool,
|
||||
}
|
||||
|
||||
impl Wall {
|
||||
fn new(size: Size, position: Position, wall_type: WallType) -> Self {
|
||||
Wall {
|
||||
position: position,
|
||||
size: size,
|
||||
wall_type: wall_type,
|
||||
visible: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn main() -> ! {
|
||||
serial_start();
|
||||
DDRB::set_mask_raw(HIGH);
|
||||
PORTB::unset_mask_raw(HIGH);
|
||||
DDRC::set_mask_raw(HIGH);
|
||||
PORTC::unset_mask_raw(HIGH);
|
||||
DDRD::set_mask_raw(HIGH);
|
||||
PORTD::unset_mask_raw(HIGH);
|
||||
|
||||
let mut step = 0;
|
||||
let mut player = Player::new();
|
||||
let mut walls: [Wall; 8] = [
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 120,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 0, y: 54 },
|
||||
WallType::Solid,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 50, y: 35 },
|
||||
WallType::Solid,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 70, y: 35 },
|
||||
WallType::Breakable,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 80, y: 35 },
|
||||
WallType::Breakable,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 90, y: 35 },
|
||||
WallType::Breakable,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 80, y: 20 },
|
||||
WallType::Solid,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 85, y: 35 },
|
||||
WallType::Solid,
|
||||
),
|
||||
Wall::new(
|
||||
Size {
|
||||
width: 5,
|
||||
height: 5,
|
||||
},
|
||||
Position { x: 75, y: 35 },
|
||||
WallType::Solid,
|
||||
),
|
||||
];
|
||||
|
||||
clear();
|
||||
let mut old_player = player.clone();
|
||||
let mut last_wall_collisioned: Option<Wall> = None;
|
||||
for wall in walls.iter() {
|
||||
if wall.visible {
|
||||
draw_wall(wall);
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
// clear();
|
||||
draw_player(&player, &old_player);
|
||||
old_player = player.clone();
|
||||
|
||||
if player.position.y > 70 {
|
||||
player = Player::new();
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut fall = true;
|
||||
|
||||
// Static wall behavior
|
||||
let mut horizontal_collision: Option<Direction> = None;
|
||||
for wall in &mut walls {
|
||||
if wall.visible {
|
||||
match player.collide(wall) {
|
||||
Some(Direction::Up) => {
|
||||
player.jumping = false;
|
||||
player.force = 0;
|
||||
while !player.collide(&wall).is_none() {
|
||||
player.position.y -= 1;
|
||||
}
|
||||
player.position.y += 1;
|
||||
fall = false;
|
||||
draw_wall(&wall);
|
||||
}
|
||||
Some(Direction::Down) => {
|
||||
player.force = 0;
|
||||
player.position.y += 1;
|
||||
fall = true;
|
||||
if wall.wall_type == WallType::Breakable {
|
||||
delete_wall(&wall);
|
||||
wall.visible = false;
|
||||
} else {
|
||||
draw_wall(&wall);
|
||||
}
|
||||
}
|
||||
Some(dir) => {
|
||||
while !player.collide(&wall).is_none() {
|
||||
if dir == Direction::Left {
|
||||
player.position.x -= 1;
|
||||
} else {
|
||||
player.position.x += 1;
|
||||
}
|
||||
}
|
||||
if dir == Direction::Left {
|
||||
player.position.x += 1;
|
||||
} else {
|
||||
player.position.x -= 1;
|
||||
}
|
||||
horizontal_collision = Some(dir);
|
||||
draw_wall(&wall);
|
||||
}
|
||||
// None => fall = true,
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gravity control
|
||||
// if the variable `fall` is true it's because all the logic of
|
||||
// The platforms doesn't stop the fallen of the character
|
||||
if fall {
|
||||
step = (step + 1) % 4;
|
||||
if step == 0 {
|
||||
player.force += 1;
|
||||
}
|
||||
} else {
|
||||
step = 0;
|
||||
}
|
||||
player.position.y += player.force;
|
||||
// player.position.x += 2;
|
||||
|
||||
let values = PINB::read();
|
||||
let left = (values >> 0) % 2 == 1;
|
||||
let right = (values >> 1) % 2 == 1;
|
||||
let jump = (values >> 2) % 2 == 1;
|
||||
|
||||
if left {
|
||||
player.position.x += 1;
|
||||
}
|
||||
if right {
|
||||
player.position.x -= 1;
|
||||
}
|
||||
if jump && !player.jumping {
|
||||
player.position.y -= 1;
|
||||
player.force = -3;
|
||||
player.jumping = true;
|
||||
}
|
||||
|
||||
let values = PINB::read();
|
||||
for n in 0..8 {
|
||||
if (values >> n) % 2 == 1 {
|
||||
serial::transmit('1' as u8);
|
||||
} else {
|
||||
serial::transmit('0' as u8);
|
||||
}
|
||||
}
|
||||
// serial::transmit('\n' as u8);
|
||||
// let values = PINC::read();
|
||||
// for n in 0..8 {
|
||||
// if (values >> n) % 2 == 1 {
|
||||
// serial::transmit('1' as u8);
|
||||
// } else {
|
||||
// serial::transmit('0' as u8);
|
||||
// }
|
||||
// }
|
||||
// serial::transmit('\n' as u8);
|
||||
// let values = PIND::read();
|
||||
// for n in 0..8 {
|
||||
// if (values >> n) % 2 == 1 {
|
||||
// serial::transmit('1' as u8);
|
||||
// } else {
|
||||
// serial::transmit('0' as u8);
|
||||
// }
|
||||
// }
|
||||
// serial::transmit('\n' as u8);
|
||||
|
||||
// delay(20);
|
||||
}
|
||||
}
|
||||
|
||||
fn delay(cycles: u32) {
|
||||
for _ in 0..(1_600 * cycles) {
|
||||
unsafe { llvm_asm!("" :::: "volatile") }
|
||||
}
|
||||
}
|
||||
|
||||
fn clear() {
|
||||
for b in ['|' as u8, 0, 2].iter() {
|
||||
serial::transmit(*b);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_player(player: &Player, old_player: &Player) {
|
||||
if player == old_player {
|
||||
return;
|
||||
}
|
||||
for b in [
|
||||
'|' as u8,
|
||||
2,
|
||||
old_player.position.x as u8,
|
||||
old_player.position.y as u8,
|
||||
old_player.size.width as u8,
|
||||
old_player.size.height as u8 - 1,
|
||||
GREEN,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
serial::transmit(*b);
|
||||
}
|
||||
for b in [
|
||||
'|' as u8,
|
||||
2,
|
||||
player.position.x as u8,
|
||||
player.position.y as u8,
|
||||
player.size.width as u8,
|
||||
player.size.height as u8 - 1,
|
||||
RED,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
serial::transmit(*b);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_wall(wall: &Wall) {
|
||||
for b in [
|
||||
'|' as u8,
|
||||
2,
|
||||
wall.position.x as u8,
|
||||
wall.position.y as u8,
|
||||
wall.size.width as u8,
|
||||
wall.size.height as u8,
|
||||
BLACK,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
serial::transmit(*b);
|
||||
}
|
||||
if wall.wall_type == WallType::Breakable {
|
||||
for b in [
|
||||
'|' as u8,
|
||||
1,
|
||||
wall.position.x as u8 + 2,
|
||||
wall.position.y as u8 + 2,
|
||||
GREEN,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
serial::transmit(*b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_wall(wall: &Wall) {
|
||||
for b in [
|
||||
'|' as u8,
|
||||
2,
|
||||
wall.position.x as u8,
|
||||
wall.position.y as u8,
|
||||
wall.size.width as u8,
|
||||
wall.size.height as u8,
|
||||
GREEN,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
serial::transmit(*b);
|
||||
}
|
||||
}
|
||||
|
||||
fn serial_start() {
|
||||
serial::Serial::new(UBRR)
|
||||
.character_size(serial::CharacterSize::EightBits)
|
||||
.mode(serial::Mode::Asynchronous)
|
||||
// .mode(serial::Mode::MasterSpi)
|
||||
.parity(serial::Parity::Disabled)
|
||||
.stop_bits(serial::StopBits::OneBit)
|
||||
.configure();
|
||||
}
|
||||
|
||||
// These do not need to be in a module, but we group them here for clarity.
|
||||
// pub mod std {
|
||||
// #[lang = "eh_personality"]
|
||||
// pub unsafe extern "C" fn rust_eh_personality(
|
||||
// _state: (),
|
||||
// _exception_object: *mut (),
|
||||
// _context: *mut (),
|
||||
// ) -> () {
|
||||
// }
|
||||
//
|
||||
// #[panic_handler]
|
||||
// fn panic(_info: &::core::panic::PanicInfo) -> ! {
|
||||
// loop {}
|
||||
// }
|
||||
// }
|
|
@ -0,0 +1,7 @@
|
|||
# export RUST_TARGET_PATH=`pwd`
|
||||
# export XARGO_RUST_SRC=/home/kirbylife/Aplicaciones/rust-avr/rust/src
|
||||
|
||||
# rustup run avr-toolchain xargo build --target avr-atmega328p --release
|
||||
cargo build -Z build-std=core --target avr-atmega328p.json --release
|
||||
avr-objcopy -O ihex -R .eeprom target/avr-atmega328p/release/blink.elf blink.hex
|
||||
avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -U flash:w:blink.hex:i
|
Loading…
Reference in New Issue