from wifi import Wifi from config import Config from toy import Toy max_laser_power = 0.25 html = """ Cat Toy

Cat Toy

%s

Laser Pointer

Turn power:
Or just turn it:

Pan and Tilt Servos

Pan:
Tilt:
%s """ t = Toy() def rootCallback(request): return html % ( '

Welcome to the Cat Toy interface by xythobuz.

', round(max_laser_power * 100.0), '' ) def servoCallback(request): q = request.find("/servos?") p1 = request.find("s1=") p2 = request.find("s2=") if (q < 0) or (p1 < 0) or (p2 < 0): return html % ( '

Error: no servo arguments found in URL query string.

', round(max_laser_power * 100.0), '

Back to main page

' ) servos = [p1, p2] result = [] for p in servos: pe = request.find("&s", p) if (pe < 0) or (p + 3 >= pe) or (pe - (p + 3) > 3): pe = request.find(" HTTP", p) if (pe < 0) or (p + 3 >= pe) or (pe - (p + 3) > 3): return html % ( '

Error parsing query string.

', round(max_laser_power * 100.0), '

Back to main page

' ) r = request[p + 3 : pe] s = int(r) result.append(s) if result[0] < t.pan_min: result[0] = t.pan_min if result[0] > t.pan_max: result[0] = t.pan_max t.angle(t.pan, result[0]) if result[1] < t.tilt_min: result[1] = t.tilt_min if result[1] > t.tilt_max: result[1] = t.tilt_max t.angle(t.tilt, result[1]) return html % ( '

Servos move to s1=' + str(result[0]) + ' s2=' + str(result[1]) + '.

', round(max_laser_power * 100.0), '

Back to main page

' ) def laserCallback(request): value = 0.0 text = "off" if request.find("?s=") == 10: pe = request.find(" HTTP", 10) r = request[13 : pe] if r != "Off": value = int(r) text = "to " + str(r) + '%' t.laser(value / 100.0) return html % ( '

Laser turned ' + text + '!

', round(max_laser_power * 100.0), '

Back to main page

' ) w = Wifi(Config.ssid, Config.password) w.add_handler("/", rootCallback) w.add_handler("/servos", servoCallback) w.add_handler("/laser", laserCallback) w.listen()