Passing int arguments in mbed::Callback

Viewed 53
#include "mbed.h"
#include <Callback.h>

InterruptIn up(p14);

void toggle1(int *player)
{
    printf("%d \n", *player);
}

int main()
{
    int player = 1;
    up.rise(callback(toggle1, &player));
}

In the mbed callback function, why the result is not 1? It is 12784.

1 Answers

You are allowing your main function to return. After it returns, player will be out of scope so it might be overwritten with something else. In general, you never want to return from main in an embedded system, so I recommend adding while (1) {} at the end of your main function.

Also, using callback is a source of unnecessary complication and potential errors. I would just put player in a global variable (and mark it as volatile). Then you can simply do up.read(&toggle1).

Related