#!/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.012; use strict; use warnings; use diagnostics; use mro 'c3'; use English qw( -no_match_vars ); use Carp; our $VERSION = 1.5; no if $] >= 5.017011, warnings => 'experimental::smartmatch'; #---AUTOPRAGMAEND--- no utf8; use Device::BCM2835; use Time::HiRes qw[sleep alarm time]; use Data::Dumper; # Make sure the process name is maplat_failbutton $0 = 'maplat_failbutton'; my $SHUTDOWN = 0; $SIG{INT} = sub{print "GOT SIGINT, exiting...\n"; $SHUTDOWN=1;}; $SIG{TERM} = sub{print "GOT SIGTERM, exiting...\n"; $SHUTDOWN=1;}; if(!Device::BCM2835::init()) { die("Can't open device!"); } my $testpinnumber = 13; # BCM internal pin number; 21 = GPIO Pin 40 my $sndidx = 1; # Select as input device Device::BCM2835::gpio_fsel($testpinnumber, &Device::BCM2835::BCM2835_GPIO_FSEL_INPT); # Select Pull up resistor mode Device::BCM2835::gpio_set_pud($testpinnumber, &Device::BCM2835::BCM2835_GPIO_PUD_UP); my $lastswitch = -1; # "Unknown" my $lastswitchtime = time; while(!$SHUTDOWN) { my $switch = Device::BCM2835::gpio_lev($testpinnumber); if($switch != $lastswitch) { my $now = time; print "SWITCH SET TO $switch\n"; if($lastswitch == -1) { $lastswitch = $switch; sleep(0.5); next; } $lastswitch = $switch; if($switch) { print "Switch pressed...\n"; $lastswitchtime = $now; } else { my $cmd; my $presslength = $now - $lastswitchtime; print "PL: $presslength\n"; if($presslength < 0.8) { print " short click\n"; $cmd = '/usr/bin/aplay -r 44100 -f S16_LE /home/cavac/src/maplat_failalert/bullshit' . $sndidx . '.raw'; $sndidx++; if($sndidx == 4) { $sndidx = 1; } } else { print " long click\n"; $cmd = '/usr/bin/aplay -r 44100 -f S16_LE /home/cavac/src/maplat_failalert/jeopardy.raw'; } print "Running $cmd\n"; `$cmd`; print "...done.\n"; } } sleep(0.1); }