| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | import time | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | try: | 
					
						
							|  |  |  |     import ppwhttp | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     raise RuntimeError("Cannot find ppwhttp. Have you copied ppwhttp.py to your Pico?") | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | r = 0 | 
					
						
							|  |  |  | g = 0 | 
					
						
							|  |  |  | b = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Edit your routes here | 
					
						
							|  |  |  | # Nothing fancy is supported, just plain ol' URLs and GET/POST methods | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | @ppwhttp.route("/", methods=["GET", "POST"]) | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | def get_home(method, url, data=None): | 
					
						
							|  |  |  |     if method == "POST": | 
					
						
							|  |  |  |         global r, g, b | 
					
						
							|  |  |  |         r = int(data.get("r", 0)) | 
					
						
							|  |  |  |         g = int(data.get("g", 0)) | 
					
						
							|  |  |  |         b = int(data.get("b", 0)) | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  |         ppwhttp.set_led(r, g, b) | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  |         print("Set LED to {} {} {}".format(r, g, b)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return """<form method="post" action="/">
 | 
					
						
							|  |  |  |     <input id="r" name="r" type="number" value="{r}" /> | 
					
						
							|  |  |  |     <input name="g" type="number" value="{g}"  /> | 
					
						
							|  |  |  |     <input name="b" type="number" value="{b}"  /> | 
					
						
							|  |  |  |     <input type="submit" value="Set LED" /> | 
					
						
							|  |  |  | </form>""".format(r=r, g=g, b=b)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | @ppwhttp.route("/test", methods="GET") | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | def get_test(method, url): | 
					
						
							|  |  |  |     return "Hello World!" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | ppwhttp.start_wifi() | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | server_sock = ppwhttp.start_server() | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | while True: | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  |     ppwhttp.handle_http_request(server_sock) | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  |     time.sleep(0.01) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Whoa there! Did you know you could run the server polling loop | 
					
						
							|  |  |  | # on Pico's *other* core!? Here's how: | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # import _thread | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # def server_loop_forever(): | 
					
						
							|  |  |  | #    # Start a server and continuously poll for HTTP requests | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | #    server_sock = ppwhttp.start_server() | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | #    while True: | 
					
						
							| 
									
										
										
										
											2021-09-29 11:16:54 +00:00
										 |  |  | #        ppwhttp.handle_http_request(server_sock) | 
					
						
							| 
									
										
										
										
											2021-04-15 08:59:11 +00:00
										 |  |  | #        time.sleep(0.01) | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Handle the server polling loop on the other core! | 
					
						
							|  |  |  | # _thread.start_new_thread(server_loop_forever, ()) | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # # Your very own main loop for fun and profit! | 
					
						
							|  |  |  | # while True: | 
					
						
							|  |  |  | #     print("Colour: {} {} {}".format(r, g, b)) | 
					
						
							|  |  |  | #     time.sleep(5.0) |