グラフィックLCDを使ってみよう

04 プッシュボタンのクラスを作る



getESC()はGlcdクラスにありましたが、プッシュボタンを管理するpushButtonを作ります。
BreakOut.h
#ifndef BREAKOUT_H
#define BREAKOUT_H

#include "Glcd.h"
#include "pushButton.h"

class BreakOut : public Glcd, public pushButton
{
  public:
    BreakOut();
 //   bool getESC();
    void drawBar();
  protected:
  private:
    DigitalIn *esc;
    int bar_x;
    int bar_y;
    unsigned char bar[18];
    int n;
    static unsigned char block[];
    static unsigned char wall[];
};

#endif // BREAKOUT_H
 1 
 2 
 3 
 4 
 5 
 6 
 7 pushButtonクラスから派生させる
 8 
 9 
10 
11 機能をpushButtonに移動させる
12 ボールを跳ね返すバーを描く
13 
14 
15 
16 バーのx座標
17 バーのy座標
18 バーのグラフィックデータ
19 バーのデータ数
20 ブロックのグラフィックデータ
21 壁のグラフィックデータ
22 
23 
24 


BreakOut.cpp
#include "BreakOut.h"
unsigned char BreakOut::block[]=
{0x00, 0x7e, 0x56, 0x6a, 0x56, 0x6a, 0x56, 0x6a,
 0x56, 0x6a, 0x56, 0x6a, 0x56, 0x6a, 0x7e,0x00};
unsigned char BreakOut::wall[]=
{
  0xff, 0xff, 0xff, 0xff
};

BreakOut::BreakOut()
{
  //ctor
//  esc  = new DigitalIn(p8);
  bar_x = 40;
  bar_y = 7;
  n=18;
  for(int i=0; i<n; i++) bar[i]=0x0f;
  for(int i=0; i<8; i++) {
    draw(96, i, wall, 4);
  }
  for(int y=0; y<3; y++)
    for(int x=0; x<96; x+=16)
      draw(x, y, block, 16);
}
/*
bool BreakOut::getESC()
{
  return esc->read();
}
*/

  // バー描画
void BreakOut::drawBar()
{    // カーソルの色を変更
  draw(bar_x, bar_y, bar, n );
}
 1 
 2 ブロックのグラフィックデータ
 3 
 4 
 5 壁のグラフィックデータ
 6 
 7 
 8 
 9 
10 コンストラクタ
11 
12 
13 pushButtonクラスに移動させる
14 バーの初期位置
15 
16 
17 バーのグラフィックデータ作成
18 
19 壁を描く
20 
21 
22 
23 ブロックを描く
24 
25 pushButtonクラスに移動させる
26 
27 
28 
29 
30 
31 
32 
33 バーを描く
34 
35 
36 

pushButtonを作る


終了/開始、左、右の3つのボタンを使います。

pushButton.h
#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H
#include "mbed.h"

class pushButton
{
  public:
    pushButton();
    int getKey();
    protected:
  private:
    DigitalIn *esc, *left, *right;
};

#endif // PUSHBUTTON_H
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 コンストラクタ
 9 3つのボタンを読み込む
10 
11 
12 ボタンオブジェクトへのポインタ3つ
13 
14 
15 


pushButton.cpp
#include "pushButton.h"

pushButton::pushButton()
{
  //ctor
  esc = new DigitalIn(p8);      // esc
  left = new DigitalIn(p11);    // left
  right = new DigitalIn(p12);   // right
}

int pushButton::getKey()
{
  int val=0;
  if(!esc->read()) val=1;
  if(!left->read()) val|=2;
  if(!right->read()) val|=4;
  return val;
}
 1 
 2 
 3 
 4 
 5 
 6 ESCキー
 7 左
 8 右
 9 
10 
11 キーの状態を読み報告する
12 
13 
14 ESCはbit0
15 左はbit1
16 右はbit1
17 状態を返す
18 


メインプログラム



main.cpp
#include "mbed.h"
#include "BreakOut.h"
// pushButton classの導入

int main() {
  BreakOut LCD;
  while(true){
    switch(int k= LCD.getKey())
    {
      case 1: LCD.print(100, 0, "ESC  ");
              wait_ms(200);
              LCD.clear(100,0,3*5);
              break;
      case 2: LCD.print(100, 0, "left ");
              wait_ms(200);
              LCD.clear(100,0,4*5);
              break;
      case 4: LCD.print(100, 0, "right");
              wait_ms(200);
              LCD.clear(100,0,5*5);
              break;
      default:
              k +=0x30;
              LCD.drawChar(100, 1, k); break;
    }
    wait_ms(10);
  }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 キーコードを読み込む
 9 各ボタンが押されるとそのボタン名を表示する
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27