Events in Processing/Arduino

Hey, I've code alot of C#, and im wondering. Is there a way of implementing events into arduino / processing?

For example my main code can contain a class which can call functions in the main code.

Thank you

Serial data handling, mouse button activation/mouse movement, keyboard actions, etc. are some of the activities that Process already implements using events, so, yes it is possible to have Processing be event driven.

For example my main code can contain a class which can call functions in the main code.

Class instance objects can call sketch functions. The relationship between this and event handling is not clear.

If you are more specific on what you want to do, we can explain how to do it, if it is possible.

Main.pde

#include <Packet.h>

int port = 9600;
Packet pr;

void setup()
{
  Serial.begin(port);
  pr.Boot(port);
}

void loop()
{
  pr.Update();
}

void onData(int data)
{
  Serial.println("Data:" + data);
}

Packet.h

#ifndef Packet_h
#define Packet_h

#include "WProgram.h"

class Packet
{
  public:
      void Boot(int speed);
    void Update();
};

#endif

Packet.cpp

#include "WProgram.h"
#include "Packet.h"

void Packet::Boot(int speed)
{
      Serial.print("[PacketReader] Started on speed: ");
      Serial.println(speed);
}

void Packet::Update()
{
      if (Serial.available() > 0)
      {
            int incomingByte = Serial.read();
            
            // I want to call onData
            
      }
}

The best way for you to do this is to add a method to allow the user of the class to specify a function to be called when Update has something to do. Define the signature that the callback needs to have, and create a pointer to a function with that signature as a member of the class.

An example of the signature:

void SetColorByStyleCallback(void (*pFunc)(int style,
               unsigned int *red,
               unsigned int *grn,
               unsigned int *blu));

An example of the member to hold the pointer to the function:

void (*pColorByStyleFunc)(int style,
              unsigned int *red,
              unsigned int *grn,
              unsigned int *blu);

Then, the sketch should register a callback:

pr.SetColorByStyleCallback(GetColorForStyle);

Then, in the Update method, call the callback function, if one is registered:

if(pColorByStyleFunc)
{
      pColorByStyleFunc(cellStyle, &ioRed, &ioGreen, &ioBlue);
}

The callback function can be as simple, or as complicated, as you like. In the case you are asking about, it will be much simpler.

Hmm, i tried it the way you said and didn't work, I'm not very experienced at C++,

In function setup': C:\Users\Lucas\AppData\Local\Temp\build7867387122952549225.tmp/dakfnsdon.cpp:15: undefined reference to Packet:: onDataCallback(void (*)(int))'

Main.pde

#include <Packet.h>

int port = 9600;
Packet pr;

void setup()
{
  Serial.begin(port);
  pr.Boot(port);
  
  pr.onDataCallback(OnData); 
}

void loop()
{
  pr.Update();
}

void OnData(int data)
{
  Serial.println("Data:" + data);
}

Packet.h

#ifndef Packet_h
#define Packet_h

#include "WProgram.h"

class Packet
{
  public:
      void Boot(int speed);
    void Update();
      void onDataCallback(void (*dFunction)(int data));
};

#endif

Packet.cpp

#include "WProgram.h"
#include "Packet.h"

void (*dFunction)(int data);

void Packet::Boot(int speed)
{
      Serial.print("[PacketReader] Started on speed: ");
      Serial.println(speed);
}

void Packet::Update()
{
      if (Serial.available() > 0)
      {
            int incomingByte = Serial.read();
            
            if(dFunction)
            {
                  dFunction(incomingByte);
            } 
            
      }
}

I left out the implementation of the callback registration:

void Class::SetColorByStyleCallback(void (*pFunc)(int style,
       unsigned int *red,
       unsigned int *grn,
       unsigned int *blu))
{
      pColorByStyleFunc = pFunc;
}

Wow! Thank you so much :smiley:
You really helped me :]
Thanks