예제 #1
0
파일: jsi2c.c 프로젝트: nkolban/Espruino
static bool i2c_rd_bit(i2cInfo *inf) {
  jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA
  dly(inf);
  jshPinSetValue(inf->pinSCL, 1); // stop forcing SDA
  int timeout = I2C_TIMEOUT;
  while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
  if (!timeout) err("Timeout (rd)");
  dly(inf);
  bool b = jshPinGetValue(inf->pinSDA);
  jshPinSetValue(inf->pinSCL, 0);
  return b;
}
예제 #2
0
파일: jsi2c.c 프로젝트: nkolban/Espruino
static void i2c_stop(i2cInfo *inf) {
  jshPinSetValue(inf->pinSDA, 0);
  dly(inf);
  jshPinSetValue(inf->pinSCL, 1);
  int timeout = I2C_TIMEOUT;
  while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
  if (!timeout) err("Timeout (stop)");
  dly(inf);
  jshPinSetValue(inf->pinSDA, 1);
  dly(inf);
  if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (stop)");
  dly(inf);
  inf->started = false;
}
예제 #3
0
bool jshGetWatchedPinState(IOEventFlags device) {
  Pin i;
  for (i=0;i<JSH_PIN_COUNT;i++)
    if (gpioEventFlags[i]==device)
      return jshPinGetValue(i);
  return false;
}
예제 #4
0
IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
  if (jshIsPinValid(pin)) {
    IOEventFlags exti = getNewEVEXTI();
    if (shouldWatch) {
      if (exti) {
        gpioEventFlags[pin] = exti;
        jshPinSetState(pin, JSHPINSTATE_GPIO_IN);
#ifdef SYSFS_GPIO_DIR
        gpioShouldWatch[pin] = true;
        gpioLastState[pin] = jshPinGetValue(pin);
#endif
#ifdef USE_WIRINGPI
        wiringPiISR(pin, INT_EDGE_BOTH, irqEXTIs[exti-EV_EXTI0]);
#endif
      } else 
        jsError("You can only have a maximum of 16 watches!");
    }
    if (!shouldWatch || !exti) {
      gpioEventFlags[pin] = 0;
#ifdef SYSFS_GPIO_DIR
      gpioShouldWatch[pin] = false;
#endif
#ifdef USE_WIRINGPI
      wiringPiISR(pin, INT_EDGE_BOTH, irqEXTIDoNothing);
#endif

    }
    return shouldWatch ? exti : EV_NONE;
  } else jsError("Invalid pin!");
  return EV_NONE;
}
예제 #5
0
void jshInputThread() {
  while (isInitialised) {
    bool shortSleep = false;
    /* Handle the delayed Ctrl-C -> interrupt behaviour (see description by EXEC_CTRL_C's definition)  */
    if (execInfo.execute & EXEC_CTRL_C_WAIT)
      execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C_WAIT) | EXEC_INTERRUPTED;
    if (execInfo.execute & EXEC_CTRL_C)
      execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C) | EXEC_CTRL_C_WAIT;
    // Read from the console
    while (kbhit()) {
      int ch = getch();
      if (ch<0) break;
      jshPushIOCharEvent(EV_USBSERIAL, (char)ch);
    }
    // Read from any open devices - if we have space
    if (jshGetEventsUsed() < IOBUFFERMASK/2) {
      int i;
      for (i=0;i<=EV_DEVICE_MAX;i++) {
        if (ioDevices[i]) {
          char buf[32];
          // read can return -1 (EAGAIN) because O_NONBLOCK is set
          int bytes = (int)read(ioDevices[i], buf, sizeof(buf));
          if (bytes>0) {
            //int j; for (j=0;j<bytes;j++) printf("]] '%c'\r\n", buf[j]);
            jshPushIOCharEvents(i, buf, (unsigned int)bytes);
            shortSleep = true;
          }
        }
      }
    }
    // Write any data we have
    IOEventFlags device = jshGetDeviceToTransmit();
    while (device != EV_NONE) {
      char ch = (char)jshGetCharToTransmit(device);
      //printf("[[ '%c'\r\n", ch);
      if (ioDevices[device]) {
        write(ioDevices[device], &ch, 1);
        shortSleep = true;
      }
      device = jshGetDeviceToTransmit();
    }


#ifdef SYSFS_GPIO_DIR
    Pin pin;
    for (pin=0;pin<JSH_PIN_COUNT;pin++)
      if (gpioShouldWatch[pin]) {
        shortSleep = true;
        bool state = jshPinGetValue(pin);
        if (state != gpioLastState[pin]) {
          jshPushIOEvent(pinToEVEXTI(pin) | (state?EV_EXTI_IS_HIGH:0), jshGetSystemTime());
          gpioLastState[pin] = state;
        }
      }
#endif

    usleep(shortSleep ? 1000 : 50000);
  }
}
예제 #6
0
파일: jsi2c.c 프로젝트: nkolban/Espruino
static void i2c_start(i2cInfo *inf) {
  if (inf->started) {
    // reset
    jshPinSetValue(inf->pinSDA, 1);
    dly(inf);
    jshPinSetValue(inf->pinSCL, 1);
    int timeout = I2C_TIMEOUT;
    while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
    if (!timeout) err("Timeout (start)");
    dly(inf);
  }
  if (!jshPinGetValue(inf->pinSDA)) err("Arbitration (start)");
  jshPinSetValue(inf->pinSDA, 0);
  dly(inf);
  jshPinSetValue(inf->pinSCL, 0);
  dly(inf);
  inf->started = true;
}
예제 #7
0
bool jshPinInput(Pin pin) {
  bool value = false;
  if (jshIsPinValid(pin)) {
    jshPinSetState(pin, JSHPINSTATE_GPIO_IN);

    value = jshPinGetValue(pin);
  } else jsError("Invalid pin!");
  return value;
}
예제 #8
0
bool jshGetWatchedPinState(IOEventFlags device) {
#ifdef SYSFS_GPIO_DIR
  Pin i;
  for (i=0;i<JSH_PIN_COUNT;i++)
    if (gpioEventFlags[i]==device)
      return jshPinGetValue(i);
#endif
  return false;
}
예제 #9
0
bool jshPinInput(Pin pin) {
  bool value = false;
  if (jshIsPinValid(pin)) {
    if (!jshGetPinStateIsManual(pin))
      jshPinSetState(pin, JSHPINSTATE_GPIO_IN);

    value = jshPinGetValue(pin);
  } else jsExceptionHere(JSET_ERROR, "Invalid pin!");
  return value;
}
예제 #10
0
void jshPinWatch(Pin pin, bool shouldWatch) {
  if (jshIsPinValid(pin)) {
#ifdef SYSFS_GPIO_DIR
    gpioShouldWatch[pin] = shouldWatch;
    if (shouldWatch) {
      jshPinSetState(pin, JSHPINSTATE_GPIO_IN);
      gpioLastState[pin] = jshPinGetValue(pin);
    }
#endif
  } else jsError("Invalid pin!");
}
예제 #11
0
파일: jsi2c.c 프로젝트: nkolban/Espruino
static void i2c_wr_bit(i2cInfo *inf, bool b) {
  jshPinSetValue(inf->pinSDA, b);
  dly(inf);
  jshPinSetValue(inf->pinSCL, 1);
  dly(inf);
  int timeout = I2C_TIMEOUT;
  while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
  if (!timeout) err("Timeout (wr)");
  jshPinSetValue(inf->pinSCL, 0);
  jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA (needed?)
}
예제 #12
0
/** Reset one-wire, return true if a device was present */
static bool NO_INLINE OneWireReset(Pin pin) {
  jshPinSetState(pin, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
  //jshInterruptOff();
  jshPinSetValue(pin, 0);
  jshDelayMicroseconds(500);
  jshPinSetValue(pin, 1);
  jshDelayMicroseconds(80);
  bool hasDevice = !jshPinGetValue(pin);
  //jshInterruptOn();
  jshDelayMicroseconds(420);
  return hasDevice;
}
예제 #13
0
void jshIdle() {
  while (kbhit()) {
    jshPushIOCharEvent(EV_USBSERIAL, (char)getch());
  }

#ifdef SYSFS_GPIO_DIR
  Pin pin;
  for (pin=0;pin<JSH_PIN_COUNT;pin++)
    if (gpioShouldWatch[pin]) {
      bool state = jshPinGetValue(pin);
      if (state != gpioLastState[pin]) {
        jshPushIOEvent(pinToEVEXTI(pin) | (state?EV_EXTI_IS_HIGH:0), jshGetSystemTime());
        gpioLastState[pin] = state;
      }
    }
#endif
}
예제 #14
0
/** Write 'bits' bits, and return what was read (to read, you must send all 1s) */
static JsVarInt NO_INLINE OneWireRead(Pin pin, int bits) {
  jshPinSetState(pin, JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
  JsVarInt result = 0;
  JsVarInt mask = 1;
  while (bits-- > 0) {
    jshInterruptOff();
    jshPinSetValue(pin, 0);
    jshDelayMicroseconds(3);
    jshPinSetValue(pin, 1);
    jshDelayMicroseconds(10); // leave time to let it rise
    if (jshPinGetValue(pin))
      result = result | mask;
    jshInterruptOn();
    jshDelayMicroseconds(53);
    mask = mask << 1;
  }

  return result;
}
예제 #15
0
파일: main.c 프로젝트: CWBudde/Espruino
int main() {

  jshInit();

  bool buttonState = false;
#ifdef BTN1_PININDEX
  buttonState = jshPinGetValue(BTN1_PININDEX) == BTN1_ONSTATE;
#endif
  jsvInit(0);
  jsiInit(!buttonState /* load from flash by default */); // pressing USER button skips autoload

  while (1) 
  {
    jsiLoop();
  }
  //jsiKill();
  //jsvKill();
  //jshKill();
}
예제 #16
0
void jshPinWatch(Pin pin, bool shouldWatch) {
  if (jshIsPinValid(pin)) {
#ifdef SYSFS_GPIO_DIR
    IOEventFlags exti = getNewEVEXTI();
    if (shouldWatch) {
      if (exti) {
        gpioShouldWatch[pin] = true;
        gpioEventFlags[pin] = exti;
        jshPinSetState(pin, JSHPINSTATE_GPIO_IN);
        gpioLastState[pin] = jshPinGetValue(pin);
      } else 
        jsError("You can only have a maximum of 16 watches!");
    }
    if (!shouldWatch || !exti) {
      gpioShouldWatch[pin] = false;
      gpioEventFlags[pin] = 0;
    }
#endif
  } else jsError("Invalid pin!");
}
예제 #17
0
파일: board_spi.c 프로젝트: Hmaal/Espruino
long ReadWlanInterruptPin(void)
{
    return jshPinGetValue(WLAN_IRQ_PIN);
}
예제 #18
0
파일: board_spi.c 프로젝트: gbraad/Espruino
long cc3000_read_irq_pin(void)
{
    return jshPinGetValue(WLAN_IRQ_PIN);
예제 #19
0
bool jshPinInput(Pin pin) {
  if (jshIsPinValid(pin))
    return jshPinGetValue(pin);
  return false;
}