ftdi_switch/ftdi_switch.go

59 lines
981 B
Go
Raw Normal View History

2023-08-18 07:25:59 +00:00
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)
}