Files
Embedded-Hacking/drivers/0x05_servo_cbm/Src/main.c
T
Kevin Thomas e54c756423 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
2026-04-06 08:33:17 -04:00

108 lines
2.8 KiB
C

/**
******************************************************************************
* @file main.c
* @author Kevin Thomas
* @brief SG90 servo motor sweep demonstration.
*
* Demonstrates servo control using the servo driver. The servo
* sweeps from 0 to 180 degrees and back in 10-degree steps,
* reporting each angle over UART.
*
* Wiring:
* GPIO0 -> UART TX (USB-to-UART adapter RX)
* GPIO1 -> UART RX (USB-to-UART adapter TX)
* GPIO6 -> Servo signal (orange/yellow)
* 5V -> Servo power (red)
* GND -> Servo ground (brown/black)
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_servo.h"
#include "rp2350_uart.h"
#include "rp2350_delay.h"
#define STEP_DEGREES 10
#define STEP_DELAY_MS 150
/**
* @brief Convert a uint8_t to a decimal string.
* @param value number to convert (0-255)
* @param buf output buffer (at least 4 bytes)
* @retval None
*/
static void _uint8_to_str(uint8_t value, char *buf)
{
uint8_t idx = 0;
if (value >= 100)
buf[idx++] = (char)('0' + value / 100);
if (value >= 10)
buf[idx++] = (char)('0' + (value / 10) % 10);
buf[idx++] = (char)('0' + value % 10);
buf[idx] = '\0';
}
/**
* @brief Print an angle value as a decimal string over UART.
* @param angle angle in degrees (0-180)
* @retval None
*/
static void _print_angle(uint8_t angle)
{
char buf[4];
_uint8_to_str(angle, buf);
uart_puts("Angle: ");
uart_puts(buf);
uart_puts(" deg\r\n");
}
/**
* @brief Sweep servo angle upward from 0 to 180 degrees.
* @retval None
*/
static void _sweep_up(void)
{
for (uint8_t angle = 0; angle <= 180; angle += STEP_DEGREES) {
servo_set_angle(angle);
_print_angle(angle);
delay_ms(STEP_DELAY_MS);
}
}
/**
* @brief Sweep servo angle downward from 180 to 0 degrees.
* @retval None
*/
static void _sweep_down(void)
{
for (int16_t angle = 180; angle >= 0; angle -= STEP_DEGREES) {
servo_set_angle((uint8_t)angle);
_print_angle((uint8_t)angle);
delay_ms(STEP_DELAY_MS);
}
}
/**
* @brief Application entry point for the servo sweep demo.
* @retval int does not return
*/
int main(void)
{
uart_puts("Servo driver initialized on GPIO6\r\n");
uart_puts("Sweeping 0 -> 180 -> 0 degrees\r\n");
while (1) {
_sweep_up();
_sweep_down();
}
}