]> code.octet-stream.net Git - broadcaster/blob - broadcaster-radio/gpio.go
Add licence, etc.
[broadcaster] / broadcaster-radio / gpio.go
1 package main
2
3 import (
4 gpio "github.com/warthog618/go-gpiocdev"
5 "github.com/warthog618/go-gpiocdev/device/rpi"
6 "log"
7 "strconv"
8 )
9
10 type PTT interface {
11 EngagePTT()
12 DisengagePTT()
13 }
14
15 type COS interface {
16 WaitForChannelClear()
17 COSValue() bool
18 }
19
20 var ptt PTT = &DefaultPTT{}
21 var cos COS = &DefaultCOS{}
22
23 type PiPTT struct {
24 pttLine *gpio.Line
25 }
26
27 type PiCOS struct {
28 cosLine *gpio.Line
29 clearWait chan bool
30 }
31
32 func InitRaspberryPiPTT(pttNum int, chipName string) {
33 pttPin, err := rpi.Pin("GPIO" + strconv.Itoa(pttNum))
34 if err != nil {
35 log.Fatal("invalid PTT pin configured", ptt)
36 }
37 pttLine, err := gpio.RequestLine(chipName, pttPin, gpio.AsOutput(0))
38 if err != nil {
39 log.Fatal("unable to open requested pin for PTT GPIO:", ptt, ". Are you running as root?")
40 }
41 ptt = &PiPTT{
42 pttLine: pttLine,
43 }
44 }
45
46 func InitRaspberryPiCOS(cosNum int, chipName string) {
47 var piCOS PiCOS
48 piCOS.clearWait = make(chan bool)
49 cosPin, err := rpi.Pin("GPIO" + strconv.Itoa(cosNum))
50 if err != nil {
51 log.Fatal("invalid COS Pin configured", cos)
52 }
53 cosHandler := func(event gpio.LineEvent) {
54 if event.Type == gpio.LineEventFallingEdge {
55 log.Println("COS: channel clear")
56 close(piCOS.clearWait)
57 piCOS.clearWait = make(chan bool)
58 statusCollector.COS <- false
59 }
60 if event.Type == gpio.LineEventRisingEdge {
61 log.Println("COS: channel in use")
62 statusCollector.COS <- true
63 }
64 }
65 cosLine, err := gpio.RequestLine(chipName, cosPin, gpio.AsInput, gpio.WithBothEdges, gpio.WithEventHandler(cosHandler))
66 if err != nil {
67 log.Fatal("unable to open requested pin for COS GPIO:", cos, ". Are you running as root?")
68 }
69 piCOS.cosLine = cosLine
70 cos = &piCOS
71 }
72
73 func (g *PiCOS) COSValue() bool {
74 val, err := g.cosLine.Value()
75 if err != nil {
76 log.Fatal("Unable to read COS value")
77 }
78 return val != 0
79 }
80
81 func (g *PiCOS) WaitForChannelClear() {
82 ch := g.clearWait
83 val, err := g.cosLine.Value()
84 if err != nil || val == 0 {
85 return
86 }
87 // wait for close
88 <-ch
89 }
90
91 func (g *PiPTT) EngagePTT() {
92 log.Println("PTT: on")
93 g.pttLine.SetValue(1)
94 statusCollector.PTT <- true
95 }
96
97 func (g *PiPTT) DisengagePTT() {
98 log.Println("PTT: off")
99 g.pttLine.SetValue(0)
100 statusCollector.PTT <- false
101 }
102
103 type DefaultPTT struct {
104 }
105
106 func (g *DefaultPTT) EngagePTT() {
107 statusCollector.PTT <- true
108 }
109
110 func (g *DefaultPTT) DisengagePTT() {
111 statusCollector.PTT <- false
112 }
113
114 type DefaultCOS struct {
115 }
116
117 func (g *DefaultCOS) WaitForChannelClear() {
118 log.Println("Assuming channel is clear since COS GPIO is not configured")
119 }
120
121 func (g *DefaultCOS) COSValue() bool {
122 return false
123 }