|
| 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 | +} |
0 commit comments