USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
Main.java
Go to the documentation of this file.
1 
9 /* This file is part of USBUART Library. http://usbuart.info/
10  *
11  * Copyright © 2016 Eugene Hutorny <eugene@hutorny.in.ua>
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 package info.usbuart.testapp;
29 
30 import android.app.Activity;
31 import android.content.*;
32 import android.hardware.usb.UsbDevice;
33 import android.os.Bundle;
34 import android.os.IBinder;
35 import android.system.ErrnoException;
36 import android.util.Log;
37 import android.view.Menu;
38 import android.view.MenuInflater;
39 import android.view.MenuItem;
40 import android.widget.TextView;
42 import info.usbuart.api.UsbUartContext;
45 import info.usbuart.service.UsbUartService;
46 
47 import java.io.*;
48 
49 public class Main extends Activity implements ServiceConnection, IService.BusListener {
50 
51  @Override
52  public void onCreate(Bundle savedInstanceState) {
53  super.onCreate(savedInstanceState);
54  setContentView(R.layout.main);
55  textLog = (TextView) findViewById(R.id.textLog);
56  new Thread(logUpdater).start();
57  startService();
58  }
59 
60  private void startService() {
61  Intent intent = new Intent(getApplicationContext(), UsbUartService.class);
62  startService(intent);
63  bindService(intent, this, BIND_ABOVE_CLIENT);
64  }
65 
66  @Override
67  public void onServiceConnected(ComponentName name, IBinder svc) {
68  binder = svc;
69  service = (IService) binder.queryLocalInterface(IService.class.getCanonicalName());
70  if( service != null ) {
71  service.addBusListener(this);
72  service.setOptions(new Options(EIA_TIA_232_Info._115200_8N1n(),Options.FifoNaming.VIDPID, true, true));
73  }
74  }
75 
76  @Override
77  public void onServiceDisconnected(ComponentName name) {
78  if( service != null ) service.removeBusListener(this);
79  binder = null;
80  service = null;
81  }
82 
83  @Override
84  protected void onDestroy() {
85  if( service != null ) service.removeBusListener(this);
86  if( runner != null ) runner.interrupt();
87  runner = null;
88  if( binder != null ) unbindService(this);
89  super.onDestroy();
90  }
91 
92  @Override
93  public boolean onCreateOptionsMenu(Menu menu) {
94  super.onCreateOptionsMenu(menu);
95  MenuInflater inflater = getMenuInflater();
96  inflater.inflate(R.menu.main, menu);
97  return true;
98  }
99 
100  public void onClickMenuTest(final MenuItem item) {
101  if (service == null) {
102  print("USBUART service is not available");
103  return;
104  }
105  if (device == null) {
106  print("please attach a USBUART device");
107  return;
108  }
109  try {
110  test();
111  } catch (Exception e) {
112  print(e.getMessage());
113  }
114  }
115 
116  private void test() throws ErrnoException, UsbUartContext.Error, IOException {
117  String[] fifo = service.getFifo(device.getDeviceName(), 0, EIA_TIA_232_Info._115200_8N1n());
118  FileInputStream in = new FileInputStream(fifo[0]);
119  final Pump pump = new Pump(in);
120  pump.start();
121  final FileOutputStream o = new FileOutputStream(fifo[1]);
122  final PrintStream out = new PrintStream(o);
123  new Thread(new Runnable() {
124  @Override
125  public void run() {
126  for(int i = 0; i < 1000; ++i) {
127  if(i % 10 == 0 ) out.println();
128  out.printf("%5d",i);
129  }
130  try { Thread.sleep(7000); } catch (InterruptedException ignored) {}
131  out.close();
132  try { o.close(); } catch (IOException ignore) {}
133  pump.stop();
134  }
135  }).start();
136 
137  }
138 
139  @Override
140  public boolean attached(UsbDevice device) {
141  if( ! device.equals(this.device) )
142  print("\n+ " + UsbUartService.formatDevice(device));
143  this.device = device;
144  return false;
145  }
146 
147  @Override
148  public void detached(UsbDevice device) {
149  if( device.equals(this.device) ) {
150  this.device = null;
151  print("\n- " + UsbUartService.formatDevice(device));
152  }
153  }
154 
155  private class Pump implements Runnable {
156  final BufferedReader reader;
157  boolean terminated = false;
158 
159  private Pump(InputStream stream) throws IOException {
160  reader = new BufferedReader(new InputStreamReader(stream));
161  }
162  private void stop() {
163  if( terminated ) return;
164  terminated = true;
165  try {
166  reader.close();
167  synchronized (reader) { reader.notify(); }
168  } catch (IOException e) {
169  Log.w("PUMP", e.getMessage());
170  }
171  }
172 
173  @Override
174  public void run() {
175  Log.d("pump", "started");
176  while( ! terminated && ! Thread.currentThread().isInterrupted() ) {
177  try {
178  boolean done = false;
179  while ( reader.ready() ) {
180  int chr = reader.read();
181  if( chr < 0 ) {
182  terminated = true;
183  break;
184  } else {
185  synchronized (log) {
186  log.append((char) chr);
187  //TODO terminate test when log grows too large
188  }
189  done = true;
190  }
191  }
192  if( done ) refreshLog();
193  synchronized (reader) { reader.wait(100); }
194  } catch (IOException ignored) {
195  } catch (InterruptedException e) {
196  terminated = true;
197  }
198  }
199  Log.d("pump", "terminated");
200  }
201  void start() {
202  new Thread(this).start();
203  }
204  }
205 
206  private void refreshLog() {
207  if( textLog.length() == log.length() ) return;
208  if( getMainLooper().getThread() != Thread.currentThread() ) {
209  textLog.post(refresher);
210  return;
211  }
212  textLog.setText(log);
213  textLog.postInvalidate();
214  textLog.post(scroller);
215  }
216 
217  private void print(String s) {
218  log.append(s);
219  log.append('\n');
220  refreshLog();
221  }
222 
223 
224  private final Runnable refresher = new Runnable() {
225  @Override
226  public void run() {
227  refreshLog();
228  }
229  };
230 
231  private final Runnable logUpdater = new Runnable() {
232  @Override
233  public void run() {
234  try {
235  while( ! Thread.currentThread().isInterrupted() ) {
236  Thread.sleep(100);
237  textLog.post(refresher);
238  }
239  } catch (InterruptedException ignored) {}
240  }
241  };
242 
243  private final Runnable scroller = new Runnable() {
244  @Override
245  public void run() {
246  final int scrollAmount = textLog.getLayout().getLineTop(textLog.getLineCount()) - textLog.getHeight();
247  // if there is no need to scroll, scrollAmount will be <=0
248  if (scrollAmount > 0)
249  textLog.scrollTo(0, scrollAmount);
250  }
251  };
252 
253  private TextView textLog;
254  private final StringBuilder log = new StringBuilder();
255  private UsbDevice device = null;
256  private Thread runner;
257  private IBinder binder;
258  private IService service;
259 }
Configuration options for USBUART service.
Definition: Options.java:33