44
44
#endif
45
45
46
46
#if ENABLED(FILAMENT_MOTION_SENSOR)
47
- #define FILAMENT_HAS_MOTION
48
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
49
- #define FILAMENT_HAS_SWITCH
50
- #endif
51
- #else
52
- #define FILAMENT_HAS_SWITCH
47
+ #define HAS_FILAMENT_MOTION 1
48
+ #endif
49
+ #if DISABLED(FILAMENT_MOTION_SENSOR) || ENABLED(FILAMENT_SWITCH_AND_MOTION)
50
+ #define HAS_FILAMENT_SWITCH 1
53
51
#endif
54
52
55
53
void event_filament_runout (const uint8_t extruder);
@@ -106,11 +104,11 @@ class TFilamentMonitor : public FilamentMonitorBase {
106
104
static void filament_present (const uint8_t extruder) {
107
105
response.filament_present (extruder);
108
106
}
109
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
110
- static void filament_motion_present (const uint8_t extruder) {
111
- response.filament_motion_present (extruder);
112
- }
113
- #endif
107
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
108
+ static void filament_motion_present (const uint8_t extruder) {
109
+ response.filament_motion_present (extruder);
110
+ }
111
+ #endif
114
112
115
113
#if HAS_FILAMENT_RUNOUT_DISTANCE
116
114
static float & runout_distance () { return response.runout_distance_mm ; }
@@ -184,71 +182,25 @@ class FilamentSensorBase {
184
182
static void filament_present (const uint8_t extruder) {
185
183
runout.filament_present (extruder); // ...which calls response.filament_present(extruder)
186
184
}
187
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
188
- static void filament_motion_present (const uint8_t extruder) {
189
- runout.filament_motion_present (extruder);
190
- }
191
- #endif
185
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
186
+ static void filament_motion_present (const uint8_t extruder) {
187
+ runout.filament_motion_present (extruder); // ...which calls response.filament_motion_present(extruder)
188
+ }
189
+ #endif
192
190
193
191
public:
194
192
static void setup () {
195
- #define _INIT_RUNOUT_PIN (P,S,U,D ) do { if (ENABLED (U)) SET_INPUT_PULLUP (P); else if (ENABLED (D)) SET_INPUT_PULLDOWN (P); else SET_INPUT (P); }while (0 )
193
+ #define _INIT_RUNOUT_PIN (P,S,U,D ) do { if (ENABLED (U)) SET_INPUT_PULLUP (P); else if (ENABLED (D)) SET_INPUT_PULLDOWN (P); else SET_INPUT (P); }while (0 );
196
194
#define INIT_RUNOUT_PIN (N ) _INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN, FIL_RUNOUT##N##_STATE, FIL_RUNOUT##N##_PULLUP, FIL_RUNOUT##N##_PULLDOWN)
197
- #if NUM_RUNOUT_SENSORS >= 1
198
- INIT_RUNOUT_PIN (1 );
199
- #endif
200
- #if NUM_RUNOUT_SENSORS >= 2
201
- INIT_RUNOUT_PIN (2 );
202
- #endif
203
- #if NUM_RUNOUT_SENSORS >= 3
204
- INIT_RUNOUT_PIN (3 );
205
- #endif
206
- #if NUM_RUNOUT_SENSORS >= 4
207
- INIT_RUNOUT_PIN (4 );
208
- #endif
209
- #if NUM_RUNOUT_SENSORS >= 5
210
- INIT_RUNOUT_PIN (5 );
211
- #endif
212
- #if NUM_RUNOUT_SENSORS >= 6
213
- INIT_RUNOUT_PIN (6 );
214
- #endif
215
- #if NUM_RUNOUT_SENSORS >= 7
216
- INIT_RUNOUT_PIN (7 );
217
- #endif
218
- #if NUM_RUNOUT_SENSORS >= 8
219
- INIT_RUNOUT_PIN (8 );
220
- #endif
195
+ REPEAT_1 (NUM_RUNOUT_SENSORS, INIT_RUNOUT_PIN);
221
196
#undef INIT_RUNOUT_PIN
222
197
223
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
198
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
224
199
#define INIT_MOTION_PIN (N ) _INIT_RUNOUT_PIN(FIL_MOTION##N##_PIN, FIL_MOTION##N##_STATE, FIL_MOTION##N##_PULLUP, FIL_MOTION##N##_PULLDOWN)
225
- #if NUM_MOTION_SENSORS >= 1
226
- INIT_MOTION_PIN (1 );
227
- #endif
228
- #if NUM_MOTION_SENSORS >= 2
229
- INIT_MOTION_PIN (2 );
230
- #endif
231
- #if NUM_MOTION_SENSORS >= 3
232
- INIT_MOTION_PIN (3 );
233
- #endif
234
- #if NUM_MOTION_SENSORS >= 4
235
- INIT_MOTION_PIN (4 );
236
- #endif
237
- #if NUM_MOTION_SENSORS >= 5
238
- INIT_MOTION_PIN (5 );
239
- #endif
240
- #if NUM_MOTION_SENSORS >= 6
241
- INIT_MOTION_PIN (6 );
242
- #endif
243
- #if NUM_MOTION_SENSORS >= 7
244
- INIT_MOTION_PIN (7 );
245
- #endif
246
- #if NUM_MOTION_SENSORS >= 8
247
- INIT_MOTION_PIN (8 );
248
- #endif
200
+ REPEAT_1 (NUM_MOTION_SENSORS, INIT_MOTION_PIN);
201
+ #undef INIT_MOTION_PIN
249
202
#endif
250
203
#undef _INIT_RUNOUT_PIN
251
- #undef INIT_MOTION_PIN
252
204
}
253
205
254
206
// Return a bitmask of runout pin states
@@ -260,35 +212,12 @@ class FilamentSensorBase {
260
212
261
213
// Return a bitmask of runout flag states (1 bits always indicates runout)
262
214
static uint8_t poll_runout_states () {
263
- return poll_runout_pins () ^ uint8_t (0
264
- #if NUM_RUNOUT_SENSORS >= 1
265
- | (FIL_RUNOUT1_STATE ? 0 : _BV (1 - 1 ))
266
- #endif
267
- #if NUM_RUNOUT_SENSORS >= 2
268
- | (FIL_RUNOUT2_STATE ? 0 : _BV (2 - 1 ))
269
- #endif
270
- #if NUM_RUNOUT_SENSORS >= 3
271
- | (FIL_RUNOUT3_STATE ? 0 : _BV (3 - 1 ))
272
- #endif
273
- #if NUM_RUNOUT_SENSORS >= 4
274
- | (FIL_RUNOUT4_STATE ? 0 : _BV (4 - 1 ))
275
- #endif
276
- #if NUM_RUNOUT_SENSORS >= 5
277
- | (FIL_RUNOUT5_STATE ? 0 : _BV (5 - 1 ))
278
- #endif
279
- #if NUM_RUNOUT_SENSORS >= 6
280
- | (FIL_RUNOUT6_STATE ? 0 : _BV (6 - 1 ))
281
- #endif
282
- #if NUM_RUNOUT_SENSORS >= 7
283
- | (FIL_RUNOUT7_STATE ? 0 : _BV (7 - 1 ))
284
- #endif
285
- #if NUM_RUNOUT_SENSORS >= 8
286
- | (FIL_RUNOUT8_STATE ? 0 : _BV (8 - 1 ))
287
- #endif
288
- );
215
+ #define _OR_RUNOUT (N ) | (FIL_RUNOUT##N##_STATE ? 0 : _BV(N - 1 ))
216
+ return poll_runout_pins () ^ uint8_t (0 REPEAT_1 (NUM_RUNOUT_SENSORS, _OR_RUNOUT));
217
+ #undef _OR_RUNOUT
289
218
}
290
219
291
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
220
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
292
221
// Return a bitmask of motion pin states
293
222
static uint8_t poll_motion_pins () {
294
223
#define _OR_MOTION (N ) | (READ(FIL_MOTION##N##_PIN) ? _BV((N) - 1 ) : 0 )
@@ -298,37 +227,14 @@ class FilamentSensorBase {
298
227
299
228
// Return a bitmask of motion flag states (1 bits always indicates runout)
300
229
static uint8_t poll_motion_states () {
301
- return poll_motion_pins () ^ uint8_t (0
302
- #if NUM_MOTION_SENSORS >= 1
303
- | (FIL_MOTION1_STATE ? 0 : _BV (1 - 1 ))
304
- #endif
305
- #if NUM_MOTION_SENSORS >= 2
306
- | (FIL_MOTION2_STATE ? 0 : _BV (2 - 1 ))
307
- #endif
308
- #if NUM_MOTION_SENSORS >= 3
309
- | (FIL_MOTION3_STATE ? 0 : _BV (3 - 1 ))
310
- #endif
311
- #if NUM_MOTION_SENSORS >= 4
312
- | (FIL_MOTION4_STATE ? 0 : _BV (4 - 1 ))
313
- #endif
314
- #if NUM_MOTION_SENSORS >= 5
315
- | (FIL_MOTION5_STATE ? 0 : _BV (5 - 1 ))
316
- #endif
317
- #if NUM_MOTION_SENSORS >= 6
318
- | (FIL_MOTION6_STATE ? 0 : _BV (6 - 1 ))
319
- #endif
320
- #if NUM_MOTION_SENSORS >= 7
321
- | (FIL_MOTION7_STATE ? 0 : _BV (7 - 1 ))
322
- #endif
323
- #if NUM_MOTION_SENSORS >= 8
324
- | (FIL_MOTION8_STATE ? 0 : _BV (8 - 1 ))
325
- #endif
326
- );
230
+ #define _OR_MOTION (N ) | (FIL_MOTION##N##_STATE ? 0 : _BV(N - 1 ))
231
+ return poll_motion_pins () ^ uint8_t (0 REPEAT_1 (NUM_MOTION_SENSORS, _OR_MOTION));
232
+ #undef _OR_MOTION
327
233
}
328
234
#endif
329
235
};
330
236
331
- #if ENABLED(FILAMENT_HAS_MOTION)
237
+ #if HAS_FILAMENT_MOTION
332
238
333
239
/* *
334
240
* This sensor uses a magnetic encoder disc and a Hall effect
@@ -342,14 +248,14 @@ class FilamentSensorBase {
342
248
343
249
static void poll_motion_sensor () {
344
250
static uint8_t old_state;
345
- const uint8_t new_state = TERN (FILAMENT_BOTH_SWITCH_AND_MOTION , poll_motion_pins, poll_runout_pins)(),
251
+ const uint8_t new_state = TERN (FILAMENT_SWITCH_AND_MOTION , poll_motion_pins, poll_runout_pins)(),
346
252
change = old_state ^ new_state;
347
253
old_state = new_state;
348
254
349
255
#if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
350
256
if (change) {
351
257
SERIAL_ECHOPGM (" Motion detected:" );
352
- LOOP_L_N (e, TERN (FILAMENT_BOTH_SWITCH_AND_MOTION , NUM_MOTION_SENSORS, NUM_RUNOUT_SENSORS))
258
+ LOOP_L_N (e, TERN (FILAMENT_SWITCH_AND_MOTION , NUM_MOTION_SENSORS, NUM_RUNOUT_SENSORS))
353
259
if (TEST (change, e)) SERIAL_CHAR (' ' , ' 0' + e);
354
260
SERIAL_EOL ();
355
261
}
@@ -363,7 +269,7 @@ class FilamentSensorBase {
363
269
// If the sensor wheel has moved since the last call to
364
270
// this method reset the runout counter for the extruder.
365
271
if (TEST (motion_detected, b->extruder ))
366
- TERN (FILAMENT_BOTH_SWITCH_AND_MOTION , filament_motion_present, filament_present)(b->extruder );
272
+ TERN (FILAMENT_SWITCH_AND_MOTION , filament_motion_present, filament_present)(b->extruder );
367
273
368
274
// Clear motion triggers for next block
369
275
motion_detected = 0 ;
@@ -372,9 +278,9 @@ class FilamentSensorBase {
372
278
static void run () { poll_motion_sensor (); }
373
279
};
374
280
375
- #endif // FILAMENT_HAS_MOTION
281
+ #endif // HAS_FILAMENT_MOTION
376
282
377
- #if ENABLED(FILAMENT_HAS_SWITCH)
283
+ #if HAS_FILAMENT_SWITCH
378
284
379
285
/* *
380
286
* This is a simple endstop switch in the path of the filament.
@@ -412,26 +318,26 @@ class FilamentSensorBase {
412
318
}
413
319
};
414
320
415
- #endif // FILAMENT_HAS_SWITCH
321
+ #endif // HAS_FILAMENT_SWITCH
416
322
417
323
/* *
418
324
* This is a simple endstop switch in the path of the filament.
419
325
* It can detect filament runout, but not stripouts or jams.
420
326
*/
421
327
class FilamentSensor : public FilamentSensorBase {
422
328
private:
423
- TERN_ (FILAMENT_HAS_MOTION , static FilamentSensorEncoder encoder_sensor);
424
- TERN_ (FILAMENT_HAS_SWITCH , static FilamentSensorSwitch switch_sensor);
329
+ TERN_ (HAS_FILAMENT_MOTION , static FilamentSensorEncoder encoder_sensor);
330
+ TERN_ (HAS_FILAMENT_SWITCH , static FilamentSensorSwitch switch_sensor);
425
331
426
332
public:
427
333
static void block_completed (const block_t * const b) {
428
- TERN_ (FILAMENT_HAS_MOTION , encoder_sensor.block_completed (b));
429
- TERN_ (FILAMENT_HAS_SWITCH , switch_sensor.block_completed (b));
334
+ TERN_ (HAS_FILAMENT_MOTION , encoder_sensor.block_completed (b));
335
+ TERN_ (HAS_FILAMENT_SWITCH , switch_sensor.block_completed (b));
430
336
}
431
337
432
338
static void run () {
433
- TERN_ (FILAMENT_HAS_MOTION , encoder_sensor.run ());
434
- TERN_ (FILAMENT_HAS_SWITCH , switch_sensor.run ());
339
+ TERN_ (HAS_FILAMENT_MOTION , encoder_sensor.run ());
340
+ TERN_ (HAS_FILAMENT_SWITCH , switch_sensor.run ());
435
341
}
436
342
};
437
343
@@ -442,9 +348,9 @@ class FilamentSensorBase {
442
348
443
349
typedef struct {
444
350
float runout[NUM_RUNOUT_SENSORS];
445
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
446
- float motion[NUM_MOTION_SENSORS];
447
- #endif
351
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
352
+ float motion[NUM_MOTION_SENSORS];
353
+ #endif
448
354
} countdown_t ;
449
355
450
356
// RunoutResponseDelayed triggers a runout event only if the length
@@ -459,9 +365,9 @@ class FilamentSensorBase {
459
365
460
366
static void reset () {
461
367
LOOP_L_N (i, NUM_RUNOUT_SENSORS) filament_present (i);
462
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
463
- LOOP_L_N (i, NUM_MOTION_SENSORS) filament_motion_present (i);
464
- #endif
368
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
369
+ LOOP_L_N (i, NUM_MOTION_SENSORS) filament_motion_present (i);
370
+ #endif
465
371
}
466
372
467
373
static void run () {
@@ -472,10 +378,10 @@ class FilamentSensorBase {
472
378
t = millis () + 1000UL ;
473
379
LOOP_L_N (i, NUM_RUNOUT_SENSORS)
474
380
SERIAL_ECHOF (i ? F (" , " ) : F (" Runout remaining mm: " ), mm_countdown.runout [i]);
475
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
476
- LOOP_L_N (i, NUM_MOTION_SENSORS)
477
- SERIAL_ECHOF (i ? F (" , " ) : F (" Motion remaining mm: " ), mm_countdown.motion [i]);
478
- #endif
381
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
382
+ LOOP_L_N (i, NUM_MOTION_SENSORS)
383
+ SERIAL_ECHOF (i ? F (" , " ) : F (" Motion remaining mm: " ), mm_countdown.motion [i]);
384
+ #endif
479
385
SERIAL_EOL ();
480
386
}
481
387
#endif
@@ -484,21 +390,21 @@ class FilamentSensorBase {
484
390
static uint8_t has_run_out () {
485
391
uint8_t runout_flags = 0 ;
486
392
LOOP_L_N (i, NUM_RUNOUT_SENSORS) if (mm_countdown.runout [i] < 0 ) SBI (runout_flags, i);
487
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
488
- LOOP_L_N (i, NUM_MOTION_SENSORS) if (mm_countdown.motion [i] < 0 ) SBI (runout_flags, i);
489
- #endif
393
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
394
+ LOOP_L_N (i, NUM_MOTION_SENSORS) if (mm_countdown.motion [i] < 0 ) SBI (runout_flags, i);
395
+ #endif
490
396
return runout_flags;
491
397
}
492
398
493
399
static void filament_present (const uint8_t extruder) {
494
400
mm_countdown.runout [extruder] = runout_distance_mm;
495
401
}
496
402
497
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
498
- static void filament_motion_present (const uint8_t extruder) {
499
- mm_countdown.motion [extruder] = runout_distance_mm;
500
- }
501
- #endif
403
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
404
+ static void filament_motion_present (const uint8_t extruder) {
405
+ mm_countdown.motion [extruder] = runout_distance_mm;
406
+ }
407
+ #endif
502
408
503
409
static void block_completed (const block_t * const b) {
504
410
if (b->steps .x || b->steps .y || b->steps .z || did_pause_print) { // Allow pause purge move to re-trigger runout state
@@ -508,10 +414,10 @@ class FilamentSensorBase {
508
414
float mm = (TEST (b->direction_bits , E_AXIS) ? -steps : steps) * planner.mm_per_step [E_AXIS_N (e)];
509
415
if (e < NUM_RUNOUT_SENSORS)
510
416
mm_countdown.runout [e] -= mm;
511
- #if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION )
512
- if (e < NUM_MOTION_SENSORS)
513
- mm_countdown.motion [e] -= mm;
514
- #endif
417
+ #if ENABLED(FILAMENT_SWITCH_AND_MOTION )
418
+ if (e < NUM_MOTION_SENSORS)
419
+ mm_countdown.motion [e] -= mm;
420
+ #endif
515
421
}
516
422
}
517
423
};
0 commit comments