Skip to content

Commit 54b2452

Browse files
author
Robert van der Tuuk
committed
removed globals, they were an irritation for to long
1 parent 0c8a14a commit 54b2452

File tree

7 files changed

+135
-123
lines changed

7 files changed

+135
-123
lines changed

Ledart/MatrixSim/Interfaces/PygameInterface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, width, height, blocksize, fullscreen=False):
1414
self.flags |= pygame.FULLSCREEN
1515
self.window = pygame.display.set_mode((self.width, self.height),
1616
self.flags)
17-
if blocksize < 4:
17+
if blocksize < 7:
1818
self.pixelsurface = pygame.Surface((blocksize, blocksize))
1919
else:
2020
self.pixelsurface = pygame.Surface((blocksize - 2, blocksize - 2))

Ledart/Patterns/MetaBalls.py

Lines changed: 18 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,9 @@
11
from Ledart import Graphics, BLACK, BLUE, RED, WHITE
22
from Ledart import translate
3-
from colorsys import hsv_to_rgb
4-
import operator
5-
import random
6-
import math
7-
8-
class Vector(object):
9-
def __init__(self, **axes):
10-
super(Vector, self).__setattr__('axes', axes)
11-
12-
def magnitude(self):
13-
""" for every N square it, sum list. take square root. """
14-
magnitude = sum([(self.axes[axis] * self.axes[axis]) for axis in self.axes]) ** 0.5
15-
return magnitude
16-
17-
def set_mag(self, mag):
18-
self = self.normalize()
19-
self *= mag
20-
21-
def dist(self, other):
22-
deltas = [(self.axes[axis] - other.axes[axis]) ** 2 for axis in self.axes]
23-
return abs(sum(deltas)) ** 0.5
24-
25-
def normalize(self):
26-
return self.__div__(self.magnitude())
27-
28-
def limit(self, lim):
29-
pass
30-
31-
def heading(self):
32-
pass
33-
34-
def rotate(self):
35-
pass
36-
37-
def lerp(self):
38-
pass
39-
40-
def angleBetween(self):
41-
pass
42-
43-
def dot(self):
44-
pass
45-
46-
def cross(self):
47-
pass
48-
49-
def randomVec(self):
50-
pass
51-
52-
def __operation__(self, op, val):
53-
result = {}
54-
if isinstance(val, (int, float)):
55-
for axis in self.axes:
56-
result[axis] = op(self.axes[axis], val)
57-
elif isinstance(val, Vector):
58-
for axis in self.axes:
59-
result[axis] = op(self.axes[axis], val.axes[axis])
60-
else:
61-
raise TypeError
62-
return Vector(**result)
63-
64-
def __add__(self, val):
65-
return self.__operation__(operator.add, val)
66-
67-
def __sub__(self, val):
68-
return self.__operation__(operator.sub, val)
69-
70-
def __mul__(self, val):
71-
return self.__operation__(operator.mul, val)
72-
73-
def __div__(self, val):
74-
return self.__operation__(operator.div, val)
75-
76-
def __len__(self):
77-
return self.mag()
78-
79-
def __getattr__(self, attr):
80-
if attr in self.axes:
81-
return self.axes[attr]
82-
elif attr == 'mag':
83-
return self.magnitude()
84-
else:
85-
raise AttributeError
86-
87-
def __setattr__(self, attr, value):
88-
if attr in self.axes:
89-
self.axes[attr] = value
90-
elif attr == 'mag':
91-
self.set_mag(value)
92-
else:
93-
raise KeyError
94-
95-
def __str__(self):
96-
values = {}
97-
values.update(self.axes)
98-
values['mag'] = self.mag
99-
return str(values)
1003

4+
from colorsys import *
5+
from Ledart import Vector
6+
import random
1017

1028
class Ball(object):
1039
def __init__(self, g=None, x=0, y=0, size=1):
@@ -106,11 +12,12 @@ def __init__(self, g=None, x=0, y=0, size=1):
10612
self.pos = Vector(x=x, y=y)
10713
self.size = size
10814
self.g = g
109-
self.mass = self.size
15+
self.mass = self.size ** 6 / 2
11016
self.max_speed = 0
11117

11218
def update(self):
11319
self.speed += self.accel
20+
self.speed *= 0.998
11421
self.pos += self.speed
11522
self.accel *= 0
11623

@@ -129,7 +36,7 @@ def handle_collision(self):
12936
self.speed.y *= -1
13037

13138
def draw(self):
132-
self.g.draw_circle(self.pos.x, self.pos.y, self.size, BLUE)
39+
self.g.draw_circle(self.pos.x, self.pos.y, self.size, BLACK)
13340

13441

13542
class MetaBalls(Graphics):
@@ -144,6 +51,9 @@ def __init__(self, **kwargs):
14451
# ball.speed += random.randint(20, 80) / 100.
14552
self.balls.append(ball)
14653
self.max = 0
54+
self.totalballsize = 0
55+
for ball in self.balls:
56+
self.totalballsize += ball.size
14757

14858
def generate(self):
14959
self.fill(BLACK)
@@ -156,19 +66,22 @@ def generate(self):
15666
distance = (ball.pos.dist(Vector(x=x, y=y)) + 1)
15767
sumc += ball.size / distance
15868

159-
color = min(sumc * 80, 0xff) / 0xff
160-
self[point] = [int(c * 0xff) for c in hsv_to_rgb(color, 1, 1)]
69+
# color = min(sumc * 80, 0xff) / 0xff
70+
# print(sumc)
71+
colorfunc = hsv_to_rgb
72+
color = min(sumc * (0x80 - self.totalballsize), 0xff) / 0xff
73+
self[point] = [int(c * 0xff) for c in colorfunc(1 - color, 1, 1)]
74+
16175

16276
for ball in self.balls:
16377
ball.update()
164-
ball.draw()
78+
# ball.draw()
16579
for otherball in self.balls:
166-
if ball != otherball:
80+
if ball != otherball: #and ball.pos.dist(otherball.pos) < self.width / 4:
16781
force = ball.pos - otherball.pos
16882
dist = max(force.mag, ball.size)
16983
force = force.normalize()
170-
G = 0.01
84+
G = 6.67191*(10**-11)
17185
strength = (G * ball.mass * otherball.mass) / (dist ** 2)
17286
force *= strength
17387
ball.accel -= force
174-
# print(ball.speed)

Ledart/Tools/Vector.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import operator
2+
import math
3+
4+
5+
class Vector(object):
6+
def __init__(self, **axes):
7+
super(Vector, self).__setattr__('axes', axes)
8+
9+
def magnitude(self):
10+
""" for every N square it, sum list. take square root. """
11+
magnitude = sum([(self.axes[axis] * self.axes[axis]) for axis in self.axes]) ** 0.5
12+
return magnitude
13+
14+
def set_mag(self, mag):
15+
self = self.normalize()
16+
self *= mag
17+
18+
def dist(self, other):
19+
deltas = [(self.axes[axis] - other.axes[axis]) ** 2 for axis in self.axes]
20+
return abs(sum(deltas)) ** 0.5
21+
22+
def normalize(self):
23+
m = self.magnitude()
24+
if m:
25+
return self / m
26+
27+
return self * 0
28+
29+
def limit(self, lim):
30+
if self.mag > lim:
31+
self = self.normalize()
32+
self *= lim
33+
return Vector(**self.axes)
34+
35+
def heading(self):
36+
pass
37+
38+
def rotate(self):
39+
pass
40+
41+
def lerp(self):
42+
pass
43+
44+
def angleBetween(self):
45+
pass
46+
47+
def dot(self):
48+
pass
49+
50+
def cross(self):
51+
pass
52+
53+
def randomVec(self):
54+
pass
55+
56+
def __operation__(self, op, val):
57+
result = {}
58+
if isinstance(val, (int, float)):
59+
for axis in self.axes:
60+
result[axis] = op(self.axes[axis], val)
61+
elif isinstance(val, Vector):
62+
for axis in self.axes:
63+
result[axis] = op(self.axes[axis], val.axes[axis])
64+
else:
65+
raise TypeError
66+
return Vector(**result)
67+
68+
def __add__(self, val):
69+
return self.__operation__(operator.add, val)
70+
71+
def __sub__(self, val):
72+
return self.__operation__(operator.sub, val)
73+
74+
def __mul__(self, val):
75+
return self.__operation__(operator.mul, val)
76+
77+
def __div__(self, val):
78+
return self.__operation__(operator.div, val)
79+
80+
def __len__(self):
81+
return self.mag
82+
83+
def __getattr__(self, attr):
84+
if attr in self.axes:
85+
return self.axes[attr]
86+
elif attr == 'mag':
87+
return self.magnitude()
88+
else:
89+
raise AttributeError
90+
91+
def __setattr__(self, attr, value):
92+
if attr in self.axes:
93+
self.axes[attr] = value
94+
elif attr == 'mag':
95+
self.set_mag(value)
96+
else:
97+
raise KeyError
98+
99+
def __str__(self):
100+
values = {}
101+
values.update(self.axes)
102+
values['mag'] = self.mag
103+
return str(values)

Ledart/Tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from Ledart.Tools.NumberSegmentBitMap import *
44
from Ledart.Tools.Palet import *
55
from Ledart.Tools.Timing import *
6-
6+
from Ledart.Tools.Vector import *

Ledart/configs/pattern_conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# protocol = LegacyLmcp(dispmode=rgb24)
2525

2626
matrixsim = MatrixScreen(dims=dims,
27-
pixelsize=1,
27+
pixelsize=4,
2828
fullscreen=False,
2929
interface=interface_opts["pygame"])
3030

@@ -49,7 +49,7 @@
4949
# dest: DisplayImage(dims=dims, fname='/home/duality/Pictures/System-Shock-2.jpg'),
5050
# dest: VideoPlay(dims=dims, fname='/home/duality/Videos/bad-noshadow.mp4'),
5151
# dest: VideoPlay(dims=dims, fname='/home/duality/Videos/bad.mkv'),
52-
dest: CamCapture(dims=dims),
52+
# dest: CamCapture(dims=dims),
5353
# dest: Water(dims=dims),
5454
# dest: AliasedWPlasma(dims=dims),
5555
# dest: AliasedFire(dims=dims),
@@ -72,7 +72,7 @@
7272
# dest: GraphicsPixelTest(dims=dims),
7373
# dest: GraphicsDotTest(dims=dims),
7474
# dest: SpiroGraph(dims=dims),
75-
# dest: MetaBalls(dims=dims),
75+
dest: MetaBalls(dims=dims),
7676
# dest: VUmeter(dims=dims)
7777

7878
# dest: Fft(dims=dims, mode=1),

Ledart/runPatternJob.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from utils import matrix
2020

2121
basepath = os.path.dirname(os.path.realpath(__file__))
22-
protocol, matrixscreen, targets = None, None, None
2322

2423

2524
def tst_patterns(directory, showpass=False):
@@ -56,7 +55,7 @@ def listpatterns():
5655
print(pattern)
5756

5857

59-
def sendout(args, targets, protocol):
58+
def sendout(args, targets, protocol, matrixsim):
6059
# sendout function that sends out data to the networked devices and
6160
# also to the matrix screen simulator if enabled.
6261
# or only to the matrix simulator if no pattern is selected.
@@ -65,9 +64,9 @@ def sendout(args, targets, protocol):
6564
pattern = targets[t]
6665
# generate the next set of images to send.
6766
pattern.generate()
68-
if matrixscreen:
69-
matrixscreen.handleinput()
70-
matrixscreen.process(pattern)
67+
if matrixsim:
68+
matrixsim.handleinput()
69+
matrixsim.process(pattern)
7170
if args.sendOnChange:
7271
changed = (Surface(pattern) != Surface(sendout.previous))
7372
sendout.previous = Surface(pattern)
@@ -121,10 +120,7 @@ def main():
121120
cleanup(4)
122121
else:
123122
# load config
124-
global targets
125-
global protocol
126-
global matrixscreen
127-
targets, protocol, matrixscreen = load_targets(args.config)
123+
targets, protocol, matrixsim = load_targets(args.config)
128124

129125
# check if there is anything configured.
130126
if not len(targets):
@@ -160,9 +156,9 @@ def main():
160156
sys.stdout.write(fmtstr % fmt)
161157
sys.stdout.flush()
162158
if args.fps > 0:
163-
sendout(args, targets, protocol)
159+
sendout(args, targets, protocol, matrixsim)
164160
time.sleep(abs(fps))
165161
# else send everything out as fast as possible
166162
else:
167-
sendout(args, targets, protocol)
163+
sendout(args, targets, protocol, matrixsim)
168164
previousTime = currentTime

Ledart/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ def load_targets(configfile):
135135

136136
targets = variables.get('targets', None)
137137
protocol = variables.get('protocol', None)
138-
matrix_sim = variables.get('matrixsim', None)
138+
matrixsim = variables.get('matrixsim', None)
139139

140140
# print("configfile: %s" % configfile)
141141
# print("protocol: %s" % protocol)
142142
# print("matrixsim: %s" % matrix_sim)
143143
# print("targets: %s" % (str(targets)))
144144

145-
return (targets, protocol, matrix_sim)
145+
return (targets, protocol, matrixsim)
146146

147147
def checkList(first, second):
148148
for item1, item2 in zip(first, second):

0 commit comments

Comments
 (0)