예제 #1
0
// newPwmPin creates a new pwm pin with specified pin number
func newPwmPin(pinNum string, ocp string) (p *pwmPin, err error) {
	done := make(chan error, 0)
	p = &pwmPin{
		pinNum: strings.ToUpper(pinNum),
	}

	pwmDevice, err := glob(fmt.Sprintf("%v/pwm_test_%v.*", ocp, p.pinNum))
	if err != nil {
		return
	}

	p.pwmDevice = pwmDevice[0]

	go func() {
		for {
			if _, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_RDONLY, 0644); err == nil {
				break
			}
		}
		for {
			if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
				defer fi.Close()
				if _, err = fi.WriteString("0"); err != nil {
					done <- err
				}
				fi.Sync()
				break
			}
		}
		for {
			if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/polarity", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
				defer fi.Close()
				if _, err = fi.WriteString("0"); err != nil {
					done <- err
				}
				fi.Sync()
				break
			}
		}
		for {
			if fi, err := sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
				defer fi.Close()
				if _, err = fi.WriteString("1"); err != nil {
					done <- err
				}
				fi.Sync()
				break
			}
		}
		done <- nil
	}()

	select {
	case err = <-done:
		return p, err
	case <-time.After(500 * time.Millisecond):
		return p, errors.New("could not initialize pwm device")
	}
}
예제 #2
0
func ensureSlot(slots, item string) (err error) {
	fi, err := sysfs.OpenFile(slots, os.O_RDWR|os.O_APPEND, 0666)
	defer fi.Close()
	if err != nil {
		return
	}

	// ensure the slot is not already written into the capemanager
	// (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190)
	scanner := bufio.NewScanner(fi)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Index(line, item) > 0 {
			return
		}
	}

	_, err = fi.WriteString(item)
	if err != nil {
		return err
	}
	fi.Sync()

	scanner = bufio.NewScanner(fi)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Index(line, item) > 0 {
			return
		}
	}
	return
}
예제 #3
0
func writeFile(path string, data []byte) (i int, err error) {
	file, err := sysfs.OpenFile(path, os.O_WRONLY, 0644)
	defer file.Close()
	if err != nil {
		return
	}

	return file.Write(data)
}
예제 #4
0
// releae writes string to close a pwm pin
func (p *pwmPin) release() (err error) {
	fi, err := sysfs.OpenFile(fmt.Sprintf("%v/run", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
	defer fi.Close()
	if err != nil {
		return
	}
	_, err = fi.WriteString("0")
	return
}
예제 #5
0
func (r *Adaptor) piBlaster(data string) (err error) {
	fi, err := sysfs.OpenFile("/dev/pi-blaster", os.O_WRONLY|os.O_APPEND, 0644)
	defer fi.Close()

	if err != nil {
		return err
	}

	_, err = fi.WriteString(data)
	return
}
예제 #6
0
// pwmWrite writes to a pwm pin with specified period and duty
func (p *pwmPin) pwmWrite(period string, duty string) (err error) {
	f1, err := sysfs.OpenFile(fmt.Sprintf("%v/period", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
	defer f1.Close()
	if err != nil {
		return
	}
	if _, err = f1.WriteString(period); err != nil {
		return
	}

	f2, err := sysfs.OpenFile(fmt.Sprintf("%v/duty", p.pwmDevice), os.O_WRONLY|os.O_APPEND, 0666)
	defer f2.Close()
	if err != nil {
		return
	}

	_, err = f2.WriteString(duty)

	return
}
예제 #7
0
func readFile(path string) ([]byte, error) {
	file, err := sysfs.OpenFile(path, os.O_RDONLY, 0644)
	defer file.Close()
	if err != nil {
		return make([]byte, 0), err
	}

	buf := make([]byte, 200)
	var i = 0
	i, err = file.Read(buf)
	if i == 0 {
		return buf, err
	}
	return buf[:i], err
}
예제 #8
0
// DigitalWrite writes a digital value to specified pin.
// valid usr pin values are usr0, usr1, usr2 and usr3
func (b *Adaptor) DigitalWrite(pin string, val byte) (err error) {
	if strings.Contains(pin, "usr") {
		fi, err := sysfs.OpenFile(b.usrLed+pin+"/brightness", os.O_WRONLY|os.O_APPEND, 0666)
		defer fi.Close()
		if err != nil {
			return err
		}
		_, err = fi.WriteString(strconv.Itoa(int(val)))
		return err
	}
	sysfsPin, err := b.digitalPin(pin, sysfs.OUT)
	if err != nil {
		return err
	}
	return sysfsPin.Write(int(val))
}
예제 #9
0
// AnalogRead returns an analog value from specified pin
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
}