Initial commit

This commit is contained in:
Thomas Klaehn 2023-08-18 09:25:59 +02:00
commit f089990fc2
4 changed files with 114 additions and 0 deletions

58
ftdi_switch.go Normal file
View File

@ -0,0 +1,58 @@
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)
}

39
ftdi_switch_test.go Normal file
View File

@ -0,0 +1,39 @@
package ftdi_switch
import (
"testing"
"gotest.tools/v3/assert"
)
const (
ID_VENDOR int = 0x0403
ID_PRODUCT int = 0x6014
)
func TestMain(t *testing.T) {
logger.SetPrefix("ftdi_switch: ")
logger.Println("Starting")
var err error
err = Start(ID_VENDOR, ID_PRODUCT, 0)
assert.NilError(t, err)
err = Write_bit(3, true)
assert.NilError(t, err)
err = Write_bit(5, true)
assert.NilError(t, err)
err = Write_bit(6, true)
assert.NilError(t, err)
err = Write_bit(7, true)
assert.NilError(t, err)
err = Write_byte(0)
assert.NilError(t, err)
err = Stop()
assert.NilError(t, err)
}

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module git.blackfinn.de/solar/ftdi_switch
go 1.20.0
require (
github.com/ziutek/ftdi v0.0.1
gotest.tools/v3 v3.5.0
)
require github.com/google/go-cmp v0.5.9 // indirect

7
go.sum Normal file
View File

@ -0,0 +1,7 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/ziutek/ftdi v0.0.1 h1:IrFtqLisIzEfgSnuLg1suZYLkn227DTyE464XswAGK4=
github.com/ziutek/ftdi v0.0.1/go.mod h1:fUzQhjslJJSlwoMW5Cd+0RUc8KuMDfsuZB+wfJAz1eg=
github.com/ziutek/lcd v0.0.0-20141212131202-924f223d0903/go.mod h1:ZBCPhfHIcCtzsrXIcyEiSPDKHrjT9fXtJNKj2t1HCKw=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=