refactor: enforce max 8 code lines, add docstrings, fix warnings across all Rust and C SDK projects

Rust (all 15 projects):
- Refactored overlength functions: format_counter, format_u8, format_f32_1,
  format_u32_minimal, gpio_drive, read_sensor, poll_sensor, format_round_trip,
  format_u32, prepare_write_buf, write_min_digits, write_temp, UartDriver::init,
  init_spi, angle_to_pulse_us, compute_servo_level
- Added 200+ docstrings to test functions, mock structs, impl blocks
- Fixed pub static comments (//) to doc comments (///) in all main.rs files
- Fixed helper function ordering (helpers above callers)
- Fixed Fn(u32) -> FnMut(u32) bound in button poll_button
- Moved OneShot trait import from main.rs to board.rs in adc project
- Added unsafe {} blocks in flash unsafe fn bodies (Rust 2024 edition)
- Removed unused hal::Clock imports from pwm/servo main.rs
- All 15 projects build with zero errors and zero warnings

C Pico SDK (all 15 projects):
- Added docstrings to all public functions, macros, and static variables
- All 15 projects rebuilt with zero errors

Cleanup:
- Removed build/ and target/ directories from git tracking
- Added target/ to .gitignore
- Deleted temporary fix_rust_docs.py script
This commit is contained in:
Kevin Thomas
2026-04-06 08:33:17 -04:00
parent 94dac7f76b
commit e54c756423
9896 changed files with 3106 additions and 312146 deletions
+64 -20
View File
@@ -37,10 +37,10 @@ use hal::gpio::{FunctionNull, FunctionUart, Pin, PullDown, PullNone};
use hal::uart::{DataBits, Enabled, StopBits, UartConfig, UartPeripheral};
// Alias our HAL crate
#[cfg(rp2350)]
use rp235x_hal as hal;
#[cfg(rp2040)]
use rp2040_hal as hal;
#[cfg(rp2350)]
use rp235x_hal as hal;
/// External crystal frequency in Hz (12 MHz).
pub(crate) const XTAL_FREQ_HZ: u32 = 12_000_000u32;
@@ -96,7 +96,13 @@ pub(crate) fn init_clocks(
watchdog: &mut hal::Watchdog,
) -> hal::clocks::ClocksManager {
hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ, xosc, clocks, pll_sys, pll_usb, resets, watchdog,
XTAL_FREQ_HZ,
xosc,
clocks,
pll_sys,
pll_usb,
resets,
watchdog,
)
.unwrap()
}
@@ -175,6 +181,39 @@ pub(crate) fn init_delay(clocks: &hal::clocks::ClocksManager) -> cortex_m::delay
cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz())
}
/// Write 3-character right-justified angle digits into `buf`.
fn write_angle_digits(buf: &mut [u8], a: u32) -> usize {
if a >= 100 {
write_angle_hundreds(buf, a);
} else if a >= 10 {
write_angle_tens(buf, a);
} else {
write_angle_ones(buf, a);
}
3
}
/// Write digits for angles >= 100.
fn write_angle_hundreds(buf: &mut [u8], a: u32) {
buf[0] = b'0' + (a / 100) as u8;
buf[1] = b'0' + ((a / 10) % 10) as u8;
buf[2] = b'0' + (a % 10) as u8;
}
/// Write digits for angles 10..99 with leading space.
fn write_angle_tens(buf: &mut [u8], a: u32) {
buf[0] = b' ';
buf[1] = b'0' + (a / 10) as u8;
buf[2] = b'0' + (a % 10) as u8;
}
/// Write digit for angles 0..9 with leading spaces.
fn write_angle_ones(buf: &mut [u8], a: u32) {
buf[0] = b' ';
buf[1] = b' ';
buf[2] = b'0' + a as u8;
}
/// Format an angle into "Angle: NNN deg\r\n".
///
/// # Arguments
@@ -194,20 +233,6 @@ pub(crate) fn format_angle(buf: &mut [u8], angle: i32) -> usize {
pos + 6
}
/// Write 3-character right-justified angle digits into `buf`.
fn write_angle_digits(buf: &mut [u8], a: u32) -> usize {
if a >= 100 {
buf[0] = b'0' + (a / 100) as u8;
buf[1] = b'0' + ((a / 10) % 10) as u8;
buf[2] = b'0' + (a % 10) as u8;
} else if a >= 10 {
buf[0] = b' '; buf[1] = b'0' + (a / 10) as u8; buf[2] = b'0' + (a % 10) as u8;
} else {
buf[0] = b' '; buf[1] = b' '; buf[2] = b'0' + a as u8;
}
3
}
/// Sweep the servo angle upward from 0 to 180 in STEP_DEGREES increments.
///
/// # Arguments
@@ -265,10 +290,22 @@ fn apply_angle(
delay.delay_ms(STEP_DELAY_MS);
}
/// Compute the pulse width in microseconds for the given angle.
fn compute_pulse_us(angle: i32) -> u32 {
crate::servo::angle_to_pulse_us(
angle as f32,
crate::servo::SERVO_DEFAULT_MIN_US,
crate::servo::SERVO_DEFAULT_MAX_US,
) as u32
}
/// Compute the PWM level for a given angle using servo constants.
fn compute_servo_level(angle: i32) -> u32 {
let pulse = crate::servo::angle_to_pulse_us(angle as f32, crate::servo::SERVO_DEFAULT_MIN_US, crate::servo::SERVO_DEFAULT_MAX_US);
crate::servo::pulse_us_to_level(pulse as u32, crate::servo::SERVO_WRAP, crate::servo::SERVO_HZ)
crate::servo::pulse_us_to_level(
compute_pulse_us(angle),
crate::servo::SERVO_WRAP,
crate::servo::SERVO_HZ,
)
}
/// Type alias for PWM slice 3 (servo on GPIO 6, channel A).
@@ -281,7 +318,14 @@ type PwmSlice3 = hal::pwm::Slice<hal::pwm::Pwm3, hal::pwm::FreeRunning>;
/// * `pac` - PAC Peripherals singleton (consumed).
pub(crate) fn run(mut pac: hal::pac::Peripherals) -> ! {
let mut wd = hal::Watchdog::new(pac.WATCHDOG);
let clocks = init_clocks(pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS, &mut wd);
let clocks = init_clocks(
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut wd,
);
let pins = init_pins(pac.IO_BANK0, pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let uart = init_uart(pac.UART0, pins.gpio0, pins.gpio1, &mut pac.RESETS, &clocks);
let mut delay = init_delay(&clocks);
+5 -7
View File
@@ -54,24 +54,22 @@ use panic_halt as _;
#[cfg(target_arch = "arm")]
use panic_probe as _;
// Clock trait for accessing system clock frequency
use hal::Clock;
// HAL entry-point macro
use hal::entry;
// Alias our HAL crate
#[cfg(rp2350)]
use rp235x_hal as hal;
#[cfg(rp2040)]
use rp2040_hal as hal;
#[cfg(rp2350)]
use rp235x_hal as hal;
// Second-stage boot loader for RP2040
/// Second-stage boot loader for RP2040
#[unsafe(link_section = ".boot2")]
#[used]
#[cfg(rp2040)]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
// Boot metadata for the RP2350 Boot ROM
/// Boot metadata for the RP2350 Boot ROM
#[unsafe(link_section = ".start_block")]
#[used]
#[cfg(rp2350)]
@@ -83,7 +81,7 @@ fn main() -> ! {
board::run(hal::pac::Peripherals::take().unwrap())
}
// Picotool binary info metadata
/// Picotool binary info metadata
#[unsafe(link_section = ".bi_entries")]
#[used]
pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 5] = [
+37 -10
View File
@@ -81,6 +81,17 @@ pub fn clamp_pulse_us(pulse_us: u16, min_us: u16, max_us: u16) -> u16 {
}
}
/// Clamp a floating-point angle to the valid servo range [0.0, 180.0].
fn clamp_degrees(degrees: f32) -> f32 {
if degrees < 0.0f32 {
0.0f32
} else if degrees > 180.0f32 {
180.0f32
} else {
degrees
}
}
/// Map a servo angle in degrees to a pulse width in microseconds.
///
/// Clamps degrees to [0, 180], then linearly maps to the pulse range.
@@ -95,13 +106,7 @@ pub fn clamp_pulse_us(pulse_us: u16, min_us: u16, max_us: u16) -> u16 {
///
/// Pulse width in microseconds corresponding to the given angle.
pub fn angle_to_pulse_us(degrees: f32, min_us: u16, max_us: u16) -> u16 {
let d = if degrees < 0.0f32 {
0.0f32
} else if degrees > 180.0f32 {
180.0f32
} else {
degrees
};
let d = clamp_degrees(degrees);
let ratio = d / 180.0f32;
let span = (max_us - min_us) as f32;
(min_us as f32 + ratio * span + 0.5f32) as u16
@@ -127,75 +132,97 @@ mod tests {
// Import all parent module items
use super::*;
/// Pulse us to level 1000us.
#[test]
fn pulse_us_to_level_1000us() {
let level = pulse_us_to_level(1000, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 1000);
}
/// Pulse us to level 2000us.
#[test]
fn pulse_us_to_level_2000us() {
let level = pulse_us_to_level(2000, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 2000);
}
/// Pulse us to level 1500us.
#[test]
fn pulse_us_to_level_1500us() {
let level = pulse_us_to_level(1500, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 1500);
}
/// Pulse us to level zero.
#[test]
fn pulse_us_to_level_zero() {
let level = pulse_us_to_level(0, SERVO_WRAP, SERVO_HZ);
assert_eq!(level, 0);
}
/// Clamp pulse us below min.
#[test]
fn clamp_pulse_us_below_min() {
assert_eq!(clamp_pulse_us(500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 1000);
assert_eq!(
clamp_pulse_us(500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US),
1000
);
}
/// Clamp pulse us above max.
#[test]
fn clamp_pulse_us_above_max() {
assert_eq!(clamp_pulse_us(3000, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 2000);
assert_eq!(
clamp_pulse_us(3000, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US),
2000
);
}
/// Clamp pulse us within range.
#[test]
fn clamp_pulse_us_within_range() {
assert_eq!(clamp_pulse_us(1500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US), 1500);
assert_eq!(
clamp_pulse_us(1500, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US),
1500
);
}
/// Angle to pulse us zero.
#[test]
fn angle_to_pulse_us_zero() {
let pulse = angle_to_pulse_us(0.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1000);
}
/// Angle to pulse us 180.
#[test]
fn angle_to_pulse_us_180() {
let pulse = angle_to_pulse_us(180.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 2000);
}
/// Angle to pulse us 90.
#[test]
fn angle_to_pulse_us_90() {
let pulse = angle_to_pulse_us(90.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1500);
}
/// Angle to pulse us clamped negative.
#[test]
fn angle_to_pulse_us_clamped_negative() {
let pulse = angle_to_pulse_us(-10.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 1000);
}
/// Angle to pulse us clamped above.
#[test]
fn angle_to_pulse_us_clamped_above() {
let pulse = angle_to_pulse_us(200.0, SERVO_DEFAULT_MIN_US, SERVO_DEFAULT_MAX_US);
assert_eq!(pulse, 2000);
}
/// Calc clk div 150mhz.
#[test]
fn calc_clk_div_150mhz() {
let div = calc_clk_div(150_000_000, SERVO_HZ, SERVO_WRAP);