Ejemplo n.º 1
0
func main() {
	sp, err := serial.OpenPort(&serial.Config{Name: "/dev/ttyACM0", Baud: 57600})
	if err != nil {
		panic(err)
	}

	board := client.New()

	fmt.Println("connecting.....")
	err = board.Connect(sp)
	defer board.Disconnect()

	if err != nil {
		panic(err)
	}

	fmt.Println("firmware name:", board.FirmwareName)
	fmt.Println("firmata version:", board.ProtocolVersion)

	pin := 13
	if err = board.SetPinMode(pin, client.Output); err != nil {
		panic(err)
	}

	level := 0

	for {
		level ^= 1
		if err := board.DigitalWrite(pin, level); err != nil {
			panic(err)
		}
		fmt.Println("level:", level)
		<-time.After(500 * time.Millisecond)
	}
}
Ejemplo n.º 2
0
Archivo: pingu.go Proyecto: joek/pingu
// NewTux creates a new instance of Tux and creates the connection to the arduino.
func NewTux(args ...interface{}) *Tux {
	t := &Tux{
		client: client.New(),
		port:   "",
		wave:   make(chan request),
		lock:   &sync.Mutex{},
		conn:   nil,
		openSP: func(port string) (io.ReadWriteCloser, error) {
			return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
		},
	}
	for _, arg := range args {
		switch arg.(type) {
		case string:
			t.port = arg.(string)
		case io.ReadWriteCloser:
			t.conn = arg.(io.ReadWriteCloser)
		}
	}

	return t
}
Ejemplo n.º 3
0
// NewFirmataAdaptor returns a new FirmataAdaptor with specified name and optionally accepts:
//
//	string: port the FirmataAdaptor uses to connect to a serial port with a baude rate of 57600
//	io.ReadWriteCloser: connection the FirmataAdaptor uses to communication with the hardware
//
// If an io.ReadWriteCloser is not supplied, the FirmataAdaptor will open a connection
// to a serial port with a baude rate of 57600. If an io.ReadWriteCloser
// is supplied, then the FirmataAdaptor will use the provided io.ReadWriteCloser and use the
// string port as a label to be displayed in the log and api.
func NewFirmataAdaptor(name string, args ...interface{}) *FirmataAdaptor {
	f := &FirmataAdaptor{
		name:  name,
		port:  "",
		conn:  nil,
		board: client.New(),
		openSP: func(port string) (io.ReadWriteCloser, error) {
			return serial.OpenPort(&serial.Config{Name: port, Baud: 57600})
		},
	}

	for _, arg := range args {
		switch arg.(type) {
		case string:
			f.port = arg.(string)
		case io.ReadWriteCloser:
			f.conn = arg.(io.ReadWriteCloser)
		}
	}

	return f
}