package com.example.serial;
|
import static java.lang.Thread.sleep;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import android.graphics.PorterDuff;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.widget.Button;
|
import android.widget.ImageView;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
public class LedActivity extends AppCompatActivity {
|
private boolean redTag = false;
|
private boolean greenTag = false;
|
private boolean yellowTag = false;
|
private Button ledRed = null;
|
private Button ledGreen = null;
|
private Button ledYellow = null;
|
private ImageView imageRed = null;
|
private ImageView imageGreen = null;
|
private ImageView imageYellow = null;
|
private LedControl ledControl;
|
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_led);
|
|
ledRed = (Button)findViewById(R.id.red);
|
ledYellow = (Button)findViewById(R.id.yellow);
|
ledGreen = (Button)findViewById(R.id.green);
|
|
imageRed = findViewById(R.id.imageRed);
|
imageYellow = findViewById(R.id.imageYellow);
|
imageGreen = findViewById(R.id.imageGreen);
|
|
ledControl = new LedControl();
|
ledControl.ledCtrl(1, 0);
|
try {
|
sleep(1);
|
ledControl.ledCtrl(1, 1);
|
} catch (InterruptedException e) {
|
throw new RuntimeException(e);
|
}
|
|
|
ledRed.setOnClickListener(new View.OnClickListener(){
|
@Override
|
public void onClick(View v) {
|
redTag = !redTag;
|
if( redTag ) {
|
if(ledControl.ledCtrl(0, 0) < 0)
|
Toast.makeText(LedActivity.this, "点亮红灯失败", Toast.LENGTH_SHORT).show();
|
else
|
imageRed.setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_IN);
|
}else{
|
if(ledControl.ledCtrl(0, 1) < 0)
|
Toast.makeText(LedActivity.this, "关闭红灯失败", Toast.LENGTH_SHORT).show();
|
else
|
imageRed.setColorFilter(getResources().getColor(android.R.color.darker_gray), PorterDuff.Mode.SRC_IN);
|
}
|
}
|
});
|
ledYellow.setOnClickListener(new View.OnClickListener(){
|
@Override
|
public void onClick(View v) {
|
yellowTag = !yellowTag;
|
if( yellowTag ) {
|
if( ledControl.ledCtrl(1, 0) < 0){
|
Toast.makeText(LedActivity.this, "点亮黄灯失败!", Toast.LENGTH_SHORT).show();
|
}else {
|
imageYellow.setColorFilter(getResources().getColor(android.R.color.holo_orange_light), PorterDuff.Mode.SRC_IN);
|
}
|
}else{
|
if( ledControl.ledCtrl(1, 1) < 0 ){
|
Toast.makeText(LedActivity.this, "关闭黄灯失败!", Toast.LENGTH_SHORT).show();
|
}else
|
imageYellow.setColorFilter(getResources().getColor(android.R.color.darker_gray), PorterDuff.Mode.SRC_IN);
|
}
|
}
|
});
|
|
ledGreen.setOnClickListener(new View.OnClickListener(){
|
@Override
|
public void onClick(View v) {
|
greenTag = !greenTag;
|
if( greenTag ) {
|
if(ledControl.ledCtrl(2, 0) < 0)
|
Toast.makeText(LedActivity.this, "打开绿灯失败", Toast.LENGTH_SHORT).show();
|
else
|
imageGreen.setColorFilter(getResources().getColor(android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
|
}else{
|
if(ledControl.ledCtrl(2, 1) < 0)
|
Toast.makeText(LedActivity.this, "关闭绿灯失败", Toast.LENGTH_SHORT).show();
|
else
|
imageGreen.setColorFilter(getResources().getColor(android.R.color.darker_gray), PorterDuff.Mode.SRC_IN);
|
}
|
}
|
});
|
|
ledControl.ledCtrl(0, 1);
|
ledControl.ledCtrl(1, 1);
|
ledControl.ledCtrl(2, 1);
|
}
|
}
|