OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_14/usb_fpga_2_14/trunk

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [java/] [ztex/] [ZtexEventHandler.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   Java host software API of ZTEX SDK
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/*
27
    Event handler for asynchronous mode.
28
*/
29
package ztex;
30
 
31
import java.io.*;
32
import java.util.*;
33
 
34
import org.usb4java.*;
35
 
36
/**
37
  * This class defines an event handler thread which can be used in asynchronous mode.
38
  */
39
 
40
// *****************************************************************************
41
// ******* ZtexEventHandler ****************************************************
42
// *****************************************************************************
43
public class ZtexEventHandler extends Thread {
44
    private volatile boolean terminate;
45
    private volatile boolean isAlive;
46
    private Context context;
47
 
48
/**
49
  * Creates the event handler for a given USB context.
50
  * @param p_context The USB context
51
  */
52
    public ZtexEventHandler ( Context p_context ) {
53
        super ();
54
        context = p_context;
55
        isAlive = false;
56
    }
57
 
58
/**
59
  * Creates the event handler for a given ZTEX device.
60
  * @param ztex The ZTEX device
61
  */
62
    public ZtexEventHandler ( Ztex1 ztex ) {
63
        super ();
64
        context = ztex.dev().context().context();
65
        isAlive = false;
66
    }
67
 
68
/**
69
  * Stops the event handler. This call waits up to 1.5s
70
  * @return true if the event handler terminated correctly.
71
  */
72
    public boolean terminate() {
73
        terminate = true;
74
        for (int i=0; isAlive && i<=1500; i+=20 ) {
75
            try { sleep(20); } catch ( InterruptedException e) { }
76
        }
77
        return !isAlive;
78
    }
79
 
80
/**
81
  * The thread body.
82
  */
83
    public void run() {
84
        isAlive = true;
85
        terminate = false;
86
        while ( ! terminate ) {
87
            int result = LibUsb.handleEventsTimeoutCompleted (context, 500, null);
88
            if ( (result!=LibUsb.SUCCESS) ) System.err.println("Event handler: "+LibUsb.strError(result));
89
        }
90
        isAlive = false;
91
    }
92
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.