Hi Ytai,
I'm working now in this project. I have read all the examples that other
users and you post here and I tried to implement it. However, I don't know
why the app doesn't show the temperature. I append the MainActivity.java in
this message.
Thanks in advance!!
--
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.
package ioio.examples.hello;
import ioio.lib.api.TwiMaster;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIO.VersionType;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.TextView;
/**
* This is the main activity of the HelloIOIO example application.
*
* It displays a toggle button on the screen, which enables control of the
* on-board LED. This example shows a very simple usage of the IOIO, by using
* the {@link IOIOActivity} class. For a more advanced use case, see the
* HelloIOIOPower example.
*/
public class MainActivity extends IOIOActivity {
/** Variable para I2C */
private TwiMaster twi;
// Temperatura en grados centĂgrados
private float temperatura;
private TextView textView1;
/**
* Called when the activity is first created. Here we normally initialize
* our GUI.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView)findViewById(R.id.textViewValor);
}
/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
class Looper extends BaseIOIOLooper {
/**
* 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.IOIOLooper#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
showVersions(ioio_, "IOIO connected!");
twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
enableUi(true);
}
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
* @throws InterruptedException
* When the IOIO thread has been interrupted.
*
* @see ioio.lib.util.IOIOLooper#loop()
*/
@Override
public void loop() throws ConnectionLostException, InterruptedException {
temperatura = ReadTMP102(twi, 0x48);
setText(String.valueOf((temperatura)));
Thread.sleep(100);
}
/**
* Called when the IOIO is disconnected.
*
* @see ioio.lib.util.IOIOLooper#disconnected()
*/
@Override
public void disconnected() {
enableUi(false);
toast("IOIO disconnected");
}
/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see ioio.lib.util.IOIOLooper#incompatible(IOIO)
*/
@Override
public void incompatible() {
showVersions(ioio_, "Incompatible firmware version!");
}
}
/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
*/
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void showVersions(IOIO ioio, String title) {
toast(String.format("%s\n" +
"IOIOLib: %s\n" +
"Application firmware: %s\n" +
"Bootloader firmware: %s\n" +
"Hardware: %s",
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER)));
}
private void toast(final String message) {
final Context context = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
}
private void enableUi(final boolean enable) {
// This is slightly trickier than expected to support a multi-IOIO use-case.
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
public float ReadTMP102 (TwiMaster twi, int address) {
float temp = 0;
int lectura = 0;
byte[] responseBuffer = new byte[2];
byte[] request = new byte[] {0x00};
try {
twi.writeRead(address,
false,
request,
request.length,
responseBuffer,
responseBuffer.length);
// byte mas significativo y menos significativo
lectura = (responseBuffer[0] & 0xFF) << 8 | (responseBuffer[1] & 0xFF);
lectura >>= 4;
//temp = lectura / 16.f + 273.15f;
// 1 LSB = 0.0625ÂșC
//temp = (float)lectura * 0.0625f;
temp = lectura / 16.f;
} catch (ConnectionLostException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return temp;
}
private void setText(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView1.setText(str);
}
});
}
}