-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buzzer.go
156 lines (135 loc) · 4.49 KB
/
buzzer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"github.com/sirupsen/logrus"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/platforms/raspi"
)
const (
// buzzerColorRed represents a physical buzzer in color red
buzzerColorRed string = "red"
// buttonRed represents a physical buzzer in color green
buzzerColorGreen string = "green"
// buttonRed represents a physical buzzer in color blue
buzzerColorBlue string = "blue"
// buttonRed represents a physical buzzer in color yellow
buzzerColorYellow string = "yellow"
// buzzerPinRed represents the Raspberry Pi GPIO pin where the
// physical buzzer in color red is connnected
buzzerPinRed = "40"
// buzzerPinGreen represents the Raspberry Pi GPIO pin where the
// physical buzzer in color green is connnected
buzzerPinGreen = "38"
// buzzerPinBlue represents the Raspberry Pi GPIO pin where the
// physical buzzer in color blue is connnected
buzzerPinBlue = "36"
// buzzerPinYellow represents the Raspberry Pi GPIO pin where the
// physical buzzer in color yellow is connnected
buzzerPinYellow = "32"
)
// buzzerHit represents the message that will
// be sent once a buzzer was hit.
type buzzerHit struct {
// Color is the color of the buzzer that was hit
// see constants buzzerColor*
Color string
}
// Buzzer represents an interface of different kind of buzzers.
// Typical usecases are hardware buzzers or software buzzers
// e.g., to emulate the hardware.
type Buzzer interface {
// Initialize sets up everything that is needed to run the buzzer.
Initialize() error
// Start boots up the buzzer and ensures that they are ready to hit.
Start() error
}
// HardwareBuzzer is the implementation of the Buzzer
// interface for physical hardware buzzers.
// See https://github.com/andygrunwald/things-with-buzzers-hardware
// for more details
type HardwareBuzzer struct {
buzzerStream chan buzzerHit
robot *gobot.Robot
logger *logrus.Logger
}
// NewHardwareBuzzer returns a new instance of
// the hardware buzzer stream
func NewHardwareBuzzer(buzzer chan buzzerHit, logger *logrus.Logger) Buzzer {
b := &HardwareBuzzer{
buzzerStream: buzzer,
logger: logger,
}
return b
}
// Initialize sets up everything that is needed to run the buzzer.
func (b *HardwareBuzzer) Initialize() error {
// Usage of https://gobot.io/ for
// dealing with physical buzzers.
//
// Whatever you do with the GPIO pins
// the raw BCM2835 pinout mapping to Raspberry Pi at
// https://godoc.org/github.com/stianeikeland/go-rpio
// is super helpful.
r := raspi.NewAdaptor()
red := gpio.NewButtonDriver(r, buzzerPinRed)
green := gpio.NewButtonDriver(r, buzzerPinGreen)
blue := gpio.NewButtonDriver(r, buzzerPinBlue)
yellow := gpio.NewButtonDriver(r, buzzerPinYellow)
work := func() {
red.On(gpio.ButtonPush, func(data interface{}) {
msg := buzzerHit{
Color: buzzerColorRed,
}
b.buzzerStream <- msg
// TODO Add bash color to visualize it better
// See https://github.com/andygrunwald/things-with-buzzers-hardware/blob/master/software/go/main.go#L65
b.logger.WithFields(logrus.Fields{
"color": "red",
}).Info("buzzer pressed")
})
green.On(gpio.ButtonPush, func(data interface{}) {
msg := buzzerHit{
Color: buzzerColorGreen,
}
b.buzzerStream <- msg
// TODO Add bash color to visualize it better
// See https://github.com/andygrunwald/things-with-buzzers-hardware/blob/master/software/go/main.go#L65
b.logger.WithFields(logrus.Fields{
"color": "green",
}).Info("buzzer pressed")
})
blue.On(gpio.ButtonPush, func(data interface{}) {
msg := buzzerHit{
Color: buzzerColorBlue,
}
b.buzzerStream <- msg
// TODO Add bash color to visualize it better
// See https://github.com/andygrunwald/things-with-buzzers-hardware/blob/master/software/go/main.go#L65
b.logger.WithFields(logrus.Fields{
"color": "blue",
}).Info("buzzer pressed")
})
yellow.On(gpio.ButtonPush, func(data interface{}) {
msg := buzzerHit{
Color: buzzerColorYellow,
}
b.buzzerStream <- msg
// TODO Add bash color to visualize it better
// See https://github.com/andygrunwald/things-with-buzzers-hardware/blob/master/software/go/main.go#L65
b.logger.WithFields(logrus.Fields{
"color": "yellow",
}).Info("buzzer pressed")
})
}
b.robot = gobot.NewRobot("buttonBot",
[]gobot.Connection{r},
[]gobot.Device{red, green, blue, yellow},
work,
)
return nil
}
// Start boots up the buzzer and ensures that they are ready to hit.
func (b *HardwareBuzzer) Start() error {
err := b.robot.Start()
return err
}