Skip to content

Commit 3804b80

Browse files
committed
add time example file
1 parent 1e87e6c commit 3804b80

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mel_example(comms_server)
2020
mel_example(virtual_daq)
2121
mel_example(serial)
2222
mel_example(csv)
23-
# mel_example(time)
23+
mel_example(time)
2424

2525
# windows only examples
2626
if(WIN32)

examples/ex_time.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// MIT License
2+
//
3+
// MEL - Mechatronics Engine & Library
4+
// Copyright (c) 2019 Mechatronics and Haptic Interfaces Lab - Rice University
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// Author(s): Evan Pezent ([email protected])
17+
18+
#include <MEL/Core/Time.hpp>
19+
#include <MEL/Core/Clock.hpp>
20+
#include <MEL/Core/Timer.hpp>
21+
#include <MEL/Core/Console.hpp> // for print
22+
#include <MEL/Utility/System.hpp> // for sleep
23+
24+
using namespace mel;
25+
26+
int main(int argc, char const *argv[])
27+
{
28+
29+
// Time
30+
31+
Time t1 = seconds(10);
32+
Time t2 = milliseconds(10);
33+
Time t3 = t1 + t2;
34+
if (t1 > t2)
35+
print(t3);
36+
37+
double t1_s = t1.as_seconds();
38+
int32 t2_ms = t2.as_milliseconds();
39+
int64 t3_us = t3.as_microseconds();
40+
41+
// Frequency
42+
43+
Frequency f1 = hertz(1000);
44+
Frequency f2 = t2.to_frequency();
45+
print(f1,f2);
46+
47+
// Clock
48+
49+
Clock clock;
50+
sleep(seconds(1));
51+
Time time1 = clock.get_elapsed_time();
52+
sleep(seconds(1));
53+
Time time2 = clock.restart();
54+
sleep(seconds(1));
55+
Time time3 = clock.get_elapsed_time();
56+
print(time1, time2, time3);
57+
58+
// Timer
59+
60+
Timer timer1(seconds(1));
61+
time1 = timer1.wait();
62+
time2 = timer1.wait();
63+
time3 = timer1.wait();
64+
print(time1, time2, time3);
65+
66+
Timer hybrid_timer(milliseconds(10), Timer::WaitMode::Hybrid);
67+
print(hybrid_timer.wait());
68+
69+
// Time loops
70+
71+
Time t = Time::Zero;
72+
Timer timer(hertz(1000));
73+
while (t < seconds(60)) {
74+
// code which executes in less than one millisecond
75+
t = timer.wait();
76+
}
77+
78+
79+
80+
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)