FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
time.cpp
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "boards/board.h"
16 #include "time.h"
17 #include "power.h"
18 
21 
23 {
25 }
26 
27 time::RTTTime time::delta(const RTTTime& time1, const RTTTime& time2)
28 {
29  uint32_t millis = ((time1.millis() <= time2.millis()) ? (time2.millis() - time1.millis()) : 0);
30  uint16_t micros = 0;
31  if (time1.micros() <= time2.micros())
32  micros = time2.micros() - time1.micros();
33  else if (millis)
34  {
35  --millis;
36  micros = ONE_MILLI_16 + time2.micros() - time1.micros();
37  }
38  return RTTTime{millis, micros};
39 }
40 
41 uint32_t time::since(uint32_t start_ms)
42 {
43  uint32_t now = time::millis();
44  return ((start_ms <= now) ? (now - start_ms) : 0);
45 }
46 
47 void time::default_delay(uint32_t ms)
48 {
49  while (ms--) time::delay_us(ONE_MILLI_16);
50 }
power::Power::sleep
static void sleep()
Enter power sleep mode as defined by Power::set_default_mode().
Definition: power.h:69
time::MILLIS_PTR
uint32_t(*)() MILLIS_PTR
Function pointer type used for time::millis global variable.
Definition: time.h:305
time::RTTTime::millis
uint32_t millis() const
Number of elapsed milliseconds.
Definition: time.h:125
time::RTTTime::micros
uint16_t micros() const
Number of elapsed microseconds (0..999).
Definition: time.h:131
time::delay
DELAY_PTR delay
Delay program execution for the given amount of milliseconds.
Definition: time.cpp:19
power.h
Simple power support for AVR MCU.
time::DELAY_PTR
void(*)(uint32_t ms) DELAY_PTR
Function pointer type used for time::delay global variable.
Definition: time.h:285
time::delta
RTTTime delta(const RTTTime &time1, const RTTTime &time2)
Compute the time delta from time1 and time2.
Definition: time.cpp:27
time::yield
void yield()
Utility method used by many FastArduino API in order to "yield" some processor time; concretely it ju...
Definition: time.cpp:22
time::delay_us
void delay_us(uint16_t us) INLINE
Delay program execution for the given amount of microseconds.
Definition: time.h:334
time::since
uint32_t since(uint32_t start_ms)
Compute the time elapsed, in milliseconds, since start_ms.
Definition: time.cpp:41
time::millis
MILLIS_PTR millis
Count number of milliseconds elapsed since some time base reference (generally since MCU startup).
Definition: time.cpp:20
time.h
Simple time utilities.
time::RTTTime
Structure used to hold a time value with microsecond precision.
Definition: time.h:50
time::default_delay
void default_delay(uint32_t ms)
Delay program execution for the given amount of milliseconds.
Definition: time.cpp:47