Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. package com.example.ftdi;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.hardware.usb.UsbManager;
  7. import android.media.AudioManager;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.util.Log;
  11. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  12. import com.google.android.material.snackbar.Snackbar;
  13. import androidx.appcompat.app.AppCompatActivity;
  14. import androidx.appcompat.widget.Toolbar;
  15. import android.view.View;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.TextView;
  21. import android.widget.ScrollView;
  22.  
  23. import com.ftdi.j2xx.FT_Device;
  24. import com.ftdi.j2xx.D2xxManager;
  25.  
  26. import android.app.Activity;
  27. import android.content.BroadcastReceiver;
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.content.IntentFilter;
  31. import android.hardware.usb.UsbManager;
  32. import android.os.Bundle;
  33. import android.os.Handler;
  34. import android.util.Log;
  35. import android.view.View;
  36. import android.text.method.ScrollingMovementMethod;
  37. import in.excogitation.zentone.*;
  38. import in.excogitation.zentone.library.ToneStoppedListener;
  39. import in.excogitation.zentone.library.ZenTone;
  40.  
  41. import java.io.IOException;
  42. import java.io.OutputStreamWriter;
  43. import java.lang.StringBuilder;
  44. import java.lang.Math.*;
  45.  
  46. //Test change XDD
  47.  
  48. public class MainActivity extends Activity {
  49. private final static String TAG = "FPGA_FIFO Activity";
  50.  
  51. private static D2xxManager ftD2xx = null;
  52. private FT_Device ftDev;
  53.  
  54. static final int READBUF_SIZE = 256;
  55. byte[] rbuf = new byte[READBUF_SIZE];
  56. char[] rchar = new char[READBUF_SIZE];
  57. int mReadSize=0;
  58.  
  59. TextView tvRead;
  60. EditText etWrite;
  61. Button btOpen;
  62. Button btWrite;
  63. Button btClose;
  64. EditText sampleData;
  65.  
  66. boolean mThreadIsStopped = true;
  67. Handler mHandler = new Handler();
  68. Thread mThread;
  69.  
  70. String buf = "";
  71.  
  72.  
  73. @Override
  74. protected void onCreate(Bundle savedInstanceState) {
  75.  
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.activity_main);
  78.  
  79. TextView textView = (TextView) findViewById(R.id.tvRead);
  80. textView.setMovementMethod(new ScrollingMovementMethod());
  81.  
  82. tvRead = (TextView) findViewById(R.id.tvRead);
  83. etWrite = (EditText) findViewById(R.id.etWrite);
  84.  
  85. btOpen = (Button) findViewById(R.id.btOpen);
  86. btWrite = (Button) findViewById(R.id.btWrite);
  87. btClose = (Button) findViewById(R.id.btClose);
  88.  
  89. sampleData = (EditText) findViewById(R.id.sampleData);
  90.  
  91. updateView(false);
  92.  
  93. try {
  94. ftD2xx = D2xxManager.getInstance(this);
  95. } catch (D2xxManager.D2xxException ex) {
  96. Log.e(TAG,ex.toString());
  97. }
  98.  
  99. IntentFilter filter = new IntentFilter();
  100. filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
  101. filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
  102. registerReceiver(mUsbReceiver, filter);
  103.  
  104. }
  105.  
  106.  
  107. //Sample button for tone generation
  108.  
  109. public void onClickAAA(View v){
  110. ZenTone.getInstance().generate(660, 1, 1.0f, new ToneStoppedListener() {
  111. @Override
  112. public void onToneStopped() {
  113. Boolean isPlaying = false;
  114. }
  115. });
  116. }
  117.  
  118. public void sendSample(View v){
  119. feedback(sampleData.getText().toString().split(","));
  120. //tvRead.append(sampleData.getText().toString());
  121. }
  122.  
  123. public void onClickOpen(View v) {
  124. openDevice();
  125. }
  126.  
  127.  
  128. public void onClickWrite(View v) {
  129. if(ftDev == null) {
  130. return;
  131. }
  132.  
  133. synchronized (ftDev) {
  134. if(ftDev.isOpen() == false) {
  135. Log.e(TAG, "onClickWrite : Device is not open");
  136. return;
  137. }
  138.  
  139. ftDev.setLatencyTimer((byte)16);
  140.  
  141. String writeString = "s";
  142. byte[] writeByte = writeString.getBytes();
  143. ftDev.write(writeByte, writeString.length());
  144. }
  145. }
  146.  
  147. public void onClickClose(View v) {
  148. closeDevice();
  149. }
  150.  
  151. @Override
  152. public void onDestroy() {
  153. super.onDestroy();
  154. mThreadIsStopped = true;
  155. unregisterReceiver(mUsbReceiver);
  156. }
  157.  
  158. /* @Override
  159. public boolean onCreateOptionsMenu(Menu menu) {
  160. // Inflate the menu; this adds items to the action bar if it is present.
  161. getMenuInflater().inflate(R.menu.main, menu);
  162. return true;
  163. }
  164. */
  165.  
  166. private void openDevice() {
  167. if(ftDev != null) {
  168. if(ftDev.isOpen()) {
  169. if(mThreadIsStopped) {
  170. updateView(true);
  171. SetConfig(57600, (byte)8, (byte)1, (byte)0, (byte)0);
  172. ftDev.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));
  173. ftDev.restartInTask();
  174. new Thread(mLoop).start();
  175. }
  176. return;
  177. }
  178. }
  179.  
  180. int devCount = 0;
  181. devCount = ftD2xx.createDeviceInfoList(this);
  182.  
  183. Log.d(TAG, "Device number : "+ Integer.toString(devCount));
  184.  
  185. D2xxManager.FtDeviceInfoListNode[] deviceList = new D2xxManager.FtDeviceInfoListNode[devCount];
  186. ftD2xx.getDeviceInfoList(devCount, deviceList);
  187.  
  188. if(devCount <= 0) {
  189. return;
  190. }
  191.  
  192. if(ftDev == null) {
  193. ftDev = ftD2xx.openByIndex(this, 0);
  194. } else {
  195. synchronized (ftDev) {
  196. ftDev = ftD2xx.openByIndex(this, 0);
  197. }
  198. }
  199.  
  200. if(ftDev.isOpen()) {
  201. if(mThreadIsStopped) {
  202. updateView(true);
  203. SetConfig(57600, (byte)8, (byte)1, (byte)0, (byte)0);
  204. ftDev.purge((byte) (D2xxManager.FT_PURGE_TX | D2xxManager.FT_PURGE_RX));
  205. ftDev.restartInTask();
  206. new Thread(mLoop).start();
  207. }
  208. }
  209. }
  210.  
  211. private Runnable mLoop = new Runnable() {
  212. @Override
  213. public void run() {
  214. int i;
  215. int readSize;
  216. mThreadIsStopped = false;
  217. while(true) {
  218. if(mThreadIsStopped) {
  219. break;
  220. }
  221.  
  222. /* try {
  223. Thread.sleep(50);
  224. } catch (InterruptedException e) {
  225. }
  226. */
  227. synchronized (ftDev) {
  228. readSize = ftDev.getQueueStatus();
  229. if(readSize>0) {
  230. mReadSize = readSize;
  231. if(mReadSize > READBUF_SIZE) {
  232. mReadSize = READBUF_SIZE;
  233. }
  234. ftDev.read(rbuf,mReadSize);
  235.  
  236. // cannot use System.arraycopy
  237. for(i=0; i<mReadSize; i++) {
  238. rchar[i] = (char)rbuf[i];
  239. }
  240. mHandler.post(new Runnable() {
  241. @Override
  242. public void run() {
  243.  
  244. String x = String.copyValueOf(rchar,0,mReadSize);
  245. String[] y = x.split(",",5);
  246. writeToFile(x, getApplicationContext());
  247. //tvRead.append(x); // Here's the line that updates the data stream
  248. feedback(y);
  249. }
  250. });
  251.  
  252. } // end of if(readSize>0)
  253. } // end of synchronized
  254. }
  255. }
  256. };
  257.  
  258. public void feedback(String[] input) {
  259.  
  260. int[] values = {0,0,0,0,0};
  261. for(int i = 0; i < 5; i++){
  262. try{
  263. double tmp = Integer.parseInt(input[i]);
  264. values[i] = (int) tmp;
  265. tvRead.append(tmp+"\n");
  266. }
  267. catch (Exception NumberFormatException){}
  268. } // Data values are now split CSV style
  269.  
  270. //if(values[0] <= 1000){
  271. ZenTone.getInstance().generate(values[0], 1, 1.0f, new ToneStoppedListener() {
  272. @Override
  273. public void onToneStopped() {
  274. Boolean isPlaying = false;
  275. }
  276. });
  277. //}
  278. }
  279. private void writeToFile(String data,Context context) {
  280. try {
  281. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("Data.txt", Context.MODE_PRIVATE));
  282. outputStreamWriter.write(data);
  283. outputStreamWriter.close();
  284. tvRead.append("DickBerg\n");
  285. }
  286. catch (IOException e) {
  287. tvRead.append("Exception" + "File write failed: " + e.toString());
  288. }
  289. }
  290.  
  291. private void closeDevice() {
  292. mThreadIsStopped = true;
  293. updateView(false);
  294. if(ftDev != null) {
  295. ftDev.close();
  296. }
  297. }
  298.  
  299. private void updateView(boolean on) {
  300. if(on) {
  301. btOpen.setEnabled(false);
  302. btWrite.setEnabled(true);
  303. btClose.setEnabled(true);
  304. } else {
  305. btOpen.setEnabled(true);
  306. btWrite.setEnabled(false);
  307. btClose.setEnabled(false);
  308. }
  309. }
  310.  
  311. public void SetConfig(int baud, byte dataBits, byte stopBits, byte parity, byte flowControl) {
  312. if (ftDev.isOpen() == false) {
  313. Log.e(TAG, "SetConfig: device not open");
  314. return;
  315. }
  316.  
  317. // configure our port
  318. // reset to UART mode for 232 devices
  319. ftDev.setBitMode((byte) 0, D2xxManager.FT_BITMODE_RESET);
  320.  
  321. ftDev.setBaudRate(baud);
  322.  
  323. switch (dataBits) {
  324. case 7:
  325. dataBits = D2xxManager.FT_DATA_BITS_7;
  326. break;
  327. case 8:
  328. dataBits = D2xxManager.FT_DATA_BITS_8;
  329. break;
  330. default:
  331. dataBits = D2xxManager.FT_DATA_BITS_8;
  332. break;
  333. }
  334.  
  335. switch (stopBits) {
  336. case 1:
  337. stopBits = D2xxManager.FT_STOP_BITS_1;
  338. break;
  339. case 2:
  340. stopBits = D2xxManager.FT_STOP_BITS_2;
  341. break;
  342. default:
  343. stopBits = D2xxManager.FT_STOP_BITS_1;
  344. break;
  345. }
  346.  
  347. switch (parity) {
  348. case 0:
  349. parity = D2xxManager.FT_PARITY_NONE;
  350. break;
  351. case 1:
  352. parity = D2xxManager.FT_PARITY_ODD;
  353. break;
  354. case 2:
  355. parity = D2xxManager.FT_PARITY_EVEN;
  356. break;
  357. case 3:
  358. parity = D2xxManager.FT_PARITY_MARK;
  359. break;
  360. case 4:
  361. parity = D2xxManager.FT_PARITY_SPACE;
  362. break;
  363. default:
  364. parity = D2xxManager.FT_PARITY_NONE;
  365. break;
  366. }
  367.  
  368. ftDev.setDataCharacteristics(dataBits, stopBits, parity);
  369.  
  370. short flowCtrlSetting;
  371. switch (flowControl) {
  372. case 0:
  373. flowCtrlSetting = D2xxManager.FT_FLOW_NONE;
  374. break;
  375. case 1:
  376. flowCtrlSetting = D2xxManager.FT_FLOW_RTS_CTS;
  377. break;
  378. case 2:
  379. flowCtrlSetting = D2xxManager.FT_FLOW_DTR_DSR;
  380. break;
  381. case 3:
  382. flowCtrlSetting = D2xxManager.FT_FLOW_XON_XOFF;
  383. break;
  384. default:
  385. flowCtrlSetting = D2xxManager.FT_FLOW_NONE;
  386. break;
  387. }
  388.  
  389. // TODO : flow ctrl: XOFF/XOM
  390. // TODO : flow ctrl: XOFF/XOM
  391. ftDev.setFlowControl(flowCtrlSetting, (byte) 0x0b, (byte) 0x0d);
  392. }
  393.  
  394. // done when ACTION_USB_DEVICE_ATTACHED
  395. @Override
  396. protected void onNewIntent(Intent intent) {
  397. openDevice();
  398. };
  399.  
  400. BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  401. public void onReceive(Context context, Intent intent) {
  402. String action = intent.getAction();
  403. if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  404. // never come here(when attached, go to onNewIntent)
  405. openDevice();
  406. } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
  407. closeDevice();
  408. }
  409. }
  410. };
  411.  
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement