2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.micropeak;
23 import org.altusmetrum.altoslib_14.*;
24 import org.altusmetrum.altosuilib_14.*;
26 public class MicroUSB extends altos_device implements AltosDevice {
28 static boolean initialized = false;
29 static boolean loaded_library = false;
31 public static boolean load_library() {
34 System.loadLibrary("altos");
35 libaltos.altos_init();
36 loaded_library = true;
37 } catch (UnsatisfiedLinkError e) {
39 System.loadLibrary("altos64");
40 libaltos.altos_init();
41 loaded_library = true;
42 } catch (UnsatisfiedLinkError e2) {
43 loaded_library = false;
48 return loaded_library;
51 public String toString() {
52 String name = getName();
54 name = "Altus Metrum";
55 return String.format("%-24.24s %s",
59 public String toShortString() {
60 String name = getName();
62 name = "Altus Metrum";
63 return String.format("%s %s",
68 public String getErrorString() {
69 altos_error error = new altos_error();
71 libaltos.altos_get_last_error(error);
72 return String.format("%s (%d)", error.getString(), error.getCode());
75 public SWIGTYPE_p_altos_file open() {
76 return libaltos.altos_open(this);
79 private boolean isFTDI() {
80 int vid = getVendor();
81 int pid = getProduct();
82 if (vid == 0x0403 && pid == 0x6015)
87 private boolean isMicro() {
88 int vid = getVendor();
89 int pid = getProduct();
90 if (vid == AltosLib.vendor_altusmetrum &&
91 pid == AltosLib.product_mpusb)
96 public boolean matchProduct(int product) {
97 return isFTDI() || isMicro();
100 public int hashCode() {
101 return getVendor() ^ getProduct() ^ getSerial() ^ getPath().hashCode();
104 public boolean equals(Object o) {
108 if (!(o instanceof MicroUSB))
111 MicroUSB other = (MicroUSB) o;
113 return getVendor() == other.getVendor() &&
114 getProduct() == other.getProduct() &&
115 getSerial() == other.getSerial() &&
116 getPath().equals(other.getPath());
119 static java.util.List<MicroUSB> list() {
123 ArrayList<MicroUSB> device_list = new ArrayList<MicroUSB>();
125 SWIGTYPE_p_altos_list list;
127 list = libaltos.altos_ftdi_list_start();
131 MicroUSB device = new MicroUSB();
132 if (libaltos.altos_list_next(list, device) == 0)
135 device_list.add(device);
137 libaltos.altos_list_finish(list);
140 list = libaltos.altos_list_start();
144 MicroUSB device = new MicroUSB();
145 if (libaltos.altos_list_next(list, device) == 0)
147 if (device.isMicro())
148 device_list.add(device);
150 libaltos.altos_list_finish(list);