InterruptIn in class with callback


I’m trying to create a class that will be able to read pin states of my board (Nucleo-F411RE) using interruption. And when interrupted, send a callback to the object that has instantiated my class to notify this state change and send on Bluetooth the new state.
I’ve managed to receive the interruption in a thread:

 _sendingThread.start(callback(&_eventQueue, &EventQueue::dispatch_forever));          
_myInterruptIn.rise(_eventQueue.event(this, &Sensors::OnWheelRise));

void Sensors::OnWheelRise()
_values[0] = true;

I have several problems here, first the member variables such _sendCallback (bool) is not keeping its value in the thread, and how to create the SendCallback method that will be able to send the data on Bluetooth which has been created on the main thread.

I’m a bit lost on how, inside a class, to access the members across threads and to change the context between IRQ, secondary thread and main thread.

Thanks for your help.

Welcome to the forum. Please use code blocks. You can produce a code block by placing triple backticks before and after your code.

// your code

I would do something like this to attach a callback.

The header file:

class Sensors {
	// ... constructor, etc.
	void attach(Callback<void()> function);

    // ...
	Callback<void()> _cb;

The implementation file:

// ...
void Sensors::attach(Callback<void()> function) {
    _cb = function;

void Sensors::OnWheelRise() {
    if(_sendCallback) {;

The main:

#include "mbed.h"
#include "Sensors.h"

Sensors sensors;

void onCbCalled() {
    // send data through BLE

int main() {
    // ...

I was not sure what you meant by “variables not keeping their values”. Maybe you are missing volatile keyword and they don’t get modified in the ISR context? Do you want to show us the relevant part of your code?