Skip to content

Commit a0a7aee

Browse files
author
埃博拉酱
committed
将函数指针非模板化以便支持λ表达式;支持计时器的暂停和继续
1 parent 1af375b commit a0a7aee

File tree

13 files changed

+252
-215
lines changed

13 files changed

+252
-215
lines changed

.vscode/c_cpp_properties.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"includePath": [
66
"${workspaceFolder}",
77
"${USERPROFILE}/Documents/Arduino/libraries/**",
8-
"${LOCALAPPDATA}/Arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino",
8+
"${LOCALAPPDATA}/Arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino",
99
"${LOCALAPPDATA}/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include",
10-
"${LOCALAPPDATA}/Arduino15/packages/arduino/hardware/avr/1.8.5/variants/**"
10+
"${LOCALAPPDATA}/Arduino15/packages/arduino/hardware/avr/1.8.6/variants/**"
1111
],
1212
"forcedInclude": [
1313
"Arduino.h"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
如果你使用的开发板并非ATMega2560系列的CPU,也可能可以使用本库。具体需要参考你的CPU数据表,看是否具有兼容的硬件计时器。
2323

24+
# 常见故障排除
25+
26+
如果链接时遇到`undefined reference to TIMSK`类似的错误,请检查你选择的开发板是否正确,以及是否使用了你的开发板所不支持的计时器。
27+
2428
Make full use of all your hardware timers on your Arduino board.
2529

2630
The only library you can choose any hardware timer you like to use in your timing function. Tones, square waves, delayed tasks, timed repetitive tasks are provided as building blocks for your own sophisticated multi-timer tasks. My library hides hardware register details for you.
@@ -43,6 +47,11 @@ This timer is also 8-bit, but different from timer 0 at 3 aspects:
4347
Moreover, builtin `analogWrite()` and `tone()` may dynamically occupy any of the timers listed above. You'll have to handle potential conflicts if you want to use these builtins.
4448

4549
If you aren't using ATMega2560 CPU series, you may or may not be able to use this library. Refer to your datasheet to see if it has compatible hardware timer configurations.
50+
51+
# Common troubleshooting
52+
53+
If you encounter an error similar to `undefined reference to TIMSK` when linking, please check whether you have selected the correct board and whether you are using a timer that is not supported by your board.
54+
4655
# API参考 API Reference
4756
```C++
4857
//在指定的毫秒数后触发一个计时器中断,调用你的函数。

examples/LedAndBuzzerShow/LedAndBuzzerShow.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
using namespace TimersOneForAll;
44
constexpr uint8_t LED = 7;
55
constexpr uint8_t Buzzer = 8;
6+
void LightDown()
7+
{
8+
digitalWrite(LED, LOW);
9+
}
610
void setup()
711
{
812
pinMode(7, OUTPUT);
913
pinMode(8, OUTPUT);
1014
Serial.begin(9600);
1115
digitalWrite(LED, HIGH);
1216
//设置计时器3在5秒后熄灭LED灯,但不阻断程序
13-
DoAfter<3, 5000, LightDown>();
17+
DoAfter<3, 5000>(LightDown);
1418

1519
//设置4号计时器,每隔2秒,就用5号计时器生成2000㎐脉冲1秒,重复3次
16-
RepeatAfter<4, 2000, PlayTone<5, Buzzer, 2000, 1000>, 3>();
20+
RepeatAfter<4, 2000, 3>(PlayTone<5, Buzzer, 2000, 1000>);
1721
//设置计时器1,将程序阻断7秒
1822
Delay<1, 7000>();
1923
//设置计时器1,将LED灯先亮2秒,再熄灭1秒,无限循环
2024
SquareWave<1, LED, 2000, 1000>();
2125
//设置计时器3在8秒后停止计时器1
22-
DoAfter<3, 8000, ShutDown<1>>();
26+
DoAfter<3, 8000>(ShutDown<1>);
2327
//观察到,LED灯明暗循环两次后,最终停在了亮状态,因为1号计时器尚未触发暗事件就被停止了
2428
//亮1秒暗2秒的方波,重复5个循环
2529
Delay<4, 8000>();
2630
SquareWave<1, LED, 1000, 2000, 5>();
2731
}
28-
void LightDown()
29-
{
30-
digitalWrite(LED, LOW);
31-
}
3232
void loop()
3333
{
3434
}

src/Internal/Debugger.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Internal/Delay.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ namespace TimersOneForAll
1212
Delaying<TimerCode> = false;
1313
}
1414
}
15-
//阻塞当前代码执行指定毫秒数
15+
// 阻塞当前代码执行指定毫秒数
1616
template <uint8_t TimerCode, uint16_t DelayMilliseconds>
1717
void Delay()
1818
{
19-
DoAfter<TimerCode, DelayMilliseconds, Internal::Undelay<TimerCode>>();
19+
DoAfter<TimerCode, DelayMilliseconds>(Internal::Undelay<TimerCode>);
2020
Internal::Delaying<TimerCode> = true;
2121
while (Internal::Delaying<TimerCode>)
2222
;
2323
}
24-
//阻塞当前代码执行指定毫秒数
24+
// 阻塞当前代码执行指定毫秒数
2525
template <uint8_t TimerCode>
2626
void Delay(uint16_t DelayMilliseconds)
2727
{
28-
DoAfter<TimerCode, Internal::Undelay<TimerCode>>(DelayMilliseconds);
28+
DoAfter<TimerCode>(DelayMilliseconds, Internal::Undelay<TimerCode>);
2929
Internal::Delaying<TimerCode> = true;
3030
while (Internal::Delaying<TimerCode>)
3131
;

src/Internal/DoAfter.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
#include "Kernel.h"
33
namespace TimersOneForAll
44
{
5-
//在指定毫秒数后执行任务
6-
template <uint8_t TimerCode, uint16_t AfterMilliseconds, void (*DoTask)()>
7-
void DoAfter()
5+
// 在指定毫秒数后执行任务
6+
template <uint8_t TimerCode, uint16_t AfterMilliseconds>
7+
void DoAfter(void (*DoTask)())
88
{
99
constexpr Internal::TimerSetting TS = Internal::GetTimerSetting(TimerCode, AfterMilliseconds);
10-
Internal::SLRepeaterSet<TimerCode, TS.TCNT, TS.PrescalerBits, DoTask, 1, nullptr>();
10+
Internal::TimerTask<TimerCode> = DoTask;
11+
Internal::SLRepeaterSet<TimerCode, TS.TCNT, TS.PrescalerBits, 1, nullptr>();
1112
}
12-
//在指定毫秒数后执行任务
13-
template <uint8_t TimerCode, void (*DoTask)()>
14-
void DoAfter(uint16_t AfterMilliseconds)
13+
// 在指定毫秒数后执行任务
14+
template <uint8_t TimerCode>
15+
void DoAfter(uint16_t AfterMilliseconds, void (*DoTask)())
1516
{
1617
Internal::TimerSetting TS = Internal::GetTimerSetting<TimerCode>(AfterMilliseconds);
17-
Internal::SLRepeaterSet<TimerCode, DoTask, 1, nullptr>(TS.TCNT, TS.PrescalerBits);
18+
Internal::TimerTask<TimerCode> = DoTask;
19+
Internal::SLRepeaterSet<TimerCode, 1, nullptr>(TS.TCNT, TS.PrescalerBits);
1820
}
1921
}

0 commit comments

Comments
 (0)