// OpenPin returns a gpio.Pin implementation specalised for the RPi.
979145
func OpenPin(number int, mode gpio.Mode) (gpio.Pin, error) {
	initOnce.Do(initRPi)
	p, err := gpio.OpenPin(number, mode)
	return &pin{Pin: p, pin: uint8(number)}, err
}
81339
func (e *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) {
	e.mutex.Lock()
	defer e.mutex.Unlock()

	i := e.pinmap[pin]
	if e.digitalPins[i.pin] == nil {
		e.digitalPins[i.pin], err = e.newExportedPin(i.pin)
		if err != nil {
			return
		}

		if i.resistor > 0 {
			e.digitalPins[i.resistor], err = e.newExportedPin(i.resistor)
			if err != nil {
				return
			}
		}

		if i.levelShifter > 0 {
			e.digitalPins[i.levelShifter], err = e.newExportedPin(i.levelShifter)
			if err != nil {
				return
			}
		}

		if len(i.mux) > 0 {
			for _, mux := range i.mux {
				e.digitalPins[mux.pin], err = e.newExportedPin(mux.pin)
				if err != nil {
					return
				}

				err = pinWrite(e.digitalPins[mux.pin], sysfs.OUT, mux.value)
				if err != nil {
					return
				}
			}
		}
	}

	if dir == "in" {
		if err = e.digitalPins[i.pin].Direction(sysfs.IN); err != nil {
			return
		}

		if i.resistor > 0 {
			err = pinWrite(e.digitalPins[i.resistor], sysfs.OUT, sysfs.LOW)
			if err != nil {
				return
			}
		}

		if i.levelShifter > 0 {
			err = pinWrite(e.digitalPins[i.levelShifter], sysfs.OUT, sysfs.LOW)
			if err != nil {
				return
			}
		}
	} else if dir == "out" {
		if err = e.digitalPins[i.pin].Direction(sysfs.OUT); err != nil {
			return
		}

		if i.resistor > 0 {
			if err = e.digitalPins[i.resistor].Direction(sysfs.IN); err != nil {
				return
			}
		}

		if i.levelShifter > 0 {
			err = pinWrite(e.digitalPins[i.levelShifter], sysfs.OUT, sysfs.HIGH)
			if err != nil {
				return
			}
		}
	}
	return e.digitalPins[i.pin], nil
}
1141841
func (p *pinner) IsPinned(c cid.Cid) (string, bool, error) {
	p.lock.RLock()
	defer p.lock.RUnlock()
	return p.isPinnedWithType(c, Any)
}
473789
func (module *BBPWMModule) makeOpenPin(pin Pin) (*BBPWMModuleOpenPin, error) {
	p := module.definedPins[pin]
	if p == nil {
		return nil, fmt.Errorf("Pin %d is not known to PWM module %s", pin, module.GetName())
	}

	e := AssignPin(pin, module)
	if e != nil {
		return nil, e
	}

	// Ensure that the cape manager knows about it
	e = module.ensureSlot(p.overlayName())
	if e != nil {
		return nil, e
	}

	dir := p.deviceDir()
	result := &BBPWMModuleOpenPin{pin: pin}
	result.periodFile = dir + "period"
	result.dutyFile = dir + "duty"
	result.runFile = dir + "run"
	result.polarityFile = dir + "polarity"

	module.openPins[pin] = result

	// ensure polarity is 0, so that the duty time represents the time the signal is high.
	e = WriteStringToFile(result.polarityFile, "0")
	if e != nil {
		return nil, e
	}

	return result, nil
}
65974
func WithDCPin(val string) func(Config) {
	return func(c Config) {
		d, ok := c.(*SSD1306Driver)
		if ok {
			d.DCPin = val
		} else {
			panic("unable to set dc pin for ssd1306")
		}
	}
}
102362
func AssignPin(pin Pin, module Module) error {
	if a := assignedPins[pin]; a != nil {
		return fmt.Errorf("Pin %d is already assigned to module %s", pin, a.module.GetName())
	}
	assignedPins[pin] = &assignedPin{pin, module}
	return nil
}
747979
func (api *Client) RemovePin(channel string, item ItemRef) error {
	return api.RemovePinContext(context.Background(), channel, item)
}
51752
func OpenTAP(name string) (int, error) {
	return open(name, syscall.IFF_TAP|syscall.IFF_NO_PI)
}
28365
func (b *boundManager) Pin(leaseName string, entity string) error {
	return errors.Trace(b.pinOp(leaseName, entity, b.manager.pins))
}
1032319
func (p *pinner) Unpin(ctx context.Context, c cid.Cid, recursive bool) error {
	p.lock.Lock()
	defer p.lock.Unlock()
	reason, pinned, err := p.isPinnedWithType(c, Any)
	if err != nil {
		return err
	}
	if !pinned {
		return ErrNotPinned
	}
	switch reason {
	case "recursive":
		if recursive {
			p.recursePin.Remove(c)
			return nil
		}
		return fmt.Errorf("%s is pinned recursively", c)
	case "direct":
		p.directPin.Remove(c)
		return nil
	default:
		return fmt.Errorf("%s is pinned indirectly under %s", c, reason)
	}
}
530618
func (b *IOPinDriver) ReadAllPinData() (pins []PinData) {
	c, _ := b.adaptor().ReadCharacteristic(pinDataCharacteristic)
	buf := bytes.NewBuffer(c)
	pinsData := make([]PinData, buf.Len()/2)

	for i := 0; i < buf.Len()/2; i++ {
		pinData := PinData{}
		pinData.pin, _ = buf.ReadByte()
		pinData.value, _ = buf.ReadByte()
		pinsData[i] = pinData
	}

	return pinsData
}
1045978
func (b *Adaptor) AnalogRead(pin string) (val int, err error) {
	analogPin, err := b.translateAnalogPin(pin)
	if err != nil {
		return
	}
	fi, err := sysfs.OpenFile(fmt.Sprintf("%v/%v", b.analogPath, analogPin), os.O_RDONLY, 0644)
	defer fi.Close()

	if err != nil {
		return
	}

	var buf = make([]byte, 1024)
	_, err = fi.Read(buf)
	if err != nil {
		return
	}

	val, _ = strconv.Atoi(strings.Split(string(buf), "\n")[0])
	return
}
604249
func ClosePin(pin Pin) error {
	gpio, e := GetGPIOModule()
	if e != nil {
		return e
	}

	return gpio.ClosePin(pin)
}
518486
func (c *cipdSubcommand) doneWithPins(pins []pinInfo, err error) int {
	return c.doneWithPinMap(map[string][]pinInfo{"": pins}, err)
}
295263
func (c *Client) GetPin() ([]byte, error) {
	if _, err := c.in.Write([]byte("GETPIN\n")); err != nil {
		return nil, err
	}
	pin, _, err := c.out.ReadLine()
	if err != nil {
		return nil, err
	}
	if bytes.HasPrefix(pin, []byte("OK")) {
		return nil, nil
	}
	if !bytes.HasPrefix(pin, []byte("D ")) {
		return nil, fmt.Errorf("unexpected response: %s", pin)
	}

	ok, _, err := c.out.ReadLine()
	if err != nil {
		return nil, err
	}
	if !bytes.HasPrefix(ok, []byte("OK")) {
		return nil, fmt.Errorf("unexpected response: %s", ok)
	}
	return pin[2:], nil
}
34851
func (r *Resolver) verifyPin(ctx context.Context, pin common.Pin) error {
	promise := r.verifying.Get(ctx, pin, func(ctx context.Context) (interface{}, error) {
		logging.Debugf(ctx, "Validating pin %s...", pin)
		_, err := r.Client.DescribeInstance(ctx, pin, nil)
		if err == nil {
			logging.Debugf(ctx, "Pin %s successfully validated", pin)
		} else {
			logging.Debugf(ctx, "Failed to resolve instance info for %s: %s", pin, err)
		}
		return nil, err
	})
	_, err := promise.Get(ctx)
	return err
}
910228
func (module *BBAnalogModule) AnalogRead(pin Pin) (int, error) {
	var e error

	// Get it if it's already open
	openPin := module.openPins[pin]
	if openPin == nil {
		// If it's not open yet, open on demand
		openPin, e = module.makeOpenAnalogPin(pin)
		// return 0, errors.New("Pin is being read for analog value but has not been opened. Have you called PinMode?")
		if e != nil {
			return 0, e
		}
	}
	return openPin.analogGetValue()
}