Skip to content

Commit 2196bca

Browse files
author
letherman255
committed
raspberry i2c works
added config israspberry added i2c to arduino
1 parent 77e9b7f commit 2196bca

29 files changed

+170
-5
lines changed

Ressources/testi2c/arduino/i2cslave/i2cslave.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
byte b = 0;
33
void setup()
44
{
5-
Wire.begin(4); // join i2c bus with address #4
5+
Wire.begin(0); // join i2c bus with address #4
66
Wire.onReceive(receiveEvent); // register event
77
Serial.begin(9600); // start serial for output
88
Serial.println("hello!");

pro/WebContent/WEB-INF/lib/junit.jar

239 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
176 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
214 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
106 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18.6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

pro/src/fr/mcnanotech/beans/SystemParam.java

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class SystemParam {
55
private String date;
66
private String admin1;
77
private String admin2;
8+
private String raspberry;
89

910
public int getDailyCredit() {
1011
return dailyCredit;
@@ -38,4 +39,14 @@ public void setDate(String date) {
3839
this.date = date;
3940
}
4041

42+
public String getRaspberry()
43+
{
44+
return raspberry;
45+
}
46+
47+
public void setRaspberry(String raspberry)
48+
{
49+
this.raspberry = raspberry;
50+
}
51+
4152
}

pro/src/fr/mcnanotech/configloader/SettingsLoader.java

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class SettingsLoader
1919
private static final String ADMIN1 = "admin1";
2020
private static final String ADMIN2 = "admin2";
2121
private static final String DATE = "date";
22+
private static final String RASPBERRY = "raspberry";
23+
2224

2325
@SuppressWarnings("resource")
2426
public SystemParam loadParams(SystemParam sp)
@@ -60,6 +62,7 @@ public SystemParam loadParams(SystemParam sp)
6062
sp.setDate(new String(props.getProperty(DATE, today.toString("dd-MMM-yyyy"))));
6163
sp.setAdmin1(new String(props.getProperty(ADMIN1, "admin")));
6264
sp.setAdmin2(new String(props.getProperty(ADMIN2, "matthias")));
65+
sp.setRaspberry(new String(props.getProperty(RASPBERRY, "false")));
6366

6467
return sp;
6568
}
@@ -73,6 +76,7 @@ public void saveParamChanges(SystemParam sp)
7376
props.setProperty(DATE, sp.getDate());
7477
props.setProperty(ADMIN2, sp.getAdmin2());
7578
props.setProperty(ADMIN1, sp.getAdmin1());
79+
props.setProperty(RASPBERRY, sp.getRaspberry());
7680
File f = new File(PROPERTY_FILE);
7781
OutputStream out = new FileOutputStream(f);
7882
props.store(out, "Fichier de configuration système");

pro/src/fr/mcnanotech/forms/UserInterfaceForm.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
public class UserInterfaceForm
2323
{
2424
private static final String SYSTEM = "system";
25-
private static final String PERIHERAL = "peripheral";
2625
private static final String ATT_IN_GAME = "isingame";
2726
private static final String USERNAME = "username";
28-
@SuppressWarnings("unused")
2927
private UserDao userDao;
3028
private String result;
3129
private Map<String, String> errors = new HashMap<String, String>();
@@ -78,7 +76,7 @@ public SystemUser play(HttpServletRequest request, UserDao userdao)
7876
}
7977
if(errors.isEmpty())
8078
{
81-
System.out.println(consoleId+" joueur "+ player.getUserName());
79+
System.out.println(consoleId + " joueur " + player.getUserName());
8280
st.addUserTo(consoleId, player);
8381
SystemThread.setInfo(st);
8482
session.setAttribute(ATT_IN_GAME, "true");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package fr.mcnanotech.gpio;
2+
3+
import java.io.IOException;
4+
5+
import com.pi4j.io.i2c.I2CBus;
6+
import com.pi4j.io.i2c.I2CDevice;
7+
import com.pi4j.io.i2c.I2CFactory;
8+
9+
public class I2CTransfer
10+
{
11+
private static I2CDevice a1 = null, a2 = null, a3 = null, a4 = null, a5 = null, a6 = null;
12+
private static I2CDevice[] arduino = {a1, a2, a3, a4, a5, a6,};
13+
private static boolean isI2Cinit = false;
14+
15+
public static void initI2C(String raspberry) throws IOException
16+
{
17+
if(raspberry.equals("true"))
18+
{
19+
final I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
20+
for(int i = 0; i < (arduino.length); i++)
21+
{
22+
if(bus.getDevice(i) != null)
23+
{
24+
arduino[i] = bus.getDevice(i);
25+
}
26+
else
27+
{
28+
System.err.println("Arduino " + i + " not found on the i2c bus.");
29+
}
30+
isI2Cinit = true;
31+
}
32+
33+
}
34+
else
35+
{
36+
System.out.println("The server is not launched on a raspberry pi, disabling the I2C");
37+
}
38+
}
39+
40+
public static void setState(int arduinoAddress, int controller, boolean state) throws IOException
41+
{
42+
if(isI2Cinit)
43+
{
44+
switch(controller)
45+
{
46+
case 0:
47+
{
48+
if(state)
49+
{
50+
arduino[arduinoAddress].write((byte)10);
51+
}
52+
else
53+
{
54+
arduino[arduinoAddress].write((byte)0);
55+
}
56+
break;
57+
}
58+
case 1:
59+
{
60+
if(state)
61+
{
62+
arduino[arduinoAddress].write((byte)11);
63+
}
64+
else
65+
{
66+
arduino[arduinoAddress].write((byte)1);
67+
}
68+
break;
69+
}
70+
case 2:
71+
{
72+
if(state)
73+
{
74+
arduino[arduinoAddress].write((byte)12);
75+
}
76+
else
77+
{
78+
arduino[arduinoAddress].write((byte)2);
79+
}
80+
break;
81+
}
82+
case 3:
83+
{
84+
if(state)
85+
{
86+
arduino[arduinoAddress].write((byte)13);
87+
}
88+
else
89+
{
90+
arduino[arduinoAddress].write((byte)3);
91+
}
92+
break;
93+
}
94+
95+
}
96+
}
97+
}
98+
}

pro/src/fr/mcnanotech/main/GamingMachine.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package fr.mcnanotech.main;
22

3+
import java.io.IOException;
34
import java.util.ArrayList;
45
import java.util.List;
56

7+
import fr.mcnanotech.gpio.I2CTransfer;
8+
69
public class GamingMachine
710
{
811
private List<Player> players = new ArrayList<Player>();
@@ -20,6 +23,17 @@ public GamingMachine(int id, int maxPlayers)
2023
public void kickAll()
2124
{
2225
this.players.clear();
26+
for(int i = 0; i < 4; i++)
27+
{
28+
try
29+
{
30+
I2CTransfer.setState(this.id, i, false);
31+
}
32+
catch(IOException e)
33+
{
34+
e.printStackTrace();
35+
}
36+
}
2337
}
2438

2539
public Player getPlayers(int index)
@@ -38,6 +52,14 @@ public boolean addPlayer(Player player)
3852
{
3953
this.players.add(player);
4054
this.updateTime();
55+
try
56+
{
57+
I2CTransfer.setState(this.id, this.players.indexOf(player), true);
58+
}
59+
catch(IOException e)
60+
{
61+
e.printStackTrace();
62+
}
4163
return true;
4264
}
4365
return false;
@@ -57,6 +79,15 @@ public boolean removePlayer(int index)
5779
{
5880
if(index < this.players.size())
5981
{
82+
83+
try
84+
{
85+
I2CTransfer.setState(this.id, index, false);
86+
}
87+
catch(IOException e)
88+
{
89+
e.printStackTrace();
90+
}
6091
this.players.remove(index);
6192
return true;
6293
}
@@ -69,6 +100,15 @@ public boolean removePlayerByName(String name)
69100
{
70101
if(player.getUserName().equals(name))
71102
{
103+
104+
try
105+
{
106+
I2CTransfer.setState(this.id, this.players.indexOf(player), false);
107+
}
108+
catch(IOException e)
109+
{
110+
e.printStackTrace();
111+
}
72112
this.players.remove(player);
73113
return true;
74114
}
@@ -125,7 +165,7 @@ public int getCreditedPlayers()
125165
}
126166
return i;
127167
}
128-
168+
129169
public int getId()
130170
{
131171
return this.id;

pro/src/fr/mcnanotech/main/SystemThread.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package fr.mcnanotech.main;
22

3+
import java.io.IOException;
4+
35
import org.joda.time.DateTime;
46

57
import fr.mcnanotech.beans.SystemParam;
68
import fr.mcnanotech.beans.SystemUser;
79
import fr.mcnanotech.configloader.SettingsLoader;
810
import fr.mcnanotech.dao.DAOFactory;
911
import fr.mcnanotech.dao.UserDao;
12+
import fr.mcnanotech.gpio.I2CTransfer;
1013

1114
public class SystemThread extends Thread
1215
{
@@ -22,11 +25,22 @@ public class SystemThread extends Thread
2225
public void run()
2326
{
2427

28+
2529
SystemParam systemparam = new SystemParam();
2630
SettingsLoader settingsloader = new SettingsLoader();
2731

2832
systemparam = settingsloader.loadParams(systemparam);
2933
settingsloader.saveParamChanges(systemparam);
34+
35+
try
36+
{
37+
I2CTransfer.initI2C(systemparam.getRaspberry());
38+
}
39+
catch(IOException e)
40+
{
41+
e.printStackTrace();
42+
}
43+
3044
this.userDao = DAOFactory.getInstance().getDaoUser();
3145
st.setDailyCredit(systemparam.getDailyCredit());
3246
long t = (System.currentTimeMillis() / TIME_BASE);

0 commit comments

Comments
 (0)