web_light/test.pl
2013-08-18 13:26:25 -07:00

64 lines
996 B
Perl
Executable file

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
use lib './lib';
use Dancer;
#use JSON;
use URI;
require 'syscall.ph';
sub find_tty {
for my $i (0..10) {
if (-e "/dev/ttyACM$i") {
return "/dev/ttyACM$i";
}
}
}
# do_ajax
any '/ajax' => sub {
# my $json_str = $request->content();
# print "D: $json_str\n";
# my $data = {map {(split /=/, $_)} split /\&/, $json_str};
my %data = params;
debug "Sending Color";
send_color(\%data);
# return RC_OK;
};
sub send_color {
my $color = shift;
my $tty = find_tty();
return unless $tty;
open(my $fh, ">", find_tty());
my $str = pack("C*", 0, 64, $color->{r}, $color->{g}, $color->{b});
$fh->syswrite($str);
my $fd = fileno $fh;
undef $fh;
syscall(&SYS_close, $fd);
}
any qr{.*} => sub {
my $file = params->{file};
debug 'Serving file '.request->path;
if (-f 'files/'.$file) {
return send_file( params->{file} );
} else {
return send_file( 'index.html' );
}
};
dance;