Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 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
 
23
#include "Marlin.h"
24
#include "utility.h"
25
#include "temperature.h"
26
 
27
void safe_delay(millis_t ms) {
28
  while (ms > 50) {
29
    ms -= 50;
30
    delay(50);
31
    thermalManager.manage_heater();
32
  }
33
  delay(ms);
34
  thermalManager.manage_heater(); // This keeps us safe if too many small safe_delay() calls are made
35
}
36
 
37
#if ENABLED(EEPROM_SETTINGS)
38
 
39
  void crc16(uint16_t *crc, const void * const data, uint16_t cnt) {
40
    uint8_t *ptr = (uint8_t *)data;
41
    while (cnt--) {
42
      *crc = (uint16_t)(*crc ^ (uint16_t)(((uint16_t)*ptr++) << 8));
43
      for (uint8_t i = 0; i < 8; i++)
44
        *crc = (uint16_t)((*crc & 0x8000) ? ((uint16_t)(*crc << 1) ^ 0x1021) : (*crc << 1));
45
    }
46
  }
47
 
48
#endif // EEPROM_SETTINGS
49
 
50
#if ENABLED(ULTRA_LCD) || (ENABLED(DEBUG_LEVELING_FEATURE) && (ENABLED(MESH_BED_LEVELING) || (HAS_ABL && !ABL_PLANAR)))
51
 
52
  char conv[8] = { 0 };
53
 
54
  #define DIGIT(n) ('0' + (n))
55
  #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
56
  #define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
57
  #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
58
 
59
  // Convert unsigned int to string 123 format
60
  char* i8tostr3(const uint8_t i) {
61
    conv[4] = RJDIGIT(i, 100);
62
    conv[5] = RJDIGIT(i, 10);
63
    conv[6] = DIGIMOD(i, 1);
64
    return &conv[4];
65
  }
66
 
67
  // Convert signed int to rj string with 123 or -12 format
68
  char* itostr3(int i) {
69
    conv[4] = MINUSOR(i, RJDIGIT(i, 100));
70
    conv[5] = RJDIGIT(i, 10);
71
    conv[6] = DIGIMOD(i, 1);
72
    return &conv[4];
73
  }
74
 
75
  // Convert unsigned int to lj string with 123 format
76
  char* itostr3left(const int i) {
77
    char *str = &conv[6];
78
    *str = DIGIMOD(i, 1);
79
    if (i >= 10) {
80
      *(--str) = DIGIMOD(i, 10);
81
      if (i >= 100)
82
        *(--str) = DIGIMOD(i, 100);
83
    }
84
    return str;
85
  }
86
 
87
  // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
88
  char* itostr4sign(const int i) {
89
    const bool neg = i < 0;
90
    const int ii = neg ? -i : i;
91
    if (i >= 1000) {
92
      conv[3] = DIGIMOD(ii, 1000);
93
      conv[4] = DIGIMOD(ii, 100);
94
      conv[5] = DIGIMOD(ii, 10);
95
    }
96
    else if (ii >= 100) {
97
      conv[3] = neg ? '-' : ' ';
98
      conv[4] = DIGIMOD(ii, 100);
99
      conv[5] = DIGIMOD(ii, 10);
100
    }
101
    else {
102
      conv[3] = ' ';
103
      conv[4] = ' ';
104
      if (ii >= 10) {
105
        conv[4] = neg ? '-' : ' ';
106
        conv[5] = DIGIMOD(ii, 10);
107
      }
108
      else {
109
        conv[5] = neg ? '-' : ' ';
110
      }
111
    }
112
    conv[6] = DIGIMOD(ii, 1);
113
    return &conv[3];
114
  }
115
 
116
  // Convert unsigned float to string with 1.23 format
117
  char* ftostr12ns(const float &f) {
118
    const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
119
    conv[3] = DIGIMOD(i, 100);
120
    conv[4] = '.';
121
    conv[5] = DIGIMOD(i, 10);
122
    conv[6] = DIGIMOD(i, 1);
123
    return &conv[3];
124
  }
125
 
126
  // Convert signed float to fixed-length string with 023.45 / -23.45 format
127
  char* ftostr52(const float &f) {
128
    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
129
    conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
130
    conv[2] = DIGIMOD(i, 1000);
131
    conv[3] = DIGIMOD(i, 100);
132
    conv[4] = '.';
133
    conv[5] = DIGIMOD(i, 10);
134
    conv[6] = DIGIMOD(i, 1);
135
    return &conv[1];
136
  }
137
 
138
  #if ENABLED(LCD_DECIMAL_SMALL_XY)
139
 
140
    // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
141
    char* ftostr4sign(const float &f) {
142
      const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
143
      if (!WITHIN(i, -99, 999)) return itostr4sign((int)f);
144
      const bool neg = i < 0;
145
      const int ii = neg ? -i : i;
146
      conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
147
      conv[4] = DIGIMOD(ii, 10);
148
      conv[5] = '.';
149
      conv[6] = DIGIMOD(ii, 1);
150
      return &conv[3];
151
    }
152
 
153
  #endif // LCD_DECIMAL_SMALL_XY
154
 
155
  // Convert float to fixed-length string with +123.4 / -123.4 format
156
  char* ftostr41sign(const float &f) {
157
    int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
158
    conv[1] = MINUSOR(i, '+');
159
    conv[2] = DIGIMOD(i, 1000);
160
    conv[3] = DIGIMOD(i, 100);
161
    conv[4] = DIGIMOD(i, 10);
162
    conv[5] = '.';
163
    conv[6] = DIGIMOD(i, 1);
164
    return &conv[1];
165
  }
166
 
167
  // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
168
  char* ftostr43sign(const float &f, char plus/*=' '*/) {
169
    long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
170
    conv[1] = i ? MINUSOR(i, plus) : ' ';
171
    conv[2] = DIGIMOD(i, 1000);
172
    conv[3] = '.';
173
    conv[4] = DIGIMOD(i, 100);
174
    conv[5] = DIGIMOD(i, 10);
175
    conv[6] = DIGIMOD(i, 1);
176
    return &conv[1];
177
  }
178
 
179
  // Convert unsigned float to rj string with 12345 format
180
  char* ftostr5rj(const float &f) {
181
    const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
182
    conv[2] = RJDIGIT(i, 10000);
183
    conv[3] = RJDIGIT(i, 1000);
184
    conv[4] = RJDIGIT(i, 100);
185
    conv[5] = RJDIGIT(i, 10);
186
    conv[6] = DIGIMOD(i, 1);
187
    return &conv[2];
188
  }
189
 
190
  // Convert signed float to string with +1234.5 format
191
  char* ftostr51sign(const float &f) {
192
    long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
193
    conv[0] = MINUSOR(i, '+');
194
    conv[1] = DIGIMOD(i, 10000);
195
    conv[2] = DIGIMOD(i, 1000);
196
    conv[3] = DIGIMOD(i, 100);
197
    conv[4] = DIGIMOD(i, 10);
198
    conv[5] = '.';
199
    conv[6] = DIGIMOD(i, 1);
200
    return conv;
201
  }
202
 
203
  // Convert signed float to string with +123.45 format
204
  char* ftostr52sign(const float &f) {
205
    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
206
    conv[0] = MINUSOR(i, '+');
207
    conv[1] = DIGIMOD(i, 10000);
208
    conv[2] = DIGIMOD(i, 1000);
209
    conv[3] = DIGIMOD(i, 100);
210
    conv[4] = '.';
211
    conv[5] = DIGIMOD(i, 10);
212
    conv[6] = DIGIMOD(i, 1);
213
    return conv;
214
  }
215
 
216
  // Convert unsigned float to string with 1234.56 format omitting trailing zeros
217
  char* ftostr62rj(const float &f) {
218
    const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
219
    conv[0] = RJDIGIT(i, 100000);
220
    conv[1] = RJDIGIT(i, 10000);
221
    conv[2] = RJDIGIT(i, 1000);
222
    conv[3] = DIGIMOD(i, 100);
223
    conv[4] = '.';
224
    conv[5] = DIGIMOD(i, 10);
225
    conv[6] = DIGIMOD(i, 1);
226
    return conv;
227
  }
228
 
229
  // Convert signed float to space-padded string with -_23.4_ format
230
  char* ftostr52sp(const float &f) {
231
    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
232
    uint8_t dig;
233
    conv[1] = MINUSOR(i, RJDIGIT(i, 10000));
234
    conv[2] = RJDIGIT(i, 1000);
235
    conv[3] = DIGIMOD(i, 100);
236
 
237
    if ((dig = i % 10)) {          // second digit after decimal point?
238
      conv[4] = '.';
239
      conv[5] = DIGIMOD(i, 10);
240
      conv[6] = DIGIT(dig);
241
    }
242
    else {
243
      if ((dig = (i / 10) % 10)) { // first digit after decimal point?
244
        conv[4] = '.';
245
        conv[5] = DIGIT(dig);
246
      }
247
      else                          // nothing after decimal point
248
        conv[4] = conv[5] = ' ';
249
      conv[6] = ' ';
250
    }
251
    return &conv[1];
252
  }
253
 
254
#endif // ULTRA_LCD || (DEBUG_LEVELING_FEATURE && (MESH_BED_LEVELING || (HAS_ABL && !ABL_PLANAR)))