-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I85c197be667e266845646d3822d07109030715b2
- Loading branch information
Showing
8 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
include ':blink', ':button' | ||
|
||
include ':blink', ':button', ':speaker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |