-
Notifications
You must be signed in to change notification settings - Fork 0
/
webui.go
91 lines (73 loc) · 2.18 KB
/
webui.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
/**
HTML5 UI
Hosted by this module.
Used when testing on PC without actual oled/led/lcd.. display and keyboard
**/
package bwGoScreenManager
import (
"fmt"
"io"
"net/http"
"golang.org/x/net/websocket"
"github.com/hjkoskel/gomonochromebitmap"
"image"
"bytes"
"image/png"
"image/color"
"strconv"
)
//TODO WS implementation later
func wsHandler(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func writePNG(w http.ResponseWriter, img *image.Image) {
buffer := new(bytes.Buffer)
if err := png.Encode(buffer, *img); err != nil {
fmt.Println("unable to encode image.")
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
if _, err := w.Write(buffer.Bytes()); err != nil {
fmt.Println("unable to write image.")
}
}
var imgNow image.Image //TODO Mutexlock?
func serveScreen(w http.ResponseWriter, r *http.Request) {
writePNG(w,&imgNow)
}
var keyPressChan *chan string
func serveKey(w http.ResponseWriter, r *http.Request) {
key:=r.URL.Query().Get("key")
fmt.Printf("UI key is %#v\n",key)
switch(key){
case "BKSP":
(*keyPressChan)<-"\b"
case "ENTER":
(*keyPressChan)<-"\n"
case "TAB":
(*keyPressChan)<-"\t"
default:
(*keyPressChan)<-key
}
(*keyPressChan)<-""
}
func HostWebUI(port int, web_KeyPresses chan string,web_ImageOutput chan gomonochromebitmap.MonoBitmap){
go func(){
colTrue:=color.RGBA{R:200,G:200,B:200,A:255}
colFalse:=color.RGBA{R:40,G:40,B:40,A:255}
for{
bw:=<-web_ImageOutput
imgNow=bw.GetImage(colTrue,colFalse)
}
}()
keyPressChan=&web_KeyPresses
http.Handle("/", http.FileServer(http.Dir("./staticFiles/")))
http.Handle("/ws", websocket.Handler(wsHandler))
http.HandleFunc("/screen.png",serveScreen)
http.HandleFunc("/key",serveKey)
fmt.Printf("Serving web ui on port %v\n",port)
err := http.ListenAndServe(fmt.Sprintf(":%v",port), nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}