1 package com.google.android.DemoKit; 2 3 import java.text.DecimalFormat; 4 import java.util.ArrayList; 5 6 import android.graphics.drawable.Drawable; 7 import android.widget.ImageView; 8 import android.widget.TextView; 9 10 public class InputController extends AccessoryController { 11 private TextView mTemperature; 12 private TextView mLightView; 13 private TextView mLightRawView; 14 private JoystickView mJoystickView; 15 ArrayList<SwitchDisplayer> mSwitchDisplayers; 16 private final DecimalFormat mLightValueFormatter = new DecimalFormat("##.#"); 17 private final DecimalFormat mTemperatureFormatter = new DecimalFormat( 18 "###" + (char)0x00B0); 19 20 InputController(DemoKitActivity hostActivity) { 21 super(hostActivity); 22 mTemperature = (TextView) findViewById(R.id.tempValue); 23 mLightView = (TextView) findViewById(R.id.lightPercentValue); 24 mLightRawView = (TextView) findViewById(R.id.lightRawValue); 25 mJoystickView = (JoystickView) findViewById(R.id.joystickView); 26 } 27 28 protected void onAccesssoryAttached() { 29 mSwitchDisplayers = new ArrayList<SwitchDisplayer>(); 30 for (int i = 0; i < 4; ++i) { 31 SwitchDisplayer sd = new SwitchDisplayer(i); 32 mSwitchDisplayers.add(sd); 33 } 34 } 35 36 public void setTemperature(int temperatureFromArduino) { 37 /* 38 * Arduino board contains a 6 channel (8 channels on the Mini and Nano, 39 * 16 on the Mega), 10-bit analog to digital converter. This means that 40 * it will map input voltages between 0 and 5 volts into integer values 41 * between 0 and 1023. This yields a resolution between readings of: 5 42 * volts / 1024 units or, .0049 volts (4.9 mV) per unit. 43 */ 44 double voltagemv = temperatureFromArduino * 4.9; 45 /* 46 * The change in voltage is scaled to a temperature coefficient of 10.0 47 * mV/degC (typical) for the MCP9700/9700A and 19.5 mV/degC (typical) 48 * for the MCP9701/9701A. The out- put voltage at 0 degC is also scaled 49 * to 500 mV (typical) and 400 mV (typical) for the MCP9700/9700A and 50 * MCP9701/9701A, respectively. VOUT = TCTA+V0degC 51 */ 52 double kVoltageAtZeroCmv = 400; 53 double kTemperatureCoefficientmvperC = 19.5; 54 double ambientTemperatureC = ((double) voltagemv - kVoltageAtZeroCmv) 55 / kTemperatureCoefficientmvperC; 56 double temperatureF = (9.0 / 5.0) * ambientTemperatureC + 32.0; 57 mTemperature.setText(mTemperatureFormatter.format(temperatureF)); 58 } 59 60 public void setLightValue(int lightValueFromArduino) { 61 mLightRawView.setText(String.valueOf(lightValueFromArduino)); 62 mLightView.setText(mLightValueFormatter 63 .format((100.0 * (double) lightValueFromArduino / 1024.0))); 64 } 65 66 public void switchStateChanged(int switchIndex, boolean switchState) { 67 if (switchIndex >= 0 && switchIndex < mSwitchDisplayers.size()) { 68 SwitchDisplayer sd = mSwitchDisplayers.get(switchIndex); 69 sd.onSwitchStateChange(switchState); 70 } 71 } 72 73 public void joystickButtonSwitchStateChanged(boolean buttonState) { 74 mJoystickView.setPressed(buttonState); 75 } 76 77 public void joystickMoved(int x, int y) { 78 mJoystickView.setPosition(x, y); 79 } 80 81 public void onTemperature(int temperature) { 82 setTemperature(temperature); 83 } 84 85 public void onLightChange(int lightValue) { 86 setLightValue(lightValue); 87 } 88 89 public void onSwitchStateChange(int switchIndex, Boolean switchState) { 90 switchStateChanged(switchIndex, switchState); 91 } 92 93 public void onButton(Boolean buttonState) { 94 joystickButtonSwitchStateChanged(buttonState); 95 } 96 97 public void onStickMoved(int x, int y) { 98 joystickMoved(x, y); 99 } 100 101 class SwitchDisplayer { 102 private final ImageView mTargetView; 103 private final Drawable mOnImage; 104 private final Drawable mOffImage; 105 106 SwitchDisplayer(int switchIndex) { 107 int viewId, onImageId, offImageId; 108 switch (switchIndex) { 109 default: 110 viewId = R.id.Button1; 111 onImageId = R.drawable.indicator_button1_on_noglow; 112 offImageId = R.drawable.indicator_button1_off_noglow; 113 break; 114 case 1: 115 viewId = R.id.Button2; 116 onImageId = R.drawable.indicator_button2_on_noglow; 117 offImageId = R.drawable.indicator_button2_off_noglow; 118 break; 119 case 2: 120 viewId = R.id.Button3; 121 onImageId = R.drawable.indicator_button3_on_noglow; 122 offImageId = R.drawable.indicator_button3_off_noglow; 123 break; 124 case 3: 125 viewId = R.id.Button4; 126 onImageId = R.drawable.indicator_button_capacitive_on_noglow; 127 offImageId = R.drawable.indicator_button_capacitive_off_noglow; 128 break; 129 } 130 mTargetView = (ImageView) findViewById(viewId); 131 mOffImage = mHostActivity.getResources().getDrawable(offImageId); 132 mOnImage = mHostActivity.getResources().getDrawable(onImageId); 133 } 134 135 public void onSwitchStateChange(Boolean switchState) { 136 Drawable currentImage; 137 if (!switchState) { 138 currentImage = mOffImage; 139 } else { 140 currentImage = mOnImage; 141 } 142 mTargetView.setImageDrawable(currentImage); 143 } 144 145 } 146 } 147