]> git.gag.com Git - fw/altos/blob - micropeak/MicroUSB.java
altos/lpc: Add support for eeprom on 2x/3x chips
[fw/altos] / micropeak / MicroUSB.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.util.*;
22 import libaltosJNI.*;
23 import org.altusmetrum.altoslib_14.*;
24 import org.altusmetrum.altosuilib_14.*;
25
26 public class MicroUSB extends altos_device implements AltosDevice {
27
28         static boolean  initialized = false;
29         static boolean  loaded_library = false;
30
31         public static boolean load_library() {
32                 if (!initialized) {
33                         try {
34                                 System.loadLibrary("altos");
35                                 libaltos.altos_init();
36                                 loaded_library = true;
37                         } catch (UnsatisfiedLinkError e) {
38                                 try {
39                                         System.loadLibrary("altos64");
40                                         libaltos.altos_init();
41                                         loaded_library = true;
42                                 } catch (UnsatisfiedLinkError e2) {
43                                         loaded_library = false;
44                                 }
45                         }
46                         initialized = true;
47                 }
48                 return loaded_library;
49         }
50
51         public String toString() {
52                 String  name = getName();
53                 if (name == null)
54                         name = "Altus Metrum";
55                 return String.format("%-24.24s %s",
56                                      name, getPath());
57         }
58
59         public String toShortString() {
60                 String  name = getName();
61                 if (name == null)
62                         name = "Altus Metrum";
63                 return String.format("%s %s",
64                                      name, getPath());
65
66         }
67
68         public String getErrorString() {
69                 altos_error     error = new altos_error();
70
71                 libaltos.altos_get_last_error(error);
72                 return String.format("%s (%d)", error.getString(), error.getCode());
73         }
74
75         public SWIGTYPE_p_altos_file open() {
76                 return libaltos.altos_open(this);
77         }
78
79         private boolean isFTDI() {
80                 int vid = getVendor();
81                 int pid = getProduct();
82                 if (vid == 0x0403 && pid == 0x6015)
83                         return true;
84                 return false;
85         }
86
87         private boolean isMicro() {
88                 int vid = getVendor();
89                 int pid = getProduct();
90                 if (vid == AltosLib.vendor_altusmetrum &&
91                     pid == AltosLib.product_mpusb)
92                         return true;
93                 return false;
94         }
95
96         public boolean matchProduct(int product) {
97                 return isFTDI() || isMicro();
98         }
99
100         public int hashCode() {
101                 return getVendor() ^ getProduct() ^ getSerial() ^ getPath().hashCode();
102         }
103
104         public boolean equals(Object o) {
105                 if (o == null)
106                         return false;
107
108                 if (!(o instanceof MicroUSB))
109                         return false;
110
111                 MicroUSB other = (MicroUSB) o;
112
113                 return getVendor() == other.getVendor() &&
114                         getProduct() == other.getProduct() &&
115                         getSerial() == other.getSerial() &&
116                         getPath().equals(other.getPath());
117         }
118
119         static java.util.List<MicroUSB> list() {
120                 if (!load_library())
121                         return null;
122
123                 ArrayList<MicroUSB> device_list = new ArrayList<MicroUSB>();
124
125                 SWIGTYPE_p_altos_list list;
126
127                 list = libaltos.altos_ftdi_list_start();
128
129                 if (list != null) {
130                         for (;;) {
131                                 MicroUSB device = new MicroUSB();
132                                 if (libaltos.altos_list_next(list, device) == 0)
133                                         break;
134                                 if (device.isFTDI())
135                                         device_list.add(device);
136                         }
137                         libaltos.altos_list_finish(list);
138                 }
139
140                 list = libaltos.altos_list_start();
141
142                 if (list != null) {
143                         for (;;) {
144                                 MicroUSB device = new MicroUSB();
145                                 if (libaltos.altos_list_next(list, device) == 0)
146                                         break;
147                                 if (device.isMicro())
148                                         device_list.add(device);
149                         }
150                         libaltos.altos_list_finish(list);
151                 }
152
153                 return device_list;
154         }
155 }