-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_gui.jl
More file actions
97 lines (80 loc) · 3.05 KB
/
main_gui.jl
File metadata and controls
97 lines (80 loc) · 3.05 KB
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
using Interact
using Blink
#using Plots
using WAV
using PortAudio
include("ColorCode.jl")
function plotBelief(belief::Belief)
plot(OrderedDict(reverse([(string(k),v) for (k,v) in belief.b],dims=1)),
plot_title="Belief",
ylabel="Key",
xlabel="Probability",
seriestype=:bar,
#bar_width=0.6,
orientation=:horizontal,
xlims=(0,1),
yticks=:all,
legend=false)
#size=(1100,500))
gui()
end
struct KeyboardFrontEnd
letters::OrderedDict{Symbol,Widget{:latex,String}}
commString::Widget{:latex,String}
end
function layoutKeyboard(keyboard::KeyboardFrontEnd)
letterObjs = collect(values(keyboard.letters))
return pad(1em,vbox(pad(0.5em,keyboard.commString),
vbox(pad(0.5em,hbox(map(x->pad(["right"],0.6em,x),letterObjs[1:9])...)),
pad(0.5em,hbox(map(x->pad(["right"],0.6em,x),letterObjs[10:18])...)),
pad(0.5em,hbox(map(x->pad(["right"],0.6em,x),letterObjs[19:26])...)),
pad(0.5em,hbox(map(x->pad(["right"],0.6em,x),letterObjs[27:end])...)))))
end
function renderAssignment(keyboard::KeyboardFrontEnd, assignment::Dict{Symbol,Int})
colors = ["red" "blue"]
foreach(x->keyboard.letters[x[1]][] = "\\fbox{\\color{$(colors[x[2]])} \\Huge \\texttt{$(keyboardStrings[x[1]])}}",assignment)
end
buttons = [button(x,style=Dict(:backgroundColor=>x)) for x in ["red" "blue"]]
keyboard = KeyboardFrontEnd(OrderedDict([sym => latex(str) for (sym,str) in keyboardStrings]),latex("\\Large\\textrm{|}"))
lmModel = LM.getModel()
lmState = LM.getStartState(lmModel)
prior = getPrior(lmModel,lmState)
commString = Ref("")
belief = Belief(prior,99,1)
history = BeliefHistory()
certaintyThreshold = 0.95
assignment = Dict([(k,1) for k in keys(keyboardStrings)])
changeAssignment(belief,assignment)
renderAssignment(keyboard,assignment)
#plotBelief(belief)
popSound,fs = wavread("pop.wav");
audioStream = PortAudioStream(0,2,adjust_channels=true)
write(audioStream,popSound) #stops lag for some reason does it? idk
function buttonCallback(button,commString)
updateBelief(belief,button,assignment)
newCommString = chooseLetter(belief,commString[],certaintyThreshold,history,lmModel,lmState)
if length(newCommString) != length(commString[])
@async write(audioStream,popSound)
end
commString[] = newCommString
changeAssignment(belief,assignment)
keyboard.commString[] = "\\Large\\textrm{$(replace(commString[]," "=>"\\ "))|}"
#plotBelief(belief)
renderAssignment(keyboard,assignment)
end
buttonCallbacks = [on(_->buttonCallback(n,commString),button) for (button,n) in zip(buttons,1:length(buttons))]
w = Window()
title(w,"ColorCode")
body!(w,vbox(layoutKeyboard(keyboard),pad(["left"],6em,hbox(pad(1em,buttons[1]), pad(1em,buttons[2])))))
js(w,Blink.JSString("""document.onkeydown = function (e) {Blink.msg("press",e.keyCode)}; """))
handle(w,"press") do key
if key == 37
buttonCallback(1,commString)
end
if key == 39
buttonCallback(2,commString)
end
end
wait()
LM.releaseModel(lmModel)
LM.releaseState(lmModel)