From a16546288e3cb569e4fd0af72a5df8b789521f12 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Fri, 18 Aug 2023 09:25:59 +0200 Subject: [PATCH] Initial commit --- ftdi_switch.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ ftdi_switch_test.go | 39 ++++++++++++++++++++++++++++++ go.mod | 10 ++++++++ go.sum | 7 ++++++ 4 files changed, 114 insertions(+) create mode 100644 ftdi_switch.go create mode 100644 ftdi_switch_test.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/ftdi_switch.go b/ftdi_switch.go new file mode 100644 index 0000000..8cf7319 --- /dev/null +++ b/ftdi_switch.go @@ -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) +} diff --git a/ftdi_switch_test.go b/ftdi_switch_test.go new file mode 100644 index 0000000..9da19ca --- /dev/null +++ b/ftdi_switch_test.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c9aa04a --- /dev/null +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..266d31e --- /dev/null +++ b/go.sum @@ -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=