reduce repeating codes

Status
Not open for further replies.

skmdmasud

Member
Hi..
in my code below i have 10 such schedules which needs to be read and write, the codes is the same only the NV_Schedule_XX changes. Is there any way of reducing this codes. can we do something like for loop and change the NV_Schedule_XX with numbers etc..


Code:
schedule_1()
{
	eeprom_update_word((&NV_schedule_01_set_water_mix_no_mix), set_water_mix_no_mix());
	eeprom_update_word((&NV_schedule_01_filter_state), filter_status());
	eeprom_update_word((&NV_schedule_01_pwr_hed_lhr), pwr_hed_LHr());
	eeprom_update_word((&NV_schedule_01_max_fill_time), max_fill_time());
	eeprom_update_word((&NV_schedule_01_water_drain_amount), set_water_drain_amount());
	eeprom_update_word((&NV_schedule_01_day), set_day(0));
	eeprom_update_word((&NV_schedule_01_total_time), set_time(0));
}

schedule_2()
{
	eeprom_update_word((&NV_schedule_02_set_water_mix_no_mix), set_water_mix_no_mix());
	eeprom_update_word((&NV_schedule_02_filter_state), filter_status());
	eeprom_update_word((&NV_schedule_02_pwr_hed_lhr), pwr_hed_LHr());
	eeprom_update_word((&NV_schedule_02_max_fill_time), max_fill_time());
	eeprom_update_word((&NV_schedule_02_water_drain_amount), set_water_drain_amount());
	eeprom_update_word((&NV_schedule_02_day), set_day(0));
	eeprom_update_word((&NV_schedule_02_total_time), set_time(0));
}

schedule_3()
{
	eeprom_update_word((&NV_schedule_03_set_water_mix_no_mix), set_water_mix_no_mix());
	eeprom_update_word((&NV_schedule_03_filter_state), filter_status());
	eeprom_update_word((&NV_schedule_03_pwr_hed_lhr), pwr_hed_LHr());
	eeprom_update_word((&NV_schedule_03_max_fill_time), max_fill_time());
	eeprom_update_word((&NV_schedule_03_water_drain_amount), set_water_drain_amount());
	eeprom_update_word((&NV_schedule_03_day), set_day(0));
	eeprom_update_word((&NV_schedule_03_total_time), set_time(0));
}
 
Personally I would use a structure then you can initialize 3 structures... Then when you read / write the values you just specify the schedule.
 
i have use case from where the schedules are getting called. Is there any way of reducing code like this.
 
When you design your software, you can elect to create data structures to hold all your variables that need saving when the system shuts down.

If your structure is saved as its stored memory, then you can retrieve it in the same way.. This makes it easier to do what you want it to do now..

I use multiple sensors. They all have the same parameters... Maximum, minimum, value etc... They are Load cells , length sensors, angle sensors and such.

I can just pass the number od the sensor to my "Save Function" and they are stored in the correct fashion...
 
can i do somthing like this

int xx;
eeprom_update_word((&NV_schedule_'xx'_set_water_mix_no_mix), set_water_mix_no_mix());

is this valid.
 
Like Ian said, use structure to hold the details of each NV_schedule_xx. Google c structure if you don´t know what we´re talking about.
 
can i do somthing like this

int xx;
eeprom_update_word((&NV_schedule_'xx'_set_water_mix_no_mix), set_water_mix_no_mix());

is this valid.


I take it this eeprom_update_word () takes two parameters... Can you show me your list of identifiers to see if you can enumerate them..
 
I take it this eeprom_update_word () takes two parameters... Can you show me your list of identifiers to see if you can enumerate them..

after studying both structure and enumerate it seem that this techniques will not reduce my code, they make code more arranged and tidy.

The eeprom update has 2 parameters

eeprom_update_word((&NV_schedule_01_set_water_mix_no_mix), set_water_mix_no_mix());

eeprom_update_word((&YOUR EEP MEMORY LOCATION, VALUE TO BE INSERTED);


I think i just got somthing, i tried this code, compilation went fine. i put the memory variable in char 'a' and then inserted 'a' into the eeprom function
char a="NV_schedule_01_set_water_mix_no_mix";
eeprom_update_word((&a), set_water_mix_no_mix());
 
Hello Mr Ian
i did the following and still compilation is fine no errors, now all i need is to increment my number variable and hopefully my memory address will change with it

int number=1; //for testing
char a[2];
a[0]="NV_schedule_";
a[1]=number;
a[2]="_set_water_mix_no_mix";

eeprom_update_word((&a), set_water_mix_no_mix());
 
That will not work. I'm not sure why your compiler didn't at least give a warning.

The only way to do what you are trying is to arrange your variables so they are continuous in memory and then use a for loop to read/write them to eeprom. In some compilers this can be done as

int Variable@0x100;

Mike.
 

my warnings are tuned off. i will check for warnings. I am using avr studio 6.
 
You could use the ## preprocessor operator that does token pasting

Try something like:

#define save(n) eeprom_update_word((&NV_schedule_##n_set_water_mix_no_mix), set_water_mix_no_mix())

/* use like this */
save(01);

or even further:

#define save(n, x) eeprom_update_word((&NV_schedule_##n_##x), ##x())

save(01, set_water_mix_no_mix);
 
Last edited:

This looks great, just what i need. Hope this will work.
 

This won't make the code any smaller but will save on typing. However, I believe the OP is looking at reducing code size.

Mike.
 
The preprocessor runs before the compiler and will effectively insert the code so that it is the same as your current code. The above is just a shortcut to save typing.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…