Interfacing from a library to a program

Yesterday i wrote a library which reads input data from a given pin, transferring it to one (or more) command(s), and (should) give it to the program i just wrote. Now my Question is: how can i make the library invoke a method in my program? Is there a reference like 'static' which i can put in front of my method in the library or something else?

My library is in the attachments (sry it's written in german :wink: ) .

Here is my program:

#include <Steuerung.h>

Steuerung s(A0, A1, A2);

void setup() {
Serial.begin(9600);
}

void loop() {
s.looop();
delay(100);
}

void buttonEvent(int but, boolean pressed) {
Serial.println(String(but)+"\t"+String(pressed));
}

Hoping someone knows how to solve my problem! :slight_smile:

Steuerung.cpp (2.09 KB)

Now my Question is: how can i make the library invoke a method in my program?

The library knows nothing about the sketch that is using it except for what you tell it. Your library needs to have methods to register callbacks for specific events. You then register functions in the sketch that the library should be called when the event happens.

Steuerung::Steuerung(int x, int y, int taster) {
  pinMode(x, INPUT);
  pinMode(y, INPUT);
  pinMode(taster, INPUT);
  
  pinx = x;
  piny = y;
  pinTast = taster;
  
  normx = analogRead(x);
  normy = analogRead(y);
}

Just about every that could possibly be done wrong in a constructor is being done wrong here.

The hardware is not ready to have the mode of the pins set. Analog pins are read only. The pinMode() function only affects digital pins.

Reading from pins that are not ready is not useful.

void Steuerung::looop() {

Good method names consist of a noun and a verb (what to do, to what), like digitalRead() reads from a digital pin. This name is next to useless. It tells up nothing about what the method does.

Amazing fact that such a well educated member is not able to answer such an easy question

He answered it. The library doesn't know about functions in the sketch. That defeats the purpose of writing a library. If you need the library to call functions from the sketch, then the library needs a function that takes a pointer to the function in the sketch That you want to call and registers it as a callback.

He is also spot on that your constructor is going to cause bugs down the road. Ignore that at your own peril.

So can you tell me how i can make such a pointer?

Timer1 is a library. It has an attachCallback() method that lets the sketch tell the library what function to call when the event of interest happens. In the Timer1 case, there is only one event of interest - the timer went off.

You seem to want to have several events of interest. You can either follow the model of Timer1 and have one event of interest, and leave it up to the sketch to determine what to do, or you can have several methods like attachInterrupt() for different events.

Now that you know what you want to do can be done, it shouldn't be all that difficult to look at the Timer1 library to see how to do it.

Or offer to pay for the help you seem to think is owed you.

Google "C++ function pointers." It is a big topic to try to put in one post and there isn't enough of your program showing to make any sort of useful example.