def wifi_connect(): wlan = network.WLAN(network.STA_IF) # STA模式 wlan.active(True) # 激活接口 start_time = time.time() # 记录时间做超时判断 if not wlan.isconnected(): print('connecting to network...') ssid = param_data.get('ssid') # 读取WiFi账号密码 passwd = param_data.get('password') print('ssid is %s , password is %s' % (ssid, passwd)) wlan.connect(ssid, passwd) while not wlan.isconnected(): # LED闪烁提示 # lamp(2) # beep(400, 500) # 超时判断,15秒没连接成功判定为超时 if time.time() - start_time > 10: print('WIFI Connected Timeout!') break if not wlan.isconnected(): wlan.active(False) do_ap() if wlan.isconnected(): # LED点亮 lamp('on') beep(1000, 500) # 串口打印信息 print('network information:', wlan.ifconfig()) # OLED数据显示 display(wlan.ifconfig()[0]) sync_ntp() httpserver(wlan)
def do_ap(): ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid='test', authmode=0) print('AP is up') addr = ('192.168.4.1', 80) s = socket.socket() s.bind(addr) s.listen(5) display('AP start') while True: c, caddr = s.accept() c_file = c.makefile( 'rwb', 0) # 返回与socket对象关联的文件对象。rwb:支持二进制模式的读写操作 0:默认值,不支持缓存 req = b'' # 定义bytes类型字符串 print('AP server start') while True: line = c_file.readline() # 逐行读取文件 if not line or line == b'\r\n': break req += line # time.sleep_ms(10) print("Request:") req = req.decode('utf-8').split('\r\n') # 解码并分割字符 req_data = req[0].lstrip().rstrip().replace( ' ', '').lower() # 列表第一个元素去除空格转换小写 if req_data.find('favicon.ico') > -1: c.close() continue else: req_data = req_data.replace('get/?', '').replace('http/1.1', '') index = req_data.find('ssid') if index > -1: req_data = req_data.split('&') for i in range(len(req_data)): data = req_data[i].split('=') param_data[data[0]] = data[1] print('req_data is:', param_data) sava_all_file() reset() with open("set.html", 'r') as f: for line in f: if 'user_data1' in line: data = param_data.get('ssid') line = line.replace('user_data1', str(data)) elif 'user_data2' in line: data = param_data.get('password') line = line.replace('user_data2', str(data)) c.send(line.encode()) c.close()
def main(): #screenOne = screen.createScreen(500,500) #draw.circle(edgeMatrix, 250, 250, 0, 50, .00001) #matrix.drawEdges(screenOne, edgeMatrix, green) # screen.display(screenOne) screenOne = screen.createScreen(500, 500) parser.parseFile('script3', screenOne, green, edgeMatrix, transformMatrix) matrix.drawEdges(screenOne, edgeMatrix, green) screen.display(screenOne)
from game import play from compute import evolution, somme from screen import display # intialiser le jeu [width, height, trials_max] = initialise(MAX_TRIALS, BOX_MAX, BOX_MIN) # On va jouer playing = True # On a pas encore essayé trials = 0 # créeer la structure du plateau de jeu board = [[1 for i in range(width)] for j in range(height)] # afficher la grille display(board) # boucle de jeu while playing and trials <= trials_max: [x, y] = play(width, height) evolution([x, y], board) display(board) trials += 1 playing = somme(board) # message de fin if not playing: print(f"vous avez réussi après {trials} essais") else: print(f"vous avez échoué après {trials} essais")
def runCommands(commands, knobs): """ Runs the given commands and returns the resulting screen """ stack = [matrix.ident()] view = screen.Screen() for command in commands: if command[0] == "ignore": pass if command[0] == "pop": stack.pop() if not stack: stack = [matrix.ident()] if command[0] == "push": stack.append(stack[-1].clone()) if command[0] == "screen": view = screen.Screen(command[1], command[2]) if command[0] == "save": view.save(command[1]) if command[0] == "display": if len(command) == 2: screen.display(command[1]) else: screen.display(view) if command[0] == "set": knobs[command[1]] = float(command[2]) if command[0] == "set_knobs": for name in knobs.keys(): knobs[name] = float(command[1]) if command[0] == "sphere": m = matrix.FaceMatrix() m.add_sphere(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "torus": m = matrix.FaceMatrix() m.add_torus(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "box": m = matrix.FaceMatrix() m.add_box(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "line": m = matrix.EdgeMatrix() m.add_edge(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "bezier": m = matrix.EdgeMatrix() m.add_bezier_curve(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "hermite": m = matrix.EdgeMatrix() m.add_hermite_curve(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "circle": m = matrix.EdgeMatrix() m.add_circle(command[1], command[2], command[3], command[4]) stack.append(stack[-1].clone()) # # # # # m.apply(stack[-1]) stack.pop() view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "move": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val) if command[0] == "scale": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val) if command[0] == "rotate": if command[3]: val = float(knobs[command[3]]) else: val = 1.0 stack[-1] *= matrix.rotate(command[1], command[2] * val) return view
#get username add passwd if hostname: if not username: username = raw_input("Remote User Name:") if not passwd: passwd = getpass.getpass("password:") try: sysinfo.connect(hostname, username, passwd) except Exception, e: print(e) exit(-1) print('Sucess connect to host:' + hostname) #connect to usb hid device dev = hid.hid(0x0483, 0x5750) scr = screen.display(128, 32) while True: #get sytem info try: info = json.loads(sysinfo.info()) style.display_percent_bar(scr, 0, 'CPU:', info['cpu']) style.display_percent_bar(scr, 1, 'MEM:', info['mem']) style.display_percent_bar(scr, 2, 'DSK:', info['disk']) #scr.write(info['time'] + '\n') except Exception, e: print e scr.clear() scr.write(str(e)) #display dev.write(scr.data())
def test_display(instructions, result): assert display(instructions).strip() == result.strip()
def run(filename): """ This function runs an mdl script """ p = mdl.parseFile(filename) if p: (commands, symbols) = p else: print "Parsing failed." return knobs = {} for s in symbols: if s[0] == "knob": knobs[s[1]] = 0.0 knobs = getKnobValues(knobs) while 1: stack = [matrix.ident()] view = screen.Screen() for command in commands: if command[0] == "pop": stack.pop() if not stack: stack = [matrix.ident()] if command[0] == "push": stack.append(stack[-1].clone()) if command[0] == "screen": view = screen.Screen(command[1], command[2]) if command[0] == "save": view.save(command[1]) if command[0] == "display": if len(command) == 2: screen.display(command[1]) else: screen.display(view) if command[0] == "set": knobs[command[1]] = float(command[2]) if command[0] == "set_knobs": for name in knobs.keys(): knobs[name] = float(command[1]) if command[0] == "sphere": m = matrix.FaceMatrix() m.add_sphere(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "torus": m = matrix.FaceMatrix() m.add_torus(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "box": m = matrix.FaceMatrix() m.add_box(*command[1:]) m.apply(stack[-1]) view.draw_FaceMatrix(m, [255, 255, 255]) if command[0] == "line": m = matrix.EdgeMatrix() m.add_edge(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "bezier": m = matrix.EdgeMatrix() m.add_bezier_curve(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "hermite": m = matrix.EdgeMatrix() m.add_hermite_curve(*command[1:]) m.apply(stack[-1]) view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "circle": m = matrix.EdgeMatrix() m.add_circle(command[1], command[2], command[3], command[4]) stack.append(stack[-1].clone()) # # # # # m.apply(stack[-1]) stack.pop() view.draw_EdgeMatrix(m, [255, 255, 255]) if command[0] == "move": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val) if command[0] == "scale": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val) if command[0] == "rotate": if command[3]: val = float(knobs[command[3]]) else: val = 1.0 stack[-1] *= matrix.rotate(command[1], command[2] * val) while 1: text = raw_input("Continue?\n> ") if not text in ["yes", "no", "n", "y"]: print "I don't understand." elif text in ["yes", "y"]: print break else: return knobs = getKnobValues(knobs)
def parseFile(fileName, screens, color, edgeMatrix, transformMatrix): scriptFile = open('{}'.format(fileName), 'r') for line in scriptFile: currentLine = line.split() if (currentLine[0][0] == '#'): pass elif (currentLine[0] == 'line'): argumentLine = scriptFile.readline().split() a = [ float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]) ] b = [ float(argumentLine[3]), float(argumentLine[4]), float(argumentLine[5]) ] matrix.addEdge(edgeMatrix, a, b) elif (currentLine[0] == 'ident'): matrix.setIdentityMatrix(transformMatrix) elif (currentLine[0] == 'move'): argumentLine = scriptFile.readline().split() moveMatrix = matrix.createTranslateMatrix(float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2])) matrix.matrixMultiplication(moveMatrix, transformMatrix) elif (currentLine[0] == 'scale'): argumentLine = scriptFile.readline().split() scaleMatrix = matrix.createScaleMatrix(float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2])) matrix.matrixMultiplication(scaleMatrix, transformMatrix) elif (currentLine[0] == 'rotate'): argumentLine = scriptFile.readline().split() rotateMatrix = matrix.createRotateMatrix(argumentLine[0], float(argumentLine[1])) matrix.matrixMultiplication(rotateMatrix, transformMatrix) elif (currentLine[0] == 'circle'): argumentLine = scriptFile.readline().split() draw.circle(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), 0) elif (currentLine[0] == 'hermite'): argumentLine = scriptFile.readline().split() draw.curve(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), float(argumentLine[4]), float(argumentLine[5]), float(argumentLine[6]), float(argumentLine[7]), 0, 'hermite') elif (currentLine[0] == 'bezier'): argumentLine = scriptFile.readline().split() draw.curve(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), float(argumentLine[4]), float(argumentLine[5]), float(argumentLine[6]), float(argumentLine[7]), 0, 'bezier') elif (currentLine[0] == 'box'): argumentLine = scriptFile.readline().split() draw.box(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), float(argumentLine[4]), float(argumentLine[5])) elif (currentLine[0] == 'sphere'): argumentLine = scriptFile.readline().split() draw.sphere(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), 0) elif (currentLine[0] == 'torus'): argumentLine = scriptFile.readline().split() draw.torus(edgeMatrix, float(argumentLine[0]), float(argumentLine[1]), float(argumentLine[2]), float(argumentLine[3]), float(argumentLine[4]), 0) elif (currentLine[0] == 'clear'): edgeMatrix = [[], [], [], []] elif (currentLine[0] == 'apply'): matrix.matrixMultiplication(transformMatrix, edgeMatrix) elif (currentLine[0] == 'display'): matrix.drawEdges(screens, edgeMatrix, color) screen.display(screens) screen.clearScreen(screens) time.sleep(.01) elif (currentLine[0] == 'save'): argumentLine = scriptFile.readline().split() matrix.drawEdges(screens, edgeMatrix, color) screen.saveExtension(screens, argumentLine[0]) else: print('Bad command, ' + currentLine[0])
def runCommands(commands, knobs, constants, coord_systems, meshesE, meshesF, base_matrix, focalLength): """ Runs the given commands and returns the resulting screen """ stack = [base_matrix.clone()] view = screen.Screen() constants[None] = [255, 255, 255, 200, 200, 200, 100, 100, 100, 0, 0, 0] lights = [] ambient = [0, 0, 0] shading_type = "wireframe" for command in commands: if command[0] == "ignore": pass elif command[0] == "pop": stack.pop() if not stack: stack = [base_matrix.clone()] elif command[0] == "push": stack.append(stack[-1].clone()) elif command[0] == "screen": view = screen.Screen(command[1], command[2]) stack = [base_matrix.clone()] elif command[0] == "save": view.save(command[1]) elif command[0] == "display": if len(command) == 2: screen.display(command[1]) else: screen.display(view) elif command[0] == "camera": base_matrix = matrix.ident() base_matrix *= matrix.move(0 - command[1], 0 - command[2], 0 - command[3]) xaim = command[4] - command[1] yaim = command[5] - command[2] zaim = command[6] - command[3] if xaim == 0 and zaim == 0: if yaim >= 0: base_matrix *= matrix.rotate("x", -90) else: base_matrix *= matrix.rotate("x", 90) else: theta = (math.atan2(yaim, math.sqrt(math.pow(xaim, 2) + math.pow(zaim, 2)))) * -180.0 / 3.14159265358979323 base_matrix *= matrix.rotate("x", theta) theta = ((math.atan2(zaim, xaim) - math.atan2(1, 0)) * 180.0 / 3.14159265358979323) base_matrix *= matrix.rotate("y", theta) stack = [base_matrix.clone()] elif command[0] == "focal": focalLength = command[1] view.focalLength = focalLength elif command[0] == "set": knobs[command[1]] = float(command[2]) elif command[0] == "set_knobs": for name in knobs.keys(): knobs[name] = float(command[1]) elif command[0] == "ambient": ambient = command[1:] elif command[0] == "light": lights.append(command[1:]) elif command[0] == "sphere": m = matrix.FaceMatrix() m.add_sphere(*[command[i] for i in range(2, 8) if i != 5]) if command[5]: m.apply(coord_systems[command[5]]) else: m.apply(stack[-1]) view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "torus": m = matrix.FaceMatrix() m.add_torus(*[command[i] for i in range(2, 9) if i != 5]) if command[5]: m.apply(coord_systems[command[5]]) else: m.apply(stack[-1]) view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "box": m = matrix.FaceMatrix() m.add_box(*(command[2:8])) if command[8]: m.apply(coord_systems[command[8]]) else: m.apply(stack[-1]) view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "line": m = matrix.PointMatrix() n = matrix.PointMatrix() m.add_point(*command[2:5]) n.add_point(*command[6:9]) if command[5]: m.apply(coord_systems[command[5]]) else: m.apply(stack[-1]) if command[9]: n.apply(coord_systems[command[9]]) else: n.apply(stack[-1]) [x0, y0, z0] = [m.get(i, 0) for i in range(3)] [x1, y1, z1] = [n.get(i, 0) for i in range(3)] m = matrix.EdgeMatrix() m.add_edge(x0, y0, z0, x1, y1, z1) view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "bezier": xs = [] ys = [] zs = [] for i in range(4): m = matrix.PointMatrix() m.add_point(*command[2 + 4 * i:5 + 4 * i]) if command[5 + 4 * i]: m.apply(coord_systems[command[5 + 4 * i]]) else: m.apply(stack[-1]) xs.append(m.get(0, 0)) ys.append(m.get(1, 0)) zs.append(m.get(2, 0)) m = matrix.EdgeMatrix() m.add_bezier_curve(xs[0], ys[0], zs[0], xs[1], ys[1], zs[1], xs[2], ys[2], zs[2], xs[3], ys[3], zs[3], command[18]) view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "hermite": xs = [] ys = [] zs = [] for i in range(4): m = matrix.PointMatrix() m.add_point(*command[2 + 4 * i:5 + 4 * i]) if command[5 + 4 * i]: m.apply(coord_systems[command[5 + 4 * i]]) else: m.apply(stack[-1]) xs.append(m.get(0, 0)) ys.append(m.get(1, 0)) zs.append(m.get(2, 0)) m = matrix.EdgeMatrix() m.add_hermite_curve(xs[0], ys[0], zs[0], xs[1], ys[1], zs[1], xs[2], ys[2], zs[2], xs[3], ys[3], zs[3], command[18]) view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "mesh": if meshesE.has_key(command[2]): m = meshesE[command[2]].clone() if command[3]: m.apply(coord_systems[command[3]]) else: m.apply(stack[-1]) view.draw_EdgeMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif meshesF.has_key(command[2]): m = meshesF[command[2]].clone() if command[3]: m.apply(coord_systems[command[3]]) else: m.apply(stack[-1]) view.draw_FaceMatrix(m, [shading_type, constants[command[1]], ambient, lights]) elif command[0] == "save_coord_system": coord_systems[command[1]] = stack[-1].clone() elif command[0] == "constants": constants[command[1]] = command[2:] elif command[0] == "shading": shading_type = command[1] elif command[0] == "move": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.move(command[1] * val, command[2] * val, command[3] * val) elif command[0] == "scale": if command[4]: val = float(knobs[command[4]]) else: val = 1.0 stack[-1] *= matrix.scale(command[1] * val, command[2] * val, command[3] * val) elif command[0] == "scaleXYZ": if command[3]: val = float(knobs[command[3]]) else: val = 1.0 if command[1] == "x": stack[-1] *= matrix.scale(command[2] * val, 1, 1) elif command[1] == "y": stack[-1] *= matrix.scale(1, command[2] * val, 1) else: stack[-1] *= matrix.scale(command[2] * val, 1, 1) elif command[0] == "rotate": if command[3]: val = float(knobs[command[3]]) else: val = 1.0 stack[-1] *= matrix.rotate(command[1], command[2] * val) return view
def httpserver(wlan): addr = (wlan.ifconfig()[0], 80) s = socket.socket() s.bind(addr) s.listen(5) display('server start') while True: c, caddr = s.accept() c_file = c.makefile( 'rwb', 0) # 返回与socket对象关联的文件对象。rwb:支持二进制模式的读写操作 0:默认值,不支持缓存 req = b'' # 定义bytes类型字符串 while True: line = c_file.readline() # 逐行读取文件 if not line or line == b'\r\n': break req += line # time.sleep_ms(10) print("Request:") req = req.decode('utf-8').split('\r\n') # 解码并分割字符 req_data = req[0].lstrip().rstrip().replace( ' ', '').lower() # 列表第一个元素去除空格转换小写 if req_data.find('favicon.ico') > -1: c.close() continue else: req_data = req_data.replace('get/?', '').replace('http/1.1', '') index = req_data.find('key=') if index > -1: value = req_data[index + 4:].lstrip().rstrip() print('key:', value) if value == 'PumpOn': display('Pump on') servo(90) lamp(2) elif value == 'PumpOff': display('pump off') servo(0) lamp(2) elif value == 'warm_on': display('warm on') lamp(2) music('Xxx') elif value == 'warm_off': display('warm off') lamp(2) music('Dh') elif value == 'fog_on': display('fog on') lamp(2) elif value == 'fog_off': display('fog off') lamp(2) elif value == 'valve_on': display('valveon on') lamp(2) elif value == 'valve_off': display('valveon off') lamp(2) elif value == 'light_on': display('light on') lamp(2) light('on') elif value == 'light_off': display('light off') lamp(2) light('off') else: display(value) lamp(5) else: index = req_data.find('fog') if index > -1: req_data = req_data.split('&') for i in range(len(req_data)): data = req_data[i].split('=') param_data[data[0]] = data[1] print('req_data is:', param_data) with open("control.html", 'r') as f: for line in f: if 'WaterTemp' in line: data = param_data.get('WaterTemp', '999') line = line.replace('WaterTemp', data) elif 'HwaterTemp' in line: data = param_data.get('HTemp', '999') line = line.replace('HwaterTemp', data) elif 'IndoorTemp' in line: data = param_data.get('AirTemp', '999') line = line.replace('IndoorTemp', data) elif 'Hum' in line: data = param_data.get('Hum', '999') line = line.replace('Hum', data) elif 'PumpState' in line: line = line.replace('PumpState', '开') elif 'WarmState' in line: line = line.replace('WarmState', '开') elif 'FogState' in line: line = line.replace('FogState', 'on') elif 'ValveonState' in line: line = line.replace('ValveonState', 'off') elif 'LightState' in line: line = line.replace('LightState', 'off') c.send(line.encode()) c.close()