From d764e4a584a8797d587a4d9f3a020366787d9e77 Mon Sep 17 00:00:00 2001
From: dequis <dx@dxzone.com.ar>
Date: Mon, 29 May 2023 15:24:50 +0200
Subject: [PATCH] python_payload: Add demo_worms to the menu, use ctx in the
 menu

Changes by moon2 from a merge commit in dev_p4 branch
---
 python_payload/demo_worms.py | 145 +++++++++++++++++++----------------
 python_payload/main.py       |  92 ++++++++++++++++------
 2 files changed, 145 insertions(+), 92 deletions(-)

diff --git a/python_payload/demo_worms.py b/python_payload/demo_worms.py
index ebe97b991c..b96beb1d57 100644
--- a/python_payload/demo_worms.py
+++ b/python_payload/demo_worms.py
@@ -4,17 +4,17 @@ import time
 import math
 
 def xy_from_polar(r,deg):
-	#rad = deg/180*math.pi
-	
-	return( (
-		r * math.sin(deg), #x
-		r * math.cos(deg)  #y
-	)  )
+    #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()))
-	
-	
+    return ((random.random(),random.random(),random.random()))
+    
+    
 WIDTH = 240
 HEIGHT = 240
 
@@ -28,8 +28,8 @@ GREY = (0.5,0.5,0.5)
 
 # Get the global context (representing the whole screen)
 global ctx
-ctx = hardware.get_ctx()
 
+'''
 #center the text horizontally and vertically
 ctx.text_align = ctx.CENTER
 ctx.text_baseline = ctx.MIDDLE
@@ -42,66 +42,75 @@ 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)
-		self.x = x
-		self.y= y
-		#(self.dx,self.dy) = xy_from_polar(1,self.direction)
-		self._lastdist = 0.0
-	
-	def draw(self):
-		ctx.rgb(*self.color)
-		ctx.round_rectangle(
-			self.x-self.size/2,
-			self.y-self.size/2,
-			self.size,self.size,self.size//2
-		).fill()
-		
-	def mutate(self):
-		self.color =  ([max(0,min(1,x+((random.random()-0.5)*0.3))) for x in self.color])
-		
-	
-	def move(self):
-		dist = math.sqrt(self.x**2+self.y**2)
-		self.size = (120-dist)/3
-		self.speed = self.size/5
-		
-		self.direction += (random.random()-0.5)*math.pi/4
-		
-		(dx,dy) = xy_from_polar(self.speed,self.direction)
-		self.x+=dx
-		self.y+=dy
-		
-		
-		if dist>110-self.size/2 and dist>self._lastdist:
-			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
-		
-worms = []
-
-for i in range(23):
-	worms.append(Worm())
-	
-
-while True:
-	for w in worms:
-		w.draw()
-		w.move()
-	
-	hardware.display_update()
-	time.sleep(0.001)
-print ("done")
+    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)
+        self.x = x
+        self.y= y
+        #(self.dx,self.dy) = xy_from_polar(1,self.direction)
+        self._lastdist = 0.0
+    
+    def draw(self):
+        global ctx
+        ctx.rgb(*self.color)
+        ctx.round_rectangle(
+            self.x-self.size/2,
+            self.y-self.size/2,
+            self.size,self.size,self.size//2
+        ).fill()
+        
+    def mutate(self):
+        self.color =  ([max(0,min(1,x+((random.random()-0.5)*0.3))) for x in self.color])
+        
+    
+    def move(self):
+        dist = math.sqrt(self.x**2+self.y**2)
+        self.size = (120-dist)/3
+        self.speed = self.size/5
+        
+        self.direction += (random.random()-0.5)*math.pi/4
+        
+        (dx,dy) = xy_from_polar(self.speed,self.direction)
+        self.x+=dx
+        self.y+=dy
+        
+        
+        if dist>110-self.size/2 and dist>self._lastdist:
+            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
+
+
+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
 
 #Known problems:
 #ctx.rotate(math.pi) turns the display black until powercycled
diff --git a/python_payload/main.py b/python_payload/main.py
index 571cdf042f..033dae6964 100644
--- a/python_payload/main.py
+++ b/python_payload/main.py
@@ -3,32 +3,48 @@ import utils
 import time
 import harmonic_demo
 import melodic_demo
+import cap_touch_demo
+import demo_worms
 
 MODULES = [
     harmonic_demo,
     melodic_demo,
+    cap_touch_demo,
+    demo_worms,
 ]
 
 CURRENT_APP_RUN = None
 VOLUME = 0
-
-SELECT_TEXT = [
-    " ##  #### #    ####  ##  #####          ",
-    "#  # #    #    #    #  #   #        ##" ,
-    "#    #    #    #    #      #     #    #",
-    " ##  #### #    #### #      #         #" ,
-    "   # #    #    #    #      #     #    #",
-    "#  # #    #    #    #  #   #        ## ",
-    " ##  #### #### ####  ##    #            ",
-]
+ctx = None
 
 BACKGROUND_COLOR = 0
 
+
+def draw_volume_slider(volume):
+    global ctx
+    length = 96 + ((volume - 20) * 1.6)
+    if length > 96:
+        length = 96
+    if length < 0:
+        length = 0
+    length = int(length)
+
+    ctx.rgb(0,0,0)#dummy
+    ctx.round_rectangle(-49,41,98,8,3).fill()#dummy idk
+
+    ctx.rgb(0,255,0)
+    ctx.round_rectangle(-51,49,102,12,3).fill()
+    ctx.rgb(0,0,0)
+    ctx.round_rectangle(-50,50,100,10,3).fill()
+    ctx.rgb(0,255,0)
+    ctx.round_rectangle(-48,52, length ,6,3).fill()
+
 def run_menu():
     global CURRENT_APP_RUN
+    global ctx
     display_fill(BACKGROUND_COLOR)
-    utils.draw_text_big(SELECT_TEXT, 0, 0)
-    utils.draw_volume_slider(VOLUME)
+    draw_volume_slider(VOLUME)
+    ctx.move_to(0,0).rgb(255,0,255).text("select :3")
     display_update()
 
     selected_petal = None
@@ -53,8 +69,9 @@ def foreground_menu():
     utils.clear_all_leds()
     utils.highlight_bottom_petal(0,0,55,55);
     utils.highlight_bottom_petal(1,55,0,55);
+    utils.highlight_bottom_petal(2,55,55,0);
+    utils.highlight_bottom_petal(3,0,110,0);
     display_fill(BACKGROUND_COLOR)
-    utils.draw_text_big(SELECT_TEXT, 0, 0)
     display_update()
 
 def set_rel_volume(vol):
@@ -71,12 +88,36 @@ def set_rel_volume(vol):
         set_global_volume_dB(VOLUME)
     time.sleep_ms(100)
 
+def captouch_cal():
+    global ctx
+    display_fill(0b0000000111100111)
+    ctx.move_to(0,0).rgb(0,255,0).text("cal")
+    display_update()
+    time.sleep_ms(500)
+    display_fill(0b0011100000000111)
+    ctx.move_to(0,0).rgb(0,255,0).text("cal")
+    display_update()
+    captouch_autocalib()
+    while(captouch_calibration_active()):
+        pass
+    display_fill(0)
+    display_update()
+
 def main():
     global CURRENT_APP_RUN
+    global ctx
     while not init_done():
         pass
 
-    captouch_autocalib()
+    captouch_autocalib() #dry run
+    while(captouch_calibration_active()):
+        pass
+
+    ctx = get_ctx()
+    ctx.text_align = ctx.CENTER
+    ctx.text_baseline = ctx.MIDDLE
+
+    captouch_cal()
 
     for module in MODULES:
         module.init()
@@ -86,16 +127,19 @@ def main():
     set_global_volume_dB(VOLUME)
 
     while True:
-        if(get_button(0) == 2):
-            if CURRENT_APP_RUN == run_menu:
-                captouch_autocalib()
+        if((get_button(1) == 2) and (CURRENT_APP_RUN == run_menu)):
+            captouch_cal()
+            foreground_menu()
+        else:
+            if(get_button(0) == 2):
+                if CURRENT_APP_RUN != run_menu:
+                    CURRENT_APP_RUN = run_menu
+                    foreground_menu()
             else:
-                CURRENT_APP_RUN = run_menu
-                foreground_menu()
-        if(get_button(0) == 1):
-            set_rel_volume(+1)
-        if(get_button(0) == -1):
-            set_rel_volume(-1)
-        CURRENT_APP_RUN()
+                if(get_button(0) == 1):
+                    set_rel_volume(+1)
+                if(get_button(0) == -1):
+                    set_rel_volume(-1)
+                CURRENT_APP_RUN()
 
 main()
-- 
GitLab