-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxmas.py
61 lines (48 loc) · 1.55 KB
/
xmas.py
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
import RPi.GPIO as GPIO
import time
import pygame
import random
from light_functions import Pin, pins, pin_numbers, relay_numbers, relay_pin_map, on, off
def russian_xmas():
# GPIO
# Turn off warnings
GPIO.setwarnings(False)
# # Set pin mapping to board, use GPIO numbers not pin numbers
GPIO.setmode(GPIO.BCM)
# # Prepare the pins for output
# "any" function executes the function on the iterator but doesn't return anything
# This is the same as "for pin in pins: GPIO.setup"
any(GPIO.setup(pin, GPIO.OUT) for pin in pin_numbers)
# Open the input sequnce file and read/parse it
with open("sequence_file.txt",'r') as f:
seq_data = f.readlines()
for i in range(len(seq_data)):
seq_data[i] = seq_data[i].rstrip()
# Load and play the music
pygame.mixer.init()
pygame.mixer.music.load("xmas.mp3")
pygame.mixer.music.play()
# Loop through the sequence and turn pins on/off
start_time = int(round(time.time()*1000))
step = 0
while True :
next_step = seq_data[step].split(",");
timestamp, pin, state = [int(num.rstrip()) for num in next_step]
current_time = int(round(time.time()*1000)) - start_time
# time to run the command
if timestamp <= current_time:
# if the command is Relay 1-6
if 1 <= pin <= 6:
# turn the pin on or off depending on the state
if state:
on(relay_pin_map[pin])
else:
off(relay_pin_map[pin])
step += 1
# If last step in sequence
if step == len(seq_data):
# if time == 279800:
GPIO.output(pin_map[logical_map[i]],False)
break
if __name__ == '__main__':
russian_xmas()