Пример #1
0
static void iotjs_pwm_set_configuration(const iotjs_jval_t* jconfiguration,
                                        iotjs_pwm_t* pwm) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_pwm_t, pwm);

  iotjs_jval_t jpin =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_PIN);
  _this->pin = iotjs_jval_as_number(&jpin);

#if defined(__linux__)
  iotjs_jval_t jchip =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_CHIP);
  _this->chip = iotjs_jval_as_number(&jchip);
  iotjs_jval_destroy(&jchip);
#endif

  iotjs_jval_t jperiod =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_PERIOD);
  if (iotjs_jval_is_number(&jperiod))
    _this->period = iotjs_jval_as_number(&jperiod);

  iotjs_jval_t jduty_cycle =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_DUTYCYCLE);
  if (iotjs_jval_is_number(&jduty_cycle))
    _this->duty_cycle = iotjs_jval_as_number(&jduty_cycle);

  iotjs_jval_destroy(&jpin);
  iotjs_jval_destroy(&jperiod);
  iotjs_jval_destroy(&jduty_cycle);
}
Пример #2
0
static jerry_value_t StatsIsTypeOf(jerry_value_t stats, int type) {
  jerry_value_t mode = iotjs_jval_get_property(stats, IOTJS_MAGIC_STRING_MODE);

  int mode_number = (int)iotjs_jval_as_number(mode);

  jerry_release_value(mode);

  return jerry_create_boolean((mode_number & S_IFMT) == type);
}
Пример #3
0
static int iotjs_spi_get_array_data(char** buf, const iotjs_jval_t* jarray) {
  iotjs_jval_t jlength =
      iotjs_jval_get_property(jarray, IOTJS_MAGIC_STRING_LENGTH);
  IOTJS_ASSERT(!iotjs_jval_is_undefined(&jlength));

  size_t length = iotjs_jval_as_number(&jlength);
  IOTJS_ASSERT((int)length >= 0);
  *buf = iotjs_buffer_allocate(length);

  for (size_t i = 0; i < length; i++) {
    iotjs_jval_t jdata = iotjs_jval_get_property_by_index(jarray, i);
    (*buf)[i] = iotjs_jval_as_number(&jdata);
    iotjs_jval_destroy(&jdata);
  }

  iotjs_jval_destroy(&jlength);

  return (int)length;
}
Пример #4
0
static int spi_get_array_data(char** buf, jerry_value_t jarray) {
  jerry_value_t jlength =
      iotjs_jval_get_property(jarray, IOTJS_MAGIC_STRING_LENGTH);
  IOTJS_ASSERT(!jerry_value_is_undefined(jlength));

  size_t length = iotjs_jval_as_number(jlength);
  IOTJS_ASSERT((int)length >= 0);
  *buf = iotjs_buffer_allocate(length);

  for (size_t i = 0; i < length; i++) {
    jerry_value_t jdata = iotjs_jval_get_property_by_index(jarray, i);
    (*buf)[i] = iotjs_jval_as_number(jdata);
    jerry_release_value(jdata);
  }

  jerry_release_value(jlength);

  return (int)length;
}
Пример #5
0
size_t iotjs_bufferwrap_length(iotjs_bufferwrap_t* bufferwrap) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_bufferwrap_t, bufferwrap);
#ifndef NDEBUG
  iotjs_jval_t jbuf = iotjs_bufferwrap_jbuffer(bufferwrap);
  iotjs_jval_t jlength =
      iotjs_jval_get_property(&jbuf, IOTJS_MAGIC_STRING_LENGTH);
  size_t length = iotjs_jval_as_number(&jlength);
  IOTJS_ASSERT(length == _this->length);
  iotjs_jval_destroy(&jbuf);
  iotjs_jval_destroy(&jlength);
#endif
  return _this->length;
}
Пример #6
0
static void iotjs_adc_set_configuration(iotjs_adc_t* adc,
                                        const iotjs_jval_t* jconfiguration) {
#if defined(__linux__)
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_adc_t, adc);
  iotjs_jval_t jdevice =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_DEVICE);
  _this->device = iotjs_jval_as_string(&jdevice);
  iotjs_jval_destroy(&jdevice);
#elif defined(__NUTTX__)
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_adc_t, adc);
  iotjs_jval_t jpin =
      iotjs_jval_get_property(jconfiguration, IOTJS_MAGIC_STRING_PIN);
  _this->pin = iotjs_jval_as_number(&jpin);
  iotjs_jval_destroy(&jpin);
#endif
}
Пример #7
0
static void iotjs_spi_set_configuration(iotjs_spi_t* spi,
                                        const iotjs_jval_t* joptions) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_spi_t, spi);

#if defined(__linux__)
  iotjs_jval_t jdevice =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_DEVICE);
  _this->device = iotjs_jval_as_string(&jdevice);
  iotjs_jval_destroy(&jdevice);
#elif defined(__NUTTX__) || defined(__TIZENRT__)
  iotjs_jval_t jbus = iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BUS);
  _this->bus = iotjs_jval_as_number(&jbus);
  iotjs_jval_destroy(&jbus);
#endif
  iotjs_jval_t jmode =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MODE);
  _this->mode = (SpiMode)iotjs_jval_as_number(&jmode);
  iotjs_jval_destroy(&jmode);

  iotjs_jval_t jchip_select =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_CHIPSELECT);
  _this->chip_select = (SpiChipSelect)iotjs_jval_as_number(&jchip_select);
  iotjs_jval_destroy(&jchip_select);

  iotjs_jval_t jmax_speed =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MAXSPEED);
  _this->max_speed = iotjs_jval_as_number(&jmax_speed);
  iotjs_jval_destroy(&jmax_speed);

  iotjs_jval_t jbits_per_word =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITSPERWORD);
  _this->bits_per_word = (SpiOrder)iotjs_jval_as_number(&jbits_per_word);
  iotjs_jval_destroy(&jbits_per_word);

  iotjs_jval_t jbit_order =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITORDER);
  _this->bit_order = (SpiOrder)iotjs_jval_as_number(&jbit_order);
  iotjs_jval_destroy(&jbit_order);

  iotjs_jval_t jloopback =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_LOOPBACK);
  _this->loopback = iotjs_jval_as_boolean(&jloopback);
  iotjs_jval_destroy(&jloopback);
}
Пример #8
0
static jerry_value_t gpio_set_configuration(iotjs_gpio_t* gpio,
                                            jerry_value_t jconfigurable) {
  jerry_value_t jpin =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_PIN);
  gpio->pin = iotjs_jval_as_number(jpin);
  jerry_release_value(jpin);

  // Direction
  jerry_value_t jdirection =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_DIRECTION);

  if (jerry_value_is_undefined(jdirection)) {
    gpio->direction = kGpioDirectionOut;
  } else {
    if (jerry_value_is_number(jdirection)) {
      gpio->direction = (GpioDirection)iotjs_jval_as_number(jdirection);
    } else {
      gpio->direction = __kGpioDirectionMax;
    }
    if (gpio->direction >= __kGpioDirectionMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - gpio.direction should be DIRECTION.IN or OUT");
    }
  }
  jerry_release_value(jdirection);

  // Mode
  jerry_value_t jmode =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_MODE);

  if (jerry_value_is_undefined(jmode)) {
    gpio->mode = kGpioModeNone;
  } else {
    if (jerry_value_is_number(jmode)) {
      gpio->mode = (GpioMode)iotjs_jval_as_number(jmode);
    } else {
      gpio->mode = __kGpioModeMax;
    }
    if (gpio->mode >= __kGpioModeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.mode should be MODE.NONE, "
                             "PULLUP, PULLDOWN, FLOAT, PUSHPULL or OPENDRAIN");

    } else if (gpio->direction == kGpioDirectionIn &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModePullup &&
               gpio->mode != kGpioModePulldown) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.IN only supports "
                             "MODE.NONE, PULLUP and PULLDOWN");

    } else if (gpio->direction == kGpioDirectionOut &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModeFloat &&
               gpio->mode != kGpioModePushpull &&
               gpio->mode != kGpioModeOpendrain) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.OUT only supports "
                             "MODE.NONE, FLOAT, PUSHPULL and OPENDRAIN");
    }
  }
  jerry_release_value(jmode);

  // Edge
  jerry_value_t jedge =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_EDGE);

  if (jerry_value_is_undefined(jedge)) {
    gpio->edge = kGpioEdgeNone;
  } else {
    if (jerry_value_is_number(jedge)) {
      gpio->edge = (GpioEdge)iotjs_jval_as_number(jedge);
    } else {
      gpio->edge = __kGpioEdgeMax;
    }
    if (gpio->edge >= __kGpioEdgeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.edge should be EDGE.NONE, "
                             "RISING, FALLING or BOTH");
    }
  }
  jerry_release_value(jedge);

  return jerry_create_undefined();
}
Пример #9
0
/* Default configuration:
 *{
 *  mode: spi.MODE[0],
 *  chipSelect: spi.CHIPSELECT.NONE,
 *  maxSpeed: 500000,
 *  bitsPerWord: 8,
 *  bitOrder: spi.BITORDER.MSB,
 *  loopback: false
 * }
 */
static jerry_value_t spi_set_configuration(iotjs_spi_t* spi,
                                           jerry_value_t joptions) {
  jerry_value_t jmode =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MODE);
  if (jerry_value_is_undefined(jmode)) {
    spi->mode = kSpiMode_0;
  } else {
    if (jerry_value_is_number(jmode)) {
      spi->mode = (SpiMode)iotjs_jval_as_number(jmode);
    } else {
      spi->mode = __kSpiModeMax;
    }

    if (spi->mode >= __kSpiModeMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - mode should be MODE[0], [1], [2] or [3]");
    }
  }
  jerry_release_value(jmode);

  jerry_value_t jchip_select =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_CHIPSELECT);
  if (jerry_value_is_undefined(jchip_select)) {
    spi->chip_select = kSpiCsNone;
  } else {
    if (jerry_value_is_number(jchip_select)) {
      spi->chip_select = (SpiChipSelect)iotjs_jval_as_number(jchip_select);
    } else {
      spi->chip_select = __kSpiCsMax;
    }

    if (spi->chip_select >= __kSpiCsMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - chipSelect should be CHIPSELECT.NONE or HIGH");
    }
  }
  jerry_release_value(jchip_select);

  jerry_value_t jmax_speed =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MAXSPEED);
  if (jerry_value_is_undefined(jmax_speed)) {
    spi->max_speed = 500000;
  } else {
    if (!jerry_value_is_number(jmax_speed)) {
      return JS_CREATE_ERROR(TYPE, "Bad arguments - maxSpeed should be Number");
    }
    spi->max_speed = iotjs_jval_as_number(jmax_speed);
  }
  jerry_release_value(jmax_speed);

  jerry_value_t jbits_per_word =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITSPERWORD);
  if (jerry_value_is_undefined(jbits_per_word)) {
    spi->bits_per_word = 8;
  } else {
    if (jerry_value_is_number(jbits_per_word)) {
      spi->bits_per_word = iotjs_jval_as_number(jbits_per_word);
    } else {
      spi->bits_per_word = 0;
    }

    if (spi->bits_per_word != 8 && spi->bits_per_word != 9) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - bitsPerWord should be 8 or 9");
    }
  }
  jerry_release_value(jbits_per_word);

  jerry_value_t jbit_order =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITORDER);
  if (jerry_value_is_undefined(jbit_order)) {
    spi->bit_order = kSpiOrderMsb;
  } else {
    if (jerry_value_is_number(jbit_order)) {
      spi->bit_order = (SpiOrder)iotjs_jval_as_number(jbit_order);
    } else {
      spi->bit_order = __kSpiOrderMax;
    }

    if (spi->bit_order >= __kSpiOrderMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - bitOrder should be BITORDER.MSB or LSB");
    }
  }
  jerry_release_value(jbit_order);

  jerry_value_t jloopback =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_LOOPBACK);
  if (jerry_value_is_undefined(jloopback)) {
    spi->loopback = false;
  } else {
    if (!jerry_value_is_boolean(jloopback)) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - loopback should be Boolean");
    }
    spi->loopback = iotjs_jval_as_boolean(jloopback);
  }
  jerry_release_value(jloopback);

  return jerry_create_undefined();
}