diff --git a/src/main.cpp b/src/main.cpp index b6e098b..772f6d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,10 +36,10 @@ #include "modules/light_sensor/light_sensor.h" #endif -Command registered_commands[MAX_COMMANDS]; +const Command * registered_commands[MAX_COMMANDS]; void (*registered_loop_functions[MAX_MODULES])(void); -uint16_t registered_commands_count = 0; +uint8_t registered_commands_count = 0; uint8_t registered_loop_fn_count = 0; const Command core_commands[] = { @@ -50,7 +50,7 @@ const Command core_commands[] = { void register_module(const Command commands[], void (*loop_fn)(void)) { for (int i = 0; CHECK_END_COMMAND(commands[i]); i++) - registered_commands[registered_commands_count++] = commands[i]; + registered_commands[registered_commands_count++] = &commands[i]; if (loop_fn) registered_loop_functions[registered_loop_fn_count++] = loop_fn; @@ -58,10 +58,10 @@ void register_module(const Command commands[], void (*loop_fn)(void)) void handle_command(String received_command) { - for (int i = 0; CHECK_END_COMMAND(registered_commands[i]); i++) + for (int i = 0; CHECK_END_COMMAND(*registered_commands[i]); i++) { - if (received_command.startsWith(registered_commands[i].command)) - return registered_commands[i].run(received_command.substring(registered_commands[i].command.length())); + if (received_command.startsWith(registered_commands[i]->command)) + return registered_commands[i]->run(received_command.substring(strlen(registered_commands[i]->command))); } #ifdef HELP @@ -151,7 +151,7 @@ void setup() endstops_setup(); #endif - registered_commands[registered_commands_count] = END_COMMAND; + registered_commands[registered_commands_count] = &end_command; Serial.println(F(VERSION_STRING)); } diff --git a/src/main.h b/src/main.h index c9a156b..e69ec23 100644 --- a/src/main.h +++ b/src/main.h @@ -11,14 +11,18 @@ { \ "", NULL \ } -#define CHECK_END_COMMAND(C) C.command.length() > 0 +#define CHECK_END_COMMAND(C) check_end_command(C) struct Command { - String command; + const char * command; void (*run)(String); }; +inline bool check_end_command(Command c) { return strlen(c.command) > 0; } +inline bool check_end_command(Command * c) {return strlen(c->command) > 0; } + +const Command end_command = END_COMMAND; uint8_t parse_arguments(char ** arguments, String, uint8_t); extern void register_module(const Command commands[], void (*loop_fn)(void)); diff --git a/src/modules/endstops/endstops.cpp b/src/modules/endstops/endstops.cpp index 2967a84..661876d 100644 --- a/src/modules/endstops/endstops.cpp +++ b/src/modules/endstops/endstops.cpp @@ -11,12 +11,12 @@ unsigned long retreat_steps = 5000; unsigned long home_move_steps = 100000; unsigned long axis_max[STAGE_N_MOTORS]; -const int axis_max_eeprom = sizeof(long)*(STAGE_N_MOTORS+2); +const uint8_t axis_max_eeprom = sizeof(long)*(STAGE_N_MOTORS+2); #ifdef ENDSTOPS_MIN -const int endstops_min_pins[] = WIRING_ENDSTOPS_MIN; +const uint8_t endstops_min_pins[] = WIRING_ENDSTOPS_MIN; #endif #ifdef ENDSTOPS_MAX -const int endstops_max_pins[] = WIRING_ENDSTOPS_MAX; +const uint8_t endstops_max_pins[] = WIRING_ENDSTOPS_MAX; #endif bool endstops_enabled = true; diff --git a/src/modules/stage/stage.cpp b/src/modules/stage/stage.cpp index 3fc106e..982031f 100644 --- a/src/modules/stage/stage.cpp +++ b/src/modules/stage/stage.cpp @@ -10,13 +10,13 @@ #include "main.h" // The array below has 3 stepper objects, for X,Y,Z respectively -const int n_motors = 3; +const uint8_t n_motors = 3; long min_step_delay; -const int min_step_delay_eeprom = sizeof(long) * n_motors; +const uint8_t min_step_delay_eeprom = sizeof(long) * n_motors; long ramp_time; -const int ramp_time_eeprom = sizeof(long) * (n_motors + 1); -const int axis_max_eeprom = sizeof(long) * (n_motors + 2); -const int non_blocking_moves_eeprom = sizeof(long) * (n_motors + 3); +const uint8_t ramp_time_eeprom = sizeof(long) * (n_motors + 1); +const uint8_t axis_max_eeprom = sizeof(long) * (n_motors + 2); +const uint8_t non_blocking_moves_eeprom = sizeof(long) * (n_motors + 3); Stepper *motors[n_motors]; signed long current_pos[n_motors]; long steps_remaining[n_motors]; @@ -93,7 +93,7 @@ void start_move(long displ[n_motors]) { // move all the axes in a nice move // split displacements into magnitude and direction, and find max. travel - unsigned long max_steps = 0; + long max_steps = 0; EACH_MOTOR { move_directions[i] = displ[i] > 0 ? 1 : -1; @@ -103,14 +103,16 @@ void start_move(long displ[n_motors]) } // scale the step delays so the move goes in a straight line, with >=1 motor // running at max. speed. - EACH_MOTOR {if (displacement[i] > 0) + EACH_MOTOR { - step_delay[i] = float(max_steps) / float(displacement[i]) * float(min_step_delay); - } - else - { - step_delay[i] = 9999999999; - } + if (displacement[i] > 0) + { + step_delay[i] = float(max_steps) / float(displacement[i]) * float(min_step_delay); + } + else + { + step_delay[i] = 9999999999; + } } EACH_MOTOR distance_moved[i] = 0; move_start_time = micros(); @@ -165,7 +167,7 @@ void stage_loop() stage_moving = false; EACH_MOTOR { - if ((long) distance_moved[i] < displacement[i]) + if ((long)distance_moved[i] < displacement[i]) { stage_moving = true; //only if all axes are done are we truly finished. @@ -186,7 +188,7 @@ void stage_loop() void stage_move_single_axis(uint8_t axis, String command) { - char * args[1]; + char *args[1]; parse_arguments(args, command, 1); int move = atoi(args[0]); EACH_MOTOR displacement[i] = 0; @@ -211,7 +213,7 @@ void stage_mrz(String command) void stage_mr(String command) { - char * args[3]; + char *args[3]; parse_arguments(args, command, 3); EACH_MOTOR { @@ -238,7 +240,7 @@ void stage_p(String command) void stage_min_step_delay(String command) { - char * args[1]; + char *args[1]; parse_arguments(args, command, 1); if (args[0][0] == '?') { @@ -256,7 +258,7 @@ void stage_min_step_delay(String command) void stage_ramp_time(String command) { - char * args[1]; + char *args[1]; parse_arguments(args, command, 1); if (args[0][0] == '?') {