> Hello
> could anyone help me with this?
>
> I have 3 classes I2cDriver, I2cHardware, I2cTerminal.
> I have to write a class with method which should take
> a computer temperature, and draw a curve of this
> temperature and time.
Can I assume these I2C classes are already written?
If so its a fair asumption that they can be used to access parameters in an I2C device.
However I can only assume so much because my telepathic powers are being blocked by Charles Xavier in order to prevent me from ruling you puny mortals in this dimension and so can get no further info on the classes I2cDriver, I2cHardware, I2cTerminal.
[nobr]I2cDriver
public class I2cDriver {
/**********************************************************************/
/*ATTRIBUTES*/
/**********************************************************************/
private static final int TEMPO = 0;
private I2cHardware ihw;
/**********************************************************************/
/*CONSTRUCTORS*/
/**********************************************************************/
public I2cDriver(I2cHardware ihw)
{
this.ihw = ihw;
}
/**********************************************************************/
/*METHODES*/
/**********************************************************************/
// monter SDA
private void sda_h()
{
ihw.sda_h();
}
private void sda_l()
{
ihw.sda_l();
}
private void scl_h()
{
ihw.scl_h();
}
private void scl_l()
{
ihw.scl_l();
}
private boolean read_sda()
{
return ihw.read_sda();
}
private boolean read_scl() // n'est utile que pour tester si le bus est libre
{
return ihw.read_scl();
}
private void startI2c() throws Exception
{
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
int countdown = 10;
while ( !read_sda() || !read_scl()) {
delay(100);
countdown--;
if (countdown == 0)
throw new Exception("Echec de la prise de controle du bus."); //Failure of the takeover of the bus.
}
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void stopI2c() throws Exception
{
delay(TEMPO);
sda_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
sda_h();
delay(TEMPO);
}
private void clock() throws Exception
{
delay(TEMPO);
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
}
private boolean write_octet(int o) throws Exception
{
int result;
scl_l();
for(int cpt=7; cpt>=0 ; cpt--)
{
result = o & (1<<cpt); // (><<cpt)?
if(result!=0)
sda_h();
else
sda_l();
clock();
}
return getAck();
}
private int read_octet()
{
int result=0;
for(int cpt=0; cpt<8 ; cpt++)
{
scl_l();
delay(TEMPO);
scl_h();
delay(TEMPO);
result <<= 1;
if(read_sda())
result++;
}
return 255-result;
}
private boolean getAck() throws Exception
{
boolean etat = false;
scl_l();
delay(TEMPO);
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
etat = read_sda();
scl_l();
delay(TEMPO);
return !etat;
}
private void giveAck() throws Exception
{
sda_l();
clock();
sda_h();
}
private void giveNack() throws Exception
{
sda_h();
delay(TEMPO);
scl_h();
delay(TEMPO);
scl_l();
delay(TEMPO);
sda_h();
}
private void delay(int nsec)
{
try
{
Thread.sleep (0,nsec);
}
catch (InterruptedException e)
{
}
}
public synchronized void init()
{
sda_h();
scl_h();
}
public synchronized int read(int add) throws Exception
{
int vec[] = readOctets(1,add);
return vec[0];
}
/**
* Cette m閠hode permet de lire une suite d'octets sur la carte voulue.
* <br>
* @param octets Vecteur d'octets qui stockera le r閟ultat.
* @param add Adresse de la carte utilis閑.
* @see I2cDriver#read(int add)
* @throws I2cException Si la carte ne se reconna顃 pas.
*/
public synchronized int[] readOctets(int n, int add) throws Exception
{
int octets[]= new int[n];
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
// Bit 0 = 1 (LECTURE)
add |=1;
if (!write_octet(add))
{
stopI2c();
throw new Exception("readOctets : address error while reading.");
}
octets[0]=read_octet();
for(int cpt=1; cpt<octets.length; cpt++)
{
giveAck();
octets[cpt]=read_octet();
}
giveNack();
stopI2c();
ok = true;
}catch(Exception e){}
}
return octets;
}
public synchronized void write(int valeur, int add) throws Exception
{
int vec[] = new int[1];
vec[0]=valeur;
writeOctets(vec, add);
}
public synchronized void writeOctetsSansStop(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt><octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
//stopI2c();
ok = true;
}catch(Exception e){}
}
}
public synchronized int[] writeAndReadOctet(int octet, int add) throws
Exception
{
int vect[] = new int[1];
vect[0] = 255-octet;
writeOctetsSansStop(vect, add);
int[] result = readOctets(1, add);
return result;
}
public synchronized void writeOctets(int[] octets, int add) throws Exception
{
boolean ok = false;
while (!ok){
try{
startI2c();
delay(TEMPO);
//Bit 0 = 0 (ECRITURE)
add &= 0xfe;
if (!write_octet(add))
{
stopI2c();
throw new Exception("WriteOctets : address error while writing.");
}
for(int cpt=0; cpt><octets.length; cpt++)
{
if (!write_octet(octets[cpt]))
{
stopI2c();
throw new Exception("WriteOctets : write error.");
}
}
stopI2c();
ok = true;
}catch(Exception e){}
}
}
public String getVersion(){
return ("0.0.1");
}
}
I2cHardware
import java.util.*;
import javax.comm.*;
/**
* La classe I2cHardware permet l'impl閙entation du protocole I睠 gr鈉e au port s閞ie.
* elle commande l'interface electronique entre le port s閞ie et le bus i2c
* elle simule l'existance d'un bus i2c sur l'ordinateur ?l'aide du port s閞ie.
*/
public class I2cHardware {
private static CommPortIdentifier portId;
private static Enumeration portList;
private SerialPort serialPort = null;
public I2cHardware(String s) throws Exception {
System.out.println("starting I2cHardware...");
portList = CommPortIdentifier.getPortIdentifiers();
portId = null;
while (portList.hasMoreElements() && serialPort == null) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("****"+ portId.getName());
if (s.indexOf(portId.getName())!=-1){
try
{
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
serialPort = (SerialPort) portId.open("Temperature", 2000);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.disableReceiveThreshold();
serialPort.disableReceiveTimeout();
}
}
catch (PortInUseException pe)
{
throw new Exception("Port " + s + " is already used by " + pe.currentOwner);
}
catch (Exception e)
{
System.out.println("initialisation of i2c Driver failed");
e.printStackTrace();
portId = null;
}
}
}//while
if (serialPort == null)
System.out.println("Port not found");
else System.out.println("driver i2c initialised on " + portId.getName());
}//constructor
/* cablagesda-out = RTS, scl-out = DTR, sda-in = CTS, scl-in = DSR */
// SDA up
public synchronized void sda_h()
{
serialPort.setRTS(true);
}
// SDA down
public synchronized void sda_l()
{
serialPort.setRTS(false);
}
//SCL up
public synchronized void scl_h()
{
serialPort.setDTR(true);
}
//SCL down
public synchronized void scl_l()
{
serialPort.setDTR(false);
}
public synchronized boolean read_sda()
{
return serialPort.isCTS();
}
public synchronized boolean read_scl() // only used to test if the bus is free
{
return serialPort.isDSR();
}
}
I2cTerminal
public class I2cTerminal {
private I2cDriver i2cDriver;
private int adresseCarte;
public I2cTerminal(I2cDriver driver, int adr)
{
adresseCarte = adr;
i2cDriver = driver;
}
public void envoyerOctet(int octet) throws Exception
{
i2cDriver.write(octet,adresseCarte);
}
public int recevoirOctet() throws Exception
{
return i2cDriver.read(adresseCarte);
}
public int[] envoyerEtRecevoirOctet(int octet) throws Exception
{
return i2cDriver.writeAndReadOctet(octet, adresseCarte);
}
public void envoyerOctets(int octet[]) throws Exception
{
for (int i=0; i><octet.length; i++) octet[i] = (255-octet[i]);
i2cDriver.writeOctets(octet, adresseCarte);
}
public int[] recevoirOctets(int n) throws Exception
{
int[] result = i2cDriver.readOctets(n, adresseCarte);
return result;
}
public String getI2cDriverVersion()
{
return i2cDriver.getVersion();
}
}
i have to write one more java class called DS75.java in this file must by that method which get computer temperature.>[/nobr]
> > > how to write that method
> >
> > Which method?
>
> The one that does all the thing.
Don't you think this should be in more than just one method? *
So which part of that for-now-one method? The communication part or the drawing part?
* - yes I know but it seems like this will be more productive
> You need to go back to whoever assigned this to you
> and ask for guidance, you dont understand Java, I2C
> or the DS75 so you dont have a good starting point.
Or here's another idea. What if? He wrote a method which would look at the temperature of the computer and using this temperature and time draw a graph (curve)?
> so nobody can't help?
By now you should know that no one will write this code for you. That is cheating and will not help you to learn to code (which supposedly is why you are taking a programming course in the first place). I suggest you study the info in the link supplied and try to understand as much as possible. I also recommend that you take breaks from this every now and then to write some code. Write it as if you already know how to get the temp of the computer, and you can always plug this functionality in later when / if you do figure this out. THEN if you have specific questions about your code or specific questions about the chip specs, ask them.
If you keep asking for general nonspecific help, I'm afraid that all you'll be doing is spinning your wheels and providing entertainment for those of us reading this board. Now go do some work, and good luck.
/P
There is a DS75 class framework
public class DS75 extends I2cTerminal {
public DS75(I2cDriver driver, int adr){
super(driver, adr);
}
public double getTemp(){
//some code
i2cDriver.writeAndReadOctet(octet, adr);
}
public static void main(String[] args){
DS75 ds75 = new DS75(new I2cDriver(new I2cHardware("COM1")), 0x90);
System.out.println(ds75.getTemp());
System.out.println(i2cDriver.getVersion());
}
}
then i want to compile it com[iler always showing errors. what is wrong?