// Needs to be called to allocate the GPIO pin
2151
func (op *DTGPIOModuleOpenPin) gpioUnexport() error {
	s := strconv.FormatInt(int64(op.gpioLogical), 10)
	e := WriteStringToFile("/sys/class/gpio/unexport", s)
	if e != nil {
		return e
	}

	return nil
}
953518
func (m *MCP23017Driver) WriteGPIO(pin uint8, val uint8, portStr string) (err error) {
	selectedPort := m.getPort(portStr)
	// read current value of IODIR register
	iodir, err := m.read(selectedPort.IODIR)
	if err != nil {
		return err
	}
	// set pin as output by clearing bit
	iodirVal := clearBit(iodir, uint8(pin))
	// write IODIR register bit
	err = m.write(selectedPort.IODIR, uint8(pin), uint8(iodirVal))
	if err != nil {
		return err
	}
	// read current value of OLAT register
	olat, err := m.read(selectedPort.OLAT)
	if err != nil {
		return err
	}
	// set or clear olat value, 0 is no output, 1 is an output
	var olatVal uint8
	if val == 0 {
		olatVal = clearBit(olat, uint8(pin))
	} else {
		olatVal = setBit(olat, uint8(pin))
	}
	// write OLAT register bit
	err = m.write(selectedPort.OLAT, uint8(pin), uint8(olatVal))
	if err != nil {
		return err
	}
	return nil
}
1100102
func (b *Adaptor) translatePin(pin string) (value int, err error) {
	if val, ok := b.pinMap[pin]; ok {
		value = val
	} else {
		err = errors.New("Not a valid pin")
	}
	return
}
328264
func (d *RaspberryPiDTDriver) createPinData() {
	switch d.BoardRevision() {
	case type0ne:
		d.pinConfigs = []*DTPinConfig{
			{[]string{"null"}, []string{"unassignable"}, 0, 0}, // 0 - spacer
			{[]string{"3.3v"}, []string{"unassignable"}, 0, 0},
			{[]string{"5v"}, []string{"unassignable"}, 0, 0},
			{[]string{"sda"}, []string{"i2c"}, 0, 0},
			{[]string{"do-not-connect-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"scl"}, []string{"i2c"}, 0, 0},
			{[]string{"ground"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio4"}, []string{"gpio"}, 4, 0},
			{[]string{"txd"}, []string{"serial"}, 0, 0},
			{[]string{"do-not-connect-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"rxd"}, []string{"serial"}, 0, 0},
			{[]string{"gpio17"}, []string{"gpio"}, 17, 0},
			{[]string{"gpio18"}, []string{"gpio"}, 18, 0}, // also supports PWM
			{[]string{"gpio21"}, []string{"gpio"}, 21, 0},
			{[]string{"do-not-connect-3"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio22"}, []string{"gpio"}, 22, 0},
			{[]string{"gpio23"}, []string{"gpio"}, 23, 0},
			{[]string{"do-not-connect-4"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio24"}, []string{"gpio"}, 24, 0},
			{[]string{"mosi"}, []string{"spi"}, 0, 0},
			{[]string{"do-not-connect-5"}, []string{"unassignable"}, 0, 0},
			{[]string{"miso"}, []string{"spi"}, 0, 0},
			{[]string{"gpio25"}, []string{"gpio"}, 25, 0},
			{[]string{"sclk"}, []string{"spi"}, 0, 0},
			{[]string{"ce0n"}, []string{"spi"}, 0, 0},
			{[]string{"do-not-connect-6"}, []string{"unassignable"}, 0, 0},
			{[]string{"ce1n"}, []string{"spi"}, 0, 0},
		}
	case typeTwo:
		d.pinConfigs = []*DTPinConfig{
			{[]string{"null"}, []string{"unassignable"}, 0, 0}, // 0 - spacer
			{[]string{"3.3v-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"5v-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"sda"}, []string{"i2c"}, 0, 0},
			{[]string{"5v-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"scl"}, []string{"i2c"}, 0, 0},
			{[]string{"ground-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio4"}, []string{"gpio"}, 4, 0},
			{[]string{"txd"}, []string{"serial"}, 0, 0},
			{[]string{"ground-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"rxd"}, []string{"serial"}, 0, 0},
			{[]string{"gpio17"}, []string{"gpio"}, 17, 0},
			{[]string{"gpio18"}, []string{"gpio"}, 18, 0}, // also supports PWM
			{[]string{"gpio27"}, []string{"gpio"}, 27, 0},
			{[]string{"ground-3"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio22"}, []string{"gpio"}, 22, 0},
			{[]string{"gpio23"}, []string{"gpio"}, 23, 0},
			{[]string{"3.3v-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio24"}, []string{"gpio"}, 24, 0},
			{[]string{"mosi"}, []string{"spi"}, 0, 0},
			{[]string{"ground-4"}, []string{"unassignable"}, 0, 0},
			{[]string{"miso"}, []string{"spi"}, 0, 0},
			{[]string{"gpio25"}, []string{"gpio"}, 25, 0},
			{[]string{"sclk"}, []string{"spi"}, 0, 0},
			{[]string{"gpio8"}, []string{"gpio"}, 8, 0},
			{[]string{"ground-5"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio7"}, []string{"gpio"}, 7, 0},
		}
	case typeAplusBPlusZeroPi2: // B+
		d.pinConfigs = []*DTPinConfig{
			{[]string{"null"}, []string{"unassignable"}, 0, 0}, // 0 - spacer
			{[]string{"3.3v-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"5v-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"sda"}, []string{"i2c"}, 0, 0},
			{[]string{"5v-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"scl"}, []string{"i2c"}, 0, 0},
			{[]string{"ground-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio4"}, []string{"gpio"}, 4, 0},
			{[]string{"txd"}, []string{"serial"}, 0, 0},
			{[]string{"ground-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"rxd"}, []string{"serial"}, 0, 0},
			{[]string{"gpio17"}, []string{"gpio"}, 17, 0},
			{[]string{"gpio18"}, []string{"gpio"}, 18, 0}, // also supports PWM
			{[]string{"gpio27"}, []string{"gpio"}, 21, 0},
			{[]string{"ground-3"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio22"}, []string{"gpio"}, 22, 0},
			{[]string{"gpio23"}, []string{"gpio"}, 23, 0},
			{[]string{"3.3v-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio24"}, []string{"gpio"}, 24, 0},
			{[]string{"mosi"}, []string{"spi"}, 0, 0},
			{[]string{"ground-4"}, []string{"unassignable"}, 0, 0},
			{[]string{"miso"}, []string{"spi"}, 0, 0},
			{[]string{"gpio25"}, []string{"gpio"}, 25, 0},
			{[]string{"sclk"}, []string{"spi"}, 0, 0},
			{[]string{"gpio8"}, []string{"gpio"}, 8, 0},
			{[]string{"ground-5"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio7"}, []string{"gpio"}, 7, 0},
			{[]string{"do-not-connect-1"}, []string{"unassignable"}, 0, 0},
			{[]string{"do-not-connect-2"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio5"}, []string{"gpio"}, 5, 0},
			{[]string{"ground-6"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio6"}, []string{"gpio"}, 6, 0},
			{[]string{"gpio12"}, []string{"gpio"}, 12, 0},
			{[]string{"gpio13"}, []string{"gpio"}, 13, 0},
			{[]string{"ground-7"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio19"}, []string{"gpio"}, 19, 0},
			{[]string{"gpio16"}, []string{"gpio"}, 16, 0},
			{[]string{"gpio26"}, []string{"gpio"}, 26, 0},
			{[]string{"gpio20"}, []string{"gpio"}, 20, 0},
			{[]string{"ground-8"}, []string{"unassignable"}, 0, 0},
			{[]string{"gpio21"}, []string{"gpio"}, 21, 0},
		}
	}
}
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()
}
880024
func golua_callallocf(fp uintptr, ptr uintptr, osize uint, nsize uint) uintptr {
	return uintptr((*((*Alloc)(unsafe.Pointer(fp))))(unsafe.Pointer(ptr), osize, nsize))
}
112157
func (p *pin) Set() {
	_, p.err = p.valueFile.Write(bytesSet)
}
648958
func (e *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
	e.mutex.Lock()
	defer e.mutex.Unlock()

	sysPin, ok := sysfsPinMap[pin]
	if !ok {
		err = errors.New("Not a valid pin")
		return
	}
	if sysPin.pwmPin != -1 {
		if e.pwmPins[sysPin.pwmPin] == nil {
			e.pwmPins[sysPin.pwmPin] = sysfs.NewPWMPin(sysPin.pwmPin)
			if err = e.pwmPins[sysPin.pwmPin].Export(); err != nil {
				return
			}
			if err = e.pwmPins[sysPin.pwmPin].SetPeriod(10000000); err != nil {
				return
			}
			if err = e.pwmPins[sysPin.pwmPin].Enable(true); err != nil {
				return
			}
		}

		sysfsPin = e.pwmPins[sysPin.pwmPin]
		return
	}
	err = errors.New("Not a PWM pin")
	return
}
782835
func (r *Adaptor) PWMPin(pin string) (raspiPWMPin sysfs.PWMPinner, err error) {
	i, err := r.translatePin(pin)
	if err != nil {
		return
	}

	r.mutex.Lock()
	defer r.mutex.Unlock()

	if r.pwmPins[i] == nil {
		r.pwmPins[i] = NewPWMPin(strconv.Itoa(i))
		r.pwmPins[i].SetPeriod(r.PiBlasterPeriod)
	}

	return r.pwmPins[i], nil
}
307318
func (m *Money) Allocate(rs ...int) ([]*Money, error) {
	if len(rs) == 0 {
		return nil, errors.New("no ratios specified")
	}

	// Calculate sum of ratios.
	var sum int
	for _, r := range rs {
		sum += r
	}

	var total int64
	var ms []*Money
	for _, r := range rs {
		party := &Money{
			amount:   mutate.calc.allocate(m.amount, r, sum),
			currency: m.currency,
		}

		ms = append(ms, party)
		total += party.amount.val
	}

	// Calculate leftover value and divide to first parties.
	lo := m.amount.val - total
	sub := int64(1)
	if lo < 0 {
		sub = -sub
	}

	for p := 0; lo != 0; p++ {
		ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{sub})
		lo -= sub
	}

	return ms, nil
}
1149696
func PinMode(pin Pin, mode PinIOMode) error {
	gpio, e := GetGPIOModule()
	if e != nil {
		return e
	}

	return gpio.PinMode(pin, mode)
}
437047
func (e *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
	sysPin := e.pinmap[pin]
	if sysPin.pwmPin != -1 {
		if e.pwmPins[sysPin.pwmPin] == nil {
			if err = e.DigitalWrite(pin, 1); err != nil {
				return
			}
			if err = changePinMode(e, strconv.Itoa(int(sysPin.pin)), "1"); err != nil {
				return
			}
			e.mutex.Lock()
			defer e.mutex.Unlock()

			e.pwmPins[sysPin.pwmPin] = sysfs.NewPWMPin(sysPin.pwmPin)
			if err = e.pwmPins[sysPin.pwmPin].Export(); err != nil {
				return
			}
			if err = e.pwmPins[sysPin.pwmPin].Enable(true); err != nil {
				return
			}
		}

		sysfsPin = e.pwmPins[sysPin.pwmPin]
		return
	}
	err = errors.New("Not a PWM pin")
	return
}
760008
func (b *Client) AnalogWrite(pin int, value int) error {
	b.pins[pin].Value = value
	return b.write([]byte{AnalogMessage | byte(pin), byte(value & 0x7F), byte((value >> 7) & 0x7F)})
}
1168897
func (a *MinMaxAllocator) Allocate(i int) (bool, error) {
	a.lock.Lock()
	defer a.lock.Unlock()

	if !a.inRange(i) {
		return false, ErrOutOfRange
	}

	if a.has(i) {
		return false, ErrConflict
	}

	a.used[i] = true
	a.free--

	return true, nil
}
1274847
func (b *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
	b.mutex.Lock()
	defer b.mutex.Unlock()

	pinInfo, err := b.translatePwmPin(pin)
	if err != nil {
		return nil, err
	}

	if b.pwmPins[pin] == nil {
		newPin := sysfs.NewPWMPin(pinInfo.channel)
		if err = muxPin(pin, "pwm"); err != nil {
			return
		}

		if newPin.Path, err = b.findPin(pinInfo.path); err != nil {
			return
		}
		if err = newPin.Export(); err != nil {
			return
		}
		if err = newPin.SetPeriod(pwmDefaultPeriod); err != nil {
			return
		}
		if err = newPin.Enable(true); err != nil {
			return
		}
		b.pwmPins[pin] = newPin
	}

	sysfsPin = b.pwmPins[pin]

	return
}
979530
func (b *IOPinDriver) AnalogRead(pin string) (val int, err error) {
	p, err := validatedPin(pin)
	if err != nil {
		return
	}

	b.ensureAnalog(p)
	b.ensureInput(p)

	pins := b.ReadAllPinData()
	return int(pins[p].value), nil
}
1326914
func NewGPIODriver(pinMap PinMap, dpf digitalPinFactory, apf analogPinFactory, ppf pwmPinFactory) GPIODriver {
	return &gpioDriver{
		pinMap: pinMap,
		dpf:    dpf,
		apf:    apf,
		ppf:    ppf,

		initializedPins: map[string]pin{},
	}
}