Page 1 of 1

NXC classes

Posted: 08 Apr 2012, 03:31
by mattallen37
Is it reasonably likely that NXC will support classes in the not-so-distant future?

Class support could be a huge time and resource saver.

I guess the answer is probably no, because otherwise it would likely already support them.

Re: NXC classes

Posted: 08 Apr 2012, 14:55
by mightor
If you're looking for an OOPL, you might want to check out Lejos. A language where OO is an afterthought will end up feeling like an ugly kludge, like Perl's OO implementation.

- Xander

Re: NXC classes

Posted: 09 Apr 2012, 01:23
by mattallen37
In the past I have considered leJOS, but for now I want to stick with C type languages. I feel like I am fairly versed in NXC, as it's the primary language I used for learning programming concepts, and I would like to be able to learn more about classes in a familiar setting. So far I have only used classes in C++ for Arduino libraries, and I really love them.

Re: NXC classes

Posted: 09 Apr 2012, 06:25
by mightor
Lejos is not that scary :) It has a very rich collection of classes, I think you'll like it.

- Xander

Re: NXC classes

Posted: 09 Apr 2012, 19:05
by afanofosc
I think Matt would like leJOS too. The only drawback being that you have to switch to a firmware that does not support NXT-G, if you want to keep using that language for some reason.

In any case, I have no plans for trying to add support for classes to NXC.

John Hansen

Re: NXC classes

Posted: 09 Apr 2012, 22:00
by mattallen37
Thanks both of you.

I'm not concerned in the least with putting leJOS FW on one of my NXTs ;)

Okay, that's fine.

I'll look into trying out leJOS.

Re: NXC classes

Posted: 10 Apr 2012, 05:02
by tcwan
mattallen37 wrote:In the past I have considered leJOS, but for now I want to stick with C type languages. I feel like I am fairly versed in NXC, as it's the primary language I used for learning programming concepts, and I would like to be able to learn more about classes in a familiar setting. So far I have only used classes in C++ for Arduino libraries, and I really love them.
Well, you don't actually have to run Java. There is also nxt-OSEK which you can program in C++ http://lejos-osek.sourceforge.net/

Re: NXC classes

Posted: 11 Apr 2012, 12:34
by pepijndevos
Or Mirah, which is more like Ruby than C, I guess, but anyway, runs like a charm on Lejos.

Re: NXC classes

Posted: 14 Apr 2012, 12:08
by spillerrec
mightor wrote:If you're looking for an OOPL, you might want to check out Lejos. A language where OO is an afterthought will end up feeling like an ugly kludge, like Perl's OO implementation.
I do like the C++ syntax though and I think OO would be a nice addition to NXC. I did make some deeper considerations on how to make OO work on the NBC level and I think it would feasible to have basic OO support in NXC. (I wrote a post with my thoughts on my blog back then.)

One of the reason I think OO would work well with NXC is because of the underlying firmware. Many of the basic functions is implemented by the use of structs so an OO interface to those makes much sense. For example LineOut could be implemented like this:

Code: Select all

class Line{ //This class is the DrawLineType struct type
	public:
		char Result;
		LocationType StartLoc;
		LocationType EndLoc;
		unsigned long Options;

		Line( LocationType start, LocationType end ){
			StartLoc = start;
			EndLoc = end;
		}
		
		Line( int x1, int y1, int x2, int y2 ){
			StartLoc.Set( x1, y1 );
			EndLoc.Set( x2, y2 );
		}
		
		void draw(){
			SysDrawLine( this ); //ASM here, just can't remember it from the top of my head...
		}
		
	private:
		static Line temp;
		static mutex tempmutex;
	public:
		static char draw( int x1, int y1, int x2, int y2, Options = DRAW_OPT_NORMAL ){
			Acquire( tempmutex );
			temp.StartLoc.Set( x1, y1 );
			temp.EndLoc.Set( x2, y2 );
			temp.Options = Options;
			temp.draw();
			return l.Result; //This would need to be done in ASM so Release is actually executed
			Release( tempmutex );
		}
		
		static char draw( LocationType start, LocationType end, Options = DRAW_OPT_NORMAL ){
			Acquire( tempmutex );
			temp.StartLoc = start;
			temp.EndLoc = end;
			temp.Options = Options;
			temp.draw();
			return l.Result; //Same note as above
			Release( tempmutex );
		}
};
Taking the notes in account, this is pretty much the same way as it works in NXC right now just in OO style. The only difference is that this is inline functions where the NXC headers uses macro functions. (Sadly, the NXC compiler didn't do a good job with inline functions last time I checked...)
You could make some pretty neat API improvements imo.

Re: NXC classes

Posted: 21 Apr 2012, 20:23
by mightor
pepijndevos wrote:Or Mirah, which is more like Ruby than C, I guess, but anyway, runs like a charm on Lejos.
I had a quick peek at Mirah but I am not seeing the advantages of running that on a JVM vs Java. What can I do with Mirah that Lejos won't let me? How would you use it combination with Lejos' classes or is that not an option and would one have to redevelop all of those?

- Xander