ftdi_switch/ftdi_switch.go
2023-08-20 08:12:19 +02:00

59 lines
981 B
Go

package ftdi_switch
import (
"log"
"github.com/ziutek/ftdi"
)
var (
logger log.Logger = *log.Default()
ftdi_device *ftdi.Device
data_byte byte = 0
)
func imit() {
logger.SetPrefix("ftdi_switch: ")
}
func Start(vendor_id int, product_id int, init_value int) error {
var err error
ftdi_device, err = ftdi.OpenFirst(vendor_id, product_id, ftdi.ChannelAny)
if err != nil {
logger.Println(err)
}
err = ftdi_device.SetBitmode(0xff, ftdi.ModeBitbang)
if err != nil {
logger.Println(err)
}
err = ftdi_device.SetBaudrate(192)
if err != nil {
logger.Println(err)
}
err = Write_byte(0)
if err != nil {
logger.Println(err)
}
return err
}
func Stop() error {
return ftdi_device.Close()
}
func Write_bit(bit byte, value bool) error {
if value {
data_byte |= 1 << bit
} else {
data_byte &= ^(1 << bit)
}
return ftdi_device.WriteByte(data_byte)
}
func Write_byte(data byte) error {
data_byte = data
return ftdi_device.WriteByte(data_byte)
}