summaryrefslogtreecommitdiffstats
path: root/twinkle.py
blob: a2cb3d2f5793c9c5ca7239b975a6e2232d37e377 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python2
"""Simple demo of twinkle lights."""
from __future__ import division
import color
import constants
#from DummyRig import DummyRig
from fish_render import *
import fish_render
from constants import *
import time
import random
try:
    import lumiversepython
except ImportError:
    have_lumiversepython = False
else:
    have_lumiversepython = True

def get_all_lights(rig):
    """Return a list of all panels."""
    return [
        [
            fish_render.Light(
                x, int(y == 'top'),
                rig.select('$side={y}[$sequence={x}]'.format(y=y, x=x))
            ) for y in ('top', 'bot')
        ] for x in xrange(1, 200)
    ]

def flip():
    """Return 0 with 99.9% prob. & 1 with .1% prob."""
    # should be black most of the time
    return random.random() < 0.0005


def twinkle(lights, rig):
    """Try to do some light things."""
    light_status = [
        {
            'is_increasing': True,
            'is_on': False,
            'change': random_change(),
            'light': light,
            'intensity': 0,
        }
            for light_col in lights for light in light_col
    ]
    shooting_stars = list()
    while True:
        if random.random() < 1:
            shooting_stars.append([random.randint(1, 180), random.random() < 0.5, 0])
        for light_s in light_status:
            is_increasing = light_s['is_increasing']
            is_on = light_s['is_on']
            change = light_s['change']
            light = light_s['light']
            intensity = light_s['intensity']
            # R and G are either 0 or 1
            if not is_on:
                if flip():
                    is_on = True
                    is_increasing = True
                    change = 1.5 / FRAME_RATE
                    light_s['is_on'] = is_on
                    light_s['is_increasing'] = is_increasing
                    light_s['change'] = change
                else:
                    continue
            intensity = clamp(0, intensity + change * (int(is_increasing) * 2 - 1), 1)
            light.light.setRGBRaw(intensity, intensity, intensity)
            light_s['intensity'] = intensity
            if intensity == 0:
                light_s['is_on'] = False
            if intensity == 1:
                light_s['is_increasing'] = False
        for i, star in enumerate(shooting_stars):
            star[2] += 1
            if star[2] > 10:
                for dx in range(-6, 7):
                    light_status[star[0] + dx]['light'].light.setRGBRaw(0, 0, 0)
                del shooting_stars[i]
                continue
            star[0] += int(star[1]) * 2 - 1
            for dx in range(-6, 7):
                intensity = 1 - 1 / 6 * abs(dx)
                light_status[star[0] + dx]['light'].light.setRGBRaw(intensity, intensity, intensity)
        rig.updateOnce()
        time.sleep(1 / FRAME_RATE)

def sunrise(lights, rig):
    #rig.select('$side={y}[$sequence={x}]'.format(y=y, x=x))
    rig.select("$side=top[$panel=27|$panel=28]")
    rig.updateOnce()
    time.sleep(1)


def main():
    """Run show."""
    if have_lumiversepython:
        rig = lumiversepython.Rig('/home/teacher/Lumiverse/PBridge.rig.json')
    else:
        rig = DummyRig(200, 2)
    rig.init()
    lights = get_all_lights(rig)
    #for light in lights:
    #    light.setRGBRaw(0, 0, 0)
    rig.updateOnce()
    time.sleep(3)
    twinkle(lights, rig)

if __name__ == '__main__':
    main()