Skip to content

Commit 666f3fd

Browse files
committed
Update so mutiple requests can be handled
2 parents 292d917 + b227581 commit 666f3fd

File tree

5 files changed

+61
-117
lines changed

5 files changed

+61
-117
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,15 @@ dht22Driver = new Dht22Driver(mArduino);
3939
dht22Driver.startup();
4040
```
4141

42-
- Call it in a handler (to be modified)
42+
- Call it
4343
```
44-
new Handler().postDelayed(new Runnable() {
45-
public void run() {
46-
try {
47-
dht22Driver.getTemperature(temperature, MainActivity.this);
48-
Log.d(TAG, "run: Temperature");
49-
} catch (IOException e) {
50-
e.printStackTrace();
51-
}
52-
}
53-
}, 0);
44+
String temperature = dht22Driver.getTemperature();
45+
String humidity = dht22Driver.getHumidity();
5446
```
5547

5648
Connections
5749
----------
58-
![Conenctions](https://github.com/mplacona/arduwrap/blob/master/arduwrap.png?raw=true)
50+
![Connections](https://github.com/mplacona/arduwrap/blob/master/arduwrap.png?raw=true)
5951

6052
License
6153
=======
Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
package rocks.androidthings.arduwrap_sample;
22

33
import android.databinding.DataBindingUtil;
4-
import android.os.Handler;
4+
import android.support.design.widget.FloatingActionButton;
55
import android.support.v7.app.AppCompatActivity;
66
import android.os.Bundle;
7-
import android.util.Log;
7+
import android.view.View;
88
import android.widget.TextView;
99

1010
import java.io.IOException;
1111

1212
import rocks.androidthings.arduwrap.Arduino;
1313
import rocks.androidthings.arduwrap.Dht22Driver;
14-
import rocks.androidthings.arduwrap.OnMessageCompleteListener;
1514
import rocks.androidthings.arduwrap_sample.databinding.ActivityMainBinding;
1615

17-
public class MainActivity extends AppCompatActivity implements OnMessageCompleteListener {
16+
public class MainActivity extends AppCompatActivity {
1817

1918
private static final String TAG = "MainActivity";
2019
private static final String UART_DEVICE_NAME = "UART0";
2120
private static final int BAUD_RATE = 115200;
2221
private static final int DATA_BITS = 8;
2322
private static final int STOP_BITS = 1;
24-
Dht22Driver dht22Driver;
23+
private Dht22Driver dht22Driver;
24+
private TextView mTemperature;
25+
private TextView mHumidity;
2526

2627
@Override
2728
protected void onCreate(Bundle savedInstanceState) {
2829
super.onCreate(savedInstanceState);
2930
final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
31+
FloatingActionButton fab = binding.floatingActionButton;
3032

31-
final TextView temperature = binding.temperatureTv;
32-
final TextView humidity = binding.humidityTv;
33+
mTemperature = binding.temperatureTv;
34+
mHumidity = binding.humidityTv;
3335

3436
Arduino mArduino = new Arduino.ArduinoBuilder()
3537
.uartDeviceName(UART_DEVICE_NAME)
@@ -41,28 +43,13 @@ protected void onCreate(Bundle savedInstanceState) {
4143
dht22Driver = new Dht22Driver(mArduino);
4244
dht22Driver.startup();
4345

44-
45-
new Handler().postDelayed(new Runnable() {
46-
public void run() {
47-
try {
48-
dht22Driver.getTemperature(temperature, MainActivity.this);
49-
Log.d(TAG, "run: Temperature");
50-
} catch (IOException e) {
51-
e.printStackTrace();
52-
}
53-
}
54-
}, 0);
55-
56-
new Handler().postDelayed(new Runnable() {
57-
public void run() {
58-
try {
59-
dht22Driver.getHumidity(humidity, MainActivity.this);
60-
Log.d(TAG, "run: Humidity");
61-
} catch (IOException e) {
62-
e.printStackTrace();
63-
}
46+
fab.setOnClickListener(new View.OnClickListener() {
47+
@Override
48+
public void onClick(View view) {
49+
mTemperature.setText(String.format("%s°C", dht22Driver.getTemperature()));
50+
mHumidity.setText(String.format("%s%%", dht22Driver.getHumidity()));
6451
}
65-
}, 1000);
52+
});
6653

6754
}
6855

@@ -71,9 +58,4 @@ protected void onDestroy(){
7158
dht22Driver.shutdown();
7259
super.onDestroy();
7360
}
74-
75-
@Override
76-
public void onMessageComplete(String message, TextView field) {
77-
field.setText(message);
78-
}
7961
}

arduwrap/src/main/java/rocks/androidthings/arduwrap/BaseSensor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
public interface BaseSensor {
20-
void onStart();
21-
void onStop();
19+
interface BaseSensor {
2220
void startup();
2321
void shutdown();
2422
}
Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package rocks.androidthings.arduwrap;
22

33
import android.util.Log;
4-
import android.widget.TextView;
54

65
import com.google.android.things.pio.PeripheralManagerService;
76
import com.google.android.things.pio.UartDevice;
8-
import com.google.android.things.pio.UartDeviceCallback;
97

108
import java.io.IOException;
119

@@ -25,15 +23,11 @@
2523
* limitations under the License.
2624
*/
2725

28-
public class Dht22Driver implements BaseSensor {
26+
public class Dht22Driver implements BaseSensor, AutoCloseable {
2927
private static final String TAG = "Dht22Driver";
3028
private final Arduino arduino;
3129

3230
private UartDevice mDevice;
33-
private StringBuffer message = new StringBuffer();
34-
private boolean receiving = false;
35-
private OnMessageCompleteListener messageCompleteListener;
36-
private TextView field;
3731

3832
public Dht22Driver(Arduino arduino){
3933
this.arduino = arduino;
@@ -50,97 +44,75 @@ public void startup() {
5044
mDevice.setStopBits(arduino.getStopBits());
5145
mDevice.setBaudrate(arduino.getBaudRate());
5246
} catch (IOException e){
47+
try {
48+
close();
49+
} catch (Exception e1) {
50+
e1.printStackTrace();
51+
}
5352
throw new IllegalStateException("Sensor can't start", e);
5453
}
54+
}
55+
56+
public String getTemperature() {
57+
String mode = "T";
58+
String response = "";
59+
byte[] buffer = new byte[10];
5560

5661
try {
57-
mDevice.registerUartDeviceCallback(mUartCallback);
62+
response = fillBuffer(buffer, mode);
5863
} catch (IOException e) {
59-
throw new IllegalStateException("Sensor can't register callback", e);
64+
65+
e.printStackTrace();
6066
}
67+
return response;
6168
}
6269

63-
public void getTemperature(TextView field, OnMessageCompleteListener listener) throws IOException {
64-
String mode = "T";
65-
mDevice.write(mode.getBytes(), mode.length());
66-
this.messageCompleteListener = listener;
67-
this.field = field;
68-
}
6970

70-
public void getHumidity(TextView field, OnMessageCompleteListener listener) throws IOException {
71+
public String getHumidity() {
7172
String mode = "H";
72-
mDevice.write(mode.getBytes(), mode.length());
73-
this.messageCompleteListener = listener;
74-
this.field = field;
75-
}
73+
String response = "";
74+
byte[] buffer = new byte[10];
7675

77-
@Override
78-
public void onStart() {
7976
try {
80-
mDevice.registerUartDeviceCallback(mUartCallback);
77+
response = fillBuffer(buffer, mode);
8178
} catch (IOException e) {
8279
e.printStackTrace();
8380
}
81+
return response;
8482
}
8583

86-
@Override
87-
public void onStop() {
88-
mDevice.unregisterUartDeviceCallback(mUartCallback);
84+
private String fillBuffer(byte[] buffer, String mode) throws IOException {
85+
mDevice.write(mode.getBytes(), mode.length());
86+
try {
87+
Thread.sleep(500);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
}
91+
92+
mDevice.read(buffer, buffer.length);
93+
return new String(buffer, "UTF-8");
8994
}
9095

9196
@Override
92-
public void shutdown() {
93-
if (mDevice != null) {
97+
public void close() throws Exception {
98+
if(mDevice != null){
9499
try {
95100
mDevice.close();
101+
} finally {
96102
mDevice = null;
97-
} catch (IOException e) {
98-
Log.w(TAG, "Unable to close UART device", e);
99103
}
100104
}
101105
}
102106

103-
private UartDeviceCallback mUartCallback = new UartDeviceCallback() {
104-
@Override
105-
public boolean onUartDeviceDataAvailable(UartDevice uart) {
106-
// Read available data from the UART device
107+
@Override
108+
public void shutdown() {
109+
if (mDevice != null) {
107110
try {
108-
readUartBuffer(uart);
111+
mDevice.close();
112+
mDevice = null;
109113
} catch (IOException e) {
110-
Log.w(TAG, "Unable to access UART device", e);
114+
Log.w(TAG, "Unable to close UART device", e);
111115
}
112-
113-
// Continue listening for more interrupts
114-
return true;
115-
}
116-
117-
@Override
118-
public void onUartDeviceError(UartDevice uart, int error) {
119-
Log.w(TAG, uart + ": Error event " + error);
120-
}
121-
};
122-
123-
private void readUartBuffer(UartDevice uart) throws IOException {
124-
byte[] buffer = new byte[1];
125-
126-
uart.read(buffer, buffer.length);
127-
String character = new String(buffer, "UTF-8");
128-
129-
if(character.compareTo("#") == 0){
130-
receiving = false;
131-
this.messageCompleteListener.onMessageComplete(message.toString(), this.field);
132-
Log.d(TAG, "Complete Message: " + message);
133-
134-
// clear buffer
135-
message.delete(0, message.length());
136-
}
137-
138-
if(receiving){
139-
message.append(character);
140-
}
141-
142-
if(character.compareTo("$") == 0){
143-
receiving = true;
144116
}
145117
}
146118
}

arduwrap/src/main/java/rocks/androidthings/arduwrap/OnMessageCompleteListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
*/
2020

2121
public interface OnMessageCompleteListener {
22-
void onMessageComplete(String message, TextView field);
22+
void onMessageComplete(String message);
2323
}

0 commit comments

Comments
 (0)