This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
OneWireLights.java
78 lines (64 loc) · 2.04 KB
/
OneWireLights.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;
class OneWireLights {
/**
* 1-Wire bus adapter
*/
private DSPortAdapter adapter = null;
/**
* 1-Wire IDs of the switches
*/
protected String[] lights = ConfigMgr.getInstance().getLights();
private static OneWireLights instance = null;
public synchronized static OneWireLights getInstance() {
if( instance == null ) {
instance = new OneWireLights();
}
return instance;
}
private OneWireLights() {
try {
this.adapter = OneWireAccessProvider.getDefaultAdapter();
}catch( Exception e ) {
System.out.println( "Oh Snap. Can't get adapter." );
}
}
/**
* Get the OneWireContainer associated with the given 1-Wire id
*
* @param id 1-Wire Device ID to fetch
*
* @return OneWireContainer corresponding to the 1-wire id
*/
protected OneWireContainer05 getSwitch( String id ) {
System.out.println( "Getting Light: " + id );
OneWireContainer owc = this.adapter.getDeviceContainer( id );
if( owc instanceof OneWireContainer05 ) {
return (OneWireContainer05) owc;
} else {
return null;
}
}
/**
* Set the status light of a slot
*
* @param slot Slot number to change light for
* @param empty Empty status to set light to (true = on)
*/
public void slotStatus( int slot, boolean empty) {
//if we don't have any lights loaded in, don't do anything.
if( lights == null ) {
return;
}
OneWireContainer05 owc = getSwitch( lights[slot] );
try {
byte[] state = owc.readDevice();
owc.setLatchState( 0, empty, false, state );
owc.writeDevice( state );
} catch ( Exception e ) {
System.out.println( "Error Setting Slot Status Light" );
e.printStackTrace();
}
}
}