Exemplo n.º 1
0
func (e *EdisonAdaptor) connectArduinoBoard() (err error) {
	if err = e.tristate.Direction(sysfs.OUT); err != nil {
		return err
	}
	if err = e.tristate.Write(sysfs.LOW); err != nil {
		return err
	}

	// Setup mux HIGH
	for _, i := range []int{263, 262} {
		io := sysfs.NewDigitalPin(i)
		if err = io.Export(); err != nil {
			return err
		}
		if err = io.Direction(sysfs.OUT); err != nil {
			return err
		}
		if err = io.Write(sysfs.HIGH); err != nil {
			return err
		}
		if err = io.Unexport(); err != nil {
			return err
		}
	}

	// Setup mux LOW
	for _, i := range []int{240, 241, 242, 243} {
		io := sysfs.NewDigitalPin(i)
		if err = io.Export(); err != nil {
			return err
		}
		if err = io.Direction(sysfs.OUT); err != nil {
			return err
		}
		if err = io.Write(sysfs.LOW); err != nil {
			return err
		}
		if err = io.Unexport(); err != nil {
			return err
		}

	}

	// Arduino IO1, IO4 and IO13
	for _, i := range []string{"131", "129", "40"} {
		if err = changePinMode(i, "0"); err != nil {
			return err
		}
	}

	err = e.tristate.Write(sysfs.HIGH)
	return
}
Exemplo n.º 2
0
// I2cStart initializes i2c device for addresss
func (e *EdisonAdaptor) I2cStart(address int) (err error) {
	if e.i2cDevice != nil {
		return
	}

	if err = e.tristate.Write(sysfs.LOW); err != nil {
		return
	}

	for _, i := range []int{14, 165, 212, 213} {
		io := sysfs.NewDigitalPin(i)
		if err = io.Export(); err != nil {
			return
		}
		if err = io.Direction(sysfs.IN); err != nil {
			return
		}
		if err = io.Unexport(); err != nil {
			return
		}
	}

	for _, i := range []int{236, 237, 204, 205} {
		io := sysfs.NewDigitalPin(i)
		if err = io.Export(); err != nil {
			return
		}
		if err = io.Direction(sysfs.OUT); err != nil {
			return
		}
		if err = io.Write(sysfs.LOW); err != nil {
			return
		}
		if err = io.Unexport(); err != nil {
			return
		}
	}

	for _, i := range []string{"28", "27"} {
		if err = changePinMode(i, "1"); err != nil {
			return
		}
	}

	if err = e.tristate.Write(sysfs.HIGH); err != nil {
		return
	}

	e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-6", address)
	return
}
Exemplo n.º 3
0
// digitalPin returns matched digitalPin for specified values
func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	var i int

	if val, ok := pins[pin][r.revision]; ok {
		i = val
	} else if val, ok := pins[pin]["*"]; ok {
		i = val
	} else {
		err = errors.New("Not a valid pin")
		return
	}

	if r.digitalPins[i] == nil {
		r.digitalPins[i] = sysfs.NewDigitalPin(i)
		if err = r.digitalPins[i].Export(); err != nil {
			return
		}
	}

	if err = r.digitalPins[i].Direction(dir); err != nil {
		return
	}

	return r.digitalPins[i], nil
}
Exemplo n.º 4
0
// digitalPin retrieves digital pin value by name
func (b *BeagleboneAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i, err := b.translatePin(pin)
	if err != nil {
		return
	}
	if b.digitalPins[i] == nil {
		b.digitalPins[i] = sysfs.NewDigitalPin(i)
		err := b.digitalPins[i].Export()
		if err != nil {
			return nil, err
		}
	}
	if err = b.digitalPins[i].Direction(dir); err != nil {
		return
	}
	return b.digitalPins[i], nil
}
Exemplo n.º 5
0
// digitalPin returns matched digitalPin for specified values
func (r *RaspiAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i, err := r.translatePin(pin)

	if err != nil {
		return
	}

	if r.digitalPins[i] == nil {
		r.digitalPins[i] = sysfs.NewDigitalPin(i)
		if err = r.digitalPins[i].Export(); err != nil {
			return
		}
	}

	if err = r.digitalPins[i].Direction(dir); err != nil {
		return
	}

	return r.digitalPins[i], nil
}
Exemplo n.º 6
0
// digitalPin returns matched digitalPin for specified values
func (c *ChipAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i, err := c.translatePin(pin)

	if err != nil {
		return
	}

	if c.digitalPins[i] == nil {
		c.digitalPins[i] = sysfs.NewDigitalPin(i)
		if err = c.digitalPins[i].Export(); err != nil {
			return
		}
	}

	if err = c.digitalPins[i].Direction(dir); err != nil {
		return
	}

	return c.digitalPins[i], nil
}
Exemplo n.º 7
0
// NewEdisonAdaptor returns a new EdisonAdaptor with specified name
func NewEdisonAdaptor(name string) *EdisonAdaptor {
	return &EdisonAdaptor{
		name: name,
		//i2cDevices: make(map[int]io.ReadWriteCloser),
		//i2cDevices: make(map[int]io.ReadWriteCloser),
		connect: func(e *EdisonAdaptor) (err error) {
			e.tristate = sysfs.NewDigitalPin(214)
			if err = e.tristate.Export(); err != nil {
				// edison: Failed to initialize Arduino board TriState
				// assuming miniboard:
				e.miniboard = true
				e.sysfsPinMap = sysfsPinsMiniboard
				return e.connectMiniboard()
			} else {
				e.sysfsPinMap = sysfsPinsArduino
				return e.connectArduinoBoard()
			}

		},
	}
}
Exemplo n.º 8
0
// digitalPin returns matched digitalPin for specified values
func (e *JouleAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i := sysfsPinMap[pin]
	if e.digitalPins[i.pin] == nil {
		e.digitalPins[i.pin] = sysfs.NewDigitalPin(i.pin)
		if err = e.digitalPins[i.pin].Export(); err != nil {
			// TODO: log error
			return
		}
	}

	if dir == "in" {
		if err = e.digitalPins[i.pin].Direction(sysfs.IN); err != nil {
			return
		}
	} else if dir == "out" {
		if err = e.digitalPins[i.pin].Direction(sysfs.OUT); err != nil {
			return
		}
	}
	return e.digitalPins[i.pin], nil
}
Exemplo n.º 9
0
// I2cStart initializes i2c device for addresss
func (e *EdisonAdaptor) I2cStart(address int) (err error) {
	if e.i2cDevice != nil {
		return
	}

	// FIXME: the I2cDevice interface doesn,t support multiple buses,
	// like the miniboard.. We select bus-6 by default here.

	if e.miniboard {
		for _, i := range []string{"20", "19"} /* bus-1 */ {
			//for _, i := range []string{"28", "27"} /* bus-6 */ {
			if err = changePinMode(i, "1"); err != nil {
				return
			}
		}

		e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-1", address)

	} else {

		if err = e.tristate.Write(sysfs.LOW); err != nil {
			return
		}

		// Confiure IO18, IO19
		// (ref: mraa's intel_edison_fab_c.c, func mraa_intel_edison_i2c_init_pre)
		for _, i := range []int{14, 165, 212, 213} {
			io := sysfs.NewDigitalPin(i)
			if err = io.Export(); err != nil {
				return
			}
			if err = io.Direction(sysfs.IN); err != nil {
				return
			}
			if err = io.Unexport(); err != nil {
				return
			}
		}

		// Continue on..
		for _, i := range []int{236, 237, 204, 205} {
			io := sysfs.NewDigitalPin(i)
			if err = io.Export(); err != nil {
				return
			}
			if err = io.Direction(sysfs.OUT); err != nil {
				return
			}
			if err = io.Write(sysfs.LOW); err != nil {
				return
			}
			if err = io.Unexport(); err != nil {
				return
			}
		}

		// Activate the I2c bus-6
		for _, i := range []string{"28", "27"} {
			if err = changePinMode(i, "1"); err != nil {
				return
			}
		}

		if err = e.tristate.Write(sysfs.HIGH); err != nil {
			return
		}

		e.i2cDevice, err = sysfs.NewI2cDevice("/dev/i2c-6", address)
	}

	return
}
Exemplo n.º 10
0
// digitalPin returns matched digitalPin for specified values
func (e *EdisonAdaptor) digitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPin, err error) {
	i := e.sysfsPinMap[pin]
	if e.digitalPins[i.gpioPin] == nil {
		e.digitalPins[i.gpioPin] = sysfs.NewDigitalPin(i.gpioPin)
		if err = e.digitalPins[i.gpioPin].Export(); err != nil {
			return
		}

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

		if i.levelShifter != 0 {
			e.digitalPins[i.levelShifter] = sysfs.NewDigitalPin(i.levelShifter)
			if err = e.digitalPins[i.levelShifter].Export(); err != nil {
				return
			}
		}

		for _, mux := range i.mux {
			e.digitalPins[mux.pin] = sysfs.NewDigitalPin(mux.pin)
			if err = e.digitalPins[mux.pin].Export(); err != nil {
				return
			}

			if err = e.digitalPins[mux.pin].Direction(sysfs.OUT); err != nil {
				return
			}

			if err = e.digitalPins[mux.pin].Write(mux.value); err != nil {
				return
			}

		}
	}

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

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

			if err = e.digitalPins[i.resistor].Write(sysfs.LOW); err != nil {
				return
			}
		}

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

			if err = e.digitalPins[i.levelShifter].Write(sysfs.LOW); err != nil {
				return
			}
		}

	} else if dir == "out" {
		if err = e.digitalPins[i.gpioPin].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 {
			if err = e.digitalPins[i.levelShifter].Direction(sysfs.OUT); err != nil {
				return
			}

			if err = e.digitalPins[i.levelShifter].Write(sysfs.HIGH); err != nil {
				return
			}
		}

	}
	return e.digitalPins[i.gpioPin], nil
}
Exemplo n.º 11
0
// NewEdisonAdaptor returns a new EdisonAdaptor with specified name
func NewEdisonAdaptor(name string) *EdisonAdaptor {
	return &EdisonAdaptor{
		name: name,
		//i2cDevices: make(map[int]io.ReadWriteCloser),
		//i2cDevices: make(map[int]io.ReadWriteCloser),
		connect: func(e *EdisonAdaptor) (err error) {
			e.tristate = sysfs.NewDigitalPin(214)
			if err = e.tristate.Export(); err != nil {
				return err
			}
			if err = e.tristate.Direction(sysfs.OUT); err != nil {
				return err
			}
			if err = e.tristate.Write(sysfs.LOW); err != nil {
				return err
			}

			for _, i := range []int{263, 262} {
				io := sysfs.NewDigitalPin(i)
				if err = io.Export(); err != nil {
					return err
				}
				if err = io.Direction(sysfs.OUT); err != nil {
					return err
				}
				if err = io.Write(sysfs.HIGH); err != nil {
					return err
				}
				if err = io.Unexport(); err != nil {
					return err
				}
			}

			for _, i := range []int{240, 241, 242, 243} {
				io := sysfs.NewDigitalPin(i)
				if err = io.Export(); err != nil {
					return err
				}
				if err = io.Direction(sysfs.OUT); err != nil {
					return err
				}
				if err = io.Write(sysfs.LOW); err != nil {
					return err
				}
				if err = io.Unexport(); err != nil {
					return err
				}

			}

			for _, i := range []string{"111", "115", "114", "109"} {
				if err = changePinMode(i, "1"); err != nil {
					return err
				}
			}

			for _, i := range []string{"131", "129", "40"} {
				if err = changePinMode(i, "0"); err != nil {
					return err
				}
			}

			err = e.tristate.Write(sysfs.HIGH)
			return
		},
	}
}