Can I retarget the chrono class to use microcontroller as tick generator?

siii fsxa

I want to use the timer in my microcontroller as tick generator of the chrono class. imagine that I have function that gets the tick from timer register in the MCU and I call it get_tcik() and this register is 32 bit unsigned integer which increments each 1 microsecond. Now to use it with chrono I've created a class like this:

class clock
{
public:
  static std::chrono::time_point<clock, std::chrono::duration<std::uint32_t, std::ratio<1, 1000000> now() {
    return std::chrono::time_point<clock, std::chrono::duration<std::uint32_t, std::ratio<1, 1000000> {std::chrono::duration<uint32_t, std::ratio<1, 1000000>> { get_tick() }};
}
};

And it doesn't work!

Howard Hinnant

It's not working because the chrono library detects suboptimal code formatting. :-)

Just kidding.

You're missing some nested types for clock that client code might expect to be there. Also you can use std::micro in place of std::ratio<1, 1000000>. Your choice isn't wrong. This is just a stylistic suggestion.

#include <chrono>

class clock
{
public:
    using rep        = std::uint32_t;
    using period     = std::micro;
    using duration   = std::chrono::duration<rep, period>;
    using time_point = std::chrono::time_point<clock>;
    static constexpr bool is_steady = true;

    static time_point now() {
        return time_point{duration { get_tick() }};
    }
};

Here's the official requirements: http://eel.is/c++draft/time.clock.req

Be advised that your clock will roll-over about every hour and 11 minutes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I retarget the chrono class to use microcontroller as tick generator?

From Dev

Why I can't use a method class as a generator?

From Dev

How can I write a generator in a JavaScript class?

From Dev

How can I use std::chrono::duration as a template parameter?

From Dev

App Generator: How can I declare a variable for use in the entire generator?

From Dev

How can I a catch an unexpectedly reset in a microcontroller

From Dev

Can't retarget project in VS2015

From Dev

Can I use __autoload in class?

From Dev

Why can't I use the gtest ValuesIn generator with a std::map?

From Dev

How can I force use generator on Coffeescript 1.9?

From Dev

How can I force use generator on Coffeescript 1.9?

From Dev

Can I use generator-function with arrow function?

From Dev

How can I use a passcode generator for authentication for remote logins?

From Dev

Can a method within a class be generator?

From Dev

Can a method within a class be generator?

From Dev

How can i use external class in Codeception?

From Dev

Why can't I use ConditionalAttribute on a class?

From Dev

How can I use `class` as a keyword argument?

From Dev

Can I use a class as a variable in .NET

From Dev

Can i use only _ (underscore) for the name of the class?

From Dev

Can I use an enum in a constructor for a class?

From Dev

How can I use HttpContext in public class

From Dev

Can I use class helpers for TWebActionItem or TWebRequest?

From Dev

Can I use a type in a forward declared class?

From Dev

Can I use a class as a variable in .NET

From Dev

Can I use FtpWebRequest class for SFTP transfers

From Dev

can I use setInterval in the definition of a javascript "class"?

From Dev

can I use invoke to abstract class method

From Dev

Can I use same mapedBy name in a class?

Related Related

  1. 1

    Can I retarget the chrono class to use microcontroller as tick generator?

  2. 2

    Why I can't use a method class as a generator?

  3. 3

    How can I write a generator in a JavaScript class?

  4. 4

    How can I use std::chrono::duration as a template parameter?

  5. 5

    App Generator: How can I declare a variable for use in the entire generator?

  6. 6

    How can I a catch an unexpectedly reset in a microcontroller

  7. 7

    Can't retarget project in VS2015

  8. 8

    Can I use __autoload in class?

  9. 9

    Why can't I use the gtest ValuesIn generator with a std::map?

  10. 10

    How can I force use generator on Coffeescript 1.9?

  11. 11

    How can I force use generator on Coffeescript 1.9?

  12. 12

    Can I use generator-function with arrow function?

  13. 13

    How can I use a passcode generator for authentication for remote logins?

  14. 14

    Can a method within a class be generator?

  15. 15

    Can a method within a class be generator?

  16. 16

    How can i use external class in Codeception?

  17. 17

    Why can't I use ConditionalAttribute on a class?

  18. 18

    How can I use `class` as a keyword argument?

  19. 19

    Can I use a class as a variable in .NET

  20. 20

    Can i use only _ (underscore) for the name of the class?

  21. 21

    Can I use an enum in a constructor for a class?

  22. 22

    How can I use HttpContext in public class

  23. 23

    Can I use class helpers for TWebActionItem or TWebRequest?

  24. 24

    Can I use a type in a forward declared class?

  25. 25

    Can I use a class as a variable in .NET

  26. 26

    Can I use FtpWebRequest class for SFTP transfers

  27. 27

    can I use setInterval in the definition of a javascript "class"?

  28. 28

    can I use invoke to abstract class method

  29. 29

    Can I use same mapedBy name in a class?

HotTag

Archive