chiark / gitweb /
HOMEAXIS: make into a function
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 8 Aug 2012 17:30:34 +0000 (18:30 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 9 Aug 2012 18:20:23 +0000 (19:20 +0100)
Replace the large macro HOMEAXIS with a function.  This avoids the
compiler generating three copies of largely identical code.  The
saving is 704 bytes of program memory.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Marlin/Marlin.pde

index 9bcbf216b639001ed636e7019904d4dafb442db6..b159cb96fd192d1252a8a9c7107389c2727c1dc2 100644 (file)
@@ -552,9 +552,15 @@ bool code_seen(char code)
   return (strchr_pointer != NULL);  //Return True if a character was found
 }
 
-static const PROGMEM float base_min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
-static const PROGMEM float base_max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
-static const PROGMEM float base_home_pos[3] = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
+#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
+static const PROGMEM type array[3] = { X_##CONFIG, Y_##CONFIG, Z_##CONFIG };
+
+XYZ_CONSTS_FROM_CONFIG(float, base_min_pos,    MIN_POS);
+XYZ_CONSTS_FROM_CONFIG(float, base_max_pos,    MAX_POS);
+XYZ_CONSTS_FROM_CONFIG(float, base_home_pos,   HOME_POS);
+XYZ_CONSTS_FROM_CONFIG(float, max_length,      MAX_LENGTH);
+XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
+XYZ_CONSTS_FROM_CONFIG(signed char, home_dir,  HOME_DIR);
 
 static void axis_is_at_home(int axis) {
   current_position[axis] = base_home_pos[axis] + add_homeing[axis];
@@ -564,32 +570,41 @@ static void axis_is_at_home(int axis) {
   else max_pos[axis] += add_homeing[axis];
 }
 
-#define HOMEAXIS(LETTER) \
-  if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
-    { \
-    current_position[LETTER##_AXIS] = 0; \
-    plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
-    destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
-    feedrate = homing_feedrate[LETTER##_AXIS]; \
-    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
-    st_synchronize();\
-    \
-    current_position[LETTER##_AXIS] = 0;\
-    plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
-    destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
-    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
-    st_synchronize();\
-    \
-    destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
-    feedrate = homing_feedrate[LETTER##_AXIS]/2 ;  \
-    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
-    st_synchronize();\
-    \
-    axis_is_at_home(LETTER##_AXIS);                                    \
-    destination[LETTER##_AXIS] = current_position[LETTER##_AXIS]; \
-    feedrate = 0.0;\
-    endstops_hit_on_purpose();\
+static void homeaxis(int axis) {
+#define HOMEAXIS_DO_BIT(LETTER) \
+  ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1) ? 1 << LETTER##_AXIS : 0)
+
+  static PROGMEM const unsigned char doaxis =
+    HOMEAXIS_DO_BIT(X) | 
+    HOMEAXIS_DO_BIT(Y) |
+    HOMEAXIS_DO_BIT(Z);
+
+  if (doaxis & (1<<axis)) {
+    current_position[axis] = 0;
+    plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
+    destination[axis] = 1.5 * max_length[axis] * home_dir[axis];
+    feedrate = homing_feedrate[axis];
+    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
+    st_synchronize();
+   
+    current_position[axis] = 0;
+    plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
+    destination[axis] = -home_retract_mm[axis] * home_dir[axis];
+    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
+    st_synchronize();
+   
+    destination[axis] = 2*home_retract_mm[axis] * home_dir[axis];
+    feedrate = homing_feedrate[axis]/2 ; 
+    plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
+    st_synchronize();
+   
+    axis_is_at_home(axis);                                     
+    destination[axis] = current_position[axis];
+    feedrate = 0.0;
+    endstops_hit_on_purpose();
   }
+}
+#define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
 
 void process_commands()
 {