* pointers with arrays? how

Hi all

I’ve been messing around with the BlinkM Communicator example.
In the example an array is built from an 8-byte message. The array is then used to control the BlinkM using the BlinkM_sendCmd();

Here is whats going on:

// the command line to contol the BlinkM is composed
byte addr = serInStr[1];
byte sendlen = serInStr[2];
byte recvlen = serInStr[3]; // not relevant regarding this question
byte* cmd = serInStr+4;

// … and the command line is send to the BlinkM functions class
BlinkM_sendCmd(addr, cmd, sendlen);

My question is… What does this line mean?

byte* cmd = serInStr+4;

I know that the ‘byte* cmd’ refers to 4 numbers (cmd (63), arg1, arg2, arg3). So far so good. But i am not sure what line means in relation to which elements of the array that the line refers to. Does +4 mean the next 4 elements in the array? And from where? And how would i write it, say if i also needed to use elements 10, 11, 12 and 13 from the same array for another byte* cmd variable? (for another BlinkM)

The example works with my Max/MSP patch, but i would like to KNOW whats going on in the arduino code.

The whole example (i’ve been cutting it down, only using neccessary elements)

#include “Wire.h”
#include “BlinkM_funcs.h”
const boolean BLINKM_ARDUINO_POWERED = true;
const int CMD_START_BYTE = 0x01;
byte serInStr[32];
int ledPin = 13;

void setup()
{
Serial.begin(19200);
pinMode(ledPin, OUTPUT);

if( BLINKM_ARDUINO_POWERED ) {
BlinkM_beginWithPower();
}
else {
BlinkM_begin();
}
}
void loop()
{
int num;

num = readCommand(serInStr);
if( num == 0 )
return;
digitalWrite(ledPin,HIGH);

byte addr = serInStr[1];
byte sendlen = serInStr[2];
byte recvlen = serInStr[3];
byte* cmd = serInStr+4;

Serial.println(sendlen);

BlinkM_sendCmd(addr, cmd, sendlen);

digitalWrite(ledPin,LOW);
}

uint8_t readCommand(byte *str)
{
uint8_t b,i;
if( ! Serial.available() ) return 0;

b = Serial.read();
if( b != CMD_START_BYTE )
return 0;

str[0] = b;
i = 100;

for( i=1; i<4; i++)
str *= Serial.read(); *

  • uint8_t sendlen = str[2];*

  • if( sendlen == 0 ) return 0;*

  • i = 100;*

  • while( Serial.available() < sendlen ) {*

  • delay(1);*

  • if( i-- == 0 ) return 0;*

  • }*

  • for( i=4; i<4+sendlen; i++ )*
    _ str = Serial.read(); _
    _
    return 4+sendlen;
    _
    }

byte* cmd    = serInStr+4;

This means cmd points to the address of serInStr[4] A pointer allways holds an address of some variabele. The string serInStr is build up like:

     0        1      2        3        4       .....    31
[ ??? ][address][sendlen][recvlen][command ............ ]

So cmd points to the place where the command string begins. This addres is passed (Including length) to another function to process it.

Rob

thx for the quick reply :)

Ok, so '+4' refers to the position in the array... and the 'sendlen' variable is used to define the length of the cmd (right?) when it is later processed by some other functions.

Let's say my array looked like this: 0 1 2 3 4 5 6 7 [not used] [adress] [sendlen] [recvlen] [command] [arg1] [arg2] [arg3] 8 9 10 11 12 [adress2] [command][arg1] [arg2] [arg3]

Would my code eventually look like this if i needed to cmd's?

addrA = serInStr[1]; addrB = serInStr[8]; byte* cmdA = serInStr+4; byte* cmdB = serInStr+9;

BlinkM_sendCmd(addrA, cmdA, sendlen); BlinkM_sendCmd(addrB, cmdB, sendlen);

The 'sendlen' is the same for both cmd's.

Thx in advance.

Would my code eventually look like this if i needed to cmd's?

Yes,

thx a lot! :)

Keep in mind that BlinkM_sendCmd is using sendlen to determine the number of arguments to the command. Unless all commands have the same number of arguments, in which case BlinkM_sendCmd wouldn't need to count them, your approach of embedding two commands with arguments into one array will not work.