写了个简单的小应用FireflyLeds.apk

本帖最后由 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)

必须顶{:3_48:}

学习学习!MARK!

可以将源码提供下吗?

schlin2999 发表于 2015-4-7 19:40
可以将源码提供下吗?

上面就是,你需要整改工程包吗

carlinluo 发表于 2015-4-8 18:14
上面就是,你需要整改工程包吗

如果能提供整个源码包更好学习点,可以吗?

http://share.weiyun.com/0a584154e5bae64a817af34a7eacc0cc可以从这下载

本帖最后由 pmos1981 于 2016-4-26 14:39 编辑

請問每次重開機就要 1.加权限
adb shell chmod 666 sys/class/leds/firefly:blue:power/brightness
adb shell chmod 666 sys/class/leds/firefly:yellow:user/brightness

有沒有直接寫死的方法?
不用再次加权限
謝謝
PS: 只要給 0 or 1 就好了

  • byte led_off = “0”.getBytes();
  • byte led_on = “1”.getBytes();

一直有疑问,开发安卓应用,使用android studio的话,sdk是使用从google下载的么?还是要指定fireprime的sdk?请不吝敕教!非常感谢!

pmos1981 发表于 2016-4-26 13:52
請問每次重開機就要 1.加权限
adb shell chmod 666 sys/class/leds/firefly:blue:power/brightness
adb s …

写死是在驱动里改,大概是叫driver下led-class.c里面

kallos 发表于 2016-5-6 17:16
一直有疑问,开发安卓应用,使用android studio的话,sdk是使用从google下载的么?还是要指定fireprime的sd …

google上下.android studio和sdk是2个东西。sdk需要到官网上下。或者问别人复制一个,毕竟翻墙麻烦,sdk也蛮大的。android studio是IDE,你也可以用eclipse+ADT或者IntelliJ IDEA

另,感谢lz分享,正在研究怎么在app里面操作GPIO,可以学习下你的源码

很好,谢谢!

顶一个high

顶顶顶

pmos1981 发表于 2016-4-26 13:52
請問每次重開機就要 1.加权限
adb shell chmod 666 sys/class/leds/firefly:blue:power/brightness
adb s …

可以在init.rc里面处理