-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerrico_sock.py
65 lines (55 loc) · 1.63 KB
/
Perrico_sock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.graphics.context_instructions import Rotate
from math import atan2, degrees
import socket
from kivy.clock import Clock
import re
class PerricoWidget(BoxLayout):
#Socket para conectarse al router openwrt
s = socket.socket()
#Patron para extraer un angulo de la cadena leida
#patron = re.compile("[-]{0,1}[0-9]{1,}.[0-9]{2}")
patron = re.compile("Z:[-]{,1}[0-9]{1,}.[0-9]{2}")
def leer(self,dt):
cartel=self.ids['cartel']
recibido = self.s.recv(40)
c = self.patron.search(recibido)
if c:
a = c.group(0)
a = a[2:]
cartel.text = a
self.inclinar_a(float(a))
def inclinar(self, angulo):
esparcido=self.ids['esparcido']
esparcido.rotation += 20*2*angulo/self.width
def inclinar_a(self, angulo):
esparcido=self.ids['esparcido']
esparcido.rotation = angulo
def on_touch_down(self, touch):
cartel=self.ids['cartel']
esparcido=self.ids['esparcido']
toque_x = touch.x-self.center_x
if toque_x>0:
sig = +1
else:
sig = -1
#angulo = sig*degrees(atan2(abs(touch.x-self.center_x), touch.y-self.center_y))
if esparcido.rotation > 180:
angulo = esparcido.rotation - 360
else:
angulo = esparcido.rotation
cartel.text = str(round(angulo,2))
self.inclinar(-toque_x)
class PerricoApp(App):
def build(self):
perrico = PerricoWidget()
perrico.s.connect(("192.168.2.1", 1234))
Clock.schedule_interval(perrico.leer,0.001)
return perrico
if __name__ == "__main__":
PerricoApp().run()