View unanswered posts | View active topics It is currently Tue Sep 16, 2014 5:42 am






Reply to topic  [ 6 posts ] 
Function pointers/custom events 
Author Message
Rookie

Joined: Mon Jan 04, 2010 10:51 am
Posts: 15
Location: Portugal
Post Function pointers/custom events
I have a method that runs on a specific task and that receives messages from the pc and translates that message to an internal robotc struct.

This is already working OK, but now I want to reuse this method in several different programs, by having a single .h file that I can include (much like is done with the 3rd party sensors). To do that I need to create some event notification mechanism that will allow me to notify any potential listeners passing them the (decoded) message struct.

In an object oriented language I would use virtual methods and inheritance. In c/c++ I would probably use function pointers. In desperation I would use global variables and polling on the subscriber (:)). Is there any way to do this in robotc without using the later? For what I could see the SetEvent works only for specific sensor-based events - can I create new events and trigger them from my code?


Wed Feb 17, 2010 3:27 pm
Profile
Expert
User avatar

Joined: Mon Oct 06, 2008 6:30 pm
Posts: 176
Location: Netherlands
Post Re: Function pointers/custom events
Not to be blunt, but the answer is short. No you can't.

_________________
My most recent blog: A grain of sugar


Wed Feb 17, 2010 4:31 pm
Profile WWW
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 136
Location: New Jersey
Post Re: Function pointers/custom events
Does the latest 3.0x support events?

Code:
const byte kTimerEvent2 = 2;
...
SetEvent(kTimerEvent2, EST_TIMER_1, eventTypeEnterHigh);

generate error:Parameter 'BAD75[1985246804]' must evaluate to constant byte value


Sat Mar 03, 2012 12:57 pm
Profile WWW
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Function pointers/custom events
Aswin wrote:
Not to be blunt, but the answer is short. No you can't.


He may not be able to use events as in other languages, but the long answer is you can write object oriented assembler if you want to, if you understand what object oriented means, its easy in a language like C.

What is an event? A piece of code waits for notification that something has happened.

over simplified psuedo code;

task sometask { while (true) { if (eventOccurred) doSomething(); yield(); }}

task main { start sometask; while (true) {if (haveMail) readMail(); if (mail == eventMail) eventOccurred=true; yield(); }}

If you want to get real fancy you build something called a message pump. google it.

The addition of function pointers in the future may make life easier, but there is nothing stopping you now from doing what you want to do. The bottom line is that a computer is a machine that can only add, multiply by power of 2, and compare+jump, 3 instructions. They expand on that and give us machine level code (microcode/assembler), then expand on that and give us C, next step is object oriented (e.g. C++) - you simply need to understand how those expansions occur and do a little on your own in some cases.

"Is there any way to do this in robotc without using the later?" Don't fear it, polling is how things work. the software often makes you believe it is not polling, but it ALWAYS is. Just keep your code 'multitask friendly' and it will be fine.

_________________
Mike aka Spiked3
http://www.spiked3.com


Mon Mar 05, 2012 1:05 pm
Profile
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 136
Location: New Jersey
Post Re: Function pointers/custom events
Yes, I am well aware of how to create my own concurrency implementation with semaphores and all, so that an event-driven mechanism works seamlessly. I was a system level developer, writing IPC for multiple OS, etc. However, I was hoping that I could have my high school students to take advantage of the event driven mechanism if it indeed existed. They are currently working under time constraint, I was hoping that they could be the user instead of having to learn the true concurrency implementation.


Mon Mar 05, 2012 2:47 pm
Profile WWW
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Function pointers/custom events
Ok, got you. Hard to tell sometimes what level a person asking the question is. I just wanted a 'newer' programmer not to depend on shortcuts as the only way. I agree then, under a time constraint, then the short answer is 'no' as already said.

_________________
Mike aka Spiked3
http://www.spiked3.com


Mon Mar 05, 2012 6:26 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.