My learning thread - thanks everyone
Wed Jan 12 2011, 05:29 am
sure ajay. But its very difficult to make a progress hehe.
i have a based code but it is in tcl language my big problem is converting it to python. Wtf! Hehe
i cant get a help here since you don know tcl and python.
i have a based code but it is in tcl language my big problem is converting it to python. Wtf! Hehe
i cant get a help here since you don know tcl and python.
Wed Jan 12 2011, 05:31 am
later i will attach the complete source code i will let you see what is tcl language..
Wed Jan 12 2011, 03:35 pm
guys what do you mean by this
what do you mean by this extern struct br sBlueRobin;
and i found a function looks like this
it seems that it calls the variable sBlueRobin.caldist_view
but why using "." or dot
when i will use struct br ??? what is br? is it variable?
when i declar a variable like this
what do the s32 mean?? hehe i need explaination.. Im using that in my code without understanding i just seen that in some examples in the web.. hehe
struct br { // BLUEROBIN_OFF, BLUEROBIN_SEARCHING, BLUEROBIN_CONNECTED, BLUEROBIN_ERROR BlueRobin_state_t state; // BLUEROBIN_NO_UPDATE, BLUEROBIN_NEW_DATA BlueRobin_update_t update; // Chest strap ID u32 cs_id; // User settings u8 user_sex; u16 user_weight; // Heart rate (1 bpm) u8 heartrate; // Calories (1 kCal) - calculated from heart rate, user weight and user sex u32 calories; // Speed (0.1 km/h) - demo version range is 0.0 to 25.5km/h u8 speed; // Distance (1 m) u32 distance; // 0=display calories, 1=display distance u8 caldist_view; }; extern struct br sBlueRobin;
what do you mean by this extern struct br sBlueRobin;
and i found a function looks like this
if (sBlueRobin.caldist_view == 0) sBlueRobin.caldist_view = 1; else sBlueRobin.caldist_view = 0;
it seems that it calls the variable sBlueRobin.caldist_view
but why using "." or dot
when i will use struct br ??? what is br? is it variable?
when i declar a variable like this
s32 weight;
what do the s32 mean?? hehe i need explaination.. Im using that in my code without understanding i just seen that in some examples in the web.. hehe
[ Edited Wed Jan 12 2011, 05:56 pm ]
Wed Jan 12 2011, 05:38 pm
actually Im using that code above. I modified it and its working fine... i just used that without understanding and now i need someone to explain that to me.... hehehe eheheh thanks in advance..... heheh
Wed Jan 12 2011, 08:18 pm
thats how structures in C/C++ are used. it looks like you never used structure.
here is a quick reference
Defining structure
Now mystruct is name of structure. a,b,c and d are members of structure. its like grouping set of variables in a single datatype and giving your own name to it.
to use a structure, you first need to create an instance of the structure. and members of structure can only be accessed through that instance.
so x and y are two instances of structure. and z is a pointer to mystruct.
using structure is very simple, lets say you want to equate members of structure x with y then you will do like this.
Pointers work a little different way we use dot (.) incase of structure variables to access the members but incase of pointers we use arrows (->) e.g.
Initializing structure:
structures are initialized just like we do in arrays. each member is separated by comma and complete thing is enclosed in curly brackets.
Working example:
lets take an example, I want to make database of user accounts in C. I can have total of 100 users. user info will have username, password, age etc. so if you just do it in traditional style what you will do is.. you will create three arrays to store information of each user.
now if you write code using the above style you will have to manage lot of arrays which might get your code look very complex. So now lets see structure style for the same..
that defines a custom data type with name users. now lets create 100 users.
lets make simple login checker now.. a function that checks if entered user details are correct or not.
well i think small tutorial on structures is good enough hope you now learn how to use structures, how to pass structure as a varaible to function, defining a structure pointer and variable etc.
here is a quick reference
Defining structure
//defining structure struct mystruct { int a; char b; char c[10]; int d[10]; };
Now mystruct is name of structure. a,b,c and d are members of structure. its like grouping set of variables in a single datatype and giving your own name to it.
to use a structure, you first need to create an instance of the structure. and members of structure can only be accessed through that instance.
//defining instance of struct struct mystruct x,y,*z;
so x and y are two instances of structure. and z is a pointer to mystruct.
using structure is very simple, lets say you want to equate members of structure x with y then you will do like this.
x.a = y.a; x.b = y.b; memcpy(x.c, y.c, 10); //copy character array
Pointers work a little different way we use dot (.) incase of structure variables to access the members but incase of pointers we use arrows (->) e.g.
z = &x; //z has address of x, or say z points to x now. y.a = z-> a; //you see the difference now? y.b = z-> b; //its same with all.. just replace . with ->
Initializing structure:
structures are initialized just like we do in arrays. each member is separated by comma and complete thing is enclosed in curly brackets.
x = {10, 'c', "hello", {10,20,30,40,50,60,70,80,90,100}}; //partially defining structure y = {30, 'y', "world"}; //now the integer array will automatically be initialized to 0.
Working example:
lets take an example, I want to make database of user accounts in C. I can have total of 100 users. user info will have username, password, age etc. so if you just do it in traditional style what you will do is.. you will create three arrays to store information of each user.
//array style.. char username[100][20]; //100 usernames each of length 20 max char password[100][15]; //100 passwords each of length 15 max int age[100]; //array to store age of 100 users
now if you write code using the above style you will have to manage lot of arrays which might get your code look very complex. So now lets see structure style for the same..
struct users { char username[20]; char password[15]; int age; };
that defines a custom data type with name users. now lets create 100 users.
struct users user[100]; //we define array of 100 1 structure for each user.
lets make simple login checker now.. a function that checks if entered user details are correct or not.
int checkuser(struct users input){ int i; int status = 0; //we have 100 users so loop will go from 0 to 99 //checking each user.. for(i=0; i<100; i++){ if(strcmp(input.username, user[i].username) == 0){ //check if username is there in database or not if(strcmp(input.password, user[i].password) == 0){ //check if password matches status = 1; //set status to 1 so that we say input user is available in database break; //break out of loop, we are done with checking } else { status = -1; //username is there but password incorrect break; //break out of loop. } } } if(i == 100){ //if i reaches 100 it means username not available in database status = -2; } return status; //return status to caller function }
well i think small tutorial on structures is good enough hope you now learn how to use structures, how to pass structure as a varaible to function, defining a structure pointer and variable etc.
romel_emperado like this.
Tags structures in cusing structure in embedded cembedded c and structuresdata structures in embedded cembedded c tutorial structures
Thu Jan 13 2011, 11:07 am
thank you very much ajay.. your tut is very detailed and understandable... thanks for your effort yar!!! heheeh I love u... hehe
Thu Jan 13 2011, 07:13 pm
how about this ajay
i know how to use #define varName but what is this
#define DISPLAY_DEFAULT_VIEW (0u)
i know how to use #define varName but what is this
(0u)thing??
Fri Jan 14 2011, 08:47 pm
guys i need help again... How do i call the function mx_cal(); ?? what is that u8 line parameter? i dont have idea how do i call that from the function i made below which is burned_cal();
// system #include "project.h" // driver #include "display.h" #include "ports.h" // logic #include "romel.h" #include "clock.h" #include "date.h" #include "user.h" // Global Variable section struct br mel; s8 weight2, time; s8 met2; float kcal; // Prototypes section void display_romel(u8 line, u8 update) { if (update == DISPLAY_LINE_UPDATE_FULL) { display_chars(LCD_SEG_L2_5_0, (u8 *)" CAL0", SEG_ON); } } void mx_cal(u8 line) { u8 select; s32 weight; s32 met; // Clear display clear_display_all(); // Convert global variables to local variables //kcalories = mel.calories/1000; met = mel.met_value/1000; if (sys.flag.use_metric_units) weight = mel.user_weight; else weight = ((s32)mel.user_weight * 2205) / 1000; // Convert kg to lb // Init value index select = 0; // Loop values until all are set or user breaks set while(1) { // Idle timeout : exit without saving if (sys.flag.idle_timeout) break; // Button STAR (short): save, then exit if (button.flag.star) { // Store local variables in global structure //mel.calories = kcalories*1000; //mel.user_sex = sex; mel.met_value = met*1000; if (sys.flag.use_metric_units) mel.user_weight = weight; else mel.user_weight = (weight * 1000) / 2205; // Set display update flag display.flag.line1_full_update = 1; break; } switch (select) { case 0: // Set user weight clear_line(LINE2); if (sys.flag.use_metric_units) { display_chars(LCD_SEG_L2_1_0, (u8 *)"KG", SEG_ON); set_value(&weight, 3, 2, USER_WEIGHT_MIN_KG, USER_WEIGHT_MAX_KG, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1); } else { display_chars(LCD_SEG_L2_1_0, (u8 *)"LB", SEG_ON); set_value(&weight, 3, 2, USER_WEIGHT_MIN_LB, USER_WEIGHT_MAX_LB, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1); weight2 = weight; } select = 1; break; case 1: //Set MET display_chars(LCD_SEG_L2_1_0, (u8 *)"ME", SEG_ON); set_value(&met, 3, 2, USER_WEIGHT_MIN, USER_METS_MAX, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1); met2 = met; select = 0; break; } } // Clear button flags button.all_flags = 0; } //calorie expernditure forMULa void burned_cal(void) { if(sTime.second == 60) { time++; } kcal = ((met2 * 3.5 * (weight2/2.2)) / 200) * time; }
[ Edited Sat Jan 15 2011, 01:39 pm ]
Powered by e107 Forum System