Guest User

Untitled

a guest
Apr 3rd, 2013
259
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * UsbControllerActivity.java
  3.  * This file is part of UsbController
  4.  *
  5.  * Copyright (C) 2012 - Manuel Di Cerbo
  6.  *
  7.  * UsbController is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * UsbController is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with UsbController. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package com.example.pokuspost;
  21.  
  22.  
  23. import android.app.Activity;
  24. import android.os.Bundle;
  25. import android.util.Log;
  26. import android.view.View;
  27. import android.widget.Button;
  28. import android.widget.SeekBar;
  29.  
  30.  
  31. /**
  32.  * (c) Neuxs-Computing GmbH Switzerland
  33.  * @author Manuel Di Cerbo, 02.02.2012
  34.  *
  35.  */
  36. public class MainActivity extends Activity {
  37.     /** Called when the activity is first created. */
  38.     private static final int VID = 0x2341;
  39.     private static final int PID = 0x0001;//I believe it is 0x0000 for the Arduino Megas
  40.     private static UsbController sUsbController;
  41.     private static int light = 0;
  42.  
  43.     @Override
  44.     public void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_main);
  47.         if(sUsbController == null){
  48.             sUsbController = new UsbController(this, mConnectionHandler, VID, PID);
  49.         }
  50.         ((Button)findViewById(R.id.light)).setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 if(light == 0){
  54.                     light = 1;
  55.                     if(sUsbController != null){
  56.                         sUsbController.send((byte)(light&0xFF));
  57.                     }
  58.                 }
  59.                 else{
  60.                     light = 0;
  61.                     if(sUsbController != null){
  62.                         sUsbController.send((byte)(light&0xFF));
  63.                     }
  64.                 }
  65.             }
  66.         });
  67.        
  68.         ((Button)findViewById(R.id.start)).setOnClickListener(new View.OnClickListener() {
  69.             @Override
  70.             public void onClick(View v) {
  71.                 Log.d("try", "start");
  72.                 if(sUsbController == null)
  73.                     sUsbController = new UsbController(MainActivity.this, mConnectionHandler, VID, PID);
  74.                 else{
  75.                     sUsbController.stop();
  76.                     sUsbController = new UsbController(MainActivity.this, mConnectionHandler, VID, PID);
  77.                 }
  78.             }
  79.         });
  80.        
  81.     }
  82.  
  83.     private final IUsbConnectionHandler mConnectionHandler = new IUsbConnectionHandler() {
  84.         @Override
  85.         public void onUsbStopped() {
  86.             Log.d("try","Usb stopped!");
  87.         }
  88.        
  89.         @Override
  90.         public void onErrorLooperRunningAlready() {
  91.             Log.d("try","Looper already running!");
  92.         }
  93.        
  94.         @Override
  95.         public void onDeviceNotFound() {
  96.             Log.d("Try", "device not found");
  97.             if(sUsbController != null){
  98.                 sUsbController.stop();
  99.                 sUsbController = null;
  100.             }
  101.         }
  102.     };
  103. }
RAW Paste Data