Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
flow3r firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dos
flow3r firmware
Commits
08270b72
Commit
08270b72
authored
1 year ago
by
iggy
Committed by
q3k
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
added worms demo
parent
6d8449cd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
python_payload/demo_worms.py
+116
-0
116 additions, 0 deletions
python_payload/demo_worms.py
with
116 additions
and
0 deletions
python_payload/demo_worms.py
0 → 100644
+
116
−
0
View file @
08270b72
import
hardware
import
random
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
)
)
def
randrgb
():
return
((
random
.
random
(),
random
.
random
(),
random
.
random
()))
WIDTH
=
240
HEIGHT
=
240
#Define a few RGB (0.0 to 1.0) colors
BLACK
=
(
0
,
0
,
0
)
RED
=
(
1
,
0
,
0
)
GREEN
=
(
0
,
1
,
0
)
BLUE
=
(
0
,
0
,
1
)
WHITE
=
(
1
,
1
,
1
)
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
#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
(
0
,
0
,
WIDTH
,
HEIGHT
).
fill
()
#Write some text
ctx
.
move_to
(
WIDTH
/
2
,
HEIGHT
/
2
).
rgb
(
*
WHITE
).
text
(
"
Hi :)
"
)
hardware
.
display_update
()
class
Worm
():
def
__init__
(
self
):
self
.
color
=
randrgb
()
self
.
direction
=
random
.
random
()
*
math
.
pi
*
2
self
.
size
=
random
.
randint
(
5
,
30
)
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
(
WIDTH
/
2
+
self
.
x
-
self
.
size
/
2
,
HEIGHT
/
2
+
self
.
y
-
self
.
size
/
2
,
self
.
size
,
self
.
size
,
self
.
size
//
2
).
fill
()
def
mutate
(
self
):
self
.
size
+=
random
.
randint
(
-
3
,
4
)
self
.
color
=
([
max
(
0
,
min
(
1
,
x
+
(
random
.
random
()
*
0.1
-
0.05
)))
for
x
in
self
.
color
])
def
move
(
self
):
if
self
.
size
<
3
:
self
.
size
+=
1
if
self
.
size
>
30
:
self
.
size
-=
1
self
.
speed
=
min
(
3
,
self
.
size
/
4
)
#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
dist
=
math
.
sqrt
(
self
.
x
**
2
+
self
.
y
**
2
)
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
"
)
#Known problems:
#ctx.rotate(math.pi) turns the display black until powercycled
#ctx.clip() causes crashes
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment