Pages: 1 2 3 [4]   Go Down
Author Topic: Event-Based Library for Arduino  (Read 14329 times)
0 Members and 1 Guest are viewing this topic.
São Paulo
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Renato
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pleasant!!! cool...  smiley-red

Everything is now fixed! Have fun! smiley-cool
« Last Edit: April 15, 2012, 10:28:20 am by Rferreira » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I love this library!

The TimedEventClass has start(eventId) and stop(eventId) to start and stop a timed event. This makes it easy to enable /  disable individual timer events as my app goes through different states.

But I could not find a way to dynamically enable / disable the other kinds of events e.g. AnalogEvent or ButtonEvent. So I cannot enable / disable these other kinds of events as my app goes through different states. Would it make sense to consider either start()/stop() methods, or removeX() methods corresponding to the addX() methods for these other event types? It would make FSM-style programming easier.

Thanks!
« Last Edit: April 17, 2012, 12:11:49 pm by Sophie424 » Logged

Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 183
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Although I am a relative newbie with Arduino, I used to program in Visual Basic. There are lots of events in VB. I have found these event driven routines you wrote extremely helpful for my project. I'd like to see a "serial data available" event for serial 1 or 1-4(mega) .

#include serTrigger.h
void setup(){
  serTrigger.setser (Serial. event1);
  serTrigger.setser (Serial1. event2);
//etc
}
void loop(){
  serTrigger.loop();
}
void event1()}
read serial into variable, set a flag and return
}

Sure beats polling, or constantly calling a subroutine. It would sure clean up my code! I'd do it myself if I knew how. I had bad luck with wri
ting functions.
Jim
Logged

8000ft above the average

Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 183
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, since I looked, that has been added.
Jim
Logged

8000ft above the average

São Paulo
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Renato
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But I could not find a way to dynamically enable / disable the other kinds of events e.g. AnalogEvent or ButtonEvent. So I cannot enable / disable these other kinds of events as my app goes through different states. Would it make sense to consider either start()/stop() methods, or removeX() methods corresponding to the addX() methods for these other event types? It would make FSM-style programming easier.

Well, Sophie. Timers are usually needed to be started and stoped, but it did not make sense with buttons and similars when theses classes were done. However your idea for FMS logic made sense and I will think in something to solve it within next days.

 smiley-wink

Although I am a relative newbie with Arduino, I used to program in Visual Basic. There are lots of events in VB. I have found these event driven routines you wrote extremely helpful for my project. I'd like to see a "serial data available" event for serial 1 or 1-4(mega) .

...

Jim, I will try to think in something for it as well, but I will never be able to test if it worked because I don't have a mega board. Are you willing to be a tester for it?
« Last Edit: April 20, 2012, 08:21:06 pm by Rferreira » Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 1
Posts: 93
Profile before you Optimize.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah nice lib!

I like the event based approach and I am working on something similar my self.
I still have to give your lib a thorough look so forgive me if I sound stupid  smiley-mr-green

The event based mechanism solves the spaghetti code problem I see in most project's source code. But I want to go even further. I also want to solve the problem of an LCD library only working when you connect the correct pins or can only handle one or fails when you have shift-registers in your design to save on IO pins.

I am now working on a stream/event bases approach using C++ templates. Because with C++ templates you don't need most virtual (although I love polymorphism) and you dont need to keep reference pointers to event handlers! Try to calculate how many bytes are lost to pointer reference to functions or other objects in your lib!?

Here's what I mean:

Code:
// BaseT must implement OnEvent().
template <class BaseT>
class MyEventSource : public BaseT
{
public:
  void loop()
  {
    // some logic to detect events

    // call BaseT
    OnEvent(someValue);
  }
};


That is the core of what I'm doing in my library. It also allows to create your own class hierarchies. Simply typedef your choice of derived template classes. This allows you to mix and match stuff together to get what you want. So now you can use the LCD logic (a template class that makes no assumptions on what pins are used or how the actual hardware is driven) and stack it on a custom class that implements the shifting logic for your shift registers that actually drives the LCD. This solves the problem with the current lib impl's that mix a lot of responsibilities into one class and in doing so killing any flexibility to do things differently.

Another thing I want to solve - which is not hard at all, but creeps in real easy - is that you make classes that do only one thing (touched on this previously). What I mean is for example the serial class. Don't make a class that handles both sending and receiving - instead make 2 classes, one for reading and one for writing. I ran into this when I wanted to make a MIDI project that receives on one MIDI port but sends on 4 ports.

I am at the beginning of creating my lib so there are a lot of problems I will run into and have to find a solution for. And now I will take a look at your work.

EDIT: looking at the lib I would suggest not creating an event for each state (as with the button down-up etc). Instead I would suggest one event that also receives the state of the button. This saves the user of your class a lot of hassle when trying to implement combined logic and it hardly makes detecting a single state harder for the moderate users... Also something I ran into with the MIDI lib that also has separate callback for each type of message. It also wastes a lot of function references eating up memory.

EDIT2: I look at the impl of ButtonEvent. You use dynamic memory allocation to reserve memory for a ButtonInfo struct in the addButton. I am personally trying to avoid any dynamic memory allocation, due to the overhead and the mentioned bugs. Also I do not agree with the notion of that default instance. Its crap. Why not let the use instantiate a ButtonEvent class for each button he/she needs!? Buttons are not usually allocated dynamically ;-) So that will eliminate the need for any dynamic memory allocations. Also try to calculate how much memory is needed by one instance of your class. Its huge (in MCU terms). Do you really need 4 milli second long vars!? Do you think anyone will use a hold timeout of 4 hours!? :-P

Try not take this criticism too personally, just trying to help.
« Last Edit: April 22, 2012, 03:15:21 am by obiwanjacobi » Logged


Buena Vista, CO
Offline Offline
Full Member
***
Karma: 0
Posts: 183
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Although I am a relative newbie with Arduino, I used to program in Visual Basic. There are lots of events in VB. I have found these event driven routines you wrote extremely helpful for my project. I'd like to see a "serial data available" event for serial 1 or 1-4(mega) .

...

Jim, I will try to think in something for it as well, but I will never be able to test if it worked because I don't have a mega board. Are you willing to be a tester for it?
[/quote]

Because I'm working with multiple Arduino communicating with APC220's, I standardized on 'Mega's'. I use sensor shields with the APC220 socket, cut the pins for serial and jumper them to serial1. I'll gladly test, but if you send me an address, I'll buy one and send it to you. It will be a chinese clone, but to me since it's open source, that's no problem. I'll consider it my contribution to your excellent library of very useful functions.

Jim
1280 or 2560?
Logged

8000ft above the average

Pages: 1 2 3 [4]   Go Up
Jump to: