Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit de1affd

Browse files
vintummalaBruce j Beare
authored andcommitted
An android Application with multiple sensors integrated (#38)
* Android Application with multiple sensors integrated. Signed-off-by: Vineela Tummalapalli <vineela.tummalapalli@intel.com> * Update README.md * Update build.gradle
1 parent e71ed20 commit de1affd

File tree

25 files changed

+517
-0
lines changed

25 files changed

+517
-0
lines changed

multisensor/.gitignore

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

multisensor/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Sample Android Things Application with multiple sensors using UPM
2+
-----------------------------------------------------------------
3+
4+
This example demonstrates an application with multiple sensors using I2c and GPIO connection using
5+
UPM.
6+
7+
Pre-Requisites:
8+
---------------
9+
Use of the Grove Kit (for Joule or Edison) makes this easy. See the following links for getting
10+
a starter kit.
11+
12+
* https://www.seeedstudio.com/Grove-Maker-Kit-for-Intel-Joule-p-2796.html
13+
* https://www.seeedstudio.com/Grove-Starter-Kit-V3-p-1855.html
14+
15+
16+
You will need:
17+
18+
1. Android Things compatible board.
19+
2. Grove header or Breakout board.
20+
3. Joule Shield.
21+
4. A Grove touch sensor.
22+
5. A Grove push button.
23+
6. A JHD1313m1 LCD display (it's in the grove kit).
24+
7. A TMP006 Temperature Sensor.
25+
8. 3 LEDs. One of each color. Red LED, Blue LED and Green LED.
26+
27+
28+
Build and install:
29+
------------------
30+
31+
On Android Studio, select the "multisensor" module in select box by the "Run" button
32+
and then click on the "Run" button.
33+
34+
35+
Changing the GPIO and I2C bus
36+
-----------------------------
37+
This example uses a GPIO (digital input) to read the state of a touch sensor, a GPIO (digital input)
38+
to read the state of a button, 3 GPIOs (digital output) to light LEDs (Red, Green and Blue).
39+
An I2C temperature sensor to get the ambient temperature and an I2c
40+
jhd1313m1 LCD display to display the temperature values.connected via the shield to the sensors.
41+
42+
The I2C and GPIO line to be used is specified in the strings.xml file (src/res/values directory).
43+
44+
````
45+
<resources>
46+
<string name="app_name">MultiSensor</string>
47+
48+
<string name="I2C_Edison_Arduino">I2C6</string>
49+
<string name="I2C_Edison_Sparkfun">I2C1</string>
50+
<string name="I2C_Joule_Tuchuck">I2C0</string>
51+
52+
<string name="LedRed_Edison_Arduino">IO13</string>
53+
<string name="LedRed_Edison_Sparkfun">GP12</string>
54+
<string name="LedRed_Joule_Tuchuck">J6_22</string>
55+
56+
<string name="Button_Edison_Arduino">IO0</string>
57+
<string name="Button_Edison_Sparkfun">GP20</string>
58+
<string name="Button_Joule_Tuchuck">J7_68</string>
59+
60+
<string name="LedBlue_Edison_Arduino">IO13</string>
61+
<string name="LedBlue_Edison_Sparkfun">GP12</string>
62+
<string name="LedBlue_Joule_Tuchuck">J6_1</string>
63+
64+
<string name="Touch_Edison_Arduino">IO0</string>
65+
<string name="Touch_Edison_Sparkfun">GP20</string>
66+
<string name="Touch_Joule_Tuchuck">J7_64</string>
67+
68+
<string name="LedGreen_Edison_Arduino">IO13</string>
69+
<string name="LedGreen_Edison_Sparkfun">GP12</string>
70+
<string name="LedGreen_Joule_Tuchuck">J7_58</string>
71+
72+
</resources>
73+
````
74+
75+
The code will automatically determine the board type being run on (modify BoardDefaults.java
76+
in the driver library to add another board) and select a string from this file for the GPIO line.
77+
These strings are programmed into the Peripheral Manager and read from there
78+
into the UPM library to determine the GPIO pin to be used.
79+
80+
See the top level README.md for a table describing the available GPIO pins and where to find them
81+
on the board.

multisensor/build.gradle

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.example.upm.androidthings.driversamples"
9+
minSdkVersion 24
10+
targetSdkVersion 25
11+
versionCode 1
12+
versionName "1.0"
13+
14+
jackOptions {
15+
enabled true
16+
}
17+
}
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
28+
}
29+
30+
dependencies {
31+
compile fileTree(dir: 'libs', include: ['*.jar'])
32+
33+
compile 'com.android.support:appcompat-v7:25.3.1'
34+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
35+
compile project(':driversupport')
36+
compile 'io.mraa.at.upm:upm_tmp006:1.+'
37+
compile 'io.mraa.at.upm:upm_jhd1313m1:1.+'
38+
compile 'io.mraa.at.upm:upm_grove:1.+'
39+
compile 'io.mraa.at.upm:upm_ttp223:1.+'
40+
}

multisensor/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/brillo/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (c) 2017 Intel Corporation.
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+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.upm.androidthings.driversamples">
20+
21+
<application
22+
android:allowBackup="true"
23+
android:icon="@mipmap/ic_launcher"
24+
android:label="@string/app_name"
25+
android:roundIcon="@mipmap/ic_launcher_round"
26+
android:supportsRtl="true">
27+
<activity android:name=".MultiSensorActivity">
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
30+
31+
<category android:name="android.intent.category.LAUNCHER" />
32+
</intent-filter>
33+
</activity>
34+
</application>
35+
36+
</manifest>
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Copyright (c) 2017 Intel Corporation.
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.upm.androidthings.driversamples;
18+
19+
import android.app.Activity;
20+
import android.os.AsyncTask;
21+
import android.os.Bundle;
22+
import android.util.Log;
23+
import android.widget.ImageView;
24+
import android.widget.TextView;
25+
26+
import com.example.upm.androidthings.driversupport.BoardDefaults;
27+
28+
import mraa.mraa;
29+
30+
public class MultiSensorActivity extends Activity {
31+
private static final String TAG = "MultiSensorActivity";
32+
33+
short[][] rgb = new short[][]{
34+
{0xd1, 0x00, 0x00}, // red
35+
{0x33, 0xdd, 0x00}, // green
36+
{0x11, 0x33, 0xcc}}; // blue
37+
TextView tv;
38+
ImageView im1;
39+
ImageView im2;
40+
ImageView im3;
41+
42+
upm_tmp006.TMP006 thermopile;
43+
upm_jhd1313m1.Jhd1313m1 lcd;
44+
upm_grove.GroveLed Redled;
45+
upm_grove.GroveButton button;
46+
upm_grove.GroveLed Blueled;
47+
upm_ttp223.TTP223 touch;
48+
upm_grove.GroveLed Greenled;
49+
50+
float Temp = 0.0f;
51+
private int ndx = 0;
52+
Runnable thermopileTask = new Runnable() {
53+
54+
@Override
55+
public void run() {
56+
// Moves the current thread into the background
57+
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
58+
59+
thermopile.setActive();
60+
try {
61+
if (lcd != null) {
62+
lcd.clear();
63+
}
64+
while (true) {
65+
// Print out temperature value in °C
66+
Temp = thermopile.getTemperature(1);
67+
Log.i(TAG, "Temperature: " + Temp + " °C");
68+
69+
updateUI();
70+
Thread.sleep(1000);
71+
}
72+
} catch (InterruptedException e) {
73+
Thread.currentThread().interrupt();
74+
} finally {
75+
lcd.delete();
76+
thermopile.delete();
77+
Redled.off();
78+
Redled.delete();
79+
button.delete();
80+
Blueled.off();
81+
Blueled.delete();
82+
touch.delete();
83+
Greenled.off();
84+
Greenled.delete();
85+
MultiSensorActivity.this.finish();
86+
}
87+
}
88+
};
89+
90+
@Override
91+
protected void onCreate(Bundle savedInstanceState) {
92+
super.onCreate(savedInstanceState);
93+
setContentView(R.layout.activity_multi_sensor);
94+
BoardDefaults bd = new BoardDefaults(this.getApplicationContext());
95+
96+
int i2cIndex = -1;
97+
int gpioRedLedIndex = -1;
98+
int gpioButtonIndex = -1;
99+
int gpioBlueLedIndex = -1;
100+
int gpioTouchIndex = -1;
101+
int gpioGreenLedIndex = -1;
102+
103+
tv = (TextView) findViewById(R.id.activity_Temp_Values);
104+
im1 = (ImageView) findViewById(R.id.imageView);
105+
im2 = (ImageView) findViewById(R.id.imageView2);
106+
im3 = (ImageView) findViewById(R.id.imageView3);
107+
108+
switch (bd.getBoardVariant()) {
109+
case BoardDefaults.DEVICE_EDISON_ARDUINO:
110+
i2cIndex = mraa.getI2cLookup(getString(R.string.I2C_Edison_Arduino));
111+
gpioRedLedIndex = mraa.getGpioLookup(getString(R.string.LedRed_Edison_Arduino));
112+
gpioButtonIndex = mraa.getGpioLookup(getString(R.string.Button_Edison_Arduino));
113+
gpioBlueLedIndex = mraa.getGpioLookup(getString(R.string.LedBlue_Edison_Arduino));
114+
gpioTouchIndex = mraa.getGpioLookup(getString(R.string.Touch_Edison_Arduino));
115+
gpioGreenLedIndex = mraa.getGpioLookup(getString(R.string.LedGreen_Edison_Arduino));
116+
break;
117+
case BoardDefaults.DEVICE_EDISON_SPARKFUN:
118+
i2cIndex = mraa.getI2cLookup(getString(R.string.I2C_Edison_Sparkfun));
119+
gpioRedLedIndex = mraa.getGpioLookup(getString(R.string.LedRed_Edison_Sparkfun));
120+
gpioButtonIndex = mraa.getGpioLookup(getString(R.string.Button_Edison_Sparkfun));
121+
gpioBlueLedIndex = mraa.getGpioLookup(getString(R.string.LedBlue_Edison_Arduino));
122+
gpioTouchIndex = mraa.getGpioLookup(getString(R.string.Touch_Edison_Sparkfun));
123+
gpioGreenLedIndex = mraa.getGpioLookup(getString(R.string.LedGreen_Edison_Arduino));
124+
break;
125+
case BoardDefaults.DEVICE_JOULE_TUCHUCK:
126+
i2cIndex = mraa.getI2cLookup(getString(R.string.I2C_Joule_Tuchuck));
127+
gpioRedLedIndex = mraa.getGpioLookup(getString(R.string.LedRed_Joule_Tuchuck));
128+
gpioButtonIndex = mraa.getGpioLookup(getString(R.string.Button_Joule_Tuchuck));
129+
gpioBlueLedIndex = mraa.getGpioLookup(getString(R.string.LedBlue_Joule_Tuchuck));
130+
gpioTouchIndex = mraa.getGpioLookup(getString(R.string.Touch_Joule_Tuchuck));
131+
gpioGreenLedIndex = mraa.getGpioLookup(getString(R.string.LedGreen_Joule_Tuchuck));
132+
break;
133+
default:
134+
throw new IllegalStateException("Unknown Board Variant: " + bd.getBoardVariant());
135+
}
136+
137+
thermopile = new upm_tmp006.TMP006(i2cIndex, (short) 0, 64);
138+
lcd = new upm_jhd1313m1.Jhd1313m1(i2cIndex);
139+
Redled = new upm_grove.GroveLed(gpioRedLedIndex);
140+
button = new upm_grove.GroveButton(gpioButtonIndex);
141+
Blueled = new upm_grove.GroveLed(gpioBlueLedIndex);
142+
touch = new upm_ttp223.TTP223(gpioTouchIndex);
143+
Greenled = new upm_grove.GroveLed(gpioGreenLedIndex);
144+
AsyncTask.execute(thermopileTask);
145+
}
146+
147+
private void updateUI() {
148+
this.runOnUiThread(new Runnable() {
149+
@Override
150+
public void run() {
151+
tv.setText("Temp values" + " " + Float.toString(Temp));
152+
153+
// Alternate rows on the LCD
154+
lcd.setCursor(ndx % 2, 0);
155+
156+
// Change the color
157+
short r = rgb[ndx % 3][0];
158+
short g = rgb[ndx % 3][1];
159+
short b = rgb[ndx % 3][2];
160+
lcd.setColor(r, g, b);
161+
162+
lcd.write("Temp" + " " + Temp + " °C");
163+
ndx++;
164+
165+
if (button.value() == 0) {
166+
Blueled.off();
167+
im3.setImageResource(R.drawable.blue_off);
168+
} else {
169+
Blueled.on();
170+
im3.setImageResource(R.drawable.blue_on);
171+
}
172+
173+
if (Temp >= 27) {
174+
Redled.on();
175+
im2.setImageResource(R.drawable.red_on);
176+
} else {
177+
Redled.off();
178+
im2.setImageResource(R.drawable.red_off);
179+
}
180+
181+
if (touch.isPressed()) {
182+
Greenled.on();
183+
im1.setImageResource(R.drawable.green_on);
184+
} else {
185+
Greenled.off();
186+
im1.setImageResource(R.drawable.green_off);
187+
}
188+
}
189+
});
190+
}
191+
192+
@Override
193+
protected void onDestroy() {
194+
super.onDestroy();
195+
196+
Thread.currentThread().interrupt();
197+
lcd.delete();
198+
thermopile.delete();
199+
Redled.delete();
200+
button.delete();
201+
Blueled.delete();
202+
touch.delete();
203+
Greenled.delete();
204+
}
205+
}
9.88 KB
Loading
6.55 KB
Loading
4.46 KB
Loading
1.37 KB
Loading

0 commit comments

Comments
 (0)