Created
February 24, 2021 12:20
-
-
Save doojinkang/99ddc208197de2a0797b2fd1079ea979 to your computer and use it in GitHub Desktop.
DcMotor Android Mainactivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dcmotor.lge.com.dcmotor; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.view.View.OnClickListener; | |
public class MainActivity extends BlunoLibrary { | |
private Button buttonScan; | |
private Button buttonSerialSend; | |
private EditText serialSendText; | |
private TextView serialReceivedText; | |
private TextView leftSpeed; | |
private TextView rightSpeed; | |
private final static String LEFT_UP = "L"; | |
private final static String RIGHT_UP = "R"; | |
private final static String LEFT_DOWN = "l"; | |
private final static String RIGHT_DOWN = "r"; | |
private int speedLeft = 0; | |
private int speedRight = 0; | |
private Button buttonLeftUp; | |
private Button buttonLeftDown; | |
private Button buttonRightUp; | |
private Button buttonRightDown; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
onCreateProcess(); | |
serialBegin(115200); | |
serialReceivedText=(TextView) findViewById(R.id.serialReveicedText); | |
serialSendText=(EditText) findViewById(R.id.serialSendText); | |
buttonSerialSend = (Button) findViewById(R.id.buttonSerialSend); | |
buttonSerialSend.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
int data = Integer.valueOf(serialSendText.getText().toString()); | |
serialSend( String.valueOf(data << 8 | data) ); | |
} | |
}); | |
buttonScan = (Button) findViewById(R.id.buttonScan); | |
buttonScan.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
buttonScanOnClickProcess(); | |
} | |
}); | |
leftSpeed = (TextView) findViewById(R.id.text_speed_left); | |
rightSpeed = (TextView) findViewById(R.id.text_speed_right); | |
buttonLeftUp = (Button) findViewById(R.id.btn_up_left); | |
buttonLeftUp.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendSpeedInfo(LEFT_UP); | |
} | |
}); | |
buttonLeftDown = (Button) findViewById(R.id.btn_down_left); | |
buttonLeftDown.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendSpeedInfo(LEFT_DOWN); | |
} | |
}); | |
buttonRightUp = (Button) findViewById(R.id.btn_up_right); | |
buttonRightUp.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendSpeedInfo(RIGHT_UP); | |
} | |
}); | |
buttonRightDown = (Button) findViewById(R.id.btn_down_right); | |
buttonRightDown.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
sendSpeedInfo(RIGHT_DOWN); | |
} | |
}); | |
leftSpeed.setText(String.valueOf(speedLeft)); | |
rightSpeed.setText(String.valueOf(speedRight)); | |
} | |
private void sendSpeedInfo(String str) { | |
serialSend(str); | |
} | |
private void sendSpeed() { | |
serialSend(String.valueOf(speedLeft << 8 | speedRight)); | |
leftSpeed.setText(String.valueOf(speedLeft)); | |
rightSpeed.setText(String.valueOf(speedRight)); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
System.out.println("BlUNOActivity onResume"); | |
onResumeProcess(); //onResume Process by BlunoLibrary | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
onActivityResultProcess(requestCode, resultCode, data); //onActivityResult Process by BlunoLibrary | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
onPauseProcess(); //onPause Process by BlunoLibrary | |
} | |
protected void onStop() { | |
super.onStop(); | |
onStopProcess(); //onStop Process by BlunoLibrary | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
onDestroyProcess(); //onDestroy Process by BlunoLibrary | |
} | |
@Override | |
public void onConectionStateChange(connectionStateEnum theConnectionState) {//Once connection state changes, this function will be called | |
switch (theConnectionState) { //Four connection state | |
case isConnected: | |
buttonScan.setText("Connected"); | |
break; | |
case isConnecting: | |
buttonScan.setText("Connecting"); | |
break; | |
case isToScan: | |
buttonScan.setText("Scan"); | |
break; | |
case isScanning: | |
buttonScan.setText("Scanning"); | |
break; | |
case isDisconnecting: | |
buttonScan.setText("isDisconnecting"); | |
break; | |
default: | |
break; | |
} | |
} | |
@Override | |
public void onSerialReceived(String theString) { //Once connection data received, this function will be called | |
// serialReceivedText.append(theString); //append the text into the EditText | |
String[] splited = theString.split(","); | |
if ( splited.length == 2 ) { | |
String sl = splited[0].trim(); | |
String sr = splited[1].trim(); | |
leftSpeed.setText(sl); | |
rightSpeed.setText(sr); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment