style: enforce coding standard — headers, @brief on #define, Doxygen on statics, remove _ prefix, max 8 lines per function

This commit is contained in:
Kevin Thomas
2026-04-16 22:44:43 -04:00
parent 0833c32c94
commit 433263eac0
309 changed files with 10464 additions and 6668 deletions
+155 -67
View File
@@ -1,8 +1,50 @@
/**
* @file 0x0026_functions.c
* @brief Functions: IR remote controls LEDs with helper function decomposition
* @author Kevin Thomas
* @date 2025
*
* MIT License
*
* Copyright (c) 2025 Kevin Thomas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* -----------------------------------------------------------------------------
*
* Demonstrates function decomposition by breaking IR-controlled LED logic
* into small, focused helper functions. An IR receiver on GPIO5 decodes
* NEC commands and activates the corresponding LED with a blink effect.
*
* Wiring:
* GPIO5 -> IR receiver OUT
* GPIO16 -> LED1 anode (with current-limiting resistor to GND)
* GPIO17 -> LED2 anode (with current-limiting resistor to GND)
* GPIO18 -> LED3 anode (with current-limiting resistor to GND)
*/
#include <stdio.h>
#include <stdbool.h>
#include "pico/stdlib.h"
#include "ir.h"
/** @brief GPIO pin number for the IR receiver */
#define IR_PIN 5
typedef struct {
@@ -17,13 +59,13 @@ typedef struct {
/**
* @brief Map NEC IR command code to LED number
*
* Translates a received NEC IR command code to a logical LED number.
* Supports three button mappings for controlling individual LEDs.
* @details Translates a received NEC IR command code to a logical
* LED number. Supports three button mappings.
*
* @param ir_command NEC command code from IR receiver
* @return int LED number (1-3) or 0 if no match
* @retval 1-3 for matched LED, 0 if no match
*/
int ir_to_led_number(int ir_command) {
static int ir_to_led_number(int ir_command) {
if (ir_command == 0x0C) return 1;
if (ir_command == 0x18) return 2;
if (ir_command == 0x5E) return 3;
@@ -33,14 +75,14 @@ int ir_to_led_number(int ir_command) {
/**
* @brief Get GPIO pin number for a given LED number
*
* Retrieves the GPIO pin associated with a logical LED number
* from the LED controller structure.
* @details Retrieves the GPIO pin associated with a logical LED
* number from the LED controller structure.
*
* @param leds Pointer to LED controller structure
* @param leds pointer to LED controller structure
* @param led_num LED number (1-3)
* @return uint8_t GPIO pin number or 0 if invalid
* @retval GPIO pin number or 0 if invalid
*/
uint8_t get_led_pin(simple_led_ctrl_t *leds, int led_num) {
static uint8_t get_led_pin(simple_led_ctrl_t *leds, int led_num) {
if (led_num == 1) return leds->led1_pin;
if (led_num == 2) return leds->led2_pin;
if (led_num == 3) return leds->led3_pin;
@@ -50,11 +92,12 @@ uint8_t get_led_pin(simple_led_ctrl_t *leds, int led_num) {
/**
* @brief Turn off all LEDs in the controller
*
* Sets all three LED GPIO outputs to low, turning off all LEDs.
* @details Sets all three LED GPIO outputs to low.
*
* @param leds Pointer to LED controller structure
* @param leds pointer to LED controller structure
* @retval None
*/
void leds_all_off(simple_led_ctrl_t *leds) {
static void leds_all_off(simple_led_ctrl_t *leds) {
gpio_put(leds->led1_pin, false);
gpio_put(leds->led2_pin, false);
gpio_put(leds->led3_pin, false);
@@ -63,14 +106,15 @@ void leds_all_off(simple_led_ctrl_t *leds) {
/**
* @brief Blink an LED pin a specified number of times
*
* Toggles the specified GPIO pin on and off for the given count,
* with configurable delay between transitions.
* @details Toggles the specified GPIO pin on and off for the given
* count, with configurable delay between transitions.
*
* @param pin GPIO pin number to blink
* @param count Number of blink cycles
* @param delay_ms Delay in milliseconds for on/off periods
* @param pin GPIO pin number to blink
* @param count number of blink cycles
* @param delay_ms delay in milliseconds for on/off periods
* @retval None
*/
void blink_led(uint8_t pin, uint8_t count, uint32_t delay_ms) {
static void blink_led(uint8_t pin, uint8_t count, uint32_t delay_ms) {
for (uint8_t i = 0; i < count; i++) {
gpio_put(pin, true);
sleep_ms(delay_ms);
@@ -82,15 +126,15 @@ void blink_led(uint8_t pin, uint8_t count, uint32_t delay_ms) {
/**
* @brief Process IR command and activate corresponding LED
*
* Handles incoming IR commands by turning off all LEDs, mapping the
* command to an LED, blinking it, then holding it steady.
* @details Turns off all LEDs, maps the command to an LED number,
* blinks it, then holds it steady.
*
* @param ir_command NEC command code from IR receiver
* @param leds Pointer to LED controller structure
* @param blink_count Number of blinks before steady state
* @return int LED number activated (1-3), 0 if none, -1 if invalid
* @param ir_command NEC command code from IR receiver
* @param leds pointer to LED controller structure
* @param blink_count number of blinks before steady state
* @retval LED number activated (1-3), 0 if none, -1 if invalid
*/
int process_ir_led_command(int ir_command, simple_led_ctrl_t *leds, uint8_t blink_count) {
static int process_ir_led_command(int ir_command, simple_led_ctrl_t *leds, uint8_t blink_count) {
if (!leds || ir_command < 0) return -1;
leds_all_off(leds);
int led_num = ir_to_led_number(ir_command);
@@ -101,49 +145,93 @@ int process_ir_led_command(int ir_command, simple_led_ctrl_t *leds, uint8_t blin
return led_num;
}
int main(void) {
stdio_init_all();
/**
* @brief Initialize all three LED GPIO pins as outputs
*
* @details Configures led1_pin, led2_pin, and led3_pin from the
* structure as GPIO outputs.
*
* @param leds pointer to the LED controller structure
* @retval None
*/
static void init_led_gpios(simple_led_ctrl_t *leds) {
gpio_init(leds->led1_pin);
gpio_set_dir(leds->led1_pin, GPIO_OUT);
gpio_init(leds->led2_pin);
gpio_set_dir(leds->led2_pin, GPIO_OUT);
gpio_init(leds->led3_pin);
gpio_set_dir(leds->led3_pin, GPIO_OUT);
}
simple_led_ctrl_t leds = {
.led1_pin = 16,
.led2_pin = 17,
.led3_pin = 18,
.led1_state = false,
.led2_state = false,
.led3_state = false
};
/**
* @brief Poll IR and handle a single received key
*
* @details Reads an IR key, processes the command with 3 blinks,
* and prints the result.
*
* @param leds pointer to the LED controller structure
* @retval None
*/
/**
* @brief Handle a valid IR key press
*
* @details Prints the NEC command, processes it with 3 blinks,
* and reports the activated LED.
*
* @param leds pointer to the LED controller structure
* @param key NEC command code
* @retval None
*/
static void handle_ir_key(simple_led_ctrl_t *leds, int key) {
printf("NEC command: 0x%02X\n", key);
int activated_led = process_ir_led_command(key, leds, 3);
if (activated_led > 0) {
printf("LED %d activated on GPIO %d\n", activated_led,
get_led_pin(leds, activated_led));
}
sleep_ms(10);
}
// Initialize LED GPIOs
gpio_init(leds.led1_pin); gpio_set_dir(leds.led1_pin, GPIO_OUT);
gpio_init(leds.led2_pin); gpio_set_dir(leds.led2_pin, GPIO_OUT);
gpio_init(leds.led3_pin); gpio_set_dir(leds.led3_pin, GPIO_OUT);
// Initialize IR receiver
ir_init(IR_PIN);
printf("IR receiver on GPIO %d ready\n", IR_PIN);
printf("Press remote buttons to control LEDs with blink effects!\n");
while (true) {
int key = ir_getkey();
if (key >= 0) {
printf("NEC command: 0x%02X\n", key);
// Process the IR command with 3 blinks before steady state
int activated_led = process_ir_led_command(key, &leds, 3);
if (activated_led > 0) {
printf("LED %d activated on GPIO %d\n", activated_led,
(activated_led == 1) ? leds.led1_pin :
(activated_led == 2) ? leds.led2_pin : leds.led3_pin);
} else if (activated_led == 0) {
printf("No LED matched or chase effect played\n");
} else {
printf("Invalid command or null pointer\n");
}
sleep_ms(10);
} else {
sleep_ms(1);
}
/**
* @brief Poll IR and handle a single received key
*
* @details Reads an IR key; if valid dispatches to handler,
* otherwise yields with a short sleep.
*
* @param leds pointer to the LED controller structure
* @retval None
*/
static void poll_and_handle_ir(simple_led_ctrl_t *leds) {
int key = ir_getkey();
if (key >= 0) {
handle_ir_key(leds, key);
} else {
sleep_ms(1);
}
}
/**
* @brief Build the default LED controller structure
*
* @details Initializes pins 16-18 with all LEDs off.
*
* @return simple_led_ctrl_t initialized structure
*/
static simple_led_ctrl_t make_default_leds(void) {
simple_led_ctrl_t leds = {
.led1_pin = 16, .led2_pin = 17, .led3_pin = 18,
.led1_state = false, .led2_state = false, .led3_state = false
};
return leds;
}
int main(void) {
stdio_init_all();
simple_led_ctrl_t leds = make_default_leds();
init_led_gpios(&leds);
ir_init(IR_PIN);
printf("IR receiver on GPIO %d ready\n", IR_PIN);
while (true) {
poll_and_handle_ir(&leds);
}
}
+76 -26
View File
@@ -32,19 +32,89 @@
#include "pico/time.h"
#include "hardware/gpio.h"
/** @brief GPIO pin connected to the IR receiver */
static unsigned int ir_pin = 0;
// Wait for 'gpio' to reach 'level' or timeout (microseconds). Return elapsed us or -1.
/**
* @brief Wait for a GPIO pin to reach a given logic level
*
* Spins until the pin matches the requested level or a microsecond timeout
* is exceeded. Returns the elapsed time in microseconds, or -1 on timeout.
*
* @param gpio GPIO pin to monitor
* @param level Desired logic level (true = HIGH, false = LOW)
* @param timeout_us Maximum wait in microseconds
* @return int64_t Elapsed microseconds, or -1 on timeout
*/
static int64_t wait_for_level(unsigned int gpio, bool level, uint32_t timeout_us) {
absolute_time_t start = get_absolute_time();
while (gpio_get(gpio) != level) {
if (absolute_time_diff_us(start, get_absolute_time()) > (int64_t)timeout_us) {
if (absolute_time_diff_us(start, get_absolute_time()) > (int64_t)timeout_us)
return -1;
}
}
return absolute_time_diff_us(start, get_absolute_time());
}
/**
* @brief Wait for the NEC 9 ms leader pulse and 4.5 ms space
*
* @return bool true if a valid leader was detected, false on timeout
*/
static bool wait_leader(void) {
if (wait_for_level(ir_pin, 0, 150000) < 0) return false;
int64_t t = wait_for_level(ir_pin, 1, 12000);
if (t < 8000 || t > 10000) return false;
t = wait_for_level(ir_pin, 0, 7000);
if (t < 3500 || t > 5000) return false;
return true;
}
/**
* @brief Read a single NEC-encoded bit from the IR receiver
*
* Measures the mark/space timing and shifts the result into the
* appropriate byte of the data array.
*
* @param data 4-byte array accumulating received bits
* @param i Bit index (0-31)
* @return bool true on success, false on timeout or protocol error
*/
static bool read_nec_bit(uint8_t *data, int i) {
if (wait_for_level(ir_pin, 1, 1000) < 0) return false;
int64_t t = wait_for_level(ir_pin, 0, 2500);
if (t < 200) return false;
int byte_idx = i / 8;
int bit_idx = i % 8;
if (t > 1200) data[byte_idx] |= (1 << bit_idx);
return true;
}
/**
* @brief Read all 32 data bits of an NEC frame
*
* @param data 4-byte array filled with the received address and command
* @return bool true if all 32 bits were read, false on timeout
*/
static bool read_32_bits(uint8_t *data) {
for (int i = 0; i < 32; ++i)
if (!read_nec_bit(data, i)) return false;
return true;
}
/**
* @brief Validate an NEC frame and extract the command byte
*
* Checks that the address and command pairs are bitwise-inverted.
*
* @param data 4-byte NEC frame (addr, ~addr, cmd, ~cmd)
* @return int Command byte (0-255) on success, -1 on validation failure
*/
static int validate_nec_frame(const uint8_t *data) {
if ((uint8_t)(data[0] + data[1]) == 0xFF && (uint8_t)(data[2] + data[3]) == 0xFF)
return data[2];
return -1;
}
void ir_init(uint8_t pin) {
ir_pin = pin;
gpio_init(pin);
@@ -53,28 +123,8 @@ void ir_init(uint8_t pin) {
}
int ir_getkey(void) {
// leader low (~9 ms)
if (wait_for_level(ir_pin, 0, 150000) < 0) return -1;
int64_t t = wait_for_level(ir_pin, 1, 12000);
if (t < 8000 || t > 10000) return -1;
t = wait_for_level(ir_pin, 0, 7000);
if (t < 3500 || t > 5000) return -1;
if (!wait_leader()) return -1;
uint8_t data[4] = {0, 0, 0, 0};
for (int i = 0; i < 32; ++i) {
if (wait_for_level(ir_pin, 1, 1000) < 0) return -1;
t = wait_for_level(ir_pin, 0, 2500);
if (t < 200) return -1;
int byte_idx = i / 8;
int bit_idx = i % 8;
if (t > 1200) data[byte_idx] |= (1 << bit_idx); // logical '1'
}
// Validate address/data inverted pairs
if ((uint8_t)(data[0] + data[1]) == 0xFF && (uint8_t)(data[2] + data[3]) == 0xFF) {
return data[2];
}
return -1;
if (!read_32_bits(data)) return -1;
return validate_nec_frame(data);
}