コンビネーションスイッチのスケッチ

published_with_changes 更新日: event_note 公開日:

labelETS2 labelGaming Gear

ハンドル左右後方に設置したコンビネーションスイッチなどのスケッチをまとめておきます。右のコンビネーションスイッチの下にはジョイスチックもつけています。ATシフトとワイパー用として使います。

今は使いませんが、バンドブレーキ用にもpinを割り振っておきます。ハンドブレーキはデジタルインプットだけでなく、効き具合を調整できるようにアナログインプットでも使えるようにしておきます。



ETS2上でのゲーム機能とピンアサインは下表のようにしています。USB HIDはSparkFun Pro Micro(たぶん互換機)です。

Game 機能pro micro
OthersAT ShiftLXJoystickHat 90D8
AT ShiftLXJoystickHat 270
ワイパーLYJoystickHat 0D9
ワイパー戻しLYJoystickHat 180
Sideon/offRXD10
Brake***RX
analogRYA10
(+/- full)RY
CruiseホーンSW1MainD2
controlクルコン ONSW2CANCELD3
クルコン 再開SW3
Opposite-CANCEL
D4
クルコン 増速SW4RES/ACCD5
クルコン 減速SW5
SET/COAST
D6
pushD7
BlinkerライトモードSW1MainD15
& Light右ウインカーSW2CANCELD18
左ウインカーSW3
Opposite-CANCEL
D19
ハイビームSW4RES/ACCD20
パッシングSW5
SET/COAST
D21


//--------------------------------------------------------------------
const	int    pin_id[]={8, 9};		// analog pin#
const	int    num_sw[]={3, 3};		// 各pinに割り当てるスイッチ数
const	int    PshSw0=0;		// no switch pushed
const	int    PshSwE=-1;		// error
const	int    num_pin = sizeof(pin_id) / sizeof(int); //配列pin_id[]の要素数
	int    psh[num_pin];		//pushed switch # [?]:配列の要素数は使うアナログピンの数
static	int    last_psh[]={0, 0};	// initial switch state
const	int    threshold[][4]={ //閾値(bit換算) 1行目が analog pin #0 から,6行目が pin #5
		{0,  269, 571,  1023}, //Joystickは ニュートラルを中心に3ポジション
		{0,  288, 560,  1023}, //4.7Kokmでの実数による数値合わせを実施
		};
	int    keyWait=20; //keyboardやbuttonを押した後の保持時間(待機時間) 
const   int    Interval = 10 ; //msec, for prevrnt chattering

// Constant that maps the phyical pin to the joystick button.
// 利用する最小のpin number
const   int    pinToButtonMap = 0;

        // Last state of the button
    int             lastButtonState[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    unsigned long prev[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

boolean beforeState = false;

#include 

Joystick_ Joystick;

void setup() {
	// Serial
	Serial.begin (9600);

  // Initialize Button Pins
  //pinMode(0, INPUT_PULLUP);
  //pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT); // joystick X-axis
  pinMode(9, INPUT); // joystick y-axis
  //pinMode(A10, INPUT); // analog Brake
  pinMode(10, INPUT_PULLUP); // on/off Brake
  pinMode(15, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

void loop() {


  for (int index = 2; index < 22; index++) //LEFT and Right switches and joystick push
	if (index == 8 || index == 9 ) {continue;}
	else if (index > 10 && index < 15) {continue;}
	else if (index == 16 || index == 17) {continue;}
	else
  {
    //Serial.println((String)"index: "+index);//delay(1000);
	int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      unsigned long cur = millis();

      if (cur - prev[index] > Interval)
      {    
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
      
      prev[index] = cur;
      }
    }
  }
	push_swHhat (0); // analog 配列[#0] pin を指定して、push_swHhatを呼び出す
	push_swVhat (1); // analog 配列[#1] pin を指定して、push_swVhatを呼び出す

  //delay(500);
}


int push_swHhat (int ip) { // Hat switch の水平軸に割り当て
	// switch pushed?
	psh[ip]=correct_sw (ip);

	if (psh[ip]!=last_psh[ip])	{
		if (psh[ip]>PshSw0) {
//			serPrint (ip);
			switch (psh[ip]){
				case 1:
        			Joystick.setHatSwitch(0, 270);
        			break;
        		case 2:
        			Joystick.setHatSwitch(0, -1);
        			break;        		
				case 3:
        			Joystick.setHatSwitch(0, 90);
        			break;
			}
		}
		else{ 
			Joystick.setHatSwitch(0, -1);
		}
  	last_psh[ip]=psh[ip];
	}
}

int push_swVhat (int ip) { // Hat switch の垂直軸に割り当て
	// switch pushed?
	psh[ip]=correct_sw (ip);

	if (psh[ip]!=last_psh[ip]) {
		if (psh[ip]>PshSw0) {
//			serPrint (ip);
			switch (psh[ip]){
				case 1:
        			Joystick.setHatSwitch(0, 180);
        			break;
        		case 2:
        			Joystick.setHatSwitch(0, -1);
        			break;        		
				case 3:
        			Joystick.setHatSwitch(0, 0);
        			break;
			}
		}
		else{
			Joystick.setHatSwitch(0, -1);
		}
  	last_psh[ip]=psh[ip];
	}
}


int		detect_sw (int ide) { //閾値と比較してスイッチの番号を見つける

	// detect switch state
	int		idx;
	int		val;

	val=analogRead (pin_id[ide]);

	for (idx=1; idx<=num_sw[ide]; idx++)
		if (val<(threshold[ide][idx - 1] + threshold[ide][idx])/2)
			break;
	/*	
	if (!beforeState) {
	//確認用のサブルーチン
	Serial.print ((String)"val:"+val);
	Serial.print ((String)" thr:"+(threshold[ide][idx - 1] + threshold[ide][idx])/2);
	Serial.print ((String)", idx:"+idx);
	Serial.println ((String)", dect戻り値:"+(idx%(num_sw[ide] + 1)));
	}
	beforeState = !beforeState;
	*/
	return (idx%(num_sw[ide] + 1)); //見つかったスイッチの番号が戻り値
}

int		correct_sw (int ic) { //2回同じスイッチ番号が続くと、正しいスイッチ番号とする。4回不一致が続くとエラー
	// detect switch state, until same result 2 times
	int		psh1;
	int		psh2;
	unsigned long     prev = 0;
	unsigned long     cur = millis();
	psh1=detect_sw (ic);
	if (cur - prev > Interval ) {
		psh2=detect_sw (ic);
		if (psh1!=psh2) {
			psh1=detect_sw (ic);
			if (psh1!=psh2) {
				psh2=detect_sw (ic);
				if (psh1!=psh2) {
					// ERROR:
					psh1=PshSwE;
				}
			}
		}
	prev = cur;
	return (psh1);
	}
}


Powered by Blogger | Designed by QooQ

keyboard_double_arrow_down

keyboard_double_arrow_down