5
5
package ssd1306_test
6
6
7
7
import (
8
+ "flag"
9
+ "fmt"
8
10
"image"
11
+ "image/color"
12
+ "image/draw"
9
13
"log"
14
+ "math"
15
+ "time"
10
16
11
17
"periph.io/x/conn/v3/i2c/i2creg"
12
18
"periph.io/x/devices/v3/ssd1306"
@@ -19,35 +25,80 @@ func Example() {
19
25
if _ , err := host .Init (); err != nil {
20
26
log .Fatal (err )
21
27
}
22
-
28
+ var width = flag .Int ("width" , 128 , "Display Width" )
29
+ var height = flag .Int ("height" , 64 , "Display Height" )
30
+ flag .Parse ()
23
31
// Use i2creg I²C bus registry to find the first available I²C bus.
24
32
b , err := i2creg .Open ("" )
25
33
if err != nil {
26
34
log .Fatal (err )
27
35
}
28
36
defer b .Close ()
29
-
30
- dev , err := ssd1306 .NewI2C (b , & ssd1306 .DefaultOpts )
37
+ opts := ssd1306 .DefaultOpts
38
+ opts .W = * width
39
+ opts .H = * height
40
+ if opts .H == 32 {
41
+ opts .Sequential = true
42
+ }
43
+ dev , err := ssd1306 .NewI2C (b , & opts )
31
44
if err != nil {
32
- log .Fatalf ("failed to initialize ssd1306 : %v " , err )
45
+ log .Fatalf ("failed to initialize display : %s " , err . Error () )
33
46
}
47
+ fmt .Printf ("device=%s\n " , dev .String ())
34
48
35
- // Draw on it.
36
49
img := image1bit .NewVerticalLSB (dev .Bounds ())
37
50
// Note: this code is commented out so periph does not depend on:
38
51
// "golang.org/x/image/font"
39
52
// "golang.org/x/image/font/basicfont"
40
53
// "golang.org/x/image/math/fixed"
41
54
//
42
- // f := basicfont.Face7x13
43
- // drawer := font.Drawer{
44
- // Dst: img,
45
- // Src: &image.Uniform{image1bit.On},
46
- // Face: f,
47
- // Dot: fixed.P(0, img.Bounds().Dy()-1-f.Descent),
48
- // }
49
- // drawer.DrawString("Hello from periph!")
55
+ // Draw on it.
56
+ /*
57
+ f := basicfont.Face7x13
58
+ drawer := font.Drawer{
59
+ Dst: img,
60
+ Src: &image.Uniform{image1bit.On},
61
+
62
+ Face: f,
63
+ Dot: fixed.P(0, img.Bounds().Dy()-1-f.Descent),
64
+ }
65
+ drawer.DrawString("Hello from periph!")
66
+ _ = dev.Draw(dev.Bounds(), img, image.Point{})
67
+ time.Sleep(5 * time.Second)
68
+ */
69
+
70
+ white := color.RGBA {255 , 255 , 255 , 255 }
71
+ black := color.RGBA {0 , 0 , 0 , 255 }
72
+ colors := []color.RGBA {white , black }
73
+
74
+ rectNum := 0
75
+ // Draw some nested rectangles
76
+ for w , h := opts .W , opts .H ; w > 0 && h > 0 ; w , h = w - 4 , h - 4 {
77
+ rect := image .Rect (0 , 0 , w , h )
78
+ draw .Draw (img , rect .Add (image.Point {X : rectNum * 2 , Y : rectNum * 2 }), & image.Uniform {colors [rectNum % 2 ]}, image.Point {}, draw .Src )
79
+ rectNum += 1
80
+ }
81
+
50
82
if err := dev .Draw (dev .Bounds (), img , image.Point {}); err != nil {
51
83
log .Fatal (err )
52
84
}
85
+ time .Sleep (5 * time .Second )
86
+
87
+ // Draw a Sine Wave
88
+ _ = dev .Invert (true )
89
+ img = image1bit .NewVerticalLSB (dev .Bounds ())
90
+ img .DrawHLine (0 , opts .W , opts .H >> 1 - 1 , image1bit .On )
91
+ img .DrawVLine (0 , opts .H , opts .W >> 1 - 1 , image1bit .On )
92
+ angle := float64 (0 )
93
+ angleStep := float64 ((4 * math .Pi ) / float64 (opts .W ))
94
+ scale := float64 ((opts .H >> 1 ) - 4 )
95
+ for step := opts .W - 1 ; step >= 0 ; step -= 1 {
96
+ y := int (float64 (math .Sin (angle )* scale )) + opts .H >> 1
97
+ img .SetBit (step , y , image1bit .On )
98
+ angle += angleStep
99
+ }
100
+ _ = dev .Draw (dev .Bounds (), img , image.Point {})
101
+ time .Sleep (10 * time .Second )
102
+
103
+ _ = dev .Halt ()
53
104
}
0 commit comments