-
• #2
var accx = SPI1.send([0xA9,0], E3)[1];
What is the trailing [1] designation for?Yes.
SPI.send
is sending an array with 2 bytes, and is returning an array with 2 bytes. But we only care about the second byte.In SPI the bytes are received as they are sent, so the first element (array index 0) is almost always useless since the device you're talking to hasn't even had a chance to know what you were asking for yet.
var data = this.spi.send([C.R_REGISTER | reg, 0,0,0,0,0], this.CSN);
Yes, it's exactly as you say.
Would this be a possible format to receive a string?
Not with that code, but
SPI1.send("a string", CS_PIN);
will return a string as the docs imply.Realistically I think in pretty much all cases it makes more sense and is more readable to ignore that functionality and instead just convert the received array to a String:
var ary = new Uint8Array(16); // send 16 bytes - so we get 16 bytes back var result = SPI1.send(ary, CS_PIN); console.log(E.toString(result));
Whether the data is a String, a Uint8Array or an Array makes no real difference to how it is transferred over SPI. At the end of the day it's just a stream of bits.
BUT a String has a length. So how you receive a String depends on what's on the other end of the SPI link and how they decided to implement it over SPI. Espruino will blindly send/receive the amount of raw data you asked it to.
Mon 2019.07.29
I am attempting to make sense of several examples. At this point it is learning only.
Stumbled across this note, and thought I'd give strings a try:
Searching churned up these examples:
SPI1.send([0x20,0b01000111], E3);
var accx = SPI1.send([0xA9,0], E3)[1];
I was not able to determine the following as the syntax appears odd, having square brackets attached to function parenthesis:
What is the trailing
[1]
designation for?Is it an array indexer, or an array of one byte that is passed, the container for the return data?
Sanity check please:
Using the source:
var data = this.spi.send([C.R_REGISTER | reg, 0,0,0,0,0], this.CSN);
shows a comma delimited list. As it appears inside the square brackets, does this command effectively say: 'Send an array of six elements initialized to zero, with element zero containing the bitwise mask of the contents of the parameter 'reg' OR'd with flags constant R_REGISTER, and apply this to the chip select pin?'
Would this be a possible format to receive a string? (making the assumption the slave is sending of course)
var ary = new Uint8Array(16);
ary = SPI1.send([CMD_SEND_STRING], CS_PIN);
or perhaps?
ary = SPI1.send([CMD_SEND_STRING], CS_PIN)[16];
What are the max buffer sizes in both directions?
Are there any other more suitable examples that might demonstrate these techniques?