Add rust arduino project

This commit is contained in:
hodasemi 2022-07-16 10:32:10 +02:00
parent 4ec946ebbd
commit f07932f225
8 changed files with 101 additions and 132 deletions

View file

@ -0,0 +1,8 @@
[build]
target = "avr-atmega328p.json"
[target.'cfg(target_arch = "avr")']
runner = "ravedude uno -cb 57600"
[unstable]
build-std = ["core"]

2
arduino/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

7
arduino/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,7 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#5A0C35",
"titleBar.activeBackground": "#7E114B",
"titleBar.activeForeground": "#FFFCFD"
}
}

29
arduino/Cargo.toml Normal file
View file

@ -0,0 +1,29 @@
[package]
name = "arduino"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "arduino"
test = false
bench = false
[dependencies]
panic-halt = "0.2"
arduino-hal = { git = "https://github.com/Rahix/avr-hal", features = ["arduino-uno"], rev = "1aacefb335517f85d0de858231e11055d9768cdf" }
# branch = "main",
# Configure the build for minimal size - AVRs have very little program memory
[profile.dev]
panic = "abort"
lto = true
opt-level = "s"
[profile.release]
panic = "abort"
codegen-units = 1
debug = true
lto = true
opt-level = "s"

View file

@ -0,0 +1,27 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega328p",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"executables": true,
"late-link-args": {
"gcc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-is-gnu": true,
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"no-default-libraries": false,
"pre-link-args": {
"gcc": [
"-mmcu=atmega328p",
"-Wl,--as-needed"
]
},
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View file

@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2022-07-08"
components = ["rust-src", "rustfmt"]
profile = "minimal"

24
arduino/src/main.rs Normal file
View file

@ -0,0 +1,24 @@
#![no_std]
#![no_main]
// use core::panic::PanicInfo;
// #[panic_handler]
// fn panic(info: &PanicInfo) -> ! {
// loop {}
// }
use panic_halt as _;
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut led = pins.d13.into_output();
loop {
led.toggle();
arduino_hal::delay_ms(1000);
}
}

View file

@ -1,132 +0,0 @@
/// ----------------------------------------------------------------
/// ----------------------- Type Definitions -----------------------
/// ----------------------------------------------------------------
enum State {
WaitingForInput,
WaitingForAck,
};
struct Button {
const byte pin_id;
uint8_t last_state;
};
/// ----------------------------------------------------------------
/// ----------------------- Board Specifics ------------------------
/// ----------------------------------------------------------------
const int button_count = 8;
const Button buttons[button_count] = {
{
.pin_id = 6,
.last_state = LOW
},
{
.pin_id = 7,
.last_state = LOW
},
{
.pin_id = 8,
.last_state = LOW
},
{
.pin_id = 9,
.last_state = LOW
},
{
.pin_id = 10,
.last_state = LOW
},
{
.pin_id = 11,
.last_state = LOW
},
{
.pin_id = 12,
.last_state = LOW
},
{
.pin_id = 13,
.last_state = LOW
}
};
const char message_start = '<';
const char message_end = '>';
/// ----------------------------------------------------------------
/// -------------------------- Variables ---------------------------
/// ----------------------------------------------------------------
State current_state;
/// ----------------------------------------------------------------
/// --------------------------- Startup ----------------------------
/// ----------------------------------------------------------------
void setup() {
// enable serial port, with 9600 Baud
Serial.begin(9600);
// setup variables
current_state = WaitingForInput;
// setup pins
for (byte i = 0; i < button_count; i++) {
pinMode(&buttons[i], INPUT);
}
}
void loop() {
// check for possible input
if (current_state == WaitingForInput) {
for (byte i = 0; i < button_count; i++) {
check_pin(&buttons[i], i);
}
}
}
void check_pin(Button* button, byte index) {
if (digitalRead(button->pin_id) == HIGH) {
if (button->last_state == LOW) {
button->last_state = HIGH;
/*
// assemble message
Serial.print(message_start);
Serial.print(index + 1);
Serial.print(message_end);
// force new line
Serial.print('\n');
*/
debug_message(button->pin_id, "HIGH");
}
} else {
if (button->last_state == HIGH) {
button->last_state = LOW;
debug_message(button->pin_id, "LOW");
}
}
}
void debug_message(byte pin_id, char c[]) {
// assemble message
Serial.print(message_start);
Serial.print(pin_id);
Serial.print(' ');
Serial.print(c);
Serial.print(message_end);
// force new line
Serial.print('\n');
}