本帖最后由 carlinluo 于 2015-3-27 15:06 编辑
可以运行在Firefly开发板上,但是默认固件的未修改LED驱动需要加权限,不然打不开LED,如下(假如你已经安装adb 工具)
1.加权限
adb shell chmod 666 sys/class/leds/firefly:blue:power/brightness
adb shell chmod 666 sys/class/leds/firefly:yellow:user/brightness
2.安装apk,可以adb 安装(注意路径),可以手动安装
adb install d:\FireflyLeds.apk
3.就3个按钮,控制firefly板子上的黄灯和蓝灯,还有个自动控制的按钮,打开之后板子上的黄色和蓝色灯轮流闪动。
4.主要代码,很简单了,俺只是个菜鸟,高手请忽略。。。
4-1 LedsActivity
package com.example.luobo.fireflyleds;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class LedsActivity extends ActionBarActivity {
private final static String TAG="LedsActivity";
public ToggleButton bluebt;
public ToggleButton yellowbt;
public ToggleButton autobt;
public LedsCtrl ledsctrl;
Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ledsctrl=new LedsCtrl();
setContentView(R.layout.activity_leds);
bluebt=(ToggleButton)findViewById(R.id.toggleButton);
bluebt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
bluebt.setChecked(isChecked);
ledsctrl.LedsOnOff(ledsctrl.BlueLed,isChecked);
}
});
yellowbt=(ToggleButton)findViewById(R.id.toggleButton2);
yellowbt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
yellowbt.setChecked(isChecked);
ledsctrl.LedsOnOff(ledsctrl.YllowLed,isChecked);
}
});
autobt=(ToggleButton)findViewById(R.id.toggleButton3);
autobt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
autobt.setChecked(isChecked);
if(isChecked)
handler.post(runnable);
else
handler.removeCallbacks(runnable);
}
});
//intent = new Intent("com.example.luobo.fireflyleds.LedsService");
//startService(intent);
}
Handler handler = new Handler(){
boolean isOn;
@Override
public void handleMessage (Message msg){
isOn=msg.getData().getBoolean("onoff");
//Log.d(TAG,"handleMessage----->"+isOn);
yellowbt.setChecked(isOn);
ledsctrl.LedsOnOff(ledsctrl.YllowLed,isOn);
bluebt.setChecked(!isOn);
ledsctrl.LedsOnOff(ledsctrl.BlueLed,!isOn);
}
};
boolean onoff;
Runnable runnable = new Runnable() {
Bundle data;
@Override
public void run() {
Message msg=handler.obtainMessage();
data =new Bundle();
//Log.d(TAG,"----->Runnable...");
onoff=!onoff;
data.putBoolean("onoff",onoff);
msg.setData(data);
handler.sendMessage(msg);
handler.postDelayed(runnable, 300);
}
};
@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_leds, 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);
}
@Override
public void onDestroy(){
super.onDestroy();
if(intent != null){
stopService(intent);
}
}
}
4-2 LedsCtrl
package com.example.luobo.fireflyleds;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by luobo on 15-3-26.
*/
public class LedsCtrl {
public final static String TAG="LedsCtrl";
public final static String BLEDPATH="sys/class/leds/firefly:blue:power/brightness";
public final static String YLEDPATH="sys/class/leds/firefly:yellow:user/brightness";
public final int BlueLed=0;
public final int YllowLed=1;
protected LedsCtrl(){
Log.d(TAG,"----->");
}
public void LedsOnOff(int LedID ,boolean on){
//Log.d(TAG,"----->LedsOnOff="+on);
OutputStream out = null;
String ledpath=null;
try {
if (LedID==BlueLed)
ledpath=BLEDPATH;
else
ledpath=YLEDPATH;
out = new BufferedOutputStream(new FileOutputStream(ledpath));
byte[] led_off = "000".getBytes();
byte[] led_on = "255".getBytes();
if(on)
out.write(led_on);
else
out.write(led_off);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FireflyLeds.zip (755 KB)
