My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

bresenham.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "../core/serial.h"
  24. /**
  25. * bresenham_t.h - Bresenham algorithm template
  26. *
  27. * An array of values / counters that tick together
  28. */
  29. #define FORCE_INLINE __attribute__((always_inline)) inline
  30. #define _O3 __attribute__((optimize("O3")))
  31. template <uint8_t uid, uint8_t size>
  32. struct BresenhamCfg { static constexpr uint8_t UID = uid, SIZE = size; };
  33. template<typename T, typename Cfg>
  34. class Bresenham {
  35. private:
  36. static constexpr T signtest = -1;
  37. static_assert(signtest < 0, "Bresenham type must be signed!");
  38. public:
  39. static T divisor, value[Cfg::SIZE], dir[Cfg::SIZE], dividend[Cfg::SIZE], counter[Cfg::SIZE];
  40. // Default: Instantiate all items with the identical parameters
  41. Bresenham(const T &inDivisor=1, const int8_t &inDir=1, const T &inDividend=1, const T &inValue=0) {
  42. for (uint8_t i = 0; i < Cfg::SIZE; i++) init(i, inDivisor, inDir, inDividend, inValue);
  43. }
  44. // Instantiate all items with the same divisor
  45. Bresenham(const T &inDivisor, const int8_t (&inDir)[Cfg::SIZE], const T (&inDividend)[Cfg::SIZE], const T (&inValue)[Cfg::SIZE]={0}) {
  46. init(inDivisor, inDir, inDividend, inValue);
  47. }
  48. // Instantiate all items with the same divisor and direction
  49. Bresenham(const T &inDivisor, const int8_t &inDir, const T (&inDividend)[Cfg::SIZE], const T (&inValue)[Cfg::SIZE]={0}) {
  50. init(inDivisor, inDir, inDividend, inValue);
  51. }
  52. // Init all items with the same parameters
  53. FORCE_INLINE static void init(const uint8_t index, const T &inDivisor=1, const int8_t &inDir=1, const T &inDividend=1, const T &inValue=0) {
  54. divisor = inDivisor;
  55. dir[index] = inDir;
  56. dividend[index] = inDividend;
  57. value[index] = inValue;
  58. prime(index);
  59. }
  60. // Init all items with the same divisor
  61. FORCE_INLINE static void init(const T &inDivisor, const int8_t (&inDir)[Cfg::SIZE], const T (&inDividend)[Cfg::SIZE], const T (&inValue)[Cfg::SIZE]={0}) {
  62. divisor = inDivisor;
  63. for (uint8_t i = 0; i < Cfg::SIZE; i++) {
  64. dir[i] = inDir[i];
  65. dividend[i] = inDividend[i];
  66. value[i] = inValue[i];
  67. }
  68. prime();
  69. }
  70. // Init all items with the same divisor and direction
  71. FORCE_INLINE static void init(const T &inDivisor, const int8_t &inDir, const T (&inDividend)[Cfg::SIZE], const T (&inValue)[Cfg::SIZE]={0}) {
  72. divisor = inDivisor;
  73. for (uint8_t i = 0; i < Cfg::SIZE; i++) {
  74. dir[i] = inDir;
  75. dividend[i] = inDividend[i];
  76. value[i] = inValue[i];
  77. }
  78. prime();
  79. }
  80. // Reinit item with new dir, dividend, value keeping the same divisor
  81. FORCE_INLINE static void reinit(const uint8_t index, const int8_t &inDir=1, const T &inDividend=1, const T &inValue=0) {
  82. dir[index] = inDir;
  83. dividend[index] = inDividend;
  84. value[index] = inValue;
  85. prime();
  86. }
  87. FORCE_INLINE static void prime(const uint8_t index) { counter[index] = -(divisor / 2); }
  88. FORCE_INLINE static void prime() { for (uint8_t i = 0; i < Cfg::SIZE; i++) prime(i); }
  89. FORCE_INLINE static void back(const uint8_t index) { counter[index] -= divisor; }
  90. FORCE_INLINE static bool tick1(const uint8_t index) {
  91. counter[index] += dividend[index];
  92. return counter[index] > 0;
  93. }
  94. FORCE_INLINE static void tick(const uint8_t index) {
  95. if (tick1(index)) { value[index] += dir[index]; back(index); }
  96. }
  97. FORCE_INLINE static void tick1() _O3 { for (uint8_t i = 0; i < Cfg::SIZE; i++) (void)tick1(i); }
  98. FORCE_INLINE static void tick() _O3 { for (uint8_t i = 0; i < Cfg::SIZE; i++) (void)tick(i); }
  99. static void report(const uint8_t index) {
  100. if (index < Cfg::SIZE) {
  101. SERIAL_ECHOPAIR("bresenham ", int(index), " : (", dividend[index], "/", divisor, ") ");
  102. if (counter[index] >= 0) SERIAL_CHAR(' ');
  103. if (labs(counter[index]) < 100) { SERIAL_CHAR(' '); if (labs(counter[index]) < 10) SERIAL_CHAR(' '); }
  104. SERIAL_ECHO(counter[index]);
  105. SERIAL_ECHOLNPAIR(" ... ", value[index]);
  106. }
  107. }
  108. static void report() { for (uint8_t i = 0; i < Cfg::SIZE; i++) report(i); }
  109. };