Skip to content

Commit 9f4f6a5

Browse files
cargo fmt code and add CI for ensuring formatting going forward (#22)
* fmt * add ci for fmt * remove --all flag
1 parent 66682cb commit 9f4f6a5

File tree

18 files changed

+345
-327
lines changed

18 files changed

+345
-327
lines changed

.github/workflows/fmt.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Format Check
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
pull_request:
7+
branches: [ master, main ]
8+
9+
env:
10+
RUST_BACKTRACE: 1
11+
12+
jobs:
13+
fmt:
14+
name: Rust Format Check
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Install Rust
21+
uses: dtolnay/rust-toolchain@stable
22+
with:
23+
components: rustfmt
24+
25+
- name: Check formatting
26+
run: cargo fmt -- --check

examples/baton.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
extern crate linux_embedded_hal as linux_hal;
21
extern crate bit_reverse;
2+
extern crate linux_embedded_hal as linux_hal;
33
extern crate pscontroller_rs;
44

5-
use std::io;
6-
use linux_hal::Spidev;
75
use linux_hal::spidev::{SpidevOptions, SPI_MODE_3};
86
use linux_hal::Pin;
7+
use linux_hal::Spidev;
8+
use std::io;
99

10-
use pscontroller_rs::{
11-
PlayStationPort,
12-
};
10+
use pscontroller_rs::PlayStationPort;
1311

1412
// Specific to the host device used on Linux, you'll have to change the following
1513
// parameters depending on your board and also export and allow writing to the GPIO
@@ -18,15 +16,15 @@ const SPI_SPEED: u32 = 10_000;
1816

1917
// This will build the SPI device communication for us
2018
fn build_spi() -> io::Result<Spidev> {
21-
let mut spi = Spidev::open(SPI_DEVICE)?;
22-
let opts = SpidevOptions::new()
23-
.bits_per_word(8)
24-
.max_speed_hz(SPI_SPEED)
25-
.mode(SPI_MODE_3)
26-
.build();
27-
spi.configure(&opts)?;
28-
29-
Ok(spi)
19+
let mut spi = Spidev::open(SPI_DEVICE)?;
20+
let opts = SpidevOptions::new()
21+
.bits_per_word(8)
22+
.max_speed_hz(SPI_SPEED)
23+
.mode(SPI_MODE_3)
24+
.build();
25+
spi.configure(&opts)?;
26+
27+
Ok(spi)
3028
}
3129

3230
fn main() {
@@ -42,7 +40,7 @@ fn main() {
4240
Err(_) => {
4341
print!("\rError reading controller");
4442
continue;
45-
},
43+
}
4644
Ok(x) => x,
4745
};
4846

@@ -52,12 +50,14 @@ fn main() {
5250
controller = controller_data.b;
5351
}
5452

55-
println!("\rA:{}, B:{} - Z: {:03} X: {:03} Y: {:03} A: {:03}",
53+
println!(
54+
"\rA:{}, B:{} - Z: {:03} X: {:03} Y: {:03} A: {:03}",
5655
controller.buttons.a(),
5756
controller.buttons.b(),
5857
controller.z,
5958
controller.x,
6059
controller.y,
61-
controller.a);
60+
controller.a
61+
);
6262
}
6363
}

examples/console.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
extern crate linux_embedded_hal as linux_hal;
21
extern crate embedded_hal;
2+
extern crate linux_embedded_hal as linux_hal;
33
extern crate pscontroller_rs;
44

5-
use std::io;
6-
use std::{thread, time};
7-
use linux_hal::Spidev;
85
use linux_hal::spidev::{SpidevOptions, SPI_MODE_3};
96
use linux_hal::Pin;
7+
use linux_hal::Spidev;
8+
use std::io;
9+
use std::{thread, time};
1010

1111
use pscontroller_rs::PlayStationPort;
1212

@@ -16,69 +16,69 @@ const SPI_DEVICE: &str = "/dev/spidev0.0";
1616
const SPI_SPEED: u32 = 100_000;
1717
// If you need to use an alternate pin for cable select, uncomment the relevant bits
1818
// and pass the pin into psp's new() function.
19-
//const SPI_ENABLE_PIN: u64 = 4;
19+
//const SPI_ENABLE_PIN: u64 = 4;
2020

2121
fn build_spi() -> io::Result<Spidev> {
22-
let mut spi = Spidev::open(SPI_DEVICE)?;
23-
let opts = SpidevOptions::new()
24-
.bits_per_word(8)
25-
.max_speed_hz(SPI_SPEED)
26-
.mode(SPI_MODE_3)
27-
.build();
28-
spi.configure(&opts)?;
22+
let mut spi = Spidev::open(SPI_DEVICE)?;
23+
let opts = SpidevOptions::new()
24+
.bits_per_word(8)
25+
.max_speed_hz(SPI_SPEED)
26+
.mode(SPI_MODE_3)
27+
.build();
28+
spi.configure(&opts)?;
2929

30-
Ok(spi)
30+
Ok(spi)
3131
}
3232

3333
fn main() {
3434
let spi = build_spi().unwrap();
3535
//let enable_pin = Pin::new(SPI_ENABLE_PIN);
3636
//let mut psp = PlayStationPort::new(spi, Some(enable_pin));
37-
let mut psp = PlayStationPort::new(spi, None::<Pin>);
38-
let mut command = [0u8; 32];
39-
let mut buffer = [0u8; 32];
37+
let mut psp = PlayStationPort::new(spi, None::<Pin>);
38+
let mut command = [0u8; 32];
39+
let mut buffer = [0u8; 32];
4040

41-
command[1] = 0x42;
41+
command[1] = 0x42;
4242

43-
let mut now = time::Instant::now();
44-
let sleep_duration = time::Duration::from_micros(20_000);
45-
let sample_duration = time::Duration::from_secs(1);
46-
let mut count = 0;
47-
let mut failure = 0;
48-
let mut rate = String::new();
43+
let mut now = time::Instant::now();
44+
let sleep_duration = time::Duration::from_micros(20_000);
45+
let sample_duration = time::Duration::from_secs(1);
46+
let mut count = 0;
47+
let mut failure = 0;
48+
let mut rate = String::new();
4949

50-
psp.enable_pressure().unwrap();
50+
psp.enable_pressure().unwrap();
5151

52-
loop {
53-
thread::sleep(sleep_duration);
52+
loop {
53+
thread::sleep(sleep_duration);
5454

55-
psp.send_command(&command, &mut buffer).unwrap();
55+
psp.send_command(&command, &mut buffer).unwrap();
5656

57-
if now.elapsed() > sample_duration {
58-
now = time::Instant::now();
59-
rate = format!("{0:04}/{1:04}", count, failure);
60-
count = 0;
61-
failure = 0;
62-
}
63-
println!("");
64-
print!("Rate: ({}) - ", rate);
57+
if now.elapsed() > sample_duration {
58+
now = time::Instant::now();
59+
rate = format!("{0:04}/{1:04}", count, failure);
60+
count = 0;
61+
failure = 0;
62+
}
63+
println!("");
64+
print!("Rate: ({}) - ", rate);
6565

66-
// Print the three byte header and X * 16bit message
67-
let mut c = 3 + (buffer[1] & 0xF) * 2;
68-
for item in buffer.iter() {
69-
// Only print the number of bytes the controller claims exists
70-
if c == 0 {
71-
break;
72-
}
66+
// Print the three byte header and X * 16bit message
67+
let mut c = 3 + (buffer[1] & 0xF) * 2;
68+
for item in buffer.iter() {
69+
// Only print the number of bytes the controller claims exists
70+
if c == 0 {
71+
break;
72+
}
7373

74-
print!("{:02x} ", item);
75-
c -= 1;
76-
}
74+
print!("{:02x} ", item);
75+
c -= 1;
76+
}
7777

78-
if buffer[1] == 0xff {
79-
failure += 1;
80-
} else {
81-
count += 1;
82-
}
83-
}
78+
if buffer[1] == 0xff {
79+
failure += 1;
80+
} else {
81+
count += 1;
82+
}
83+
}
8484
}

examples/dualshock.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
extern crate linux_embedded_hal as linux_hal;
21
extern crate bit_reverse;
2+
extern crate linux_embedded_hal as linux_hal;
33
extern crate pscontroller_rs;
44

5-
use std::io;
6-
use linux_hal::Spidev;
75
use linux_hal::spidev::{SpidevOptions, SPI_MODE_3};
86
use linux_hal::Pin;
7+
use linux_hal::Spidev;
8+
use std::io;
99

10-
use pscontroller_rs::{
11-
PlayStationPort,
12-
Device,
13-
classic::GamepadButtons,
14-
dualshock::ControlDS
15-
};
10+
use pscontroller_rs::{classic::GamepadButtons, dualshock::ControlDS, Device, PlayStationPort};
1611

1712
// Specific to the host device used on Linux, you'll have to change the following
1813
// parameters depending on your board and also export and allow writing to the GPIO
1914
const SPI_DEVICE: &str = "/dev/spidev0.0";
2015
const SPI_SPEED: u32 = 10_000;
2116

22-
// This will build the
17+
// This will build the
2318
fn build_spi() -> io::Result<Spidev> {
24-
let mut spi = Spidev::open(SPI_DEVICE)?;
25-
let opts = SpidevOptions::new()
26-
.bits_per_word(8)
27-
.max_speed_hz(SPI_SPEED)
28-
.mode(SPI_MODE_3)
29-
.build();
30-
spi.configure(&opts)?;
19+
let mut spi = Spidev::open(SPI_DEVICE)?;
20+
let opts = SpidevOptions::new()
21+
.bits_per_word(8)
22+
.max_speed_hz(SPI_SPEED)
23+
.mode(SPI_MODE_3)
24+
.build();
25+
spi.configure(&opts)?;
3126

32-
Ok(spi)
27+
Ok(spi)
3328
}
3429

3530
fn set_motors(buttons: &GamepadButtons, small: &mut bool, big: &mut u8) {
@@ -68,32 +63,36 @@ fn main() {
6863
Err(_) => {
6964
print!("\rError reading controller");
7065
continue;
71-
},
66+
}
7267
Ok(x) => x,
7368
};
7469

7570
match controller {
7671
Device::DualShock(x) | Device::AnalogJoystick(x) => {
77-
println!("DualShock: Start? {0} - R:{1:02x},{2:02x}, L:{3:02x},{4:02x}",
72+
println!(
73+
"DualShock: Start? {0} - R:{1:02x},{2:02x}, L:{3:02x},{4:02x}",
7874
x.buttons.start(),
7975
x.rx,
8076
x.ry,
8177
x.lx,
82-
x.ly);
78+
x.ly
79+
);
8380

8481
set_motors(&x.buttons, &mut small, &mut big);
85-
},
82+
}
8683
Device::DualShock2(x) => {
87-
println!("DualShock2: Start? {0} - R:{1:02x},{2:02x} - X Pressure:{3:02x}",
84+
println!(
85+
"DualShock2: Start? {0} - R:{1:02x},{2:02x} - X Pressure:{3:02x}",
8886
x.buttons.start(),
8987
x.rx,
9088
x.ry,
91-
x.pressures[6]);
89+
x.pressures[6]
90+
);
9291

9392
set_motors(&x.buttons, &mut small, &mut big);
94-
},
93+
}
9594
Device::None => println!("Please plug in a controller"),
9695
_ => println!("This example doesn't support the current controller"),
97-
}
96+
}
9897
}
9998
}

0 commit comments

Comments
 (0)