Test 1 (ChatGPT Code)

This commit is contained in:
Fast-Blast
2026-03-04 17:36:21 +01:00
parent 014a36b709
commit 884fcdae59
2 changed files with 74 additions and 2 deletions

25
Cargo.lock generated Normal file
View File

@@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "atov-software"
version = "0.1.0"
dependencies = [
"rppal",
]
[[package]]
name = "libc"
version = "0.2.182"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
[[package]]
name = "rppal"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dc171bbe325b04172e18d917c58c2cf1fb5adfd9ffabb1d6b3d62ba4c1c1331"
dependencies = [
"libc",
]

View File

@@ -1,3 +1,50 @@
fn main() { use rppal::gpio::{Gpio, OutputPin};
println!("Hello, world!"); use rppal::pwm::{Pwm, Channel, Polarity};
use std::error::Error;
struct Motor {
in1: OutputPin,
in2: OutputPin,
pwm: Pwm,
}
impl Motor {
fn forward(&mut self, speed: f64) {
self.in1.set_high();
self.in2.set_low();
self.pwm.set_duty_cycle(speed).unwrap();
}
fn backward(&mut self, speed: f64) {
self.in1.set_low();
self.in2.set_high();
self.pwm.set_duty_cycle(speed).unwrap();
}
fn stop(&mut self) {
self.in1.set_low();
self.in2.set_low();
self.pwm.set_duty_cycle(0.0).unwrap();
}
}
fn main() -> Result<(), Box<dyn Error>> {
let gpio = Gpio::new()?;
let in1 = gpio.get(27)?.into_output();
let in2 = gpio.get(22)?.into_output();
let pwm = Pwm::with_frequency(
Channel::Pwm0,
1000.0,
0.0,
Polarity::Normal,
true,
)?;
let mut left_motor = Motor { in1, in2, pwm };
left_motor.forward(0.7);
Ok(())
} }