From 7e41196dce9069c1a75edb43d2caaa9452f231ab Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Wed, 4 Sep 2024 21:05:02 +0300 Subject: [PATCH] Add: beta working mt12232a display --- display/display.go | 3 + display/mt-12232a.go | 161 +++++++++++++++++++++++++++++++ main.go | 130 ++++++++++++------------- pkg/mt12232a/mt12232a.go | 115 ++++++++++++++-------- pkg/parallel8bit/parallel8bit.go | 41 ++++++++ 5 files changed, 345 insertions(+), 105 deletions(-) create mode 100644 display/mt-12232a.go diff --git a/display/display.go b/display/display.go index 1bb306c..9821876 100644 --- a/display/display.go +++ b/display/display.go @@ -19,6 +19,7 @@ type DisplayModel int const ( SSD1306 DisplayModel = iota MT12864A + MT12232A ) func New(logger *log.Logger, model DisplayModel) (Display, error) { @@ -27,6 +28,8 @@ func New(logger *log.Logger, model DisplayModel) (Display, error) { return newSsd1306(logger) case MT12864A: return newMt12864a(logger) + case MT12232A: + return newMt12232a(logger) } return nil, fmt.Errorf("invalid display model") } diff --git a/display/mt-12232a.go b/display/mt-12232a.go new file mode 100644 index 0000000..7c929dd --- /dev/null +++ b/display/mt-12232a.go @@ -0,0 +1,161 @@ +package display + +import ( + "fmt" + "image" + "log" + "math/rand" + "time" + + "gitea.unprism.ru/yotia/display-test/pkg/mt12232a" + "github.com/stianeikeland/go-rpio/v4" +) + +type displayMt12232a struct { + logger *log.Logger + + // GPIO pins + dev mt12232a.Device +} + +func newMt12232a(logger *log.Logger) (Display, error) { + dev, err := mt12232a.New(log.New(logger.Writer(), "display-mt12864 : ", log.LstdFlags)) + if err != nil { + return nil, fmt.Errorf("mt12864 create: %w", err) + } + // Allocate bits + bits := make([][]byte, 32) + for i := 0; i < 32; i++ { + bits[i] = make([]byte, 122) + } + + // if err := dev.PowerOn(); err != nil { + // return nil, fmt.Errorf("power on: %w", err) + // } + + d := displayMt12232a{ + logger: logger, + dev: dev, + } + + // Temp debug draw + d.logger.Println(d.dev.ReadStatus(0)&0xF0, d.dev.ReadStatus(1)&0xF0) + //d.logger.Println("Draw...") + //d.test() + //d.logger.Println("Draw finished") + + return &d, nil +} + +func (d *displayMt12232a) test() { + d.logger.Println("Write") + d.dev.WriteCodeL(0xB8) + d.dev.WriteCodeL(0x13) + d.dev.WriteDataL(47) + d.logger.Println("Read") + // d.dev.WriteCodeL(0xB8) + d.dev.WriteCodeL(0x13) + d.logger.Println(d.dev.ReadDataL()) + d.logger.Println(d.dev.ReadDataL()) + + //color := []byte{1, 0, 1, 0, 1, 0, 1, 0} + //color := []byte{1, 0, 1, 0, 1, 0, 0, 0} + + //color := []byte{0, 0, 0, 0, 0, 0, 0, 0} + //color := byte(3) + start := time.Now() + //d.dev.WriteCodeL(0xB8) + //d.dev.WriteCodeL(0x13) + //d.dev.WriteDatasL([]byte{0, 0, 0, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 0, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 1, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 1, 0}) + //d.dev.WriteDatasL([]byte{1, 1, 1, 1, 1, 1, 1, 1}) + // + //d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 1, 0}) + //d.dev.WriteCodeL(0xB8 | 1) + //d.dev.WriteCodeL(0x13) + //d.dev.WriteDatasL([]byte{1, 0, 0, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 0, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 0, 0}) + //d.dev.WriteDatasL([]byte{1, 0, 1, 0, 1, 0, 1, 0}) + + for p := byte(0); p < 4; p++ { + d.dev.WriteCodeL(p | 0xB8) + d.dev.WriteCodeL(0x13) + for c := 0; c < 61; c++ { + d.dev.WriteDatasL([]byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)}) + } + d.dev.WriteCodeR(p | 0xB8) + d.dev.WriteCodeR(0x00) + for c := 0; c < 61; c++ { + d.dev.WriteDatasR([]byte{byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2), byte(rand.Int() % 2)}) + } + } + end := time.Now() + d.logger.Println(end.Sub(start)) + // for p := byte(0); p < 8; p++ { + // d.dev.WriteCodeL(p | 0xB8) + // d.dev.WriteCodeL(0x40) + // for c := 0; c < 64; c++ { + // d.dev.WriteDataL(Logo128[p][c]) + // } + // d.dev.WriteCodeR(p | 0xB8) + // d.dev.WriteCodeR(0x40) + // for c := 64; c < 128; c++ { + // d.dev.WriteDataR(Logo128[p][c]) + // } + // } + //d.dev.WriteCodeL(0xAF) // Display on + //d.dev.WriteCodeR(0xAF) // Display on + +} + +func (d *displayMt12232a) GetBounds() image.Rectangle { + return image.Rect(0, 0, 122, 32) +} + +func (d *displayMt12232a) Flush(img *image.Gray) error { + for p := byte(0); p < 4; p++ { + d.dev.WriteCodeL((3 - p) | 0xB8) + d.dev.WriteCodeL(0x13) + for c := 0; c < 61; c++ { + d.dev.WriteDatasL([]byte{ + img.Pix[int(p<<3+7)*122+c], + img.Pix[int(p<<3+6)*122+c], + img.Pix[int(p<<3+5)*122+c], + img.Pix[int(p<<3+4)*122+c], + img.Pix[int(p<<3+3)*122+c], + img.Pix[int(p<<3+2)*122+c], + img.Pix[int(p<<3+1)*122+c], + img.Pix[int(p<<3+0)*122+c], + }) + } + d.dev.WriteCodeR((3 - p) | 0xB8) + d.dev.WriteCodeR(0x00) + for c := 61; c < 122; c++ { + d.dev.WriteDatasR([]byte{ + img.Pix[int(p<<3+7)*122+c], + img.Pix[int(p<<3+6)*122+c], + img.Pix[int(p<<3+5)*122+c], + img.Pix[int(p<<3+4)*122+c], + img.Pix[int(p<<3+3)*122+c], + img.Pix[int(p<<3+2)*122+c], + img.Pix[int(p<<3+1)*122+c], + img.Pix[int(p<<3+0)*122+c], + }) + } + } + return nil +} + +func (d *displayMt12232a) Close() error { + return rpio.Close() +} diff --git a/main.go b/main.go index 5d53078..2a5adfe 100644 --- a/main.go +++ b/main.go @@ -84,94 +84,92 @@ func DrawProgress(ctx context.Context, timeout time.Duration) error { func Init(ctx context.Context) error { // Display init var err error - dev, err = display.New(log.New(log.Writer(), "display", log.LstdFlags), display.SSD1306) + dev, err = display.New(log.New(log.Writer(), "display", log.LstdFlags), display.MT12232A) if err != nil { return fmt.Errorf("new display: %w", err) } logger.Println("Display inited") + + // Create drawer d := drawer.New(dev) d.Clear() - d.PutText(0, d.GetFont().Metrics().Height.Ceil(), "Initing modem...") + //d.FillBar(0, 0, 10, 10, 1) + d.PutText(0, 10, "CGSG") + d.PutText(0, 24, "forever!!!") d.Flush() // Modem init - mInitCtx, mInitCtxCancel := context.WithCancel(ctx) - go DrawProgress(mInitCtx, 13*time.Second) - defer mInitCtxCancel() - - m = modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags)) - if err := m.Init(); err != nil { - return fmt.Errorf("modem init: %w", err) - } - logger.Println("Display inited") - mInitCtxCancel() - - d.Clear() - d.PutText(0, d.GetFont().Metrics().Height.Ceil(), "Modem inited.") - d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, "Starting test...") - d.Flush() + //mInitCtx, mInitCtxCancel := context.WithCancel(ctx) + //go DrawProgress(mInitCtx, 13*time.Second) + //defer mInitCtxCancel() + // + //m = modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags)) + //if err := m.Init(); err != nil { + // return fmt.Errorf("modem init: %w", err) + //} + //logger.Println("Display inited") + //mInitCtxCancel() + // + //d.Clear() + //d.PutText(0, d.GetFont().Metrics().Height.Ceil(), "Modem inited.") + //d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, "Starting test...") + //d.Flush() return nil } func Close() { - m.Close() - if d != nil { - d.Clear() - d.Flush() - } + //m.Close() + //if d != nil { + // d.Clear() + // d.Flush() + //} dev.Close() } -func MainLoop(ctx context.Context) error { - mainLoopTimeout := time.Second - - frameCounter := 0 - deadline := time.Now().Add(100 * time.Millisecond) - for { - select { - case <-ctx.Done(): - return nil - case <-time.After(time.Until(deadline)): - deadline = time.Now().Add(mainLoopTimeout) - - st, _ := m.Gps().GetStatus() - data := m.GetData() - - coordsStr := fmt.Sprintf("%03d°%02.0f'%s %03d°%02.0f'%s", - int(data.Latitude), (data.Latitude-float64(int(data.Latitude)))*100, data.LatitudeIndicator, - int(data.Longitude), (data.Longitude-float64(int(data.Longitude)))*100, data.LongitudeIndicator) - var str1 string - var str2 string - - if frameCounter%4 < 2 { - str2 = fmt.Sprintf("Sat. count: %d", st.FoundSatelitesCount) - } else { - str2 = coordsStr - } - str1 = fmt.Sprintf("%s", time.Now().Format("15:04:05")) - - d.Clear() - d.PutText(0, d.GetFont().Metrics().Height.Ceil(), str1) - d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, str2) - d.Flush() - frameCounter++ - } - } -} +//func MainLoop(ctx context.Context) error { +// mainLoopTimeout := time.Second +// +// frameCounter := 0 +// deadline := time.Now().Add(100 * time.Millisecond) +// for { +// select { +// case <-ctx.Done(): +// return nil +// case <-time.After(time.Until(deadline)): +// deadline = time.Now().Add(mainLoopTimeout) +// +// st, _ := m.Gps().GetStatus() +// data := m.GetData() +// +// coordsStr := fmt.Sprintf("%03d°%02.0f'%s %03d°%02.0f'%s", +// int(data.Latitude), (data.Latitude-float64(int(data.Latitude)))*100, data.LatitudeIndicator, +// int(data.Longitude), (data.Longitude-float64(int(data.Longitude)))*100, data.LongitudeIndicator) +// var str1 string +// var str2 string +// +// if frameCounter%4 < 2 { +// str2 = fmt.Sprintf("Sat. count: %d", st.FoundSatelitesCount) +// } else { +// str2 = coordsStr +// } +// str1 = fmt.Sprintf("%s", time.Now().Format("15:04:05")) +// +// d.Clear() +// d.PutText(0, d.GetFont().Metrics().Height.Ceil(), str1) +// d.PutText(0, d.GetFont().Metrics().Height.Ceil()*2, str2) +// d.Flush() +// frameCounter++ +// } +// } +//} func mainE(ctx context.Context) error { logger = log.New(os.Stdout, "main : ", log.LstdFlags) // - disp, err := display.New(logger, display.MT12864A) - if err != nil { + if err := Init(ctx); err != nil { return err } - time.Sleep(2 * time.Second) - defer disp.Close() - // if err := Init(ctx); err != nil { - // return err - // } - // defer Close() + defer Close() // if err := MainLoop(ctx); err != nil { // return err // } diff --git a/pkg/mt12232a/mt12232a.go b/pkg/mt12232a/mt12232a.go index e42cafd..74a2c85 100644 --- a/pkg/mt12232a/mt12232a.go +++ b/pkg/mt12232a/mt12232a.go @@ -21,12 +21,13 @@ type mt12232a struct { type Device interface { PowerOn() error - SetPixel(x, y byte, c bool) error WriteCodeL(c byte) error WriteCodeR(c byte) error WriteDataL(b byte) error WriteDataR(b byte) error + WriteDatasL(b []byte) error + WriteDatasR(b []byte) error ReadDataL() (byte, error) ReadDataR() (byte, error) @@ -42,23 +43,22 @@ func New(logger *log.Logger) (Device, error) { logger: logger, dev: parallel8bit.New(logger, parallel8bit.DevicePins{ - PinA0: rpio.Pin(18), - PinRW: rpio.Pin(17), - PinE: rpio.Pin(27), + PinA0: rpio.Pin(19), + PinRW: rpio.Pin(13), + PinE: rpio.Pin(12), PinDB0: rpio.Pin(22), PinDB1: rpio.Pin(10), - PinDB2: rpio.Pin(9), // From 21 + PinDB2: rpio.Pin(9), PinDB3: rpio.Pin(11), - PinDB4: rpio.Pin(12), - PinDB5: rpio.Pin(16), // From 32 - PinDB6: rpio.Pin(20), // From 31 - PinDB7: rpio.Pin(13), // From 33 - + PinDB4: rpio.Pin(21), + PinDB5: rpio.Pin(20), + PinDB6: rpio.Pin(26), + PinDB7: rpio.Pin(16), }), - pinCS: rpio.Pin(19), - pinRES: rpio.Pin(21), + pinCS: rpio.Pin(17), + pinRES: rpio.Pin(27), } d.pinCS.Output() d.pinRES.Output() @@ -71,47 +71,37 @@ func (d *mt12232a) Close() error { return rpio.Close() } -func (d *mt12232a) SetPixel(x, y byte, c bool) error { - // var c8 byte - // var mask byte - - // Check bounds - if x > 127 || y > 63 { - return fmt.Errorf("positions out of bounds") - } - - if x < 64 { // Left crystal - d.WriteCodeL(0xB8 | (y >> 3)) // Set page - d.WriteCodeL(0x40 | x) // Set addr - // c8=ReadDataL(); // ?? ok - // c8=ReadDataL(); // Read byte - // m8=1<<(y&0x07);//Вычислить маску нужного бита в байте - // if (c==1) //Зажигать точку? - // c8|=m8//Установить нужный бит в байте - // else //Или гасить точку? - // c8&=~m8;//Сбросить нужный бит в байте - // WriteCodeL(0x40|x);//Снова установить адрес нужного байта - d.WriteDataL(0x34) // Write byte - } - return nil +func (d *mt12232a) status() { + d.logger.Println("STATUS:", d.ReadStatus(0)&0xF0, d.ReadStatus(1)&0xF0) } func (d *mt12232a) PowerOn() error { + d.status() d.logger.Println(d.ReadStatus(0)) // Should be 0 d.logger.Println("Reset") + d.status() d.pinRES.Low() time.Sleep(time.Microsecond) d.logger.Println(d.ReadStatus(0)) // Should be 48 (power off and reset) d.pinRES.High() - time.Sleep(10 * time.Microsecond) + time.Sleep(4 * time.Millisecond) d.logger.Println(d.ReadStatus(0)) // Should be 32 (power off) // Module is reset and should be turned off if d.ReadStatus(0) == 0 || d.ReadStatus(1) == 0 { return fmt.Errorf("no response from display(or it is possible that it is turned on but...)") } + for i := 0; i < 100; i++ { + if ((d.ReadStatus(0) >> 4) & 1) == 0 { + break + } + } + d.status() + d.status() + d.status() d.logger.Println("Power on") + d.status() d.WriteCodeL(0xE2) // Reset d.WriteCodeR(0xE2) // Reset d.WriteCodeL(0xEE) // ReadModifyWrite off @@ -125,15 +115,45 @@ func (d *mt12232a) PowerOn() error { d.WriteCodeL(0xA1) // Invert scan RAM d.WriteCodeR(0xA0) // NonInvert scan RAM + d.status() d.logger.Println("Display on") d.WriteCodeL(0xAF) // Display on d.WriteCodeR(0xAF) // Display on + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + d.status() + time.Sleep(100 * time.Millisecond) + // Check that crystals are turned on - if (d.ReadStatus(0) & (1 << 5)) != 0 { + if ((d.ReadStatus(0) >> 5) & 1) == 1 { + d.logger.Println("Left cristal is still off") + } + if ((d.ReadStatus(0) >> 5) & 1) == 1 { + d.logger.Println("Right cristal is still off") + } + + // The same but with error + if ((d.ReadStatus(0) >> 5) & 1) == 1 { return fmt.Errorf("Left cristal is still off") } - if (d.ReadStatus(1) & (1 << 5)) != 0 { + if ((d.ReadStatus(0) >> 5) & 1) == 1 { return fmt.Errorf("Right cristal is still off") } @@ -160,6 +180,14 @@ func (d *mt12232a) WriteDataR(b byte) error { return d.writeByte(b, 1, 1) } +func (d *mt12232a) WriteDatasL(b []byte) error { + return d.writeBytes(b, 1, 0) +} + +func (d *mt12232a) WriteDatasR(b []byte) error { + return d.writeBytes(b, 1, 1) +} + // Read data func (d *mt12232a) ReadDataL() (byte, error) { @@ -173,7 +201,6 @@ func (d *mt12232a) ReadDataR() (byte, error) { // Low level functions func (d *mt12232a) writeByte(b byte, cd, cs rpio.State) error { - // d.logger.Println("Write byte", b, cd, l, r) if err := d.waitReady(cs); err != nil { return fmt.Errorf("wait ready: %w", err) } @@ -183,6 +210,16 @@ func (d *mt12232a) writeByte(b byte, cd, cs rpio.State) error { return nil } +func (d *mt12232a) writeBytes(b []byte, cd, cs rpio.State) error { + if err := d.waitReady(cs); err != nil { + return fmt.Errorf("wait ready: %w", err) + } + d.pinCS.Write(cs) // Select cristals + + d.dev.WriteBytes(b, cd) + return nil +} + func (d *mt12232a) readByte(cd, cs rpio.State) (byte, error) { // Setup if err := d.waitReady(cs); err != nil { diff --git a/pkg/parallel8bit/parallel8bit.go b/pkg/parallel8bit/parallel8bit.go index 180b41c..003024e 100644 --- a/pkg/parallel8bit/parallel8bit.go +++ b/pkg/parallel8bit/parallel8bit.go @@ -18,8 +18,11 @@ const ( type Device interface { Reset() WriteByte(b byte, cd rpio.State) + WriteBytes(b []byte, cd rpio.State) ReadByte(cd rpio.State) byte + Pins() DevicePins + WaitReady() error } @@ -44,6 +47,7 @@ type DevicePins struct { type device struct { logger *log.Logger + isBusOutput bool DevicePins } @@ -54,6 +58,10 @@ func New(logger *log.Logger, pins DevicePins) Device { } } +func (d *device) Pins() DevicePins { + return d.DevicePins +} + func (d *device) Reset() { d.PinA0.Output() d.PinRW.Output() @@ -88,6 +96,31 @@ func (d *device) WriteByte(b byte, cd rpio.State) { time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout) } +func (d *device) WriteBytes(b []byte, cd rpio.State) { + // d.logger.Println("Write byte", b, cd, l, r) + d.busOutput() + d.PinRW.Low() // We write + d.PinA0.Write(cd) + + // Write bus + d.PinDB0.Write(rpio.State(b[0])) + d.PinDB1.Write(rpio.State(b[1])) + d.PinDB2.Write(rpio.State(b[2])) + d.PinDB3.Write(rpio.State(b[3])) + d.PinDB4.Write(rpio.State(b[4])) + d.PinDB5.Write(rpio.State(b[5])) + d.PinDB6.Write(rpio.State(b[6])) + d.PinDB7.Write(rpio.State(b[7])) + + // Strobe start + d.PinE.High() + time.Sleep(dataStrobeTimeout) + + // Strobe end + d.PinE.Low() + time.Sleep(time.Millisecond - dataStrobeTimeout - adressWriteTimeout) +} + func (d *device) ReadByte(cd rpio.State) byte { // Setup var b byte @@ -148,6 +181,9 @@ func (d *device) WaitReady() error { // Set bus pins to output func (d *device) busOutput() { + // if d.isBusOutput { + // return + // } d.PinDB0.Output() d.PinDB1.Output() d.PinDB2.Output() @@ -156,9 +192,13 @@ func (d *device) busOutput() { d.PinDB5.Output() d.PinDB6.Output() d.PinDB7.Output() + d.isBusOutput = true } func (d *device) busInput() { + //if !d.isBusOutput { + // return + //} d.PinDB0.Input() d.PinDB1.Input() d.PinDB2.Input() @@ -167,4 +207,5 @@ func (d *device) busInput() { d.PinDB5.Input() d.PinDB6.Input() d.PinDB7.Input() + d.isBusOutput = false }