forked from jkemble/radiopixel-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
134 lines (116 loc) · 3.87 KB
/
Player.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Player.h"
void Player::SetSequence( Sequence *_sequence )
{
if ( sequence != _sequence )
{
sequence = _sequence;
step = sequence->Reset( );
stepTime = millis( );
}
}
void Player::AdvanceSequence( )
{
if ( !sequence)
{
return;
}
step = sequence->Advance( step );
stepTime = millis( );
}
bool Player::GetCommand( RadioPixel::Command *command )
{
command->command = HC_PATTERN;
command->brightness = sequence->GetBrightness( step );
command->speed = speed;
command->pattern = patternId;
command->color[ 0 ] = pattern->color( 0 );
command->color[ 1 ] = pattern->color( 1 );
command->color[ 2 ] = pattern->color( 2 );
command->level[ 0 ] = pattern->level( 0 );
command->level[ 1 ] = pattern->level( 1 );
command->level[ 2 ] = pattern->level( 2 );
return true;
}
bool Player::UpdatePattern( time_t now, Stripper *strip )
{
if ( !sequence )
{
return false;
}
// move to the next command if needed
if ( ( sequence->GetDuration( step ) > 0 ) &&
( now >= ( stepTime + sequence->GetDuration( step ) ) ) )
{
step = sequence->Advance( step, true );
stepTime = now;
}
// update the pattern to match the command
bool changed = false;
switch ( sequence->GetCommand( step ) )
{
case HC_PATTERN:
if ( !pattern ||
sequence->GetPatternId( step ) != patternId ||
sequence->GetColors( step, 0 ) != pattern->color( 0 ) ||
sequence->GetColors( step, 1 ) != pattern->color( 1 ) ||
sequence->GetColors( step, 2 ) != pattern->color( 2 ) ||
sequence->GetLevels( step, 0 ) != pattern->level( 0 ) ||
sequence->GetLevels( step, 1 ) != pattern->level( 1 ) ||
sequence->GetLevels( step, 2 ) != pattern->level( 2 ) )
{
#ifdef DEBUG
Serial.print( F("changing pattern to "));
Serial.println( sequence->GetPatternId( step ));
#endif
delete pattern;
patternId = sequence->GetPatternId( step );
pattern = CreatePattern( patternId );
time_t duration( pattern->GetDuration( strip ) );
time_t offset = ( now * speed / 100 ) % duration;
uint32_t colors[ 3 ];
colors[ 0 ] = sequence->GetColors( step, 0 );
colors[ 1 ] = sequence->GetColors( step, 1 );
colors[ 2 ] = sequence->GetColors( step, 2 );
uint8_t levels[ 3 ];
levels[ 0 ] = sequence->GetLevels( step, 0 );
levels[ 1 ] = sequence->GetLevels( step, 1 );
levels[ 2 ] = sequence->GetLevels( step, 2 );
pattern->Init( strip, colors, levels, offset );
strip->show();
changed = true;
strip->setBrightness( sequence->GetBrightness( step ) );
speed = sequence->GetSpeed( step );
}
break;
case HC_CONTROL:
strip->setBrightness( sequence->GetBrightness( step ) );
speed = sequence->GetSpeed( step );
changed = true;
break;
case HC_NONE:
default:
break;
}
return changed;
}
void Player::UpdateStrip( time_t now, Stripper *strip )
{
// update the strip if it's time
if ( pattern && strip && ( ( now - lastUpdate ) > FRAME_MS ) )
{
time_t duration( pattern->GetDuration( strip ) );
time_t offset = ( ( now - stepTime ) * speed / 100 ) % duration;
unsigned long count = ( ( now - stepTime ) * speed / 100 ) / duration;
unsigned long lastCount = ( ( lastUpdate - stepTime ) * speed / 100 ) / duration;
if ( count != lastCount )
{
pattern->Loop( strip, offset );
}
else
{
pattern->Update( strip, offset );
}
strip->show();
lastUpdate = now;
}
}