Now my question is:
Is the "HiTechnic Sensor Multiplexer (NSX2020)" also supported?
http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NSX2020
I tried to check in the package for libraries but i couldn't find them.
Thank you
dark.
Moderators: roger, 99jonathan, imaqine
public class HTSensrMux extends I2CSensor {
public static final int STATE_AUTODETECT=0;
public static final int STATE_HALTED=1;
public static final int STATE_RUNNING=2;
protected static final int STATUS_BATTERY_BIT = 1;
protected static final int STATUS_BUSY_BIT = 2;
protected static final int STATUS_HALT_BIT = 4;
protected static final byte CMD_HALT = 0;
protected static final byte CMD_AUTODETECT = 1;
protected static final byte CMD_RUN = 2;
public static final byte CHMODE_DIG_SENS_BIT = 1;
public static final byte CHMODE_9V_EN_BIT = 2;
public static final byte CHMODE_DIG0_BIT = 4;
public static final byte CHMODE_DIG1_BIT = 8;
public static final byte CHMODE_SLOW_BIT = 16;
public final SMuxSensorPort S1 = new SMuxSensorPort(this,1);
public final SMuxSensorPort S2 = new SMuxSensorPort(this,2);
public final SMuxSensorPort S3 = new SMuxSensorPort(this,3);
public final SMuxSensorPort S4 = new SMuxSensorPort(this,4);
public HTSensrMux(I2CPort port) {
super(port);
port.setType(TYPE_LOWSPEED_9V);
this.setAddress(0x10/2);
}
public String getProductID(){
byte[] buf = new byte[8];
int failed = this.getData(0x08, buf, 8);
if(failed!=0) return "failed. ";
String str="l:";
for(int i=0; i<8; i++){
str+=(char)buf[i];
}
return str;
}
public int AutodetectAndRun(){
if(getCurrentState()!=STATE_HALTED){
setState(STATE_HALTED);
}
if(getCurrentState()!=STATE_HALTED) return 1;
setState(STATE_AUTODETECT);
if(getCurrentState()!=STATE_HALTED) return 2;
setState(STATE_RUNNING);
return 0;
}
public String getSensorType(){
byte[] buf = new byte[8];
int failed = this.getData(0x10, buf, 8);
if(failed!=0) return "failed. ";
String str="l:";
for(int i=0; i<8; i++){
str+=(char)buf[i];
}
return str;
}
public String getVersion(){
byte[] buf = new byte[8];
int failed = this.getData(0x00, buf, 8);
if(failed!=0) return "failed. ";
String str="l:";
for(int i=0; i<8; i++){
str+=(char)buf[i];
}
return str;
}
public boolean isBatteryLow(){
byte[] buf = new byte[1];
getData(0x21,buf,1);
return ((buf[0]&STATUS_BATTERY_BIT)!=0);
}
public int getCurrentState(){
byte[] stat = new byte[1];
getData(0x21,stat,1);
if((stat[0]&STATUS_HALT_BIT)!=0) return STATE_HALTED;
if((stat[0]&STATUS_BUSY_BIT)!=0) return STATE_AUTODETECT;
return STATE_RUNNING;
}
public void setState(int state){
if(state==STATE_HALTED){
sendData(0x20, CMD_HALT);
try{Thread.sleep(50);}catch(Exception e){}
}else if(state==STATE_AUTODETECT){
sendData(0x20, CMD_AUTODETECT);
try{Thread.sleep(500);}catch(Exception e){}
}else{
sendData(0x20, CMD_RUN);
}
}
//------------------------------------------------------Channel read properties-------------------------------------------------
public boolean isDigitalSensor(int channel){
return ((getSensorMode(channel)&CHMODE_DIG_SENS_BIT)!=0);
}
public byte getSensorMode(int channel){
byte[] buf = new byte[1];
getData(0x22+5*(channel-1),buf,1);
return buf[0];
}
public void setSensorMode(int channel,byte mode){
byte[] buf = new byte[]{mode};
sendData(0x22+5*(channel-1),buf,1);
}
public short getAnalogueValue(int channel){
byte[] buf = new byte[2];
getData(0x36+2*(channel-1),buf,2);
return (short)((buf[0] & 0x00FF) * 4 + (buf[1] & 0x00FF));
}
}
public class SMuxSensorPort implements ADSensorPort{
protected int channel;
protected HTSensrMux sMux;
public SMuxSensorPort(HTSensrMux multiplexer, int channel){
this.channel=channel;
sMux=multiplexer;
}
public boolean readBooleanValue() {
return readRawValue()>512;
}
public int readRawValue() {
return sMux.getAnalogueValue(channel);
}
public int readValue() {
return 1-readRawValue()/512;
}
public int getMode() {
// TODO Auto-generated method stub
return 0;
}
public int getType() {
// TODO Auto-generated method stub
return 0;
}
public void setMode(int mode) {
// TODO Auto-generated method stub
}
public void setType(int type) {
if(type==TYPE_LIGHT_ACTIVE){
sMux.setState(HTSensrMux.STATE_HALTED);
byte mode = sMux.getSensorMode(channel);
mode=(byte)(mode|HTSensrMux.CHMODE_DIG0_BIT);
sMux.setSensorMode(channel,mode);
sMux.setState(HTSensrMux.STATE_RUNNING);
}else if(type==TYPE_LIGHT_INACTIVE){
sMux.setState(HTSensrMux.STATE_HALTED);
byte mode = sMux.getSensorMode(channel);
mode=(byte)(mode&(~HTSensrMux.CHMODE_DIG0_BIT));
sMux.setSensorMode(channel,mode);
sMux.setState(HTSensrMux.STATE_RUNNING);
}
}
public void setTypeAndMode(int type, int mode) {
setType(type);
setMode(mode);
}
}
import lejos.nxt.*;
public class HTSensorMuxTest {
public static void main(String[] args) {
HTSensorMux sm1 = new HTSensorMux(SensorPort.S1);
LCD.drawString("" + sm1.getProductID(), 0,2);
LCD.drawString("" + sm1.getCurrentState(), 0,3);
LCD.drawString("" + sm1.getVersion(), 0,3);
LCD.refresh();
SMuxSensorPort us1 = sm1.S1;
us1.setMode(us1.MODE_RAW);
us1.setType(us1.TYPE_LOWSPEED_9V);
//UltrasonicSensor us = new UltrasonicSensor((I2CPort) us1);
while(!Button.ESCAPE.isPressed()){
LCD.drawString("" + us1.readRawValue(), 0,0);
LCD.drawString("" + us1.readValue(), 0,1);
}
}
}
class SMuxColorSensor{
protected SMuxSensorPort port;
public SMuxColorSensor(SMuxSensorPort port){
this.port=port;
//let the SMux know that it is an Digital Sensor
port.getMultiplexer().setSensorMode(port.getMultiplexerChannel(),HTSensrMux.CHMODE_DIG_SENS_BIT);
//The address of the color sensor is one
port.getMultiplexer().setDeviceAddress(port.getMultiplexerChannel(),(byte)1);
//Read the bytes from the register address 0x42 to 0x46 (colornumber, red value, green value, blue value)
port.getMultiplexer().setRegisterAddress(port.getMultiplexerChannel(),(byte)0x42);
}
public void getColor(int[] rgb){
byte[] data = new byte[4];
//read the i2cbuffer
port.getMultiplexer().getI2CBufferData(port.getMultiplexerChannel(),data,4);
rgb[0]=data[1]<0?255+data[1]:data[1];
rgb[1]=data[2]<0?255+data[2]:data[2];
rgb[2]=data[3]<0?255+data[3]:data[3];
}
}
/**
* Constructor
* @param port the port
*/
public SensorMux(I2CPort port) {
super(port, 0x10, I2CPort.LEGO_MODE, TYPE_LOWSPEED_9V);
}
clonejo wrote:Was the class just broken or has the address changed?
Users browsing this forum: No registered users and 1 guest