From 4602fead4c4c33c8eb036248058428d138d8e432 Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 8 Feb 2025 11:50:33 -0700 Subject: [PATCH] add more documentation for 32 byte limit. minor code restructure to be more transparent --- serlcd/serlcd.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/serlcd/serlcd.go b/serlcd/serlcd.go index 9bd2119..93a6c72 100644 --- a/serlcd/serlcd.go +++ b/serlcd/serlcd.go @@ -28,6 +28,14 @@ type Dev struct { } const ( + // For i2c there's a buffer limitation of 32 bytes. + // See this issue: + // + // https://github.com/sparkfun/OpenLCD/issues/29 + // + // Writing more than that will lock the device up necessitating + // a device reset. + _MAX_I2C_WRITE = 32 DefaultI2CAddress uint16 = 0x72 ) @@ -197,13 +205,10 @@ func (dev *Dev) Write(p []byte) (n int, err error) { n, err = dev.w.Write(p) return } - // Evidently, for i2c there's a buffer limitation of 32 bytes. Writing - // more than that will lock the device up. - writeLimit := 32 for n < len(p) { bytesToWrite := len(p) - n - if bytesToWrite > writeLimit { - bytesToWrite = 32 + if bytesToWrite > _MAX_I2C_WRITE { + bytesToWrite = _MAX_I2C_WRITE } w := p[n : n+bytesToWrite] err = dev.conn.Tx(w, nil)