class GetName (object): handle = GATT.Name sleep_interval = 0.150 def __init__ (self, link): self.link = link self.resp = GATTResponse( ) # super(GATTResponse, self).__init__(self) def done (self): return self.resp.received( ) or False def sleep (self): time.sleep(self.sleep_interval) def step (self): self.sleep( ) def prolog (self): self.received = self.resp.received( ) self.on_response(self.resp.received( )[0]) def operate (self): self.link.requestor.read_by_uuid_async(self.handle, self.resp) while not self.done( ): self.step( ) self.prolog( ) def on_response (self, name): print "name is ", name self.link.setName(name)
class GetName(object): handle = GATT.Name sleep_interval = 0.150 def __init__(self, link): self.link = link self.resp = GATTResponse() # super(GATTResponse, self).__init__(self) def done(self): return self.resp.received() or False def sleep(self): time.sleep(self.sleep_interval) def step(self): self.sleep() def prolog(self): self.received = self.resp.received() self.on_response(self.resp.received()[0]) def operate(self): self.link.requestor.read_by_uuid_async(self.handle, self.resp) while not self.done(): self.step() self.prolog() def on_response(self, name): print "name is ", name self.link.setName(name)
class AsyncReader(object): def __init__(self, address): self.requester = GATTRequester(address, False) self.response = GATTResponse() self.connect() self.request_data() self.wait_response() def connect(self): print("Connecting...", end=' ') sys.stdout.flush() self.requester.connect(True) print("OK!") def request_data(self): self.requester.read_by_handle_async(0x1, self.response) def wait_response(self): while not self.response.received(): time.sleep(0.1) data = self.response.received()[0] print("bytes received:", end=' ') for b in data: print(hex(ord(b)), end=' ') print("")
def read_async(self, handle): response = GATTResponse() self.req.read_by_handle_async(handle, response) while not response.received(): time.sleep(0.1) return response.received()[0]
def _get_np(self): response = GATTResponse() self.req.read_by_handle_async(self.ROBOROACH_NUM_PULSES_HANDLE, response) while not response.received(): time.sleep(0.1) num_pulses = response.received()[0] return num_pulses.encode('hex')
def read_async_command(self, handle_id, length=1): """ 1バイトリードのときはintで、 2バイト以上リードするときは配列で返します。 """ response = GATTResponse() handle = HANDLE[handle_id] self.req.read_by_handle_async(handle, response) while not response.received(): time.sleep(0.1) ans = response.received()[0] ans = map(lambda n: ord(n), ans) if length == 1: return ans[0] else: return ans
def draw(canvas): global hit_1, hit_2 global paddle1_pos, paddle2_pos, ball_pos, ball_vel, l_score, r_score global finish_flag global req, req1, response, response1 global l_score_extra, r_score_extra canvas.fill((255, 255, 0)) pygame.draw.line(canvas, WHITE, [WIDTH / 2, 0], [WIDTH / 2, HEIGHT], 1) pygame.draw.line(canvas, WHITE, [PAD_WIDTH, 0], [PAD_WIDTH, HEIGHT], 1) pygame.draw.line(canvas, WHITE, [WIDTH - PAD_WIDTH, 0], [WIDTH - PAD_WIDTH, HEIGHT], 1) pygame.draw.circle(canvas, WHITE, [WIDTH // 2, HEIGHT // 2], 70, 1) #update ball ball_pos[0] += int(ball_vel[0]) ball_pos[1] += int(ball_vel[1]) #draw paddles and ball pygame.draw.circle(canvas, BLACK, ball_pos, 20, 0) if int(ball_pos[0]) < WIDTH / 2: if not req.is_connected(): try: req1.disconnect() except: pass req.connect(True) response = GATTResponse() l_score_extra = 0 req.read_by_handle_async(0x000e, response) if response.received(): # print "1 works" print "Reading data from 1 : " + str( ord(response.received()[-1])) hit_1 = ord(response.received()[-1]) if hit_1 == 1: l_score_extra += (ball_pos[0] * 400) / WIDTH / float(WIDTH) pygame.draw.polygon(window, RED, [[ paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT ], [ paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT ], [ paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT ], [ paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT ]], 0) # top.update_idletasks() else: if not req1.is_connected(): try: req.disconnect() except: pass req1.connect(True) response1 = GATTResponse() r_score_extra = 0 req1.read_by_handle_async(0x000e, response1) if response1.received(): # print "2 works" print "Reading data from 2 :" + str( ord(response1.received()[-1])) hit_2 = ord(response1.received()[-1]) if hit_2 == 1: r_score_extra += ( (WIDTH - ball_pos[0]) * 400) / WIDTH / float(WIDTH) pygame.draw.polygon(window, RED, [[ paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT ], [ paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT ], [ paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT ], [ paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT ]], 0) # draw(canvas) #ball collision check on top and bottom walls if int(ball_pos[1]) <= BALL_RADIUS: ball_vel[1] = -ball_vel[1] sounds["da-ding"].play() # draw(canvas) if int(ball_pos[1]) >= HEIGHT + 1 - BALL_RADIUS: ball_vel[1] = -ball_vel[1] sounds["da-ding"].play() # draw(canvas) #ball collison check on gutters or paddles if int(ball_pos[0]) <= 200 and hit_1 == 0: sounds["warn"].play() # draw(canvas) if int(ball_pos[0]) >= 1400 and hit_2 == 0: sounds["warn"].play() # draw(canvas) if int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH and int( ball_pos[1]) in range(paddle1_pos[1] - HALF_PAD_HEIGHT, paddle1_pos[1] + HALF_PAD_HEIGHT, 1) and hit_1 == 1: ball_vel[0] = -ball_vel[0] sounds["click"].play() l_score -= int(l_score_extra) l_score_extra = 0 # draw(canvas) # sounds["ping"].play() elif int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH: if not finish_flag: l_score -= 10 l_score -= int(l_score_extra) l_score_extra = 0 sounds["ping"].play() time.sleep(2) ball_init(True) # draw(canvas) if int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH and int( ball_pos[1]) in range(paddle2_pos[1] - HALF_PAD_HEIGHT, paddle2_pos[1] + HALF_PAD_HEIGHT, 1) and hit_2 == 1: ball_vel[0] = -ball_vel[0] sounds["click"].play() r_score -= int(r_score_extra) r_score_extra = 0 # draw(canvas) elif int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH: if not finish_flag: r_score -= 10 r_score -= int(r_score_extra) r_score_extra = 0 sounds["ping"].play() time.sleep(2) ball_init(False) # draw(canvas) #update scores myfont1 = pygame.font.SysFont(None, 48) myfont2 = pygame.font.SysFont(None, 48) if l_score > 0 and r_score > 0: myfont1.set_underline(1) label1 = myfont1.render("PLAYER1", 1, (0, 0, 0)) myfont1.set_underline(0) label5 = myfont1.render(str(l_score), 1, (0, 0, 0)) myfont2.set_underline(1) label2 = myfont2.render("PLAYER2", 1, (0, 0, 0)) myfont2.set_underline(0) label6 = myfont2.render(str(r_score), 1, (0, 0, 0)) canvas.blit(label1, (250, 20)) canvas.blit(label2, (1050, 20)) canvas.blit(label5, (290, 65)) canvas.blit(label6, (1090, 65)) # top.update_idletasks() else: finish_flag = 1 if l_score > r_score: label1 = myfont1.render("WIN", 1, (0, 255, 0)) label2 = myfont2.render("LOST", 1, (255, 0, 0)) canvas.blit(label1, (250, 20)) canvas.blit(label2, (1050, 20)) elif r_score > l_score: label1 = myfont1.render("LOST", 1, (255, 0, 0)) label2 = myfont2.render("WIN", 1, (0, 255, 0)) canvas.blit(label1, (250, 20)) canvas.blit(label2, (1050, 20)) else: label3 = myfont1.render("MATCH DRAW", 1, (0, 0, 255)) canvas.blit(label3, (850, 20))
import time import os service = DiscoveryService("hci1") devices = service.discover(2) tag_address = "B0:B4:48:BF:C3:83" for address, name in devices.items(): print("name: {}, address: {}".format(name, address)) if address == tag_address: print "cilia found" req = GATTRequester(tag_address, False, "hci1") response = GATTResponse() req.connect() req.read_by_handle_async(0x3A, response) while not response.received(): time.sleep(0.1) steps = response.received()[0] #print "steps..." #print type(steps) #print steps #for b in steps: # print hex(ord(b)),' ' req.write_by_handle(0x3C, str(bytearray([0xff, 0xff]))) req.write_by_handle(0x3E, str(bytearray([0x64]))) data = req.read_by_handle(0x3C)[0] #for d in data: # print hex(ord(d)),' ' #print("")
def draw(canvas): global hit_1 ,hit_2 global paddle1_pos, paddle2_pos, ball_pos, ball_vel, l_score, r_score global finish_flag global req, req1, response, response1 global l_score_extra, r_score_extra canvas.fill((255,255,0)) pygame.draw.line(canvas, WHITE, [WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1) pygame.draw.line(canvas, WHITE, [PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1) pygame.draw.line(canvas, WHITE, [WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1) pygame.draw.circle(canvas, WHITE, [WIDTH//2, HEIGHT//2], 70, 1) #update ball ball_pos[0] += int(ball_vel[0]) ball_pos[1] += int(ball_vel[1]) #draw paddles and ball pygame.draw.circle(canvas, BLACK, ball_pos, 20, 0) if int(ball_pos[0]) < WIDTH/2 : if not req.is_connected(): try: req1.disconnect() except: pass req.connect(True) response = GATTResponse() l_score_extra=0 req.read_by_handle_async(0x000e, response) if response.received(): # print "1 works" print "Reading data from 1 : " + str(ord(response.received()[-1])) hit_1 = ord(response.received()[-1]) if hit_1 == 1: l_score_extra += (ball_pos[0]*400)/WIDTH/float(WIDTH) pygame.draw.polygon(window, RED, [[paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT], [paddle1_pos[0] - HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT], [paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] + HALF_PAD_HEIGHT], [paddle1_pos[0] + HALF_PAD_WIDTH, paddle1_pos[1] - HALF_PAD_HEIGHT]], 0) # top.update_idletasks() else: if not req1.is_connected(): try: req.disconnect() except: pass req1.connect(True) response1 = GATTResponse() r_score_extra=0 req1.read_by_handle_async(0x000e, response1) if response1.received(): # print "2 works" print "Reading data from 2 :" + str(ord(response1.received()[-1])) hit_2 = ord(response1.received()[-1]) if hit_2 == 1: r_score_extra += ((WIDTH-ball_pos[0])*400)/WIDTH/float(WIDTH) pygame.draw.polygon(window, RED, [[paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT], [paddle2_pos[0] - HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT], [paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] + HALF_PAD_HEIGHT], [paddle2_pos[0] + HALF_PAD_WIDTH, paddle2_pos[1] - HALF_PAD_HEIGHT]], 0) # draw(canvas) #ball collision check on top and bottom walls if int(ball_pos[1]) <= BALL_RADIUS: ball_vel[1] = - ball_vel[1] sounds["da-ding"].play() # draw(canvas) if int(ball_pos[1]) >= HEIGHT + 1 - BALL_RADIUS: ball_vel[1] = -ball_vel[1] sounds["da-ding"].play() # draw(canvas) #ball collison check on gutters or paddles if int(ball_pos[0])<=200 and hit_1==0: sounds["warn"].play() # draw(canvas) if int(ball_pos[0])>=1400 and hit_2==0: sounds["warn"].play() # draw(canvas) if int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH and int(ball_pos[1]) in range(paddle1_pos[1] - HALF_PAD_HEIGHT,paddle1_pos[1] + HALF_PAD_HEIGHT,1) and hit_1==1: ball_vel[0] = -ball_vel[0] sounds["click"].play() l_score -= int(l_score_extra) l_score_extra = 0 # draw(canvas) # sounds["ping"].play() elif int(ball_pos[0]) <= BALL_RADIUS + PAD_WIDTH: if not finish_flag: l_score -= 10 l_score -= int(l_score_extra) l_score_extra = 0 sounds["ping"].play() time.sleep(2) ball_init(True) # draw(canvas) if int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH and int(ball_pos[1]) in range(paddle2_pos[1] - HALF_PAD_HEIGHT,paddle2_pos[1] + HALF_PAD_HEIGHT,1) and hit_2==1: ball_vel[0] = -ball_vel[0] sounds["click"].play() r_score -= int(r_score_extra) r_score_extra = 0 # draw(canvas) elif int(ball_pos[0]) >= WIDTH + 1 - BALL_RADIUS - PAD_WIDTH: if not finish_flag: r_score -= 10 r_score -= int(r_score_extra) r_score_extra = 0 sounds["ping"].play() time.sleep(2) ball_init(False) # draw(canvas) #update scores myfont1 = pygame.font.SysFont(None,48) myfont2 = pygame.font.SysFont(None,48) if l_score>0 and r_score>0: myfont1.set_underline(1) label1 = myfont1.render("PLAYER1", 1, (0,0,0)) myfont1.set_underline(0) label5 = myfont1.render(str(l_score), 1, (0,0,0)) myfont2.set_underline(1) label2 = myfont2.render("PLAYER2", 1, (0,0,0)) myfont2.set_underline(0) label6 = myfont2.render(str(r_score), 1, (0,0,0)) canvas.blit(label1, (250,20)) canvas.blit(label2, (1050, 20)) canvas.blit(label5, (290,65)) canvas.blit(label6, (1090,65)) # top.update_idletasks() else: finish_flag = 1 if l_score>r_score: label1=myfont1.render("WIN", 1, (0,255,0)) label2=myfont2.render("LOST",1, (255,0,0)) canvas.blit(label1, (250,20)) canvas.blit(label2, (1050, 20)) elif r_score>l_score: label1=myfont1.render("LOST",1, (255,0,0)) label2=myfont2.render("WIN" ,1, (0,255,0)) canvas.blit(label1, (250,20)) canvas.blit(label2, (1050, 20)) else: label3=myfont1.render("MATCH DRAW",1, (0,0,255)) canvas.blit(label3, (850,20))
time.sleep(0.3) continue #forward/back right = olddata["root_y"] - data["root_y"] left = data["root_y"] - olddata["root_y"] #turn right = ((right + (olddata["root_x"] - data["root_x"])) / 20) + 90 left = ((left + (olddata["root_x"] - data["root_x"])) / 20) + 90 left = toStr(left) right = toStr(right) full = right + left req.write_by_handle_async(0x002a, str(bytearray(full)), response) while not response.received(): time.sleep(0.1) ++retry if retry > 3: req.disconnect() req.connect(True, "random") retry = 0 try: req.is_connected() except: req.connect(True, "random") olddata = data time.sleep(0.3)