package POEDaemon::TCPServer::Main::Periodic::WS2801; use strict; use warnings FATAL => 'all'; no warnings 'redefine'; use POE; use POEDaemon; sub states { return $_[0], [qw( tcpserver_main_periodic_ws2801_start tcpserver_main_periodic_ws2801_send_data )]; } sub tcpserver_main_periodic_ws2801_start { my ($kernel, $heap) = @_[KERNEL, HEAP]; #$heap->{periodic}->{tcpserver_main_periodic_ws2801_periodic3s_send_data} = 3; $kernel->yield('tcpserver_main_periodic_ws2801_send_data'); } sub tcpserver_main_periodic_ws2801_send_data { my ($kernel, $heap, $state) = @_[KERNEL, HEAP, STATE]; foreach my $wheel_id (keys %{$heap->{tcpserver}->{connections}}) { my $client = $heap->{tcpserver}->{connections}->{$wheel_id}; next unless $client; next unless $client->{poeclient} && $client->{poeclient_pins} && $client->{poeclient_pins}->{out_ws2801}; $heap->{ws2801_colorchanger} = {} unless exists $heap->{ws2801_colorchanger}; my $colors = $heap->{ws2801_colorchanger}; my $value_min = 0; my $value_max = 255; $colors->{value} = $value_min unless defined $colors->{value}; $colors->{inc} = 1 unless defined $colors->{inc}; $colors->{curr_cval} = 0 unless defined $colors->{curr_cval}; $colors->{cvalues} = { 0 => $value_min, 1 => $value_min, 2 => $value_min } unless defined $colors->{cvalues}; my $cvalues = $colors->{cvalues}; my $step_up = 1; my $step_down = 1; #if ($cvalues->{$colors->{curr_cval}} == $value_max) #if ($cvalues->{$colors->{curr_cval}} == 127) #if ($cvalues->{$colors->{curr_cval}} == int($value_max / 2)) if ($cvalues->{$colors->{curr_cval}} == $value_min || $cvalues->{$colors->{curr_cval}} == int($value_max / 2) || $cvalues->{$colors->{curr_cval}} == $value_max) #if ($cvalues->{$colors->{curr_cval}} == $value_min) #if ($cvalues->{$colors->{curr_cval}} =~ /^(?:127|255)$/) #if ($cvalues->{$colors->{curr_cval}} =~ /^(?:0|127|255)$/) #if ($cvalues->{$colors->{curr_cval}} == int(rand() * 256)) { $colors->{inc} = int(rand() * 2); #$colors->{curr_cval}++; $colors->{curr_cval} = int(rand() * 3); #if ($colors->{curr_cval} > 2) #{ # $colors->{curr_cval} = 0; #} } if ($colors->{inc}) { $colors->{value} += $step_up; $cvalues->{$colors->{curr_cval}} += $step_up; } else { $colors->{value} -= $step_down; $cvalues->{$colors->{curr_cval}} -= $step_up; } if ($colors->{value} < $value_min) { $colors->{value} = $value_min; } elsif ($colors->{value} > $value_max) { $colors->{value} = $value_max; } if ($cvalues->{$colors->{curr_cval}} < $value_min) { $cvalues->{$colors->{curr_cval}} = $value_min; } elsif ($cvalues->{$colors->{curr_cval}} > $value_max) { $cvalues->{$colors->{curr_cval}} = $value_max; } if ($cvalues->{$colors->{curr_cval}} == $value_min) { $colors->{inc} = 1; } elsif ($cvalues->{$colors->{curr_cval}} == $value_max) { $colors->{inc} = 0; } my $element_count = 50; #my $element_count = 1; my @args; foreach (1 .. $element_count) { #my $arg = join ',', # int rand 2, # int rand 2, # int rand 2; # # #push @args, $arg eq '0,0,0' ? '1,1,1' : $arg; push @args, '0,0,0'; } my $rand_rgb = join ',', int rand 2, int rand 2, int rand 2; #my $rand_rgb = join ',', # int rand 256, # int rand 256, # int rand 256; #my $rand_rgb = join ',', # int rand 256, # int rand 256, # int rand 8; #my $rand_rgb = join ',', # int rand 256, # int rand 256, # int rand 256; #my $rand_rgb = join ',', # int rand 256, # int rand 256, # int rand 127; #my $rand_rgb = join ',', # #int(rand() * $value_max), # #$value_max, # #$colors->{value}, # $cvalues->{0}, # #int(rand() * $value_max), # #$value_max, # #$colors->{value}, # $cvalues->{1}, # #int(rand() * $value_max); # #$value_max; # #$colors->{value}; # $cvalues->{2}; $args[int rand $element_count] = $rand_rgb; #$args[0] = $rand_rgb; my $data = join ' ', @args; #log_enabled && logline "data = %s", $data; my $output = sprintf "%s ws2801 %s", POESERVER_CMD_PREFIX, $data; #next; $kernel->yield(tcpserver_output => { wheel_id => $wheel_id, output => $output, }); } unless ($heap->{shutdown}) { $kernel->delay($state => 3); } } 1;