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:
Filip Ayazi 2021-07-11 05:21:38 +01:00
parent 2dab1008dc
commit 1c8e725282
4 changed files with 36 additions and 30 deletions

View file

@ -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));
}

View file

@ -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));

View file

@ -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;

View file

@ -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,7 +103,9 @@ 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
{
if (displacement[i] > 0)
{
step_delay[i] = float(max_steps) / float(displacement[i]) * float(min_step_delay);
}