feat: 新增LED的CPP支持,优化LED的C模块结构,新增interface

This commit is contained in:
2025-11-10 00:19:57 +08:00
parent c6365a7548
commit a49d1df0d5
12 changed files with 317 additions and 103 deletions

View File

@@ -1,11 +1,12 @@
option(FEATURE_LED "Build LED Module" ON)
if (FEATURE_LED)
add_library(led_lib INTERFACE)
add_library(led_lib INTERFACE)
target_sources(led_lib PUBLIC led.c)
if (FEATURE_LED)
if (DRIVER_CPP)
target_link_libraries(led_lib INTERFACE gpio)
target_sources(led_lib INTERFACE led_cpp.cpp)
endif ()
target_include_directories(led_lib INTERFACE ./include)
target_sources(led_lib INTERFACE led.c)
target_compile_definitions(led_lib INTERFACE FEATURE_LED)
endif ()

View File

@@ -5,10 +5,28 @@
#ifndef CLION_STM32_LED_H
#define CLION_STM32_LED_H
void led_on(void);
#include "main.h"
#ifdef __cplusplus
extern "C" {
void led_off(void);
#endif
typedef struct {
GPIO_TypeDef *port;
uint16_t pin;
} led_t;
void led_init(const led_t *led);
void led_on(const led_t *led);
void led_off(const led_t *led);
void led_toggle(const led_t *led);
#ifdef __cplusplus
}
#endif
#endif //CLION_STM32_LED_H

View File

@@ -0,0 +1,29 @@
//
// Created by Wind on 2025/11/9.
//
#ifndef CLION_STM32_LED_CPP_H
#define CLION_STM32_LED_CPP_H
#ifdef __cplusplus
#include "gpio_base.hpp"
class LED : public Gpio {
public:
LED(GPIO_TypeDef *port, uint16_t pin);
~LED();
void ledOn() const;
void ledOff() const;
void ledToggle() const;
};
#endif
#endif //CLION_STM32_LED_CPP_H

View File

@@ -4,13 +4,35 @@
#include "led.h"
#include "main.h"
void led_on(void) {
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_SET);
void led_init(const led_t *led) {
const GPIO_TypeDef *port_ = led->port;
if (port_ == GPIOA)
__HAL_RCC_GPIOA_CLK_ENABLE();
else if (port_ == GPIOB)
__HAL_RCC_GPIOB_CLK_ENABLE();
else if (port_ == GPIOC)
__HAL_RCC_GPIOC_CLK_ENABLE();
else if (port_ == GPIOD)
__HAL_RCC_GPIOD_CLK_ENABLE();
else if (port_ == GPIOE)
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = led->pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(led->port, &GPIO_InitStruct);
}
void led_off(void) {
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin,GPIO_PIN_RESET);
}
void led_on(const led_t *led) {
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_SET);
}
void led_off(const led_t *led) {
HAL_GPIO_WritePin(led->port, led->pin, GPIO_PIN_RESET);
}
void led_toggle(const led_t *led) {
HAL_GPIO_TogglePin(led->port, led->pin);
}

View File

@@ -0,0 +1,22 @@
//
// Created by Wind on 2025/11/9.
//
#include "include/led_cpp.h"
LED::LED(GPIO_TypeDef *port, const uint16_t pin) : Gpio(port, pin) {
}
LED::~LED() = default;
void LED::ledOn() const {
this->write(true);
}
void LED::ledOff() const {
this->write(false);
}
void LED::ledToggle() const {
this->toggle();
}