Skip to content

Commit 40e8786

Browse files
author
Dave Smith
committed
Add ADC sample for the ADS1015 module.
Change-Id: Ic24e43f1fc41dd3a470837d40575fd3f99559128 Signed-off-by: Dave Smith <smithdave@google.com>
1 parent 8fef931 commit 40e8786

File tree

11 files changed

+301
-0
lines changed

11 files changed

+301
-0
lines changed

ads1015/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ads1015/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
ADS1015 ADC sample for Android Things
2+
=====================================
3+
4+
This Android Things sample demonstrates how to use an ADS1015 ADC module
5+
with the ADS1xxx contrib driver.
6+
7+
8+
Pre-requisites
9+
--------------
10+
11+
- Android Things compatible board
12+
- Android Studio 2.2+
13+
- 1 [ADS1015 ADC module](https://www.adafruit.com/product/1083)
14+
- jumper wires
15+
- 1 breadboard
16+
17+
Schematics
18+
----------
19+
20+
![Schematics for Raspberry Pi 3](rpi3_schematics.png)
21+
22+
Build and install
23+
=================
24+
25+
On Android Studio, click on the "Run" button.
26+
27+
If you prefer to run on the command line, from this repository's root directory, type
28+
29+
```bash
30+
./gradlew ads1015:installDebug
31+
adb shell am start com.example.androidthings.driversamples/.MainActivity
32+
```
33+
34+
If you have everything set up correctly, the latest ADC reading will be
35+
written to logcat.
36+
37+
License
38+
-------
39+
40+
Copyright 2018 The Android Open Source Project, Inc.
41+
42+
Licensed to the Apache Software Foundation (ASF) under one or more contributor
43+
license agreements. See the NOTICE file distributed with this work for
44+
additional information regarding copyright ownership. The ASF licenses this
45+
file to you under the Apache License, Version 2.0 (the "License"); you may not
46+
use this file except in compliance with the License. You may obtain a copy of
47+
the License at
48+
49+
http://www.apache.org/licenses/LICENSE-2.0
50+
51+
Unless required by applicable law or agreed to in writing, software
52+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
53+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
54+
License for the specific language governing permissions and limitations under
55+
the License.

ads1015/build.gradle

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2018, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: 'com.android.application'
18+
19+
android {
20+
compileSdkVersion 27
21+
22+
defaultConfig {
23+
applicationId 'com.example.androidthings.driversamples'
24+
minSdkVersion 27
25+
targetSdkVersion 27
26+
versionCode 1
27+
versionName "1.0"
28+
}
29+
30+
buildTypes {
31+
release {
32+
minifyEnabled false
33+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34+
}
35+
}
36+
37+
}
38+
39+
dependencies {
40+
compileOnly 'com.google.android.things:androidthings:1.0'
41+
implementation 'com.google.android.things.contrib:driver-adc:2.0'
42+
}

ads1015/rpi3_schematics.fzz

18.7 KB
Binary file not shown.

ads1015/rpi3_schematics.png

272 KB
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2018 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
package="com.example.android.things.ads1015">
19+
20+
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
21+
22+
<application
23+
android:label="@string/app_name">
24+
<uses-library android:name="com.google.android.things" />
25+
26+
<activity android:name=".MainActivity">
27+
<intent-filter>
28+
<action android:name="android.intent.action.MAIN" />
29+
<category android:name="android.intent.category.LAUNCHER" />
30+
</intent-filter>
31+
32+
<!--
33+
Uncomment the following intent filter block to enable this activity to
34+
launch automatically on boot, and re-launch if the app terminates.
35+
-->
36+
<!--
37+
<intent-filter>
38+
<action android:name="android.intent.action.MAIN"/>
39+
<category android:name="android.intent.category.HOME"/>
40+
<category android:name="android.intent.category.DEFAULT"/>
41+
</intent-filter>
42+
-->
43+
</activity>
44+
</application>
45+
46+
</manifest>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2018, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.things.ads1015;
18+
19+
import android.os.Build;
20+
21+
@SuppressWarnings("WeakerAccess")
22+
public class BoardDefaults {
23+
private static final String DEVICE_RPI3 = "rpi3";
24+
private static final String DEVICE_IMX6UL_PICO = "imx6ul_pico";
25+
private static final String DEVICE_IMX7D_PICO = "imx7d_pico";
26+
27+
/**
28+
* Return the preferred I2C port for each board.
29+
*/
30+
public static String getI2CPort() {
31+
switch (Build.DEVICE) {
32+
case DEVICE_RPI3:
33+
return "I2C1";
34+
case DEVICE_IMX6UL_PICO:
35+
return "I2C2";
36+
case DEVICE_IMX7D_PICO:
37+
return "I2C1";
38+
default:
39+
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
40+
}
41+
}
42+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2018, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.things.ads1015;
18+
19+
import android.app.Activity;
20+
import android.os.Bundle;
21+
import android.os.Handler;
22+
import android.os.HandlerThread;
23+
import android.util.Log;
24+
import android.widget.TextView;
25+
26+
import com.google.android.things.contrib.driver.adc.ads1xxx.Ads1xxx;
27+
28+
import java.io.IOException;
29+
30+
/**
31+
* Sample activity that demonstrates the usage of the
32+
* Ads1xxx driver with an ADS1015 module.
33+
*
34+
* https://www.adafruit.com/product/1083
35+
*/
36+
public class MainActivity extends Activity {
37+
private static final String TAG = MainActivity.class.getSimpleName();
38+
39+
private static final int DELAY_MS = 100; // 10 samples/second
40+
41+
private Ads1xxx mAdcDriver;
42+
private Handler mHandler;
43+
private HandlerThread mReadThread;
44+
45+
@Override
46+
protected void onCreate(Bundle savedInstanceState) {
47+
super.onCreate(savedInstanceState);
48+
49+
try {
50+
Log.d(TAG, "Initializing ADC Driver");
51+
mAdcDriver = new Ads1xxx(BoardDefaults.getI2CPort(), Ads1xxx.Configuration.ADS1015);
52+
// Increase default range to fit +3.3V
53+
mAdcDriver.setInputRange(Ads1xxx.RANGE_4_096V);
54+
55+
// Set up I/O polling thread
56+
mReadThread = new HandlerThread("ADC Reader");
57+
mReadThread.start();
58+
mHandler = new Handler(mReadThread.getLooper());
59+
mHandler.post(mReadAction);
60+
} catch (IOException e) {
61+
Log.e(TAG, "Failed to initialize ADC driver", e);
62+
}
63+
}
64+
65+
@Override
66+
protected void onDestroy() {
67+
super.onDestroy();
68+
mReadThread.quit();
69+
70+
try {
71+
mAdcDriver.close();
72+
} catch (IOException e) {
73+
Log.e(TAG, "Failed to close ADC driver", e);
74+
} finally {
75+
mAdcDriver = null;
76+
}
77+
}
78+
79+
/* Read a single analog sample and log the result */
80+
private Runnable mReadAction = new Runnable() {
81+
@Override
82+
public void run() {
83+
try {
84+
// Read differential between IN0+/IN1-
85+
final int value = mAdcDriver.readDifferentialInput(Ads1xxx.INPUT_DIFF_0P_1N);
86+
Log.i(TAG, "Current ADC value: " + value);
87+
} catch (IOException e) {
88+
Log.e(TAG, "Unable to read analog sample", e);
89+
}
90+
91+
mHandler.postDelayed(this, DELAY_MS);
92+
}
93+
};
94+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2018 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<resources>
18+
<string name="app_name">ADS1015</string>
19+
</resources>

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ buildscript {
3030
allprojects {
3131
repositories {
3232
jcenter()
33+
google()
3334
}
3435
}
3536

0 commit comments

Comments
 (0)