Skip to content

Commit

Permalink
sample-nativepio: add PWM sample
Browse files Browse the repository at this point in the history
Change-Id: I85c197be667e266845646d3822d07109030715b2
  • Loading branch information
proppy committed Feb 7, 2017
1 parent b20dc96 commit bd93d55
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 2 deletions.
3 changes: 1 addition & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
include ':blink', ':button'

include ':blink', ':button', ':speaker'
50 changes: 50 additions & 0 deletions speaker/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2017, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply plugin: 'com.android.application'

android {
compileSdkVersion = 24
buildToolsVersion '24.0.3'

defaultConfig {
applicationId = 'com.example.androidthings.nativepio'
minSdkVersion 24
targetSdkVersion 24
versionCode 1
versionName "1.0"
ndk {
abiFilters 'armeabi-v7a', 'x86'
}
externalNativeBuild {
cmake {
arguments "-DLIBANDROIDTHINGS_DIR=${rootProject.projectDir}/libandroidthings"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
}
39 changes: 39 additions & 0 deletions speaker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidthings.nativepio">

<application android:allowBackup="true" android:icon="@android:drawable/sym_def_app_icon"
android:label="@string/app_name">
<uses-library android:name="com.google.android.things"/>
<activity android:name="android.app.NativeActivity">
<meta-data android:name="android.app.lib_name" android:value="speaker" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!-- Launch activity automatically on boot -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.IOT_LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>

</manifest>
35 changes: 35 additions & 0 deletions speaker/src/main/cpp/AndroidSystemProperties.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "AndroidSystemProperties.h"

#include <jni.h>
#include <android/native_activity.h>

AndroidSystemProperties::AndroidSystemProperties(ANativeActivity* activity) {
JNIEnv *jni;
activity->vm->AttachCurrentThread(&jni, NULL);
jclass buildClass = jni->FindClass("android/os/Build");
jfieldID buildDeviceFieldID = jni->GetStaticFieldID(buildClass, "DEVICE",
"Ljava/lang/String;");
jstring buildDeviceField = (jstring) jni->GetStaticObjectField(buildClass,
buildDeviceFieldID);
const char *buildDevice = jni->GetStringUTFChars(buildDeviceField, NULL);
buildDevice_ = buildDevice;
jni->ReleaseStringUTFChars(buildDeviceField, buildDevice);
activity->vm->DetachCurrentThread();
}
35 changes: 35 additions & 0 deletions speaker/src/main/cpp/AndroidSystemProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef ANDROID_SYSTEM_PROPERTIES_H
#define ANDROID_SYSTEM_PROPERTIES_H

#include <string>

struct ANativeActivity;

class AndroidSystemProperties {
public:
AndroidSystemProperties(ANativeActivity* activity);
const std::string& getBuildDevice() {
return buildDevice_;
}
private:
std::string buildDevice_;
};


#endif //ANDROID_SYSTEM_PROPERTIES_H
46 changes: 46 additions & 0 deletions speaker/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

cmake_minimum_required(VERSION 3.4.1)

add_library(speaker SHARED
speaker.cpp)

target_include_directories(speaker PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue
${LIBANDROIDTHINGS_DIR}/${ANDROID_ABI}/include)

add_library(android-system-properties STATIC
AndroidSystemProperties.cpp)
target_include_directories(android-system-properties PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue
${LIBANDROIDTHINGS_DIR}/${ANDROID_ABI}/include)

add_library(native-app-glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

add_library(androidthings SHARED
IMPORTED)
set_target_properties(androidthings
PROPERTIES IMPORTED_LOCATION
${LIBANDROIDTHINGS_DIR}/${ANDROID_ABI}/lib/libandroidthings.so)

target_link_libraries(speaker
android
log
native-app-glue
androidthings
android-system-properties)
95 changes: 95 additions & 0 deletions speaker/src/main/cpp/speaker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <cmath>

#include <android/log.h>
#include <android/looper.h>
#include <android_native_app_glue.h>

#include <pio/gpio.h>
#include <pio/peripheral_manager_client.h>

#include "AndroidSystemProperties.h"

const char* TAG = "speaker";
const double NOTE_A4_FREQUENCY = 440.0;
const double FREQUENCY_INC_PER_MS = 0.1;

#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
#define ASSERT(cond, ...) if (!(cond)) { __android_log_assert(#cond, TAG, __VA_ARGS__);}

int64_t millis() {
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return now.tv_sec*1000 + llround(now.tv_nsec / 1e6);
}

void android_main(android_app* app) {
app_dummy(); // prevent native-app-glue to be stripped.

AndroidSystemProperties systemProperties(app->activity);
const char* SPEAKER_PWM;
if (systemProperties.getBuildDevice() == "rpi3") {
SPEAKER_PWM = "PWM1";
} else if (systemProperties.getBuildDevice() == "edison") {
SPEAKER_PWM = "IO3";
} else {
LOGE("unsupported device: %s", systemProperties.getBuildDevice().c_str());
return;
}

APeripheralManagerClient* client = APeripheralManagerClient_new();
ASSERT(client, "failed to open peripheral manager client");
APwm* pwm;
APeripheralManagerClient_openPwm(client, SPEAKER_PWM, &pwm);
ASSERT(pwm, "failed to open PWM: %s", SPEAKER_PWM);
int setDutyCycleResult = APwm_setDutyCycle(pwm, 50.0);
ASSERT(setDutyCycleResult == 0, "failed to set PWM duty cycle: %s", SPEAKER_PWM);
int enablePwmResult = APwm_setEnabled(pwm, 1);
ASSERT(enablePwmResult == 0, "failed to enable PWM: %s", SPEAKER_PWM);

double frequency = NOTE_A4_FREQUENCY;
int64_t lastMs = millis();
while (!app->destroyRequested) {
android_poll_source* source;
int pollResult = ALooper_pollOnce(0, NULL, NULL, (void**)&source);
if (pollResult >= 0) {
if (source != NULL) {
// forward event to native-app-glue to handle lifecycle and input event
// and update `app` state.
source->process(app, source);
}
}
if (APwm_setFrequencyHz(pwm, frequency) != 0) {
continue; // retry
}
int64_t now = millis();
int64_t elapsedMs = now - lastMs;
frequency += FREQUENCY_INC_PER_MS * elapsedMs;
if (frequency > NOTE_A4_FREQUENCY * 2) {
frequency = NOTE_A4_FREQUENCY;
}
lastMs = now;
}
APwm_setEnabled(pwm, 0);
APwm_delete(pwm);
APeripheralManagerClient_delete(client);
}
4 changes: 4 additions & 0 deletions speaker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Speaker</string>
</resources>

0 comments on commit bd93d55

Please sign in to comment.