cloud_laser/main.py

143 lines
3.3 KiB
Python
Raw Normal View History

import stupid_artnet.StupidArtnet as artnet
import time
import yaml
import argparse
from voluptuous import Schema, Required
import random
from dataclasses import dataclass
CONFIG_SCHEME = Schema(
{
Required('target'): str,
Required('universe'): int,
Required('min_sleep'): float,
Required('max_sleep'): float,
Required('positions'): list
}
)
COLORS = {'red': 40, 'green': 80, 'yellow': 1, "pattern": 120, "magic": 160}
@dataclass(frozen=True)
class Cloud:
x: int
y: int
color: str
def laser_on(artnet):
artnet.set_single_value(10, 0)
artnet.show()
def laser_off(artnet):
artnet.set_single_value(10, 17)
artnet.show()
def set_mode(artnet, mode):
artnet.set_single_value(1, mode)
def set_color(artnet, color):
artnet.set_single_value(8, color)
def set_pos(artnet, pos):
artnet.set_single_value(5, pos[0])
artnet.set_single_value(6, pos[1])
def intTryParse(value):
try:
return int(value), True
except ValueError:
return value, False
def load_config(config_path):
with open(config_path, "r") as f:
data = yaml.safe_load(f)
# validate config
CONFIG_SCHEME(data)
positions = list()
for p in data['positions']:
positions.append(Cloud(x = p['x'], y = p['y'], color=p['color']))
data['positions'] = positions
return data
def interactive(an, config):
import keyboard
x = 0
y = 0
color_keys = list(COLORS.keys())
c = 0
set_color(an, COLORS[color_keys[c]])
print_pos = lambda x,y: print(f"x:{x} y:{y}")
while True:
if keyboard.is_pressed("w"):
x = x + 1
print_pos(x,y)
if keyboard.is_pressed("s"):
x = x - 1
print_pos(x,y)
if keyboard.is_pressed("a"):
y = y - 1
print_pos(x,y)
if keyboard.is_pressed("d"):
y = y + 1
print_pos(x,y)
if keyboard.is_pressed(" "):
pass
#save point in config file
if keyboard.is_pressed("c"):
c = c + 1
if c >= len(color_keys):
c = 0
set_color(an, COLORS[color_keys[c]])
set_pos(an, (x, y))
time.sleep(0.05)
def main(args):
config = load_config(args.config)
# setup laser
an = artnet.StupidArtnet(targetIP=config['target'], universe = config['universe'])
an.start()
set_mode(an, 0xFF)
if args.interactive:
interactive(an, config)
positions = config['positions']
# loop through colors
while True:
p = random.choice(positions)
# exclude current position from next choises
positions = list(set(config['positions']) - set([p]))
# set position and choose color
set_pos(an, (p.x, p.y))
if p.color == 'random':
c = random.choice(list(COLORS.values()))
else:
c = COLORS.get(p.color)
set_color(an, c)
# wait random amount of times
sleep_time = config['min_sleep'] + (config['max_sleep'] - config['min_sleep']) * random.uniform(0, 1)
time.sleep(sleep_time)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config")
parser.add_argument("--interactive", action="store_true")
args = parser.parse_args()
main(args)