// WritePin sets a given pin High or Low
// by setting the clear or set registers respectively
503042
func WritePin(pin Pin, state State) {
	p := uint8(pin)

	// Set register, 7 / 8 depending on bank
	// Clear register, 10 / 11 depending on bank
	setReg := p/32 + 7
	clearReg := p/32 + 10

	memlock.Lock()

	if state == Low {
		gpioMem[clearReg] = 1 << (p & 31)
	} else {
		gpioMem[setReg] = 1 << (p & 31)
	}
	memlock.Unlock() // not deferring saves ~600ns
}
1114724
func (b *IOPinDriver) WritePinIOConfig(config int) (err error) {
	b.ioMask = config
	data := &bytes.Buffer{}
	binary.Write(data, binary.LittleEndian, uint32(config))
	err = b.adaptor().WriteCharacteristic(pinIOConfigCharacteristic, data.Bytes())
	return
}
526723
func (o *LargeObject) Write(p []byte) (int, error) {
	n, err := fpInt32(o.lo.fp.CallFn("lowrite", []fpArg{fpIntArg(o.fd), p}))
	return int(n), err
}
596856
func (m *MCP23017Driver) write(reg uint8, pin uint8, val uint8) (err error) {
	if debug {
		log.Printf("write: MCP address: 0x%X, register:0x%X,value: 0x%X\n", m.GetAddressOrDefault(mcp23017Address), reg, val)
	}
	if _, err = m.connection.Write([]uint8{reg, val}); err != nil {
		return err
	}
	return nil
}
645557
func (s *Session) verifyPin(pin []byte) error {
	if _, err := s.Channel.transmitEncrypted(claSCWallet, insVerifyPin, 0, 0, pin); err != nil {
		return err
	}
	s.verified = true
	return nil
}
1177633
func AssignPins(pins PinList, module Module) error {
	for _, pin := range pins {
		e := AssignPin(pin, module)
		if e != nil {
			return e
		}
	}
	return nil
}
929079
func (c Conn) CloseWrite() error {
	if cw, ok := c.Wire.(interface {
		CloseWrite() error
	}); ok {
		return cw.CloseWrite()
	}
	return errors.New("pinned: underlying connection does not support CloseWrite")
}
958417
func (api *Client) AddPin(channel string, item ItemRef) error {
	return api.AddPinContext(context.Background(), channel, item)
}
628656
func (s *State232) Write(p []byte) (nn int, err error) {
	l := len(p)
	s.clen += l
	s.tail = append(s.tail, p...)
	return l, nil
}
224463
func (c *Client) currentPin() (string, string) {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.leader, c.pinned
}
1021232
func (b *IOPinDriver) WritePinADConfig(config int) (err error) {
	b.adMask = config
	data := &bytes.Buffer{}
	binary.Write(data, binary.LittleEndian, uint32(config))
	err = b.adaptor().WriteCharacteristic(pinADConfigCharacteristic, data.Bytes())
	return
}
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)
	}
}
911414
func (p Pin) Close() {
	if p.f != nil {
		p.f.Close()
		p.f = nil
	}
}
405551
func (e *Adaptor) PwmWrite(pin string, val byte) (err error) {
	pwmPin, err := e.PWMPin(pin)
	if err != nil {
		return
	}
	period, err := pwmPin.Period()
	if err != nil {
		return err
	}
	duty := gobot.FromScale(float64(val), 0, 255.0)
	return pwmPin.SetDutyCycle(uint32(float64(period) * duty))
}
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
}
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
}
223644
func (s *WriterSender) Write(p []byte) (int, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	n, err := s.writer.Write(p)
	if err != nil {
		return n, err
	}
	_ = s.writer.Flush()

	if s.buffer.Len() > 80 {
		err = s.doSend()
	}

	return n, err
}