-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
123 lines (104 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"log"
"time"
mcp "github.com/ardnew/mcp2221a"
)
func main() {
m, err := mcp.New(0, mcp.VID, mcp.PID)
if nil != err {
log.Fatalf("Open(): %v", err)
}
defer m.Close()
log.Print(mcp.PackageVersion())
// reset device to default settings stored in flash memory
if err := m.Reset(5 * time.Second); nil != err {
log.Fatalf("Reset(): %v", err)
}
// print the description strings stored in flash memory
if s, err := m.USBManufacturer(); nil != err {
log.Fatalf("USBManufacturer(): %s", err)
} else {
log.Printf("USB Manufacturer = \"%s\"", s)
}
if s, err := m.USBProduct(); nil != err {
log.Fatalf("USBProduct(): %s", err)
} else {
log.Printf("USB Product = \"%s\"", s)
}
if s, err := m.USBSerialNo(); nil != err {
log.Fatalf("USBSerialNo(): %s", err)
} else {
log.Printf("USB Serial No = \"%s\"", s)
}
if s, err := m.FactorySerialNo(); nil != err {
log.Fatalf("FactorySerialNo(): %s", err)
} else {
log.Printf("Factory Serial No = \"%s\"", s)
}
pin := byte(0)
log.Printf("setting pin %d = 0 (Flash, non-volatile)", pin)
if err := m.GPIO.FlashConfig(pin, 0, mcp.ModeGPIO, mcp.DirOutput); nil != err {
log.Fatalf("GPIO.FlashConfig(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
time.Sleep(2 * time.Second)
// --
log.Printf("setting pin %d = 1 (SRAM, volatile)", pin)
if err := m.GPIO.SetConfig(pin, 1, mcp.ModeGPIO, mcp.DirOutput); nil != err {
log.Fatalf("GPIO.SetConfig(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
time.Sleep(2 * time.Second)
// --
log.Printf("resetting device ...")
if err := m.Reset(5 * time.Second); nil != err {
log.Fatalf("Reset(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
time.Sleep(2 * time.Second)
// --
log.Printf("setting pin %d = 1 (Flash, non-volatile)", pin)
if err := m.GPIO.FlashConfig(pin, 1, mcp.ModeGPIO, mcp.DirOutput); nil != err {
log.Fatalf("GPIO.FlashConfig(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
time.Sleep(2 * time.Second)
// --
log.Printf("setting pin %d = 0 (SRAM, volatile)", pin)
if err := m.GPIO.SetConfig(pin, 0, mcp.ModeGPIO, mcp.DirOutput); nil != err {
log.Fatalf("GPIO.SetConfig(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
time.Sleep(2 * time.Second)
// --
log.Printf("resetting device ...")
if err := m.Reset(5 * time.Second); nil != err {
log.Fatalf("Reset(): %v", err)
}
if get, err := m.GPIO.Get(pin); nil != err {
log.Fatalf("GPIO.Get(): %v", err)
} else {
log.Printf("Pin[%d] = %d", pin, get)
}
}