; httpform.vnm ; Illustrates HTML forms in Venom ; ; In an application where the VM2 is running a web server, ; HTML forms and buttons with GET parameters in their links ; are two means for user input to the VM2 ; ; This simple application illutates these by enabling the user to control ; the colour of a panel by adjusting the red, green and blue components ; ; Techniques used: ; * Buttons with direct links back to the same page to change one parameter at at time ; * a form in which three numerical values are edited and then sent with a submit button ; * use of numerical values to alter the colour and shape of HTML elements, in this case ; creating a simple bar graph ; ; *** This code only works on Venom 2 release 2010-11-09 or later *** ; ; Some experience of HTML is inevitably required to create applications like this. ; ; to test this program, run on your local LAN, and use a web browser to view ; http://vm2form/form.html ; or (if that doesn't work) ; http://aaa.bbb.dccc.ddd/form.html ; where aaa.bbb.ccc.ddd is the IP address displayed when the program starts To init Make fs FileSystem("fla") ; some graphics files will be in flash file system ; attempt to register with DHCP Make eth Ethernet myipaddress := eth.Address('i') ; set initial colour red := 100 green := 200 blue := 150 End To main Print "server running on IP address ", myipaddress:"IP", CR Start server_task(1) Start server_task(2) ; At this point we could run a control system concurrently with the web server tasks ; Here it's just an idle loop Forever Wait(10) End To server_task(n) AutoDestruct Local http := New HTTPServer(fs) Local req := New String(100) ;Local s := New String(20) Local r, g, b ; temporary colour values ;Local textcolor Print "server task ", n:1, CR Forever [ http.Get(req) Print "req: from ", http.Address:"IP", " : ", req, CR If req.Compare("form.html") = 0 OrElse req.Compare("/") = 0 [ ; test for existence of each value by requesting as a string first r := http.Value("red", red) g := http.Value("green", green) b := http.Value("blue", blue) ; sanitize input: ignore values out of range If r >= 0 AndAlso r <= 255 red := r If g >= 0 AndAlso g <= 255 green := g If b >= 0 AndAlso b <= 255 blue := b ; make sure browser requests data each time instead of using cache http.Period := 0 http.PrintF("<html><head><title>VM2 HTTP form demo</title>\n") http.PrintF("<style>button, input, div {margin: 3px}</style>\n") http.PrintF("\n</head>\n<body>") http.PrintF("<form method=\"form.html\" method=\"GET\">\n") nudgebutton(http, 0, "red", red) nudgebutton(http, 1, "red", red) bar(http, "red", red) http.PrintF("<input size=4 name=\"red\" value=\"%u\">\n red (0..255)<br>", red) nudgebutton(http, 0, "green", green) nudgebutton(http, 1, "green", green) bar (http, "green", green) http.PrintF("<input size=4 name=\"green\" value=\"%u\">\n green (0..255)<br>", green) nudgebutton(http, 0, "blue", blue) nudgebutton(http, 1, "blue", blue) bar(http, "blue", blue) http.PrintF("<input size=4 name=\"blue\" value=\"%u\">\n blue (0..255)<br>", blue) http.PrintF("<input type=\"submit\"></form><br>\n") If (red + blue + green) > 400 textcolour := "black" Else textcolour := "white" http.PrintF("<div style=\"background: rgb(%u, %u, %u); color: %s; padding: 30px\">", red, green, blue, textcolour) http.PrintF("background example<br>R=%u G=%u B=%u = #%02x%02x%02x</div>\n</body>\n</html>\n", red, green, blue, red, green, blue) ] ; if request wasn't recognised, we have sent no data ; and the http object will generate a 404 response http.Flush ] End ; server_task ; code to create one '+' or '-' button To nudgebutton(http, up, bname, bvalue) If up bvalue := bvalue + 16 Else bvalue := bvalue - 16 If bvalue > 255 bvalue := 255 If bvalue < 0 bvalue := 0 ; force into range of preferred values: 11, 22, 33 ... ff bvalue := (bvalue And $f0) Or (bvalue >> 4) If up up := "+" Else up := "-" http.PrintF("<button type=\"button\" onclick=\"location = 'form.html?%s=%u'\">%s</button>", bname, bvalue, up) End ; draw one coloured bar To bar(http, colourname, colourvalue) http.PrintF("\n<div style=\"width: 200px; border: 1px solid %s; float: left\">", colourname) http.PrintF("\n<span style=\"width: %upx; background: %s; float: left\">&nbsp;\n</div></div>\n", (colourvalue * 200) Div 256, colourname) End ; bar