<Click above Microcontroller and Microcontroller Development Community Follow this public account:)>Introduction:We will adopt the same layered design approach used previously for the button module and display module to design the buzzer module. Similarly, performing layered design on the buzzer module is a good practice that helps provide maintainability and reusability of the code.Below, we will introduce how to modularize the buzzer module into three layers: the driver layer, middleware layer, and application layer using C language.Layer Diagram:The layer diagram of the buzzer module and the interfaces of each layer can be represented as:
Driver Layer:The driver layer is closely related to the hardware. It is responsible for directly operating the hardware, including pin configuration, PWM control, and other low-level operations. Only the driver layer interface functions are provided here. Its implementation depends on different MCU platforms.For the middleware layer, the driver layer abstracts the differences of specific hardware platforms.
void buzzer_init(void);void buzzer_set_freq_duty(uint32_t freq_hz,uint8_t duty_percent);void buzzer_on(void);void buzzer_off(void);
Middleware Layer:The middleware layer acts as a bridge between the driver layer and the application layer, encapsulating various sound modes of the buzzer and providing higher-level interfaces to the application layer.
/*buzzer_middleware.h*//*Predefined sound modes*/typedef enum{BUZZER_MODE_START,BUZZER_MODE_SUCCESS,BUZZER_MODE_ERROR,BUZZER_NOTIFICATION,BUZZER_MODE_CUSTOM,}BuzzerMode_t;/*Initialize middleware layer*/void buzzer_middleware_init(void);void buzzer_play_mode(BuzzerMode_t mode);void buzzer_play_custom(uint32_t freq_hz, uint32_t duration, uint8_t repeat);void buzzer_stop(void);
Application Layer:The application layer uses the interfaces provided by the middleware layer to implement business logic that is strongly related to the application.
/*buzzer_application.h*/#ifndef BUZZER_APPLICATION_Hvoid buzzer_app_init(void);void buzzer_alarm(void);void buzzer_play_welcome(void);#endif
Conclusion: This article introduced how to perform hierarchical software design for the buzzer module.This layered design makes the code structure clear, easy to maintain, and extend.Related Links:From Scratch: Modular Design of Embedded Software and Practical UML – 1. IntroductionFrom Scratch: Modular Design of Embedded Software and Practical UML – 2. Requirement AnalysisFrom Scratch: Modular Design of Embedded Software and Practical UML – 3. Object-Oriented AnalysisFrom Scratch: Modular Design of Embedded Software and Practical UML – 4. Software Layers and Directory StructureFrom Scratch: Modular Design of Embedded Software and Practical UML – 5. Button Modular DesignFrom Scratch: Modular Design of Embedded Software and Practical UML – 6. Display Modular Design