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

@@ -0,0 +1,12 @@
add_subdirectory(led)
add_library(driver INTERFACE)
target_link_libraries(driver INTERFACE led_lib)
target_sources(driver INTERFACE ./base/interface.cpp)
target_include_directories(driver INTERFACE ./base)
if (DRIVER_CPP)
add_subdirectory(base/gpio)
target_compile_definitions(driver INTERFACE DRIVER_CPP)
endif ()

View File

@@ -0,0 +1,3 @@
add_library(gpio INTERFACE)
target_sources(gpio PUBLIC include/gpio_base.hpp)
target_include_directories(gpio INTERFACE ./include)

View File

@@ -0,0 +1,79 @@
//
// Created by Wind on 2025/11/9.
//
#ifndef CLION_STM32_GPIO_H
#define CLION_STM32_GPIO_H
#include "main.h"
class Gpio {
public:
enum class Mode {
Input,
Output,
Alternate,
Analog
};
enum class PULL {
None,
PullUp,
PullDown,
};
Gpio(GPIO_TypeDef *port, const uint16_t pin) : port_(port), pin_(pin) {
enableClock();
}
void setMode(const Mode mode, const PULL pull) const {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_;
switch (mode) {
case Mode::Input: GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
break;
case Mode::Output: GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
break;
case Mode::Alternate: GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
break;
case Mode::Analog: GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
break;
}
switch (pull) {
case PULL::None: GPIO_InitStruct.Pull = GPIO_NOPULL;
break;
case PULL::PullUp: GPIO_InitStruct.Pull = GPIO_PULLUP;
break;
case PULL::PullDown: GPIO_InitStruct.Pull = GPIO_PULLDOWN;
break;
}
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(port_, &GPIO_InitStruct);
}
void write(const bool v) const { HAL_GPIO_WritePin(port_, pin_, v ? GPIO_PIN_SET : GPIO_PIN_RESET); }
void toggle() const { HAL_GPIO_TogglePin(port_, pin_); }
bool read() const { return HAL_GPIO_ReadPin(port_, pin_); }
private:
GPIO_TypeDef *port_{};
uint16_t pin_{};
void enableClock() const {
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();
}
};
#endif //CLION_STM32_GPIO_H

View File

@@ -0,0 +1,33 @@
//
// Created by Wind on 2025/11/9.
//
#include "interface.h"
#include "led.h"
#ifdef DRIVER_CPP
#include "led_cpp.h"
void cpp_interface() {
const auto led = LED(LED_GPIO_Port, LED_Pin);
while (true) {
led.toggle();
HAL_Delay(50);
}
}
#endif
void interface() {
#ifdef DRIVER_CPP
cpp_interface();
#else
const led_t led = {
LED_GPIO_Port, LED_Pin
};
led_init(&led);
while (true) {
led_toggle(&led);
HAL_Delay(1000);
}
#endif
}

View File

@@ -0,0 +1,17 @@
//
// Created by Wind on 2025/11/9.
//
#ifndef CLION_STM32_CPP_INTERFACE_H
#define CLION_STM32_CPP_INTERFACE_H
#ifdef __cplusplus
extern "C" {
#endif
void interface();
#ifdef __cplusplus
}
#endif
#endif //CLION_STM32_CPP_INTERFACE_H

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();
}