Pages: [1]   Go Down
Author Topic: Which way is better? Pass struct or pass individual parameters?  (Read 1362 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm revamping my phi-menu after some initial defeat trying to turn it into a class.

So regarding turning the interactive LCD functions and menu into a library, I have the following two directions to take:
The functions I want to make available in the library are input functions that ask a user to enter information, integer, float, select from list, and input arbitrary texts.

They all return status of the user input (confirmed or escaped or else) and return values through the very first pointer.

Here's some prototypes if I pass individual parameters:
Code:
int input_integer(int *buffer, int low, int high, int step, byte col, byte row, int options)
int input_float(float *buffer, float low, float high, float step, byte col, byte row, int options)
int select_list(int *buffer, byte col, byte row, char ** list, byte total_items, byte list_style, int options)
int input_panel(char* msg, byte length, byte col, byte row, char _start, char _end)

I can have prototypes like:
Code:
int input_integer(phi_menu_parameters *para)
int input_float(phi_menu_parameters *para)
int select_list(phi_menu_parameters *para)
int input_panel(phi_menu_parameters *para)


Can someone critic on the pros and cons of each way on memory usage, FLASH usage, understandability of the above as a library, especially for newbies to use the library to construct decent interactive programs with LCD and buttons? Thank you. Been thinking too much and will listen to any input.
If instead I use a struct which is like this:
Code:
struct phi_menu_para
{
int *buffer;
int i_low;
int i_high;
int i_step;
float * f_buffer;
float f_low;
float f_high;
float f_step;
byte col;
byte row;
int options;
char ** list;
byte total_items;
byte list_style;
char* msg;
byte length;
char _start;
char _end;
};
« Last Edit: March 24, 2011, 11:04:48 pm by liudr » Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Update:

I used unions to cut down the original 35-byte structure to 22 bytes:

Code:
int input_integer(phi_menu_parameters *para)
int input_float(phi_menu_parameters *para)
int select_list(phi_menu_parameters *para)
int input_panel(phi_menu_parameters *para)

union buffer_pointer
{
int *i_buffer;
float * f_buffer;
char ** list;
char* msg;
} ;

union four_bytes
{
int i;
float f;
byte b;
char c;
}

typedef struct
{
buffer_pointer ptr;
four_bytes low;
four_bytes high;
four_bytes step;
four_bytes total;
byte col;
byte row;
int options;
} phi_menu_para; //22 bytes

« Last Edit: March 24, 2011, 11:04:30 pm by liudr » Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8517
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For my money I'd say always pass a pointer to a struct around, it's one single item and that makes things more unterstandable, easier to remember, and less error prone.

so something like

Code:
select_list(&buffer, col, row, &list, total_items, list_style, options)

becomes

Code:
select_list(myStructPtr)

As for how this jells with beginners understanding I don't know though.

Quote
memory usage
Probably less stack to us a single pointer, but maybe more dereferencing inside the called function which would increase the code size. 
______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So taking Graynomad's suggestion, I'm thinking along this line of syntax if a developer wants a user to enter a number between 0 and 20, with default value of 10 and increment of 1:

Code:
phi_menu_para myPara; // Define a parameter structure to store parameters
int value=10; // This is the ultimate storage for the integer
int buffer=value; // This is the temporary storage for the integer. It is the buffer the function will use and it changes as the user changes the value.
myPara.ptr.i_buffer=&buffer; // Set up the parameters
myPara.low.i=0;
myPara.high.i=20;
myPara.step.i=2;
myPara.col=7;
myPara.row=2;
myPara.option=1;
if (input_integer(myPara)!=-1) value=buffer; // If the user didn't press escape (return -1) then update the ultimate storage with the value in the buffer.
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8517
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int buffer=value; // This is the temporary storage for the integer. It is the buffer the function will use and it changes as the user changes the value.
myPara.ptr.i_buffer=&buffer; // Set up the parameters
I don't see any reason to keep a pointer to a global.

I haven't been following the logic of this menu system so can really just deal with generalities.

Does input_integer() use the contents of myPara to set up the display and handle UI for a particular menu item?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not all storage is global. A function can have a local variable and asks the user to input the value of this variable.

Yes the phi_menu (now I want to call it phi_prompt) is a system that handles UI on 16X2 LCD and 1 to 6 buttons. One function call like input_integer() will return with information of what integer the user inputs so a developer will be focused on functions of his/her project and have the phi_prompt handle all simple UI like "Input motor RPM" then call input_integer() with appropriate parameter struct then you receive user input of say 2000 and then run the motor at 2000RPM.
Logged


Pages: [1]   Go Up
Jump to: