Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

brmd::Alphasign: Manage serial directly, no external helper

  • Loading branch information...
commit cd88ecb79122e7fbac4dbb15ca4ab7af9bf012a3 1 parent 84d0656
Petr Baudis authored May 13, 2011

Showing 2 changed files with 77 additions and 6 deletions. Show diff stats Hide diff stats

  1. 5  TODO
  2. 78  brmd/brmd.pl
5  TODO
@@ -4,6 +4,11 @@
4 4
 * Potentially some prettier icons and richer javascript stubs.
5 5
 
6 6
 
  7
+* Alphasign: Web interface (while OFF AIR).
  8
+	Also add real support for colors and beeping streaming
  9
+	notification.
  10
+
  11
+
7 12
 * Way to unlock door remotely (through some secure channel).
8 13
 	Bi-directional communication will be required.
9 14
 
78  brmd/brmd.pl
@@ -9,6 +9,7 @@
9 9
 our $channel = "#brmlab";
10 10
 our $streamurl = "http://nat.brmlab.cz:8090/brmstream.asf";
11 11
 our $devdoor = $ARGV[0]; $devdoor ||= "/dev/ttyUSB0";
  12
+our $devasign = $ARGV[0]; $devasign ||= "/dev/ttyUSB1";
12 13
 our ($status, $streaming, $topic) = (0, 0, 'BRMLAB OPEN');
13 14
 
14 15
 my $irc = brmd::IRC->new();
@@ -597,7 +598,9 @@ sub stream_stop {
597 598
 
598 599
 package brmd::Alphasign;
599 600
 
600  
-use POE;
  601
+use POE qw(Wheel::ReadWrite Filter::Block);
  602
+use Symbol qw(gensym);
  603
+use Device::SerialPort;
601 604
 
602 605
 sub new {
603 606
 	my $class = shift;
@@ -606,6 +609,7 @@ sub new {
606 609
 	POE::Session->create(
607 610
 		object_states => [
608 611
 			$self => [ qw(_start _default
  612
+					serial_error text
609 613
 					stream_start stream_stop) ],
610 614
 		],
611 615
 	);
@@ -615,6 +619,14 @@ sub new {
615 619
 
616 620
 sub _start {
617 621
 	$_[KERNEL]->alias_set("$_[OBJECT]");
  622
+
  623
+	$_[HEAP]->{serial} = POE::Wheel::ReadWrite->new(
  624
+		Handle => serial_open($devasign),
  625
+		Filter => POE::Filter::Block->new(
  626
+			LengthCodec => [ \&as_encoder, \&as_decoder ],
  627
+			),
  628
+		ErrorEvent => "serial_error",
  629
+	) or die "Alphasign fail: $!";
618 630
 }
619 631
 
620 632
 sub _default {
@@ -632,17 +644,71 @@ sub _default {
632 644
 	print join ' ', @output, "\n";
633 645
 }
634 646
 
635  
-sub stream_switch {
636  
-	my ($s) = @_;
637  
-	system('~/alphasign/'.($s?'on':'off').'_air.py');
  647
+sub serial_open {
  648
+	my ($device) = @_;
  649
+	# Open a serial port, and tie it to a file handle for POE.
  650
+	my $handle = gensym();
  651
+	my $port = tie(*$handle, "Device::SerialPort", $device);
  652
+	die "can't open port: $!" unless $port;
  653
+	$port->datatype('raw');
  654
+	$port->baudrate(9600);
  655
+	$port->databits(8);
  656
+	$port->parity("none");
  657
+	$port->stopbits(1);
  658
+	$port->handshake("none");
  659
+	$port->write_settings();
  660
+	$port->write("E\$" . "AAU0100FF00" . "CDU075A2000" . "BBL000F0000");
  661
+	return $handle;
  662
+}
  663
+
  664
+sub serial_error {
  665
+	my ($heap) = ($_[HEAP]);
  666
+	print "$_[ARG0] error $_[ARG1]: $_[ARG2]\n";
  667
+	print "bye!\n";
638 668
 }
639 669
 
  670
+sub as_encoder {
  671
+	my $stuff = shift;
  672
+        $$stuff = "\x00" x 5   # packet sync characters
  673
+	          . "\x01"     # start of header
  674
+	          . "Z"        # all types
  675
+	          . "00"       # broadcast address
  676
+	          . "\x02"     # start of text
  677
+	          . $$stuff    # raw data
  678
+	          . "\x04";    # end of transmission
  679
+	return;
  680
+}
  681
+
  682
+sub as_decoder {
  683
+	return 1; # XXX
  684
+}
  685
+
  686
+# TODO: have text() a simple markup parser, and raw_text() for writing it out
  687
+
  688
+sub text {
  689
+	my ($heap, $mode, $string) = (@_[HEAP, ARG0, ARG1]);
  690
+	print "out text: $mode, $string\n";
  691
+	my $s = "A"          # text mode
  692
+	        . "A"        # file label
  693
+	        . "\x1B"     # start of text
  694
+	        . " "        # use middle line (irrelevant on singleline display)
  695
+	        . $mode      # display mode
  696
+	        . "\x1C1"    # set default color = red
  697
+	        . $string;   # text to display
  698
+	$_[HEAP]{serial}->put($s);
  699
+}
  700
+
  701
+#sub stream_switch {
  702
+#	my ($s) = @_;
  703
+#	system('~/alphasign/'.($s?'on':'off').'_air.py');
  704
+#}
  705
+
640 706
 sub stream_start {
641  
-	stream_switch(1);
  707
+	$_[KERNEL]->yield('text', 'b', "\x1D01ON AIR\x1D00");
642 708
 }
643 709
 
644 710
 sub stream_stop {
645  
-	stream_switch(0);
  711
+	$_[KERNEL]->yield('text', 'b', "\x1D00OFF AIR\x1D00");
646 712
 }
647 713
 
648 714
 1;

0 notes on commit cd88ecb

Please sign in to comment.
Something went wrong with that request. Please try again.