Page 1 of 2

NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 09:03
by HaWe
(because it's more a compiler or syntax problem, I moved this post from the Tetrix hardware thread to the software forum...)

I just tried my first Tetrix Motor Control program, but it doesn't compile!
(Bricxcc 3.3.8.9. built 3.11.2010)

Code: Select all

// #include "nxcio.h"


void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
  byte devAddr, modeReg, powerReg;
  char result;

  devAddr = (TXmotor+2)&14;
  modeReg = 0x44 + (TXmotor % 2)*3;
  powerReg= 0x45 + (TXmotor % 2);

  // NXTsensorPort, I2Caddress, 44H byte Motor_mode: 0=power control
  result=WriteI2CRegister(NXTport, devAddr, modeReg,  0);
  // NXTsensorPort, I2Caddress, 45H s/byte  Motor_power: e.g. 50 =50%
  result=WriteI2CRegister(NXTport, devAddr, powerReg, percentage);
}

task main(){

  TXMotorOn(0, 1, 50);
  Wait(2000);
  TXMotorOn(0, 1,-50);
  Wait(2000);
  TXMotorOn(0, 1,  0);
  Wait(2000);
  while (true);
}
# Error: Invalid constant expression: __TXMotorOn_7qG2_modeReg_7qG2_000
File "c:\Temp\temp.nxc" ; line 15
# set __WDSC_SensorRegister, __TXMotorOn_7qG2_modeReg_7qG2_000
#----------------------------------------------------------
# Error: Invalid constant expression: __TXMotorOn_7qG2_powerReg_7qG2_000
File "c:\Temp\temp.nxc" ; line 17
# set __WDSC_SensorRegister, __TXMotorOn_7qG2_powerReg_7qG2_000
#----------------------------------------------------------
2 errors during compilation
what's wrong???

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 09:29
by mightor
I did some experimentation and it seems that if the register is not known at compile time then it just won't work.

Code: Select all

result=WriteI2CRegister(NXTport, devAddr, powerReg, percentage);  
breaks

Code: Select all

result=WriteI2CRegister(NXTport, devAddr, 0x45, percentage); 
compiles fine.

So you need to use the standard method of I2C communication, not this short cut.

- Xander

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 09:42
by HaWe
thx for your hint, Xander! I already was almost back to despair.

I will post it to the wishlist, but can you pls tell me how to rebuild the code to the standard method in the meantime?

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:11
by mightor
Try this, it hasn't been tested, of course. I based the code on other stuff I've written before.

Code: Select all

// #include "nxcio.h"


void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
  const byte retLen = 0;
  byte modeMsg[];
  byte powerMsg[];
  byte devAddr, modeReg, powerReg;
  char result;

  devAddr = (TXmotor+2)&14;
  modeReg = 0x44 + (TXmotor % 2)*3;
  powerReg= 0x45 + (TXmotor % 2);

  // Create the message arrays
  ArrayBuild(modeMsg, devAddr, modeReg);
  ArrayBuild(powerMsg, devAddr, powerReg, percentage);
  
  // Send the first message as soon as the bus is ready
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  if(I2CWrite(NXTport, retLen, modeMsg) != NO_ERR)
    return;

  // Send the second message when the first one is done
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  if(I2CWrite(NXTport, retLen, powerMsg) != NO_ERR)
    return;
}

task main(){

  TXMotorOn(0, 1, 50);
  Wait(2000);
  TXMotorOn(0, 1,-50);
  Wait(2000);
  TXMotorOn(0, 1,  0);
  Wait(2000);
  while (true);
}

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:14
by mightor
The first message needs to built up like this:

Code: Select all

ArrayBuild(modeMsg, devAddr, modeReg, 0);
My bad.

- Xander

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:17
by mightor
This code should do what you need:

Code: Select all

long MyWriteI2CRegister(byte port, byte address, byte register, byte regvalue) {
  const byte retLen = 0;
  byte msg[];

  // Create the message array
  ArrayBuild(msg, address, register, regvalue);

  // Send the first message as soon as the bus is ready
  while ((I2CCheckStatus(port) == STAT_COMM_PENDING) && (I2CCheckStatus(port) != NO_ERR)) Yield();
  return I2CWrite(port, retLen, msg);
}
Again, not tested :)

- Xander

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:27
by HaWe
thx a lot again, Xander.
I tried the new code with the new ArrayBuild,
it compiles without error,
mux power is on,
the NXT screen says "running"

- but nothing happens. :cry:
I surely will therefore have to wait for John's Tetrix implementation.

---
acc. to your

Code: Select all

MyWriteI2CRegister(byte port, byte address, byte register, byte regvalue)
will it be possible to pass also a int or a long as a regvalue (e.g., for passing encoder values)?

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:32
by mightor
doc-helmut wrote:will it be possible to pass also a int or a long as a regvalue (e.g., for passing encoder values)?
Nope, as long as NXC doesn't support function overloading it can't be done with a single function name.

- Xander

Re: NXC: WriteI2CRegister compile error

Posted: 05 Dec 2010, 10:53
by HaWe
ok, thx, but I must admit: it is even much harder than I thought with the I2C motor program for Tetrix
But that's not a WriteI2CRegister compile issue any longer.

- thread can be closed -

Re: NXC: WriteI2CRegister compile error

Posted: 07 Dec 2010, 16:47
by afanofosc
I am fixing this problem in WriteI2CRegister so that you can pass in a variable or a constant.

John Hansen