Browse Source

add outline mode

Thomas Buck 1 year ago
parent
commit
88a7c2b5c2
1 changed files with 58 additions and 21 deletions
  1. 58
    21
      CatToy.py

+ 58
- 21
CatToy.py View File

@@ -4,13 +4,17 @@ from toy import Toy
4 4
 import random
5 5
 from machine import Timer
6 6
 
7
-max_laser_power = 0.15
7
+max_laser_power = 0.1
8 8
 
9 9
 limits = [
10 10
     # pan_min, pan_max, tilt_min, tilt_max, name
11 11
     (84, 120, 53, 76, 'office desk, front right')
12 12
 ]
13 13
 
14
+timerRunning = False
15
+timerData = None
16
+outlineIndex = 0
17
+
14 18
 def buildPage(header, footer):
15 19
     html = """<!DOCTYPE html>
16 20
     <html>
@@ -47,7 +51,9 @@ def buildPage(header, footer):
47 51
                 </select><br>
48 52
                 Steps: <input type="text" name="steps" value="100"><br>
49 53
                 Duration: <input type="text" name="duration" value="1000">ms<br>
50
-                <input type="submit" name="s" value="Start Program">
54
+                <input type="submit" name="s" value="Random">
55
+                <input type="submit" name="s" value="Outline"><br>
56
+                Status: %s
51 57
             </form>
52 58
             %s
53 59
         </body>
@@ -60,7 +66,11 @@ def buildPage(header, footer):
60 66
         sl += '<option value="' + val + '">' + name + '</option>'
61 67
     sl += '<option value="">None</option>'
62 68
 
63
-    page = html % (header, int(max_laser_power * 100.0), sl, footer)
69
+    status = "No program running"
70
+    if timerRunning:
71
+        status = "Program in progress"
72
+
73
+    page = html % (header, int(max_laser_power * 100.0), sl, status, footer)
64 74
     return page
65 75
 
66 76
 random.seed()
@@ -145,8 +155,19 @@ def doMove(pan_min, pan_max, tilt_min, tilt_max, dur):
145 155
     t.angle(t.tilt, tilt)
146 156
     t.angle(t.pan, pan)
147 157
 
148
-timerRunning = False
149
-timerData = None
158
+def doOutline(pan_min, pan_max, tilt_min, tilt_max, dur):
159
+    global outlineIndex
160
+    points = [
161
+        (pan_min, tilt_min),
162
+        (pan_min, tilt_max),
163
+        (pan_max, tilt_max),
164
+        (pan_max, tilt_min)
165
+    ]
166
+    outlineIndex = (outlineIndex + 1) % 4
167
+    pan, tilt = points[outlineIndex]
168
+    print("outline move: tilt={} pan={} duration={}".format(tilt, pan, dur))
169
+    t.angle(t.tilt, tilt)
170
+    t.angle(t.pan, pan)
150 171
 
151 172
 def timerCallback(unused):
152 173
     global timerRunning, timerData
@@ -154,27 +175,34 @@ def timerCallback(unused):
154 175
     if not timerRunning:
155 176
         return
156 177
 
157
-    pan_min, pan_max, tilt_min, tilt_max, steps, duration = timerData
178
+    pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline = timerData
158 179
 
159 180
     dur = duration
160
-    if dur < 200:
161
-        dur = random.randint(200, 2000)
181
+    if not outline:
182
+        if dur < 200:
183
+            dur = random.randint(200, 2000)
184
+        else:
185
+            dur = random.randint(200, duration)
162 186
     else:
163
-        dur = random.randint(200, duration)
187
+        if dur < 200:
188
+            dur = 500
164 189
 
165 190
     if steps > 0:
166 191
         steps -= 1
167
-        doMove(pan_min, pan_max, tilt_min, tilt_max, dur)
192
+        if not outline:
193
+            doMove(pan_min, pan_max, tilt_min, tilt_max, dur)
194
+        else:
195
+            doOutline(pan_min, pan_max, tilt_min, tilt_max, dur)
168 196
         tim = Timer(period = dur, mode=Timer.ONE_SHOT, callback = timerCallback)
169 197
     else:
170 198
         timerRunning = False
171 199
         t.laser(0.0)
172 200
 
173
-    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration)
201
+    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline)
174 202
 
175
-def startRepeat(pan_min, pan_max, tilt_min, tilt_max, steps, duration):
203
+def startRepeat(pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline):
176 204
     global timerRunning, timerData
177
-    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration)
205
+    timerData = (pan_min, pan_max, tilt_min, tilt_max, steps, duration, outline)
178 206
 
179 207
     if not timerRunning:
180 208
         timerRunning = True
@@ -188,22 +216,26 @@ def stopRepeat():
188 216
 
189 217
 def repeatCallback(request):
190 218
     q = request.find("/repeat?")
191
-    pl = request.find("limit=")
192
-    ps = request.find("steps=")
193
-    pd = request.find("duration=")
194
-    if (q < 0) or (pl < 0) or (ps < 0) or (pd < 0):
195
-        print("repeat query error: q={} pl={} ps={} pd={}".format(q, pl, ps, pd))
219
+    pl = request.find("limit=", q)
220
+    ps = request.find("steps=", pl)
221
+    pd = request.find("duration=", ps)
222
+    pp = request.find("s=", pd)
223
+    if (q < 0) or (pl < 0) or (ps < 0) or (pd < 0) or (pp < 0):
224
+        print("repeat query error: q={} pl={} ps={} pd={} pp={}".format(q, pl, ps, pd, pp))
196 225
         return buildPage(
197 226
             '<p>Error: no repeat arguments found in URL query string.</p>',
198 227
             '<p><a href="/">Back to main page</a></p>'
199 228
         )
200 229
 
201
-    data = [("limit=", pl), ("steps=", ps), ("duration=", pd)]
230
+    data = [("limit=", pl), ("steps=", ps), ("duration=", pd), ("s=", pp)]
202 231
     result = []
203 232
     for s, p in data:
233
+        #print(p)
204 234
         pe = request.find("&", p)
235
+        #print(pe)
205 236
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
206 237
             pe = request.find(" HTTP", p)
238
+            #print(pe)
207 239
         if (pe < 0) or (p + len(s) > pe) or (pe - (p + 3) > 40):
208 240
             print("repeat query error: p={} pe={}".format(p, pe))
209 241
             return buildPage(
@@ -212,8 +244,9 @@ def repeatCallback(request):
212 244
             )
213 245
         r = request[p + len(s) : pe]
214 246
         result.append(r)
247
+        #print()
215 248
 
216
-    print("repeat: limit={} steps={} duration={}".format(result[0], result[1], result[2]))
249
+    print("repeat: limit={} steps={} duration={} s={}".format(result[0], result[1], result[2], result[3]))
217 250
 
218 251
     if len(result[0]) == 0:
219 252
         stopRepeat()
@@ -222,10 +255,14 @@ def repeatCallback(request):
222 255
             '<p><a href="/">Back to main page</a></p>'
223 256
         )
224 257
 
258
+    outline = False
259
+    if result[3].lower() == "outline":
260
+        outline = True
261
+
225 262
     for pan_min, pan_max, tilt_min, tilt_max, name in limits:
226 263
         val = name.replace(' ', '_').replace(',', '').lower()
227 264
         if result[0] == val:
228
-            startRepeat(pan_min, pan_max, tilt_min, tilt_max, int(result[1]), int(result[2]))
265
+            startRepeat(pan_min, pan_max, tilt_min, tilt_max, int(result[1]), int(result[2]), outline)
229 266
             break
230 267
 
231 268
     return buildPage(

Loading…
Cancel
Save