mbed-os-5のTicker

オンラインコンパイラからKeilStudioになり、os2から5へ移行せよとのことでトライしているのですが、Tickerのattachでエラーになります。

Ticker tbase;
int tb_reach=0;
void tbase_t(void){
    tb_reach=0;
}
int main(){
    tbase.attach( &tbase_t,0,1);

ビルドすると、
‘attach’ is deprecated: The attach function does not support cv-qualifiers. Replaced by attach(callback(obj, method), t).

どう書けばよいかがわからないんです。

Hello,

I do not know Japanase but…

tbase.attach(callback(tbase_t),0.1);

BR, Jan

Hello and thank you
But I get an error.

mbed-os5.15.7

compile main.cpp
/src/main.cpp:21:11: error: no matching member function for call to 'attach'
    tbase.attach( callback(tbase_t) , 0,1);
    ~~~~~~^~~~~~
/extras/mbed-os.lib/drivers/Ticker.h:109:10: note: candidate template ignored: could not match 'T *' against 'Callback<void ()>'
    void attach(T *obj, M method, float t)
         ^
/extras/mbed-os.lib/drivers/Ticker.h:91:10: note: candidate function template not viable: requires 2 arguments, but 3 were provided
    void attach(F &&func, float t)
         ^

it must be a dot, no comma.

BR, Jan

Ah! ,sorry. And thank you.
The error no longer appears.

But it doesn’t seem to work.
I’ll think about it.

os 5.15.7
Nucleo-F411

I do not know do you do in rest of code but this part only set your variable to zero every 100ms.

BR, Jan

1 Like

こんにちは。

Mbed OS 5.15.x でのTicker APIの使用例がこちらにあります。
https://os.mbed.com/docs/mbed-os/v5.15/apis/ticker.html

最初に記載されたコードの例だと、100ms毎にtb_reach変数に0を設定する感じになると思いますが、具体的にはどのように動かないのでしょうか?

ありがとうございます。試しているコードは下記になります。上の質問では0.1になっているところを0.5にしてます。

#include "mbed.h"

Ticker tbase;
DigitalOut led(LED1);

int tb_reach=0;
void tbase_t(void){
    tb_reach=0;
}
int main(){
    tbase.attach( &tbase_t , 0.5 ) ;
    while (true) {
        led = !led;
        while( tb_reach );
        tb_reach = 1;
    }
}

os5、exampke-blinkeyのthread_sleep_for(500);の代わりにTickerで0.5秒ごとにLEDを点滅させたいのですが、LED1端子は変化がありません。
関数 tbase_t の中に led=!led; を書くとLEDは点滅するのですが、tb_reachに0がセットされないようで、点滅しないのです。

ご説明ありがとうございます。こちらでも再現しました。

whileステートメントでのtb_reach変数の参照が最適化されてしまって、意図した動作になっていないようです。以下のようにvolatile修飾すれば、期待した動作になると思いますのでお試しください。

volatile int tb_reach=0;
1 Like

ありがとうございます!。意図したとおりに動作できました。

Tickerに与える時間を2.0とかにするとたまに動く時があったり、極端に小さくする(0.001)と動いたりしていて、ずっとわからないままでした。助かりました。