Example #1
0
/*JSON{
  "type" : "method",
  "class" : "I2C",
  "name" : "readFrom",
  "generate" : "jswrap_i2c_readFrom",
  "params" : [
    ["address","JsVar","The 7 bit address of the device to request bytes from, or an object of the form `{address:12, stop:false}` to send this data without a STOP signal."],
    ["quantity","int32","The number of bytes to request"]
  ],
  "return" : ["JsVar","The data that was returned - as a Uint8Array"],
  "return_object" : "Uint8Array"
}
Request bytes from the given slave device, and return them as a Uint8Array (packed array of bytes). This is like using Arduino Wire's requestFrom, available and read functions.  Sends a STOP
 */
JsVar *jswrap_i2c_readFrom(JsVar *parent, JsVar *addressVar, int nBytes) {
  IOEventFlags device = jsiGetDeviceFromClass(parent);
  if (!DEVICE_IS_I2C(device)) return 0;

  bool sendStop = true;
  int address = i2c_get_address(addressVar, &sendStop);

  if (nBytes<=0)
    return 0;
  if ((unsigned int)nBytes+256 > jsuGetFreeStack()) {
    jsExceptionHere(JSET_ERROR, "Not enough free stack to receive this amount of data");
    return 0;
  }
  unsigned char *buf = (unsigned char *)alloca((size_t)nBytes);

  jshI2CRead(device, (unsigned char)address, nBytes, buf, sendStop);

  JsVar *array = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, nBytes);
  if (array) {
    JsvArrayBufferIterator it;
    jsvArrayBufferIteratorNew(&it, array, 0);
    unsigned int i;
    for (i=0;i<(unsigned)nBytes;i++) {
      jsvArrayBufferIteratorSetByteValue(&it, (char)buf[i]);
      jsvArrayBufferIteratorNext(&it);
    }
    jsvArrayBufferIteratorFree(&it);
  }
  return array;
}
Example #2
0
void jswrap_spi_send_cb(int c, jswrap_spi_send_data *data) {
  int result = data->spiSend(c, &data->spiSendData);
  if (c>=0) data->txAmt++;
  if (result>=0) {
    jsvArrayBufferIteratorSetByteValue(&data->it, (char)result);
    jsvArrayBufferIteratorNext(&data->it);
    data->rxAmt++;
  }
}
Example #3
0
/**
 * Send a single byte to the SPI device, used ad callback.
 */
void jswrap_spi_send_cb(
    int c,                     //!< The byte to send through SPI.
    jswrap_spi_send_data *data //!< Control information on how to send to SPI.
  ) {
  // Invoke the SPI send function to transmit the single byte.
  int result = data->spiSend(c, &data->spiSendData);
  if (c>=0) data->txAmt++;
  if (result>=0) {
    jsvArrayBufferIteratorSetByteValue(&data->it, (char)result);
    jsvArrayBufferIteratorNext(&data->it);
    data->rxAmt++;
  }
}
Example #4
0
/*JSON{
  "type" : "method",
  "class" : "OneWire",
  "name" : "read",
  "generate" : "jswrap_onewire_read",
  "params" : [["count","JsVar","(optional) The amount of bytes to read"]],
  "return" : ["JsVar","The byte that was read, or a Uint8Array if count was specified and >=0"]
}
Read a byte
 */
JsVar *jswrap_onewire_read(JsVar *parent, JsVar *count) {
  Pin pin = onewire_getpin(parent);
  if (!jshIsPinValid(pin)) return 0;
  if (jsvIsNumeric(count)) {
    JsVarInt c = jsvGetInteger(count);
    JsVar *arr = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, c);
    if (!arr) return 0;
    JsvArrayBufferIterator it;
    jsvArrayBufferIteratorNew(&it, arr, 0);
    while (c--) {
      jsvArrayBufferIteratorSetByteValue(&it, (char)OneWireRead(pin, 8));
      jsvArrayBufferIteratorNext(&it);
    }
    jsvArrayBufferIteratorFree(&it);
    return arr;
  } else {
    return jsvNewFromInteger(OneWireRead(pin, 8));
  }
}