command deserialization
This commit is contained in:
parent
028458d80b
commit
5e31ad2e24
3 changed files with 384 additions and 8 deletions
315
Cargo.lock
generated
Normal file
315
Cargo.lock
generated
Normal file
|
@ -0,0 +1,315 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "arduino-hal"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/rahix/avr-hal?rev=fafaf587a32a4500239fd073f89d1b9c36b48092#fafaf587a32a4500239fd073f89d1b9c36b48092"
|
||||
dependencies = [
|
||||
"atmega-hal",
|
||||
"avr-device",
|
||||
"avr-hal-generic",
|
||||
"cfg-if",
|
||||
"embedded-hal 1.0.0",
|
||||
"ufmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atmega-hal"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/rahix/avr-hal?rev=fafaf587a32a4500239fd073f89d1b9c36b48092#fafaf587a32a4500239fd073f89d1b9c36b48092"
|
||||
dependencies = [
|
||||
"avr-device",
|
||||
"avr-hal-generic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "avr-device"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "520f2031240156132bd83639d86aeb5b1907e6a228d9a4b44c3e9699827e6dae"
|
||||
dependencies = [
|
||||
"avr-device-macros",
|
||||
"bare-metal",
|
||||
"cfg-if",
|
||||
"vcell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "avr-device-macros"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47c26fd925156183eb10e821b2ef7e06f8163f5a64a0bbe52fc896be2c6cbd3f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "avr-hal-generic"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/rahix/avr-hal?rev=fafaf587a32a4500239fd073f89d1b9c36b48092#fafaf587a32a4500239fd073f89d1b9c36b48092"
|
||||
dependencies = [
|
||||
"avr-device",
|
||||
"embedded-hal 0.2.7",
|
||||
"embedded-hal 1.0.0",
|
||||
"embedded-hal-bus",
|
||||
"embedded-storage",
|
||||
"nb 1.1.0",
|
||||
"paste",
|
||||
"ufmt",
|
||||
"unwrap-infallible",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bare-metal"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
|
||||
|
||||
[[package]]
|
||||
name = "critical-section"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff"
|
||||
dependencies = [
|
||||
"nb 0.1.3",
|
||||
"void",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-hal-bus"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57b4e6ede84339ebdb418cd986e6320a34b017cdf99b5cc3efceec6450b06886"
|
||||
dependencies = [
|
||||
"critical-section",
|
||||
"embedded-hal 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "embedded-storage"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heapless"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
|
||||
dependencies = [
|
||||
"hash32",
|
||||
"serde",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nb"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"
|
||||
dependencies = [
|
||||
"nb 1.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nb"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
|
||||
|
||||
[[package]]
|
||||
name = "panic-halt"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11"
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "robot-controller"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"arduino-hal",
|
||||
"avr-device",
|
||||
"embedded-hal 1.0.0",
|
||||
"nb 1.1.0",
|
||||
"panic-halt",
|
||||
"serde",
|
||||
"serde-json-core",
|
||||
"ufmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-json-core"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b81787e655bd59cecadc91f7b6b8651330b2be6c33246039a65e5cd6f4e0828"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.104",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stable_deref_trait"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ufmt"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a64846ec02b57e9108d6469d98d1648782ad6bb150a95a9baac26900bbeab9d"
|
||||
dependencies = [
|
||||
"ufmt-macros",
|
||||
"ufmt-write",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ufmt-macros"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d337d3be617449165cb4633c8dece429afd83f84051024079f97ad32a9663716"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ufmt-write"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "unwrap-infallible"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "151ac09978d3c2862c4e39b557f4eceee2cc72150bc4cb4f16abf061b6e381fb"
|
||||
|
||||
[[package]]
|
||||
name = "vcell"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002"
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
|
@ -15,6 +15,9 @@ panic-halt = "1.0.0"
|
|||
ufmt = "0.2.0"
|
||||
nb = "1.1.0"
|
||||
embedded-hal = "1.0"
|
||||
avr-device = "0.7.0"
|
||||
serde-json-core = "0.6.0"
|
||||
serde = { version = "1.0.219", default-features = false, features = ["serde_derive", "derive"] }
|
||||
|
||||
[dependencies.arduino-hal]
|
||||
git = "https://github.com/rahix/avr-hal"
|
||||
|
|
74
src/main.rs
74
src/main.rs
|
@ -2,6 +2,7 @@
|
|||
#![no_main]
|
||||
#![feature(abi_avr_interrupt)]
|
||||
|
||||
use core::cell::RefCell;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
use arduino_hal::clock::MHz16;
|
||||
|
@ -10,7 +11,10 @@ use arduino_hal::hal::usart::UsartReader;
|
|||
use arduino_hal::pac::USART0;
|
||||
use arduino_hal::port::mode::*;
|
||||
use arduino_hal::prelude::*;
|
||||
use avr_device::interrupt::{self, Mutex};
|
||||
use panic_halt as _;
|
||||
use serde::Deserialize;
|
||||
use serde_json_core::from_str;
|
||||
|
||||
fn shift_out(data_pin: &mut Pin<Output, PB0>, clock_pin: &mut Pin<Output, PD4>, data: &u8) {
|
||||
for i in 0..8 {
|
||||
|
@ -42,11 +46,30 @@ fn update_shift_register(
|
|||
|
||||
static mut UART_RX: MaybeUninit<UsartReader<USART0, Pin<Input, PD0>, Pin<Output, PD1>, MHz16>> = MaybeUninit::uninit();
|
||||
|
||||
#[avr_device::interrupt(atmega328p)]
|
||||
fn USART_RX() {
|
||||
let rx = unsafe{ &mut *UART_RX.as_mut_ptr() };
|
||||
static INPUT_LINE: Mutex<RefCell<[u8; 20]>> = Mutex::new(RefCell::new([0;20]));
|
||||
|
||||
rx.read();
|
||||
#[avr_device::interrupt(atmega328p)]
|
||||
unsafe fn USART_RX() {
|
||||
let rx = &mut *UART_RX.as_mut_ptr();
|
||||
|
||||
static mut BUF: [u8; 20] = [0;20];
|
||||
static mut N: usize = 0; // index into line
|
||||
|
||||
if let Ok(val) = rx.read() {
|
||||
if val == b'\n' {
|
||||
|
||||
interrupt::free(|cs| {
|
||||
let mut line = INPUT_LINE.borrow(cs).borrow_mut();
|
||||
*line = BUF;
|
||||
line[N] = b'\0';
|
||||
});
|
||||
|
||||
N = 0;
|
||||
} else {
|
||||
BUF[N] = val;
|
||||
N += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -102,15 +125,50 @@ fn main() -> ! {
|
|||
|
||||
led.toggle();
|
||||
|
||||
// fwd left
|
||||
shift_register |= LEFT_FWD | RIGHT_FWD;
|
||||
let command = decode_command();
|
||||
|
||||
if command.left > 0. {
|
||||
shift_register |= LEFT_FWD;
|
||||
}
|
||||
if command.left < 0. {
|
||||
shift_register |= LEFT_REV;
|
||||
}
|
||||
|
||||
if command.right > 0. {
|
||||
shift_register |= RIGHT_FWD;
|
||||
}
|
||||
if command.right < 0. {
|
||||
shift_register |= RIGHT_REV;
|
||||
}
|
||||
|
||||
fn to_pwm(val: f32) -> u8 {
|
||||
(val.abs() * 255.0) as u8
|
||||
}
|
||||
|
||||
// 16/255
|
||||
tc2.ocr2a.write(|w| w.bits(000)); // left
|
||||
tc2.ocr2b.write(|w| w.bits(000)); // right
|
||||
tc2.ocr2a.write(|w| w.bits(to_pwm(command.left))); // left
|
||||
tc2.ocr2b.write(|w| w.bits(to_pwm(command.right))); // right
|
||||
|
||||
update_shift_register(&mut data_pin, &mut latch_pin, &mut clock_pin, &shift_register);
|
||||
|
||||
arduino_hal::delay_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
struct Command {
|
||||
left: f32,
|
||||
right: f32,
|
||||
}
|
||||
|
||||
fn decode_command() -> Command {
|
||||
let input = interrupt::free(|cs| {
|
||||
INPUT_LINE.borrow(cs).borrow().clone()
|
||||
});
|
||||
|
||||
let Some(length) = input.iter().position(|n| *n == 0) else { return Default::default() };
|
||||
|
||||
let Ok(input) = str::from_utf8(&input[..length]) else { return Default::default() };
|
||||
|
||||
from_str(input.trim()).unwrap_or((Default::default(),0)).0
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue