Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -28,7 +28,7 @@ private
'G' => (3, "--. "), 'H' => (4, ".... "), 'I' => (2, ".. "),
'J' => (4, ".--- "), 'K' => (3, "-.- "), 'L' => (4, ".-.. "),
'M' => (2, "-- "), 'N' => (2, "-. "), 'O' => (3, "--- "),
'P' => (4, "--.- "), 'Q' => (4, "--.- "), 'R' => (3, ".-. "),
'P' => (4, ".--. "), 'Q' => (4, "--.- "), 'R' => (3, ".-. "),
'S' => (3, "... "), 'T' => (1, "- "), 'U' => (3, "..- "),
'V' => (4, "...- "), 'W' => (3, ".-- "), 'X' => (4, "-..- "),
'Y' => (4, "-.-- "), 'Z' => (4, "--.. "), '1' => (5, ".----"),

View file

@ -0,0 +1,9 @@
>~>48*-:0\`#@_2*::"!"%\"!"/3+g75v
^v('_')v!:-*57g+3/"!"\%"!":+1\-*<
^$$,*84_\#!:#:2#-%#15#\9#/*#2+#,<
##X)P)##Z*##3(D)5);(##8(/)A)8)9(#
($(&(*(2(B(A(?(;(3([)M)##1(##V)L)
$%1'-')&$$.''&2'&%$'%&0'#%%%#&,''
'(&*&#$&&*'$&)'%'/'########6)##$%
1'-')&$$.''&2'&%$'%&0'#%%%#&,'''(
&*&#$&&*'$&)'%'/'################

View file

@ -0,0 +1,26 @@
defmodule Morse do
@morse %{"!" => "---.", "\"" => ".-..-.", "$" => "...-..-", "'" => ".----.",
"(" => "-.--.", ")" => "-.--.-", "+" => ".-.-.", "," => "--..--",
"-" => "-....-", "." => ".-.-.-", "/" => "-..-.",
"0" => "-----", "1" => ".----", "2" => "..---", "3" => "...--",
"4" => "....-", "5" => ".....", "6" => "-....", "7" => "--...",
"8" => "---..", "9" => "----.",
":" => "---...", ";" => "-.-.-.", "=" => "-...-", "?" => "..--..",
"@" => ".--.-.",
"A" => ".-", "B" => "-...", "C" => "-.-.", "D" => "-..",
"E" => ".", "F" => "..-.", "G" => "--.", "H" => "....",
"I" => "..", "J" => ".---", "K" => "-.-", "L" => ".-..",
"M" => "--", "N" => "-.", "O" => "---", "P" => ".--.",
"Q" => "--.-", "R" => ".-.", "S" => "...", "T" => "-",
"U" => "..-", "V" => "...-", "W" => ".--", "X" => "-..-",
"Y" => "-.--", "Z" => "--..",
"[" => "-.--.", "]" => "-.--.-", "_" => "..--.-" }
def code(text) do
String.upcase(text)
|> String.codepoints
|> Enum.map(fn c -> Dict.get(@morse, c, " ") end)
|> Enum.join(" ")
end
end
IO.puts Morse.code("Hello, World!")

View file

@ -0,0 +1,153 @@
// Command morse translates an input string into morse code,
// showing the output on the console, and playing it as sound.
// Only works on ubuntu.
package main
import (
"flag"
"fmt"
"log"
"regexp"
"strings"
"syscall"
"time"
"unicode"
)
// A key represents an action on the morse key.
// It's either on or off, for the given duration.
type key struct {
duration int
on bool
sym string // for debug output
}
var (
runeToKeys = map[rune][]key{}
interCharGap = []key{{1, false, ""}}
punctGap = []key{{7, false, " / "}}
charGap = []key{{3, false, " "}}
wordGap = []key{{7, false, " / "}}
)
const rawMorse = `
A:.- J:.--- S:... 1:.---- .:.-.-.- ::---...
B:-... K:-.- T:- 2:..--- ,:--..-- ;:-.-.-.
C:-.-. L:.-.. U:..- 3:...-- ?:..--.. =:-...-
D:-.. M:-- V:...- 4:....- ':.----. +:.-.-.
E:. N:-. W:.-- 5:..... !:-.-.-- -:-....-
F:..-. O:--- X:-..- 6:-.... /:-..-. _:..--.-
G:--. P:.--. Y:-.-- 7:--... (:-.--. ":.-..-.
H:.... Q:--.- Z:--.. 8:---.. ):-.--.- $:...-..-
I:.. R:.-. 0:----- 9:----. &:.-... @:.--.-.
`
func init() {
// Convert the rawMorse table into a map of morse key actions.
r := regexp.MustCompile("([^ ]):([.-]+)")
for _, m := range r.FindAllStringSubmatch(rawMorse, -1) {
c := m[1][0]
keys := []key{}
for i, dd := range m[2] {
if i > 0 {
keys = append(keys, interCharGap...)
}
if dd == '.' {
keys = append(keys, key{1, true, "."})
} else if dd == '-' {
keys = append(keys, key{3, true, "-"})
} else {
log.Fatalf("found %c in morse for %c", dd, c)
}
runeToKeys[rune(c)] = keys
runeToKeys[unicode.ToLower(rune(c))] = keys
}
}
}
// MorseKeys translates an input string into a series of keys.
func MorseKeys(in string) ([]key, error) {
afterWord := false
afterChar := false
result := []key{}
for _, c := range in {
if unicode.IsSpace(c) {
afterWord = true
continue
}
morse, ok := runeToKeys[c]
if !ok {
return nil, fmt.Errorf("can't translate %c to morse", c)
}
if unicode.IsPunct(c) && afterChar {
result = append(result, punctGap...)
} else if afterWord {
result = append(result, wordGap...)
} else if afterChar {
result = append(result, charGap...)
}
result = append(result, morse...)
afterChar = true
afterWord = false
}
return result, nil
}
func main() {
var ditDuration time.Duration
flag.DurationVar(&ditDuration, "d", 40*time.Millisecond, "length of dit")
flag.Parse()
in := "hello world."
if len(flag.Args()) > 1 {
in = strings.Join(flag.Args(), " ")
}
keys, err := MorseKeys(in)
if err != nil {
log.Fatalf("failed to translate: %s", err)
}
for _, k := range keys {
if k.on {
if err := note(true); err != nil {
log.Fatalf("failed to play note: %s", err)
}
}
fmt.Print(k.sym)
time.Sleep(ditDuration * time.Duration(k.duration))
if k.on {
if err := note(false); err != nil {
log.Fatalf("failed to stop note: %s", err)
}
}
}
fmt.Println()
}
// Implement sound on ubuntu. Needs permission to access /dev/console.
var consoleFD uintptr
func init() {
fd, err := syscall.Open("/dev/console", syscall.O_WRONLY, 0)
if err != nil {
log.Fatalf("failed to get console device: %s", err)
}
consoleFD = uintptr(fd)
}
const KIOCSOUND = 0x4B2F
const clockTickRate = 1193180
const freqHz = 600
// note either starts or stops a note.
func note(on bool) error {
arg := uintptr(0)
if on {
arg = clockTickRate / freqHz
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, consoleFD, KIOCSOUND, arg)
if errno != 0 {
return errno
}
return nil
}

View file

@ -0,0 +1,93 @@
local M = {}
-- module-local variables
local BUZZER = pio.PB_10
local dit_length, dah_length, word_length
-- module-local functions
local buzz, dah, dit, init, inter_element_gap, medium_gap, pause, sequence, short_gap
buzz = function(duration)
pio.pin.output(BUZZER)
pio.pin.setlow(BUZZER)
tmr.delay(tmr.SYS_TIMER, duration)
pio.pin.sethigh(BUZZER)
pio.pin.input(BUZZER)
end
dah = function()
buzz(dah_length)
end
dit = function()
buzz(dit_length)
end
init = function(baseline)
dit_length = baseline
dah_length = 2 * baseline
word_length = 4 * baseline
end
inter_element_gap = function()
pause(dit_length)
end
medium_gap = function()
pause(word_length)
end
pause = function(duration)
tmr.delay(tmr.SYS_TIMER, duration)
end
sequence = function(codes)
if codes then
for _,f in ipairs(codes) do
f()
inter_element_gap()
end
short_gap()
end
end
short_gap = function()
pause(dah_length)
end
local morse = {
a = { dit, dah }, b = { dah, dit, dit, dit }, c = { dah, dit, dah, dit },
d = { dah, dit, dit }, e = { dit }, f = { dit, dit, dah, dit },
g = { dah, dah, dit }, h = { dit, dit, dit ,dit }, i = { dit, dit },
j = { dit, dah, dah, dah }, k = { dah, dit, dah }, l = { dit, dah, dit, dit },
m = { dah, dah }, n = { dah, dit }, o = { dah, dah, dah },
p = { dit, dah, dah, dit }, q = { dah, dah, dit, dah }, r = { dit, dah, dit },
s = { dit, dit, dit }, t = { dah }, u = { dit, dit, dah },
v = { dit, dit, dit, dah }, w = { dit, dah, dah }, x = { dah, dit, dit, dah },
y = { dah, dit, dah, dah }, z = { dah, dah, dit, dit },
["0"] = { dah, dah, dah, dah, dah }, ["1"] = { dit, dah, dah, dah, dah },
["2"] = { dit, dit, dah, dah, dah }, ["3"] = { dit, dit, dit, dah, dah },
["4"] = { dit, dit, dit, dit, dah }, ["5"] = { dit, dit, dit, dit, dit },
["6"] = { dah, dit, dit, dit, dit }, ["7"] = { dah, dah, dit, dit, dit },
["8"] = { dah, dah, dah, dit, dit }, ["9"] = { dah, dah, dah, dah, dit },
[" "] = { medium_gap }
}
-- public interface
M.beep = function(message)
message = message:lower()
for _,ch in ipairs { message:byte(1, #message) } do
sequence(morse[string.char(ch)])
end
end
M.set_dit = function(duration)
init(duration)
end
-- initialization code
init(50000)
return M

View file

@ -0,0 +1,2 @@
morse = require 'morse'
morse.beep "I am the very model of a modern major-general."

View file

@ -1,5 +1,5 @@
my %m = ' ', '_ _ ',
<
|<
! ---.
" .-..-.
$ ...-..-
@ -58,7 +58,7 @@ my %m = ' ', '_ _ ',
>.map: -> $c, $m is copy {
$m.=subst(rx/'-'/, 'BGAAACK!!! ', :g);
$m.=subst(rx/'.'/, 'buck ', :g);
$c => $m ~ '_ ';
$c => $m ~ '_';
}
say prompt("Gimme a string: ").uc.comb.map: { %m{$_} // "<scratch> " }