BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
arduino_i2c_keyboard
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
arduino_i2c_keyboard
Commits
76c6fe42
Commit
76c6fe42
authored
Jul 18, 2018
by
Forest Godfrey
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Arduino timers.
parent
93b0aedf
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
196 additions
and
0 deletions
+196
-0
Timer.cpp
arduino_sketch/Timer.cpp
+127
-0
Timer.h
arduino_sketch/Timer.h
+69
-0
No files found.
arduino_sketch/Timer.cpp
0 → 100644
View file @
76c6fe42
/**
*
2018-07-04: <fgodfrey@bigw.org> This file was fored from the .PNG Arduino Framework's master branch
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Timer.h"
Timer
::
Timer
(
unsigned
long
int
ms
){
Create
(
ms
,
NULL
,
false
);
}
Timer
::
Timer
(
unsigned
long
int
ms
,
CallBackType
callback
){
Create
(
ms
,
callback
,
false
);
}
Timer
::
Timer
(
unsigned
long
int
ms
,
CallBackType
callback
,
bool
isSingle
){
Create
(
ms
,
callback
,
isSingle
);
}
void
Timer
::
Create
(
unsigned
long
int
ms
,
CallBackType
callback
,
bool
isSingle
){
setInterval
(
ms
);
setEnabled
(
false
);
setSingleShot
(
isSingle
);
setOnTimer
(
callback
);
LastTime
=
0
;
}
void
Timer
::
setInterval
(
unsigned
long
int
ms
){
msInterval
=
(
ms
>
0
)
?
ms
:
0
;
}
void
Timer
::
setEnabled
(
bool
Enabled
){
blEnabled
=
Enabled
;
}
void
Timer
::
setSingleShot
(
bool
isSingle
){
blSingleShot
=
isSingle
;
}
void
Timer
::
setOnTimer
(
CallBackType
callback
){
onRun
=
callback
;
}
void
Timer
::
Start
(){
LastTime
=
millis
();
setEnabled
(
true
);
}
void
Timer
::
Resume
(){
LastTime
=
millis
()
-
DiffTime
;
setEnabled
(
true
);
}
void
Timer
::
Stop
(){
setEnabled
(
false
);
}
void
Timer
::
Pause
(){
DiffTime
=
millis
()
-
LastTime
;
setEnabled
(
false
);
}
void
Timer
::
Update
(){
if
(
Tick
())
onRun
();
}
bool
Timer
::
Tick
(){
if
(
!
blEnabled
)
return
false
;
if
(
LastTime
>
millis
()
*
2
)
//millis restarted
LastTime
=
0
;
if
((
unsigned
long
int
)(
millis
()
-
LastTime
)
>=
msInterval
)
{
LastTime
=
millis
();
if
(
isSingleShot
())
setEnabled
(
false
);
return
true
;
}
return
false
;
}
unsigned
long
int
Timer
::
getInterval
(){
return
msInterval
;
}
unsigned
long
int
Timer
::
getCurrentTime
(){
return
(
unsigned
long
int
)(
millis
()
-
LastTime
);
}
unsigned
long
int
Timer
::
getRemaining
()
{
if
(
blEnabled
)
{
return
msInterval
-
(
unsigned
long
int
)(
millis
()
-
LastTime
);
}
else
{
return
msInterval
-
(
unsigned
long
int
)(
millis
()
-
(
millis
()
-
DiffTime
));
}
}
CallBackType
Timer
::
getOnTimerCallback
(){
return
onRun
;
}
bool
Timer
::
isEnabled
(){
return
blEnabled
;
}
bool
Timer
::
isSingleShot
(){
return
blSingleShot
;
}
arduino_sketch/Timer.h
0 → 100644
View file @
76c6fe42
/**
*
2018-07-04: <fgodfrey@bigw.org> This file was fored from the .PNG Arduino Framework's master branch
This file is part of .PNG Arduino Framework.
.PNG Arduino Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
.PNG Arduino Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with .PNG Arduino Framework. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdlib.h"
#include "Arduino.h"
#ifndef TIMER_H
#define TIMER_H
typedef
void
(
*
CallBackType
)();
class
Timer
{
private
:
void
Create
(
unsigned
long
int
ms
,
CallBackType
callback
,
bool
isSingle
);
unsigned
long
int
msInterval
;
bool
blEnabled
;
bool
blSingleShot
;
CallBackType
onRun
;
bool
Tick
();
unsigned
long
LastTime
;
unsigned
long
DiffTime
;
//used when I pause the Timer and need to resume
public
:
Timer
(
unsigned
long
int
ms
);
Timer
(
unsigned
long
int
ms
,
CallBackType
callback
);
Timer
(
unsigned
long
int
ms
,
CallBackType
callback
,
bool
isSingle
);
~
Timer
();
void
setInterval
(
unsigned
long
int
ms
);
void
setEnabled
(
bool
Enabled
);
void
setSingleShot
(
bool
isSingle
);
void
setOnTimer
(
CallBackType
callback
);
void
Start
();
void
Resume
();
void
Pause
();
void
Stop
();
void
Update
();
unsigned
long
int
getInterval
();
unsigned
long
int
getCurrentTime
();
unsigned
long
int
getRemaining
();
CallBackType
getOnTimerCallback
();
bool
isEnabled
();
bool
isSingleShot
();
};
#endif // TIMER_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment