Exemple #1
0
JObject* InitGpio() {
    Module* module = GetBuiltinModule(MODULE_GPIO);
    JObject* jgpio = module->module;

    if (jgpio == NULL) {
        jgpio = new JObject();

        jgpio->SetMethod("initialize", Initialize);
        jgpio->SetMethod("release", Release);
        jgpio->SetMethod("setPin", SetPin);
        jgpio->SetMethod("writePin", WritePin);
        jgpio->SetMethod("readPin", ReadPin);
        jgpio->SetMethod("setPort", SetPort);
        jgpio->SetMethod("writePort", WritePort);
        jgpio->SetMethod("readPort", ReadPort);
        jgpio->SetMethod("query", Query);

#define SET_CONSTANT(object, name, constant) \
  do { \
    JObject value(constant); \
    object->SetProperty(name, value); \
  } while (0)

        SET_CONSTANT(jgpio, "kGpioDirectionNone", kGpioDirectionNone);
        SET_CONSTANT(jgpio, "kGpioDirectionIn", kGpioDirectionIn);
        SET_CONSTANT(jgpio, "kGpioDirectionOut", kGpioDirectionOut);

        SET_CONSTANT(jgpio, "kGpioModeNone", kGpioModeNone);
        SET_CONSTANT(jgpio, "kGpioModePullup", kGpioModePullup);
        SET_CONSTANT(jgpio, "kGpioModePulldown", kGpioModePulldown);
        SET_CONSTANT(jgpio, "kGpioModeFloat", kGpioModeFloat);
        SET_CONSTANT(jgpio, "kGpioModePushpull", kGpioModePushpull);
        SET_CONSTANT(jgpio, "kGpioModeOpendrain", kGpioModeOpendrain);

        SET_CONSTANT(jgpio, "kGpioErrOk", kGpioErrOk);
        SET_CONSTANT(jgpio, "kGpioErrInitialize", kGpioErrInitialize);
        SET_CONSTANT(jgpio, "kGpioErrNotInitialized", kGpioErrNotInitialized);
        SET_CONSTANT(jgpio, "kGpioErrWrongUse", kGpioErrWrongUse);
        SET_CONSTANT(jgpio, "kGpioErrSysErr", kGpioErrSys);

#undef SET_CONSTANT

        Gpio* gpio = Gpio::Create(*jgpio);
        IOTJS_ASSERT(gpio == reinterpret_cast<Gpio*>(jgpio->GetNative()));

        module->module = jgpio;
    }

    return jgpio;
}
JHANDLER_FUNCTION(Buffer, handler) {
  IOTJS_ASSERT(handler.GetThis()->IsObject());
  IOTJS_ASSERT(handler.GetArgLength() == 2);
  IOTJS_ASSERT(handler.GetArg(0)->IsObject());
  IOTJS_ASSERT(handler.GetArg(1)->IsNumber());

  int length = handler.GetArg(1)->GetInt32();
  JObject* jbuffer = handler.GetArg(0);
  JObject* jbuiltin = handler.GetThis();

  BufferWrap* buffer_wrap = new BufferWrap(*jbuffer, *jbuiltin, length);
  IOTJS_ASSERT(buffer_wrap == (BufferWrap*)(jbuiltin->GetNative()));
  IOTJS_ASSERT(buffer_wrap->buffer() != NULL);

  return true;
}