Browse Source

Merge pull request #5951 from thinkyhead/rc_print_2d_array

Reduce code size (40b, or 166b with bilinear subdivision)
Scott Lahteine 8 years ago
parent
commit
1ef6ccd919
1 changed files with 26 additions and 34 deletions
  1. 26
    34
      Marlin/Marlin_main.cpp

+ 26
- 34
Marlin/Marlin_main.cpp View File

2432
   /**
2432
   /**
2433
    * Print calibration results for plotting or manual frame adjustment.
2433
    * Print calibration results for plotting or manual frame adjustment.
2434
    */
2434
    */
2435
-  static void print_bilinear_leveling_grid() {
2436
-    SERIAL_ECHOPGM("Bilinear Leveling Grid:\n ");
2437
-    for (uint8_t x = 0; x < ABL_GRID_MAX_POINTS_X; x++) {
2438
-      SERIAL_PROTOCOLPGM("    ");
2439
-      if (x < 10) SERIAL_PROTOCOLCHAR(' ');
2435
+  static void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, float (*fn)(const uint8_t, const uint8_t)) {
2436
+    for (uint8_t x = 0; x < sx; x++) {
2437
+      for (uint8_t i = 0; i < precision + 2 + (x < 10 ? 1 : 0); i++)
2438
+        SERIAL_PROTOCOLCHAR(' ');
2440
       SERIAL_PROTOCOL((int)x);
2439
       SERIAL_PROTOCOL((int)x);
2441
     }
2440
     }
2442
     SERIAL_EOL;
2441
     SERIAL_EOL;
2443
-    for (uint8_t y = 0; y < ABL_GRID_MAX_POINTS_Y; y++) {
2442
+    for (uint8_t y = 0; y < sy; y++) {
2444
       if (y < 10) SERIAL_PROTOCOLCHAR(' ');
2443
       if (y < 10) SERIAL_PROTOCOLCHAR(' ');
2445
       SERIAL_PROTOCOL((int)y);
2444
       SERIAL_PROTOCOL((int)y);
2446
-      for (uint8_t x = 0; x < ABL_GRID_MAX_POINTS_X; x++) {
2445
+      for (uint8_t x = 0; x < sx; x++) {
2447
         SERIAL_PROTOCOLCHAR(' ');
2446
         SERIAL_PROTOCOLCHAR(' ');
2448
-        float offset = bed_level_grid[x][y];
2447
+        float offset = fn(x, y);
2449
         if (offset != UNPROBED) {
2448
         if (offset != UNPROBED) {
2450
-          if (offset > 0) SERIAL_CHAR('+');
2451
-          SERIAL_PROTOCOL_F(offset, 2);
2449
+          if (offset >= 0) SERIAL_CHAR('+');
2450
+          SERIAL_PROTOCOL_F(offset, precision);
2452
         }
2451
         }
2453
         else
2452
         else
2454
-          SERIAL_PROTOCOLPGM(" ====");
2453
+          for (uint8_t i = 0; i < precision + 3; i++)
2454
+            SERIAL_PROTOCOLCHAR(i ? '=' : ' ');
2455
       }
2455
       }
2456
       SERIAL_EOL;
2456
       SERIAL_EOL;
2457
     }
2457
     }
2458
     SERIAL_EOL;
2458
     SERIAL_EOL;
2459
   }
2459
   }
2460
 
2460
 
2461
+  static void print_bilinear_leveling_grid() {
2462
+    SERIAL_ECHOLNPGM("Bilinear Leveling Grid:");
2463
+    print_2d_array(ABL_GRID_MAX_POINTS_X, ABL_GRID_MAX_POINTS_Y, 2,
2464
+      [](const uint8_t x, const uint8_t y) { return bed_level_grid[x][y]; }
2465
+    );
2466
+  }
2467
+
2461
   #if ENABLED(ABL_BILINEAR_SUBDIVISION)
2468
   #if ENABLED(ABL_BILINEAR_SUBDIVISION)
2469
+
2462
     #define ABL_GRID_POINTS_VIRT_X (ABL_GRID_MAX_POINTS_X - 1) * (BILINEAR_SUBDIVISIONS) + 1
2470
     #define ABL_GRID_POINTS_VIRT_X (ABL_GRID_MAX_POINTS_X - 1) * (BILINEAR_SUBDIVISIONS) + 1
2463
     #define ABL_GRID_POINTS_VIRT_Y (ABL_GRID_MAX_POINTS_Y - 1) * (BILINEAR_SUBDIVISIONS) + 1
2471
     #define ABL_GRID_POINTS_VIRT_Y (ABL_GRID_MAX_POINTS_Y - 1) * (BILINEAR_SUBDIVISIONS) + 1
2464
     #define ABL_TEMP_POINTS_X (ABL_GRID_MAX_POINTS_X + 2)
2472
     #define ABL_TEMP_POINTS_X (ABL_GRID_MAX_POINTS_X + 2)
2468
 
2476
 
2469
     static void bed_level_virt_print() {
2477
     static void bed_level_virt_print() {
2470
       SERIAL_ECHOLNPGM("Subdivided with CATMULL ROM Leveling Grid:");
2478
       SERIAL_ECHOLNPGM("Subdivided with CATMULL ROM Leveling Grid:");
2471
-      for (uint8_t x = 0; x < ABL_GRID_POINTS_VIRT_X; x++) {
2472
-        SERIAL_PROTOCOLPGM("       ");
2473
-        if (x < 10) SERIAL_PROTOCOLCHAR(' ');
2474
-        SERIAL_PROTOCOL((int)x);
2475
-      }
2476
-      SERIAL_EOL;
2477
-      for (uint8_t y = 0; y < ABL_GRID_POINTS_VIRT_Y; y++) {
2478
-        if (y < 10) SERIAL_PROTOCOLCHAR(' ');
2479
-        SERIAL_PROTOCOL((int)y);
2480
-        for (uint8_t x = 0; x < ABL_GRID_POINTS_VIRT_X; x++) {
2481
-          SERIAL_PROTOCOLCHAR(' ');
2482
-          float offset = bed_level_grid_virt[x][y];
2483
-          if (offset != UNPROBED) {
2484
-            if (offset >= 0) SERIAL_CHAR('+');
2485
-            SERIAL_PROTOCOL_F(offset, 5);
2486
-          }
2487
-          else
2488
-            SERIAL_PROTOCOLPGM(" ====");
2489
-        }
2490
-        SERIAL_EOL;
2491
-      }
2492
-      SERIAL_EOL;
2479
+      print_2d_array(ABL_GRID_POINTS_VIRT_X, ABL_GRID_POINTS_VIRT_Y, 5,
2480
+        [](const uint8_t x, const uint8_t y) { return bed_level_grid_virt[x][y]; }
2481
+      );
2493
     }
2482
     }
2483
+
2494
     #define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I))
2484
     #define LINEAR_EXTRAPOLATION(E, I) ((E) * 2 - (I))
2495
     float bed_level_virt_coord(const uint8_t x, const uint8_t y) {
2485
     float bed_level_virt_coord(const uint8_t x, const uint8_t y) {
2496
       uint8_t ep = 0, ip = 1;
2486
       uint8_t ep = 0, ip = 1;
2528
       }
2518
       }
2529
       return bed_level_grid[x - 1][y - 1];
2519
       return bed_level_grid[x - 1][y - 1];
2530
     }
2520
     }
2521
+
2531
     static float bed_level_virt_cmr(const float p[4], const uint8_t i, const float t) {
2522
     static float bed_level_virt_cmr(const float p[4], const uint8_t i, const float t) {
2532
       return (
2523
       return (
2533
           p[i-1] * -t * sq(1 - t)
2524
           p[i-1] * -t * sq(1 - t)
2536
         - p[i+2] * sq(t) * (1 - t)
2527
         - p[i+2] * sq(t) * (1 - t)
2537
       ) * 0.5;
2528
       ) * 0.5;
2538
     }
2529
     }
2530
+
2539
     static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const float &tx, const float &ty) {
2531
     static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const float &tx, const float &ty) {
2540
       float row[4], column[4];
2532
       float row[4], column[4];
2541
       for (uint8_t i = 0; i < 4; i++) {
2533
       for (uint8_t i = 0; i < 4; i++) {
2546
       }
2538
       }
2547
       return bed_level_virt_cmr(row, 1, tx);
2539
       return bed_level_virt_cmr(row, 1, tx);
2548
     }
2540
     }
2541
+
2549
     void bed_level_virt_interpolate() {
2542
     void bed_level_virt_interpolate() {
2550
       for (uint8_t y = 0; y < ABL_GRID_MAX_POINTS_Y; y++)
2543
       for (uint8_t y = 0; y < ABL_GRID_MAX_POINTS_Y; y++)
2551
         for (uint8_t x = 0; x < ABL_GRID_MAX_POINTS_X; x++)
2544
         for (uint8_t x = 0; x < ABL_GRID_MAX_POINTS_X; x++)
2565
   #endif // ABL_BILINEAR_SUBDIVISION
2558
   #endif // ABL_BILINEAR_SUBDIVISION
2566
 #endif // AUTO_BED_LEVELING_BILINEAR
2559
 #endif // AUTO_BED_LEVELING_BILINEAR
2567
 
2560
 
2568
-
2569
 /**
2561
 /**
2570
  * Home an individual linear axis
2562
  * Home an individual linear axis
2571
  */
2563
  */

Loading…
Cancel
Save