My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Clock.h 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include <chrono>
  24. #include <thread>
  25. class Clock {
  26. public:
  27. static uint64_t ticks(uint32_t frequency = Clock::frequency) {
  28. return (Clock::nanos() - Clock::startup.count()) / (1000000000ULL / frequency);
  29. }
  30. static uint64_t nanosToTicks(uint64_t ns, uint32_t frequency = Clock::frequency) {
  31. return ns / (1000000000ULL / frequency);
  32. }
  33. // Time acceleration compensated
  34. static uint64_t ticksToNanos(uint64_t tick, uint32_t frequency = Clock::frequency) {
  35. return (tick * (1000000000ULL / frequency)) / Clock::time_multiplier;
  36. }
  37. static void setFrequency(uint32_t freq) {
  38. Clock::frequency = freq;
  39. }
  40. // Time Acceleration compensated
  41. static uint64_t nanos() {
  42. auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
  43. return (now.count() - Clock::startup.count()) * Clock::time_multiplier;
  44. }
  45. static uint64_t micros() {
  46. return Clock::nanos() / 1000;
  47. }
  48. static uint64_t millis() {
  49. return Clock::micros() / 1000;
  50. }
  51. static double seconds() {
  52. return Clock::nanos() / 1000000000.0;
  53. }
  54. static void delayCycles(uint64_t cycles) {
  55. std::this_thread::sleep_for(std::chrono::nanoseconds( (1000000000L / frequency) * cycles) / Clock::time_multiplier );
  56. }
  57. static void delayMicros(uint64_t micros) {
  58. std::this_thread::sleep_for(std::chrono::microseconds( micros ) / Clock::time_multiplier);
  59. }
  60. static void delayMillis(uint64_t millis) {
  61. std::this_thread::sleep_for(std::chrono::milliseconds( millis ) / Clock::time_multiplier);
  62. }
  63. static void delaySeconds(double secs) {
  64. std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(secs * 1000) / Clock::time_multiplier);
  65. }
  66. // Will reduce timer resolution increasing likelihood of overflows
  67. static void setTimeMultiplier(double tm) {
  68. Clock::time_multiplier = tm;
  69. }
  70. private:
  71. static std::chrono::nanoseconds startup;
  72. static uint32_t frequency;
  73. static double time_multiplier;
  74. };