Added display print API

This commit is contained in:
Gregor Riepl 2022-01-16 23:57:43 +01:00
parent 55d5eb0c6e
commit daa199b6bb
3 changed files with 3785 additions and 3778 deletions

View file

@ -236,7 +236,7 @@ static void test2() {
int main() {
init();
while (1) {
test();
loop();
//test();
}
}

File diff suppressed because it is too large Load diff

View file

@ -11,6 +11,8 @@ import (
const (
// highest slot number (range: 0..maxSlot)
maxSlot = 4
// highest display line number (range: 0..maxLine)
maxLine = 1
)
type Server struct {
@ -106,6 +108,33 @@ func (s *Server) registerHandlers() {
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
})
s.HandleFunc("/display/", func(rw http.ResponseWriter, r *http.Request) {
if matched, _ := path.Match("/display/[0-9]*", r.URL.Path); matched {
line, err := strconv.ParseUint(path.Base(r.URL.Path), 10, 32)
if err != nil {
log.Printf("Error decoding line number: %v", err)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else if line < 0 || line > maxLine {
log.Printf("Invalid line number: %d", line)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
} else {
query := r.URL.Query()
if _, ok := query["text"]; ok {
text := query.Get("text")
s.can.DisplayPrint(int(line), text)
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "Printed on line %d: '%s'", int(line), text)
} else {
log.Printf("Invalid query, missing text")
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}
} else {
log.Printf("Mismatched path: %s", r.URL.Path)
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
})
}
func (s *Server) Start() error {