LoginSignup
2
1

More than 5 years have passed since last update.

C++ Builder XE4 > TTimer > バースト機能(仮称)の実装 > 1秒インターバルに対して0.1秒インターバルで処理する

Last updated at Posted at 2018-03-08
動作環境
C++ Builder XE4

処理概要

  • 1秒インターバルの処理がある
  • ある機能だけ0.1秒インターバルにしたい

実装案

  • 1秒インターバルの中で0.1秒インターバルのTTimerを動かす
    • TMR_1sec: 1秒インターバル
    • TMR_burst: 0.1秒インターバル

code v0.1

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TTimer *TMR_1sec;
    TMemo *Memo1;
    TButton *B_start;
    TTimer *TMR_burst;
    TCheckBox *CHK_burst;
    void __fastcall TMR_1secTimer(TObject *Sender);
    void __fastcall B_startClick(TObject *Sender);
    void __fastcall TMR_burstTimer(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


/*
@spec: 2018/03/08 TMR_burst->Tagをカウントダウンとして使う
*/


void __fastcall TForm1::TMR_1secTimer(TObject *Sender)
{
    String wrk = Now().FormatString(L"hh:nn:ss.zzz");
    Memo1->Lines->Add(wrk);

    if (CHK_burst->Checked) {
        TMR_1sec->Enabled = false;  // 連射時は1秒タイマーは1回だけにする
        TMR_burst->Interval = 100;  // msec
        TMR_burst->Tag = 10 - 1;  // カウントダウン, 1: 実施済みの分
        TMR_burst->Enabled = true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_startClick(TObject *Sender)
{
    TMR_1sec->Interval = 1000; // msec
    TMR_1sec->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TMR_burstTimer(TObject *Sender)
{
    String wrk = Now().FormatString(L"hh:nn:ss.zzz");
    Memo1->Lines->Add(wrk);

    TMR_burst->Tag -= 1;  // Tag:カウントダウン用
    if (TMR_burst->Tag == 0) {
        TMR_burst->Enabled = false;
    }
}
//---------------------------------------------------------------------------

Memo1->Lines->Add(wrk);処理は関数化して、2箇所からinvokeする方がなおよいが、この記事ではここまで。

UDP通信やTCP/IP通信の場合、相手からの応答時間もあるので、100msecで処理が終わらない場合もある。試験中の装置ではUDP通信は100msecで可能だった。

実行例

qiita.png

バーストモード中の7of9 Powered by Qiita and Qiita users
(やる気のない時の3倍くらい)

link

バーストモードは消耗が激しいので、長時間は無理。

検索用キーワード

  • burst mode
  • 赤いやつ
  • 加速装置
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1