@@ -17,6 +17,7 @@ import (
1717 "periph.io/x/conn/v3"
1818 "periph.io/x/conn/v3/display"
1919 "periph.io/x/conn/v3/gpio"
20+ "periph.io/x/conn/v3/gpio/gpioreg"
2021 "periph.io/x/conn/v3/physic"
2122 "periph.io/x/conn/v3/spi"
2223)
@@ -28,36 +29,36 @@ var _ draw.Image = &DevImpression{}
2829var (
2930 // For more: https://github.com/pimoroni/inky/issues/115#issuecomment-887453065
3031 dsc = []color.NRGBA {
31- {0 , 0 , 0 , 0 }, // Black
32+ {0 , 0 , 0 , 255 }, // Black
3233 {255 , 255 , 255 , 255 }, // White
3334 {0 , 255 , 0 , 255 }, // Green
3435 {0 , 0 , 255 , 255 }, // Blue
3536 {255 , 0 , 0 , 255 }, // Red
3637 {255 , 255 , 0 , 255 }, // Yellow
3738 {255 , 140 , 0 , 255 }, // Orange
38- {255 , 255 , 255 , 255 },
39+ {255 , 255 , 255 , 0 }, // Clear
3940 }
4041
4142 sc = []color.NRGBA {
42- {57 , 48 , 57 , 0 }, // Black
43+ {57 , 48 , 57 , 255 }, // Black
4344 {255 , 255 , 255 , 255 }, // White
4445 {58 , 91 , 70 , 255 }, // Green
4546 {61 , 59 , 94 , 255 }, // Blue
4647 {156 , 72 , 75 , 255 }, // Red
4748 {208 , 190 , 71 , 255 }, // Yellow
4849 {177 , 106 , 73 , 255 }, // Orange
49- {255 , 255 , 255 , 255 },
50+ {255 , 255 , 255 , 0 }, // Clear
5051 }
5152
5253 sc7 = []color.NRGBA {
53- {0 , 0 , 0 , 0 }, // Black
54+ {0 , 0 , 0 , 255 }, // Black
5455 {217 , 242 , 255 , 255 }, // White
5556 {3 , 124 , 76 , 255 }, // Green
5657 {27 , 46 , 198 , 255 }, // Blue
5758 {245 , 80 , 34 , 255 }, // Red
5859 {255 , 255 , 68 , 255 }, // Yellow
5960 {239 , 121 , 44 , 255 }, // Orange
60- {255 , 255 , 255 , 255 },
61+ {255 , 255 , 255 , 0 }, // Clear
6162 }
6263)
6364
@@ -155,7 +156,7 @@ func NewImpression(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinI
155156 if o .Model == IMPRESSION73 {
156157 cSpeed = acSpeed
157158 }
158- c , err := p .Connect (cSpeed , spi .Mode0 , cs0Pin )
159+ c , err := p .Connect (cSpeed , spi .Mode0 , 8 )
159160 if err != nil {
160161 return nil , fmt .Errorf ("failed to connect to inky over spi: %v" , err )
161162 }
@@ -173,6 +174,11 @@ func NewImpression(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinI
173174 maxTxSize = 4096 // Use a conservative default.
174175 }
175176 }
177+ // If possible, grab the CS pin.
178+ cs := gpioreg .ByName (cs0Pin )
179+ if cs != nil && cs .Out (csDisabled ) != nil {
180+ cs = nil
181+ }
176182
177183 d := & DevImpression {
178184 Dev : Dev {
@@ -186,6 +192,7 @@ func NewImpression(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinI
186192 model : o .Model ,
187193 variant : o .DisplayVariant ,
188194 pcbVariant : o .PCBVariant ,
195+ cs : cs ,
189196 },
190197 saturation : 50 , // Looks good enough for most of the images.
191198 }
@@ -217,10 +224,10 @@ func NewImpression(p spi.Port, dc gpio.PinOut, reset gpio.PinOut, busy gpio.PinI
217224}
218225
219226// blend recalculates the palette based on the saturation level.
220- func (d * DevImpression ) blend () [] color.Color {
221- sat := float64 (d .saturation / 100 )
227+ func (d * DevImpression ) blend () color.Palette {
228+ sat := float64 (d .saturation ) / 100.0
222229
223- pr := []color.Color {}
230+ pr := make ( []color.Color , 0 )
224231 for i := 0 ; i < 7 ; i ++ {
225232 var rs , gs , bs uint8
226233 if d .Dev .model == IMPRESSION73 {
@@ -288,7 +295,7 @@ func (d *DevImpression) Render() error {
288295
289296 merged := make ([]uint8 , len (d .Pix )/ 2 )
290297 for i , offset := 0 , 0 ; i < len (d .Pix )- 1 ; i , offset = i + 2 , offset + 1 {
291- merged [offset ] = (d .Pix [i ]<< 4 ) & 0xF0 | d .Pix [i + 1 ]& 0x0F
298+ merged [offset ] = (( d .Pix [i ] << 4 ) & 0xF0 ) | ( d .Pix [i + 1 ] & 0x0F )
292299 }
293300
294301 return d .update (merged )
@@ -519,17 +526,16 @@ func (d *DevImpression) updateAC(pix []uint8) error {
519526 }
520527
521528 // TODO there has to be a better way to force the white colour to be used instead of clear...
522- buf := make ([]byte , len (pix ))
523529 for i := range pix {
524530 if pix [i ]& 0xF == 7 {
525- buf [i ] = (pix [i ] & 0xF0 ) + 1
531+ pix [i ] = (pix [i ] & 0xF0 ) + 1
526532 }
527533 if pix [i ]& 0xF0 == 0x70 {
528- buf [i ] = (pix [i ] & 0xF ) + 0x10
534+ pix [i ] = (pix [i ] & 0xF ) + 0x10
529535 }
530536 }
531537
532- if err := d .sendCommand (ac073TC1DTM , buf ); err != nil {
538+ if err := d .sendCommand (ac073TC1DTM , pix ); err != nil {
533539 return err
534540 }
535541
@@ -558,8 +564,28 @@ func (d *DevImpression) wait(dur time.Duration) {
558564 log .Printf ("Err: %s" , err )
559565 return
560566 }
567+ if d .busy .Read () == gpio .High {
568+ time .Sleep (dur )
569+ return
570+ }
561571 // Wait for rising edges (Low -> High) or the timeout.
562- d .busy .WaitForEdge (dur )
572+ tEnd := time .Now ().Add (dur )
573+ edgeDur := dur
574+ for tEnd .After (time .Now ()) {
575+ // Debounce the edge
576+ edge := d .busy .WaitForEdge (edgeDur )
577+ if edge {
578+ // The python driver is using 10ms debounce period
579+ time .Sleep (10 * time .Millisecond )
580+ l := d .busy .Read ()
581+ if l {
582+ // It's still high. Return
583+ return
584+ }
585+ // It was a bounce. Recalculate the duration to wait for the edge.
586+ edgeDur = time .Until (tEnd )
587+ }
588+ }
563589}
564590
565591// ColorModel returns the device native color model.
@@ -589,7 +615,7 @@ func (d *DevImpression) Set(x, y int, c color.Color) {
589615// Draw updates the display with the image.
590616func (d * DevImpression ) Draw (r image.Rectangle , src image.Image , sp image.Point ) error {
591617 if r != d .Bounds () {
592- return fmt .Errorf ("partial updates are not supported" )
618+ return fmt .Errorf ("partial updates are not supported r=%#v bounds=%#v" , r , d . Bounds () )
593619 }
594620
595621 if src .Bounds () != d .Bounds () {
@@ -598,6 +624,7 @@ func (d *DevImpression) Draw(r image.Rectangle, src image.Image, sp image.Point)
598624
599625 // Dither the image using Floyd–Steinberg dithering algorithm otherwise it won't look as good on the screen.
600626 draw .FloydSteinberg .Draw (d , r , src , image.Point {})
627+
601628 return d .Render ()
602629}
603630
0 commit comments