mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-07-10 22:33:54 +02:00
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:
@@ -238,6 +238,16 @@ fn lcd_send(i2c: &mut impl I2c, addr: u8, value: u8, mode: u8, delay: &mut corte
|
||||
lcd_write4(i2c, addr, value & 0x0F, mode, delay);
|
||||
}
|
||||
|
||||
/// Send three 0x03 nibbles with required power-on delays.
|
||||
fn lcd_reset_pulse_3x(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::Delay) {
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_ms(5);
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_us(150);
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_us(150);
|
||||
}
|
||||
|
||||
/// Execute the HD44780 4-bit mode power-on reset sequence.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -251,16 +261,6 @@ fn lcd_hd44780_reset(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::
|
||||
delay.delay_us(150);
|
||||
}
|
||||
|
||||
/// Send three 0x03 nibbles with required power-on delays.
|
||||
fn lcd_reset_pulse_3x(i2c: &mut impl I2c, addr: u8, delay: &mut cortex_m::delay::Delay) {
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_ms(5);
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_us(150);
|
||||
lcd_write4(i2c, addr, 0x03, 0, delay);
|
||||
delay.delay_us(150);
|
||||
}
|
||||
|
||||
/// Send post-reset configuration commands to the HD44780.
|
||||
///
|
||||
/// # Arguments
|
||||
|
||||
@@ -97,6 +97,30 @@ pub fn cursor_address(line: u8, position: u8) -> u8 {
|
||||
0x80 | (position + ROW_OFFSETS[row])
|
||||
}
|
||||
|
||||
/// Extract the six decimal digits of a counter value into an array.
|
||||
fn fill_digit_array(count: u32) -> [u8; 6] {
|
||||
[
|
||||
((count / 100000) % 10) as u8,
|
||||
((count / 10000) % 10) as u8,
|
||||
((count / 1000) % 10) as u8,
|
||||
((count / 100) % 10) as u8,
|
||||
((count / 10) % 10) as u8,
|
||||
(count % 10) as u8,
|
||||
]
|
||||
}
|
||||
|
||||
/// Write six counter digits into `buf` starting at `start`, suppressing leading zeros.
|
||||
fn write_counter_digits(buf: &mut [u8], start: usize, digits: &[u8; 6]) -> usize {
|
||||
let mut pos = start;
|
||||
let mut leading = true;
|
||||
for (i, &d) in digits.iter().enumerate() {
|
||||
if i == 5 || d != 0 { leading = false; }
|
||||
buf[pos] = if leading { b' ' } else { b'0' + d };
|
||||
pos += 1;
|
||||
}
|
||||
pos
|
||||
}
|
||||
|
||||
/// Format a counter value as `"Count: NNNNNN"` (right-justified, 6 digits).
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -108,24 +132,9 @@ pub fn cursor_address(line: u8, position: u8) -> u8 {
|
||||
///
|
||||
/// Number of bytes written into the buffer.
|
||||
pub fn format_counter(buf: &mut [u8], count: u32) -> usize {
|
||||
let prefix = b"Count: ";
|
||||
buf[..7].copy_from_slice(prefix);
|
||||
let mut pos = 7;
|
||||
let digits = [
|
||||
((count / 100000) % 10) as u8,
|
||||
((count / 10000) % 10) as u8,
|
||||
((count / 1000) % 10) as u8,
|
||||
((count / 100) % 10) as u8,
|
||||
((count / 10) % 10) as u8,
|
||||
(count % 10) as u8,
|
||||
];
|
||||
let mut leading = true;
|
||||
for (i, &d) in digits.iter().enumerate() {
|
||||
if i == 5 || d != 0 { leading = false; }
|
||||
buf[pos] = if leading { b' ' } else { b'0' + d };
|
||||
pos += 1;
|
||||
}
|
||||
pos
|
||||
buf[..7].copy_from_slice(b"Count: ");
|
||||
let digits = fill_digit_array(count);
|
||||
write_counter_digits(buf, 7, &digits)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -133,59 +142,70 @@ mod tests {
|
||||
// Import all parent module items
|
||||
use super::*;
|
||||
|
||||
/// Build nibble command mode.
|
||||
#[test]
|
||||
fn build_nibble_command_mode() {
|
||||
let b = build_nibble(0x03, 4, 0, 0x08);
|
||||
assert_eq!(b, 0x38);
|
||||
}
|
||||
|
||||
/// Build nibble data mode.
|
||||
#[test]
|
||||
fn build_nibble_data_mode() {
|
||||
let b = build_nibble(0x04, 4, 1, 0x08);
|
||||
assert_eq!(b, 0x49);
|
||||
}
|
||||
|
||||
/// Build nibble no backlight.
|
||||
#[test]
|
||||
fn build_nibble_no_backlight() {
|
||||
let b = build_nibble(0x0F, 4, 0, 0x00);
|
||||
assert_eq!(b, 0xF0);
|
||||
}
|
||||
|
||||
/// Nibble with en sets bit.
|
||||
#[test]
|
||||
fn nibble_with_en_sets_bit() {
|
||||
assert_eq!(nibble_with_en(0x38), 0x3C);
|
||||
}
|
||||
|
||||
/// Nibble without en clears bit.
|
||||
#[test]
|
||||
fn nibble_without_en_clears_bit() {
|
||||
assert_eq!(nibble_without_en(0x3C), 0x38);
|
||||
}
|
||||
|
||||
/// Cursor address line0 col0.
|
||||
#[test]
|
||||
fn cursor_address_line0_col0() {
|
||||
assert_eq!(cursor_address(0, 0), 0x80);
|
||||
}
|
||||
|
||||
/// Cursor address line1 col0.
|
||||
#[test]
|
||||
fn cursor_address_line1_col0() {
|
||||
assert_eq!(cursor_address(1, 0), 0xC0);
|
||||
}
|
||||
|
||||
/// Cursor address line0 col5.
|
||||
#[test]
|
||||
fn cursor_address_line0_col5() {
|
||||
assert_eq!(cursor_address(0, 5), 0x85);
|
||||
}
|
||||
|
||||
/// Cursor address line1 col15.
|
||||
#[test]
|
||||
fn cursor_address_line1_col15() {
|
||||
assert_eq!(cursor_address(1, 15), 0xCF);
|
||||
}
|
||||
|
||||
/// Cursor address clamps line.
|
||||
#[test]
|
||||
fn cursor_address_clamps_line() {
|
||||
assert_eq!(cursor_address(5, 0), 0xC0);
|
||||
}
|
||||
|
||||
/// Format counter zero.
|
||||
#[test]
|
||||
fn format_counter_zero() {
|
||||
let mut buf = [0u8; 16];
|
||||
@@ -193,6 +213,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"Count: 0");
|
||||
}
|
||||
|
||||
/// Format counter one.
|
||||
#[test]
|
||||
fn format_counter_one() {
|
||||
let mut buf = [0u8; 16];
|
||||
@@ -200,6 +221,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"Count: 1");
|
||||
}
|
||||
|
||||
/// Format counter large.
|
||||
#[test]
|
||||
fn format_counter_large() {
|
||||
let mut buf = [0u8; 16];
|
||||
@@ -207,6 +229,7 @@ mod tests {
|
||||
assert_eq!(&buf[..n], b"Count: 123456");
|
||||
}
|
||||
|
||||
/// Format counter six digits.
|
||||
#[test]
|
||||
fn format_counter_six_digits() {
|
||||
let mut buf = [0u8; 16];
|
||||
|
||||
@@ -65,13 +65,13 @@ use rp235x_hal as hal;
|
||||
#[cfg(rp2040)]
|
||||
use rp2040_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 +83,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] = [
|
||||
|
||||
Reference in New Issue
Block a user