def start_(self): # 以下注释代码仅用于开发者调试,请勿打开 # if util.init_minicap(self.port): # return # if util.init_minitouch(self.port + 1): # return logger.debug("cap screen start") self.tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 262144) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 262144) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # TCP_NODELAY在linux系统下可能会要求root权限,先屏蔽 # self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.TCP_NODELAY, 1) self.tcpSocket.settimeout(1) ret = self.tcpSocket.connect_ex((self.host, self.port)) if ret != 0: # todo: pass self.tcpSocket.setblocking(False) self.signal.connect_result.emit(ret) self.start() self.touch = Touch() ret = self.touch.start(self.host, self.touch_port) if not ret: self.signal.minitouch_connect_result.emit(False) else: self.screen.bind_touch(self.touch)
def __init__(self): self.delays = { 'Bad Apple': 300, } self.song_list = { # 'Caramelldansen (Speedycake Remix)': 9999, 'Bad Apple': 3, # 'Elemental Creati': 3, 'I know You know': 2, 'boom_clap': 4, # 'Through the Fire and Flame': 1, 'Southern Country 2': 7, # 'Eternal': 8, # 'Punjabi MC - Munda Tho Bach Ke Rahi': 9, # 'Salt N Peppa - Push It': 10, } t = Touch(p.touch_pins, threshold=150) self.r = RhythGame(p.pin_ws2812, p.pin_ws2813, p.pin_outer, touch_driver=t, brightness=255, song_list=self.song_list)
def main(): loop = asyncio.get_event_loop() queue = asyncio.Queue(maxsize=8) song = Song('dr_chaos') loop.create_task(queue_put(queue, song)) pulse = Note() max_concurrent = 8 for _ in range(max_concurrent): loop.create_task(consume(queue, pulse.pulse)) fire_anim = Fire(num_leds=10) loop.create_task(loop_stuff(fire_anim)) touch_pins = [1, 2, 3] touch = Touch(touch_pins) timer = Timer(0) timer.init(period=100, mode=Timer.PERIODIC, callback=touch.cb) loop.create_task(report_touch(touch)) # import esp # esp.neopixel_write(pin, grb_buf, is800khz) # rtc = RTC() # rtc.datetime() loop.run_forever()
class SensorFeatureGenerator: def __init__(self): self.tc = Touch() self.ds = Distance() def generate_sensor_feature(self): tc_ft = Feature() tc_ft.type = feature.Type.Touch.value tc_ft.received_time = int( (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000) tc_ft.timeout = int(random.uniform(100, 5000)) tc_ft.data = self.tc.get_touch() ds_ft = Feature() ds_ft.type = feature.Type.TOFDistance.value ds_ft.received_time = int( (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds() * 1000) ds_ft.timeout = int(random.uniform(100, 5000)) ds_ft.data = self.ds.get_distance() return tc_ft, ds_ft def simulate_data_flow_forever(self): while True: while True: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((common.HOST, common.PORT)) break except ConnectionRefusedError: time.sleep(1) continue tc_ft, ds_ft = self.generate_sensor_feature() sock.sendall(bytes(tc_ft.to_json() + "\n", "utf-8")) print("SEND: " + tc_ft.to_json()) received = str(sock.recv(1024), "utf-8") #print("RECV: " + received) # time.sleep(0.2) sock.close() while True: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((common.HOST, common.PORT)) break except ConnectionRefusedError: time.sleep(1) continue sock.sendall(bytes(ds_ft.to_json() + "\n", "utf-8")) print("SEND: " + ds_ft.to_json()) received = str(sock.recv(1024), "utf-8") #print("RECV: " + received) sock.close()
def __init__(self, **kwargs): super(GameWindow, self).__init__(**kwargs) self.card_deck = [i + 1 for i in range(52)] self.players = [Player() for i in range(4)] # initial distribution of cards at the beginning of game count = 0 for player in self.players: self.add_widget(player) # if count == 0: # player.player_cards = [27,28,29,30,44] # for card in player.player_cards: # self.card_deck.remove(card) # count+=1 # continue # if count == 1: # player.player_cards = [7] # for card in player.player_cards: # self.card_deck.remove(card) # count+=1 # continue for i in range(7): card = random.choice(self.card_deck) self.card_deck.remove(card) player.add_card(card) count += 1 self.add_widget(Touch()) self.game_log = ScrolllabelLabel() self.add_widget(self.game_log) self.display_cards() self.number_of_cards_left = len(self.card_deck) self.start_game()
def read_cb(drv, ptr): # print(ptr, b) data = lv.indev_data_t.cast(ptr) TOUCH.event() if DEBUG: print(TOUCH.state, TOUCH.points) data.point = lv.point_t({'x': TOUCH.points[1][0], 'y': TOUCH.points[1][1]}) data.state = lv.INDEV_STATE.PR if TOUCH.state == 1 else lv.INDEV_STATE.REL return False if config_touchscreen_support: i2c = I2C(I2C.I2C0, freq=1000*1000, scl=24, sda=27) # 24 27) devices = i2c.scan() print("devs", devices) # devs 0 [16, 38, 52, 56] TouchLow.config(i2c) TOUCH = Touch(480, 320, 200) lv.init() disp_buf1 = lv.disp_buf_t() buf1_1 = bytearray(320*10) lv.disp_buf_init(disp_buf1,buf1_1, None, len(buf1_1)//4) disp_drv = lv.disp_drv_t() lv.disp_drv_init(disp_drv) disp_drv.buffer = disp_buf1 disp_drv.flush_cb = lv_h.flush if board_m1n: disp_drv.hor_res = 240 disp_drv.ver_res = 240 else:
class app: layer = 0 # set help_draw to top ctrl = agent() btn = sipeed_button() loop = 0 loading = False toth = Touch(480, 320, 50) touch_select = 0 msa301 = None i2c4 = None touch_left = b"\x00\x00\x00\x00\x03\x0F\x3F\xFF\xFF\x3F\x0F\x03\x00\x00\x00\x00\x07\x0F\x3F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x3F\x0F\x03" touch_right = b"\xE0\xF0\xFC\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC\xF0\xC0\x00\x00\x00\x00\xC0\xF0\xFC\xFF\xFF\xFC\xF0\xC0\x00\x00\x00\x00" touch_close = b"\x00\x1C\x3C\x78\x7C\x6E\x07\x03\x03\x07\x6E\x7C\x78\x3C\x1C\x00\x00\x38\x3C\x1E\x3E\x76\xE0\xC0\xC0\xE0\x76\x3E\x1E\x3C\x38\x00" def touch_draw(): app.toth.event() app.loop = (app.loop + 1) % 200 value = math.cos(math.pi * app.loop / 32) * 8 # temp code tmp_x, tmp_l, tmp_r = 0, 0, 0 if app.toth.state == 2: print(app.toth.state, app.toth.points) p = app.toth.points[1] if p[0] > 430 and p[1] > 260: app.layer -= 1 app.exit_application() elif p[0] < 60 and p[1] < 60: app.touch_select = -1 #print(app.touch_select) if launcher.app_select == 1: app.current.page -= 1 elif launcher.app_select == 3: photos.image_last() elif launcher.app_select == 0: ai_camera.back() elif p[0] > 420 and p[1] < 60: app.touch_select = +1 #print(app.touch_select) if launcher.app_select == 1: app.current.page -= 1 elif launcher.app_select == 3: photos.image_last() elif launcher.app_select == 0: ai_camera.next() if app.toth.state == 1: p = app.toth.points[1] if p[0] > 430 and p[1] > 270: tmp_x = int(value) + 30 elif p[0] < 50 and p[1] < 50: tmp_l = int(value) + 30 elif p[0] > 430 and p[1] < 50: tmp_r = int(value) + 30 ui.canvas.draw_circle(ui.height, 0, 40 + tmp_x, thickness=8, fill=False, color=(100, 100, 100)) ui.canvas.draw_circle(ui.height, 0, 38 + tmp_x, fill=True, color=(255, 255, 255)) #ui.canvas.draw_string(ui.height - 15, 5, "x", scale=2, color=(0, 0, 0)) ui.canvas.draw_font(ui.height - 22, 8, 16, 16, app.touch_close, scale=1, color=(155, 155, 0)) ui.canvas.draw_circle(0, ui.weight, 40 + tmp_l, thickness=8, fill=False, color=(100, 100, 100)) ui.canvas.draw_circle(0, ui.weight, 38 + tmp_l, fill=True, color=(255, 255, 100)) #ui.canvas.draw_string(10, ui.weight - 30, "<", scale=2, color=(0, 0, 0)) ui.canvas.draw_font(8, ui.weight - 25, 16, 16, app.touch_left, scale=1, color=(155, 155, 0)) ui.canvas.draw_circle(ui.height, ui.weight, 40 + tmp_r, thickness=8, fill=False, color=(100, 100, 100)) ui.canvas.draw_circle(ui.height, ui.weight, 38 + tmp_r, fill=True, color=(100, 255, 255)) #ui.canvas.draw_string(ui.height - 15, ui.weight - 30, ">", scale=2, color=(0, 0, 0)) ui.canvas.draw_font(ui.height - 20, ui.weight - 25, 16, 16, app.touch_right, scale=1, color=(0, 155, 155)) #@ui.warp_template(CubeAudio.event) @ui.warp_template(ui.bg_in_draw) #@ui.warp_template(ui.help_in_draw) def draw_load(): app.loop = (app.loop + 1) % 200 value = math.cos(math.pi * app.loop / 8) * 6 #print(value) if app.loading or app.loop > 20: ui.canvas.draw_string(200 - int(value) * 2, 68 + (int(value) % 8) * 2, "A", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_string(200 - int(value), 72 + (int(value) % 8), "A", color=(0x2d, 0x85, 0xf0), scale=8, mono_space=0) else: ui.canvas.draw_string(203, 73, "A", color=(64, 64, 64), scale=8, mono_space=0) if app.loading or app.loop > 40: ui.canvas.draw_string(203, 72 - int(value) * 5 - 9, " m", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_string(200, 70 - int(value) * 4 - 8, " m", color=(0xf4, 0x43, 0x3c), scale=8, mono_space=0) else: ui.canvas.draw_string(203, 72, " m", color=(64, 64, 64), scale=8, mono_space=0) if app.loading or app.loop > 40: ui.canvas.draw_string(203, 72, " i", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_string(200, 70, " i", color=(0xff, 0xbc, 0x32), scale=8, mono_space=0) ui.canvas.draw_rectangle((304, 77, 12, 12), color=(0x70, 0x70, 0x70), fill=True) ui.canvas.draw_string(259, 44 + int(value), " . ", color=(64, 64, 64), scale=4, mono_space=0) ui.canvas.draw_string(256, 42 + int(value), " . ", color=(0xff, 0xbc, 0x32), scale=4, mono_space=0) else: ui.canvas.draw_string(203, 72, " i", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_rectangle((304, 77, 12, 12), color=(0x70, 0x70, 0x70), fill=True) ui.canvas.draw_string(259, 44, " . ", color=(64, 64, 64), scale=4, mono_space=0) if app.loading or app.loop > 60: ui.canvas.draw_string(203 + int(value) * 2 + 10, 72, " g", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_string(200 + int(value) * 2 + 10, 70, " g", color=(0x0a + int(value) * 50, 0xa8, 0x58 + int(value) * 50), scale=8, mono_space=0) else: ui.canvas.draw_string(203, 72, " g", color=(64, 64, 64), scale=8, mono_space=0) if app.loading or app.loop > 60: ui.canvas.draw_string(203 - int(value) * 2 + 20, 72, " o", color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_string(200 - int(value) * 2 + 20, 70, " o", color=(0xf4, 0x43 + int(value) * 50, 0x3c), scale=8, mono_space=0) else: ui.canvas.draw_string(203, 72, " o", color=(64, 64, 64), scale=8, mono_space=0) if app.loading == False and app.loop < 20: ui.canvas.draw_string(203, 73, "Amigo", color=(64 + int(value) * 2, 64 + int(value) * 2, 64 + int(value) * 2), scale=8, mono_space=0) if app.loop > 70: app.loading = True ui.canvas.draw_string(320, 280, "Now Loading...", color=(164 + int(value) * 8, 164 + int(value) * 8, 164 + int(value) * 8), scale=2, mono_space=0) if app.loop == 100: app.layer += 1 ui.display() # @ui.warp_template(ui.bg_in_draw) # ui_3d_launcher need remove @ui.warp_template(launcher.draw) #@ui.warp_template(taskbar.mem_draw) @ui.warp_template(taskbar.battery_draw) @ui.warp_template(ui.bg_in_draw) def draw_launcher(): ui.display() #@ui.warp_template(CubeAudio.event) @ui.warp_template(ui.grey_draw) @ui.warp_template(ui.anime_draw) @ui.warp_template(touch_draw) #@ui.warp_template(CubeAudio.event) #@ui.warp_template(taskbar.mem_draw) # @ui.warp_template(system_info.info_draw) def draw_pages(): if app.current != None: app.current.draw() app.loop = (app.loop + 1) % 200 value = math.cos(math.pi * app.loop / 12) * 2 + 20 for i in range(100): try: #ui.canvas.draw_rectangle((240, 0, 240, 320), fill=False, thickness=3, color=(175, 175, 175)) if app.msa301 != None: accel = app.msa301.acceleration x, y = 240, 160 # center #print(accel) ui.canvas.draw_circle( x + int(accel[0] * 15), y + int(accel[1] * 20), int(value), fill=True, color=(150 + int(accel[0] * 20), 150 + int(accel[1] * 20), 100 + int(accel[2] * 20))) # 10ms ui.canvas.draw_circle(x + int(accel[0] * 15), y + int(accel[1] * 20), int(value) + 1, thickness=3, fill=False, color=(0, 0, 0)) # 10ms else: app.msa301 = MSA301(app.i2c3) break except Exception as e: pass # gc.collect() #ui.canvas.draw_string(240 + 10, 140, "x", (255, 0, 0), scale=2) #ui.canvas.draw_string(240 + 10, 140, "x", (255, 0, 0), scale=2) #ui.canvas.draw_line(240 + 120, 150, 120 + int(accel[0] * 8), 150, color=(41, 131, 255)) #ui.canvas.draw_string(240 + 10, 160, "y", (0, 255, 0), scale=2) #ui.canvas.draw_line(240 + 120, 170, 120 + int(accel[1] * 8), 170, color=(141, 31, 255)) #ui.canvas.draw_string(240 + 10, 180, "z", (0, 0, 255), scale=2) #ui.canvas.draw_line(240 + 120, 190, 120 + int(accel[2] * 8), 190, color=(241, 131, 55)) ui.display() photos_title = "/" photos_temp = None #@ui.warp_template(taskbar.time_draw) #@ui.warp_template(sample_page.sample_draw) #@ui.warp_template(ui.grey_draw) #@ui.warp_template(CubeAudio.event) #@ui.warp_template(touch_draw) def draw_photos(): if photos.photos_len() > 0: if app.photos_title != photos.image_path(): app.photos_title = photos.image_path() if app.photos_temp != None: tmp = app.photos_temp del tmp app.photos_temp = None if app.photos_temp: ui.canvas.draw_image(app.photos_temp, 0, 0) else: app.photos_temp = image.Image(app.photos_title) # [mem < 800*1024] #t, ui.canvas = ui.canvas, None #del t #ui.canvas = image.Image(photos.image_path()) ui.canvas.draw_string(2, 2, app.photos_title, color=(255, 255, 255), scale=1, mono_space=0) else: ui.canvas.draw_string(40, 120, "Please put pictures\n in '/sd/imgs' folder", color=(255, 255, 255), scale=3, mono_space=0) app.touch_draw() ui.display() sht3x = None sidu = None temp = 0 points = [] isconnected = False @ui.warp_template(touch_draw) #@ui.warp_template(taskbar.mem_draw) #@ui.warp_template(taskbar.time_draw) @ui.warp_template(CubeAudio.event) #@ui.warp_template(explorer.draw) def draw_demo(): app.loop = (app.loop + 1) % 200 value = math.cos(math.pi * app.loop / 16) * 10 ui.canvas.draw_string(10, 5, "Seeed Grove", color=(40 + int(value) * 2, 240 + int(value) * 2, 40 + int(value) * 2), scale=3, mono_space=0) try: CubeAudio.event() if app.isconnected == False: CubeAudio.event() if app.loop % 5 == 1: result = app.i2c4.scan() ui.canvas.draw_string(290, 80, "Scan Dev: " + str(result), color=(140 + int(value) * 5, 240 + int(value) * 5, 140 + int(value) * 5), scale=2, mono_space=0) if SHT3x_ADDR in result: app.sht3x = SHT3x(app.i2c4, SHT3x_ADDR) app.isconnected = True CubeAudio.event() if SHT31_ADDR in result: app.sht3x = SHT3x(app.i2c4, SHT31_ADDR) app.isconnected = True CubeAudio.event() ui.canvas.draw_string( 280, 25, "Wait Grove Sensor \n sht31/35 <<< << <-", color=(140 + int(value) * 5, 240 + int(value) * 5, 140 + int(value) * 5), scale=2, mono_space=0) if CubeAudio.event() == False: value = math.cos(math.pi * app.loop / 100) * 50 tmp = int(value) #print(value) ui.canvas.draw_circle(0, 0, 100 + tmp, fill=False, color=(0, (150 + tmp) + 10, 0)) ui.canvas.draw_circle(0, 0, 100 + tmp * 2, fill=False, color=(0, (150 + tmp) + 20, 0)) ui.canvas.draw_circle(0, 0, 100 + tmp * 3, fill=False, color=(0, (150 + tmp) + 30, 0)) ui.canvas.draw_circle(0, 0, 100 + tmp * 4, fill=False, color=(0, (150 + tmp) + 40, 0)) else: data = app.sht3x.read_temp_humd() # print(data) if app.sidu == None: app.sidu = image.Image(os.getcwd() + "/res/images/sidu.jpg") ui.canvas.draw_circle(350, 160, 100, fill=True, color=(255, 255, 255)) ui.canvas.draw_image(app.sidu, 270, 60, alpha=235 + int(value) * 2) ui.canvas.draw_string(330, 190, "%.2d" % data[1], scale=4, color=(80, 80, 80)) ui.canvas.draw_rectangle(60, 60, 180, 200, thickness=4, color=(155, 155, 155)) if len(app.points) > 18: app.points.pop(0) elif data[0] > 1 and app.temp != int(data[0] * 10): app.temp = int(data[0] * 10) app.points.append(app.temp) for p in range(len(app.points)): #print(app.points) if p < 1: b = (60 + int(10 * (p))), 450 - app.points[p] ui.canvas.draw_circle(b[0], b[1], 3, fill=True, color=(255, 155, 150)) else: a, b = ((60 + int(10 * (p - 1))), 450 - app.points[p - 1]), ((60 + int(10 * (p))), 450 - app.points[p]) ui.canvas.draw_circle(b[0], b[1], 3, fill=False, color=(155, 155, 155)) ui.canvas.draw_line(a[0], a[1], b[0], b[1], thickness=4, color=(255, 255, 255)) ui.canvas.draw_string( 60, 280, "Average temperature: %s" % str(sum(app.points) / len(app.points) / 10.0), color=(240 + int(value) * 5, 240 + int(value) * 5, 240 + int(value) * 5), scale=2, mono_space=0) CubeAudio.event() ui.display() except Exception as e: app.layer = 1 app.isconnected = False app.points = [] raise e def draw_camera(): try: gc.collect() ai_camera.ai_draw() for model in ai_camera.models: #print(model.__qualname__, ai_camera.model.__qualname__) if 'ai_sample' == ai_camera.model.__qualname__: ui.canvas.draw_string(340, 80, " AI\nDemo", scale=5) ui.canvas.draw_string( 50, 260, "Press Left (<) or Right (>) to View", scale=2) pass elif 'FaceDetect' == ai_camera.model.__qualname__: if ai_camera.model.bbox != None: bbox = ai_camera.model.bbox ui.canvas.draw_string(50, 260, "Face Detect %d" % len(bbox), scale=5) for pos in range(len(bbox)): i = bbox[pos] # print(i.x(), i.y(), i.w(), i.h()) face_cut = ui.canvas.cut(i.x(), i.y(), i.w(), i.h()) face_cut_128 = face_cut.resize(80, 80) ui.canvas.draw_image(face_cut_128, 320 + int( (pos % 2) * 80), int((pos // 2) * 80)) else: ui.canvas.draw_string(50, 260, "Find Detect", scale=5) elif 'FaceReco' == ai_camera.model.__qualname__: ui.canvas.draw_string(50, 260, "Face Recognition", scale=5) elif 'find_color' == ai_camera.model.__qualname__: ui.canvas.draw_string( 50, 260, "Find Color For Red (53, 31, 44, 82, 18, 78)", scale=2) ui.canvas.draw_string(50, 260, " Red", color=(255, 0, 0), scale=2) ui.canvas.draw_string(340, 30, "Red\nSum\n %d" % len(ai_camera.model.blobs), scale=5) elif 'HowMany' == ai_camera.model.__qualname__: if ai_camera.model.things != None: ui.canvas.draw_string(340, 30, "How\nMany\n %d" % len(ai_camera.model.things), scale=5) ui.canvas.draw_string(50, 260, "How many things are there?", scale=2) elif 'MaybeIs' == ai_camera.model.__qualname__: ui.canvas.draw_string(340, 50, "Maybe\n Is\n", scale=3) ui.canvas.draw_string(340, 150, "%s" % str(ai_camera.model.result), scale=2) ui.canvas.draw_string(50, 260, "What is likely to be?", scale=2) elif 'MoblieNet' == ai_camera.model.__qualname__: ui.canvas.draw_string(340, 50, "MoblieNet\n 1000class\n", scale=3) ui.canvas.draw_string(340, 150, "What is likely to be?", scale=2) app.touch_draw() ui.display() except Exception as e: # ai_camera.next() # protect.restart() # temp patch app.layer -= 1 raise e #raise Exception("This is a Easter egg(Known Bug) This error requires a restart. It will soon be resolved. :)") current = None def load_application(selected): if app.current != None: # clear last application del app.current app.current = None if selected == 0: pass elif selected == 1: app.current = pages() app.current.tips = "Weclome to Maix Amigo" elif selected == 2: CubeAudio.load(os.getcwd() + "/res/sound/loop.wav", 100) app.points = [] pass #app.layer -= 1 # return last layer #raise Exception("Settings Unrealized.") elif selected == 3: photos.scan() def exec_application(): try: if launcher.app_select == 0: app.draw_camera() if launcher.app_select == 1: app.draw_pages() if launcher.app_select == 2: app.draw_demo() if launcher.app_select == 3: app.draw_photos() except Exception as e: app.layer -= 1 raise e def exit_application(): try: if launcher.app_select == 0: ai_camera.exit() ai_camera.jump(0) ai_camera.reload() elif launcher.app_select == 1: pass elif launcher.app_select == 2: t, app.sidu = app.sidu, None # Clear del t elif launcher.app_select == 3: t, app.photos_temp = app.photos_temp, None # Clear del t except Exception as e: app.layer -= 1 raise e rgb = 0 def rgb_change(rgb): sipeed_led.r.value(rgb & 0b001) sipeed_led.g.value(rgb & 0b010) sipeed_led.b.value(rgb & 0b100) def on_event(): #app.btn.event() app.btn.expand_event() if app.btn.home( ) == 2 or launcher.app_run: # click button release to 2 launcher.app_run = False # print('into', app.layer) if app.layer == 1: app.layer += 1 # launcher into application app.load_application(launcher.app_select) elif app.layer == 2: if app.btn.interval() > 1000: # long press app.layer -= 1 app.exit_application() # application return launcher else: app.layer += 1 # help into launcher #launcher.btn.enable = True #if launcher.app_run: #launcher.app_run = False #print('launcher.app_select', launcher.app_select) if app.btn.next() == 1: app.rgb = (app.rgb + 1) % 8 app.rgb_change(app.rgb) if launcher.app_select == 3: photos.image_next() if app.btn.back() == 1: app.rgb = (app.rgb - 1) % 8 app.rgb_change(app.rgb) if launcher.app_select == 3: photos.image_last() @ui.warp_template(ui.blank_draw) #@ui.warp_template(ui.grey_draw) @catch def draw(): app.on_event() ui.canvas.draw_rectangle((0, 0, ui.height, ui.weight), fill=True, color=(0x70, 0x70, 0x70)) # gc.collect() if app.layer == 0: app.draw_load() elif app.layer == 1: app.draw_launcher() elif app.layer == 2: app.exec_application() def run(): # debug into app_select # launcher.app_select = 2 # app.layer = 1 ui.height, ui.weight = 480, 320 # button_io.config(23, 20, 31) # amigo tft button_io.config(16, 23, 20) # amigo ips sipeed_led.init(14, 15, 17, 32) app.i2c3 = I2C(I2C.I2C3, freq=100 * 1000, scl=24, sda=27) app.i2c4 = I2C(I2C.I2C4, freq=100 * 1000, scl=9, sda=7) TouchLow.config(i2c3=app.i2c3) # amigo #if AXP173_ADDR in i2c.scan(): axp173 = AXP173(i2c_dev=app.i2c3) axp173.enable_adc(True) # 默认充电限制在 4.2V, 190mA 档位 axp173.setEnterChargingControl(True) axp173.exten_output_enable() # amigo sensor config. axp173.writeREG(0x27, 0x20) axp173.writeREG(0x28, 0x0C) taskbar.init(axp173) CubeAudio.init(app.i2c3) if CubeAudio.check(): CubeAudio.ready() fm.register(13, fm.fpioa.I2S0_MCLK, force=True) fm.register(21, fm.fpioa.I2S0_SCLK, force=True) fm.register(18, fm.fpioa.I2S0_WS, force=True) fm.register(35, fm.fpioa.I2S0_IN_D0, force=True) fm.register(34, fm.fpioa.I2S0_OUT_D2, force=True) #app.ctrl.event(100, lambda *args: time.sleep(1)) #app.ctrl.event(10, app.on_event) app.ctrl.event(5, app.draw) #ui.enable = False while True: last = 0 while True: try: # print((int)(1000 / (time.ticks_ms() - last)), 'fps') # last = time.ticks_ms() # print_mem_free() gc.collect() app.ctrl.cycle() protect.keep() #time.sleep(0.1) except KeyboardInterrupt: protect.stop() raise KeyboardInterrupt() except Exception as e: # gc.collect() print(e)
def __init__(self, n): # hack in the fake "touch" for mark 2 boards. assert n == 7 from touch import Touch Touch()
def __init__(self): self.tc = Touch() self.ds = Distance()
class CapScreen(threading.Thread): def __init__(self, on_connect_result, on_mini_cap_head, on_frame, on_mini_touch_connect_result, on_screenshots_result, screen, host="127.0.0.1", port=8081): threading.Thread.__init__(self) self.signal = CapScreenSignal() self.signal.connect_result.connect(on_connect_result) self.signal.minicap_head.connect(on_mini_cap_head) self.signal.frame.connect(on_frame) self.signal.minitouch_connect_result.connect( on_mini_touch_connect_result) self.signal.screenshots.connect(on_screenshots_result) self.tcpSocket = None self.touch = None self.host = host self.port = port self.touch_port = port + 1 self.head = None self.screen = screen self.frame_buff = None def start_(self): # 以下注释代码仅用于开发者调试,请勿打开 # if util.init_minicap(self.port): # return # if util.init_minitouch(self.port + 1): # return logger.debug("cap screen start") self.tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 262144) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 262144) self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # TCP_NODELAY在linux系统下可能会要求root权限,先屏蔽 # self.tcpSocket.setsockopt(socket.SOL_SOCKET, socket.TCP_NODELAY, 1) self.tcpSocket.settimeout(1) ret = self.tcpSocket.connect_ex((self.host, self.port)) if ret != 0: # todo: pass self.tcpSocket.setblocking(False) self.signal.connect_result.emit(ret) self.start() self.touch = Touch() ret = self.touch.start(self.host, self.touch_port) if not ret: self.signal.minitouch_connect_result.emit(False) else: self.screen.bind_touch(self.touch) def run(self): self.head = self.parse_head() self.signal.minicap_head.emit(self.head) while self.tcpSocket: n_buff = safe_receive(self.tcpSocket, 4) if n_buff is None: break n = struct.unpack("I", n_buff)[0] self.frame_buff = safe_receive(self.tcpSocket, n) if self.frame_buff is None: break else: self.signal.frame.emit(self.frame_buff) self.stop() def parse_head(self): buff = safe_receive(self.tcpSocket, MINICAP_HEAD_SIZE) if buff is None: pass else: return MinicapHead(buff) def save_screenshots(self, frame, filepath, show_toast=True): logger.debug("save screenshots to %s" % filepath) path = os.path.dirname(filepath) if not os.path.exists(path): os.makedirs(path) with open(filepath, "wb") as fp: fp.write(frame) self.signal.screenshots.emit(filepath, show_toast) def screenshots(self, filepath, show_toast=True): if not self.frame_buff: return False if not filepath: filepath = "./screenshots/{date}/{time}.jpg".format( date=time.strftime("%y-%m-%d"), time=time.strftime("%H-%M-%S")) t = threading.Thread(target=self.save_screenshots, args=(self.frame_buff, filepath, show_toast)) t.start() return True def is_connected(self): return self.tcpSocket is not None def handle_rpc_event(self, op_dict): if not self.touch: return False cmd = op_dict.get("cmd") if cmd == "screenshots": self.screenshots(op_dict.get("filepath"), show_toast=False) elif cmd == "shell": adb_helper.shell(op_dict.get("param")) else: self.touch.op(op_dict) def stop(self): if self.tcpSocket: self.tcpSocket.close() self.tcpSocket = None self.signal.frame.emit(b"") if self.touch: self.touch.close_socket()
class launcher: effect = [] alpha = 0 app_select = 0 app_run = False app_sets = [ Camera(60, 200), System(160, 200), Demo(260, 200), Photo(360, 200), ] toth = Touch(480, 320, 50) btn = sipeed_button() agent = agent() def init(): launcher.agent.event(150, launcher.key_event) launcher.agent.event(50, launcher.touch_event) def touch_event(): launcher.toth.event() #print(launcher.toth.state, launcher.toth.points) if launcher.toth.state == 1: old = launcher.toth.points[0] sel = launcher.toth.points[1] #if len(launcher.effect) > 8: #launcher.effect.pop(0) #launcher.effect.append((old[0], old[1])) if 250 < sel[2] - old[2] and sel[2] - old[2] < 500: launcher.toth.state = 2 if launcher.toth.state == 2: #print(launcher.toth.state, launcher.toth.points) #launcher.effect = [] old = launcher.toth.points[0] sel = launcher.toth.points[1] #print(sel, old, sel[2] - old[2]) if sel[2] - old[2] < 1000: # start #if 136 < sel[1] and sel[1] < 200: for i in range(len(launcher.app_sets)): p = launcher.app_sets[i] x, y = p.x, 320 - p.y # print(x, p.w, y, p.h) if x < sel[0] and sel[1] < y and sel[ 0] < x + p.w and y - p.h < sel[1]: launcher.app_select = i if sel[2] - old[2] < 250: print('launcher.app_select', launcher.app_select) launcher.app_run = True def key_event(): launcher.btn.event() if launcher.btn.back() == 1: launcher.app_select -= 1 elif launcher.btn.next() == 1: launcher.app_select += 1 elif launcher.btn.home() == 1: print('start', launcher.app_select) # launcher.app_run = True # ui.canvas.draw_string(15, 120, '(%s)' % launcher.app_sets[launcher.app_select]) launcher.app_select = launcher.app_select % len( launcher.app_sets) # lock pos a = b'\x00\x00\x03\x03\x07\x07\x0E\x0E\x1C\x1F\x38\x38\x70\xF0\x00\x00\x00\x00\xC0\xC0\xE0\xE0\x70\x70\x38\xF8\x1C\x1C\x0E\x0F\x00\x00' m = b'\x00\x00\x00\x00\x00\x67\x7F\x61\x61\x61\x61\x61\x61\x61\x00\x00\x00\x00\x00\x00\x00\x1C\xFE\x86\x86\x86\x86\x86\x86\x86\x00\x00' i = b'\x00\x00\x01\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x80\x00\x00\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00\x00' g = b'\x00\x00\x00\x00\x00\x07\x1C\x18\x38\x1C\x1F\x30\x1F\x30\x70\x1F\x00\x00\x00\x00\x00\xCE\x72\x30\x38\x70\xC0\x00\xF8\x0C\x0E\xF8' o = b'\x00\x00\x00\x00\x00\x03\x1E\x38\x70\x70\x70\x38\x1C\x03\x00\x00\x00\x00\x00\x00\x00\xC0\x78\x1C\x0C\x0E\x0E\x1C\x38\xC0\x00\x00' def draw(): launcher.agent.parallel_cycle() #ui.canvas.draw_rectangle((0, 0, ui.height, ui.weight), #fill = True, color = (0, 0, 0)) #ui.canvas.draw_rectangle((0, 0, ui.height, ui.weight), fill = True, color = (0x70, 0x80, 0x90)) ui.canvas.draw_rectangle((0, 0, ui.height, ui.weight), fill=True, color=(215, 228, 181)) #ui.canvas.draw_rectangle((0, 0, ui.height, ui.weight), #fill = True, color = (37, 40, 55)) #ui.canvas.draw_string(203, 73, "Amigo", #color=(64, 64, 64), scale=8, mono_space=0) ui.canvas.draw_font(182, 82, 16, 16, launcher.a, scale=5, color=(37, 40, 55)) ui.canvas.draw_font(180, 80, 16, 16, launcher.a, scale=5, color=(0x2d, 0x85, 0xf0)) ui.canvas.draw_font(252, 82, 16, 16, launcher.m, scale=4, color=(37, 40, 55)) ui.canvas.draw_font(250, 80, 16, 16, launcher.m, scale=4, color=(0xf4, 0x43, 0x3c)) ui.canvas.draw_font(292, 82, 16, 16, launcher.i, scale=4, color=(37, 40, 55)) ui.canvas.draw_font(290, 80, 16, 16, launcher.i, scale=4, color=(0xff, 0xbc, 0x32)) ui.canvas.draw_font(332, 77, 16, 16, launcher.g, scale=4, color=(37, 40, 55)) ui.canvas.draw_font(330, 75, 16, 16, launcher.g, scale=4, color=(0x0a, 0xa8, 0x58)) ui.canvas.draw_font(392, 82, 16, 16, launcher.o, scale=4, color=(37, 40, 55)) ui.canvas.draw_font(390, 80, 16, 16, launcher.o, scale=4, color=(0xf4, 0x43, 0x3c)) #ui.canvas.draw_string(200, 70, "A", #color=(0x2d, 0x85, 0xf0), scale=8, mono_space=0) #ui.canvas.draw_string(200, 70, " m", #color=(0xf4, 0x43, 0x3c), scale=8, mono_space=0) #ui.canvas.draw_string(200, 70, " i", #color=(0xff, 0xbc, 0x32), scale=8, mono_space=0) #ui.canvas.draw_string(200, 70, " g", #color=(0x0a, 0xa8, 0x58), scale=8, mono_space=0) #ui.canvas.draw_string(200, 70, " o", #color=(0xf4, 0x43, 0x3c), scale=8, mono_space=0) value = math.cos(math.pi * launcher.alpha / 12) * 50 + 200 launcher.alpha = (launcher.alpha + 1) % 24 for pos in range(0, len(launcher.app_sets)): checked = (pos == launcher.app_select) launcher.app_sets[pos].draw(checked, value if checked else 255) #for pos in range(len(launcher.effect)): #tmp = launcher.effect[pos] #cor = (pos + 1) * 32 #ui.canvas.draw_circle(tmp[0], 320 - tmp[1], (pos + 1) * 3, thickness=1, fill=False, color=(cor, cor, cor)) launcher.agent.parallel_cycle()
def handle_touch_down(self, touch_event): touch_id = int(touch_event.value) self.active_touches[self.focused_touch_index] = Touch(touch_id)