Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
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
Container registry
Model registry
Operate
Environments
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
0c3955b5
Commit
0c3955b5
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
examples: Update conwaylife to work with new LCD API.
parent
21ca2d76
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/conwaylife.py
+10
-10
10 additions, 10 deletions
examples/conwaylife.py
examples/lcd.py
+0
-36
0 additions, 36 deletions
examples/lcd.py
examples/pyb.py
+37
-1
37 additions, 1 deletion
examples/pyb.py
with
47 additions
and
47 deletions
examples/conwaylife.py
+
10
−
10
View file @
0c3955b5
#import essential libraries
#import essential libraries
import
lcd
import
pyb
import
pyb
lcd
=
pyb
.
LCD
(
'
x
'
)
lcd
.
light
(
1
)
# do 1 iteration of Conway's Game of Life
# do 1 iteration of Conway's Game of Life
def
conway_step
():
def
conway_step
():
for
x
in
range
(
128
):
# loop over x coordinates
for
x
in
range
(
128
):
# loop over x coordinates
...
@@ -21,26 +23,24 @@ def conway_step():
...
@@ -21,26 +23,24 @@ def conway_step():
# apply the rules of life
# apply the rules of life
if
self
and
not
(
2
<=
num_neighbours
<=
3
):
if
self
and
not
(
2
<=
num_neighbours
<=
3
):
lcd
.
reset
(
x
,
y
)
# not enough, or too many neighbours: cell dies
lcd
.
pixel
(
x
,
y
,
0
)
# not enough, or too many neighbours: cell dies
elif
not
self
and
num_neighbours
==
3
:
elif
not
self
and
num_neighbours
==
3
:
lcd
.
set
(
x
,
y
)
# exactly 3 neigbours around an empty cell: cell is born
lcd
.
pixel
(
x
,
y
,
1
)
# exactly 3 neigbours around an empty cell: cell is born
# randomise the start
# randomise the start
def
conway_rand
():
def
conway_rand
():
lcd
.
clear
(
)
# clear the LCD
lcd
.
fill
(
0
)
# clear the LCD
for
x
in
range
(
128
):
# loop over x coordinates
for
x
in
range
(
128
):
# loop over x coordinates
for
y
in
range
(
32
):
# loop over y coordinates
for
y
in
range
(
32
):
# loop over y coordinates
if
pyb
.
rand
()
&
1
:
# get a 1-bit random number
lcd
.
pixel
(
x
,
y
,
pyb
.
rng
()
&
1
)
# set the pixel randomly
lcd
.
set
(
x
,
y
)
# set the pixel randomly
# loop for a certain number of frames, doing iterations of Conway's Game of Life
# loop for a certain number of frames, doing iterations of Conway's Game of Life
def
conway_go
(
num_frames
):
def
conway_go
(
num_frames
):
for
i
in
range
(
num_frames
):
for
i
in
range
(
num_frames
):
conway_step
()
# do 1 iteration
conway_step
()
# do 1 iteration
lcd
.
show
()
# update the LCD
lcd
.
show
()
# update the LCD
pyb
.
delay
(
30
0
)
pyb
.
delay
(
5
0
)
# PC testing
# testing
lcd
=
lcd
.
LCD
(
128
,
32
)
conway_rand
()
conway_rand
()
conway_go
(
100
0
)
conway_go
(
100
)
This diff is collapsed.
Click to expand it.
examples/lcd.py
deleted
100644 → 0
+
0
−
36
View file @
21ca2d76
# LCD testing object for PC
# uses double buffering
class
LCD
:
def
__init__
(
self
,
width
,
height
):
self
.
width
=
width
self
.
height
=
height
self
.
buf1
=
[[
0
for
x
in
range
(
self
.
width
)]
for
y
in
range
(
self
.
height
)]
self
.
buf2
=
[[
0
for
x
in
range
(
self
.
width
)]
for
y
in
range
(
self
.
height
)]
def
clear
(
self
):
for
y
in
range
(
self
.
height
):
for
x
in
range
(
self
.
width
):
self
.
buf1
[
y
][
x
]
=
self
.
buf2
[
y
][
x
]
=
0
def
show
(
self
):
print
(
''
)
# blank line to separate frames
for
y
in
range
(
self
.
height
):
for
x
in
range
(
self
.
width
):
self
.
buf1
[
y
][
x
]
=
self
.
buf2
[
y
][
x
]
for
y
in
range
(
self
.
height
):
row
=
''
.
join
([
'
*
'
if
self
.
buf1
[
y
][
x
]
else
'
'
for
x
in
range
(
self
.
width
)])
print
(
row
)
def
get
(
self
,
x
,
y
):
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
return
self
.
buf1
[
y
][
x
]
else
:
return
0
def
reset
(
self
,
x
,
y
):
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
self
.
buf2
[
y
][
x
]
=
0
def
set
(
self
,
x
,
y
):
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
self
.
buf2
[
y
][
x
]
=
1
This diff is collapsed.
Click to expand it.
examples/pyb.py
+
37
−
1
View file @
0c3955b5
...
@@ -6,8 +6,44 @@ def delay(n):
...
@@ -6,8 +6,44 @@ def delay(n):
pass
pass
rand_seed
=
1
rand_seed
=
1
def
r
and
():
def
r
ng
():
global
rand_seed
global
rand_seed
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
# for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure"
rand_seed
=
(
rand_seed
*
653276
)
%
8388593
rand_seed
=
(
rand_seed
*
653276
)
%
8388593
return
rand_seed
return
rand_seed
# LCD testing object for PC
# uses double buffering
class
LCD
:
def
__init__
(
self
,
port
):
self
.
width
=
128
self
.
height
=
32
self
.
buf1
=
[[
0
for
x
in
range
(
self
.
width
)]
for
y
in
range
(
self
.
height
)]
self
.
buf2
=
[[
0
for
x
in
range
(
self
.
width
)]
for
y
in
range
(
self
.
height
)]
def
light
(
self
,
value
):
pass
def
fill
(
self
,
value
):
for
y
in
range
(
self
.
height
):
for
x
in
range
(
self
.
width
):
self
.
buf1
[
y
][
x
]
=
self
.
buf2
[
y
][
x
]
=
value
def
show
(
self
):
print
(
''
)
# blank line to separate frames
for
y
in
range
(
self
.
height
):
for
x
in
range
(
self
.
width
):
self
.
buf1
[
y
][
x
]
=
self
.
buf2
[
y
][
x
]
for
y
in
range
(
self
.
height
):
row
=
''
.
join
([
'
*
'
if
self
.
buf1
[
y
][
x
]
else
'
'
for
x
in
range
(
self
.
width
)])
print
(
row
)
def
get
(
self
,
x
,
y
):
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
return
self
.
buf1
[
y
][
x
]
else
:
return
0
def
pixel
(
self
,
x
,
y
,
value
):
if
0
<=
x
<
self
.
width
and
0
<=
y
<
self
.
height
:
self
.
buf2
[
y
][
x
]
=
value
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