戻る

コントローラー入力

コントローラーはバージョンが2種類あるようで
version 1はアナログ2軸の方向キーとデジタルボタンx2 (CTS Electrocomponents 254SA104B50B)
version 2はデジタル4ボタンの方向キーとデジタルボタンx2 (アルプス電気 SKRHABE010)
で構成されている。プログラム側で判別してそれぞれのプログラムを書く必要がある。
旧ロットはversion 1、最近流通している新ロットはversion 2の様です。
下記urlのTinyArcadeの起動メニューのソースファイルに書いてある方法を採用しました。
https://tinycircuits.com/blogs/learn/171319495-tiny-arcade-menu-update

サンプルプログラム

#include <stdlib.h>
#include <TinyScreen.h>

// Pin
static const int DIR_X_PIN = 42;
static const int DIR_Y_PIN = 1;
static const int DIR_UP_PIN = 42;
static const int DIR_DOWN_PIN = 19;
static const int DIR_LEFT_PIN = 25;
static const int DIR_RIGHT_PIN = 15;
static const int BUTTON_LEFT_PIN = 45;
static const int BUTTON_RIGHT_PIN = 44;
static const int DIR_THRESHOLD = 250;

TinyScreen tiny_screen = TinyScreen(TinyScreenPlus);
int version = 1;

void setup()
{
	tiny_screen.begin();
	tiny_screen.setBitDepth(TSBitDepth8);
	tiny_screen.setBrightness(8);
	tiny_screen.setFont(liberationSansNarrow_12ptFontInfo);
	tiny_screen.fontColor(TS_8b_White, TS_8b_Black);
	// コントローラー初期化
	pinMode(4, INPUT_PULLUP);
	if(!digitalRead(4))
	{
		version = 2;
		pinMode(DIR_UP_PIN, INPUT_PULLUP);
		pinMode(DIR_DOWN_PIN, INPUT_PULLUP);
		pinMode(DIR_LEFT_PIN, INPUT_PULLUP);
		pinMode(DIR_RIGHT_PIN, INPUT_PULLUP);
	}
	pinMode(BUTTON_LEFT_PIN, INPUT_PULLUP);
	pinMode(BUTTON_RIGHT_PIN, INPUT_PULLUP);
}

void loop()
{
	char version_text[128];
	char text[128];
	sprintf(version_text, "Stick version %d", version);
	strcpy(text, "PUSH ");
	if(version == 1)
	{
		int dir_x = analogRead(DIR_X_PIN) - 512;
		if(dir_x < -DIR_THRESHOLD)
		{
			strcat(text, "R ");
		}
		else if(dir_x > DIR_THRESHOLD)
		{
			strcat(text, "L ");
		}
		int dir_y = analogRead(DIR_Y_PIN) - 512;
		if(dir_y < -DIR_THRESHOLD)
		{
			strcat(text, "U ");
		}
		else if(dir_y > DIR_THRESHOLD)
		{
			strcat(text, "D ");
		}
	}
	else
	{
		if(!digitalRead(DIR_UP_PIN))
		{
			strcat(text, "U ");
		}
		if(!digitalRead(DIR_DOWN_PIN))
		{
			strcat(text, "D ");
		}
		if(!digitalRead(DIR_LEFT_PIN))
		{
			strcat(text, "L ");
		}
		if(!digitalRead(DIR_RIGHT_PIN))
		{
			strcat(text, "R ");
		}
	}
	if(!digitalRead(BUTTON_LEFT_PIN))
	{
		strcat(text, "1 ");
	}
	if(!digitalRead(BUTTON_RIGHT_PIN))
	{
		strcat(text, "2 ");
	}
	tiny_screen.clearScreen();
	tiny_screen.setCursor(0, 12);
	tiny_screen.print("ControllerSample");
	// version
	tiny_screen.setCursor(0, 24);
	tiny_screen.print(version_text);
	// push
	tiny_screen.setCursor(0, 36);
	tiny_screen.print(text);
	delay(30);
}

実行結果

ControllerSample.jpg

押したボタンの文字が画面に表示される。この写真では下、ボタン1、ボタン2を同時押ししている。

サンプルプログラムのダウンロード

バイナリはこちら
プログラムはこちら

添付ファイル: fileControllerSample.zip 1021件 [詳細] fileControllerSample_source.zip 1038件 [詳細] fileControllerSample.jpg 1077件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-06-17 (日) 02:34:05 (2139d)