Reduce memory use
* Command definition now uses char * instead of String * registered_commands is now an array of pointers to Commands, previously this was an array of Command and was allocated to MAX_COMMANDS entries which were then overwritten with the module provided Commands, so every Command used twice as much memory. This saves ~200bytes * Several (u)ints are now (u)int8_t On the nano with all modules included this reduces global RAM use from 1676 bytes to 1237 bytes (from 81.8% to 60.4%).
This commit is contained in:
parent
2dab1008dc
commit
1c8e725282
4 changed files with 36 additions and 30 deletions
14
src/main.cpp
14
src/main.cpp
|
|
@ -36,10 +36,10 @@
|
||||||
#include "modules/light_sensor/light_sensor.h"
|
#include "modules/light_sensor/light_sensor.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Command registered_commands[MAX_COMMANDS];
|
const Command * registered_commands[MAX_COMMANDS];
|
||||||
void (*registered_loop_functions[MAX_MODULES])(void);
|
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;
|
uint8_t registered_loop_fn_count = 0;
|
||||||
|
|
||||||
const Command core_commands[] = {
|
const Command core_commands[] = {
|
||||||
|
|
@ -50,7 +50,7 @@ const Command core_commands[] = {
|
||||||
void register_module(const Command commands[], void (*loop_fn)(void))
|
void register_module(const Command commands[], void (*loop_fn)(void))
|
||||||
{
|
{
|
||||||
for (int i = 0; CHECK_END_COMMAND(commands[i]); i++)
|
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)
|
if (loop_fn)
|
||||||
registered_loop_functions[registered_loop_fn_count++] = 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)
|
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))
|
if (received_command.startsWith(registered_commands[i]->command))
|
||||||
return registered_commands[i].run(received_command.substring(registered_commands[i].command.length()));
|
return registered_commands[i]->run(received_command.substring(strlen(registered_commands[i]->command)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HELP
|
#ifdef HELP
|
||||||
|
|
@ -151,7 +151,7 @@ void setup()
|
||||||
endstops_setup();
|
endstops_setup();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
registered_commands[registered_commands_count] = END_COMMAND;
|
registered_commands[registered_commands_count] = &end_command;
|
||||||
Serial.println(F(VERSION_STRING));
|
Serial.println(F(VERSION_STRING));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,14 +11,18 @@
|
||||||
{ \
|
{ \
|
||||||
"", NULL \
|
"", NULL \
|
||||||
}
|
}
|
||||||
#define CHECK_END_COMMAND(C) C.command.length() > 0
|
#define CHECK_END_COMMAND(C) check_end_command(C)
|
||||||
|
|
||||||
struct Command
|
struct Command
|
||||||
{
|
{
|
||||||
String command;
|
const char * command;
|
||||||
void (*run)(String);
|
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);
|
uint8_t parse_arguments(char ** arguments, String, uint8_t);
|
||||||
extern void register_module(const Command commands[], void (*loop_fn)(void));
|
extern void register_module(const Command commands[], void (*loop_fn)(void));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,12 @@
|
||||||
unsigned long retreat_steps = 5000;
|
unsigned long retreat_steps = 5000;
|
||||||
unsigned long home_move_steps = 100000;
|
unsigned long home_move_steps = 100000;
|
||||||
unsigned long axis_max[STAGE_N_MOTORS];
|
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
|
#ifdef ENDSTOPS_MIN
|
||||||
const int endstops_min_pins[] = WIRING_ENDSTOPS_MIN;
|
const uint8_t endstops_min_pins[] = WIRING_ENDSTOPS_MIN;
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENDSTOPS_MAX
|
#ifdef ENDSTOPS_MAX
|
||||||
const int endstops_max_pins[] = WIRING_ENDSTOPS_MAX;
|
const uint8_t endstops_max_pins[] = WIRING_ENDSTOPS_MAX;
|
||||||
#endif
|
#endif
|
||||||
bool endstops_enabled = true;
|
bool endstops_enabled = true;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,13 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
// The array below has 3 stepper objects, for X,Y,Z respectively
|
// 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;
|
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;
|
long ramp_time;
|
||||||
const int ramp_time_eeprom = sizeof(long) * (n_motors + 1);
|
const uint8_t ramp_time_eeprom = sizeof(long) * (n_motors + 1);
|
||||||
const int axis_max_eeprom = sizeof(long) * (n_motors + 2);
|
const uint8_t axis_max_eeprom = sizeof(long) * (n_motors + 2);
|
||||||
const int non_blocking_moves_eeprom = sizeof(long) * (n_motors + 3);
|
const uint8_t non_blocking_moves_eeprom = sizeof(long) * (n_motors + 3);
|
||||||
Stepper *motors[n_motors];
|
Stepper *motors[n_motors];
|
||||||
signed long current_pos[n_motors];
|
signed long current_pos[n_motors];
|
||||||
long steps_remaining[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
|
// move all the axes in a nice move
|
||||||
// split displacements into magnitude and direction, and find max. travel
|
// split displacements into magnitude and direction, and find max. travel
|
||||||
unsigned long max_steps = 0;
|
long max_steps = 0;
|
||||||
EACH_MOTOR
|
EACH_MOTOR
|
||||||
{
|
{
|
||||||
move_directions[i] = displ[i] > 0 ? 1 : -1;
|
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
|
// scale the step delays so the move goes in a straight line, with >=1 motor
|
||||||
// running at max. speed.
|
// 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);
|
if (displacement[i] > 0)
|
||||||
}
|
{
|
||||||
else
|
step_delay[i] = float(max_steps) / float(displacement[i]) * float(min_step_delay);
|
||||||
{
|
}
|
||||||
step_delay[i] = 9999999999;
|
else
|
||||||
}
|
{
|
||||||
|
step_delay[i] = 9999999999;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
EACH_MOTOR distance_moved[i] = 0;
|
EACH_MOTOR distance_moved[i] = 0;
|
||||||
move_start_time = micros();
|
move_start_time = micros();
|
||||||
|
|
@ -165,7 +167,7 @@ void stage_loop()
|
||||||
stage_moving = false;
|
stage_moving = false;
|
||||||
EACH_MOTOR
|
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.
|
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)
|
void stage_move_single_axis(uint8_t axis, String command)
|
||||||
{
|
{
|
||||||
char * args[1];
|
char *args[1];
|
||||||
parse_arguments(args, command, 1);
|
parse_arguments(args, command, 1);
|
||||||
int move = atoi(args[0]);
|
int move = atoi(args[0]);
|
||||||
EACH_MOTOR displacement[i] = 0;
|
EACH_MOTOR displacement[i] = 0;
|
||||||
|
|
@ -211,7 +213,7 @@ void stage_mrz(String command)
|
||||||
|
|
||||||
void stage_mr(String command)
|
void stage_mr(String command)
|
||||||
{
|
{
|
||||||
char * args[3];
|
char *args[3];
|
||||||
parse_arguments(args, command, 3);
|
parse_arguments(args, command, 3);
|
||||||
EACH_MOTOR
|
EACH_MOTOR
|
||||||
{
|
{
|
||||||
|
|
@ -238,7 +240,7 @@ void stage_p(String command)
|
||||||
|
|
||||||
void stage_min_step_delay(String command)
|
void stage_min_step_delay(String command)
|
||||||
{
|
{
|
||||||
char * args[1];
|
char *args[1];
|
||||||
parse_arguments(args, command, 1);
|
parse_arguments(args, command, 1);
|
||||||
if (args[0][0] == '?')
|
if (args[0][0] == '?')
|
||||||
{
|
{
|
||||||
|
|
@ -256,7 +258,7 @@ void stage_min_step_delay(String command)
|
||||||
|
|
||||||
void stage_ramp_time(String command)
|
void stage_ramp_time(String command)
|
||||||
{
|
{
|
||||||
char * args[1];
|
char *args[1];
|
||||||
parse_arguments(args, command, 1);
|
parse_arguments(args, command, 1);
|
||||||
if (args[0][0] == '?')
|
if (args[0][0] == '?')
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue