Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.silverspoon.bulldog.devices.sensors;

import java.io.IOException;

import io.silverspoon.bulldog.core.gpio.DigitalOutput;
import io.silverspoon.bulldog.core.io.bus.spi.SpiBus;
import io.silverspoon.bulldog.core.io.bus.spi.SpiConnection;
import io.silverspoon.bulldog.core.io.bus.spi.SpiDevice;
import io.silverspoon.bulldog.core.io.bus.spi.SpiMessage;
import io.silverspoon.bulldog.core.util.BulldogUtil;

/**
* Texas Instruments LM74 temperature sensor implementation.
*
* @author <a href="mailto:[email protected]">Matej Perejda</a>
*
*/
public class LM74TemperatureSensor extends SpiDevice {

private DigitalOutput digitalOutput = null;

/**
* This constructor is disabled. It can be used just if DigitalOutput is initialized.
*
* @param connection
*/
private LM74TemperatureSensor(SpiConnection connection) {
super(connection);
}

/**
* This constructor is disabled. It can be used just if DigitalOutput is initialized.
*
* @param bus
* @param address
*/
private LM74TemperatureSensor(SpiBus bus, int address) {
super(bus, address);
}

public LM74TemperatureSensor(SpiConnection connection, DigitalOutput digitalOutput) {
super(connection);
this.digitalOutput = digitalOutput;
}

/**
* LM74 activation signal. LM74'll be activated when signal goes from 1 to 0.
*/
public void initSensor() {
digitalOutput.low();
digitalOutput.toggle();
BulldogUtil.sleepMs(500);
}

/**
* Reading a temperature from temp sensor. Two's complement calculation.
*
* @see Page 11: http://www.ti.com/lit/ds/symlink/lm74.pdf
* @return float temperature value
* @throws IOException
**/
public float readTemperature() throws IOException {

float temperature = 0;
int bitShift;

this.initSensor();
this.open();

// sending bytes
byte[] buffer = new byte[] {(byte) 0x00, (byte) 0x00};

try {
SpiMessage message = this.transfer(buffer);

byte[] rec = message.getReceivedBytes();

int rec_0 = rec[0] & 0xFF;

int merged = (((BulldogUtil.getUnsignedByte(rec[0]) << 8)) | BulldogUtil.getUnsignedByte(rec[1]));
int first = rec_0 & Integer.parseInt("80", 16);

// negative temperature
// first binary digit 1 represents negative value
// 128 = b10000000
if (first == 128) {
int substr = merged - 1;
short inverted = (short) ~substr;
bitShift = inverted >> 3;
temperature = (float) (bitShift * 0.0625 * -1f);
}
// positive temperature
else {
bitShift = merged >> 3;
temperature = (float) (bitShift * 0.0625);
}

// chip shutdown
buffer = new byte[] {(byte) 0xFF};

this.transfer(buffer);

return temperature;
} catch (IOException e) {
throw new IOException("Something went wrong! SpiMessage was not transfered.");
} finally {
this.close();
}
}

}