Browse Source

more work on firmware.

Thomas Buck 2 years ago
parent
commit
9ad5ee7e53
9 changed files with 462 additions and 76 deletions
  1. 1
    1
      include/data.h
  2. 43
    2
      include/statemachine.h
  3. 9
    4
      include/steppers.h
  4. 3
    3
      src/data.cpp
  5. 4
    4
      src/encoder.cpp
  6. 41
    7
      src/lcd.cpp
  7. 212
    17
      src/statemachine.cpp
  8. 125
    34
      src/states.cpp
  9. 24
    4
      src/steppers.cpp

+ 1
- 1
include/data.h View File

9
 };
9
 };
10
 
10
 
11
 struct data_config_preset {
11
 struct data_config_preset {
12
-    uint8_t rows, cols;
12
+    uint8_t count_x, count_y;
13
     float distance_x, distance_y;
13
     float distance_x, distance_y;
14
     float offset_x, offset_y;
14
     float offset_x, offset_y;
15
     float top_z, bottom_z;
15
     float top_z, bottom_z;

+ 43
- 2
include/statemachine.h View File

94
     void dataGet(GetFuncPtr get);
94
     void dataGet(GetFuncPtr get);
95
     void dataCall(CallFuncPtr call);
95
     void dataCall(CallFuncPtr call);
96
 
96
 
97
+    void setPrefix(String pre);
98
+
97
     virtual void enterState(void);
99
     virtual void enterState(void);
98
     virtual void inState(StateMachineInput smi);
100
     virtual void inState(StateMachineInput smi);
99
 
101
 
106
 
108
 
107
     int menuPos, menuOff;
109
     int menuPos, menuOff;
108
     int count;
110
     int count;
109
-    Array<const char *, STATE_ARRAY_SIZE> contents;
111
+    Array<String, STATE_ARRAY_SIZE> contents;
112
+    String prefix;
110
 };
113
 };
111
 
114
 
112
 template <typename T>
115
 template <typename T>
114
 public:
117
 public:
115
     StateValue(State *_parent, T &_value, T _min, T _max);
118
     StateValue(State *_parent, T &_value, T _min, T _max);
116
 
119
 
120
+    void setHeading(const char *_heading);
121
+    void setText(const char *_text);
122
+
123
+    typedef void(*EnterFuncPtr)(void);
117
     typedef void(*UpdateFuncPtr)(T value);
124
     typedef void(*UpdateFuncPtr)(T value);
118
 
125
 
126
+    void onEnter(EnterFuncPtr func);
127
+    void onLiveUpdate(UpdateFuncPtr func);
119
     void onUpdate(UpdateFuncPtr func);
128
     void onUpdate(UpdateFuncPtr func);
120
 
129
 
121
     virtual void enterState(void);
130
     virtual void enterState(void);
126
 
135
 
127
     T &value;
136
     T &value;
128
     T min, max;
137
     T min, max;
129
-    UpdateFuncPtr updateFunc;
138
+    const char *heading;
139
+    const char *text;
140
+    EnterFuncPtr onEnterFunc;
141
+    UpdateFuncPtr updateFunc, updateLiveFunc;
130
 };
142
 };
131
 
143
 
144
+template <typename T, size_t N>
145
+class StateValues : public State {
146
+public:
147
+    StateValues(State *_parent);
148
+
149
+    void setData(size_t index, const char *name, T *value, T min, T max);
150
+
151
+    void setHeading(const char *_heading);
152
+    void setText(const char *_text);
153
+
154
+    typedef void(*UpdateFuncPtr)(size_t index, T value);
155
+
156
+    void onLiveUpdate(UpdateFuncPtr func);
157
+    void onUpdate(UpdateFuncPtr func);
158
+
159
+    virtual void enterState(void);
160
+    virtual void inState(StateMachineInput smi);
161
+
162
+private:
163
+    void display(void);
164
+
165
+    T *values[N];
166
+    T mins[N], maxs[N];
167
+    const char *texts[N];
168
+    const char *heading;
169
+    size_t pos;
170
+    bool editing;
171
+    UpdateFuncPtr updateFunc, updateLiveFunc;
172
+};
132
 
173
 
133
 #endif // _STATE_MACHINE_H_
174
 #endif // _STATE_MACHINE_H_

+ 9
- 4
include/steppers.h View File

7
 bool steppers_homed(void);
7
 bool steppers_homed(void);
8
 void steppers_start_homing(void);
8
 void steppers_start_homing(void);
9
 
9
 
10
-int steppers_move_x(long pos);
11
-int steppers_move_y(long pos);
12
-int steppers_move_z(long pos);
13
-int steppers_move_e(long pos);
10
+float steppers_pos_x(void);
11
+float steppers_pos_y(void);
12
+float steppers_pos_z(void);
13
+float steppers_pos_e(void);
14
+
15
+int steppers_move_x(float pos);
16
+int steppers_move_y(float pos);
17
+int steppers_move_z(float pos);
18
+int steppers_move_e(float pos);
14
 
19
 
15
 void steppers_kill(void);
20
 void steppers_kill(void);
16
 
21
 

+ 3
- 3
src/data.cpp View File

7
 
7
 
8
 // TODO make defines platform specific
8
 // TODO make defines platform specific
9
 #define EEPROM_SIZE 4096
9
 #define EEPROM_SIZE 4096
10
-#define RAM_SIZE (8192 / 2)
10
+#define RAM_SIZE (8192 / 8)
11
 
11
 
12
 struct data_config {
12
 struct data_config {
13
     uint8_t data_schema_version;
13
     uint8_t data_schema_version;
111
             return false;
111
             return false;
112
         }
112
         }
113
     } else {
113
     } else {
114
-        Serial.print("read=");
114
+        Serial.print(F("checksum read="));
115
         Serial.print(config.checksum);
115
         Serial.print(config.checksum);
116
-        Serial.print(" calc=");
116
+        Serial.print(F(" calc="));
117
         Serial.println(checksum);
117
         Serial.println(checksum);
118
         last_error = "Checksum";
118
         last_error = "Checksum";
119
         return false;
119
         return false;

+ 4
- 4
src/encoder.cpp View File

53
 
53
 
54
 #ifdef DEBUG_ENCODER
54
 #ifdef DEBUG_ENCODER
55
     if (diff != 0) {
55
     if (diff != 0) {
56
-        Serial.print("encoder_change: ");
56
+        Serial.print(F("encoder_change: "));
57
         Serial.print(diff);
57
         Serial.print(diff);
58
-        Serial.print(" ticks: ");
58
+        Serial.print(F(" ticks: "));
59
         Serial.println(new_pos);
59
         Serial.println(new_pos);
60
     }
60
     }
61
 #endif // DEBUG_ENCODER
61
 #endif // DEBUG_ENCODER
72
         ignore_until_next_unclick = true;
72
         ignore_until_next_unclick = true;
73
 
73
 
74
 #ifdef DEBUG_ENCODER
74
 #ifdef DEBUG_ENCODER
75
-        Serial.println("encoder_click: 1");
75
+        Serial.println(F("encoder_click: 1"));
76
 #endif // DEBUG_ENCODER
76
 #endif // DEBUG_ENCODER
77
     }
77
     }
78
 
78
 
83
 #ifdef KILL_PIN
83
 #ifdef KILL_PIN
84
 #ifdef DEBUG_ENCODER
84
 #ifdef DEBUG_ENCODER
85
     if (kill_state == 1) {
85
     if (kill_state == 1) {
86
-        Serial.println("kill_switch: 1");
86
+        Serial.println(F("kill_switch: 1"));
87
     }
87
     }
88
 #endif // DEBUG_ENCODER
88
 #endif // DEBUG_ENCODER
89
 
89
 

+ 41
- 7
src/lcd.cpp View File

63
 #define SCROLL_DELAY 500
63
 #define SCROLL_DELAY 500
64
 unsigned long last_scroll_time = 0;
64
 unsigned long last_scroll_time = 0;
65
 
65
 
66
+#define IS_TEXT_CHAR(x) (((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z')))
67
+
66
 void lcd_shorten_multiline(String s[], int n, int w, String *remainder) {
68
 void lcd_shorten_multiline(String s[], int n, int w, String *remainder) {
67
-    // if a line is longer then w, take end off and put in front of next line
68
     String for_next_line = "";
69
     String for_next_line = "";
69
     for (int i = 0; i < n; i++) {
70
     for (int i = 0; i < n; i++) {
71
+        // take previously stored string and put in front of line
70
         s[i] = for_next_line + s[i];
72
         s[i] = for_next_line + s[i];
71
         for_next_line = "";
73
         for_next_line = "";
74
+
75
+        // if a line is longer then w, take end off and store
72
         if (s[i].length() > w) {
76
         if (s[i].length() > w) {
73
-            for_next_line = s[i].substring(w);
77
+            for_next_line += s[i].substring(w);
74
             s[i] = s[i].substring(0, w);
78
             s[i] = s[i].substring(0, w);
75
         }
79
         }
80
+
81
+        // if a newline is in this line, end the text there and split it
82
+        for (int j = 0; j < s[i].length(); j++) {
83
+            if (s[i][j] == '\n') {
84
+                for_next_line = s[i].substring(j + 1) + for_next_line;
85
+                s[i] = s[i].substring(0, j);
86
+                break;
87
+            }
88
+        }
89
+
90
+        // if the last char of this line and the first char of for_next_line
91
+        // exist and are both upper/lowercase characters, check if one of the
92
+        // previous 5 chars is a space, and instead split there
93
+        if ((n > 1) && (for_next_line.length() > 0) && IS_TEXT_CHAR(s[i][s[i].length() - 1]) && IS_TEXT_CHAR(for_next_line[0])) {
94
+            for (int j = s[i].length() - 2; (j >= 0) && (j >= s[i].length() - 2 - 5); j--) {
95
+                if (s[i][j] == ' ') {
96
+                for_next_line = s[i].substring(j + 1) + for_next_line;
97
+                s[i] = s[i].substring(0, j + 1);
98
+                break;
99
+                }
100
+            }
101
+        }
76
     }
102
     }
77
 
103
 
78
     // if something remains at end, store remainder, if given, otherwise leave at last line
104
     // if something remains at end, store remainder, if given, otherwise leave at last line
176
     lcd.firstPage();
202
     lcd.firstPage();
177
     do {
203
     do {
178
         lcd.setFont(u8g2_font_luBS12_tr);
204
         lcd.setFont(u8g2_font_luBS12_tr);
179
-        lcd.drawStr(0, 14 * 1, "Fuellfix v2");
180
-        lcd.drawStr(0, 14 * 2, "Initializing");
205
+
206
+        String s = F("Fuellfix v2");
207
+        lcd.drawStr(0, 14 * 1, s.c_str());
208
+
209
+        s = F("Initializing");
210
+        lcd.drawStr(0, 14 * 2, s.c_str());
181
 
211
 
182
         lcd.setFont(u8g2_font_t0_12b_tr);
212
         lcd.setFont(u8g2_font_t0_12b_tr);
183
-        lcd.drawStr(0, 14 * 2 + 8 + 12 * 1, "Software Version " FIRMWARE_VERSION);
184
-        lcd.drawStr(0, 14 * 2 + 8 + 12 * 2, "made by: xythobuz.de");
213
+
214
+        s = F("Software Version " FIRMWARE_VERSION);
215
+        lcd.drawStr(0, 14 * 2 + 8 + 12 * 1, s.c_str());
216
+
217
+        s = F("made by: xythobuz.de");
218
+        lcd.drawStr(0, 14 * 2 + 8 + 12 * 2, s.c_str());
185
     } while (lcd.nextPage());
219
     } while (lcd.nextPage());
186
 #endif // USE_FULL_GRAPHIC_LCD
220
 #endif // USE_FULL_GRAPHIC_LCD
187
 }
221
 }
269
 
303
 
270
 void lcd_set_menu_text(int line, const char *t) {
304
 void lcd_set_menu_text(int line, const char *t) {
271
     Serial.print(line);
305
     Serial.print(line);
272
-    Serial.print(": ");
306
+    Serial.print(F(": "));
273
     Serial.println(t);
307
     Serial.println(t);
274
 
308
 
275
     text_mode = lcd_mode_menu;
309
     text_mode = lcd_mode_menu;

+ 212
- 17
src/statemachine.cpp View File

51
     }
51
     }
52
 
52
 
53
     lcd_clear();
53
     lcd_clear();
54
-    lcd_set_heading(heading);
55
-    lcd_set_text(text);
54
+    if (heading != NULL) {
55
+        lcd_set_heading(heading);
56
+    } else if (getTitle() != NULL) {
57
+        lcd_set_heading(getTitle());
58
+    }
59
+    if (text != NULL) {
60
+        lcd_set_text(text);
61
+    }
56
 }
62
 }
57
 
63
 
58
 void StateText::inState(struct StateMachineInput smi) {
64
 void StateText::inState(struct StateMachineInput smi) {
95
     }
101
     }
96
 
102
 
97
     for (int i = menuOff; (i < menuOff + lcd_text_lines()) && (i < size); i++) {
103
     for (int i = menuOff; (i < menuOff + lcd_text_lines()) && (i < size); i++) {
98
-        String s = "";
104
+        String s;
99
         if (i == menuPos) {
105
         if (i == menuPos) {
100
-            s += "> ";
106
+            s = F("> ");
101
         } else {
107
         } else {
102
-            s += "  ";
108
+            s = F("  ");
103
         }
109
         }
104
         if (i == children.size()) {
110
         if (i == children.size()) {
105
             s += getParent()->getTitle();
111
             s += getParent()->getTitle();
154
 StateDynamicMenu::StateDynamicMenu(State *_parent) : State(_parent) {
160
 StateDynamicMenu::StateDynamicMenu(State *_parent) : State(_parent) {
155
     menuPos = 0;
161
     menuPos = 0;
156
     menuOff = 0;
162
     menuOff = 0;
163
+    prefix = "";
164
+    countFunc = NULL;
165
+    getFunc = NULL;
166
+    callFunc = NULL;
157
 }
167
 }
158
 
168
 
159
 void StateDynamicMenu::dataCount(CountFuncPtr count) {
169
 void StateDynamicMenu::dataCount(CountFuncPtr count) {
173
     lcd_set_heading(getTitle());
183
     lcd_set_heading(getTitle());
174
 
184
 
175
     for (int i = menuOff; (i < menuOff + lcd_text_lines()) && (i < count + 1); i++) {
185
     for (int i = menuOff; (i < menuOff + lcd_text_lines()) && (i < count + 1); i++) {
176
-        String s = "";
186
+        String s;
177
         if (i == menuPos) {
187
         if (i == menuPos) {
178
-            s += "> ";
188
+            s = F("> ");
179
         } else {
189
         } else {
180
-            s += "  ";
190
+            s = F("  ");
181
         }
191
         }
182
         if (i == count) {
192
         if (i == count) {
183
             s += getParent()->getTitle();
193
             s += getParent()->getTitle();
188
     }
198
     }
189
 }
199
 }
190
 
200
 
201
+void StateDynamicMenu::setPrefix(String pre) {
202
+    prefix = pre;
203
+}
204
+
191
 void StateDynamicMenu::enterState(void) {
205
 void StateDynamicMenu::enterState(void) {
192
     // cache all entries on entering state
206
     // cache all entries on entering state
193
     if (countFunc != NULL) {
207
     if (countFunc != NULL) {
198
     contents.clear();
212
     contents.clear();
199
     for (int i = 0; i < count; i++) {
213
     for (int i = 0; i < count; i++) {
200
         if (getFunc != NULL) {
214
         if (getFunc != NULL) {
201
-            contents.push_back(getFunc(i));
215
+            contents.push_back(prefix + String(getFunc(i)));
202
         } else {
216
         } else {
203
-            contents.push_back("no get func");
217
+            contents.push_back(prefix + String(i + 1));
204
         }
218
         }
205
     }
219
     }
206
 
220
 
249
 StateValue<T>::StateValue(State *_parent, T &_value, T _min, T _max) : State(_parent), value(_value) {
263
 StateValue<T>::StateValue(State *_parent, T &_value, T _min, T _max) : State(_parent), value(_value) {
250
     min = _min;
264
     min = _min;
251
     max = _max;
265
     max = _max;
266
+    heading = NULL;
267
+    text = NULL;
268
+    onEnterFunc = NULL;
252
     updateFunc = NULL;
269
     updateFunc = NULL;
270
+    updateLiveFunc = NULL;
271
+}
272
+
273
+template <typename T>
274
+void StateValue<T>::setHeading(const char *_heading) {
275
+    heading = _heading;
276
+}
277
+
278
+template <typename T>
279
+void StateValue<T>::setText(const char *_text) {
280
+    text = _text;
281
+}
282
+
283
+template <typename T>
284
+void StateValue<T>::onEnter(EnterFuncPtr func) {
285
+    onEnterFunc = func;
253
 }
286
 }
254
 
287
 
255
 template <typename T>
288
 template <typename T>
258
 }
291
 }
259
 
292
 
260
 template <typename T>
293
 template <typename T>
294
+void StateValue<T>::onLiveUpdate(UpdateFuncPtr func) {
295
+    updateLiveFunc = func;
296
+}
297
+
298
+template <typename T>
261
 void StateValue<T>::display(void) {
299
 void StateValue<T>::display(void) {
262
-    String s = String(min) + " .. " + String(value) + " .. " + String(max);
300
+    lcd_clear();
301
+    if (heading == NULL) {
302
+        lcd_set_heading(getTitle());
303
+    } else {
304
+        lcd_set_heading(heading);
305
+    }
306
+
307
+    String s = String(min) + F(" .. ") + String(value) + F(" .. ") + String(max);
308
+
309
+    if (text != NULL) {
310
+        s = text + String(F("\n")) + s;
311
+    }
312
+
263
     lcd_set_text(s.c_str());
313
     lcd_set_text(s.c_str());
264
 }
314
 }
265
 
315
 
316
+
266
 template <typename T>
317
 template <typename T>
267
 void StateValue<T>::enterState(void) {
318
 void StateValue<T>::enterState(void) {
268
-    lcd_clear();
269
-    lcd_set_heading(getTitle());
270
-
319
+    if (onEnterFunc != NULL) {
320
+        onEnterFunc();
321
+    }
271
     display();
322
     display();
272
 }
323
 }
273
 
324
 
281
         if (value > max) {
332
         if (value > max) {
282
             value = max;
333
             value = max;
283
         }
334
         }
335
+        if (updateLiveFunc != NULL) {
336
+            updateLiveFunc(value);
337
+        }
284
         display();
338
         display();
285
     }
339
     }
286
     if (smi.click) {
340
     if (smi.click) {
291
     }
345
     }
292
 }
346
 }
293
 
347
 
348
+template class StateValue<int>;
294
 template class StateValue<float>;
349
 template class StateValue<float>;
295
 
350
 
296
 // --------------------------------------
351
 // --------------------------------------
297
 
352
 
298
 template <typename T, size_t N>
353
 template <typename T, size_t N>
354
+StateValues<T, N>::StateValues(State *_parent) : State(_parent) {
355
+    heading = NULL;
356
+    updateFunc = NULL;
357
+    updateLiveFunc = NULL;
358
+    pos = 0;
359
+    editing = false;
360
+}
361
+
362
+template <typename T, size_t N>
363
+void StateValues<T, N>::setData(size_t index, const char *name, T *value, T min, T max) {
364
+    if (index >= N) {
365
+        return;
366
+    }
367
+    values[index] = value;
368
+    mins[index] = min;
369
+    maxs[index] = max;
370
+    texts[index] = name;
371
+}
372
+
373
+template <typename T, size_t N>
374
+void StateValues<T, N>::setHeading(const char *_heading) {
375
+    heading = _heading;
376
+}
377
+
378
+template <typename T, size_t N>
379
+void StateValues<T, N>::onUpdate(UpdateFuncPtr func) {
380
+    updateFunc = func;
381
+}
382
+
383
+template <typename T, size_t N>
384
+void StateValues<T, N>::onLiveUpdate(UpdateFuncPtr func) {
385
+    updateLiveFunc = func;
386
+}
387
+
388
+template <typename T, size_t N>
389
+void StateValues<T, N>::display(void) {
390
+    lcd_clear();
391
+
392
+    if (heading == NULL) {
393
+        lcd_set_heading(getTitle());
394
+    } else {
395
+        lcd_set_heading(heading);
396
+    }
397
+
398
+    for (size_t i = 0; i < (N + 1); i++) {
399
+        String s;
400
+
401
+        if (i == pos) {
402
+            if (editing) {
403
+                s = F("# ");
404
+            } else {
405
+                s = F("> ");
406
+            }
407
+        } else {
408
+            s = F("  ");
409
+        }
410
+
411
+        if (i < N) {
412
+            s += texts[i] + String(*(values[i])) + F(" (") + String(mins[i]) + F("/") + String(maxs[i]) + F(")");
413
+        } else {
414
+            if (getChild() != NULL) {
415
+                s += F("Continue");
416
+            } else {
417
+                s += F("Done");
418
+            }
419
+        }
420
+
421
+        lcd_set_menu_text(i, s.c_str());
422
+    }
423
+}
424
+
425
+template <typename T, size_t N>
426
+void StateValues<T, N>::enterState(void) {
427
+    pos = 0;
428
+    display();
429
+}
430
+
431
+template <typename T, size_t N>
432
+void StateValues<T, N>::inState(StateMachineInput smi) {
433
+    if (editing) {
434
+        if (smi.encoder != 0) {
435
+            *(values[pos]) -= smi.encoder;
436
+            if (*(values[pos]) < mins[pos]) {
437
+                *(values[pos]) = mins[pos];
438
+            }
439
+            if (*(values[pos]) > maxs[pos]) {
440
+                *(values[pos]) = maxs[pos];
441
+            }
442
+            if (updateLiveFunc != NULL) {
443
+                updateLiveFunc(pos, *(values[pos]));
444
+            }
445
+            display();
446
+        }
447
+        if (smi.click) {
448
+            editing = false;
449
+            display();
450
+        }
451
+    } else {
452
+        if (smi.encoder != 0) {
453
+            int tmp = pos;
454
+            tmp -= smi.encoder;
455
+
456
+            while (tmp < 0) {
457
+                tmp += N + 1;
458
+            }
459
+
460
+            while (tmp >= (N + 1)) {
461
+                tmp -= N + 1;
462
+            }
463
+
464
+            pos = tmp;
465
+
466
+            display();
467
+        }
468
+        if (smi.click) {
469
+            if (pos < N) {
470
+                editing = true;
471
+                display();
472
+            } else {
473
+                if (updateFunc != NULL) {
474
+                    for (size_t i = 0; i < N; i++) {
475
+                        updateFunc(i, *(values[i]));
476
+                    }
477
+                }
478
+                if (getChild() != NULL) {
479
+                    states_go_to(getChild());
480
+                } else if (getParent() != NULL) {
481
+                    states_go_to(getParent());
482
+                }
483
+            }
484
+        }
485
+    }
486
+}
487
+
488
+template class StateValues<uint8_t, 2>;
489
+template class StateValues<float, 2>;
490
+
491
+// --------------------------------------
492
+
493
+template <typename T, size_t N>
299
 void array_print(Array<T, N> *arr) {
494
 void array_print(Array<T, N> *arr) {
300
-    Serial.print("Array length: ");
495
+    Serial.print(F("Array length: "));
301
     Serial.print(arr->size());
496
     Serial.print(arr->size());
302
-    Serial.println(" contents:");
497
+    Serial.println(F(" contents:"));
303
     for (int i = 0; i < arr->size(); i++) {
498
     for (int i = 0; i < arr->size(); i++) {
304
         Serial.print(i);
499
         Serial.print(i);
305
-        Serial.print(": ");
500
+        Serial.print(F(": "));
306
         Serial.println(arr->at(i)->getTitle());
501
         Serial.println(arr->at(i)->getTitle());
307
     }
502
     }
308
 }
503
 }

+ 125
- 34
src/states.cpp View File

17
 
17
 
18
 StateMenu sm_auto = StateMenu(&sm_menu);
18
 StateMenu sm_auto = StateMenu(&sm_menu);
19
 StateDynamicMenu sm_presets = StateDynamicMenu(&sm_auto);
19
 StateDynamicMenu sm_presets = StateDynamicMenu(&sm_auto);
20
+// TODO dispense
21
+
20
 StateText sm_new_preset = StateText(&sm_auto);
22
 StateText sm_new_preset = StateText(&sm_auto);
23
+StateText sm_wiz_move = StateText(&sm_new_preset);
24
+StateValues<uint8_t, 2> sm_wiz_count = StateValues<uint8_t, 2>(&sm_wiz_move);
25
+StateValues<float, 2> sm_wiz_first_pos = StateValues<float, 2>(&sm_wiz_count);
26
+StateValues<float, 2> sm_wiz_last_pos = StateValues<float, 2>(&sm_wiz_first_pos);
27
+// TODO move z above container
28
+// TODO move z inside container
29
+// TODO move to extruder depth
30
+StateText sm_new_preset_done = StateText(&sm_wiz_last_pos);
31
+
21
 StateDynamicMenu sm_mod_preset = StateDynamicMenu(&sm_auto);
32
 StateDynamicMenu sm_mod_preset = StateDynamicMenu(&sm_auto);
33
+// TODO modify
34
+
22
 StateDynamicMenu sm_del_preset = StateDynamicMenu(&sm_auto);
35
 StateDynamicMenu sm_del_preset = StateDynamicMenu(&sm_auto);
36
+// TODO delete
23
 
37
 
38
+float move_pos_x = 0.0, move_pos_y = 0.0, move_pos_z = 0.0, move_pos_e = 0.0;
24
 StateMenu sm_move = StateMenu(&sm_menu);
39
 StateMenu sm_move = StateMenu(&sm_menu);
25
-StateText sm_move_x = StateText(&sm_move);
26
-StateText sm_move_y = StateText(&sm_move);
27
-StateText sm_move_z = StateText(&sm_move);
28
-StateText sm_move_e = StateText(&sm_move);
40
+StateValue<float> sm_move_x = StateValue<float>(&sm_move, move_pos_x, X_AXIS_MIN, X_AXIS_MAX);
41
+StateValue<float> sm_move_y = StateValue<float>(&sm_move, move_pos_y, Y_AXIS_MIN, Y_AXIS_MAX);
42
+StateValue<float> sm_move_z = StateValue<float>(&sm_move, move_pos_z, Z_AXIS_MIN, Z_AXIS_MAX);
43
+StateValue<float> sm_move_e = StateValue<float>(&sm_move, move_pos_e, E_AXIS_MIN, E_AXIS_MAX);
29
 
44
 
30
 StateMenu sm_config = StateMenu(&sm_menu);
45
 StateMenu sm_config = StateMenu(&sm_menu);
31
 StateText sm_conf_load = StateText(&sm_config);
46
 StateText sm_conf_load = StateText(&sm_config);
41
 
56
 
42
 State *current_state = NULL;
57
 State *current_state = NULL;
43
 String strbuf;
58
 String strbuf;
59
+struct data_config_preset wizard_preset = { .count_x = 1, .count_y = 1 };
60
+float wizard_first_x = 0.0, wizard_first_y = 0.0;
61
+float wizard_last_x = 0.0, wizard_last_y = 0.0;
44
 
62
 
45
 void states_init(void) {
63
 void states_init(void) {
46
     // ----------------------------------
64
     // ----------------------------------
79
     sm_move.setTitle("Move Axis");
97
     sm_move.setTitle("Move Axis");
80
 
98
 
81
     sm_move_x.setTitle("X Axis");
99
     sm_move_x.setTitle("X Axis");
100
+    sm_move_x.onEnter([]() {
101
+        move_pos_x = steppers_pos_x();
102
+    });
103
+    sm_move_x.onLiveUpdate([](float v) {
104
+        steppers_move_x(v);
105
+    });
106
+
82
     sm_move_y.setTitle("Y Axis");
107
     sm_move_y.setTitle("Y Axis");
108
+    sm_move_y.onEnter([]() {
109
+        move_pos_y = steppers_pos_y();
110
+    });
111
+    sm_move_y.onLiveUpdate([](float v) {
112
+        steppers_move_y(v);
113
+    });
114
+
83
     sm_move_z.setTitle("Z Axis");
115
     sm_move_z.setTitle("Z Axis");
116
+    sm_move_z.onEnter([]() {
117
+        move_pos_z = steppers_pos_z();
118
+    });
119
+    sm_move_z.onLiveUpdate([](float v) {
120
+        steppers_move_z(v);
121
+    });
122
+
84
     sm_move_e.setTitle("E Axis");
123
     sm_move_e.setTitle("E Axis");
124
+    sm_move_e.onEnter([]() {
125
+        move_pos_e = steppers_pos_e();
126
+    });
127
+    sm_move_e.onLiveUpdate([](float v) {
128
+        steppers_move_e(v);
129
+    });
85
 
130
 
86
     // ----------------------------------
131
     // ----------------------------------
87
 
132
 
88
     sm_auto.setTitle("Filling Menu");
133
     sm_auto.setTitle("Filling Menu");
89
 
134
 
90
     sm_presets.setTitle("Use Preset");
135
     sm_presets.setTitle("Use Preset");
136
+    sm_presets.setPrefix("Preset ");
91
     sm_presets.dataCount([]() {
137
     sm_presets.dataCount([]() {
92
         return (int)data_preset_count();
138
         return (int)data_preset_count();
93
     });
139
     });
94
-    sm_presets.dataGet([](int i) {
95
-        // TODO can not build a name string here
96
-        // dynamically. need to have a name stored
97
-        // somewhere, in data/eeprom, for each preset
98
-        // that we can pass here
99
-        return "foo";
100
-    });
101
 
140
 
102
     sm_new_preset.setTitle("Add new Preset");
141
     sm_new_preset.setTitle("Add new Preset");
103
-    sm_new_preset.setHeading("Add new Preset");
104
-    sm_new_preset.onEnter([]() {
105
-        struct data_config_preset preset;
106
-        if (!data_preset_add(preset)) {
107
-            strbuf = String(data_eeprom_error()) + " Error adding preset!";
108
-            sm_new_preset.setText(strbuf.c_str());
142
+    sm_new_preset.setHeading("Preset Wizard");
143
+    sm_new_preset.setText("Moving axes into initial positions. Click to continue.");
144
+
145
+    sm_wiz_move.setTitle("Initial Movement");
146
+    sm_wiz_move.onEnter([]() {
147
+        //steppers_start_homing();
148
+    });
149
+    sm_wiz_move.whenIn([](StateMachineInput smi) {
150
+        //if (smi.motors_done) {
151
+        //    if (steppers_homed()) {
152
+                states_go_to(&sm_wiz_count);
153
+        //    }
154
+        //}
155
+
156
+        // TODO update text with current axis
157
+    });
158
+
159
+    sm_wiz_count.setTitle("Container Count");
160
+    sm_wiz_count.setData(0, "X: ", &wizard_preset.count_x, 1, 50);
161
+    sm_wiz_count.setData(1, "Y: ", &wizard_preset.count_y, 1, 100);
162
+
163
+    sm_wiz_first_pos.setTitle("First Container Position");
164
+    sm_wiz_first_pos.setData(0, "X: ", &wizard_first_x, X_AXIS_MIN, X_AXIS_MAX);
165
+    sm_wiz_first_pos.setData(1, "Y: ", &wizard_first_y, Y_AXIS_MIN, Y_AXIS_MAX);
166
+    sm_wiz_first_pos.onLiveUpdate([](size_t i, float v) {
167
+        if (i == 0) {
168
+            steppers_move_x(v);
169
+        } else {
170
+            steppers_move_y(v);
171
+        }
172
+    });
173
+    sm_wiz_first_pos.onUpdate([](size_t i, float v) {
174
+        if (i == 0) {
175
+            wizard_preset.offset_x = v;
176
+        } else {
177
+            wizard_preset.offset_y = v;
178
+        }
179
+    });
180
+
181
+    sm_wiz_last_pos.setTitle("Last Container Position");
182
+    sm_wiz_last_pos.setData(0, "X: ", &wizard_last_x, X_AXIS_MIN, X_AXIS_MAX);
183
+    sm_wiz_last_pos.setData(1, "Y: ", &wizard_last_y, Y_AXIS_MIN, Y_AXIS_MAX);
184
+    sm_wiz_last_pos.onLiveUpdate([](size_t i, float v) {
185
+        if (i == 0) {
186
+            steppers_move_x(v);
109
         } else {
187
         } else {
110
-            strbuf = "Preset " + String(data_preset_count()) + " has been added!";
111
-            sm_new_preset.setText(strbuf.c_str());
188
+            steppers_move_y(v);
189
+        }
190
+    });
191
+    sm_wiz_last_pos.onUpdate([](size_t i, float v) {
192
+        if (i == 0) {
193
+            wizard_preset.distance_x = (v - wizard_preset.offset_x) / wizard_preset.count_x;
194
+        } else {
195
+            wizard_preset.distance_y = (v - wizard_preset.offset_y) / wizard_preset.count_y;
196
+        }
197
+    });
198
+
199
+    sm_new_preset_done.setTitle("Preset Wizard Done");
200
+    sm_new_preset_done.setHeading("Preset Wizard Done");
201
+    sm_new_preset_done.onEnter([]() {
202
+        if (!data_preset_add(wizard_preset)) {
203
+            strbuf = String(data_eeprom_error()) + F(" Error adding preset!");
204
+            sm_new_preset_done.setText(strbuf.c_str());
205
+        } else {
206
+            strbuf = F("Preset ");
207
+            strbuf += String(data_preset_count());
208
+            strbuf += F(" has been added! Don't forget to store config to EEPROM to keep it.");
209
+            sm_new_preset_done.setText(strbuf.c_str());
210
+        }
211
+    });
212
+    sm_new_preset_done.whenIn([](StateMachineInput smi) {
213
+        if (smi.click) {
214
+            states_go_to(&sm_auto);
112
         }
215
         }
113
     });
216
     });
114
 
217
 
115
     sm_mod_preset.setTitle("Modify Preset");
218
     sm_mod_preset.setTitle("Modify Preset");
219
+    sm_mod_preset.setPrefix("Preset ");
116
     sm_mod_preset.dataCount([]() {
220
     sm_mod_preset.dataCount([]() {
117
         return (int)data_preset_count();
221
         return (int)data_preset_count();
118
     });
222
     });
119
-    sm_mod_preset.dataGet([](int i) {
120
-        // TODO can not build a name string here
121
-        // dynamically. need to have a name stored
122
-        // somewhere, in data/eeprom, for each preset
123
-        // that we can pass here
124
-        return "foo";
125
-    });
126
 
223
 
127
     sm_del_preset.setTitle("Delete Preset");
224
     sm_del_preset.setTitle("Delete Preset");
225
+    sm_del_preset.setPrefix("Preset ");
128
     sm_del_preset.dataCount([]() {
226
     sm_del_preset.dataCount([]() {
129
         return (int)data_preset_count();
227
         return (int)data_preset_count();
130
     });
228
     });
131
-    sm_del_preset.dataGet([](int i) {
132
-        // TODO can not build a name string here
133
-        // dynamically. need to have a name stored
134
-        // somewhere, in data/eeprom, for each preset
135
-        // that we can pass here
136
-        return "foo";
137
-    });
138
 
229
 
139
     // ----------------------------------
230
     // ----------------------------------
140
 
231
 
211
 
302
 
212
     if (smi.kill) {
303
     if (smi.kill) {
213
         steppers_kill();
304
         steppers_kill();
214
-        Serial.println("Kill button pressed!");
305
+        Serial.println(F("Kill button pressed!"));
215
         blocking_beep(KILL_BEEP_TIME, KILL_BEEP_FREQ, KILL_BEEP_REPEAT - 1);
306
         blocking_beep(KILL_BEEP_TIME, KILL_BEEP_FREQ, KILL_BEEP_REPEAT - 1);
216
         states_go_to(&sm_ask_homing);
307
         states_go_to(&sm_ask_homing);
217
         return;
308
         return;

+ 24
- 4
src/steppers.cpp View File

274
     return 0;
274
     return 0;
275
 }
275
 }
276
 
276
 
277
-int steppers_move_x(long pos) {
277
+int steppers_move_x(float pos) {
278
     Serial.print(F("Moving X to "));
278
     Serial.print(F("Moving X to "));
279
     Serial.print(pos);
279
     Serial.print(pos);
280
     Serial.print(F(" mm ("));
280
     Serial.print(F(" mm ("));
294
     return steppers_move_axis(stepper_x, pos * XY_STEPS_PER_MM * X_AXIS_MOVEMENT_DIR);
294
     return steppers_move_axis(stepper_x, pos * XY_STEPS_PER_MM * X_AXIS_MOVEMENT_DIR);
295
 }
295
 }
296
 
296
 
297
-int steppers_move_y(long pos) {
297
+int steppers_move_y(float pos) {
298
     Serial.print(F("Moving Y to "));
298
     Serial.print(F("Moving Y to "));
299
     Serial.print(pos);
299
     Serial.print(pos);
300
     Serial.print(F(" mm ("));
300
     Serial.print(F(" mm ("));
314
     return steppers_move_axis(stepper_y, pos * XY_STEPS_PER_MM * Y_AXIS_MOVEMENT_DIR);
314
     return steppers_move_axis(stepper_y, pos * XY_STEPS_PER_MM * Y_AXIS_MOVEMENT_DIR);
315
 }
315
 }
316
 
316
 
317
-int steppers_move_z(long pos) {
317
+int steppers_move_z(float pos) {
318
     Serial.print(F("Moving Z to "));
318
     Serial.print(F("Moving Z to "));
319
     Serial.print(pos);
319
     Serial.print(pos);
320
     Serial.print(F(" mm ("));
320
     Serial.print(F(" mm ("));
334
     return steppers_move_axis(stepper_z, pos * Z_STEPS_PER_MM * Z_AXIS_MOVEMENT_DIR);
334
     return steppers_move_axis(stepper_z, pos * Z_STEPS_PER_MM * Z_AXIS_MOVEMENT_DIR);
335
 }
335
 }
336
 
336
 
337
-int steppers_move_e(long pos) {
337
+int steppers_move_e(float pos) {
338
     Serial.print(F("Moving E to "));
338
     Serial.print(F("Moving E to "));
339
     Serial.print(pos);
339
     Serial.print(pos);
340
     Serial.print(F(" mm ("));
340
     Serial.print(F(" mm ("));
356
     return steppers_move_axis(stepper_e, E_MM_TO_PERCENT(pos) * E_STEPS_PER_PERCENT * E_AXIS_MOVEMENT_DIR);
356
     return steppers_move_axis(stepper_e, E_MM_TO_PERCENT(pos) * E_STEPS_PER_PERCENT * E_AXIS_MOVEMENT_DIR);
357
 }
357
 }
358
 
358
 
359
+float steppers_pos_x(void) {
360
+    float v = stepper_x.targetPosition();
361
+    return v / XY_STEPS_PER_MM / X_AXIS_MOVEMENT_DIR;
362
+}
363
+
364
+float steppers_pos_y(void) {
365
+    float v = stepper_y.targetPosition();
366
+    return v / XY_STEPS_PER_MM / Y_AXIS_MOVEMENT_DIR;
367
+}
368
+
369
+float steppers_pos_z(void) {
370
+    float v = stepper_z.targetPosition();
371
+    return v / Z_STEPS_PER_MM / Z_AXIS_MOVEMENT_DIR;
372
+}
373
+
374
+float steppers_pos_e(void) {
375
+    float v = stepper_e.targetPosition();
376
+    return E_PERCENT_TO_MM(v / E_STEPS_PER_PERCENT / E_AXIS_MOVEMENT_DIR);
377
+}
378
+
359
 void steppers_kill(void) {
379
 void steppers_kill(void) {
360
     stepper_x.setCurrentPosition(0);
380
     stepper_x.setCurrentPosition(0);
361
     stepper_y.setCurrentPosition(0);
381
     stepper_y.setCurrentPosition(0);

Loading…
Cancel
Save