ROBOTC.net forums
http://robotc.net/forums/

Function pointers/custom events
http://robotc.net/forums/viewtopic.php?f=1&t=2206
Page 1 of 1

Author:  miguelb [ Wed Feb 17, 2010 3:27 pm ]
Post subject:  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?

Author:  Aswin [ Wed Feb 17, 2010 4:31 pm ]
Post subject:  Re: Function pointers/custom events

Not to be blunt, but the answer is short. No you can't.

Author:  elizabeth.mabrey [ Sat Mar 03, 2012 12:57 pm ]
Post subject:  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

Author:  Spiked3 [ Mon Mar 05, 2012 1:05 pm ]
Post subject:  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.

Author:  elizabeth.mabrey [ Mon Mar 05, 2012 2:47 pm ]
Post subject:  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.

Author:  Spiked3 [ Mon Mar 05, 2012 6:26 pm ]
Post subject:  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.

Author:  UnforeseenGamer [ Wed Mar 30, 2016 3:31 pm ]
Post subject:  Re: Function pointers/custom events

For those curious, BNSLib has some event-bus-like functionality. I've never tested it on Mindstorms but I don't suppose it would be too hard to port over from the Cortex.

Example: https://github.com/JMarple/BNSLibrary/blob/dev/Examples/EventBusExample.c
(Note that the eventbus is only on the dev branch on github, I never got around to updating the main branch)

EDIT: Apologizes for bumping an old thread... I came across this in google and thought this was a couple weeks old, not 4 years old *doh*

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/