This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
morse.go
156 lines (145 loc) · 3.08 KB
/
morse.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 hoin
import (
"fmt"
"strings"
"time"
)
func stringToMorse(s string) []int {
var data []int
for _, word := range strings.Split(strings.ToLower(s), " ") {
for _, r := range word {
var timings []int
switch r {
case 'a':
timings = []int{1, 3}
case 'b':
timings = []int{3, 1, 1, 1}
case 'c':
timings = []int{3, 1, 3, 1}
case 'd':
timings = []int{3, 1, 1}
case 'e':
timings = []int{1}
case 'f':
timings = []int{1, 1, 3, 1}
case 'g':
timings = []int{3, 3, 1}
case 'h':
timings = []int{1, 1, 1, 1}
case 'i':
timings = []int{1, 1}
case 'j':
timings = []int{1, 3, 3, 3}
case 'k':
timings = []int{3, 1, 3}
case 'l':
timings = []int{1, 3, 1, 1}
case 'm':
timings = []int{3, 3}
case 'n':
timings = []int{3, 1}
case 'o':
timings = []int{3, 3, 3}
case 'p':
timings = []int{1, 3, 3, 1}
case 'q':
timings = []int{3, 3, 1, 3}
case 'r':
timings = []int{1, 3, 1}
case 's':
timings = []int{1, 1, 1}
case 't':
timings = []int{3}
case 'u':
timings = []int{1, 1, 3}
case 'v':
timings = []int{1, 1, 1, 3}
case 'w':
timings = []int{1, 3, 3}
case 'x':
timings = []int{3, 1, 1, 3}
case 'y':
timings = []int{3, 1, 3, 3}
case 'z':
timings = []int{3, 3, 1, 1}
case '0':
timings = []int{3, 3, 3, 3, 3}
case '1':
timings = []int{1, 3, 3, 3, 3}
case '2':
timings = []int{1, 1, 3, 3, 3}
case '3':
timings = []int{1, 1, 1, 3, 3}
case '4':
timings = []int{1, 1, 1, 1, 3}
case '5':
timings = []int{1, 1, 1, 1, 1}
case '6':
timings = []int{3, 1, 1, 1, 1}
case '7':
timings = []int{3, 3, 1, 1, 1}
case '8':
timings = []int{3, 3, 3, 1, 1}
case '9':
timings = []int{3, 3, 3, 3, 1}
default:
continue
}
for _, t := range timings {
if t == 0 {
data = append(data, 0, 0)
continue
}
data = append(data, t, 0)
}
data = append(data, make([]int, 3)...)
}
data = append(data, make([]int, 7)...)
}
return data
}
func printMorse(p Printer, message string, f func(t int) error) error {
errMsg := "could not send morse code beeps: %w"
data := stringToMorse(message)
for _, t := range data {
err := f(t)
if err != nil {
return fmt.Errorf(errMsg, err)
}
if t != 0 {
err := p.Beep(1, t)
if err != nil {
return fmt.Errorf(errMsg, err)
}
}
time.Sleep(100 * time.Millisecond)
}
return nil
}
// Morse beeps out the message in morse code
func (p Printer) Morse(message string) error {
return printMorse(p, message, func(t int) error { return nil })
}
// MorsePrint beeps out the morse message but also prints it to the paper
func (p Printer) MorsePrint(message string) error {
errMsg := "could not print morse: %w"
err := printMorse(p, message, func(t int) error {
var err error
if t == 0 {
err = p.Print(" ")
} else if t == 1 {
err = p.Print(".")
} else {
err = p.Print("-")
}
return err
})
if err != nil {
return fmt.Errorf(errMsg, err)
}
err = p.LF()
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}