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;_
}