// 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 ioio-users+...@googlegroups.com.
To post to this group, send email to ioio-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.
private TextView tmpTextView;
private TwiMaster twi;
int address = 0x48;
byte[] request = new byte[] { 0x00, 0x48};
byte[] response = new byte[2];
Looper Class:
public void setup() throws ConnectionLostException {
twi = ioio_.openTwiMaster(1, TwiMaster.Rate.RATE_100KHz, false);
}
public void loop() throws ConnectionLostException {
try {
twi.writeRead(address, false, request, request.length, response, response.length);
/* Update UI */
updateViews();
} catch (InterruptedException e) {
ioio_.disconnect();
} catch (ConnectionLostException e) {
throw e; } } }
Still Nothing.
--
Dear Ytai,
private TwiMaster twi;
private static final byte address = 0x48;
private static final byte[] request = {0x00};
private final byte[] response = new byte[2];
class Looper extends BaseIOIOLooper {
public void setup() throws ConnectionLostException {
twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"TMP102 Connected!", Toast.LENGTH_SHORT).show();
}
});
}
public float getTemperature() throws Exception {
if (!twi.writeRead(address, false, request, 1, response, 2)) {
throw new Exception("I2C transaction with TMP102 failed.");
}
int temp = Temp.from(response[0]) << 8 | Temp.from(response[1]);
temp >>= 4;
return temp / 16.f + 273.15f;
}
}
Is this what I have is Correct? I don't get any error.
Can you send your latest code as well as a more elaborate explanation on what's not working?
class Looper extends BaseIOIOLooper {
public void setup() throws ConnectionLostException {
twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"TMP102 Connected!", Toast.LENGTH_SHORT).show();
}
});
}
public float getTemperature() throws Exception {
if (!twi.writeRead(address, false, request, 1, response, 2)) {
throw new Exception("I2C transaction with TMP102 failed.");
}
int temp = Temp.from(response[0]) << 8 | Temp.from(response[1]);
temp >>= 4;
return temp / 16.f + 273.15f;
}
}
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
--
It should be periodically called from loop() and the result sent to the UI widget (TextView).
class Looper extends BaseIOIOLooper {
public void setup() throws ConnectionLostException {
twi = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_100KHz, false);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"TMP102 Connected!", Toast.LENGTH_SHORT).show();
}
});
}
public void loop() throws ConnectionLostException, InterruptedException {
try {
twi.writeRead(address, false, request, request.length, response, response.length);
/* Update UI */
updateViews(getTemperature());
} catch (InterruptedException e) {
ioio_.disconnect();
} catch (ConnectionLostException e) {
throw e;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public float getTemperature() throws Exception {
if (!twi.writeRead(address, false, request, 1, response, 2)) {
throw new Exception("I2C transaction with TMP102 failed.");
}
int temp = Temp.from(response[0]) << 8 | Temp.from(response[1]);
temp >>= 4;
return temp / 16.f + 273.15f;
}
}
private void updateViews(float f) {
final String str = String.format("%.2f", f);
runOnUiThread(new Runnable() {
@Override
public void run() {
tmpTextView.setText(str);
}
});
}
The code seems OK. What are you seeing in the logs?
Also, did you intentionally change the TWI number back to 0?
Looks OK. Those are in degrees Kelvin.
Dear Ytai, Im trying to set Progress Bar for my Temperature Value Im not sure how its done, heres my code:
class Looper extends BaseIOIOLooper {
public void setup() throws ConnectionLostException {
twi = ioio_.openTwiMaster(1, TwiMaster.Rate.RATE_100KHz, false);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"TMP102 Connected!", Toast.LENGTH_SHORT).show();
}
});
}
public void loop() throws ConnectionLostException, InterruptedException {
try {
twi.writeRead(address, false, request, request.length, response, response.length);
/* Update UI */
updateViews(getTemperature());
setProgressBar(); ???
} catch (InterruptedException e) {
ioio_.disconnect();
} catch (ConnectionLostException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
}
// Get Temperature in C Method
public float getTemperature() throws Exception {
celcius = Temp.from(response[0]) << 8 | Temp.from(response[1]);
celcius >>=4;
return celcius / 16.f;
}
}
// Update TextView with Temperature Reading
private void updateViews(float f) {
final String str = String.format(Locale.UK, "%.1f", f);
runOnUiThread(new Runnable() {
@Override
public void run() {
tmpTextView.setText(str);
}
});
}
// Set Value to ProgressBar
private void setProgressBar(final int value) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBarTemp.setProgress(value);
}
});
}
My XML File for ProgressBar:
<ProgressBar
android:id="@+id/progressBarTemp"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="250dp"
android:layout_height="273dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="false"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/circular_progressbar" />
And my Circular progress Bar in Drawable Folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<shape android:shape="ring"
android:innerRadiusRatio="4"
android:thicknessRatio="8.0">
<gradient
android:startColor="#287AA9"
android:centerColor="#FFCC00"
android:endColor="#ff0000"
android:type="sweep" />
</shape>
</item>
</layer-list>
I was able to competed this successfully, Thank you Very Much for your Help with this Sensor Ytai!
--
----------------------------------------------------------
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[] 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'm struggling to get my TWI reading out, Im not sure where Im going wrong?TMP102 DataSheetAny 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));
}
});
}
OK, great! Let me know if there's anything else I can help with.