Curly brackets (Warning non-adult content)

Post test messages here

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
illian
 
Posts: 18
Joined: Thu Dec 31, 2009 4:07 pm

Curly brackets (Warning non-adult content)

Post by illian »

rant rant rant rant rant.
grrrr, spit, growl, and stamp.

:x

Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Re: Curly brackets (Warning non-adult content)

Post by Kdees »

My thoughts exactly....

illian
 
Posts: 18
Joined: Thu Dec 31, 2009 4:07 pm

Re: Curly brackets (Warning non-adult content)

Post by illian »

Matron has changed my medication and I'm feeling much better now thank you. :mrgreen:

illian
 
Posts: 18
Joined: Thu Dec 31, 2009 4:07 pm

Re: Curly brackets (Warning non-adult content)

Post by illian »

If any noobs like me wander into this post, looking for a get out of jail / how-to about curley brackets, After about a week of guessing and getting nowhere, here's how I got around the headbanging problems I was having; I've not seen this method suggested anywhere else, probably because OP know what they are doing! But if there's a utility or better way of doing this, someone let me know!


Real coders look away now! :-)



The structure I was working on, with keypad, LCD, buttons and external sensors etc looked something like this;

Code: Select all


{
   {
      {
      }
   {
      {
         {
         {
         {
         }
         }
         }
            {
            }
      }
   }
}

Thats the sort of pattern I was trying to get my head around and failing horribly... Far too ambitious for a first project!
so...
SERIAL MONITOR ON!

Code: Select all

{
serial.print("loop 1 start");
//code goes here

     {
    serial.print("loop 2 start");
    //code goes here

          {
      serial.print("loop 3 start");
      //code goes here
      serial.print("loop 3 end");
           }

     {
  serial.print("loop 4 start");
  //code goes here

          {
     serial.print("loop 5 start");
     //code goes here

          {
         serial.print("loop 6 start");
         //code goes here

         {
         serial.print("loop 7 start");
         //code goes here

         {
          serial.print("loop 8 start");
         //code goes here
         serial.print("loop 8 end");
         }

         serial.print("loop 7 end");
         }

         serial.print("loop 6 end");
         }
              {
              serial.print("loop 9 start");
              //code goes here
              serial.print("loop 9 end");   
              }

         serial.print("loop 5 end");  
         }

     serial.print("loop 4 end");
     }

     serial.print("loop 2 end");
     }

serial.print("loop 1 end");
}
It works with void setup and void loop. If you watch the serial monitor it will show you each loop open / loop close wizz past. You can probably get away with commenting out loop 1 and 2, or if you are just looking at one specific part of the code, just comment out the tuff you don't need /everything else.. You can also scroll back up the serial monitor and watch it line by line... I ended up using these on every line of code when trying to figure out why it was doing something I wasn't expecting, it saved me lots of time and headaches. :-)

Obviously, you can use this to watch variables too, although when I tried to watch an LED pin going high or low, I didn't get the serial monitor print of HIGH /LOW... but the pin number! eg; serial.print(ledPin) came back with 0 or 45 (the pin number of ledPin)!

Hope it helps.

User avatar
westfw
 
Posts: 2010
Joined: Fri Apr 27, 2007 1:01 pm

Re: Curly brackets (Warning non-adult content)

Post by westfw »

"printf style debugging" ! Common enough to have its own fan page on facebook, but congratulations on rediscovering it on your own. (I always feel a bit bipolar about rediscovering things. One one hand, if I had just looked in the right place, I could have had it all laid out in nice tutorial form, and been on my way a lot quicker. OTOH, Hey! I figured something out all on my own that is about the same as the way LOTS have people are using!)

A good code editor will aid you in matching brackets, which is very helpful.

(Be (grateful (usage ("lisp") fallen(true))))

charliex
 
Posts: 179
Joined: Sat Nov 28, 2009 5:54 pm

Re: Curly brackets (Warning non-adult content)

Post by charliex »

one trick i use fairly often is to nest the console/serial output, so you store a simple nest counter, add one each nest, subtract one each time you leave the nest. then just print out n*space (or tab if you have the real estate and a {
and then the same amount for each debug print, that way you get a nicely formatted output. of course its slower, but its easy to read.

On all my functions i have a prolog and epilog function that shows the start and end of the function block, that way i can add in diagnostics easily, such as simple logging, simple profiling, coverage, memory checking etc.
its just a simple thing that looks like this
void func(void)
{
prolog("func");

.....
epilog("func");
}

some compilers can generate them, but it varies, some only do prolog, some none at all.

illian
 
Posts: 18
Joined: Thu Dec 31, 2009 4:07 pm

Re: Curly brackets (Warning non-adult content)

Post by illian »

LOL,

@westfw
"DEBUGGING"? as in "The methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware thus making it behave as expected"?? Hell no, this is how I 'create' the code! :-) The good news is that I know what the F is short for in "printf"
I guessed that it was something that would only be of use to ubernoobs, but having read through hundreds of questions looking for answers, I don't think I'm the only one to find the step between doing tutorials and modifying other peoples code was quite a small and rather easy step, but from modifying other code to generating your "own" code a massive step.

@charliex
(Needing ALL the help I can get!) can you expand on what you mean with those ideas?

I see what you are doing with prolog / epilog, but can't see how you'd apply them to logging, profiling etc.. The nest counter idea is even better, does it work with the arduino IDE or just 'real' coding?

I have a text editor open on the (twin screen system) other screen and copy paste and document what I'm changing with time / date into that each and do that every time... I'm not sure that I can remember the reason I started doing this other than easiy getting back to where I was before I broke it some more.. :oops:

charliex
 
Posts: 179
Joined: Sat Nov 28, 2009 5:54 pm

Re: Curly brackets (Warning non-adult content)

Post by charliex »

Well this is a simple one in C, i just typed it up so it might have some errors, but the idea is the same

in C printf you don't need the loop since its capable of printing n char's , but i wanted to expand the logic.

Code: Select all


// for printf
#include <stdio.h>

// global to store nest depth
static int nestDepth = 0 ;

// our pro/epilog debug routine
void _prolog( const char *funcName, const char*fileName, int lineNumber)
{
	int i = nestDepth;

	// use this for visual c++ clicking in console output window for going to a source code/line file, but makes the output less clear
//	printf("%s(%d) : error C1000:\n",fileName,lineNumber);
	
	// print out tabs for n nest level
	while( i-- ) printf("\t");

	
	printf("%s() {\n",funcName);
	
	//increase nesting
	nestDepth++;
}

void _epilog( const char *funcName, const char*fileName, int lineNumber)
{

	int i;
	// decrease depth
	nestDepth--;

	i = nestDepth;

	
	// print out tabs for n nest level
	while( i-- ) printf("\t");

	printf("}\n");

}

#define PROLOG( f )  _prolog(f, __FILE__, __LINE__ )  
#define EPILOG( f )  _epilog(f, __FILE__, __LINE__ )  

so the program

Code: Select all

// program code

void bar(void )
{
	PROLOG("bar");
	
	// do stuff

	EPILOG("bar");
}

void foo(void )
{
	PROLOG("foo");

	bar();

	EPILOG("foo");
}


int main()
{


	PROLOG("main");

	foo();

	EPILOG("main");
}
should generate an output of, then your debug fuction just prints out 1+(n*tab) and then the message and it'll be structured like code.

Code: Select all

main() {
        foo() {
                bar() {
                }
        }
}
not everyone likes this style of bracket, so you can easily adjust it to put the leading one underneath

in C you can make it simpler by using the %*c method, so printf("%*c{\n",nestDepth,'\t' ); works but i wanted to expand the idea.

The stuff gets more complex, but its basically things like maintaining a linked list of allocations, with what are called guard bytes ( add on 2*int) to every allocation then write a special int to the beginning and end of the allocated data, then you return the memory pointer+int to the caller, so you have to modify malloc/free/new/delete etc. Then in the epilog/prolog code you run through and check the guard bytes haven't been overwritten.

so its the malloc routine does something like ( again excuse any typos or logic errors)

the idea is you want the memory to look like

0xDE, 0xAD , n * size , 0xF0, 0x0D

where n*size is the memory that calling program is allowed to use, it is not allowed to touch the first two bytes, or the last two bytes (given an int is 16 bit)

Code: Select all


// assuming a 16 bit int
#define GUARD_1 ( 0xDEAD) 
#define GUARD_2 ( 0xF00D) 

void *malloc( size_t size)  {

  int *temp;
  // add some space for the guard bytes
   size += sizeof(int * 2 ) ;

 // allocate original space + two extra int's 
 temp = malloc( size ) ;

 temp[0] = GUARD_1;
 temp[size+2] = GUARD_2; 

// skip the guard int
 temp ++;
   // pass back the memory the program should use
  return (void *)temp;
}
the free function, takes the address , subtracts an int and checks the guard bytes.

phew, thats a lot to take in , feel free to ask for clarification, the guard bytes is a very common technique so there are lots of web pages on it, probably explained a lot better than i did ! ;)

for java its a bit different, but the ideas are applicable.

( Edited for some typos :) )
Last edited by charliex on Thu Feb 11, 2010 3:19 pm, edited 1 time in total.

charliex
 
Posts: 179
Joined: Sat Nov 28, 2009 5:54 pm

Re: Curly brackets (Warning non-adult content)

Post by charliex »

Oh yeah, __FILE__ and __LINE__ are defined by the c preprocessor standard, so it fills them in automatically with the filename of the source and the line number its used on, some compiler have __FUNC__ but not many unfortunately, so i just use a name, and i sometimes put the full prototype type in, i also add a priority level, so high/medium/low which helps to cut down on the noise.

illian
 
Posts: 18
Joined: Thu Dec 31, 2009 4:07 pm

Re: Curly brackets (Warning non-adult content)

Post by illian »

Charliex, thanks for your time in explaining all that!

Most of that was way over my head, but most things are at my level at the moment! :D I'll take a wander over to google and look up some of the vocabulary, I'm sure I'll have some questions..

The first questoin that comes to mind is the Arduino is C++ based as I recall... is that right? While hunting for the answer / reason to this ;
when I tried to watch an LED pin going high or low, I didn't get the serial monitor print of HIGH /LOW... but the pin number! eg; serial.print(ledPin) came back with 0 or 45 (the pin number of ledPin)!


I seem to remember seeing something about boolean "1" wasn't guaranteed in C (could be anything other than 0) but this could be different in C++ and therefore in Arduino too?? does that make sense to anyone?

charliex
 
Posts: 179
Joined: Sat Nov 28, 2009 5:54 pm

Re: Curly brackets (Warning non-adult content)

Post by charliex »

Sure i'd be happy to explain in simpler steps if you like, all its doing is adding one to a counter everything you enter a function, then subtracing one from the count when you leave it, then it prints out as many spaces as there is in the count.

C doesn't have a boolean type, C++ does. In later C++ editons bool is a builtin and is either true or false, the compiler will take care of any conversions, at a possible performance hit. when you printf a bool, it becomes 1 or 0

you can also use the C ? : operator. so

the difference here with this Bool and c++ bool is that its not typesafe.

Code: Select all

#define TRUE (1)
#define FALSE (0)

typedef Bool int;

Bool ret ;

ret = read_adc()?TRUE:FALSE;

will test for read_adc's return for 0 or > 0 and set either ret to 1, or 0. its short hand for if( read_adc() ) ret = 1 ; else ret = 0;

Locked
Please be positive and constructive with your questions and comments.

Return to “Test Message Forum (closed)”