Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Module.h (Module.cpp is empty)
- #ifndef STM32_HAL_MODULE_H
- #define STM32_HAL_MODULE_H
- #include "main.h"
- extern "C"
- {
- #define GENERAL_INIT \
- if (init == nullptr) \
- { \
- initTemplate.Mode = GPIO_MODE_OUTPUT_PP; \
- initTemplate.Pull = GPIO_PULLDOWN; \
- initTemplate.Speed = GPIO_SPEED_FREQ_LOW; \
- } \
- else \
- { \
- initTemplate.Mode = init->Mode; \
- initTemplate.Pull = init->Pull; \
- initTemplate.Speed = init->Speed; \
- }
- class Module
- {
- public:
- GPIO_TypeDef *gpio;
- uint16_t pin;
- Module(GPIO_TypeDef *gpio, uint16_t pinAt) : gpio(gpio), pin(pinAt) {};
- };
- }
- #endif
- // Board.h
- #ifndef STM32_HAL_BOARD_H
- #define STM32_HAL_BOARD_H
- #include "Led.h"
- #include "Button.h"
- extern "C"
- {
- class Board
- {
- public:
- Led led0;
- Led led1;
- Led buzzer0;
- Button button0;
- Button extiButton0;
- Board();
- static Board *mainBoard;
- [[noreturn]] void run();
- };
- }
- #endif //STM32_HAL_BOARD_H
- // Board.cpp
- #include "Board.h"
- Board *Board::mainBoard = new Board();
- Board::Board()
- {
- Led::initGpio(nullptr);
- Button::initGpio(nullptr);
- led0 = Led(GPIO_PIN_0);
- led1 = Led(GPIO_PIN_1);
- buzzer0 = Led(GPIO_PIN_3);
- button0 = Button(GPIO_PIN_2);
- extiButton0 = Button(GPIO_PIN_15);
- }
- [[noreturn]] void Board::run()
- {
- while (true)
- {
- if (button0.isPressed())
- {
- led0.on();
- led1.off();
- buzzer0.toggle();
- HAL_Delay(10);
- }
- else
- {
- led0.off();
- led1.on();
- buzzer0.off();
- }
- if (extiButton0.isPressed())
- {
- led1.toggle();
- HAL_Delay(100);
- }
- }
- }
- // Led.h
- #ifndef STM32_HAL_LED_H
- #define STM32_HAL_LED_H
- extern "C"
- {
- #include "Module.h"
- class Led : public Module
- {
- public:
- Led();
- explicit Led(uint16_t pin);
- void on() const;
- void off() const;
- void toggle() const;
- static GPIO_InitTypeDef initTemplate;
- static void initGpio(GPIO_InitTypeDef *init);
- };
- }
- #endif
- // Led.cpp
- #include "Led.h"
- GPIO_InitTypeDef Led::initTemplate;
- Led::Led() : Module(GPIOA, GPIO_PIN_0) {}
- Led::Led(uint16_t pin) : Module(GPIOA, pin)
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_InitTypeDef init;
- init.Mode = initTemplate.Mode;
- init.Pull = initTemplate.Pull;
- init.Speed = initTemplate.Speed;
- init.Pin = pin;
- HAL_GPIO_Init(GPIOA, &init);
- }
- void Led::initGpio(GPIO_InitTypeDef *init = nullptr)
- {
- GENERAL_INIT
- }
- void Led::on() const
- {
- HAL_GPIO_WritePin(GPIOA, pin, GPIO_PIN_RESET);
- }
- void Led::off() const
- {
- HAL_GPIO_WritePin(GPIOA, pin, GPIO_PIN_SET);
- }
- void Led::toggle() const
- {
- HAL_GPIO_TogglePin(GPIOA, pin);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement