fivdi / spi-device

SPI serial bus access with Node.js
MIT License
117 stars 16 forks source link

Uncaught Error: EINVAL, incorrect arguments passed to transferSync(message) #19

Closed caderoux closed 3 years ago

caderoux commented 3 years ago

I'm trying to port the Unicorn Hat Mini library to nodejs: https://github.com/pimoroni/unicornhatmini-python/blob/master/library/unicornhatmini/__init__.py

I'm just getting started and these initializations of the LED matrix controllers are giving me issues:

        for device, pin, offset in self.left_matrix, self.right_matrix:
            device.no_cs = True
            device.max_speed_hz = spi_max_speed_hz
            GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH)
            self.xfer(device, pin, [CMD_SOFT_RESET])
            self.xfer(device, pin, [CMD_GLOBAL_BRIGHTNESS, 0x01])
            self.xfer(device, pin, [CMD_SCROLL_CTRL, 0x00])
            self.xfer(device, pin, [CMD_SYSTEM_CTRL, 0x00])
            self.xfer(device, pin, [CMD_WRITE_DISPLAY, 0x00] + self.buf[offset:offset + (28 * 8)])
            self.xfer(device, pin, [CMD_COM_PIN_CTRL, 0xff])
            self.xfer(device, pin, [CMD_ROW_PIN_CTRL, 0xff, 0xff, 0xff, 0xff])
            self.xfer(device, pin, [CMD_SYSTEM_CTRL, 0x03])

Right now, I am just using synchronous code to get things working:

    constructor({bus, device, offset}) {
        this.bus = bus;
        this.device = device;
        this.offset = offset;

        this.SpiDevice = spi.openSync(this.bus, this.device, options);
        this.xfer([CMD_SOFT_RESET]);
        this.xfer([CMD_GLOBAL_BRIGHTNESS, 0x01]);
        this.xfer([CMD_SCROLL_CTRL, 0x00]);
        this.xfer([CMD_SYSTEM_CTRL, 0x00]);
        this.xfer([CMD_WRITE_DISPLAY, 0x00] + streambuffer[this.offset + (28 * 8)]);
        this.xfer([CMD_COM_PIN_CTRL, 0xff]);
        this.xfer([CMD_ROW_PIN_CTRL, 0xff, 0xff, 0xff, 0xff]);
        this.xfer([CMD_SYSTEM_CTRL, 0x03]);
    }

    xfer(command) {
        this.SpiDevice.transferSync({sendBuffer : Buffer.from(command), byteLength : command.length});
    }

I'm getting this error on the very first transfer:

Uncaught Error: EINVAL, incorrect arguments passed to transferSync(message)

which appears to come from here:

 if (info.Length() < 1 || !info[0]->IsArray()) {
    return Nan::ThrowError(
      Nan::ErrnoException(
        EINVAL,
        "transfer",
        "incorrect arguments passed to transferSync(message)"
      )
    );
  }

My command is 1 byte long - is that the problem?

fivdi commented 3 years ago

My command is 1 byte long - is that the problem?

No, this is not the problem.

Please take a few moments to read the documentation for the transferSync method. It explains how to call transferSync. The code posted above isn't calling transferSync correctly.

Also, take a look at the Usage section of the readme. It demonstrates how to call transfer which is similar to transferSync.

fivdi commented 3 years ago

I'm going to go ahead and assume all is ok with spi-device here and close this issue. If it does turn out to be an issue with spi-device the ticket can be reopened.

caderoux commented 3 years ago

My command is 1 byte long - is that the problem?

No, this is not the problem.

Please take a few moments to read the documentation for the transferSync method. It explains how to call transferSync. The code posted above isn't calling transferSync correctly.

Also, take a look at the Usage section of the readme. It demonstrates how to call transfer which is similar to transferSync.

Yes, I found the problem, the message parameter needs to be an array of sub-messages.