Skip to content

Commit 5a4fd20

Browse files
committed
refact(ArduinoAPI/Tone): add ToneContext & update code style
1 parent 7024353 commit 5a4fd20

File tree

2 files changed

+116
-77
lines changed

2 files changed

+116
-77
lines changed

Keilduino/ArduinoAPI/Tone.cpp

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* MIT License
3-
* Copyright (c) 2017 - 2022 _VIFEXTech
3+
* Copyright (c) 2017 - 2025 _VIFEXTech
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -20,109 +20,118 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
* SOFTWARE.
2222
*/
23+
24+
/*********************
25+
* INCLUDES
26+
*********************/
27+
2328
#include "Tone.h"
2429
#include "Arduino.h"
2530

31+
/*********************
32+
* DEFINES
33+
*********************/
34+
2635
#define TONE_FREQ_MAX 500000U
2736

28-
static TIM_TypeDef* Tone_Timer = NULL;
29-
static bool Tone_IsContinuousModeEnable = false;
30-
static uint8_t Tone_Pin = NOT_A_PIN;
31-
static uint32_t Tone_ToggleCounter = 0;
32-
33-
/**
34-
* @brief tone中断入口,被定时中断调用
35-
* @param 无
36-
* @retval 无
37-
*/
38-
static void Tone_TimerHandler()
39-
{
40-
if(!Tone_IsContinuousModeEnable)
41-
{
42-
if(Tone_ToggleCounter == 0)
43-
{
44-
noTone(Tone_Pin);
45-
return;
46-
}
37+
/**********************
38+
* TYPEDEFS
39+
**********************/
4740

48-
Tone_ToggleCounter--;
49-
}
41+
typedef struct {
42+
TIM_TypeDef* timer;
43+
uint32_t toggleCount;
44+
bool isContinuous;
45+
uint8_t pin;
46+
} ToneContext_t;
5047

51-
togglePin(Tone_Pin);
52-
}
48+
/**********************
49+
* STATIC PROTOTYPES
50+
**********************/
5351

54-
/**
55-
* @brief 改变tone所使用的定时器
56-
* @param TIMx: 定时器地址
57-
* @retval 无
58-
*/
59-
void toneSetTimer(TIM_TypeDef* TIMx)
60-
{
61-
Timer_SetInterruptBase(
62-
TIMx,
63-
0xFF,
64-
0xFF,
65-
Tone_TimerHandler,
66-
TONE_PREEMPTIONPRIORITY_DEFAULT,
67-
TONE_SUBPRIORITY_DEFAULT
68-
);
69-
Tone_Timer = TIMx;
70-
}
52+
static void toneTimerHandler();
53+
54+
/**********************
55+
* STATIC VARIABLES
56+
**********************/
57+
58+
static ToneContext_t ToneContext = { NULL, 0, false, NOT_A_PIN };
59+
60+
/**********************
61+
* MACROS
62+
**********************/
7163

72-
/**
73-
* @brief 在Pin上生成指定频率 (50%占空比的方波)
74-
* @param pin: 产生音调的 Pin
75-
* @param freq: 频率(Hz)
76-
* @param duration: 音调的持续时间 (以毫秒为单位)
77-
* @retval 无
78-
*/
7964
void tone(uint8_t pin, uint32_t freq, uint32_t duration)
8065
{
66+
if (!IS_PIN(pin)) {
67+
return;
68+
}
69+
8170
noTone(pin);
8271

83-
if(duration == 0 || freq == 0 || freq > TONE_FREQ_MAX)
84-
{
72+
if (duration == 0 || freq == 0 || freq > TONE_FREQ_MAX) {
8573
return;
8674
}
8775

88-
Tone_Pin = pin;
89-
Tone_IsContinuousModeEnable = (duration == TONE_DURATION_INFINITE) ? true : false;
76+
ToneContext.pin = pin;
77+
ToneContext.isContinuous = (duration == TONE_DURATION_INFINITE) ? true : false;
9078

91-
if(!Tone_IsContinuousModeEnable)
92-
{
93-
Tone_ToggleCounter = freq * duration / 1000 * 2;
79+
if (!ToneContext.isContinuous) {
80+
ToneContext.toggleCount = freq * duration / 1000 * 2;
9481

95-
if(Tone_ToggleCounter == 0)
96-
{
82+
if (ToneContext.toggleCount == 0) {
9783
return;
9884
}
9985

100-
Tone_ToggleCounter--;
86+
ToneContext.toggleCount--;
10187
}
10288

103-
if(Tone_Timer == NULL)
104-
{
89+
if (ToneContext.timer == NULL) {
10590
toneSetTimer(TONE_TIMER_DEFAULT);
10691
}
10792

108-
Timer_SetInterruptTimeUpdate(Tone_Timer, TONE_FREQ_MAX / freq);
109-
Timer_SetEnable(Tone_Timer, true);
93+
Timer_SetInterruptTimeUpdate(ToneContext.timer, TONE_FREQ_MAX / freq);
94+
Timer_SetEnable(ToneContext.timer, true);
11095
}
11196

112-
/**
113-
* @brief 关闭声音
114-
* @param Pin: 产生音调的引脚编号
115-
* @retval 无
116-
*/
11797
void noTone(uint8_t pin)
11898
{
119-
if(Tone_Timer)
120-
{
121-
Timer_SetEnable(Tone_Timer, false);
99+
if (ToneContext.timer) {
100+
Timer_SetEnable(ToneContext.timer, false);
122101
}
123102

124103
digitalWrite(pin, LOW);
125-
Tone_IsContinuousModeEnable = false;
126-
Tone_Pin = NOT_A_PIN;
127-
Tone_ToggleCounter = 0;
104+
ToneContext.isContinuous = false;
105+
ToneContext.pin = NOT_A_PIN;
106+
ToneContext.toggleCount = 0;
107+
}
108+
109+
void toneSetTimer(TIM_TypeDef* timer)
110+
{
111+
Timer_SetInterruptBase(
112+
timer,
113+
0xFF,
114+
0xFF,
115+
toneTimerHandler,
116+
TONE_PREEMPTIONPRIORITY_DEFAULT,
117+
TONE_SUBPRIORITY_DEFAULT);
118+
ToneContext.timer = timer;
119+
}
120+
121+
/**********************
122+
* STATIC FUNCTIONS
123+
**********************/
124+
125+
static void toneTimerHandler()
126+
{
127+
if (!ToneContext.isContinuous) {
128+
if (ToneContext.toggleCount == 0) {
129+
noTone(ToneContext.pin);
130+
return;
131+
}
132+
133+
ToneContext.toggleCount--;
134+
}
135+
136+
togglePin(ToneContext.pin);
128137
}

Keilduino/ArduinoAPI/Tone.h

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* MIT License
3-
* Copyright (c) 2017 - 2022 _VIFEXTech
3+
* Copyright (c) 2017 - 2025 _VIFEXTech
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -20,15 +20,45 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
* SOFTWARE.
2222
*/
23-
#ifndef __TONE_H
24-
#define __TONE_H
23+
#ifndef TONE_H
24+
#define TONE_H
25+
26+
/*********************
27+
* INCLUDES
28+
*********************/
2529

2630
#include "timer.h"
2731

32+
/*********************
33+
* DEFINES
34+
*********************/
35+
2836
#define TONE_DURATION_INFINITE 0xFFFFFFFFU
2937

30-
void toneSetTimer(TIM_TypeDef* TIMx);
38+
/**********************
39+
* GLOBAL PROTOTYPES
40+
**********************/
41+
42+
/**
43+
* @brief Generate a square wave on the specified pin at the specified frequency.
44+
* @param pin The pin on which to generate the square wave.
45+
* @param freq The frequency of the square wave in Hz.
46+
* @param duration The duration of the tone in milliseconds.
47+
* @note If duration is set to TONE_DURATION_INFINITE, the tone will continue until noTone() is called.
48+
*/
3149
void tone(uint8_t pin, uint32_t freq, uint32_t duration = TONE_DURATION_INFINITE);
50+
51+
/**
52+
* @brief Stop generating the tone on the specified pin.
53+
* @param pin The pin on which the tone is being generated.
54+
*/
3255
void noTone(uint8_t pin);
3356

57+
/**
58+
* @brief Set the timer used for generating tones.
59+
* @param timer The timer to be used for tone generation.
60+
* @note This function should be called before using the tone() function.
61+
*/
62+
void toneSetTimer(TIM_TypeDef* timer);
63+
3464
#endif

0 commit comments

Comments
 (0)