Skip to content

Commit aea9f78

Browse files
authored
Merge pull request #4 from natnqweb/ReadmeUpdate
Update README.md
2 parents f627477 + 7c4b57c commit aea9f78

File tree

1 file changed

+110
-82
lines changed

1 file changed

+110
-82
lines changed

README.md

Lines changed: 110 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,147 @@
11

22
# Simpletimer
3+
34
a timer based on micros that will make your coding a lot easier
45

5-
timer()
6+
timer()
67
as simple as that you can perform action every amount of time you feed to function for example
78

8-
Simpletimer timer1;
9+
``` C++
10+
Simpletimer timer1{};
911
timer1.timer(1000)
1012
{
1113
//entry every 1000ms
1214
}
13-
i use it as a tool in my projects so i don't need to repeat myself creating timers
14-
from Version 2.0 up you can register callbacks now only 'void functions(void)'
15+
```
16+
17+
from Version 2.0 up, you can register callbacks now only 'void functions(void)'
1518
1619
# callback example
17-
#include "Simpletimer.h"
18-
// callback function cant take anything and return anything
19-
Simpletimer timer1;
20-
void callback1(){
21-
Serial.println("entry every 1 sec");
22-
}
2320
24-
void setup(){
25-
Serial.begin(9600);
26-
timer1.register_callback(callback1);
27-
}
28-
void loop(){
29-
timer1.call_callback(1000);
21+
``` C++
22+
23+
#include "Simpletimer.h"
24+
25+
Simpletimer timer1{};
26+
27+
// callback function cant take anything and return anything
28+
void callback1() {
29+
Serial.println("entry every 1 sec");
30+
}
31+
32+
void setup() {
33+
Serial.begin(9600);
34+
timer1.register_callback(callback1);
35+
}
36+
37+
void loop() {
38+
timer1.run(1000);
39+
}
40+
41+
```
3042

31-
}
3243
version 2.1
44+
3345
# multiple callbacks example
34-
#include "Simpletimer.h"
35-
// callback function cant take anything and return anything
36-
#define ledpin 8
37-
38-
Simpletimer multicb;
39-
40-
void callback1();
41-
void callback2();
42-
void callback3();
43-
void blink();
44-
void callback1(){
45-
Serial.println(F("entry every 1 sec"));
46-
}
47-
void callback2(){
48-
Serial.println(F("entry every 0.5 sec"));
49-
}
50-
void callback3(){
51-
Serial.println(F("entry every 0.2 sec"));
52-
}
53-
volatile bool state=false;
54-
void blink(){
55-
state=!state;
56-
}
57-
void setup(){
58-
pinMode(ledpin,OUTPUT);
59-
size_t number_of_callbacks=4;
60-
Simpletimer::callback all_callbacks[4]={callback1,callback2,callback3,blink};
61-
unsigned long timers[4]={1000,500,200,199};
6246

63-
Serial.begin(115200);
64-
multicb.register_multiple_callbacks(all_callbacks,timers,number_of_callbacks);
65-
Serial.println(F("program started"));
47+
``` C++
6648

67-
}
68-
void loop(){
49+
#include <Simpletimer.h>
50+
// callback function cant take anything and return anything
51+
#define ledpin LED_BUILTIN
6952

53+
Simpletimer multicb{};
54+
bool state = false;
7055

56+
void callback1();
57+
void callback2();
58+
void callback3();
59+
void blink();
7160

72-
multicb.run();
73-
digitalWrite(ledpin,state);
61+
void callback1() {
62+
Serial.println(F("entry every 1 sec"));
63+
}
7464

65+
void callback2() {
66+
Serial.println(F("entry every 0.5 sec"));
67+
}
7568

76-
}
77-
## perform tasks only once in loop
78-
#include <Simpletimer.h>
79-
// callback function cant take anything and return anything
80-
81-
Simpletimer::RunOnce Once;
82-
int i=1;
83-
void callback1(){
84-
Serial.println(F("entry once"));
85-
i++;
86-
}
69+
void callback3() {
70+
Serial.println(F("entry every 0.2 sec"));
71+
}
8772

88-
void callback2(){
89-
Serial.println(F("other method"));
90-
i++;
91-
Serial.println("number of tasks performed : "+String(i));
92-
}
73+
void blink() {
74+
state = !state;
75+
}
9376

94-
void setup(){
77+
static const unsigned int number_of_callbacks = 4;
78+
static Simpletimer::callback all_callbacks[number_of_callbacks]
79+
{
80+
callback1, //-- 1
81+
callback2, //-- 2
82+
callback3, //-- 3
83+
blink //-- 4
84+
};
85+
static unsigned long timers[number_of_callbacks]
86+
{
87+
1000, //callback1 -- 1
88+
500, //callback2 -- 2
89+
200, //callback3 -- 3
90+
2000 // blink -- 4
91+
};
9592

96-
Serial.begin(115200);
93+
void setup() {
94+
pinMode(ledpin, OUTPUT);
9795

96+
Serial.begin(115200);
9897

98+
multicb.register_multiple_callbacks(all_callbacks, timers, number_of_callbacks);
9999

100-
}
101-
void loop(){
102-
//static Simpletimer::RunOnce DoSomething([&](){ here goes your code that need to run once});
103-
// perform task once in loop
104-
static Simpletimer::RunOnce DoSomething;
105-
DoSomething.Run([&](){callback1(); Serial.println(F("you can place here anything "));});
106-
// reset previous task in anotherone and then it can perform second time
107-
static Simpletimer::RunOnce reset_previous_task;
108-
reset_previous_task.Run([&](){DoSomething.Reset(); Serial.println("task 1 RESET being called now");});
109-
//another way to perform task once you can always reset calling Once.Reset();
110-
Once.Run(callback2);
100+
Serial.println(F("program started"));
101+
}
102+
void loop() {
103+
multicb.run();
104+
digitalWrite(ledpin, state);
105+
}
111106

107+
```
112108

109+
## perform tasks only once in loop
113110

111+
``` C++
114112

113+
#include "Simpletimer.h"
114+
// callback function cant take anything and return anything
115115

116+
Simpletimer::RunOnce Once{};
116117

118+
int i = 1;
117119

118-
}
120+
void callback1() {
121+
Serial.println(F("entry once"));
122+
i++;
123+
}
124+
125+
void callback2() {
126+
Serial.println(F("other method"));
127+
i++;
128+
Serial.println("number of tasks performed : " + String(i));
129+
}
130+
131+
void setup() {
132+
Serial.begin(115200);
133+
}
134+
135+
void loop() {
136+
//static Simpletimer::RunOnce DoSomething([&](){ here goes your code that need to run once});
137+
// perform task once in loop
138+
static Simpletimer::RunOnce DoSomething;
139+
DoSomething.Run([&]() {callback1(); Serial.println(F("you can place here anything "));});
140+
// reset previous task in anotherone and then it can perform second time
141+
static Simpletimer::RunOnce reset_previous_task;
142+
reset_previous_task.Run([&]() {DoSomething.Reset(); Serial.println("task 1 RESET being called now");});
143+
//another way to perform task once you can always reset calling Once.Reset();
144+
Once.Run(callback2);
145+
}
119146

147+
```

0 commit comments

Comments
 (0)