/
ledKnightRider.py
90 lines (79 loc) · 4.25 KB
/
ledKnightRider.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
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
#!/usr/bin/python
# ===========================================================================
# Hatalogico KNIGHT RIDER for 8 LEDs - powered by Adafruit's Libraries
# -------------------------------------------------
# Date: 12/4/2015
# Author: John Lumley
#
# BIG THANKS TO ADAFRUIT INDUSTRIES FOR MAKING THIS POSSIBLE AND EASY
# ===========================================================================
import time, os, sys
# DETERMINE CURRENT PATH
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(scriptPath)
# APPEND FOLDER OF REQUIRED LIBRARY
sys.path.append("Adafruit/Adafruit_PWM_Servo_Driver")
# FINALLY LOAD THE LIBRARY
from Adafruit_PWM_Servo_Driver import PWM
LED_PIN_0 = 1
LED_PIN_1 = 3
LED_PIN_2 = 5
LED_PIN_3 = 7
LED_PIN_4 = 9
LED_PIN_5 = 11
LED_PIN_6 = 13
LED_PIN_7 = 15
BRIGHT_STEP_0=0
BRIGHT_STEP_1=2048
BRIGHT_STEP_2=3070
BRIGHT_STEP_3=3500
BRIGHT_STEP_4=3800
BRIGHT_STEP_5=4048
BRIGHT_STEP_6=4095
BRIGHT_STEP_7=4095
# BUILD 16 ARRAYS OF 8 POSITIONS/VALUES FOR PWM LOOP
position = {}
position[0] = [ BRIGHT_STEP_0, BRIGHT_STEP_5, BRIGHT_STEP_6, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7 ]
position[1] = [ BRIGHT_STEP_0, BRIGHT_STEP_4, BRIGHT_STEP_5, BRIGHT_STEP_6, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7 ]
position[2] = [ BRIGHT_STEP_0, BRIGHT_STEP_3, BRIGHT_STEP_4, BRIGHT_STEP_5, BRIGHT_STEP_6, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7 ]
position[3] = [ BRIGHT_STEP_0, BRIGHT_STEP_2, BRIGHT_STEP_3, BRIGHT_STEP_4, BRIGHT_STEP_5, BRIGHT_STEP_6, BRIGHT_STEP_7, BRIGHT_STEP_7 ]
position[4] = [ BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2, BRIGHT_STEP_3, BRIGHT_STEP_4, BRIGHT_STEP_5, BRIGHT_STEP_6, BRIGHT_STEP_7 ]
position[5] = [ BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2, BRIGHT_STEP_3, BRIGHT_STEP_4, BRIGHT_STEP_5, BRIGHT_STEP_6 ]
position[6] = [ BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2, BRIGHT_STEP_3, BRIGHT_STEP_4, BRIGHT_STEP_5 ]
position[7] = [ BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2, BRIGHT_STEP_3, BRIGHT_STEP_4 ]
position[8] = [ BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2, BRIGHT_STEP_3 ]
position[9] = [ BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1, BRIGHT_STEP_2 ]
position[10] = [ BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0, BRIGHT_STEP_1 ]
position[11] = [ BRIGHT_STEP_7, BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_1, BRIGHT_STEP_0 ]
position[12] = [ BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_2, BRIGHT_STEP_0 ]
position[13] = [ BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_3, BRIGHT_STEP_0 ]
position[14] = [ BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_4, BRIGHT_STEP_0 ]
position[15] = [ BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_7, BRIGHT_STEP_6, BRIGHT_STEP_5, BRIGHT_STEP_0 ]
# SETUP THE PWM DRIVER (I2C ADDRESS IS 70 BY DEFAULT ON HATALOGICO)
# EACH CHANNEL RANGES FROM 0 (off) TO 4095 (on)
pwm = PWM(0x70)
# SET FREQUENCY
pwm.setPWMFreq(120)
while (True):
for posNo in range(0, 16, +1):
pwm.setPWM(LED_PIN_0, 0, position[ posNo ][0])
pwm.setPWM(LED_PIN_1, 0, position[ posNo ][1])
pwm.setPWM(LED_PIN_2, 0, position[ posNo ][2])
pwm.setPWM(LED_PIN_3, 0, position[ posNo ][3])
pwm.setPWM(LED_PIN_4, 0, position[ posNo ][4])
pwm.setPWM(LED_PIN_5, 0, position[ posNo ][5])
pwm.setPWM(LED_PIN_6, 0, position[ posNo ][6])
pwm.setPWM(LED_PIN_7, 0, position[ posNo ][7])
# HAVE A LITTLE NAP
time.sleep(0.05)
for posNo in range(15, -1, -1):
pwm.setPWM(LED_PIN_0, 0, position[ posNo ][0])
pwm.setPWM(LED_PIN_1, 0, position[ posNo ][1])
pwm.setPWM(LED_PIN_2, 0, position[ posNo ][2])
pwm.setPWM(LED_PIN_3, 0, position[ posNo ][3])
pwm.setPWM(LED_PIN_4, 0, position[ posNo ][4])
pwm.setPWM(LED_PIN_5, 0, position[ posNo ][5])
pwm.setPWM(LED_PIN_6, 0, position[ posNo ][6])
pwm.setPWM(LED_PIN_7, 0, position[ posNo ][7])
# HAVE A LITTLE NAP
time.sleep(0.05)