From 25fbb8a03c72f473a2d7f6cfb5961a1c43b1a6a2 Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Sun, 24 Jan 2021 10:12:38 +0000 Subject: add piano alarm --- piano-alarm/mt300-v2/usr/bin/sensor.pl | 93 ++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 piano-alarm/mt300-v2/usr/bin/sensor.pl (limited to 'piano-alarm/mt300-v2/usr/bin/sensor.pl') diff --git a/piano-alarm/mt300-v2/usr/bin/sensor.pl b/piano-alarm/mt300-v2/usr/bin/sensor.pl new file mode 100755 index 0000000..9a5501d --- /dev/null +++ b/piano-alarm/mt300-v2/usr/bin/sensor.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl + +use strict; + +use Device::SerialPort; + +sub get_line($) { + my $port = shift; + + my $ret = ""; + + while (1) { + my ( $count, $byte ) = $port->read(1); + + if ( $count > 0 ) { + $ret .= $byte; + return $ret if $byte eq "\n"; + } + else { + return $ret; + } + } +} + +sub display($$$$$$) { + my ( $t, $h, $show_t, $show_h, $int, $mrtg ) = @_; + + if ( $int or $mrtg ) { + $t = int( $t * 100 ); + $h = int( $h * 10 ); + } + + if ($mrtg) { + if ($show_t) { + print "$t\n"; + print "0\n"; + print "long time\n"; + print "temperature\n"; + } + + if ($show_h) { + print "$t\n"; + print "0\n"; + print "long time\n"; + print "humidity\n"; + } + } + else { + print "$t\n" if $show_t; + print "$h\n" if $show_h; + } +} + +my $keypad_port = "/dev/ttyS1"; +my $keypad = new Device::SerialPort($keypad_port) + || die "can't open $keypad_port\n"; + +$keypad->baudrate(115200); +$keypad->parity("none"); +$keypad->databits(8); +$keypad->stopbits(1); +$keypad->stty_icanon(0); + +$keypad->read_char_time(0); +$keypad->read_const_time(1000); + +my $mrtg = 0; +my $temp = 0; +my $humid = 0; +my $int = 0; + +for my $opt (@ARGV) { + $mrtg = 1 if $opt eq '-m'; + $temp = 1 if $opt eq '-t'; + $humid = 1 if $opt eq '-h'; + $int = 1 if $opt eq '-i'; +} + +for ( my $tries = 0 ; $tries < 4 ; ++$tries ) { + my $line = get_line($keypad); + + chomp $line; + chomp $line; + + if ( $line =~ /\$SNTHD,([-.0-9]+),([-.0-9]+)/ ) { + + display( $1, $2, $temp, $humid, $int, $mrtg ); + exit 0; + } +} + +exit 1; + -- cgit v1.2.3