Ejemplo n.º 1
0
    init(() => {
      var pinMappings = getPins();
      this[pins] = (Object.keys(pinMappings).map((pin) => {
        var pinInfo = pinMappings[pin];
        var supportedModes = [ INPUT_MODE, OUTPUT_MODE ];
        if (pinInfo.peripherals.indexOf('pwm') != -1) {
          supportedModes.push(PWM_MODE, SERVO_MODE);
        }
        var instance = this[instances][pin] = {
          peripheral: new DigitalInput(pin),
          mode: INPUT_MODE,
          previousWrittenValue: LOW
        };
        return Object.create(null, {
          supportedModes: {
            enumerable: true,
            value: Object.freeze(supportedModes)
          },
          mode: {
            enumerable: true,
            get() {
              return instance.mode;
            }
          },
          value: {
            enumerable: true,
            get() {
              switch(instance.mode) {
                case INPUT_MODE:
                  return instance.peripheral.read();
                  break;
                case OUTPUT_MODE:
                  return instance.previousWrittenValue;
                  break;
              }
            },
            set(value) {
              if (instance.mode == OUTPUT_MODE) {
                instance.peripheral.write(value);
              }
            }
          },
          report: {
            enumerable: true,
            value: 1
          },
          analogChannel: {
            enumerable: true,
            value: 127
          }
        });
      }));

      this[isReady] = true;
      this.emit('ready');
      this.emit('connect');
    });
Ejemplo n.º 2
0
 pinToGPIO(pin) {
   const pins = getPins()[pin].pins;
   const gpioRegex = /GPIO([\d]+)/;
 
   let result;
   let i;
   
   for (i = 0; i < pins.length; i++) {
     result = gpioRegex.exec(pins[i]);
     if (result !== null) {
       return parseInt(result[1],10);
     }
   }
   return null;
 }
Ejemplo n.º 3
0
    init(() => {
      const pinMappings = getPins();
      this[pins] = [];

      // Slight hack to get the LED in there, since it's not actually a pin
      pinMappings[LED_PIN] = {
        pins: [ LED_PIN ],
        peripherals: [ 'gpio' ]
      };

      Object.keys(pinMappings).forEach((pin) => {
        const pinInfo = pinMappings[pin];
        const supportedModes = [ INPUT_MODE, OUTPUT_MODE ];
        if (pinInfo.peripherals.indexOf('pwm') != -1) {
          supportedModes.push(PWM_MODE, SERVO_MODE);
        }
        const instance = this[instances][pin] = {
          peripheral: null,
          mode: UNKNOWN_MODE,
          previousWrittenValue: LOW
        };
        this[pins][pin] = Object.create(null, {
          supportedModes: {
            enumerable: true,
            value: Object.freeze(supportedModes)
          },
          mode: {
            enumerable: true,
            get() {
              return instance.mode;
            }
          },
          value: {
            enumerable: true,
            get() {
              switch (instance.mode) {
                case INPUT_MODE:
                  return instance.peripheral.read();
                case OUTPUT_MODE:
                  return instance.previousWrittenValue;
                default:
                  return null;
              }
            },
            set(value) {
              if (instance.mode == OUTPUT_MODE) {
                instance.peripheral.write(value);
              }
            }
          },
          report: {
            enumerable: true,
            value: 1
          },
          analogChannel: {
            enumerable: true,
            value: 127
          }
        });
      });

      // Fill in the holes, sins pins are sparse on the A+/B+/2
      for (let i = 0; i < this[pins].length; i++) {
        if (!this[pins][i]) {
          this[pins][i] = Object.create(null, {
            supportedModes: {
              enumerable: true,
              value: Object.freeze([])
            },
            mode: {
              enumerable: true,
              get() {
                return UNKNOWN_MODE;
              }
            },
            value: {
              enumerable: true,
              get() {
                return 0;
              },
              set() {}
            },
            report: {
              enumerable: true,
              value: 1
            },
            analogChannel: {
              enumerable: true,
              value: 127
            }
          });
        }
      }

      this[isReady] = true;
      this.emit('ready');
      this.emit('connect');
    });
Ejemplo n.º 4
0
function RaspiIO({ includePins, excludePins, enableSerial, enableI2C = true } = {}) {
    const options = {
        pluginName: 'Raspi IO',
        pinInfo: clone(raspi_board_1.getPins()),
        platform: {
            base: raspi_1.module,
            gpio: raspi_gpio_1.module,
            led: raspi_led_1.module,
            pwm: raspi_soft_pwm_1.module
        }
    };
    if (enableI2C) {
        options.platform.i2c = raspi_i2c_1.module;
        options.i2cIds = {
            DEFAULT: raspi_board_1.getBoardRevision() === raspi_board_1.VERSION_1_MODEL_B_REV_1 ? 0 : 1
        };
    }
    if (typeof enableSerial === 'undefined') {
        enableSerial =
            raspi_board_1.getBoardRevision() !== raspi_board_1.VERSION_3_MODEL_B &&
                raspi_board_1.getBoardRevision() !== raspi_board_1.VERSION_3_MODEL_A_PLUS &&
                raspi_board_1.getBoardRevision() !== raspi_board_1.VERSION_3_MODEL_B_PLUS &&
                raspi_board_1.getBoardRevision() !== raspi_board_1.VERSION_1_MODEL_ZERO_W;
    }
    if (enableSerial) {
        options.platform.serial = raspi_serial_1.module;
        options.serialIds = {
            DEFAULT: raspi_serial_1.DEFAULT_PORT
        };
    }
    // Clone the pins so the internal object in raspi-board isn't mutated
    if (Array.isArray(includePins)) {
        const newPinMappings = {};
        for (const pin of includePins) {
            const normalizedPin = raspi_board_1.getPinNumber(pin);
            if (normalizedPin === null) {
                throw new Error(`Invalid pin "${pin}" specified in includePins`);
            }
            newPinMappings[normalizedPin] = options.pinInfo[normalizedPin];
        }
        options.pinInfo = newPinMappings;
    }
    else if (Array.isArray(excludePins)) {
        for (const pin of excludePins) {
            const normalizedPin = raspi_board_1.getPinNumber(pin);
            if (normalizedPin === null) {
                throw new Error(`Invalid pin "${pin}" specified in excludePins`);
            }
            delete options.pinInfo[normalizedPin];
        }
    }
    // I2C pins need to be dedicated in Raspi IO, so filter out any peripherals other than I2C on I2C pins
    if (enableI2C) {
        for (const pin in options.pinInfo) {
            if (options.pinInfo[pin].peripherals.indexOf(j5_io_types_1.PeripheralType.I2C) !== -1) {
                options.pinInfo[pin].peripherals = [j5_io_types_1.PeripheralType.I2C];
            }
        }
    }
    // UART pins need to be dedicated in Raspi IO, so filter out any peripherals other than UART on UART pins
    if (enableSerial) {
        for (const pin in options.pinInfo) {
            if (options.pinInfo[pin].peripherals.indexOf(j5_io_types_1.PeripheralType.UART) !== -1) {
                options.pinInfo[pin].peripherals = [j5_io_types_1.PeripheralType.UART];
            }
        }
    }
    return new j5_io_1.J5IO(options);
}