Browse Source

Move number-to-string function to utility.*

Scott Lahteine 8 years ago
parent
commit
305913545e
6 changed files with 238 additions and 268 deletions
  1. 3
    248
      Marlin/ultralcd.cpp
  2. 0
    17
      Marlin/ultralcd.h
  3. 1
    0
      Marlin/ultralcd_impl_DOGM.h
  4. 4
    2
      Marlin/ultralcd_impl_HD44780.h
  5. 183
    0
      Marlin/utility.cpp
  6. 47
    1
      Marlin/utility.h

+ 3
- 248
Marlin/ultralcd.cpp View File

@@ -28,6 +28,7 @@
28 28
 #include "temperature.h"
29 29
 #include "stepper.h"
30 30
 #include "configuration_store.h"
31
+#include "utility.h"
31 32
 
32 33
 #if ENABLED(PRINTCOUNTER)
33 34
   #include "printcounter.h"
@@ -1878,6 +1879,7 @@ void kill_screen(const char* lcd_msg) {
1878 1879
    *
1879 1880
    */
1880 1881
   #if ENABLED(FWRETRACT)
1882
+
1881 1883
     static void lcd_control_retract_menu() {
1882 1884
       START_MENU();
1883 1885
       MENU_ITEM(back, MSG_CONTROL);
@@ -1895,6 +1897,7 @@ void kill_screen(const char* lcd_msg) {
1895 1897
       MENU_ITEM_EDIT(float3, MSG_CONTROL_RETRACT_RECOVERF, &retract_recover_feedrate_mm_s, 1, 999);
1896 1898
       END_MENU();
1897 1899
     }
1900
+
1898 1901
   #endif // FWRETRACT
1899 1902
 
1900 1903
   #if ENABLED(SDSUPPORT)
@@ -2936,252 +2939,4 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
2936 2939
 
2937 2940
 #endif // ULTIPANEL
2938 2941
 
2939
-/*********************************/
2940
-/** Number to string conversion **/
2941
-/*********************************/
2942
-
2943
-#define DIGIT(n) ('0' + (n))
2944
-#define DIGIMOD(n) DIGIT((n) % 10)
2945
-
2946
-char conv[8];
2947
-
2948
-// Convert float to rj string with 123 or -12 format
2949
-char *ftostr3(const float& x) { return itostr3((int)x); }
2950
-
2951
-// Convert float to rj string with _123, -123, _-12, or __-1 format
2952
-char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
2953
-
2954
-// Convert unsigned int to string with 12 format
2955
-char* itostr2(const uint8_t& x) {
2956
-  int xx = x;
2957
-  conv[0] = DIGIMOD(xx / 10);
2958
-  conv[1] = DIGIMOD(xx);
2959
-  conv[2] = '\0';
2960
-  return conv;
2961
-}
2962
-
2963
-// Convert float to string with +123.4 / -123.4 format
2964
-char* ftostr41sign(const float& x) {
2965
-  int xx = int(abs(x * 10)) % 10000;
2966
-  conv[0] = x >= 0 ? '+' : '-';
2967
-  conv[1] = DIGIMOD(xx / 1000);
2968
-  conv[2] = DIGIMOD(xx / 100);
2969
-  conv[3] = DIGIMOD(xx / 10);
2970
-  conv[4] = '.';
2971
-  conv[5] = DIGIMOD(xx);
2972
-  conv[6] = '\0';
2973
-  return conv;
2974
-}
2975
-
2976
-// Convert signed float to string with 023.45 / -23.45 format
2977
-char *ftostr32(const float& x) {
2978
-  long xx = abs(x * 100);
2979
-  conv[0] = x >= 0 ? DIGIMOD(xx / 10000) : '-';
2980
-  conv[1] = DIGIMOD(xx / 1000);
2981
-  conv[2] = DIGIMOD(xx / 100);
2982
-  conv[3] = '.';
2983
-  conv[4] = DIGIMOD(xx / 10);
2984
-  conv[5] = DIGIMOD(xx);
2985
-  conv[6] = '\0';
2986
-  return conv;
2987
-}
2988
-
2989
-// Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
2990
-char* ftostr43sign(const float& x, char plus/*=' '*/) {
2991
-  long xx = x * 1000;
2992
-  if (xx == 0)
2993
-    conv[0] = ' ';
2994
-  else if (xx > 0)
2995
-    conv[0] = plus;
2996
-  else {
2997
-    xx = -xx;
2998
-    conv[0] = '-';
2999
-  }
3000
-  conv[1] = DIGIMOD(xx / 1000);
3001
-  conv[2] = '.';
3002
-  conv[3] = DIGIMOD(xx / 100);
3003
-  conv[4] = DIGIMOD(xx / 10);
3004
-  conv[5] = DIGIMOD(xx);
3005
-  conv[6] = '\0';
3006
-  return conv;
3007
-}
3008
-
3009
-// Convert unsigned float to string with 1.23 format
3010
-char* ftostr12ns(const float& x) {
3011
-  long xx = x * 100;
3012
-  xx = abs(xx);
3013
-  conv[0] = DIGIMOD(xx / 100);
3014
-  conv[1] = '.';
3015
-  conv[2] = DIGIMOD(xx / 10);
3016
-  conv[3] = DIGIMOD(xx);
3017
-  conv[4] = '\0';
3018
-  return conv;
3019
-}
3020
-
3021
-// Convert signed int to lj string with +012 / -012 format
3022
-char* itostr3sign(const int& x) {
3023
-  int xx;
3024
-  if (x >= 0) {
3025
-    conv[0] = '+';
3026
-    xx = x;
3027
-  }
3028
-  else {
3029
-    conv[0] = '-';
3030
-    xx = -x;
3031
-  }
3032
-  conv[1] = DIGIMOD(xx / 100);
3033
-  conv[2] = DIGIMOD(xx / 10);
3034
-  conv[3] = DIGIMOD(xx);
3035
-  conv[4] = '.';
3036
-  conv[5] = '0';
3037
-  conv[6] = '\0';
3038
-  return conv;
3039
-}
3040
-
3041
-// Convert signed int to rj string with 123 or -12 format
3042
-char* itostr3(const int& x) {
3043
-  int xx = x;
3044
-  if (xx < 0) {
3045
-    conv[0] = '-';
3046
-    xx = -xx;
3047
-  }
3048
-  else
3049
-    conv[0] = xx >= 100 ? DIGIMOD(xx / 100) : ' ';
3050
-
3051
-  conv[1] = xx >= 10 ? DIGIMOD(xx / 10) : ' ';
3052
-  conv[2] = DIGIMOD(xx);
3053
-  conv[3] = '\0';
3054
-  return conv;
3055
-}
3056
-
3057
-// Convert unsigned int to lj string with 123 format
3058
-char* itostr3left(const int& xx) {
3059
-  if (xx >= 100) {
3060
-    conv[0] = DIGIMOD(xx / 100);
3061
-    conv[1] = DIGIMOD(xx / 10);
3062
-    conv[2] = DIGIMOD(xx);
3063
-    conv[3] = '\0';
3064
-  }
3065
-  else if (xx >= 10) {
3066
-    conv[0] = DIGIMOD(xx / 10);
3067
-    conv[1] = DIGIMOD(xx);
3068
-    conv[2] = '\0';
3069
-  }
3070
-  else {
3071
-    conv[0] = DIGIMOD(xx);
3072
-    conv[1] = '\0';
3073
-  }
3074
-  return conv;
3075
-}
3076
-
3077
-// Convert signed int to rj string with _123, -123, _-12, or __-1 format
3078
-char *itostr4sign(const int& x) {
3079
-  int xx = abs(x);
3080
-  int sign = 0;
3081
-  if (xx >= 100) {
3082
-    conv[1] = DIGIMOD(xx / 100);
3083
-    conv[2] = DIGIMOD(xx / 10);
3084
-  }
3085
-  else if (xx >= 10) {
3086
-    conv[0] = ' ';
3087
-    sign = 1;
3088
-    conv[2] = DIGIMOD(xx / 10);
3089
-  }
3090
-  else {
3091
-    conv[0] = ' ';
3092
-    conv[1] = ' ';
3093
-    sign = 2;
3094
-  }
3095
-  conv[sign] = x < 0 ? '-' : ' ';
3096
-  conv[3] = DIGIMOD(xx);
3097
-  conv[4] = '\0';
3098
-  return conv;
3099
-}
3100
-
3101
-// Convert unsigned float to rj string with 12345 format
3102
-char* ftostr5rj(const float& x) {
3103
-  long xx = abs(x);
3104
-  conv[0] = xx >= 10000 ? DIGIMOD(xx / 10000) : ' ';
3105
-  conv[1] = xx >= 1000 ? DIGIMOD(xx / 1000) : ' ';
3106
-  conv[2] = xx >= 100 ? DIGIMOD(xx / 100) : ' ';
3107
-  conv[3] = xx >= 10 ? DIGIMOD(xx / 10) : ' ';
3108
-  conv[4] = DIGIMOD(xx);
3109
-  conv[5] = '\0';
3110
-  return conv;
3111
-}
3112
-
3113
-// Convert signed float to string with +1234.5 format
3114
-char* ftostr51sign(const float& x) {
3115
-  long xx = abs(x * 10);
3116
-  conv[0] = (x >= 0) ? '+' : '-';
3117
-  conv[1] = DIGIMOD(xx / 10000);
3118
-  conv[2] = DIGIMOD(xx / 1000);
3119
-  conv[3] = DIGIMOD(xx / 100);
3120
-  conv[4] = DIGIMOD(xx / 10);
3121
-  conv[5] = '.';
3122
-  conv[6] = DIGIMOD(xx);
3123
-  conv[7] = '\0';
3124
-  return conv;
3125
-}
3126
-
3127
-// Convert signed float to string with +123.45 format
3128
-char* ftostr52sign(const float& x) {
3129
-  long xx = abs(x * 100);
3130
-  conv[0] = (x >= 0) ? '+' : '-';
3131
-  conv[1] = DIGIMOD(xx / 10000);
3132
-  conv[2] = DIGIMOD(xx / 1000);
3133
-  conv[3] = DIGIMOD(xx / 100);
3134
-  conv[4] = '.';
3135
-  conv[5] = DIGIMOD(xx / 10);
3136
-  conv[6] = DIGIMOD(xx);
3137
-  conv[7] = '\0';
3138
-  return conv;
3139
-}
3140
-
3141
-// Convert signed float to space-padded string with -_23.4_ format
3142
-char* ftostr52sp(const float& x) {
3143
-  long xx = x * 100;
3144
-  uint8_t dig;
3145
-  if (xx < 0) { // negative val = -_0
3146
-    xx = -xx;
3147
-    conv[0] = '-';
3148
-    dig = (xx / 1000) % 10;
3149
-    conv[1] = dig ? DIGIT(dig) : ' ';
3150
-  }
3151
-  else { // positive val = __0
3152
-    dig = (xx / 10000) % 10;
3153
-    if (dig) {
3154
-      conv[0] = DIGIT(dig);
3155
-      conv[1] = DIGIMOD(xx / 1000);
3156
-    }
3157
-    else {
3158
-      conv[0] = ' ';
3159
-      dig = (xx / 1000) % 10;
3160
-      conv[1] = dig ? DIGIT(dig) : ' ';
3161
-    }
3162
-  }
3163
-
3164
-  conv[2] = DIGIMOD(xx / 100); // lsd always
3165
-
3166
-  dig = xx % 10;
3167
-  if (dig) { // 2 decimal places
3168
-    conv[5] = DIGIT(dig);
3169
-    conv[4] = DIGIMOD(xx / 10);
3170
-    conv[3] = '.';
3171
-  }
3172
-  else { // 1 or 0 decimal place
3173
-    dig = (xx / 10) % 10;
3174
-    if (dig) {
3175
-      conv[4] = DIGIT(dig);
3176
-      conv[3] = '.';
3177
-    }
3178
-    else {
3179
-      conv[3] = conv[4] = ' ';
3180
-    }
3181
-    conv[5] = ' ';
3182
-  }
3183
-  conv[6] = '\0';
3184
-  return conv;
3185
-}
3186
-
3187 2942
 #endif // ULTRA_LCD

+ 0
- 17
Marlin/ultralcd.h View File

@@ -167,21 +167,4 @@
167 167
 
168 168
 #endif //ULTRA_LCD
169 169
 
170
-char* itostr2(const uint8_t& x);
171
-char* itostr3sign(const int& x);
172
-char* itostr3(const int& x);
173
-char* itostr3left(const int& x);
174
-char* itostr4sign(const int& x);
175
-
176
-char* ftostr3(const float& x);
177
-char* ftostr4sign(const float& x);
178
-char* ftostr41sign(const float& x);
179
-char* ftostr32(const float& x);
180
-char* ftostr43sign(const float& x, char plus=' ');
181
-char* ftostr12ns(const float& x);
182
-char* ftostr5rj(const float& x);
183
-char* ftostr51sign(const float& x);
184
-char* ftostr52sign(const float& x);
185
-char* ftostr52sp(const float& x); // remove zero-padding from ftostr32
186
-
187 170
 #endif //ULTRALCD_H

+ 1
- 0
Marlin/ultralcd_impl_DOGM.h View File

@@ -45,6 +45,7 @@
45 45
 #include "ultralcd.h"
46 46
 #include "ultralcd_st7920_u8glib_rrd.h"
47 47
 #include "dogm_bitmaps.h"
48
+#include "utility.h"
48 49
 #include "duration_t.h"
49 50
 
50 51
 #include <U8glib.h>

+ 4
- 2
Marlin/ultralcd_impl_HD44780.h View File

@@ -24,9 +24,11 @@
24 24
 #define ULTRALCD_IMPL_HD44780_H
25 25
 
26 26
 /**
27
-* Implementation of the LCD display routines for a Hitachi HD44780 display. These are common LCD character displays.
28
-**/
27
+ * Implementation of the LCD display routines for a Hitachi HD44780 display.
28
+ * These are the most common LCD character displays.
29
+ */
29 30
 
31
+#include "utility.h"
30 32
 #include "duration_t.h"
31 33
 
32 34
 extern volatile uint8_t buttons;  //an extended version of the last checked buttons in a bit array.

+ 183
- 0
Marlin/utility.cpp View File

@@ -32,3 +32,186 @@ void safe_delay(millis_t ms) {
32 32
   }
33 33
   delay(ms);
34 34
 }
35
+
36
+#if ENABLED(ULTRA_LCD)
37
+
38
+  char conv[8];
39
+
40
+  #define DIGIT(n) ('0' + (n))
41
+  #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
42
+  #define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
43
+  #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
44
+
45
+  // Convert unsigned int to string with 12 format
46
+  char* itostr2(const uint8_t& x) {
47
+    int xx = x;
48
+    conv[0] = DIGIMOD(xx, 10);
49
+    conv[1] = DIGIMOD(xx, 1);
50
+    conv[2] = '\0';
51
+    return conv;
52
+  }
53
+
54
+  // Convert signed int to rj string with 123 or -12 format
55
+  char* itostr3(const int& x) {
56
+    int xx = x;
57
+    conv[0] = MINUSOR(xx, RJDIGIT(xx, 100));
58
+    conv[1] = RJDIGIT(xx, 10);
59
+    conv[2] = DIGIMOD(xx, 1);
60
+    conv[3] = '\0';
61
+    return conv;
62
+  }
63
+
64
+  // Convert unsigned int to lj string with 123 format
65
+  char* itostr3left(const int& xx) {
66
+    char *str = &conv[3];
67
+    *str = '\0';
68
+    *(--str) = DIGIMOD(xx, 1);
69
+    if (xx >= 10) {
70
+      *(--str) = DIGIMOD(xx, 10);
71
+      if (xx >= 100)
72
+        *(--str) = DIGIMOD(xx, 100);
73
+    }
74
+    return str;
75
+  }
76
+
77
+  // Convert signed int to rj string with _123, -123, _-12, or __-1 format
78
+  char *itostr4sign(const int& x) {
79
+    int xx = abs(x), sign = 0;
80
+    if (xx >= 100) {
81
+      conv[1] = DIGIMOD(xx, 100);
82
+      conv[2] = DIGIMOD(xx, 10);
83
+    }
84
+    else {
85
+      conv[0] = ' ';
86
+      if (xx >= 10) {
87
+        sign = 1;
88
+        conv[2] = DIGIMOD(xx, 10);
89
+      }
90
+      else {
91
+        conv[1] = ' ';
92
+        sign = 2;
93
+      }
94
+    }
95
+    conv[sign] = x < 0 ? '-' : ' ';
96
+    conv[3] = DIGIMOD(xx, 1);
97
+    conv[4] = '\0';
98
+    return conv;
99
+  }
100
+
101
+  // Convert unsigned float to string with 1.23 format
102
+  char* ftostr12ns(const float& x) {
103
+    long xx = abs(x * 100);
104
+    conv[0] = DIGIMOD(xx, 100);
105
+    conv[1] = '.';
106
+    conv[2] = DIGIMOD(xx, 10);
107
+    conv[3] = DIGIMOD(xx, 1);
108
+    conv[4] = '\0';
109
+    return conv;
110
+  }
111
+
112
+  // Convert signed float to fixed-length string with 023.45 / -23.45 format
113
+  char *ftostr32(const float& x) {
114
+    long xx = x * 100;
115
+    conv[0] = MINUSOR(xx, DIGIMOD(xx, 10000));
116
+    conv[1] = DIGIMOD(xx, 1000);
117
+    conv[2] = DIGIMOD(xx, 100);
118
+    conv[3] = '.';
119
+    conv[4] = DIGIMOD(xx, 10);
120
+    conv[5] = DIGIMOD(xx, 1);
121
+    conv[6] = '\0';
122
+    return conv;
123
+  }
124
+
125
+  // Convert float to fixed-length string with +123.4 / -123.4 format
126
+  char* ftostr41sign(const float& x) {
127
+    int xx = x * 10;
128
+    conv[0] = MINUSOR(xx, '+');
129
+    conv[1] = DIGIMOD(xx, 1000);
130
+    conv[2] = DIGIMOD(xx, 100);
131
+    conv[3] = DIGIMOD(xx, 10);
132
+    conv[4] = '.';
133
+    conv[5] = DIGIMOD(xx, 1);
134
+    conv[6] = '\0';
135
+    return conv;
136
+  }
137
+
138
+  // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
139
+  char* ftostr43sign(const float& x, char plus/*=' '*/) {
140
+    long xx = x * 1000;
141
+    conv[0] = xx ? MINUSOR(xx, plus) : ' ';
142
+    conv[1] = DIGIMOD(xx, 1000);
143
+    conv[2] = '.';
144
+    conv[3] = DIGIMOD(xx, 100);
145
+    conv[4] = DIGIMOD(xx, 10);
146
+    conv[5] = DIGIMOD(xx, 1);
147
+    conv[6] = '\0';
148
+    return conv;
149
+  }
150
+
151
+  // Convert unsigned float to rj string with 12345 format
152
+  char* ftostr5rj(const float& x) {
153
+    long xx = abs(x);
154
+    conv[0] = RJDIGIT(xx, 10000);
155
+    conv[1] = RJDIGIT(xx, 1000);
156
+    conv[2] = RJDIGIT(xx, 100);
157
+    conv[3] = RJDIGIT(xx, 10);
158
+    conv[4] = DIGIMOD(xx, 1);
159
+    conv[5] = '\0';
160
+    return conv;
161
+  }
162
+
163
+  // Convert signed float to string with +1234.5 format
164
+  char* ftostr51sign(const float& x) {
165
+    long xx = x * 10;
166
+    conv[0] = MINUSOR(xx, '+');
167
+    conv[1] = DIGIMOD(xx, 10000);
168
+    conv[2] = DIGIMOD(xx, 1000);
169
+    conv[3] = DIGIMOD(xx, 100);
170
+    conv[4] = DIGIMOD(xx, 10);
171
+    conv[5] = '.';
172
+    conv[6] = DIGIMOD(xx, 1);
173
+    conv[7] = '\0';
174
+    return conv;
175
+  }
176
+
177
+  // Convert signed float to string with +123.45 format
178
+  char* ftostr52sign(const float& x) {
179
+    long xx = x * 100;
180
+    conv[0] = MINUSOR(xx, '+');
181
+    conv[1] = DIGIMOD(xx, 10000);
182
+    conv[2] = DIGIMOD(xx, 1000);
183
+    conv[3] = DIGIMOD(xx, 100);
184
+    conv[4] = '.';
185
+    conv[5] = DIGIMOD(xx, 10);
186
+    conv[6] = DIGIMOD(xx, 1);
187
+    conv[7] = '\0';
188
+    return conv;
189
+  }
190
+
191
+  // Convert signed float to space-padded string with -_23.4_ format
192
+  char* ftostr52sp(const float& x) {
193
+    long xx = x * 100;
194
+    uint8_t dig;
195
+    conv[0] = MINUSOR(xx, RJDIGIT(xx, 10000));
196
+    conv[1] = RJDIGIT(xx, 1000);
197
+    conv[2] = DIGIMOD(xx, 100);
198
+
199
+    if ((dig = xx % 10)) {          // second digit after decimal point?
200
+      conv[3] = '.';
201
+      conv[4] = DIGIMOD(xx, 10);
202
+      conv[5] = DIGIT(dig);
203
+    }
204
+    else {
205
+      if ((dig = (xx / 10) % 10)) { // first digit after decimal point?
206
+        conv[3] = '.';
207
+        conv[4] = DIGIT(dig);
208
+      }
209
+      else                          // nothing after decimal point
210
+        conv[3] = conv[4] = ' ';
211
+      conv[5] = ' ';
212
+    }
213
+    conv[6] = '\0';
214
+    return conv;
215
+  }
216
+
217
+#endif // ULTRA_LCD

+ 47
- 1
Marlin/utility.h View File

@@ -25,4 +25,50 @@
25 25
 
26 26
 void safe_delay(millis_t ms);
27 27
 
28
-#endif
28
+#if ENABLED(ULTRA_LCD)
29
+
30
+  // Convert unsigned int to string with 12 format
31
+  char* itostr2(const uint8_t& x);
32
+
33
+  // Convert signed int to rj string with 123 or -12 format
34
+  char* itostr3(const int& x);
35
+
36
+  // Convert unsigned int to lj string with 123 format
37
+  char* itostr3left(const int& xx);
38
+
39
+  // Convert signed int to rj string with _123, -123, _-12, or __-1 format
40
+  char *itostr4sign(const int& x);
41
+
42
+  // Convert unsigned float to string with 1.23 format
43
+  char* ftostr12ns(const float& x);
44
+
45
+  // Convert signed float to fixed-length string with 023.45 / -23.45 format
46
+  char *ftostr32(const float& x);
47
+
48
+  // Convert float to fixed-length string with +123.4 / -123.4 format
49
+  char* ftostr41sign(const float& x);
50
+
51
+  // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
52
+  char* ftostr43sign(const float& x, char plus=' ');
53
+
54
+  // Convert unsigned float to rj string with 12345 format
55
+  char* ftostr5rj(const float& x);
56
+
57
+  // Convert signed float to string with +1234.5 format
58
+  char* ftostr51sign(const float& x);
59
+
60
+  // Convert signed float to space-padded string with -_23.4_ format
61
+  char* ftostr52sp(const float& x);
62
+
63
+  // Convert signed float to string with +123.45 format
64
+  char* ftostr52sign(const float& x);
65
+
66
+  // Convert float to rj string with 123 or -12 format
67
+  FORCE_INLINE char *ftostr3(const float& x) { return itostr3((int)x); }
68
+
69
+  // Convert float to rj string with _123, -123, _-12, or __-1 format
70
+  FORCE_INLINE char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
71
+
72
+#endif // ULTRA_LCD
73
+
74
+#endif // __UTILITY_H__

Loading…
Cancel
Save