人体感应代码逻辑
程序员文章站
2022-08-28 11:45:20
需求:人靠近马上灯亮,人离开等待5s后灯灭,使用Theard和Hander private boolean isTurnONLed = false; //标志位,是否打开开关 @Override public void onCreate() { super.onCreate();startDetectionTP(); } @Nullable @Override public IBinder onBind(Intent in....
需求:
人靠近马上灯亮,人离开等待5s后灯灭,使用Theard和Hander
private boolean isTurnONLed = false; //标志位,是否打开开关
@Override
public void onCreate() {
super.onCreate();
startDetectionTP();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void startDetectionTP(){
Log.i(TAG, "startDetectionTP.....................");
Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: //灯灭
Log.d(TAG,"------------Extinguish-------------------------------1");
isTurnONLed = false;
setNodeString("/sys/class/gpio/gpio1248/value","0");
setNodeString("/sys/class/gpio/gpio1249/value","0");
setNodeString("/sys/class/gpio/gpio1250/value","0");
setNodeString("/sys/class/gpio/gpio1251/value","0");
setNodeString("/sys/class/gpio/gpio1255/value","0");
setNodeString("/sys/class/gpio/gpio1229/value","0");
break;
case 2: //灯亮
isTurnONLed = true;
Log.d(TAG,"-----------------light--------------------------2");
setNodeString("/sys/class/gpio/gpio1248/value","1");
setNodeString("/sys/class/gpio/gpio1249/value","1");
setNodeString("/sys/class/gpio/gpio1250/value","1");
setNodeString("/sys/class/gpio/gpio1251/value","1");
setNodeString("/sys/class/gpio/gpio1255/value","1");
setNodeString("/sys/class/gpio/gpio1229/value","1");
break;
} //end switch
} //handleMessage
}; //end handler
new Thread(){
UsbManager mUsbManager = (UsbManager) USBDetectionService.this.getSystemService(Context.USB_SERVICE);
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void run() {
super.run();
// Log.i(TAG, "getUSBName().....................");
while(true) {
String value=readFile("/sys/bus/iio/devices/iio:device0/in_voltage0_raw");
Log.d(TAG,"value--------------------------------------------------"+value);
int valueInt=Integer.parseInt(value);
if(valueInt>500){
Log.i(TAG_WP, ".....light......");
if(!isTurnONLed){
Log.i(TAG_WP, "light.....11......");
mHandler.removeMessages(1); //移除handle状态值
mHandler.sendEmptyMessage(2);
}
}//end if
else{
Log.i(TAG_WP, "Extinguish..............");
if(isTurnONLed){
isTurnONLed = false; //此时设置开关状态,防止小于5s时候再次靠近操作
Log.i(TAG_WP, "Extinguish.......11.......");
mHandler.sendEmptyMessageDelayed(1,5000); //延迟5s发送
}
}//end else
try {
sleep(1000); //1s check 1 time
} catch (InterruptedException e) {
e.printStackTrace();
}
///////////////////////////////////////
} //end while
}
}.start(); //end Theard
}
本文地址:https://blog.csdn.net/yhm2046/article/details/107215086
上一篇: 使用python3实现操作串口详解
下一篇: python 实现UTC时间加减的方法