View unanswered posts | View active topics It is currently Thu Dec 13, 2018 4:16 am



Reply to topic  [ 1 post ] 
 fit-statUSB: Random Color Generator 
Author Message

Joined: Mon May 28, 2012 12:25 pm
Posts: 104
Post fit-statUSB: Random Color Generator
Overview

fit-statUSB isn't all work and no play. It can be used to implement things which are colorful and entertaining. In this case it is configured to be a random color generator.


Random Color Generator In Action

Unfortunately, the camera image is a bit washed out. In reality, the effect is very good

Attachment:
ezgif.com-resize2.gif
ezgif.com-resize2.gif [ 1.76 MiB | Viewed 442 times ]


Attachment:
ezgif.com-resize.gif
ezgif.com-resize.gif [ 1.21 MiB | Viewed 442 times ]



Perl App

#!/usr/bin/perl -w
#
#fitrandom.pl

# Darryl Hassell 8/8/2018

use Tk;
use Device::SerialPort;
use Time::HiRes ('sleep');

# Define & Initialize Variables

my $red_random_dec = 255;
my $green_random_dec = 255;
my $blue_random_dec = 255;

my $red_random_hex = 'FF';
my $green_random_hex = 'FF';
my $blue_random_hex = 'FF';

my $random_color = '#FFFFFF';

# Configure Serial Port
my $port = Device::SerialPort -> new("/dev/ttyACM0");
$port -> baudrate(115200);
$port -> databits(8);
$port -> parity("none");
$port -> stopbits(1);
$port -> write_settings;

# Set Sleep Delay After Writes in Seconds
our $delay = 0.1;

# Initialize fit-statUSB to Max Brightness White
$port -> write("F0.1\n");
$port -> purge_rx;
$port -> write("$random_color\n");
$port -> purge_rx;

# Create Main Window
$WinMain = MainWindow->new;
$WinMain -> resizable (0,0);

# Label Main Window
$myframe = $WinMain -> Frame ->pack();
$Mem_Use_Label = $myframe -> Label (-text => 'fit-statUSB Random Color Demo', -background => "$random_color", -foreground => "black", -font => "helvetica 9 bold")->pack(-ipadx => 20,-ipady => 20);


# Exit Program Button
$stopbutton = $myframe -> Button (-text => 'Exit', -command => \&Exit, -foreground => 'black', -background => 'lightblue');
$stopbutton -> pack (-side => 'bottom');


$WinMain -> repeat(500, \&GetRandom);

MainLoop();

################################ Exit Program ################################

sub Exit {

$port -> write("#000000\n");
$port -> purge_rx;
sleep($delay);
exit;
}

################################ Generate Random Colors ################################

sub GetRandom {

########## Red ##########
my $red_random_dec1 = int(rand(15)) + 1;
my $red_random_hex1 = sprintf("%X", $red_random_dec1);

my $red_random_dec2 = int(rand(15)) + 1;
my $red_random_hex2 = sprintf("%X", $red_random_dec2);


########## Green ##########
my $green_random_dec1 = int(rand(15)) + 1;
my $green_random_hex1 = sprintf("%X", $green_random_dec1);

my $green_random_dec2 = int(rand(15)) + 1;
my $green_random_hex2 = sprintf("%X", $green_random_dec2);

########## Blue ##########
my $blue_random_dec1 = int(rand(15)) + 1;
my $blue_random_hex1 = sprintf("%X", $blue_random_dec1);

my $blue_random_dec2 = int(rand(15)) + 1;
my $blue_random_hex2 = sprintf("%X", $blue_random_dec2);

my $random_color = '#'.$red_random_hex1.$red_random_hex2.$green_random_hex1.$green_random_hex2.$blue_random_hex1.$blue_random_hex2;


# Send Color and Brightness to fit-statUSB
$port -> write("$random_color\n");
$port -> purge_rx;


$Mem_Use_Label -> configure (-text => 'fit-statUSB Random Color Demo', -background => "$random_color");

}




Conclusions

fit-statUSB can be used for fun and entertaining creations.


Fri Aug 10, 2018 1:34 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.