def update(self):
        gx, gy = motion.get_gravity()[:2]
        self.physics_gravity = (gx * 10, gy * 10)

        time_str = time.strftime('%H:%M:%S')
        if time_str == self.time_str:
            return
        for i, c in enumerate(time_str):
            if c != self.time_str[i]:
                # Digit has changed, replace it with a new sprite...
                old = self.clock_nodes[i]
                sprite = sk.SpriteNode(self.textures[c])
                sprite.position += old.position.x, old.position.y + s * 2
                sprite.alpha = 0.0
                sprite.run_action(sk.Action.fade_in(0.7))
                move = sk.Action.move_by(0, -s * 2, 0.85)
                move.timing_mode = sk.TIMING_EASE_OUT
                sprite.run_action(move)
                self.clock_nodes[i] = sprite
                self.add_child(sprite)
                # Make the old sprite drop by assigning a physics body:
                b = sk.PhysicsBody.from_texture(old.texture)
                b.restitution = 0.25
                old.physics_body = b
                self.dropped_sprites.add(old)
                # Remove sprites that are no longer visible:
                offscreen = {
                    s
                    for s in self.dropped_sprites if s.position[1] < 0
                }
                map(sk.Node.remove_from_parent, offscreen)
                self.dropped_sprites -= offscreen
        self.time_str = time_str
Exemple #2
0
def main():
    console.alert(
        'Motion Plot',
        'When you tap Continue, accelerometer (motion) data will be recorded for 5 seconds.',
        'Continue')
    motion.start_updates()
    sleep(0.2)
    print('Capturing motion data...')
    num_samples = 100
    data = []
    for i in range(num_samples):
        sleep(0.05)
        g = motion.get_gravity()
        data.append(g)
    motion.stop_updates()
    print('Capture finished, plotting...')

    x_values = [x * 0.05 for x in range(num_samples)]
    for i, color, label in zip(range(3), 'rgb', 'XYZ'):
        plt.plot(x_values, [g[i] for g in data], color, label=label, lw=2)
    plt.grid(True)
    plt.xlabel('t')
    plt.ylabel('G')
    plt.gca().set_ylim([-1.0, 1.0])
    plt.legend()
    plt.show()
Exemple #3
0
def main():
	console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue')
	motion.start_updates()
	sleep(0.2)
	print('Capturing motion data...')
	w = 1000
	h = 1200
	while True:
		sleep(0.01)
		current = motion.get_gravity()
		newMotion = [0,0,0]
		for i in range(len(current)):
			newMotion[i] = (current[i]*(10**3))//1
		#print(newMotion)
		x = newMotion[0]+500
		y = newMotion[1]+500
		z = newMotion[2]+1000
		goalX = w/2
		goalY = h/2
		canvas.set_size(w, h)
		canvas.set_fill_color(0, 0, 0)
		canvas.fill_ellipse(goalX, goalY, 30, 30)
		if (abs(goalX-x) < 10 and abs(goalY-y) < 10):
			canvas.set_fill_color(0, 1, 0)
			canvas.fill_ellipse(x, y, 30, 30)
		else:
			canvas.set_fill_color(1, 0, 0)
			canvas.fill_ellipse(x, y, 30, 30)

	motion.stop_updates()
	print('Capture finished, plotting...')
Exemple #4
0
def main():
    console.alert('Motion Experiment 2',
                  'yo gang gang, we gonna measure this motion', 'Continue')
    motion.start_updates()
    sleep(0.2)
    print('Capturing motion data...')
    w = 512
    h = 512
    while True:
        sleep(0.1)
        current = motion.get_gravity()
        newMotion = [0, 0, 0]
        for i in range(len(current)):
            newMotion[i] = (current[i] * (10**3)) // 1
        #print(newMotion)
        if newMotion[0] < 1001 and newMotion[0] > 900 and newMotion[
                1] > -50 and newMotion[1] < 50 and newMotion[
                    2] > -50 and newMotion[2] < 50:
            canvas.set_size(w, h)
            canvas.set_fill_color(1, 0, 0)
            canvas.fill_ellipse(0, 0, w, h)
        if newMotion[0] > -1001 and newMotion[0] < -900 and newMotion[
                1] > -50 and newMotion[1] < 50 and newMotion[
                    2] > -50 and newMotion[2] < 50:
            canvas.set_size(w, h)
            canvas.set_fill_color(0, 1, 0)
            canvas.fill_ellipse(0, 0, w, h)
        if newMotion[0] > -50 and newMotion[0] < 50 and newMotion[
                1] > -50 and newMotion[1] < 50 and newMotion[
                    2] > -1001 and newMotion[2] < -900:
            canvas.set_size(w, h)
            canvas.set_fill_color(0, 0, 1)
            canvas.fill_ellipse(0, 0, w, h)
    motion.stop_updates()
    print('Capture finished, plotting...')
Exemple #5
0
def environ_data(sender):
    import motion
    import location

    motion.start_updates()
    location.start_updates()

    x = motion.get_attitude()
    environ['att'].text = str(x) + '\n' + environ['att'].text
    x = motion.get_gravity()
    environ['grav'].text = str(x) + '\n' + environ['grav'].text
    x = motion.get_user_acceleration()
    environ['acc'].text = str(x) + '\n' + environ['acc'].text
    x = motion.get_magnetic_field()
    environ['mag'].text = str(x) + '\n' + environ['mag'].text

    x = location.get_location()
    coord = {'latitude': x['latitude'], 'longitude': x['longitude']}
    print(coord)
    y = location.reverse_geocode(coord)
    print(y)
    environ['geo'].text = str(x)

    motion.stop_updates()
    location.stop_updates()
Exemple #6
0
 def update(self):
     self.text.text = str(len(self.particles))
     (x, y, z) = motion.get_gravity()
     gravity = (-y / 10, x / 10)
     for particle in self.particles:
         particle.update(self.size, gravity)
     self.addParticle()
 def settilt(self):
     '''Reset ball position to the center of the box.'''
     global tilt
     gy, gx, gz = motion.get_gravity()
     tilt = gy
     tvx, tvy, tvw, tvh = self.frame
     self.ball.x = int(tvw / 2) - (self.ball.bounds[2] / 2) + tvx
     self.ball.y = int(tvh / 2) - (self.ball.bounds[3] / 2) + tvy
 def settilt(self):
   '''Reset ball position to the center of the box.'''
   global tilt
   gy, gx, gz = motion.get_gravity()
   tilt = gy
   tvx, tvy, tvw, tvh = self.frame
   self.ball.x = int(tvw / 2) - (self.ball.bounds[2] / 2) + tvx
   self.ball.y = int(tvh / 2) - (self.ball.bounds[3] / 2) + tvy
    def draw(self):
        global box_node, box
        global pitch, roll, yaw, ax, ay, az, gx, gy, gz
        global main_view
        global ax1, fig
        #処理が追い付かないのでタイマーをいれる
        #time.sleep(0.01)
        #加速度、ジャイロの値を更新
        ax, ay, az = motion.get_user_acceleration()
        gx, gy, gz = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        mgx, mgy, mgz, mga = motion.get_magnetic_field()

        pitch, roll, yaw = [x for x in gravity_vectors]
        # ラジアン→度へ変換
        pitch = -pitch * 180 / 3.1415926
        roll = roll * 180 / 3.1415926
        yaw = -yaw * 180 / 3.1415926
        #再描画
        box_node.runAction_(
            SCNAction.rotateToX_y_z_duration_(math.pi * pitch / 180,
                                              math.pi * roll / 180,
                                              -math.pi * yaw / 180, 0))
        #加速度、ジャイロ、地磁気センサーの値を表示
        main_view['ax'].text = str(round(ax, 2))
        main_view['ay'].text = str(round(ay, 2))
        main_view['az'].text = str(round(az, 2))

        main_view['gx'].text = str(round(gx, 2))
        main_view['gy'].text = str(round(gy, 2))
        main_view['gz'].text = str(round(gz, 2))

        main_view['mx'].text = str(round(mgx, 2))
        main_view['my'].text = str(round(mgy, 2))
        main_view['mz'].text = str(round(mgz, 2))
        # graphics
        self.py.pop(0)
        self.ry.pop(0)
        self.yy.pop(0)  # left
        self.py.append(pitch)
        self.ry.append(roll)
        self.yy.append(yaw)  # add data

        ax1.plot(self.xl, self.py, color='lightgreen', lw='1')  # pitch graph
        ax1.plot(self.xl, self.ry, color='red', lw='1')  # roll graph
        ax1.plot(self.xl, self.yy, color='skyblue', lw='1')  # yaw graph

        plt.savefig('rt.png')  # save the graph on the consolen
        main_view['imageview1'].image = ui.Image.named('rt.png')  # imageview
        plt.cla()  # clear graph
        plt.close()  # close graph
        fig = plt.figure()  #
        ax1 = fig.add_subplot(111)  #
        ax1.grid(True)
        ymin = -180
        ymax = 180
        plt.ylim(ymin, ymax)
Exemple #10
0
 def update(self):
     x, y, z = motion.get_gravity()
     if abs(x) > abs(y):
         if x > 0:
             self.label_node.text = 'LANDSCAPE, RIGHT'
         else:
             self.label_node.text = 'LANDSCAPE, LEFT'
     else:
         if y < 0:
             self.label_node.text = 'PORTRAIT'
Exemple #11
0
 def update(self):
     x, y, z = motion.get_gravity()
     if abs(x) > abs(y):
         if x > 0:
             self.label_node.text = 'LANDSCAPE, RIGHT'
         else:
             self.label_node.text = 'LANDSCAPE, LEFT'
     else:
         if y < 0:
             self.label_node.text = 'PORTRAIT'
Exemple #12
0
    def update(self):
        motion.start_updates()
        time.sleep(0.3)
        g3 = motion.get_gravity()
        motion.stop_updates()
 #       for g in g3:
 #           print(round(g, 3), end = '\t')
 #       print()
        self.isProne = g3[2] > 0.3
        
        self.isLowBattery = self.device.batteryLevel() < 0.05
Exemple #13
0
    def _take_photo(self,size):
        while size == self._que.qsize():
            pass
        self.whiteWaiter.submit(self._whiteWaiter)
        self.savingPhotoView.alpha = 0.5

        # motion.start_updates()
        motionData = motion.get_gravity()
        # motion.stop_updates()

        if motionData[0] >= 0.5:
            rot = 1
        elif motionData[0] <= -0.5:
            rot = 0
        else:
            rot = 3
        # delta = time.time() - self.shoot
        # print('shooted time:{}'.format(delta))
        ciimage = self._que.get()

        uiImg = UIImage.imageWithCIImage_scale_orientation_(
            ciimage, 1.0, rot)

        if self._fileformat == 'PNG':
            data = ObjCInstance(c.UIImagePNGRepresentation(uiImg.ptr))
            fmt = 'png'
        elif self._fileformat == 'CIImage':
            data = uiImg.CIImage()
        elif self._fileformat == 'UIImage':
            data = uiImg
        else:
            quality = 0.8
            data = ObjCInstance(
                c.UIImageJPEGRepresentation(uiImg.ptr, quality))
            fmt = 'jpg'

        if self._saveAlbum:
            temppath = self._saveData2temp(data,fmt)
            photos.create_image_asset(temppath)

        if self._fileformat == 'PIL':
            temppath = self._saveData2temp(data,'pil')
            data = self._temp2pil(temppath)
            
        self._que.task_done()
        
        if self._autoclose:
            self.data = data
            time.sleep(1)
            self.close()
        
        if self._que.all_tasks_done:
            self.latestPhotoView.image = self._get_latest_photo_from_path(temppath)
            self.savingPhotoView.alpha = 0.0
Exemple #14
0
    def _rotateViewsAnimation(self):
        motionData = motion.get_gravity()
        # print(motionData)
        if motionData[0] >= 0.5:

            rad = math.pi/2*-1
        elif motionData[0] <= -0.5:
            rad = math.pi/2
        else:
            rad = 0

        def animation():
            self.zoomView.transform = ui.Transform.rotation(rad)
            self.latestPhotoView.transform = ui.Transform.rotation(rad)
        ui.animate(animation, duration=0.3)
Exemple #15
0
def ios(rate=quantity(1, units.second)):
    """Retrieve motion information from an iOS device.

    This component requires the `motion` module provided by Pythonista.

    Parameters
    ----------
    rate: time quantity, required
        Rate at which motion data will be retrieved.

    Yields
    ------
    records: dict
        Records will contain information including the current acceleration due to gravity and the user, along with device attitude.
    """

    import time
    import motion # pylint: disable=import-error

    rate = rate.to(units.seconds).magnitude

    motion.start_updates()

    try:
        while True:
            gravity = quantity(motion.get_gravity(), units.meters * units.seconds * units.seconds)
            acceleration = quantity(motion.get_user_acceleration(), units.meters * units.seconds * units.seconds)
            attitude = quantity(motion.get_attitude(), units.radians)

            record = dict()
            add_field(record, ("gravity", "x"), gravity[0])
            add_field(record, ("gravity", "y"), gravity[1])
            add_field(record, ("gravity", "z"), gravity[2])

            add_field(record, ("acceleration", "x"), acceleration[0])
            add_field(record, ("acceleration", "y"), acceleration[1])
            add_field(record, ("acceleration", "z"), acceleration[2])

            add_field(record, ("attitude", "roll"), attitude[0])
            add_field(record, ("attitude", "pitch"), attitude[1])
            add_field(record, ("attitude", "yaw"), attitude[2])

            yield record

            time.sleep(rate)

    except GeneratorExit:
        motion.stop_updates()
Exemple #16
0
    def update(self):
        m = motion.get_magnetic_field()
        self.mf.text = '{:.1f} {:.1f} {:.1f} {:.3f}'.format(
            m[0], m[1], m[2], m[3])

        acc = motion.get_user_acceleration()
        self.acc.text = '{:.3f} {:.3f} {:.3f}'.format(acc[0], acc[1], acc[2])

        grav = motion.get_gravity()
        self.grav.text = '{:.3f} {:.3f} {:.3f}'.format(grav[0], grav[1],
                                                       grav[2])

        #determine screen orientation. Portrait modes flip 180 deg if the device is face down. Use local z gravity to detect.

        orient = int(self.wv.eval_js('window.orientation'))

        if (orient == 0):
            #portrait, home button on bottom
            if grav[2] <= 0.:
                init_angle = -90
            else:
                init_angle = 90

        elif (orient == 90):
            #landscape, home button on right
            init_angle = 0

        elif (orient == -90):
            #landscape, home button on right
            init_angle = 180

        else:
            #portrait, home button on top
            if grav[2] <= 0.:
                init_angle = 90
            else:
                init_angle = -90

        self.ori.text = str(orient)

        att = motion.get_attitude()
        self.att.text = '{:.3f} {:.3f} {:.3f}'.format(att[0], att[1], att[2])

        ang = (init_angle - int(math.degrees(att[2]))) % 360
        self.ang.text = str(ang)
Exemple #17
0
def main():
    console.alert('Motion Experiment 2',
                  'yo gang gang, we gonna measure this motion', 'Continue')
    motion.start_updates()
    sleep(0.2)
    print('Capturing motion data...')
    while True:
        sleep(0.05)
        current = motion.get_gravity()
        newMotion = [0, 0, 0]
        for i in range(len(current)):
            newMotion[i] = (current[i] * (10**2)) // 1
        x = newMotion[0] + 100
        y = newMotion[1] + 100
        z = newMotion[2] + 100
        print(x, y, z)
    motion.stop_updates()
    print('Capture finished, plotting...')
Exemple #18
0
def waitForLandscapeMode():
    msg = 'Please, hold your phone in landscape mode'
    console.hud_alert(msg, duration=3)
    motion.start_updates()
    try:
        count = 0
        while True:
            x, y, z = motion.get_gravity()
            count += 1
            if count > 2:
                if abs(x) > abs(y):
                    break
                else:
                    console.hud_alert(msg, duration=2)
            time.sleep(0.5)
    finally:
        motion.stop_updates()
    time.sleep(1)
 def draw(self):
   '''Update the ball position based on gravity.'''
   if self.ball == None : return
   m = Point(0,0)
   gy, gx, gz = motion.get_gravity()
   gy -= tilt
   if abs(gx) >= gravsense:
     m.x = mrate * sgn(gx)
   if abs(gy) >= gravsense:
     m.y = mrate * sgn(gy)
   #If movement in x or y then update.
   if m.x or m.y:
     tvx, tvy, tvw, tvh = self.frame
     tvmaxx = tvx + tvw - self.ball.bounds[2]
     tvmaxy = tvy + tvh - self.ball.bounds[3]
     self.ball.x = max(tvx, min(tvmaxx, self.ball.x + m.x)) #Clamp values within box.
     self.ball.y = max(tvy, min(tvmaxy, self.ball.y + m.y))
     self.ball.set_needs_display()
Exemple #20
0
 async def gravity(self, instance, async_lib):
     motion.start_updates()
     try:
         while True:
             timestamp = time.time()
             gravity = motion.get_gravity()
             user_acceleration = motion.get_user_acceleration()
             attitude = motion.get_attitude()
             magnetic_field = motion.get_magnetic_field()
             await self.gravity.write(value=gravity, timestamp=timestamp)
             await self.user_acceleration.write(value=user_acceleration,
                                                timestamp=timestamp)
             await self.attitude.write(value=attitude, timestamp=timestamp)
             await self.magnetic_field.write(value=magnetic_field,
                                             timestamp=timestamp)
             await async_lib.library.sleep(0.01)
     finally:
         # TODO: put in @gravity.shutdown when in released version
         motion.stop_updates()
Exemple #21
0
 def draw(self):
     '''Update the ball position based on gravity.'''
     if self.ball == None: return
     m = Point(0, 0)
     gy, gx, gz = motion.get_gravity()
     gy -= tilt
     if abs(gx) >= gravsense:
         m.x = mrate * sgn(gx)
     if abs(gy) >= gravsense:
         m.y = mrate * sgn(gy)
     #If movement in x or y then update.
     if m.x or m.y:
         tvx, tvy, tvw, tvh = self.frame
         tvmaxx = tvx + tvw - self.ball.bounds[2]
         tvmaxy = tvy + tvh - self.ball.bounds[3]
         self.ball.x = max(tvx, min(tvmaxx, self.ball.x +
                                    m.x))  #Clamp values within box.
         self.ball.y = max(tvy, min(tvmaxy, self.ball.y + m.y))
         self.ball.set_needs_display()
Exemple #22
0
def Main(socket):   
    print('Server Started')
    
    s.listen(1)
    c, addr = s.accept()
    print('Connection From: ' + str(addr))
    
    motion.start_updates()
    while True:
        xaccel, yaccel, zaccel= motion.get_gravity()
        xrot, yrot, zrot= motion.get_attitude()
        message= str(xaccel)[:6] + ',' + str(yaccel)[:6] + ',' + str(zaccel)[:6] + ','+ str(xrot)[:6] + ',' + str(yrot)[:6] + ',' + str(zrot)[:6]
        print(message)
        try:
            c.send(str(message))
            time.sleep(0.05)
        except:
            break
    c.close()
    Main(socket)
Exemple #23
0
def main():
	console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue')
	motion.start_updates()
	sleep(0.2)
	print('Capturing motion data...')
	num_samples = 100
	w = 512
	h = 512
	for nums in range(num_samples):
		canvas.set_size(w, h)
		sleep(0.05)
		current = motion.get_gravity()
		newMotion = [0,0,0]
		for i in range(len(current)):
			newMotion[i] = (current[i]*(10**3))//1
		print(newMotion)
		canvas.set_fill_color(1, 0, 0)
		canvas.fill_ellipse(0, 0, newMotion[1], newMotion[2])
	motion.stop_updates()
	print('Capture finished, plotting...')
Exemple #24
0
def main():
	console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue')
	motion.start_updates()
	sleep(0.2)
	print('Capturing motion data...')

	while True:
		sleep(0.05)
		current = motion.get_gravity()
		newMotion = [0,0,0]
		for i in range(len(current)):
			newMotion[i] = (current[i]*(10**3))//1
		#print(newMotion)
		if newMotion[0] < 1001 and newMotion[0] > 900 and newMotion[1] > -50 and newMotion[1] < 50 and newMotion[2] > -50 and newMotion[2] < 50:
			print("Position: Landscape Right")
		if newMotion[0] > -1001 and newMotion[0] < -900 and newMotion[1] > -50 and newMotion[1] < 50 and newMotion[2] > -50 and newMotion[2] < 50:
			print("Position: Landscape Left")
		if newMotion[0] > -50 and newMotion[0] < 50 and newMotion[1] > -50 and newMotion[1] < 50 and newMotion[2] > -1001 and newMotion[2] < -900:
			print("Position: Flat")
	motion.stop_updates()
	print('Capture finished, plotting...')
Exemple #25
0
def main():
    console.alert('Motion Experiment 2',
                  'yo gang gang, we gonna measure this motion', 'Continue')
    motion.start_updates()
    sleep(0.2)
    print('Capturing motion data...')
    w = 1000
    h = 1200
    goalX = w / 2
    goalY = h / 2
    moalX = w / 2 - 100
    moalY = h / 2 - 100
    while True:
        sleep(0.01)
        current = motion.get_gravity()
        newMotion = [0, 0, 0]
        for i in range(len(current)):
            newMotion[i] = (current[i] * (10**2)) // 1
        #print(newMotion)
        x = newMotion[0] + 100
        y = newMotion[1] + 100
        z = newMotion[2] + 100
        goalX += (x - 100)
        goalY += (y - 100)
        moalX += (x - 102) * 2
        moalY += (y - 102) * 2
        if goalX <= 0 or goalY <= 0 or goalX >= w or goalY >= h:
            goalX -= (x - 100)
            goalY -= (y - 100)
        if moalX <= 0 or moalY <= 0 or moalX >= w or moalY >= h:
            moalX -= (x - 102) * 2
            moalY -= (y - 102) * 2
        canvas.set_size(w, h)
        canvas.set_fill_color(0, 0, 0)
        canvas.fill_ellipse(goalX, goalY, 30, 30)
        canvas.set_fill_color(0, 0, 1)
        canvas.fill_ellipse(moalX, moalY, 30, 30)

    motion.stop_updates()
    print('Capture finished, plotting...')
Exemple #26
0
def main():
	console.alert('Motion Plot', 'When you tap Continue, accelerometer (motion) data will be recorded for 5 seconds.', 'Continue')
	motion.start_updates()
	sleep(0.2)
	print 'Capturing motion data...'
	num_samples = 100
	data = []
	for i in xrange(num_samples):
		sleep(0.05)
		g = motion.get_gravity()
		data.append(g)
	motion.stop_updates()
	print 'Capture finished, plotting...'	
	
	x_values = [x*0.05 for x in xrange(num_samples)]
	for i, color, label in zip(range(3), 'rgb', 'XYZ'):
		plt.plot(x_values, [g[i] for g in data], color, label=label, lw=2)
	plt.grid(True)
	plt.xlabel('t')
	plt.ylabel('G')
	plt.gca().set_ylim([-1.0, 1.0])
	plt.legend()
	plt.show()
    def draw(self):
        #Center scaling
        self.centerX = self.size.w / 2
        self.centerY = self.size.h / 2
        self.centerX2 = self.size.w / 2
        self.centerY2 = self.size.h * (1 / 2) - scale * 3.5

        time.sleep(0.1)  #time between each redraw
        self.timerCount += 1  #add to the timer to animate and do things

        #Title Text
        tint(0, 0, 0, 1)
        text('Thread 1.0',
             font_name='Courier',
             font_size=16.0,
             x=self.centerX2,
             y=self.centerY2 + self.radius + 400,
             alignment=5)

        #Locations recorded count
        tint(0.4, 0.4, 0.4, 1)

        locationCountText = "Locations: " + str(len(self.locations))
        text(locationCountText,
             font_name='Verdana',
             font_size=10,
             x=50,
             y=self.centerY2 + self.radius + 400,
             alignment=5)

        if self.checkedOnce == 4:
            locationLeftText = "Left: " + str(len(self.locationsLeft))
            text(locationLeftText,
                 font_name='Verdana',
                 font_size=10,
                 x=self.size.x - 60,
                 y=self.centerY2 + self.radius + 400,
                 alignment=5)

        #motion
        gravX, gravY, gravZ = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        yaw = gravity_vectors[2]

        #convert yaw to degrees
        yaw = -yaw * 180 / math.pi

        ############# redraw screen ############
        #Reset Button
        fill(0.9, 0.9, 0.9)
        stroke_weight(0)
        ellipse(self.centerX2 - scale * 3 - self.radius,
                self.centerY2 - self.radius - 130, self.radius * 2,
                self.radius * 2)
        tint(0.4, 0.4, 0.4, 1)
        text('Reset',
             font_name='Verdana',
             font_size=12.0,
             x=self.centerX2 - 112,
             y=self.centerY2 + self.radius - 167,
             alignment=5)

        #SOS Button and
        fill(0.95, 0.6, 0.6)
        tint(1, 1, 1, 1)
        if self.MoreState == True:

            #SOS Button
            fill(0.95, 0.6, 0.6)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 - 0.0 - self.radius,
                    self.centerY2 - self.radius - 130, self.radius * 2,
                    self.radius * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

            #MAP Button
            if len(self.locations) >= 2:
                fill(0.6, 0.6, 0.95)
                tint(1, 1, 1, 1)
                ellipse(self.centerX2 - scale * 3 - self.radius,
                        self.centerY2 - self.radius - 20, self.radius * 2,
                        self.radius * 2)
                text('Map View',
                     font_name='Verdana',
                     font_size=12.0,
                     x=self.centerX2 - 112,
                     y=self.centerY2 + self.radius - 57,
                     alignment=5)

            #SOS Button
            fill(0.95, 0.6, 0.6)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 - 0.0 - self.radius,
                    self.centerY2 - self.radius - 130, self.radius * 2,
                    self.radius * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

        #More button at the bottom of screen
        elif self.MoreState == False:
            ellipse(self.size.x / 2 - self.radius / 2, 0 - self.radius / 2 - 5,
                    self.radius, self.radius)

        #compass draw
        tint(0.4, 0.4, 0.4, 1)
        stroke(0.4, 0.4, 0.4)
        stroke_weight(0)
        fill(0.9, 0.9, 0.9)
        ellipse(self.centerX2 + scale * 3 - self.radius,
                self.centerY2 - self.radius - 130, self.radius * 2,
                self.radius * 2)
        stroke_weight(0)
        ellipse(self.centerX2 + scale * 3 - self.radius + 3,
                self.centerY2 - self.radius - 127, self.radius * 1.85,
                self.radius * 1.85)
        ellipse(self.centerX2 + scale * 3 - self.radius + 6,
                self.centerY2 - self.radius - 124, self.radius * 1.7,
                self.radius * 1.7)
        yawSin = math.sin(math.radians(yaw))
        yawCos = math.cos(math.radians(yaw))
        stroke(0.4, 0.4, 0.4)
        stroke_weight(2)
        line(self.centerX2 - yawCos * self.radius + scale * 3,
             self.centerY2 - yawSin * self.radius - 130,
             self.centerX2 + yawCos * self.radius + scale * 3,
             self.centerY2 + yawSin * self.radius - 130)
        stroke(1, 0.3, 0.3)
        line(300, 72.5, self.centerX2 + yawCos * self.radius + scale * 3,
             self.centerY2 + yawSin * self.radius - 130)
        #circle on top of compass
        stroke_weight(0)
        stroke(0.4, 0.4, 0.4)
        fill(1, 1, 1)
        ellipse(self.centerX2 + scale * 3 - self.radius + 9.5,
                self.centerY2 - self.radius - 120.5, self.radius * 1.5,
                self.radius * 1.5)

        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''
            return directionSym

        #compass text
        tint(0.4, 0.4, 0.4, 1)
        if self.compassStat == False:
            text(directionText(yaw),
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + scale * 3,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)
        else:
            tempYaw = yaw + 180 + 90
            if tempYaw > 360:
                tempYaw = tempYaw % 360
            yawString = str(int(round(tempYaw, 0))) + chr(186)
            text(yawString,
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + scale * 3,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

        #Center Screen Text
        tint(0.4, 0.4, 0.4, 1)
        if self.measuringOn == False and self.checkedOnce == 0:
            text('Tap to Start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)
        elif self.measuringOn == True and self.checkedOnce == 1:
            if self.timerCount // 10 % 3 == 0:
                text('Measuring.',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 1:
                text('Measuring..',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 2:
                text('Measuring...',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            text('Tap to Stop',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 120,
                 alignment=5)
        elif self.measuringOn == False and self.checkedOnce == 2:
            text('Calculating',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)

        #When measuring is on record locations and put into array
        if self.measuringOn == True:
            current = location.get_location()
            latLong = (round(current['latitude'],
                             4), round(current['longitude'], 4)
                       )  #solved the close points problem using rounding
            if latLong not in self.locations:
                self.locations += [latLong]

        #after measuring is off, setup array to use (states Calculating)
        elif self.checkedOnce == 2 and len(self.locations) > 2:
            #the first and last locations are usually off so pop them off
            # self.locations.pop(0)
            # self.locations.pop()
            self.locationsReversed = copy.deepcopy(self.locations)
            self.locationsReversed.reverse()
            self.locationsLeft = copy.deepcopy(self.locations)

            #do this to avoid tapping too early
            self.checkedOnce += 1

        #after the array set up, tap to get back to where you were
        elif self.checkedOnce == 3:
            text('Tap to return to start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)

            # locationList = str(self.locations[0]) + ' ' + str(self.locations[-1])
            # text(locationList, font_name='Verdana', font_size=5.0, x=self.centerX2, y=self.centerY2+self.radius+80, alignment=5)
            # locationListReversed = str(self.locationsReversed[0]) + ' ' + str(self.locationsReversed[-1])
            # text(locationListReversed, font_name='Verdana', font_size=5.0, x=self.centerX2, y=self.centerY2+self.radius+60, alignment=5)

        #The return back..
        elif self.checkedOnce == 4:
            x, y = self.locations[-1]
            x = round(x, 10)
            y = round(y, 10)
            loc = "[" + str(x) + ", " + str(y) + "]"
            fill(0.9, 0.9, 0.9)
            stroke(0, 0, 0)
            stroke_weight(0)
            rect(0, 560, self.size.w, 50)
            # text('Destination:', font_name='Verdana', font_size=12.0, x=70, y=550, alignment=5)
            # text(loc, font_name='Verdana', font_size=12.0, x=230, y=550, alignment=5)

            totalDistanceTraveled = 0
            for i in range(len(self.locations) - 1):
                firstPosX, firstPosY = self.locations[i]
                nextPosX, nextPosY = self.locations[i + 1]
                dist = ((nextPosX - firstPosX)**2 +
                        (nextPosY - firstPosY)**2)**0.5
                totalDistanceTraveled += dist

            text('Distance Traveled:',
                 font_name='Verdana',
                 font_size=12.0,
                 x=70,
                 y=585,
                 alignment=5)
            text(str(totalDistanceTraveled),
                 font_name='Verdana',
                 font_size=12.0,
                 x=230,
                 y=585,
                 alignment=5)

            textPosX = self.size.x / 2
            textPosY = self.size.y / 2

            if (len(self.locationsLeft) > 1):
                currPosX, currPosY = self.locations[-1]
                nextPosX, nextPosY = self.locations[-2]

                #differenceX = nextPosX - currPosX
                #differenceY = nextPosY - currPosY

                differenceX = 1
                differenceY = 1

                directionAngle = math.atan(differenceY / differenceX)
                degreeAngle = math.degrees(directionAngle)

                #point in right direction
                if (currPosX <= nextPosX and currPosY <= nextPosY):
                    text(str(round(degreeAngle, 4)),
                         font_name='Verdana',
                         font_size=16.0,
                         x=textPosX,
                         y=textPosY,
                         alignment=5)
                elif (currPosX <= nextPosX and currPosY >= nextPosY):
                    text(str(round(degreeAngle, 4) + 180),
                         font_name='Verdana',
                         font_size=16.0,
                         x=textPosX,
                         y=textPosY,
                         alignment=5)
                elif (currPosX >= nextPosX and currPosY >= nextPosY):
                    text(str(round(degreeAngle, 4) + 180),
                         font_name='Verdana',
                         font_size=16.0,
                         x=textPosX,
                         y=textPosY,
                         alignment=5)
                elif (currPosX >= nextPosX and currPosY <= nextPosY):
                    text(str(round(degreeAngle, 4) + 360),
                         font_name='Verdana',
                         font_size=16.0,
                         x=textPosX,
                         y=textPosY,
                         alignment=5)

                #tracing back steps
                current = location.get_location()
                latLong = (round(current['latitude'],
                                 4), round(current['longitude'], 4)
                           )  #solved the close points problem using rounding
                if latLong in self.locationsLeft:
                    loc = self.locationsLeft.index(latLong)
                    # self.locationsLeft.remove(latLong)
                    self.locationsLeft = self.locationsLeft[0:loc]
            else:
                text("Welcome Back.",
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY,
                     alignment=5)
Exemple #28
0
    def draw(self):
        time.sleep(0.1)  #time between each redraw
        self.timerCount += 1  #add to the timer to animate and do things

        #Title Text
        tint(self.theme["titleColor"])
        text('Thread 1.0',
             font_name='Courier',
             font_size=16.0,
             x=self.centerX2,
             y=self.centerY2 + self.scale + 400,
             alignment=5)

        #Locations recorded count
        tint(self.theme["locationCount"])
        locationCountText = "Locations: " + str(len(self.locations))
        text(locationCountText,
             font_name='Verdana',
             font_size=10,
             x=50,
             y=self.centerY2 + self.scale + 400,
             alignment=5)

        if self.checkedOnce == 4:
            locationLeftText = "Left: " + str(len(self.locationsLeft))
            text(locationLeftText,
                 font_name='Verdana',
                 font_size=10,
                 x=self.size.x - 60,
                 y=self.centerY2 + self.scale + 400,
                 alignment=5)

        #motion
        gravX, gravY, gravZ = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        pitch = gravity_vectors[0]
        roll = gravity_vectors[1]
        yaw = gravity_vectors[2]

        #convert yaw to degrees
        yaw = -yaw * 180 / math.pi
        pitch = -pitch * 180 / math.pi
        roll = -roll * 180 / math.pi

        ############# redraw screen ############
        #Reset Button
        fill(self.theme["buttonFill"])
        stroke_weight(0)
        ellipse(self.centerX2 - self.scale * 3 - self.scale,
                self.centerY2 - self.scale - 130, self.scale * 2,
                self.scale * 2)
        tint(self.theme["buttonText"])
        text('Reset',
             font_name='Verdana',
             font_size=12.0,
             x=self.centerX2 - 112,
             y=self.centerY2 + self.scale - 167,
             alignment=5)

        #LOCATION GET
        current = location.get_location()
        latLong = (round(current['latitude'],
                         4), round(current['longitude'], 4)
                   )  #solved the close points problem using rounding
        picTaken = latLong in self.photoLocations

        #orientation calculation
        yawSin = math.sin(math.radians(yaw))
        yawCos = math.cos(math.radians(yaw))
        rollSin = math.sin(math.radians(roll))
        rollCos = math.cos(math.radians(roll))
        pitchSin = math.sin(math.radians(pitch))
        pitchCos = math.cos(math.radians(pitch))

        #compass draw
        tint(self.theme["compassTint"])
        stroke(self.theme["compassStroke"])
        stroke_weight(0)
        fill(self.theme["compassFill1"])
        ellipse(self.centerX2 + self.scale * 3 - self.scale,
                self.centerY2 - self.scale - 130, self.scale * 2,
                self.scale * 2)
        stroke_weight(0)
        ellipse(self.centerX2 + self.scale * 3 - self.scale + 3,
                self.centerY2 - self.scale - 127, self.scale * 1.85,
                self.scale * 1.85)
        ellipse(self.centerX2 + self.scale * 3 - self.scale + 6,
                self.centerY2 - self.scale - 124, self.scale * 1.7,
                self.scale * 1.7)

        stroke_weight(2)
        stroke(self.theme["compassStrokeSouth"])
        line(self.centerX2 - yawCos * self.scale + self.scale * 3,
             self.centerY2 - yawSin * self.scale - 130,
             self.centerX2 + yawCos * self.scale + self.scale * 3,
             self.centerY2 + yawSin * self.scale - 130)
        stroke(self.theme["compassStroke2"])
        line(300, 72.5, self.centerX2 + yawCos * self.scale + self.scale * 3,
             self.centerY2 + yawSin * self.scale - 130)

        #circle on top of compass
        stroke_weight(0)
        stroke(self.theme["compassStroke"])
        fill(self.theme["compassTopFill"])
        ellipse(self.centerX2 + self.scale * 3 - self.scale + 9.5,
                self.centerY2 - self.scale - 120.5, self.scale * 1.5,
                self.scale * 1.5)

        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''
            return directionSym

        #compass text
        tint(self.theme["buttonText"])
        if self.compassStat == False:
            text(directionText(yaw),
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + self.scale * 3,
                 y=self.centerY2 + self.scale - 167,
                 alignment=5)
        else:
            tempYaw = yaw + 180 + 90
            if tempYaw > 360:
                tempYaw = tempYaw % 360
            yawString = str(int(round(tempYaw, 0))) + chr(186)
            text(yawString,
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + self.scale * 3,
                 y=self.centerY2 + self.scale - 167,
                 alignment=5)

        #Center Screen Text
        tint(self.theme["buttonText"])
        if self.measuringOn == False and self.checkedOnce == 0:
            text('Tap to Start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale + 150,
                 alignment=5)
        elif self.measuringOn == True and self.checkedOnce == 1:
            if self.timerCount // 10 % 3 == 0:
                text('Measuring.',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.scale + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 1:
                text('Measuring..',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.scale + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 2:
                text('Measuring...',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.scale + 150,
                     alignment=5)
            text('Tap to Stop',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale + 120,
                 alignment=5)
        elif self.measuringOn == False and self.checkedOnce == 2:
            text('Calculating',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale + 150,
                 alignment=5)

        #if not enough values are recorded
        tint(self.theme["needMoreText"])
        if self.needMore == True:
            text("Need to record more locations.",
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 - 20,
                 alignment=5)

        tint(self.theme["buttonText"])
        #When measuring is on record locations and put into array
        if self.measuringOn == True:

            if picTaken == False:
                #camera mode
                stroke_weight(0)
                fill(self.theme["buttonFill"])
                tint(self.theme["buttonText"])
                ellipse(self.size.x / 2 - self.scale / 2,
                        0 - self.scale / 2 + 72, self.scale, self.scale)
                text('Snap',
                     font_name='Verdana',
                     font_size=8.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.scale - 167,
                     alignment=5)

            if latLong not in self.locations:
                self.locations += [latLong]
                if self.activator == True:
                    self.activator = False
                self.loopPromptState = 0

            elif latLong in self.locations[0:-5] and self.activator == False:
                #sound.play_effect('arcade:Laser_1')
                self.loopPrompt = True
                self.activator = True

            if self.loopPromptState == 1:
                if self.currentLoc != latLong:
                    self.currentLoc = latLong
                    self.locations += [latLong]

            elif self.loopPromptState == 2:
                loc = self.locations.index(latLong)
                self.locations = self.locations[0:loc]
                self.loopPromptState = 0

        #after measuring is off, setup array to use (states Calculating)
        elif self.checkedOnce == 2 and len(self.locations) > 3:
            self.locationsLeft = copy.deepcopy(self.locations)

            #do this to avoid tapping too early
            self.checkedOnce += 1

        #after the array set up, tap to get back to where you were
        elif self.checkedOnce == 3:
            text('Tap to return to start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale + 150,
                 alignment=5)

        #The return back..
        elif self.checkedOnce == 4:

            x, y = self.locations[-1]
            x = round(x, 10)
            y = round(y, 10)

            loc = "[" + str(x) + ", " + str(y) + "]"
            fill(self.theme["buttonFill"])
            stroke(self.theme["titleColor"])
            stroke_weight(0)
            # rect(0,560,self.size.w,50)
            # # text('Destination:', font_name='Verdana', font_size=12.0, x=70, y=550, alignment=5)
            # # text(loc, font_name='Verdana', font_size=12.0, x=230, y=550, alignment=5)

            totalDistanceTraveled = 0
            for i in range(len(self.locations) - 1):
                firstPosX, firstPosY = self.locations[i]
                nextPosX, nextPosY = self.locations[i + 1]
                dist = ((nextPosX - firstPosX)**2 +
                        (nextPosY - firstPosY)**2)**0.5
                totalDistanceTraveled += dist

            # text('Distance Traveled:', font_name='Verdana', font_size=12.0, x=70, y=585, alignment=5)
            # text(str(totalDistanceTraveled), font_name='Verdana', font_size=12.0, x=230, y=585, alignment=5)

            textPosX = self.size.x / 2
            textPosY = self.size.y / 2

            if (len(self.locationsLeft) > 1):

                if (len(self.locationsLeft) > 3):
                    currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]
                    secondPosX, secondPosY = self.locationsLeft[-3]
                    thirdPosX, thirdPosY = self.locationsLeft[-4]

                    xNextAverage = (nextPosX + secondPosX + thirdPosX) / 3
                    yNextAverage = (nextPosY + secondPosY + thirdPosY) / 3

                elif (len(self.locationsLeft) > 2):
                    currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]
                    secondPosX, secondPosY = self.locationsLeft[-3]

                    xNextAverage = (nextPosX + secondPosX) / 2
                    yNextAverage = (nextPosY + secondPosY) / 2

                elif (len(self.locationsLeft) > 1):
                    currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]

                    xNextAverage = (nextPosX) / 1
                    yNextAverage = (nextPosY) / 1

                xVal3 = math.cos(math.radians(xNextAverage)) * math.sin(
                    math.radians(currPosY - yNextAverage))
                yVal3 = math.cos(math.radians(currPosX)) * math.sin(
                    math.radians(xNextAverage)) - math.sin(
                        math.radians(currPosX)) * math.cos(
                            math.radians(xNextAverage)) * math.cos(
                                math.radians(currPosY - yNextAverage))
                radianAngle3 = (math.atan2(xVal3, yVal3))
                degreeAngle3 = math.degrees(radianAngle3)
                if degreeAngle3 < 0:
                    degreeAngle3 = 360 + degreeAngle3

                #take the average of the next 3 points to get a good direction.
                # trueDegree = (degreeAngle1 + degreeAngle2 + degreeAngle3)/3
                trueDegree = degreeAngle3

                #draw directing arrow
                tempYaw1 = -(360 - yaw + trueDegree)
                trueDegreeSin = math.sin(math.radians(tempYaw1))
                trueDegreeCos = math.cos(math.radians(tempYaw1))

                # text(str(trueDegree), font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.scale+150, alignment=5)

                #experimental point on arrow. Works well! but code is ugly. Will implement for loop later
                def pointMaker(self, roll, pitch, yaw):
                    stroke_weight(20)
                    stroke(self.theme["titleColor"])
                    line(self.centerX2 - trueDegreeCos * 100,
                         self.centerY - trueDegreeSin * 100,
                         self.centerX2 + trueDegreeCos * 100,
                         self.centerY + trueDegreeSin * 100)
                    stroke(self.theme["compassStroke2"])
                    line(self.centerX2 + trueDegreeCos * 100,
                         self.centerY + trueDegreeSin * 100, self.centerX2,
                         self.centerY)
                    stroke_weight(19)
                    line(self.centerX2 + trueDegreeCos * 102,
                         self.centerY + trueDegreeSin * 102, self.centerX2,
                         self.centerY)
                    stroke_weight(18)
                    line(self.centerX2 + trueDegreeCos * 104,
                         self.centerY + trueDegreeSin * 104, self.centerX2,
                         self.centerY)
                    stroke_weight(17)
                    line(self.centerX2 + trueDegreeCos * 106,
                         self.centerY + trueDegreeSin * 106, self.centerX2,
                         self.centerY)
                    stroke_weight(16)
                    line(self.centerX2 + trueDegreeCos * 108,
                         self.centerY + trueDegreeSin * 108, self.centerX2,
                         self.centerY)
                    stroke_weight(15)
                    line(self.centerX2 + trueDegreeCos * 110,
                         self.centerY + trueDegreeSin * 110, self.centerX2,
                         self.centerY)
                    stroke_weight(14)
                    line(self.centerX2 + trueDegreeCos * 112,
                         self.centerY + trueDegreeSin * 112, self.centerX2,
                         self.centerY)
                    stroke_weight(13)
                    line(self.centerX2 + trueDegreeCos * 114,
                         self.centerY + trueDegreeSin * 114, self.centerX2,
                         self.centerY)
                    stroke_weight(12)
                    line(self.centerX2 + trueDegreeCos * 116,
                         self.centerY + trueDegreeSin * 116, self.centerX2,
                         self.centerY)
                    stroke_weight(11)
                    line(self.centerX2 + trueDegreeCos * 118,
                         self.centerY + trueDegreeSin * 118, self.centerX2,
                         self.centerY)
                    stroke_weight(10)
                    line(self.centerX2 + trueDegreeCos * 120,
                         self.centerY + trueDegreeSin * 120, self.centerX2,
                         self.centerY)
                    stroke_weight(9)
                    line(self.centerX2 + trueDegreeCos * 122,
                         self.centerY + trueDegreeSin * 122, self.centerX2,
                         self.centerY)
                    stroke_weight(8)
                    line(self.centerX2 + trueDegreeCos * 124,
                         self.centerY + trueDegreeSin * 124, self.centerX2,
                         self.centerY)
                    stroke_weight(7)
                    line(self.centerX2 + trueDegreeCos * 126,
                         self.centerY + trueDegreeSin * 126, self.centerX2,
                         self.centerY)
                    stroke_weight(6)
                    line(self.centerX2 + trueDegreeCos * 128,
                         self.centerY + trueDegreeSin * 128, self.centerX2,
                         self.centerY)
                    stroke_weight(5)
                    line(self.centerX2 + trueDegreeCos * 130,
                         self.centerY + trueDegreeSin * 130, self.centerX2,
                         self.centerY)
                    stroke_weight(4)
                    line(self.centerX2 + trueDegreeCos * 132,
                         self.centerY + trueDegreeSin * 132, self.centerX2,
                         self.centerY)
                    stroke_weight(3)
                    line(self.centerX2 + trueDegreeCos * 134,
                         self.centerY + trueDegreeSin * 134, self.centerX2,
                         self.centerY)
                    stroke_weight(2)
                    line(self.centerX2 + trueDegreeCos * 136,
                         self.centerY + trueDegreeSin * 136, self.centerX2,
                         self.centerY)

                pointMaker(self, roll, pitch, yaw)

                stroke_weight(0)

                #tracing back steps
                if latLong in self.locationsLeft:
                    loc = self.locationsLeft.index(latLong)
                    # self.locationsLeft.remove(latLong)
                    self.locationsLeft = self.locationsLeft[0:loc]

                #images on the trip back
                if latLong in self.photoLocations:
                    photoLoc = len(self.photoLocations
                                   ) - self.photoLocations.index(latLong)
                    self.imageMode = True

                    tint(self.theme["textColor2"])
                    fill(self.theme["notificationColor"])
                    stroke(self.theme["notificationColor"])

                    rect(0, 150, self.size.w, 50)
                    text("Landmark! Tap here to see!",
                         font_name='Courier',
                         font_size=16.0,
                         x=self.centerX2,
                         y=150 + 25,
                         alignment=5)
                    self.currentImage = self.photoLibrary.assets[-photoLoc]
                    self.photoWidth = self.currentImage.pixel_width
                    self.photoHeight = self.currentImage.pixel_height

                    self.ui_image = self.currentImage.get_ui_image()

            else:
                text("Welcome Back.",
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY,
                     alignment=5)

        #SOS Button and
        fill(self.theme["needMoreText"])
        tint(self.theme["textColor2"])
        if self.MoreState == True:
            #to seperate the active buttons from the non active ones
            fill(self.theme["transparentFill"])
            rect(0, 0, self.size.x, self.size.y)
            #SOS Button
            fill(self.theme["needMoreText"])
            tint(self.theme["textColor2"])
            ellipse(self.centerX2 - 0.0 - self.scale,
                    self.centerY2 - self.scale - 130, self.scale * 2,
                    self.scale * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale - 167,
                 alignment=5)

            #MAP Button
            if len(self.locations) >= 2:
                fill(0.6, 0.6, 0.95)
                tint(1, 1, 1, 1)
                ellipse(self.centerX2 - self.scale * 3 - self.scale,
                        self.centerY2 - self.scale - 20, self.scale * 2,
                        self.scale * 2)
                text('Map View',
                     font_name='Verdana',
                     font_size=12.0,
                     x=self.centerX2 - 112,
                     y=self.centerY2 + self.scale - 57,
                     alignment=5)

                fill(247 / 255, 170 / 255, 103 / 255)
                tint(1, 1, 1, 1)
                ellipse(self.centerX2 - self.scale,
                        self.centerY2 - self.scale - 20, self.scale * 2,
                        self.scale * 2)
                text('Share',
                     font_name='Verdana',
                     font_size=12.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.scale - 57,
                     alignment=5)

                tint(0.7, 0.7, 0.7, 1)
                if self.clipState == True:
                    text('Copied to Clipboard',
                         font_name='Verdana',
                         font_size=16.0,
                         x=self.size.x / 2,
                         y=self.size.y / 2 - 65,
                         alignment=5)

            fill(118 / 255, 191 / 255, 247 / 255)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 + self.scale * 3 - self.scale,
                    self.centerY2 - self.scale - 20, self.scale * 2,
                    self.scale * 2)
            text('Trace',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2 + 112,
                 y=self.centerY2 + self.scale - 57,
                 alignment=5)

            #SOS Button
            fill(0.95, 0.6, 0.6)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 - 0.0 - self.scale,
                    self.centerY2 - self.scale - 130, self.scale * 2,
                    self.scale * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.scale - 167,
                 alignment=5)

        #More button at the bottom of screen
        elif self.MoreState == False:
            ellipse(self.size.x / 2 - self.scale / 2, 0 - self.scale / 2 - 5,
                    self.scale, self.scale)

        if self.imageModeOpen == True:
            fill(0, 0, 0, 0.5)
            rect(0, 0, self.size.x, self.size.y)
            fill(0.2, 0.2, 0.2)
            rect(0, self.halfScreenFrame, self.widthFrame, self.heightFrame)

#-----------Draw Path State---------------------------------------------------
        if self.pathState == True and len(self.locations) > 0:
            fill(0.7, 0.7, 0.7)
            rect(14, self.size.y / 2 - 55, self.size.x - 28,
                 self.size.y / 3 + 12)
            fill(1, 1, 1)
            rect(19, self.size.y / 2 - 50, self.size.x - 38,
                 self.size.y / 3 + 2)

            tint(1, 0, 0, 1)
            fill(1, 0, 0)
            stroke(1, 0, 0, 1)
            stroke_weight(2)
            iPointX, iPointY = self.locations[0]
            newLocs = [(0, 0)]
            maxDiffX = 0
            maxDiffY = 0
            xSepVals = []
            ySepVals = []

            for loc in self.locations[1:]:
                nPointX, nPointY = loc
                dataX = (iPointX - nPointX) * 100000
                dataY = (iPointY - nPointY) * 100000
                newLocs += [(dataX, dataY)]

            for loc in newLocs:
                xSepVals += [loc[0]]
                ySepVals += [loc[1]]

            maxDiffX = (max(xSepVals) - min(xSepVals))
            maxDiffY = (max(ySepVals) - min(ySepVals))

            generalMaxDiff = max(maxDiffX, maxDiffY)

            miniX = min(xSepVals)
            jumpX = 0 - miniX
            evenedLocsX = []
            for val in xSepVals:
                evenedLocsX += [val + jumpX]

            miniY = min(ySepVals)
            jumpY = 0 - miniY
            evenedLocsY = []
            for val in ySepVals:
                evenedLocsY += [val + jumpY]

            xCenter = self.size.x / 2
            yCenter = self.size.y / 2 - 50 + 2
            colorCount = len(newLocs)
            for i in range(len(newLocs) - 1):
                xLoc1, yLoc1 = evenedLocsX[i], evenedLocsY[i]
                xLoc2, yLoc2 = evenedLocsX[i + 1], evenedLocsY[i + 1]
                stroke(((255 / colorCount) * i) / 255, 0, 0)
                fill(((255 / colorCount) * i) / 255, 0, 0)
                #text(str(i),font_name='Verdana', font_size=4.0, x=(335/generalMaxDiff)*xLoc1+20, y=(222/generalMaxDiff)*yLoc1+yCenter+5, alignment=5)
                #line(xCenter + xLoc1, yCenter + yLoc1, xCenter + xLoc2, yCenter + yLoc2)
                line((335 / generalMaxDiff) * xLoc1 + 20,
                     (222 / generalMaxDiff) * yLoc1 + yCenter,
                     (335 / generalMaxDiff) * xLoc2 + 20,
                     (222 / generalMaxDiff) * yLoc2 + yCenter)
                ellipse((335 / generalMaxDiff) * xLoc1 + 20 - 2,
                        (222 / generalMaxDiff) * yLoc1 + yCenter - 2, 4, 4)
            ellipse((335 / generalMaxDiff) * evenedLocsX[-1] + 20 - 2,
                    (222 / generalMaxDiff) * evenedLocsY[-1] + yCenter - 2, 4,
                    4)


#-----------END OF PATHSTATE---------------------------------------------------

#prompt when the user goes in a loop or crosses paths with the recorded path
        if self.loopPrompt == True:
            fill(0, 0, 0)
            stroke_weight(0)
            #prompt for the loop
            rect(50, self.size.y / 2 - 50, self.size.x - 100,
                 self.size.y / 6 + 40)
            tint(1, 1, 1, 1)
            text("Stop Moving!",
                 font_name='Verdana',
                 font_size=18.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 + 65,
                 alignment=5)
            text("Looks like you\'ve made a loop",
                 font_name='Verdana',
                 font_size=13.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 + 38,
                 alignment=5)
            stroke(1, 1, 1, 1)
            stroke_weight(3)
            line(50, self.size.y / 2, self.size.x - 50, self.size.y / 2)
            line(self.size.x / 2, self.size.y / 2 - 50, self.size.x / 2,
                 self.size.y / 2)
            text("Continue",
                 font_name='Verdana',
                 font_size=15.0,
                 x=self.size.x / 2 - 70,
                 y=self.size.y / 2 - 25,
                 alignment=5)
            text("Break",
                 font_name='Verdana',
                 font_size=15.0,
                 x=self.size.x / 2 + 70,
                 y=self.size.y / 2 - 25,
                 alignment=5)
Exemple #29
0
    def draw(self):
        #Center scaling
        self.centerX = self.size.w/2
        self.centerY = self.size.h/2
        self.centerX2 = self.size.w/2
        self.centerY2 = self.size.h * (1/2)-scale*3.5

        time.sleep(0.1) #time between each redraw
        self.timerCount += 1 #add to the timer to animate and do things

        #Title Text
        tint(0,0,0,1)
        text('Thread 1.0', font_name='Courier', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+400, alignment=5)

        #Locations recorded count
        tint(0.4,0.4,0.4,1)

        locationCountText = "Locations: " + str(len(self.locations))
        text(locationCountText, font_name='Verdana', font_size=10, x=50, y=self.centerY2+self.radius+400, alignment=5)

        if self.checkedOnce == 4:
            locationLeftText = "Left: " + str(len(self.locationsLeft))
            text(locationLeftText, font_name='Verdana', font_size=10, x=self.size.x-60, y=self.centerY2+self.radius+400, alignment=5)


        #motion
        gravX,gravY,gravZ= motion.get_gravity()
        gravity_vectors=motion.get_attitude()
        pitch = gravity_vectors[0]
        roll = gravity_vectors[1]
        yaw = gravity_vectors[2]

        #convert yaw to degrees
        yaw = -yaw*180/math.pi
        pitch = -pitch*180/math.pi

        ############# redraw screen ############
        #Reset Button
        fill(0.9,0.9,0.9)
        stroke_weight(0)
        ellipse(self.centerX2-scale*3-self.radius,self.centerY2-self.radius-130,self.radius*2,self.radius*2)
        tint(0.4,0.4,0.4,1)
        text('Reset', font_name='Verdana', font_size=12.0, x=self.centerX2-112, y=self.centerY2+self.radius-167, alignment=5)




        #compass draw
        tint(0.4,0.4,0.4,1)
        stroke(0.4,0.4,0.4)
        stroke_weight(0)
        fill(0.9,0.9,0.9)
        ellipse(self.centerX2+scale*3-self.radius,self.centerY2-self.radius-130,self.radius*2,self.radius*2)
        stroke_weight(0)
        ellipse(self.centerX2+scale*3-self.radius+3,self.centerY2-self.radius-127,self.radius*1.85,self.radius*1.85)
        ellipse(self.centerX2+scale*3-self.radius+6,self.centerY2-self.radius-124,self.radius*1.7,self.radius*1.7)
        yawSin = math.sin(math.radians(yaw))
        yawCos = math.cos(math.radians(yaw))
        stroke(0.4,0.4,0.4)
        stroke_weight(2)






        line(self.centerX2-yawCos*self.radius+scale*3,self.centerY2-yawSin*self.radius-130,self.centerX2+yawCos*self.radius+scale*3,self.centerY2+yawSin*self.radius-130)
        stroke(1,0.3,0.3)
        line(300,72.5,self.centerX2+yawCos*self.radius+scale*3,self.centerY2+yawSin*self.radius-130)
        #circle on top of compass
        stroke_weight(0)
        stroke(0.4,0.4,0.4)
        fill(1,1,1)
        ellipse(self.centerX2+scale*3-self.radius+9.5,self.centerY2-self.radius-120.5,self.radius*1.5,self.radius*1.5)
        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''
            return directionSym

        #compass text
        tint(0.4,0.4,0.4,1)
        if self.compassStat == False:
            text(directionText(yaw), font_name='Verdana', font_size=10.0, x=self.centerX2+scale*3, y=self.centerY2+self.radius-167, alignment=5)
        else:
            tempYaw = yaw+180+90
            if tempYaw > 360:
                tempYaw = tempYaw % 360
            yawString = str(int(round(tempYaw,0))) + chr(186)
            text(yawString, font_name='Verdana', font_size=10.0, x=self.centerX2+scale*3, y=self.centerY2+self.radius-167, alignment=5)


        #Center Screen Text
        tint(0.4,0.4,0.4,1)
        if self.measuringOn == False and self.checkedOnce == 0:
            text('Tap to Start', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)
        elif self.measuringOn == True and self.checkedOnce == 1:
            if self.timerCount//10 % 3 == 0:
                text('Measuring.', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)
            elif self.timerCount//10 % 3 == 1:
                text('Measuring..', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)
            elif self.timerCount//10 % 3 == 2:
                text('Measuring...', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)
            text('Tap to Stop', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+120, alignment=5)
        elif self.measuringOn == False and self.checkedOnce == 2:
            text('Calculating', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)


        #if not enough values are recorded
        tint(0.95,0.6,0.6,1)
        if self.needMore == True:
            text("Need to record more locations.", font_name='Verdana', font_size=16.0, x=self.size.x/2, y=self.size.y/2-20, alignment=5)


        tint(0.4,0.4,0.4,1)
        #When measuring is on record locations and put into array
        if self.measuringOn == True:
            current = location.get_location()
            latLong = (round(current['latitude'],4), round(current['longitude'],4)) #solved the close points problem using rounding
            if latLong not in self.locations:
                self.locations += [latLong]
                if self.activator == True:
                    self.activator = False
                self.loopPromptState = 0

            elif latLong in self.locations[0:-5] and self.activator == False:
                #sound.play_effect('arcade:Laser_1')
                self.loopPrompt = True
                self.activator = True
            if self.loopPromptState == 1:
                if self.currentLoc != latLong:
                    self.currentLoc = latLong
                    self.locations += [latLong]
            elif self.loopPromptState == 2:
                loc = self.locations.index(latLong)
                self.locations = self.locations[0:loc]
                self.loopPromptState = 0
#---------------------------------------------------------
#---------------------------------------------------------
#-----Loop Prompt Works? Test it.----------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------
#---------------------------------------------------------


        #after measuring is off, setup array to use (states Calculating)
        elif self.checkedOnce == 2 and len(self.locations) > 0:
            self.locationsLeft = copy.deepcopy(self.locations)

            #do this to avoid tapping too early
            self.checkedOnce += 1

        #after the array set up, tap to get back to where you were
        elif self.checkedOnce == 3:
            text('Tap to return to start', font_name='Verdana', font_size=16.0, x=self.centerX2, y=self.centerY2+self.radius+150, alignment=5)

        #The return back..
        elif self.checkedOnce == 4:
            x,y = self.locations[-1]
            x = round(x,10)
            y = round(y,10)
            loc = "[" + str(x) + ", " + str(y) + "]"
            fill(0.9,0.9,0.9)
            stroke(0,0,0)
            stroke_weight(0)
            # rect(0,560,self.size.w,50)
            # # text('Destination:', font_name='Verdana', font_size=12.0, x=70, y=550, alignment=5)
            # # text(loc, font_name='Verdana', font_size=12.0, x=230, y=550, alignment=5)

            totalDistanceTraveled = 0
            for i in range(len(self.locations)-1):
                firstPosX, firstPosY = self.locations[i]
                nextPosX, nextPosY = self.locations[i+1]
                dist = ((nextPosX - firstPosX)**2 + (nextPosY - firstPosY)**2)**0.5
                totalDistanceTraveled += dist

            text('Distance Traveled:', font_name='Verdana', font_size=12.0, x=70, y=585, alignment=5)
            text(str(totalDistanceTraveled), font_name='Verdana', font_size=12.0, x=230, y=585, alignment=5)

            textPosX = self.size.x/2
            textPosY = self.size.y/2


            if (len(self.locationsLeft) > 0):
                # currPosX, currPosY = self.locationsLeft[-1]
                # nextPosX, nextPosY = self.locationsLeft[-2]
                # secondPosX, secondPosY = self.locationsLeft[-3]
                # thirdPosX, thirdPosY = self.locationsLeft[-4]

                currPosX, currPosY = [40.4465, 79.9427]
                nextPosX, nextPosY = [40.4465, 79.9428]
                secondPosX, secondPosY = [40.4466, 79.9428]
                thirdPosX, thirdPosY = [40.4466, 79.9429]


                differenceX1 = nextPosX - currPosX
                differenceY1 = nextPosY - currPosY
                differenceX2 = secondPosX - currPosX
                differenceY2 = secondPosY - currPosY
                differenceX3 = thirdPosX - currPosX
                differenceY3 = thirdPosY - currPosY
                degreeAngle1 = 0
                degreeAngle2 = 0
                degreeAngle3 = 0

                if differenceX1 == 0:
                    if currPosX > nextPosX:
                        degreeAngle1 = 180.0
                    else:
                        degreeAngle1 = 0.0
                else:
                    directionAngle1 = math.atan(differenceY1/differenceX1)
                    degreeAngle1 = math.degrees(directionAngle1)

                #point in right direction
                if (currPosX <= nextPosX and currPosY < nextPosY):
                    degreeAngle1 = 90 - round(degreeAngle1, 4)
                elif (currPosX <= nextPosX and currPosY >= nextPosY):
                    degreeAngle1 = 90 - round(degreeAngle1, 4)
                elif (currPosX >= nextPosX and currPosY >= nextPosY):
                    degreeAngle1 = round(degreeAngle1, 4) + 180
                elif (currPosX >= nextPosX and currPosY <= nextPosY):
                    degreeAngle1 = round(degreeAngle1, 4) + 180


                if differenceX2 == 0:
                    if currPosX > secondPosX:
                        degreeAngle2 = 180.0
                    else:
                        degreeAngle2 = 0.0
                else:
                    directionAngle2 = math.atan(differenceY2/differenceX2)
                    degreeAngle2 = math.degrees(directionAngle2)

                #point in right direction
                if (currPosX <= secondPosX and currPosY <= secondPosY):
                    degreeAngle2 = round(degreeAngle2, 4)
                elif (currPosX <= secondPosX and currPosY >= secondPosY):
                    degreeAngle2 = round(degreeAngle2, 4) + 180
                elif (currPosX >= secondPosX and currPosY >= secondPosY):
                    degreeAngle2 = round(degreeAngle2, 4) + 180
                elif (currPosX >= secondPosX and currPosY <= secondPosY):
                    degreeAngle2 = round(degreeAngle2, 4) + 360

                if differenceX3 == 0:
                    if currPosX > thirdPosX:
                        degreeAngle3 = 180.0
                    else:
                        degreeAngle3 = 0.0
                else:
                    directionAngle3 = math.atan(differenceY3/differenceX3)
                    degreeAngle3 = math.degrees(directionAngle3)

                #point in right direction
                if (currPosX <= thirdPosX and currPosY <= thirdPosY):
                    degreeAngle3 = round(degreeAngle3, 4)
                elif (currPosX <= thirdPosX and currPosY >= thirdPosY):
                    degreeAngle3 = round(degreeAngle3, 4) + 180
                elif (currPosX >= thirdPosX and currPosY >= thirdPosY):
                    degreeAngle3 = round(degreeAngle3, 4) + 180
                elif (currPosX >= thirdPosX and currPosY <= thirdPosY):
                    degreeAngle3 = round(degreeAngle3, 4) + 360

                trueDegree = (degreeAngle1 + degreeAngle2 + degreeAngle3)/3
                text(str(trueDegree), font_name='Verdana', font_size=16.0, x=textPosX, y=textPosY, alignment=5)
                text(str(degreeAngle1), font_name='Verdana', font_size=16.0, x=textPosX, y=textPosY-20, alignment=5)
                text(str(degreeAngle2), font_name='Verdana', font_size=16.0, x=textPosX, y=textPosY-40, alignment=5)
                text(str(degreeAngle3), font_name='Verdana', font_size=16.0, x=textPosX, y=textPosY-60, alignment=5)


                # #tracing back steps
                # current = location.get_location()
                # latLong = (round(current['latitude'],4), round(current['longitude'],4)) #solved the close points problem using rounding
                # if latLong in self.locationsLeft:
                #     loc = self.locationsLeft.index(latLong)
                #     # self.locationsLeft.remove(latLong)
                #     self.locationsLeft = self.locationsLeft[0:loc]
            else:
                text("Welcome Back.", font_name='Verdana', font_size=16.0, x=textPosX, y=textPosY, alignment=5)


        #SOS Button and
        fill(0.95,0.6,0.6)
        tint(1,1,1,1)
        if self.MoreState == True:
            #to seperate the active buttons from the non active ones
            fill(0,0,0,0.5)
            rect(0,0,self.size.x,self.size.y)
            #SOS Button
            fill(0.95,0.6,0.6,1)
            tint(1,1,1,1)
            ellipse(self.centerX2-0.0-self.radius,self.centerY2-self.radius-130,self.radius*2,self.radius*2)
            text('SOS', font_name='Verdana', font_size=12.0, x=self.centerX2, y=self.centerY2+self.radius-167, alignment=5)

            #MAP Button
            if len(self.locations) >= 2:
                fill(0.6,0.6,0.95)
                tint(1,1,1,1)
                ellipse(self.centerX2-scale*3-self.radius,self.centerY2-self.radius-20,self.radius*2,self.radius*2)
                text('Map View', font_name='Verdana', font_size=12.0, x=self.centerX2-112, y=self.centerY2+self.radius-57, alignment=5)

            #SOS Button
            fill(0.95,0.6,0.6)
            tint(1,1,1,1)
            ellipse(self.centerX2-0.0-self.radius,self.centerY2-self.radius-130,self.radius*2,self.radius*2)
            text('SOS', font_name='Verdana', font_size=12.0, x=self.centerX2, y=self.centerY2+self.radius-167, alignment=5)

        #More button at the bottom of screen
        elif self.MoreState == False:
            ellipse(self.size.x/2-self.radius/2,0-self.radius/2-5,self.radius,self.radius)



        if self.loopPrompt == True:
            fill(0,0,0)
            #prompt for the loop
            rect(50,self.size.y/2-50,self.size.x-100,self.size.y/6+40)
            tint(1,1,1,1)
            text("Stop Moving!", font_name='Verdana', font_size=18.0, x=self.size.x/2, y=self.size.y/2+65, alignment=5)
            text("Looks like you\'ve made a loop", font_name='Verdana', font_size=13.0, x=self.size.x/2, y=self.size.y/2+38, alignment=5)
            stroke(1,1,1,1)
            stroke_weight(3)
            line(50, self.size.y/2, self.size.x-50, self.size.y/2)
            line(self.size.x/2, self.size.y/2-50, self.size.x/2, self.size.y/2)
            text("Continue", font_name='Verdana', font_size=15.0, x=self.size.x/2-70, y=self.size.y/2-25, alignment=5)
            text("Break", font_name='Verdana', font_size=15.0, x=self.size.x/2+70, y=self.size.y/2-25, alignment=5)
Exemple #30
0
    def draw(self):
        #Center scaling
        self.centerX = self.size.w / 2
        self.centerY = self.size.h / 2
        self.centerX2 = self.size.w / 2
        self.centerY2 = self.size.h * (1 / 2) - scale * 3.5

        time.sleep(0.1)  #time between each redraw
        self.timerCount += 1  #add to the timer to animate and do things

        #Title Text
        tint(0, 0, 0, 1)
        text('Thread 1.0',
             font_name='Courier',
             font_size=16.0,
             x=self.centerX2,
             y=self.centerY2 + self.radius + 400,
             alignment=5)

        #Locations recorded count
        tint(0.4, 0.4, 0.4, 1)

        locationCountText = "Locations: " + str(len(self.locations))
        text(locationCountText,
             font_name='Verdana',
             font_size=10,
             x=50,
             y=self.centerY2 + self.radius + 400,
             alignment=5)

        if self.checkedOnce == 4:
            locationLeftText = "Left: " + str(len(self.locationsLeft))
            text(locationLeftText,
                 font_name='Verdana',
                 font_size=10,
                 x=self.size.x - 60,
                 y=self.centerY2 + self.radius + 400,
                 alignment=5)

        #motion
        gravX, gravY, gravZ = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        pitch = gravity_vectors[0]
        roll = gravity_vectors[1]
        yaw = gravity_vectors[2]

        #convert yaw to degrees
        yaw = -yaw * 180 / math.pi
        pitch = -pitch * 180 / math.pi
        roll = -roll * 180 / math.pi

        ############# redraw screen ############
        #Reset Button
        fill(0.9, 0.9, 0.9)
        stroke_weight(0)
        ellipse(self.centerX2 - scale * 3 - self.radius,
                self.centerY2 - self.radius - 130, self.radius * 2,
                self.radius * 2)
        tint(0.4, 0.4, 0.4, 1)
        text('Reset',
             font_name='Verdana',
             font_size=12.0,
             x=self.centerX2 - 112,
             y=self.centerY2 + self.radius - 167,
             alignment=5)

        current = location.get_location()
        latLong = (round(current['latitude'],
                         4), round(current['longitude'], 4)
                   )  #solved the close points problem using rounding
        picTaken = latLong in self.photoLocations

        if self.measuringOn == True and picTaken == False:
            #camera mode
            stroke_weight(0)
            fill(0.9, 0.9, 0.9)
            tint(0.4, 0.4, 0.4, 1)
            ellipse(self.size.x / 2 - self.radius / 2,
                    0 - self.radius / 2 + 72, self.radius, self.radius)
            text('Snap',
                 font_name='Verdana',
                 font_size=8.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

        #compass draw
        tint(0.4, 0.4, 0.4, 1)
        stroke(0.4, 0.4, 0.4)
        stroke_weight(0)
        fill(0.9, 0.9, 0.9)
        ellipse(self.centerX2 + scale * 3 - self.radius,
                self.centerY2 - self.radius - 130, self.radius * 2,
                self.radius * 2)
        stroke_weight(0)
        ellipse(self.centerX2 + scale * 3 - self.radius + 3,
                self.centerY2 - self.radius - 127, self.radius * 1.85,
                self.radius * 1.85)
        ellipse(self.centerX2 + scale * 3 - self.radius + 6,
                self.centerY2 - self.radius - 124, self.radius * 1.7,
                self.radius * 1.7)
        yawSin = math.sin(math.radians(yaw))
        yawCos = math.cos(math.radians(yaw))
        rollSin = math.sin(math.radians(roll))
        rollCos = math.cos(math.radians(roll))
        pitchSin = math.sin(math.radians(pitch))
        pitchCos = math.cos(math.radians(pitch))
        stroke(0.4, 0.4, 0.4)
        stroke_weight(2)

        #line(self.centerX2-yawCos*self.radius+scale*3,self.centerY2-yawSin*self.radius-50,self.centerX2+yawCos*self.radius+scale*3,self.centerY2+yawSin*self.radius-50)
        #line(self.centerX2-pitchCos*self.radius-0,self.centerY2-pitchSin*self.radius,self.centerX2+pitchCos*self.radius-0,self.centerY2+pitchSin*self.radius)
        #line(self.centerX2-rollCos*self.radius-scale*3,self.centerY2-rollSin*self.radius,self.centerX2+rollCos*self.radius-scale*3,self.centerY2+rollSin*self.radius)

        stroke(0.4, 0.4, 0.4)
        line(self.centerX2 - yawCos * self.radius + scale * 3,
             self.centerY2 - yawSin * self.radius - 130,
             self.centerX2 + yawCos * self.radius + scale * 3,
             self.centerY2 + yawSin * self.radius - 130)
        stroke(1, 0.3, 0.3)
        line(300, 72.5, self.centerX2 + yawCos * self.radius + scale * 3,
             self.centerY2 + yawSin * self.radius - 130)

        #circle on top of compass
        stroke_weight(0)
        stroke(0.4, 0.4, 0.4)
        fill(1, 1, 1)
        ellipse(self.centerX2 + scale * 3 - self.radius + 9.5,
                self.centerY2 - self.radius - 120.5, self.radius * 1.5,
                self.radius * 1.5)

        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''
            return directionSym

        #compass text
        tint(0.4, 0.4, 0.4, 1)
        if self.compassStat == False:
            text(directionText(yaw),
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + scale * 3,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)
        else:
            tempYaw = yaw + 180 + 90
            if tempYaw > 360:
                tempYaw = tempYaw % 360
            yawString = str(int(round(tempYaw, 0))) + chr(186)
            text(yawString,
                 font_name='Verdana',
                 font_size=10.0,
                 x=self.centerX2 + scale * 3,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

        #Center Screen Text
        tint(0.4, 0.4, 0.4, 1)
        if self.measuringOn == False and self.checkedOnce == 0:
            text('Tap to Start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)
        elif self.measuringOn == True and self.checkedOnce == 1:
            if self.timerCount // 10 % 3 == 0:
                text('Measuring.',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 1:
                text('Measuring..',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            elif self.timerCount // 10 % 3 == 2:
                text('Measuring...',
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
            text('Tap to Stop',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 120,
                 alignment=5)
        elif self.measuringOn == False and self.checkedOnce == 2:
            text('Calculating',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)

        #if not enough values are recorded
        tint(0.95, 0.6, 0.6, 1)
        if self.needMore == True:
            text("Need to record more locations.",
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 - 20,
                 alignment=5)

        tint(0.4, 0.4, 0.4, 1)
        #When measuring is on record locations and put into array
        if self.measuringOn == True:
            current = location.get_location()
            latLong = (round(current['latitude'],
                             4), round(current['longitude'], 4)
                       )  #solved the close points problem using rounding
            if latLong not in self.locations:
                self.locations += [latLong]
                if self.activator == True:
                    self.activator = False
                self.loopPromptState = 0

            elif latLong in self.locations[0:-5] and self.activator == False:
                #sound.play_effect('arcade:Laser_1')
                self.loopPrompt = True
                self.activator = True

            if self.loopPromptState == 1:
                if self.currentLoc != latLong:
                    self.currentLoc = latLong
                    self.locations += [latLong]

            elif self.loopPromptState == 2:
                loc = self.locations.index(latLong)
                self.locations = self.locations[0:loc]
                self.loopPromptState = 0

        #after measuring is off, setup array to use (states Calculating)
        elif self.checkedOnce == 2 and len(self.locations) > 3:
            self.locationsLeft = copy.deepcopy(self.locations)

            #do this to avoid tapping too early
            self.checkedOnce += 1

        #after the array set up, tap to get back to where you were
        elif self.checkedOnce == 3:
            text('Tap to return to start',
                 font_name='Verdana',
                 font_size=16.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius + 150,
                 alignment=5)

        #The return back..
        elif self.checkedOnce == 4:

            x, y = self.locations[-1]
            x = round(x, 10)
            y = round(y, 10)

            loc = "[" + str(x) + ", " + str(y) + "]"
            fill(0.9, 0.9, 0.9)
            stroke(0, 0, 0)
            stroke_weight(0)
            # rect(0,560,self.size.w,50)
            # # text('Destination:', font_name='Verdana', font_size=12.0, x=70, y=550, alignment=5)
            # # text(loc, font_name='Verdana', font_size=12.0, x=230, y=550, alignment=5)

            totalDistanceTraveled = 0
            for i in range(len(self.locations) - 1):
                firstPosX, firstPosY = self.locations[i]
                nextPosX, nextPosY = self.locations[i + 1]
                dist = ((nextPosX - firstPosX)**2 +
                        (nextPosY - firstPosY)**2)**0.5
                totalDistanceTraveled += dist

            text('Distance Traveled:',
                 font_name='Verdana',
                 font_size=12.0,
                 x=70,
                 y=585,
                 alignment=5)
            text(str(totalDistanceTraveled),
                 font_name='Verdana',
                 font_size=12.0,
                 x=230,
                 y=585,
                 alignment=5)

            textPosX = self.size.x / 2
            textPosY = self.size.y / 2

            if (len(self.locationsLeft) > 3):
                currPosX, currPosY = self.locationsLeft[-1]
                nextPosX, nextPosY = self.locationsLeft[-2]
                secondPosX, secondPosY = self.locationsLeft[-3]
                thirdPosX, thirdPosY = self.locationsLeft[-4]

                # currPosX, currPosY = [40.4465, 79.9427]
                # nextPosX, nextPosY = [40.4465, 79.9428]
                # secondPosX, secondPosY = [40.4466, 79.9428]
                # thirdPosX, thirdPosY = [40.4466, 79.9429]

                xVal = math.cos(math.radians(nextPosX)) * math.sin(
                    math.radians(abs(currPosY - nextPosY)))
                yVal = math.cos(math.radians(currPosX)) * math.sin(
                    math.radians(nextPosX)) - math.sin(
                        math.radians(currPosX)) * math.cos(
                            math.radians(nextPosX)) * math.cos(
                                math.radians(abs(currPosY - nextPosY)))
                radianAngle1 = (math.atan2(xVal, yVal))
                degreeAngle1 = math.degrees(radianAngle1)
                if nextPosY < currPosY:
                    degreeAngle1 = 180 + (180 - degreeAngle1)

                xVal2 = math.cos(math.radians(secondPosX)) * math.sin(
                    math.radians(abs(currPosY - secondPosY)))
                yVal2 = math.cos(math.radians(currPosX)) * math.sin(
                    math.radians(secondPosX)) - math.sin(
                        math.radians(currPosX)) * math.cos(
                            math.radians(secondPosX)) * math.cos(
                                math.radians(abs(currPosY - secondPosY)))
                radianAngle2 = (math.atan2(xVal2, yVal2))
                degreeAngle2 = math.degrees(radianAngle2)
                if secondPosY < currPosY:
                    degreeAngle2 = 180 + (180 - degreeAngle2)

                xVal3 = math.cos(math.radians(thirdPosX)) * math.sin(
                    math.radians(abs(currPosY - thirdPosY)))
                yVal3 = math.cos(math.radians(currPosX)) * math.sin(
                    math.radians(thirdPosX)) - math.sin(
                        math.radians(currPosX)) * math.cos(
                            math.radians(thirdPosX)) * math.cos(
                                math.radians(abs(currPosY - thirdPosY)))
                radianAngle3 = (math.atan2(xVal3, yVal3))
                degreeAngle3 = math.degrees(radianAngle3)
                if thirdPosY < currPosY:
                    degreeAngle3 = 180 + (180 - degreeAngle3)

                #take the average of the next 3 points to get a good direction.
                trueDegree = (degreeAngle1 + degreeAngle2 + degreeAngle3) / 3

                #draw directing arrow
                tempYaw1 = -(360 - yaw + trueDegree)
                trueDegreeSin = math.sin(math.radians(tempYaw1))
                trueDegreeCos = math.cos(math.radians(tempYaw1))

                text(str(trueDegree),
                     font_name='Verdana',
                     font_size=16.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius + 150,
                     alignment=5)
                text(str(degreeAngle1),
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY - 20,
                     alignment=5)
                text(str(degreeAngle2),
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY - 40,
                     alignment=5)
                text(str(degreeAngle3),
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY - 60,
                     alignment=5)

                #experimental point on arrow. Works well! but code is ugly. Will implement for loop later
                def pointMaker(self, roll, pitch, yaw):
                    stroke_weight(20)
                    line(self.centerX2 - trueDegreeCos * 100 - pitch * 3,
                         self.centerY - trueDegreeSin * 100 + roll * 3,
                         self.centerX2 + trueDegreeCos * 100 - pitch * 3,
                         self.centerY + trueDegreeSin * 100 + roll * 3)
                    stroke(1, 0.3, 0.3)
                    line(self.centerX2 + trueDegreeCos * 100 - pitch * 3,
                         self.centerY + trueDegreeSin * 100 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(19)
                    line(self.centerX2 + trueDegreeCos * 102 - pitch * 3,
                         self.centerY + trueDegreeSin * 102 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(18)
                    line(self.centerX2 + trueDegreeCos * 104 - pitch * 3,
                         self.centerY + trueDegreeSin * 104 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(17)
                    line(self.centerX2 + trueDegreeCos * 106 - pitch * 3,
                         self.centerY + trueDegreeSin * 106 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(16)
                    line(self.centerX2 + trueDegreeCos * 108 - pitch * 3,
                         self.centerY + trueDegreeSin * 108 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(15)
                    line(self.centerX2 + trueDegreeCos * 110 - pitch * 3,
                         self.centerY + trueDegreeSin * 110 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(14)
                    line(self.centerX2 + trueDegreeCos * 112 - pitch * 3,
                         self.centerY + trueDegreeSin * 112 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(13)
                    line(self.centerX2 + trueDegreeCos * 114 - pitch * 3,
                         self.centerY + trueDegreeSin * 114 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(12)
                    line(self.centerX2 + trueDegreeCos * 116 - pitch * 3,
                         self.centerY + trueDegreeSin * 116 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(11)
                    line(self.centerX2 + trueDegreeCos * 118 - pitch * 3,
                         self.centerY + trueDegreeSin * 118 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(10)
                    line(self.centerX2 + trueDegreeCos * 120 - pitch * 3,
                         self.centerY + trueDegreeSin * 120 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(9)
                    line(self.centerX2 + trueDegreeCos * 122 - pitch * 3,
                         self.centerY + trueDegreeSin * 122 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(8)
                    line(self.centerX2 + trueDegreeCos * 124 - pitch * 3,
                         self.centerY + trueDegreeSin * 124 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(7)
                    line(self.centerX2 + trueDegreeCos * 126 - pitch * 3,
                         self.centerY + trueDegreeSin * 126 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(6)
                    line(self.centerX2 + trueDegreeCos * 128 - pitch * 3,
                         self.centerY + trueDegreeSin * 128 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(5)
                    line(self.centerX2 + trueDegreeCos * 130 - pitch * 3,
                         self.centerY + trueDegreeSin * 130 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(4)
                    line(self.centerX2 + trueDegreeCos * 132 - pitch * 3,
                         self.centerY + trueDegreeSin * 132 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(3)
                    line(self.centerX2 + trueDegreeCos * 134 - pitch * 3,
                         self.centerY + trueDegreeSin * 134 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)
                    stroke_weight(2)
                    line(self.centerX2 + trueDegreeCos * 136 - pitch * 3,
                         self.centerY + trueDegreeSin * 136 + roll * 3,
                         self.centerX2 - pitch * 3, self.centerY + roll * 3)

                pointMaker(self, roll, pitch, yaw)

                stroke_weight(0)

                #tracing back steps
                current = location.get_location()
                latLong = (round(current['latitude'],
                                 4), round(current['longitude'], 4)
                           )  #solved the close points problem using rounding
                if latLong in self.locationsLeft:
                    loc = self.locationsLeft.index(latLong)
                    # self.locationsLeft.remove(latLong)
                    self.locationsLeft = self.locationsLeft[0:loc]

                #images on the trip back
                if latLong in self.photoLocations:
                    loc = self.photoLocations.index(latLong)
                    self.imageMode = True

                tint(1, 1, 1)
                fill(0, 0, 0)
                stroke(0, 0, 0)
                if self.imageMode == True:
                    rect(0, 150, self.size.w, 50)
                    text("Landmark! Tap here to see!",
                         font_name='Courier',
                         font_size=16.0,
                         x=self.centerX2,
                         y=150 + 25,
                         alignment=5)
                    self.layer.image = "Dog_Face"
                    self.add_layer(self.layer)
                    self.root_layer.update(self.dt)
                    self.root_layer.draw()

            else:
                text("Welcome Back.",
                     font_name='Verdana',
                     font_size=16.0,
                     x=textPosX,
                     y=textPosY,
                     alignment=5)

        #SOS Button and
        fill(0.95, 0.6, 0.6)
        tint(1, 1, 1, 1)
        if self.MoreState == True:
            #to seperate the active buttons from the non active ones
            fill(0, 0, 0, 0.5)
            rect(0, 0, self.size.x, self.size.y)
            #SOS Button
            fill(0.95, 0.6, 0.6, 1)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 - 0.0 - self.radius,
                    self.centerY2 - self.radius - 130, self.radius * 2,
                    self.radius * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

            #MAP Button
            if len(self.locations) >= 2:
                fill(0.6, 0.6, 0.95)
                tint(1, 1, 1, 1)
                ellipse(self.centerX2 - scale * 3 - self.radius,
                        self.centerY2 - self.radius - 20, self.radius * 2,
                        self.radius * 2)
                text('Map View',
                     font_name='Verdana',
                     font_size=12.0,
                     x=self.centerX2 - 112,
                     y=self.centerY2 + self.radius - 57,
                     alignment=5)

                fill(247 / 255, 170 / 255, 103 / 255)
                tint(1, 1, 1, 1)
                ellipse(self.centerX2 - self.radius,
                        self.centerY2 - self.radius - 20, self.radius * 2,
                        self.radius * 2)
                text('Share',
                     font_name='Verdana',
                     font_size=12.0,
                     x=self.centerX2,
                     y=self.centerY2 + self.radius - 57,
                     alignment=5)

                tint(0.7, 0.7, 0.7, 1)
                if self.clipState == True:
                    text('Copied to Clipboard',
                         font_name='Verdana',
                         font_size=16.0,
                         x=self.size.x / 2,
                         y=self.size.y / 2 - 65,
                         alignment=5)

            fill(118 / 255, 191 / 255, 247 / 255)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 + scale * 3 - self.radius,
                    self.centerY2 - self.radius - 20, self.radius * 2,
                    self.radius * 2)
            text('Trace',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2 + 112,
                 y=self.centerY2 + self.radius - 57,
                 alignment=5)

            #SOS Button
            fill(0.95, 0.6, 0.6)
            tint(1, 1, 1, 1)
            ellipse(self.centerX2 - 0.0 - self.radius,
                    self.centerY2 - self.radius - 130, self.radius * 2,
                    self.radius * 2)
            text('SOS',
                 font_name='Verdana',
                 font_size=12.0,
                 x=self.centerX2,
                 y=self.centerY2 + self.radius - 167,
                 alignment=5)

        #More button at the bottom of screen
        elif self.MoreState == False:
            ellipse(self.size.x / 2 - self.radius / 2, 0 - self.radius / 2 - 5,
                    self.radius, self.radius)

#-----------PATHSTATE---------------------------------------------------
        if self.pathState == True and len(self.locations) > 0:
            fill(0.7, 0.7, 0.7)
            rect(20, self.size.y / 2 - 50, self.size.x - 40, self.size.y / 3)
            fill(1, 1, 1)
            rect(25, self.size.y / 2 - 45, self.size.x - 50,
                 self.size.y / 3 - 10)

            tint(1, 0, 0, 1)
            fill(1, 0, 0)
            stroke(1, 0, 0, 1)
            stroke_weight(2)
            #line(self.size.x/2, self.size.y/2, self.size.x/2 + 10, self.size.y/2 + 10)
            iPointX, iPointY = self.locations[0]
            newLocs = [(0, 0)]
            maxDiffX = 0
            maxDiffY = 0
            xSepVals = []
            ySepVals = []

            # for loc in self.locations:
            #     xSepVals += [loc[0]]
            #     ySepVals += [loc[1]]
            #
            # maxDiffX = (max(xSepVals) - min(xSepVals))* 100000
            # maxDiffY = (max(ySepVals) - min(ySepVals))* 100000

            for loc in self.locations[1:]:
                nPointX, nPointY = loc
                dataX = (iPointX - nPointX) * 100000
                dataY = (iPointY - nPointY) * 100000
                newLocs += [(dataX, dataY)]

            for loc in newLocs:
                xSepVals += [loc[0]]
                ySepVals += [loc[1]]

            maxDiffX = (max(xSepVals) - min(xSepVals))
            maxDiffY = (max(ySepVals) - min(ySepVals))

            generalMaxDiff = max(maxDiffX, maxDiffY)

            miniX = min(xSepVals)
            miniY = min(ySepVals)
            generalMini = min(miniX, miniY)
            jumpG = 0 - generalMini
            evenedLocsX = []
            evenedLocsY = []
            for i in range(len(xSepVals)):
                evenedLocsX += [xSepVals[i] + jumpG]
                evenedLocsY += [ySepVals[i] + jumpG]

            xCenter = self.size.x / 2
            yCenter = self.size.y / 2 - 50 + 2
            colorCount = len(newLocs)
            for i in range(len(newLocs) - 1):
                xLoc1, yLoc1 = evenedLocsX[i], evenedLocsY[i]
                xLoc2, yLoc2 = evenedLocsX[i + 1], evenedLocsY[i + 1]
                stroke(((255 / colorCount) * i) / 255, 0, 0)
                fill((255 - (i * 10)) / 255, 0, 0)
                text(str(i),
                     font_name='Verdana',
                     font_size=4.0,
                     x=xCenter + xLoc1,
                     y=yCenter + yLoc1 + 20,
                     alignment=5)
                line((335 / generalMaxDiff) * xLoc1 + 20,
                     (222 / generalMaxDiff) * yLoc1 + yCenter,
                     (335 / generalMaxDiff) * xLoc2 + 20,
                     (222 / generalMaxDiff) * yLoc2 + yCenter)
                ellipse(xCenter - 2 + xLoc1, yCenter - 2 + yLoc1, 4, 4)


#-----------END OF PATHSTATE---------------------------------------------------

        if self.loopPrompt == True:
            fill(0, 0, 0)
            stroke_weight(0)
            #prompt for the loop
            rect(50, self.size.y / 2 - 50, self.size.x - 100,
                 self.size.y / 6 + 40)
            tint(1, 1, 1, 1)
            text("Stop Moving!",
                 font_name='Verdana',
                 font_size=18.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 + 65,
                 alignment=5)
            text("Looks like you\'ve made a loop",
                 font_name='Verdana',
                 font_size=13.0,
                 x=self.size.x / 2,
                 y=self.size.y / 2 + 38,
                 alignment=5)
            stroke(1, 1, 1, 1)
            stroke_weight(3)
            line(50, self.size.y / 2, self.size.x - 50, self.size.y / 2)
            line(self.size.x / 2, self.size.y / 2 - 50, self.size.x / 2,
                 self.size.y / 2)
            text("Continue",
                 font_name='Verdana',
                 font_size=15.0,
                 x=self.size.x / 2 - 70,
                 y=self.size.y / 2 - 25,
                 alignment=5)
            text("Break",
                 font_name='Verdana',
                 font_size=15.0,
                 x=self.size.x / 2 + 70,
                 y=self.size.y / 2 - 25,
                 alignment=5)
Exemple #31
0
	def update(self):
		motion.start_updates()
		px = motion.get_gravity()
		gx = px[0] * 50
		self.x = min(max(self.x + gx, 20), self.scene.size.w - 20)
Exemple #32
0
	def update(self):
		motion.start_updates()
		gx = motion.get_gravity()[0] * 50
		self.x = min(max(self.x + gx, 20),
		self.scene.size.w - (20 + image_width))
    def draw(self):
        #set the background color to the theme's background color
        self.background_color = self.theme["backgroundColor"]

        time.sleep(0.05)  #time between each redraw
        self.timerCount += 1  #add to the timer to animate and do things

        #Title Text
        tint(self.theme["titleColor"])
        # text('Thread', font_name='Courier', font_size=16.0, x=self.centerX2, y=self.centerY2+self.scale+400, alignment=5)

        #Locations recorded count
        tint(self.theme["locationCount"])
        locationCountText = "Locations: " + str(len(self.locations))
        text(locationCountText, font_name='Verdana', font_size=10, x=50, \
        y=self.centerY2+self.scale+400, alignment=5)

        #print the amount of locations left on the journey
        def remainingText(self):
            locationLeftText = "Remaining: " + str(len(self.locationsLeft))
            text(locationLeftText, font_name='Verdana', font_size=10, \
            x=self.size.x-60, y=self.centerY2+self.scale+400, alignment=5)

        if self.functionState == 4:
            remainingText(self)

        #motion vectors
        gravX, gravY, gravZ = motion.get_gravity()
        gravityVectors = motion.get_attitude()
        pitch = gravityVectors[0]
        roll = gravityVectors[1]
        yaw = gravityVectors[2]

        #convert yaw to degrees
        yaw = -yaw * 180 / math.pi
        pitch = -pitch * 180 / math.pi
        roll = -roll * 180 / math.pi

        #draw Reset Button
        def makeResetButton(self):
            fill(self.theme["buttonFill"])
            stroke_weight(0)
            ellipse(self.centerX2-self.scale*3-self.scale,\
            self.centerY2-self.scale-130,self.scale*2,self.scale*2)
            tint(self.theme["buttonText"])
            text('Reset', font_name='Verdana', font_size=12.0, \
            x=self.centerX2-112, y=self.centerY2+self.scale-167, alignment=5)

        #LOCATION GET
        current = location.get_location()
        #latLong will be the location used through every redraw
        latLong = (round(current['latitude'],
                         4), round(current['longitude'], 4))
        #solved the close points problem using rounding
        picTaken = latLong in self.photoLocations

        #orientation calculation
        yawSin = math.sin(math.radians(yaw))
        yawCos = math.cos(math.radians(yaw))

        #derive the direction (NSEW) from the degrees
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''
            return directionSym

        #draw the compass on the bottom right and make it work
        def drawCompass(self):
            #compass draw
            tint(self.theme["compassTint"])
            stroke(self.theme["compassStroke"])
            stroke_weight(0)
            fill(self.theme["compassFill1"])
            ellipse(self.centerX2+self.scale*3-self.scale,self.centerY2-\
            self.scale-130,self.scale*2,self.scale*2)
            stroke_weight(0)
            ellipse(self.centerX2+self.scale*3-self.scale+3,self.centerY2-\
            self.scale-127,self.scale*1.85,self.scale*1.85)
            ellipse(self.centerX2+self.scale*3-self.scale+6,self.centerY2-\
            self.scale-124,self.scale*1.7,self.scale*1.7)

            stroke_weight(2)
            stroke(self.theme["compassStrokeSouth"])
            line(self.centerX2-yawCos*self.scale+self.scale*3,self.centerY2-\
            yawSin*self.scale-130,self.centerX2+yawCos*self.scale+\
            self.scale*3,self.centerY2+yawSin*self.scale-130)
            stroke(self.theme["compassStrokeNorth"])
            line(300,72.5,self.centerX2+yawCos*self.scale+self.scale*3,\
            self.centerY2+yawSin*self.scale-130)

            #circle on top of compass
            stroke_weight(0)
            stroke(self.theme["compassStroke"])
            fill(self.theme["compassTopFill"])
            ellipse(self.centerX2+self.scale*3-self.scale+9.5,\
            self.centerY2-self.scale-120.5,self.scale*1.5,self.scale*1.5)

            #compass text
            tint(self.theme["buttonText"])
            if self.compassStat == False:
                text(directionText(yaw), font_name='Verdana', font_size=10.0, \
                x=self.centerX2+self.scale*3, y=self.centerY2+self.scale-167, \
                alignment=5)
            else:
                tempYaw = yaw + 180 + 90
                if tempYaw > 360:
                    tempYaw = tempYaw % 360
                yawString = str(int(round(tempYaw, 0))) + chr(186)
                text(yawString, font_name='Verdana', font_size=10.0, \
                x=self.centerX2+self.scale*3, y=self.centerY2+self.scale-167, \
                alignment=5)

        #Center Screen Text (decide what is being displayed)
        def centerScreenText(self):
            tint(self.theme["mainTextColor"])
            if self.measuringOn == False and self.functionState == 0:
                text('Tap to Start', font_name='Verdana', font_size=16.0, \
                x=self.centerX2, y=self.centerY2+self.scale+150, alignment=5)
            elif self.measuringOn == True and self.functionState == 1:
                if self.timerCount // 20 % 3 == 0:
                    text('Recording Journey.', font_name='Verdana', \
                    font_size=16.0, x=self.centerX2, \
                    y=self.centerY2+self.scale+150, alignment=5)
                elif self.timerCount // 20 % 3 == 1:
                    text('Recording Journey..', font_name='Verdana', \
                    font_size=16.0, x=self.centerX2, \
                    y=self.centerY2+self.scale+150, alignment=5)
                elif self.timerCount // 20 % 3 == 2:
                    text('Recording Journey...', font_name='Verdana', \
                    font_size=16.0, x=self.centerX2, \
                    y=self.centerY2+self.scale+150, alignment=5)
                text('Tap to Stop', font_name='Verdana', font_size=16.0, \
                x=self.centerX2, y=self.centerY2+self.scale+120, alignment=5)
            elif self.measuringOn == False and self.functionState == 2:
                text('Calculating', font_name='Verdana', font_size=16.0, \
                x=self.centerX2, y=self.centerY2+self.scale+150, alignment=5)

            #if not enough values are recorded, say that they need more
            tint(self.theme["needMoreText"])
            if self.needMore == True:
                text("Need to record more locations.", font_name='Verdana', \
                font_size=16.0, x=self.size.x/2, y=self.size.y/2-20, \
                alignment=5)

        makeResetButton(self)

        drawCompass(self)

        centerScreenText(self)

        #When measuring is on record locations and put into array of locations
        def measuring(self):
            tint(self.theme["buttonText"])
            if picTaken == False:
                #camera mode
                stroke_weight(0)
                fill(self.theme["buttonFill"])
                tint(self.theme["buttonText"])
                ellipse(self.size.x/2-self.scale/2,0-self.scale/2+72,\
                self.scale,self.scale)
                text('Snap', font_name='Verdana', font_size=8.0, \
                x=self.centerX2, y=self.centerY2+self.scale-167, alignment=5)

            #to avoid repeats, only add if the location is not in the list
            if latLong not in self.locations:
                self.locations += [latLong]
                if self.activator == True:
                    self.activator = False
                self.loopPromptState = 0

            #if you travel in a loop ask the user if they want to keep the loop
            #or break it and get rid of the points in the loop from the path
            elif latLong in self.locations[0:-5] and self.activator == False:
                #sound.play_effect('arcade:Laser_1')
                self.loopPrompt = True
                self.activator = True

            #if you say tes to continuing, then you it will keep Recording
            #repeated points until you reach a point not in the list.
            if self.loopPromptState == 1:
                if self.currentLoc != latLong:
                    self.currentLoc = latLong
                    self.locations += [latLong]

            #if they say yes to breaking the loop, remove everything from the
            #end til that point in the list
            elif self.loopPromptState == 2:
                loc = self.locations.index(latLong)
                self.locations = self.locations[0:loc]
                self.loopPromptState = 0

        #when its in measuring state, measure.
        if self.measuringOn == True:
            measuring(self)

        #after measuring is off, setup array to use on the way back
        elif self.functionState == 2 and len(self.locations) > 3:
            self.locationsLeft = copy.deepcopy(self.locations)

            #do this to avoid tapping too early
            self.functionState += 1

        #after the array set up, tap to get back to where you were
        elif self.functionState == 3:
            tint(self.theme["mainTextColor"])
            text('Tap to return to start', font_name='Verdana', \
            font_size=16.0, x=self.centerX2, \
            y=self.centerY2+self.scale+150, alignment=5)

        #function state 4 is about getting back to the original location
        elif self.functionState == 4:

            #tracing back steps
            if latLong in self.locationsLeft:
                loc = self.locationsLeft.index(latLong)

                #chopping off everything til the point that it recognizes in
                #the list is way better than using .remove because now if I
                #loop into the list or the location sensor picks up late, I
                #can subvert those problems and allow Thread to work seamlessly

                # self.locationsLeft.remove(latLong) #dont use this
                self.locationsLeft = self.locationsLeft[0:loc]

            #pull the last location, that is the destination
            x, y = self.locations[0]
            x = round(x, 10)
            y = round(y, 10)
            loc = "[" + str(x) + ", " + str(y) + "]"
            fill(self.theme["buttonFill"])
            stroke(self.theme["titleColor"])
            stroke_weight(0)
            # rect(0,560,self.size.w,50)
            # # text('Destination:', font_name='Verdana', font_size=12.0, x=70, y=550, alignment=5)
            # # text(loc, font_name='Verdana', font_size=12.0, x=230, y=550, alignment=5)

            #calculate total distance traveled by using the distance formula
            totalDistanceTraveled = 0
            for i in range(len(self.locations) - 1):
                firstPosX, firstPosY = self.locations[i]
                nextPosX, nextPosY = self.locations[i + 1]
                dist = ((nextPosX - firstPosX)**2 + \
                (nextPosY - firstPosY)**2)**0.5
                totalDistanceTraveled += dist

            #not displaying the distance traveled because it is unnecessary
            #and usually incorrect because of the nature of the data
            # text('Distance Traveled:', font_name='Verdana', font_size=12.0, \
            # x=70, y=585, alignment=5)
            # text(str(totalDistanceTraveled), font_name='Verdana', \
            # font_size=12.0, x=230, y=585, alignment=5)

            #formatting
            textPosX = self.size.x / 2
            textPosY = self.size.y / 2

            #displaying the arrow that guides you back
            #if you have more than one, an arrow will point you back
            #if not it will say "Welcome Back."
            if (len(self.locationsLeft) > 1):
                #if you have more than 5 points left, use the average location
                #of the next 4 points on the path to get an accurate direction
                #back because points are recorded on a grid, so going from
                #point to point will be either left right forward or backward
                #and nothing in between.
                if (len(self.locationsLeft) > 4):
                    currPosX, currPosY = latLong
                    # currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]
                    secondPosX, secondPosY = self.locationsLeft[-3]
                    thirdPosX, thirdPosY = self.locationsLeft[-4]
                    fourthPosX, fourthPosY = self.locationsLeft[-5]

                    #use the average location to get a good direction
                    xNextAverage = (nextPosX + secondPosX + \
                    thirdPosX + fourthPosX)/4
                    yNextAverage = (nextPosY + secondPosY + \
                    thirdPosY + fourthPosY)/4

                #if there are 3 or 2 points left then just use what's left
                elif (len(self.locationsLeft) > 2):
                    currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]
                    secondPosX, secondPosY = self.locationsLeft[-3]

                    xNextAverage = (nextPosX + secondPosX) / 2
                    yNextAverage = (nextPosY + secondPosY) / 2

                elif (len(self.locationsLeft) > 1):
                    currPosX, currPosY = self.locationsLeft[-1]
                    nextPosX, nextPosY = self.locationsLeft[-2]

                    xNextAverage = (nextPosX) / 1
                    yNextAverage = (nextPosY) / 1

                #this formula was from: "https://www.igismap.com/formula
                #-to-find-bearing-or-heading-angle-between-two-points-
                #latitude-longitude/"
                xVal = math.cos(math.radians(xNextAverage))*\
                math.sin(math.radians(yNextAverage-currPosY))

                yVal = math.cos(math.radians(currPosX))*\
                math.sin(math.radians(xNextAverage))-\
                math.sin(math.radians(currPosX))*\
                math.cos(math.radians(xNextAverage))*\
                math.cos(math.radians(yNextAverage-currPosY))

                radianAngle = (math.atan2(xVal, yVal))
                degreeAngle = math.degrees(radianAngle)

                #had to derive this method on my own
                if degreeAngle < 0:
                    degreeAngle = 360 + degreeAngle

                trueDegree = degreeAngle

                #draw directing arrow: have to use this formula
                #it allow you to spin the phone and it will still point you in
                #the right direction
                tempYaw1 = -(360 - yaw + trueDegree)
                trueDegreeSin = math.sin(math.radians(tempYaw1))
                trueDegreeCos = math.cos(math.radians(tempYaw1))

                #draw the arrow that points you back
                def pointMaker(self, roll, pitch, yaw):
                    stroke_weight(20)
                    stroke(self.theme["guiderBack"])
                    line(self.centerX2-trueDegreeCos*130, \
                    self.centerY-trueDegreeSin*130, \
                    self.centerX2+trueDegreeCos*130, \
                    self.centerY+trueDegreeSin*130 )
                    stroke(self.theme["compassStrokeNorth"])
                    #make the point by increasing length
                    #and reducing stroke weight (thickness)
                    for i in range(20):
                        stroke_weight(20 - i)
                        line(self.centerX2+trueDegreeCos*(130+(i*2)), \
                        self.centerY+trueDegreeSin*(130+(i*2)), \
                        self.centerX2 ,self.centerY )

                #draw the point
                pointMaker(self, roll, pitch, yaw)

                stroke_weight(0)
                #display images when you are in the location that
                #an image is in
                if latLong in self.photoLocations \
                and self.imageModeOpen == False:

                    photoLoc = len(self.photoLocations) - \
                    self.photoLocations.index(latLong)

                    self.imageMode = True

                    tint(self.theme["textColor2"])
                    fill(self.theme["notificationColor"])
                    stroke(self.theme["notificationColor"])

                    #display a banner that says there is a landmark here!
                    rect(0, 150, self.size.w, 50)
                    text("Landmark! Tap here to see!", font_name='Verdana', \
                    font_size=16.0, x=self.centerX2, y=150+25, alignment=5)
                    self.currentImage = self.photoLibrary.assets[-photoLoc]
                    self.photoWidth = self.currentImage.pixel_width
                    self.photoHeight = self.currentImage.pixel_height

                    #get the image ready for display when the banner is tapped
                    self.ui_image = self.currentImage.get_ui_image()

            else:
                #when there are no locations left display Welcome Back.
                text("Welcome Back.", font_name='Verdana', font_size=16.0, \
                x=textPosX, y=textPosY, alignment=5)

        #the more state buttons to be displayed
        fill(self.theme["needMoreText"])
        tint(self.theme["textColor2"])
        if self.MoreState == True:
            #to seperate the active buttons from the non active ones
            fill(self.theme["transparentFill"])
            rect(0, 0, self.size.x, self.size.y)

            #SOS Button draw
            fill(self.theme["needMoreText"])
            tint(self.theme["textColor2"])
            ellipse(self.centerX2-0.0-self.scale,self.centerY2-self.scale-130,\
            self.scale*2,self.scale*2)
            text('SOS', font_name='Verdana', font_size=12.0, x=self.centerX2, \
            y=self.centerY2+self.scale-167, alignment=5)

            #only display certain buttons when it is over an amount of points
            if len(self.locations) >= 2:

                #MAP Button draw
                fill(self.theme["mapButton"])
                tint(self.theme["otherButtonTexts"])
                ellipse(self.centerX2-self.scale*3-self.scale,\
                self.centerY2-self.scale-20,self.scale*2,self.scale*2)
                text('Map View', font_name='Verdana', font_size=12.0, \
                x=self.centerX2-112, y=self.centerY2+self.scale-57,alignment=5)

                #Share Button Draw
                fill(self.theme["shareButton"])
                tint(self.theme["otherButtonTexts"])
                ellipse(self.centerX2-self.scale,self.centerY2-self.scale-20,\
                self.scale*2,self.scale*2)
                text('Share', font_name='Verdana', font_size=12.0, \
                x=self.centerX2, y=self.centerY2+self.scale-57, alignment=5)

                #let user know the text was copied after pressing share
                tint(self.theme["copiedText"])
                if self.clipState == True:
                    text('Copied to Clipboard', font_name='Verdana', \
                    font_size=16.0, x=self.size.x/2, y=self.size.y/2-65, \
                    alignment=5)

            #trace button draw
            fill(self.theme["traceButton"])
            tint(self.theme["otherButtonTexts"])
            ellipse(self.centerX2+self.scale*3-self.scale,\
            self.centerY2-self.scale-20,self.scale*2,self.scale*2)
            text('Trace', font_name='Verdana', font_size=12.0, \
            x=self.centerX2+112, y=self.centerY2+self.scale-57, alignment=5)

            #SOS Button Draw
            fill(self.theme["SOScolor"])
            tint(self.theme["otherButtonTexts"])
            ellipse(self.centerX2-0.0-self.scale,\
            self.centerY2-self.scale-130,self.scale*2,self.scale*2)
            text('SOS', font_name='Verdana', font_size=12.0, \
            x=self.centerX2, y=self.centerY2+self.scale-167, alignment=5)

            #Theme switching button draw
            fill(self.theme["themeButton"])
            tint(self.theme["themeText"])
            ellipse(self.centerX2-self.scale,self.size.y-115,\
            self.scale*2,self.scale*2)
            text('Theme', font_name='Verdana', font_size=12.0, \
            x=self.centerX2, y=self.size.y-77, alignment=5)

        #More button at the bottom of screen
        elif self.MoreState == False:
            fill(self.theme["moreColor"])
            ellipse(self.size.x/2-self.scale/2,0-self.scale/2-5,\
            self.scale,self.scale)

        #photo border when the photo is displayed
        if self.imageModeOpen == True:
            fill(0, 0, 0, 0.5)
            rect(0, 0, self.size.x, self.size.y)
            fill(self.theme["photoBorder"])
            rect(5, self.halfScreenFrame, self.widthFrame, self.heightFrame)

        #this function draws the path out in the frame that you can access
        #after pressing the trace button.
        def drawPath(self):
            fill(self.theme["pathBorder"])
            rect(14, self.size.y / 2 - 55, self.size.x - 28,
                 self.size.y / 3 + 12)
            fill(self.theme["pathScreenFill"])
            rect(19, self.size.y / 2 - 50, self.size.x - 38,
                 self.size.y / 3 + 2)

            tint(self.theme["pathEndColor"])
            fill(self.theme["pathEndColor"])
            stroke(self.theme["pathEndColor"])
            stroke_weight(2)
            iPointX, iPointY = self.locations[0]
            newLocs = [(0, 0)]
            maxDiffX = 0
            maxDiffY = 0
            xSepVals = []
            ySepVals = []

            #take the differences between the latLong points and essentially
            #simplify the data
            for loc in self.locations[1:]:
                nPointX, nPointY = loc
                dataX = (iPointX - nPointX) * 100000
                dataY = (iPointY - nPointY) * 100000
                newLocs += [(dataX, dataY)]

            #split up x and y values
            for loc in newLocs:
                xSepVals += [loc[0]]
                ySepVals += [loc[1]]

            #look for the maximum difference between the x vals and y values
            maxDiffX = (max(xSepVals) - min(xSepVals))
            maxDiffY = (max(ySepVals) - min(ySepVals))

            #scale it based on the maximum difference so the graph looks scaled
            #properly and one axis isn't many times smaller than the other
            generalMaxDiff = max(maxDiffX, maxDiffY)

            #make everything positive
            miniX = min(xSepVals)
            jumpX = 0 - miniX
            evenedLocsX = []
            for val in xSepVals:
                evenedLocsX += [val + jumpX]

            miniY = min(ySepVals)
            jumpY = 0 - miniY
            evenedLocsY = []
            for val in ySepVals:
                evenedLocsY += [val + jumpY]

            #start drawing in the frame
            #the color changes from red to black as you go farther from your
            #current location
            #changes from red to blue in dark mode
            xCenter = self.size.x / 2
            yCenter = self.size.y / 2 - 50 + 2
            colorCount = len(newLocs)
            for i in range(len(newLocs) - 1):
                xLoc1, yLoc1 = evenedLocsX[i], evenedLocsY[i]
                xLoc2, yLoc2 = evenedLocsX[i + 1], evenedLocsY[i + 1]
                #add if statements
                if self.theme == self.darkMode:
                    stroke(((255 / colorCount) * i) / 255, 0, 0.15)
                    fill(((255 / colorCount) * i) / 255, 0, 0.15)
                elif self.theme == self.lightMode:
                    stroke(((255 / colorCount) * i) / 255, 0, 0)
                    fill(((255 / colorCount) * i) / 255, 0, 0)
                #draw lines between points
                line((335/generalMaxDiff)*xLoc1+20,(222/generalMaxDiff)*\
                yLoc1+yCenter, (335/generalMaxDiff)*xLoc2+20,\
                (222/generalMaxDiff)*yLoc2+yCenter)
                #draw points, but they will be one shade lighter than the
                #line they are between because it looks cooler and stands out
                if len(self.locations) > 20:
                    ellipse((335/generalMaxDiff)*xLoc1+20-1,\
                    (222/generalMaxDiff)*yLoc1+yCenter-1,2,2)
                else:
                    ellipse((335/generalMaxDiff)*xLoc1+20-2,\
                    (222/generalMaxDiff)*yLoc1+yCenter-2,4,4)
            ellipse((335/generalMaxDiff)*evenedLocsX[-1]+20-2,\
            (222/generalMaxDiff)*evenedLocsY[-1]+yCenter-2,4,4)

        #if you push the trace button and you have locations, reveal the path
        if self.pathState == True and len(self.locations) > 0:
            drawPath(self)

        #when someone walks in a circle, prompt the user to decide what to do:
        #delete the looped area, or keep that path of travel.
        def loopPromptBox(self):
            fill(self.theme["loopPromptColor"])
            stroke_weight(0)
            #prompt for the loop
            rect(50, self.size.y / 2 - 50, self.size.x - 100,
                 self.size.y / 6 + 40)
            tint(self.theme["loopPromptTextColor"])
            text("Stop Moving!", font_name='Verdana', font_size=18.0, \
            x=self.size.x/2, y=self.size.y/2+65, alignment=5)
            text("Looks like you\'ve made a loop", font_name='Verdana', \
            font_size=13.0, x=self.size.x/2, y=self.size.y/2+38, alignment=5)
            stroke(self.theme["loopPromptTextColor"])
            stroke_weight(3)
            line(50, self.size.y / 2, self.size.x - 50, self.size.y / 2)
            line(self.size.x / 2, self.size.y / 2 - 50, self.size.x / 2,
                 self.size.y / 2)
            text("Continue", font_name='Verdana', font_size=15.0, \
            x=self.size.x/2-70, y=self.size.y/2-25, alignment=5)
            text("Break", font_name='Verdana', font_size=15.0, \
            x=self.size.x/2+70, y=self.size.y/2-25, alignment=5)

        #prompt when the user goes in a loop/crosses paths with recorded path
        if self.loopPrompt == True:
            loopPromptBox(self)
Exemple #34
0
 def update(self):
     motion.start_updates()
     gx = motion.get_gravity()[0] * 50
     self.x = min(max(self.x + gx, 20),
                  self.scene.size.w - (20 + image_width))
Exemple #35
0
    def draw(self):
        #Box
        self.cx = self.size.w * 0.5
        self.cy = self.size.h * 0.5
        #pitch,roll,yaw
        self.cx2 = self.size.w * 0.5
        self.cy2 = self.size.h * 0.5 - scale * 3.5
        time.sleep(0.1)
        #motion
        ax, ay, az = motion.get_user_acceleration()
        gx, gy, gz = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        mx, my, mz, ma = motion.get_magnetic_field()
        pitch, roll, yaw = [x for x in gravity_vectors]
        pitch = -pitch * 180 / math.pi
        roll = roll * 180 / math.pi
        yaw = -yaw * 180 / math.pi
        #redraw screen
        background(1, 1, 1)
        fill(1, 1, 1)
        stroke_weight(1)
        #pitch,roll,yaw描画
        # ellipse(self.cx2-scale*3-self.R,self.cy2-self.R,self.R*2,self.R*2)
        # ellipse(self.cx2-0.0-self.R,self.cy2-self.R,self.R*2,self.R*2)
        ellipse(self.cx2 + scale * 3 - self.R, self.cy2 - self.R - 130,
                self.R * 2, self.R * 2)
        roll_sin = math.sin(math.radians(roll))
        roll_cos = math.cos(math.radians(roll))
        pitch_sin = math.sin(math.radians(pitch))
        pitch_cos = math.cos(math.radians(pitch))
        yaw_sin = math.sin(math.radians(yaw))
        yaw_cos = math.cos(math.radians(yaw))
        # line(self.cx2-roll_cos*self.R-scale*3,self.cy2-roll_sin*self.R,self.cx2+roll_cos*self.R-scale*3,self.cy2+roll_sin*self.R)
        # line(self.cx2-pitch_cos*self.R-0,self.cy2-pitch_sin*self.R,self.cx2+pitch_cos*self.R-0,self.cy2+pitch_sin*self.R)

        line(self.cx2 - yaw_cos * self.R + scale * 3,
             self.cy2 - yaw_sin * self.R - 130,
             self.cx2 + yaw_cos * self.R + scale * 3,
             self.cy2 + yaw_sin * self.R - 130)
        yawMatrix = np.matrix([[yaw_cos, -yaw_sin, 0], [yaw_sin, yaw_cos, 0],
                               [0, 0, 1]])
        pitchMatrix = np.matrix([[pitch_cos, 0, pitch_sin], [0, 1, 0],
                                 [-pitch_sin, 0, pitch_cos]])
        rollMatrix = np.matrix([[1, 0, 0], [0, roll_cos, -roll_sin],
                                [0, roll_sin, roll_cos]])

        R = yawMatrix * pitchMatrix * rollMatrix
        R = np.array(R)
        x_3d, y_3d, z_3d = np.transpose(np.dot(self.Box, R), (2, 0, 1))
        zmin = np.argmin(z_3d)

        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''

            return directionSym

        #text
        tint(0, 0, 0, 1)
        #text('Direction:', font_name='Helvetica', font_size=16.0, x=self.cx2+scale*3, y=self.cy2+self.R-80, alignment=5)
        text(directionText(yaw),
             font_name='Helvetica',
             font_size=10.0,
             x=self.cx2 + scale * 3,
             y=self.cy2 + self.R - 100,
             alignment=5)
        delta = newtime - oldtime
        if delta != 0:
            ips = int((iters - olditers) / delta)
            maxips = max(maxips, ips)
        oldtime = newtime
        olditers = iters
        moonunits = d2e / moondistance
        velocity = math.hypot(shipvx, shipvy)
        escapevelocity = math.sqrt(-2.0 * (earthgrav + moongrav) / d2e)

        if (velocity > escapevelocity) and (d2e > offscreen):
            show_earth(0, 1, 0)  # show green Earth then quit
            print("\n+++++++  Escape velocity !  +++++++")
            break

        gravx, gravy, gravz = motion.get_gravity()  # device orientation

        if gravz > 0.3:  # quit if user turns device screen downwards
            show_earth(1, 1, 0)  # show yellow Earth
            break

        if gravx > 0.8:  # output info if tipped to landscape
            print(f"{moonunits:6.2f} moonu @ {velocity:7.0f} mps")

    simtime += dtime
    iters += 1

motion.stop_updates()
logfile.close()

stoptime = time.process_time()
    def draw(self):
        #Box
        self.cx = self.size.w * 0.5
        self.cy = self.size.h * 0.5
        #pitch,roll,yaw
        self.cx2 = self.size.w * 0.5
        self.cy2 = self.size.h * 0.5 - scale * 3.5
        time.sleep(0.1)
        #motion
        ax, ay, az = motion.get_user_acceleration()
        gx, gy, gz = motion.get_gravity()
        gravity_vectors = motion.get_attitude()
        mx, my, mz, ma = motion.get_magnetic_field()
        pitch, roll, yaw = [x for x in gravity_vectors]
        pitch = -pitch * 180 / math.pi
        roll = roll * 180 / math.pi
        yaw = -yaw * 180 / math.pi
        #redraw screen
        fill(0.5, 0.5, 0.5)
        stroke_weight(2)
        #pitch,roll,yaw
        # ellipse(self.cx2-scale*3-self.R,self.cy2-self.R,self.R*2,self.R*2)
        # ellipse(self.cx2-0.0-self.R,self.cy2-self.R,self.R*2,self.R*2)
        ellipse(self.cx2 + scale * 3 - self.R, self.cy2 - self.R - 130,
                self.R * 2, self.R * 2)
        fill(0.7, 0.7, 0.7)
        no_stroke()
        ellipse(self.cx2 + scale * 3 - self.R + 3, self.cy2 - self.R - 127,
                self.R * 1.85, self.R * 1.85)
        fill(0.8, 0.8, 0.8)
        ellipse(self.cx2 + scale * 3 - self.R + 6, self.cy2 - self.R - 124,
                self.R * 1.7, self.R * 1.7)
        fill(0.9, 0.9, 0.9)
        ellipse(self.cx2 + scale * 3 - self.R + 9.5, self.cy2 - self.R - 120.5,
                self.R * 1.5, self.R * 1.5)

        roll_sin = math.sin(math.radians(roll))
        roll_cos = math.cos(math.radians(roll))
        pitch_sin = math.sin(math.radians(pitch))
        pitch_cos = math.cos(math.radians(pitch))
        yaw_sin = math.sin(math.radians(yaw))
        yaw_cos = math.cos(math.radians(yaw))
        # line(self.cx2-roll_cos*self.R-scale*3,self.cy2-roll_sin*self.R,self.cx2+roll_cos*self.R-scale*3,self.cy2+roll_sin*self.R)
        # line(self.cx2-pitch_cos*self.R-0,self.cy2-pitch_sin*self.R,self.cx2+pitch_cos*self.R-0,self.cy2+pitch_sin*self.R)
        stroke(0, 0, 0)
        stroke_weight(2)
        line(self.cx2 - yaw_cos * self.R + scale * 3,
             self.cy2 - yaw_sin * self.R - 130,
             self.cx2 + yaw_cos * self.R + scale * 3,
             self.cy2 + yaw_sin * self.R - 130)
        stroke(1, 0, 0)
        line(300, 72.5, self.cx2 + yaw_cos * self.R + scale * 3,
             self.cy2 + yaw_sin * self.R - 130)

        yawMatrix = np.matrix([[yaw_cos, -yaw_sin, 0], [yaw_sin, yaw_cos, 0],
                               [0, 0, 1]])
        pitchMatrix = np.matrix([[pitch_cos, 0, pitch_sin], [0, 1, 0],
                                 [-pitch_sin, 0, pitch_cos]])
        rollMatrix = np.matrix([[1, 0, 0], [0, roll_cos, -roll_sin],
                                [0, roll_sin, roll_cos]])

        R = yawMatrix * pitchMatrix * rollMatrix
        R = np.array(R)

        #x_3d,y_3d,z_3d = np.transpose(np.dot(self.Box,R),(2,0,1))
        #zmin = np.argmin(z_3d)

        #derive the direction (NSEW)
        def directionText(yaw):
            directionSym = ''
            #Direction
            if yaw > 60 and yaw <= 120:
                directionSym = 'N'
            elif yaw > 120 and yaw <= 150:
                directionSym = 'NE'
            elif (yaw > 150 and yaw <= 180) or (yaw >= -180 and yaw <= -150):
                directionSym = 'E'
            elif yaw > -150 and yaw <= -120:
                directionSym = 'SE'
            elif yaw > -120 and yaw <= -60:
                directionSym = 'S'
            elif yaw > -60 and yaw <= -30:
                directionSym = 'SW'
            elif yaw > -30 and yaw <= 30:
                directionSym = 'W'
            elif yaw > 30 and yaw <= 60:
                directionSym = 'NW'
            else:
                directionSym = ''

            return directionSym

        #text
        tint(0, 0, 0, 1)
        text('Thread 1.0',
             font_name='Courier',
             font_size=16.0,
             x=self.cx2,
             y=self.cy2 + self.R + 400,
             alignment=5)
        if self.measuringOn == False and self.checkedOnce == 0:
            text('Tap to Start',
                 font_name='Helvetica',
                 font_size=16.0,
                 x=self.cx2,
                 y=self.cy2 + self.R + 150,
                 alignment=5)
        elif self.measuringOn == True and self.checkedOnce == 1:
            if self.testCounter % 3 == 0:
                text('Measuring.',
                     font_name='Helvetica',
                     font_size=16.0,
                     x=self.cx2,
                     y=self.cy2 + self.R + 150,
                     alignment=5)
            elif self.testCounter % 3 == 1:
                text('Measuring..',
                     font_name='Helvetica',
                     font_size=16.0,
                     x=self.cx2,
                     y=self.cy2 + self.R + 150,
                     alignment=5)
            elif self.testCounter % 3 == 2:
                text('Measuring...',
                     font_name='Helvetica',
                     font_size=16.0,
                     x=self.cx2,
                     y=self.cy2 + self.R + 150,
                     alignment=5)
            text('Tap to Stop',
                 font_name='Helvetica',
                 font_size=16.0,
                 x=self.cx2,
                 y=self.cy2 + self.R + 120,
                 alignment=5)

        elif self.measuringOn == False and self.checkedOnce == 2:
            text('Calculating',
                 font_name='Helvetica',
                 font_size=16.0,
                 x=self.cx2,
                 y=self.cy2 + self.R + 120,
                 alignment=5)

        #compass text
        text(directionText(yaw),
             font_name='Helvetica',
             font_size=10.0,
             x=self.cx2 + scale * 3,
             y=self.cy2 + self.R - 100,
             alignment=5)

        if self.measuringOn == True:
            time.sleep(0.2)
            current = location.get_location()
            latLong = (current['latitude'], current['longitude'])
            self.locations += [latLong]
            self.testCounter += 1
            #sound.play_effect('arcade:Laser_2')
        elif self.checkedOnce == 2:
            self.locationsReversed = copy.deepcopy(self.locations)
            self.locationsReversed.reverse()

            #now use the locations and map it onto a map so you know the direciton between points (NWSE)
            #start saying stuff like "Point North and Move Forward"
            #Now it makes sense why we use the compass.

            #test texts
            self.checkedOnce += 1

        elif self.checkedOnce == 3 or self.checkedOnce == 4:

            locationList = str(self.locations[0]) + ' ' + str(
                self.locations[-1])
            text(locationList,
                 font_name='Helvetica',
                 font_size=5.0,
                 x=self.cx2,
                 y=self.cy2 + self.R + 80,
                 alignment=5)
            locationListReversed = str(self.locationsReversed[0]) + ' ' + str(
                self.locationsReversed[-1])
            text(locationListReversed,
                 font_name='Helvetica',
                 font_size=5.0,
                 x=self.cx2,
                 y=self.cy2 + self.R + 60,
                 alignment=5)
Exemple #38
0
# coding: utf-8

# https://forum.omz-software.com/topic/2469/scene-gravity-and-orientation/3

# scene.gravity is deprecated - you should use motion.get_gravity instead, which is independent of the scene module.

# Basic usage looks like this:

import motion

motion.start_updates()
print(motion.get_gravity())
motion.stop_updates()

# All code that makes use of motion data needs to go between start_updates and stop_updates. If the code inside might raise an exception, you should put the entire thing in a try-finally block to make sure that motion updates are stopped so your battery doesn't drain too quickly.

import motion

motion.start_updates()
try:
    pass # Do motion things
finally:
    motion.stop_updates()

# The try block will not catch any exceptions, it only ensures that the finally block is run whether an exception is raised or not.

import scene
import motion


class MyScene (scene.Scene):