Skip to content

Commit

Permalink
gpiod: first initialized without readme file
Browse files Browse the repository at this point in the history
Tested on android-studio bumblebee including kotlin base activity, and
c++ base JNI layer api using libgpiod static libraries.

Support platform:
  - arm64-v8
  - armeabi-v7a

Signed-off-by: Wig Cheng <[email protected]>
  • Loading branch information
wigcheng committed May 4, 2022
0 parents commit 184451a
Show file tree
Hide file tree
Showing 48 changed files with 4,092 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
79 changes: 79 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.example.gpiod"
minSdk 28
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}

externalNativeBuild {
cmake {
cppFlags ''
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your app.
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.gpiod

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.gpiod", appContext.packageName)
}
}
25 changes: 25 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpiod">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Gpiod">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Gpiod">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
54 changes: 54 additions & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("JNIGpiod")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
JNIGpiod

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
JNIGpiod.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

include_directories(${ANDROID_ABI}/)
add_library(gpiod STATIC IMPORTED)
set_target_properties(gpiod PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/${ANDROID_ABI}/libgpiod.a)


target_link_libraries( # Specifies the target library.
JNIGpiod
gpiod
# Links the target library to the log library
# included in the NDK.
${log-lib} )
88 changes: 88 additions & 0 deletions app/src/main/cpp/JNIGpiod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Write C++ code here.
//
// Do not forget to dynamically load the C++ library into your application.
//
// For instance,
//
// In MainActivity.java:
// static {
// System.loadLibrary("gpiod");
// }
//
// Or, in MainActivity.kt:
// companion object {
// init {
// System.loadLibrary("gpiod")
// }
// }

#define LOG_TAG "gpiod-lib_JNI"
#include <cstring>
#include <jni.h>
#include <cinttypes>
#include <string>
#include <gpiod.h>
#include <android/log.h>

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_gpiod_MainActivity_getGpioTotalBank(JNIEnv *env, jobject thiz) {
std::ignore = thiz;
struct gpiod_chip_iter *iter;
struct gpiod_chip *chip;
unsigned int total_bank = 0;

iter = gpiod_chip_iter_new();
gpiod_foreach_chip(iter, chip) {
total_bank += 1;
}

gpiod_chip_iter_free(iter);

return total_bank;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_gpiod_MainActivity_getGpioInfo(JNIEnv *env, jobject thiz, jint gpiobank, jint gpioline) {
std::ignore = thiz;

char bank[32] = {0}, ret[4] = {0};
sprintf(bank, "gpiochip%d", gpiobank);
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "GPIO gpiobank is %d", gpiobank);
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "GPIO gpioline is %d", gpioline);

jint value = gpiod_ctxless_get_value((char *)bank, gpioline, 0, "gpioget");

sprintf(ret, "%d", value);

return env->NewStringUTF(ret);
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_gpiod_MainActivity_setGpioInfo(JNIEnv *env, jobject thiz, jint gpiobank, jint gpioline, jint gpio_value) {

std::ignore = thiz;

char bank[32] = {0}, ret_str[32] = {0};
sprintf(bank, "/dev/gpiochip%d", gpiobank);

int ret = gpiod_ctxless_set_value(bank, gpioline, gpio_value, 0, "gpioset", NULL, NULL);

if(ret)
sprintf(ret_str, "Set Failed !");
else
sprintf(ret_str, "Set Successed !");

__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "set gpio ret = %d", ret);
return env->NewStringUTF(ret_str);
}


extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_gpiod_MainActivity_stringFromJNI(JNIEnv *env, jobject thiz) {
// TODO: implement stringFromJNI()
return env->NewStringUTF("Test");
}
Loading

0 comments on commit 184451a

Please sign in to comment.