package cn.hyc.led; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button led1, led2, led3, led4; private boolean led1OnOFF = false; private boolean led2OnOFF = false; private boolean led3OnOFF = false; private boolean led4OnOFF = false; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); led1 = (Button)this.findViewById(R.id.led1); led2 = (Button)this.findViewById(R.id.led2); led3 = (Button)this.findViewById(R.id.led3); led4 = (Button)this.findViewById(R.id.led4); led1.setOnClickListener(new OnClickLed()); led2.setOnClickListener(new OnClickLed()); led3.setOnClickListener(new OnClickLed()); led4.setOnClickListener(new OnClickLed()); } public class OnClickLed implements View.OnClickListener{ public void onClick(View v) { switch (v.getId()) { case R.id.led1: if(led1OnOFF){ cmdLeds(0, 0); led1OnOFF = false; } else{ cmdLeds(1, 0); led1OnOFF = true; } break; case R.id.led2: if(led2OnOFF){ cmdLeds(0, 1); led2OnOFF = false; } else{ cmdLeds(1, 1); led2OnOFF = true; } break; case R.id.led3: if(led3OnOFF){ cmdLeds(0, 2); led3OnOFF = false; } else{ cmdLeds(1, 2); led3OnOFF = true; } break; case R.id.led4: if(led4OnOFF){ cmdLeds(0, 3); led4OnOFF = false; } else{ cmdLeds(1, 3); led4OnOFF = true; } break; default: break; } } }
public native void cmdLeds(int cmd, int arg); static { System.loadLibrary("leds"); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
#include <jni.h> #include <fcntl.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> void Java_cn_hyc_led_MainActivity_cmdLeds( JNIEnv* env, jobject thiz, jint cmd, jint arg) { int dev; dev = open("/dev/leds", O_WRONLY); ioctl(dev, cmd, arg); close(dev); }
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := leds LOCAL_SRC_FILES := leds.c include $(BUILD_SHARED_LIBRARY)