The code is set this way (may seem familiar...)
I'll have to check behavior in the morning, to see if the 0.5°c difference
is a reliable constant value.
Class for unsigned
----------------------------------------------------------
public class Unsigned {
public static int from(byte b) {
return b & 0xFF;
}
public static int from(short s) {
return s & 0xFFFF;
}
}
----------------------------------------------------------
I used helloIOio as a canvas, added tmp102 reading
----------------------------------------------------------
*public float Readtmp102(int address, TwiMaster port) {*
*float returnval = 0;*
* byte[] request = new byte[] { 0x00 };*
* byte[] tempdata = new byte[2];*
* try {*
* port.writeRead(address, false,
request,request.length,tempdata,tempdata.length);*
* //This code good, comes from Ytai*
*int temp = Unsigned.from(tempdata[0]) << 8 | Unsigned.from(tempdata[1]); *
*temp >>= 4; *
*Float Celsiustemp =* *temp / 16.f;*
*return Celsiustemp;*
*} catch (ConnectionLostException e) {*
*// TODO Auto-generated catch block *
*e.printStackTrace();*
*Log.e("tmp102test","ConnectionLost Exception in Readtmp102 : "+e);*
*} catch (InterruptedException e) {*
*// TODO Auto-generated catch block*
*e.printStackTrace();*
*Log.e("tmp102test","InterruptedException Exception in Readtmp102 :
"+e); *
*} finally {}*
*return returnval;*
*}*
private void setTemperatureSensor(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView1_.setText(str+" °c");
}
});
}
/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
led_ = ioio_.openDigitalOutput(0, true);
//tmp102
twi_temperature = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
}
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
*/
@Override
public void loop() throws ConnectionLostException {
//read tmp102
Float SensorTemperature = Readtmp102(0x48, twi_temperature);
Log.v("tmp102test"," Readtmp102 : "+SensorTemperature);
setTemperatureSensor(String.valueOf(SensorTemperature));
led_.write(!button_.isChecked());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
----------------------------------------------------------
(I would like to apologise for the clumsy post layout, i'm not used to
groups.google)
On Friday, March 21, 2014 at 9:59:40 PM UTC+1, Marius Vosylius wrote:
>
> I'm struggling to get my TWI reading out, Im not sure where Im going wrong?
>
> TMP102 DataSheet
> https://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.pdf
>
> Any help or example would me really appreciated.
>
> Main Class:
>
> // IOIO pin 26 = SDA, pin 25 = SCL
>
> private final int twiNum = 26;
>
> // TextView Display Temperature
>
> private TextView tmpTextView;
>
> private TwiMaster twi;
>
> int address;
>
> byte[] request = new byte[] { 0x00, 0x48};
>
> byte[] response = new byte[4];
>
> Setup:
>
> twi = ioio_.openTwiMaster(twiNum, TwiMaster.Rate.RATE_100KHz, false);
>
> Loop:
>
> address = 0x48;
>
> try {
>
> twi.writeRead(address, false, request, request.length, response,
> response.length);
>
> /* Update UI */
>
> updateViews();
>
> } catch (InterruptedException e) {
>
> ioio_.disconnect();
>
> } catch (ConnectionLostException e) {
>
> throw e;
>
> }
>
> private void updateViews() {
>
> runOnUiThread(new Runnable() {
>
> @Override
>
> public void run() {
>
> tmpTextView.setText(String.valueOf(response));
>
> }
>
> });
>
> }
>
--
You received this message because you are subscribed to the Google Groups
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.