1
0
mirror of https://github.com/kidoman/embd synced 2024-06-01 16:48:06 +02:00
embd/controller/servoblaster/servoblaster.go
Karan Misra 7c32e907e1 - support for servo blaster
- servo requires the underlying PWM to support a SetMicroseconds method
2014-01-09 02:35:18 +05:30

57 lines
1.2 KiB
Go

// Package servoblaster allows interfacing with the software servoblaster driver.
//
// More details on ServoBlaster at: https://github.com/richardghirst/PiBits/tree/master/ServoBlaster
package servoblaster
import (
"fmt"
"log"
"os"
)
// ServoBlaster represents a software RPi PWM/PCM based servo control module.
type ServoBlaster struct {
initialized bool
fd *os.File
// Debug level.
Debug bool
}
// New creates a new ServoBlaster instance.
func New() *ServoBlaster {
return &ServoBlaster{}
}
func (d *ServoBlaster) setup() (err error) {
if d.initialized {
return
}
if d.fd, err = os.OpenFile("/dev/servoblaster", os.O_WRONLY, os.ModeExclusive); err != nil {
return
}
d.initialized = true
return
}
// SetMicroseconds sends a command to the PWM driver to generate a us wide pulse.
func (d *ServoBlaster) SetMicroseconds(channel, us int) (err error) {
if err = d.setup(); err != nil {
return
}
cmd := fmt.Sprintf("%v=%vus\n", channel, us)
if d.Debug {
log.Printf("servoblaster: sending command %q", cmd)
}
_, err = d.fd.WriteString(cmd)
return
}
// Close closes the open driver handle.
func (d *ServoBlaster) Close() (err error) {
if d.fd != nil {
err = d.fd.Close()
}
return
}