func (megaPi *MegaPiAdaptor) Finalize() (errs []error) {
	megaPi.finalizeChannel <- struct{}{}
	<-megaPi.finalizeChannel
	if err := megaPi.connection.Close(); err != nil {
		return []error{err}
	}
	return
}
Finalize terminates the connection to the board
package megapi
import (
	"github.com/hybridgroup/gobot"
	"github.com/tarm/serial"
	"io"
	"time"
)
var _ gobot.Adaptor = (*MegaPiAdaptor)(nil)
type MegaPiAdaptor struct {
	name              string
	connection        io.ReadWriteCloser
	serialConfig      *serial.Config
	writeBytesChannel chan []byte
	finalizeChannel   chan struct{}
}
func NewMegaPiAdaptor(name string, device string) *MegaPiAdaptor {
	c := &serial.Config{Name: device, Baud: 115200}
	return &MegaPiAdaptor{
		name:              name,
		connection:        nil,
		serialConfig:      c,
		writeBytesChannel: make(chan []byte),
		finalizeChannel:   make(chan struct{}),
	}
}
func (megaPi *MegaPiAdaptor) Name() string {
	return megaPi.name
}
func (megaPi *MegaPiAdaptor) Connect() (errs []error) {
	if megaPi.connection == nil {
		sp, err := serial.OpenPort(megaPi.serialConfig)
		if err != nil {
			return []error{err}
		}
		time.Sleep(2 * time.Second)
		megaPi.connection = sp
	}
	go func() {
		for {
			select {
			case bytes := <-megaPi.writeBytesChannel:
				megaPi.connection.Write(bytes)
				time.Sleep(10 * time.Millisecond)
			case <-megaPi.finalizeChannel:
				megaPi.finalizeChannel <- struct{}{}
				return
			default:
				time.Sleep(10 * time.Millisecond)
			}
		}
	}()
	return
}