Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

The journey of a life time.....

Status
Not open for further replies.
The brackets define the function...


for example, with:

int main ()
{

<stuff for the function main goes here>

}

A couple questions to seasoned c coders (I'm a bit rusty myself) - should there be a semicolon after the int main statement (I don't think...) aaaand do the parentheses on int main define what can be passed to other programs (global/local)?

I like doing 3d animations with Pov-Ray ( www.povray.org ) which uses a C-ish language to define objects...it's really worth checking out.

Oh, ok. That makes sense. So all the brackets are for, i just to define a small block of code that needs to be grouped into a function? Hmm, that sounds simple. Maybe C isn't that complicated?
Honestly, i am starting to like it! :) So, if someone would answer my erlier; "Is C just Assembly on steroids?".
 
Marks256 said:
So, if someone would answer my erlier; "Is C just Assembly on steroids?".

No, it's nothing like assembler - except that as a compiler it generates assembly language, but a Pascal or BASIC compiler does that as well.
 
lol. Tell me, what's so complicated with C/C++ ?
It's the simplest language I've ever been faced with...

My advice:
Take some code snippets, open source projects, etc. and analyze it... try to learn from it, adapt the functionality in your own code, modify it, and so on. Plus don't forget a good C/C++ textbook.

Once you got the concept, complex design patterns won't be a problem. And I'd suggest you to learn a bit of ASM, because it's a great tool to increase the performance of your code.
 
Last edited:
_nox_ said:
lol. Tell me, what's so complicated with C/C++ ?
It's the simplest language I've ever been faced with...

It's all down to what you're used to, as a BASIC/Pascal/Assembler programmer I find C horrible and very hard to read - and all those curly brackets drive me MAD! :D
 
Nigel Goodwin said:
It's all down to what you're used to, as a BASIC/Pascal/Assembler programmer I find C horrible and very hard to read - and all those curly brackets drive me MAD! :D

C is absolutely clear to read ( at least for me )...
Sure, BASIC is much simplier for low-level thinkers, because it's almost like english :cool:
 
Are you calling me a low level thinker? I think you are. I will have you know, that C is a whole 'nother animal to me, compared to BASIC, and similar languages.
 
Awe C'mon - This was pretty obviously a joke :D Be cool.

C isn't that hard ... As I said, take a good textbook and start learning.
That's the whole magic.
 
Crap, my smiley didn't show up. I wasn't being too serious. :) <-There is my smiley! :cool: <-here is another one! :) <-And one more for show.
 
Just a word of warning - don't confuse C and C++. They are vastly different languages, despite the fact that they look similar. C++ is a seriously complicated minefield and, to put it bluntly, is pretty icky. It's also not nearly as widely supported as C for low-level/embedded hardware programming.

A note on the curly braces: code blocks are pretty much the fundamental building brick of C programs. A block of code is simply a set of instructions delimited by curly braces. They can contain any number of statements, including zero.

By themselves, blocks aren't all that grand. When they really come into their own is when you associate a block with something. For instance, the simplest example is a function:

int main()
{
int a = 4;
int b = 6;
int c = (a + b);

return 0;
}

The block is associated with the function "main" and is referred to as the body of the function.

You will also associate blocks with other things: if statements, loops, and so on. All of those should be pretty familiar from BASIC. A helpful trick is to think of the braces as the equivalent of IF/END IF and so on; the closing brace is the "end" of the code associated with the conditional/loop/function/etc.

There are a few other cases where braces are used that are slightly more advanced, but they're not worth messing with until you get nice and used to the language itself.


The main thing to remember in C is that you're pretty close to the hardware. Having a solid understanding of how memory addressing, functions, the stack, and the freestore work will really help your C coding. That's an awful lot of stuff to pick up, though, so don't be discouraged if it takes some time for things to "click."
 
I love the curly brackets and I love how they are always the same for grouping blocks of code together...unlike Pascal where it's different keywords that group the blocks of code together depending on what you are doing. It's inconsistent! It'd MADNESS!
 
Pascal is even easier, you essentially use 'begin' and 'end' to mark the blocks - far easier to spot than loads of curly brackets - I REALLY hate the curly brackets! :D
 
It takes 5x the effort to read begin as opposed to { and 3x the effort to read end as }. I just like reading math more than an english paper when it has something to do with logic. For me, I find the keywords get lost up in the other words. I indent with curly brackets and it sticks out like a sore thumb.
 
Last edited:
Eh, delimiters are totally subjective. Most programmers in Pascal/Basic dialects indent as well, and in some languages (like Python) indentation actually replaces the use of begin/end or braces.

There's something to be said for the typing savings of braces over keywords, but even still it's a matter of choice. Most of the time, it is vastly more work to figure out the logic of the program than to actually key it in. It really comes down to arbitrary choices in the end, unless of course your chosen platform has really weak support for a particular language.
 
I can't understand you all... C/C++ is so damn great and simple -
 
Urgh. C++ is the most vile and ugly language ever to curse the world. C is clean and elegant - it's a beautiful enough language for what it was meant to accomplish originally - but it just doesn't have the tools of abstraction needed for most modern software development.

Now if MCUs started supporting ML or Lisp dialects, I'd be in heaven.
 
ApochPiQ said:
Urgh. C++ is the most vile and ugly language ever to curse the world. C is clean and elegant - it's a beautiful enough language for what it was meant to accomplish originally - but it just doesn't have the tools of abstraction needed for most modern software development.

Now if MCUs started supporting ML or Lisp dialects, I'd be in heaven.

Show me the "ugly" points - I don't see them.
 
Nigel Goodwin said:
Everyone else does! - what you're saying is that your personal preference is for C/C++ - and that's all it is, YOUR personal preference.
Yes - So please show me the ugly points ... :) And stay cool.
 
Hmmm... My personal opinion about the curly brackets is rather good... they keep the code logical and simply readable... it's not unclear where a function ends, and where it starts.

Some code:

Code:
/**
 * @(#) core.hpp
 */

#pragma once
#ifndef core_hpp_h
#define core_hpp_h

#include "task.hpp"
#include "../globdefs.hpp"

/**
 * Main System Namespace
 */
namespace _msys
{
	/**
	 * Core class.
	 * $Description:
	 *    - Heart of the application. Manages the tasks and events.
	 *    - Takes care of memory
	 *    - Handles the errors
	 *    - and so on
	 */
	class core
	{
		
	public:
		/**	
		 * Constructor
		 */
		core( );

		/**
		 * Destructor
		 */
		virtual ~core( );

		void execute( );                                      /* Executes the core. */
		void add_task( IN _aux::mem_pointer< task > & );      /* Adds a task to the core */
		void kill_tasks( );                                   /* Kills all tasks. */
		void remove_task( IN _aux::mem_pointer< task > & );   /* Removes task from the core */
		void shut_down( );                                    /* Shuts the application down */

		/**
		 * Get active task list size ( for application : class )
		 */
		size_t get_alist_size( ) const;

	private:

		/**
		 * Lists all paused tasks. || Info: Another way to 
		 * to do this, would be a bIsPaused pointer. But I 
		 * assume this a much faster way to perform task pausing.
		 */
		std::list< _aux::mem_pointer< task > > paused_task_list;
		
		/**
		 * List of active tasks
		 */
		std::list< _aux::mem_pointer< task > > task_list;
	};
}

#endif

Code:
/**
 * @(#) core.cpp
 */

#include "core.hpp"

using namespace _msys;

/**
 * Constructor
 */
core::core( )
{
}

/**
 * Destructor
 */
core::~core( )
{
}

/**
 * Shuts the application down
 */
void core::shut_down( )
{
	kill_tasks();
}

/**
 * Executes the core.
 */
void core::execute( )
{ 
	std::list< _aux::mem_pointer< task > >::iterator it; /* Define iterator */

	for( it  = task_list.begin( ); it != task_list.end( ); ) /* Run through all tasks inside the active */
															 /* task list                               */
	{
		task *_t = NULL;
		_t = ( *it );
		++it;

		if( !_t->bCanKill ) /* If task shall stay active, update it */
		{
			/*
			 * Update task.
			 */
			_t->update( );
		}
	}

	/*
	 * Loop again to remove dead tasks.
	 */
	for( it  = task_list.begin( ); it != task_list.end( ); ) /* Run through all tasks inside the active */
		                                                     /* task list and check whether they shall  */
															 /* be removed.                             */
	{
		task *_t = NULL;
		_t = ( *it );
		++it;

		if( _t->bCanKill ) /* If task shall be removed , move it to the paused task list */
		{
			_t->stop( ); /* Call stop-procedure of task */

			/*
			 * Remove task from active list and set it to zero
			 */
			task_list.remove( _t ); 
			_t = NULL;
		}
	}

	/*
	 *  Call garbage collection function
	 *  from memory managment object
	 */		
	_aux::mem_object::collect_garbage();

}

/**
 * Adds a task to the core
 */
void core::add_task( IN _aux::mem_pointer< task > &_t )
{
	/*
	 * Execute task immediately ( Errors are captured by exception handling )
	 */
	 _t->start( );
	
	std::list< _aux::mem_pointer< task > >::iterator it; /* Define iterator */
	
	/*
	 * Keep the order of priorities straight.
	 */
	for( it = task_list.begin( ); it != task_list.end( ); ++it )
	{
		_aux::mem_pointer< task > &_comp = ( *it );
		if( _comp->lPriority > _t->lPriority )
		{
			break;
		}
	}
	task_list.insert( it, _t );
}

/**
 * Kills all tasks
 */
void core::kill_tasks( )
{
	/*
	 * Run through each task and set its kill flag to true
	 */
	for( std::list< _aux::mem_pointer< task > >::iterator it = task_list.begin( ); 
		 it != task_list.end( ); ++it )
	{
		( *it )->bCanKill = true;
	}
}

/**
 * Removes task from the core
 */
void core::remove_task( IN _aux::mem_pointer< task > &_t )
{
	
}

/**
 * Get active task list size ( for application : class )
 */
size_t core::get_alist_size( ) const
{
	return task_list.size();
}

Code:
/**
 * @(#) gui_control.hpp
 */

#pragma once
#ifndef gui_control_hpp_h
#define gui_control_hpp_h

#include "../task.hpp"
#include "layout.hpp"

/**
 * Context includes
 */
#include "../opengl_context.hpp"
#include "../gdiplus_context.hpp"
#include "../audio_context.hpp"
#include "../input_context.hpp"

/**
 * Control includes
 */
#include "../_cli/cli_control.hpp"

/**
 * Main System Namespace
 */
namespace _msys
{
	/**
	 * Contains everything necessary for GUI managment
 	 */
	namespace _gui
	{
		/**
		 * Main GUI managment class
		 */
		class gui_control: public task
		{
		public:
			gui_control( );
			virtual ~gui_control( );

			/**
			 * Add layout
			 */ 
			void add_layout( IN layout );

			/**
			 * Switch layout
			 */
			void switch_layout( );

			/**
			 * render the gui
			 */
			void render( );

			/**
			 * Connect the Input/Audio and Graphics interfaces
			 */
			void connect_input( IN _aux::mem_pointer< input_context >& );
			void connect_gdiplus( IN _aux::mem_pointer< gdiplus_context >& );
			void connect_opengl( IN _aux::mem_pointer< opengl_context >& );
			void connect_audio( IN _aux::mem_pointer< audio_context >& );
		
			/**
			 * Connect CLI Control
			 */
			void connect_clicontrol( IN _aux::mem_pointer< _cli::cli_control >& );

			/*-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-*/

			virtual void start( );											/* Executes the task                            */
			virtual void stop( );											/* Stops this task                              */
			virtual void sleep( IN const unsigned int );					/* Stops the task for a specified time interval */
			virtual void reset( );											/* Resets the task                              */
			virtual void update( );										    /* Updates the task                             */
			virtual void perform_event( IN event* );					 	/* Lets the task perform an event               */

			/**
			 * Realization of a function in mem_object
			 */
			virtual unsigned long size();

		private:

			/**
			 * Class describing the gui-layout ( list of various layouts which can be switched )
			 */
			std::vector< layout > layout_list;

			/**
			 * Contexts ( OpenGL -> 2D/3D Graphics, GDI+ -> 2D Graphics, DSound -> Audio, HID/DInput -> Human Interface Devices )
			 */
			_aux::mem_pointer< _msys::opengl_context >  spOpenGLContext;
			_aux::mem_pointer< _msys::gdiplus_context > spGDIPlusContext;
			_aux::mem_pointer< _msys::audio_context >   spAudioContext;
			_aux::mem_pointer< _msys::input_context >   spInputContext;

			/**
			 * CLI-Control 
			 */
			_aux::mem_pointer< _cli::cli_control > spCLIControl;
		};
	}
}
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top