Skip to content
Snippets Groups Projects
Commit 24720fd7 authored by iggy's avatar iggy
Browse files

new captouch in event.py and some cleanup

parent 55eea5c3
No related branches found
No related tags found
No related merge requests found
......@@ -4,21 +4,7 @@ import time
import math
import event
<<<<<<< HEAD
def xy_from_polar(r,deg):
#rad = deg/180*math.pi
return( (
r * math.sin(deg), #x
r * math.cos(deg) #y
) )
def randrgb():
return ((random.random(),random.random(),random.random()))
=======
>>>>>>> events
WIDTH = 240
HEIGHT = 240
......@@ -30,33 +16,6 @@ BLUE = (0,0,1)
WHITE = (1,1,1)
GREY = (0.5,0.5,0.5)
<<<<<<< HEAD
# Get the global context (representing the whole screen)
global ctx
'''
#center the text horizontally and vertically
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
#ctx.rgb() expects individual values for the channels, so unpack a list/tuple with *
#operations on ctx can be chained
#create a blue background
ctx.rgb(*BLUE).rectangle(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT).fill()
#Write some text
ctx.move_to(0,0).rgb(*WHITE).text("Hi :)")
hardware.display_update()
'''
class Worm():
def __init__(self):
self.color = randrgb()
self.direction = random.random()*math.pi*2
self.size = 10
self.speed = self.size/5
(x,y) = xy_from_polar(40, self.direction+90)
=======
def xy_from_polar(r,deg):
#rad = deg/180*math.pi
......@@ -82,17 +41,12 @@ class Worm():
self.size = 50
self.speed = self.size/5
(x,y) = xy_from_polar(100, self.direction)
>>>>>>> events
self.x = x
self.y= y
#(self.dx,self.dy) = xy_from_polar(1,self.direction)
self._lastdist = 0.0
def draw(self):
<<<<<<< HEAD
global ctx
=======
>>>>>>> events
ctx.rgb(*self.color)
ctx.round_rectangle(
self.x-self.size/2,
......@@ -102,24 +56,17 @@ class Worm():
def mutate(self):
self.color = ([max(0,min(1,x+((random.random()-0.5)*0.3))) for x in self.color])
<<<<<<< HEAD
=======
self.size = 20
>>>>>>> events
def move(self):
dist = math.sqrt(self.x**2+self.y**2)
<<<<<<< HEAD
self.size = (120-dist)/3
=======
target_size = (130-dist)/3
if self.size>target_size: self.size-=1
if self.size<target_size: self.size+=1
>>>>>>> events
self.speed = self.size/5
self.direction += (random.random()-0.5)*math.pi/4
......@@ -129,42 +76,13 @@ class Worm():
self.y+=dy
<<<<<<< HEAD
if dist>110-self.size/2 and dist>self._lastdist:
=======
if dist>120-self.size/2 and dist>self._lastdist:
>>>>>>> events
polar_position=math.atan2(self.y,self.x)
dx=dx*-abs(math.cos(polar_position))
dy=dy*-abs(math.sin(polar_position))
self.direction=-math.atan2(dy,dx)
self.mutate()
self._lastdist = dist
<<<<<<< HEAD
global worms
def init():
global worms
global ctx
worms = []
for i in range(23):
worms.append(Worm())
ctx = hardware.get_ctx()
def run():
global worms
for w in worms:
w.draw()
w.move()
hardware.display_update()
time.sleep(0.001)
def foreground():
pass
=======
def handle_input(data):
......@@ -172,7 +90,6 @@ def handle_input(data):
if len(worms)>10:
worms.pop(0)
>>>>>>> events
def init(data={}):
# Get the global context (representing the whole screen)
......
......@@ -29,15 +29,23 @@ class Engine():
self.events_timed.append(event)
self._sort_timed()
def add_input(self,event):
self.events_input.append(event)
def remove(self,group_id):
self.remove_input(group_id)
self.remove_timed(group_id)
def remove_timed(self,group_id):
self.events_timed = [event for event in self.events_timed if event.group_id==group_id]
self.events_timed = [event for event in self.events_timed if event.group_id!=group_id]
self._sort_timed()
def remove_input(self,group_id):
self.events_input = [event for event in self.events_input if event.group_id!=group_id]
def _sort_timed(self):
self.events_timed = sorted(self.events_timed, key = lambda event: event.deadline)
def add_input(self,event):
self.events_input.append(event)
def _handle_timed(self):
if not self.next_timed and self.events_timed:
......@@ -52,48 +60,53 @@ class Engine():
self.next_timed = None
def _handle_input(self):
input_state={
"b0":(hardware.get_button(0),"button",0),
"b1":(hardware.get_button(1),"button",1)
}
input_state = []
#buttons
for i in [0,1]:
input_state.append({
"type" : "button",
"index" : i,
"value" : hardware.get_button(i)
})
#captouch
for i in range(0,10):
input_state["c"+str(i)]=(hardware.get_captouch(i),"captouch",i)
input_state.append({
"type" : "captouch",
"index" : i,
"value" : hardware.get_captouch(i),
"radius" : hardware.captouch_get_petal_rad(i),
"angle" : hardware.captouch_get_petal_phi(i)/10000
})
if not self.last_input_state:
self.last_input_state=input_state
#tprint (input_state)
return
diff=[]
for key in input_state:
if input_state[key][0] != self.last_input_state[key][0]:
diff.append({
"type" : input_state[key][1],
"index" : input_state[key][2],
"value" : input_state[key][0],
"from" : self.last_input_state[key][0],
"ticks_ms": time.ticks_ms(),
"change": True
})
for i in range(len(input_state)):
entry = input_state[i]
last_entry = self.last_input_state[i]
#update for all
entry["ticks_ms"] = time.ticks_ms()
if entry["value"] != last_entry["value"]:
#update only when value changed
entry["change"] = True
entry["from"] = last_entry["value"]
else:
diff.append({
"type" : input_state[key][1],
"index" : input_state[key][2],
"value" : input_state[key][0],
"ticks_ms": time.ticks_ms(),
"change": False
})
#update only when value did not change
entry["change"] = False
if diff:
#print(diff)
for d in diff:
triggered_events = list(filter(lambda e: e.condition(d),self.events_input))
#find and trigger the events q
triggered_events = list(filter(lambda e: e.condition(entry),self.events_input))
#print (triggered_events)
#map(lambda e: e.trigger(d), triggered_events)
for e in triggered_events:
e.trigger(d)
e.trigger(entry)
self.last_input_state=input_state
......@@ -105,6 +118,7 @@ class Engine():
self._handle_timed()
self._handle_input()
self._handle_userloop()
hardware.display_update()
def eventloop(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment