-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.pl
200 lines (162 loc) · 3.85 KB
/
scan.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use strict;
use warnings;
use database;
use Data::Dumper;
# print Dumper( database::getPersonList() );
my $n = -1;
while ($n < 10) {
$n++;
print "Trying port..." . $n . "\n";
initSerial($n);
open( PORT, "/dev/ttyUSB" . $n) or next;
print "Opened port..." . $n . "\n";
last;
}
my $currentString = "";
# Main program loop. Reads data from barcode scanner character by character
while (1)
{
my $char;
read( PORT, $char, 1 );
if ( $char =~ /\n/ )
{
# \n marks the end of a complete scan.
chomp($currentString);
print "BARCODE|$currentString|";
handleBarcode($currentString);
$currentString = "";
}
else
{
$currentString .= $char;
}
}
close PORT;
my $gLastPersonScanned;
my $gLastCommandScanned = "";
# Return hash of barcodes to person ID
#
sub getPeopleHash
{
return database::getPersonList();
}
sub getCommandHash
{
# Maps command barcodes onto routines
my %commandHash = (
'000000084062' => \&sayHello,
'9780719540158' => \&identifyPerson,
'9780859340601' => \&removeLastEntry,
'9780859344012' => \&whosInLab,
'9780859341592' => \&logAllOut,
);
return \%commandHash;
}
sub sayHello
{
say("Hello - you ve run a command");
}
sub identifyPerson
{
say("Now scan person");
$gLastCommandScanned = "identify";
# TODO - probably maintain some nasty gloabl state for this command...
}
sub removeLastEntry
{
my $lastEntry = database::removeLastEntry();
say("Entry for $lastEntry has been removed");
}
sub whosInLab
{
# fetch all people who are currently IN
say("The following people are in the lab");
my @peoplePresent = @{database::getWhosInLab()};
for my $person (@peoplePresent)
{
say($person);
}
}
sub logAllOut
{
# fetch all people who are currently IN
# Say who's in the lab, then log them all out.
whosInLab();
database::logAllOut();
say("Logging everyone out");
}
sub ignoreScan
{
#Do database lookup
return 0;
}
# The main barcode handling code
# Called each time a barcode is read
# There are two main categories of barcodes - people and command.
# IN: a barcode to handle
# OUT: nothing
sub handleBarcode
{
my ($barcode) = @_;
my %peopleHash = %{ getPeopleHash() };
my %commandHash = %{ getCommandHash() };
if ( $peopleHash{$barcode} )
{
# Can't remeber what this is for...
if ( ignoreScan($barcode) )
{
say("Ignoring");
return;
}
my $state = database::getState($barcode);
if ( $state eq 'IN' )
{
say( "Goodbye " . $peopleHash{$barcode} );
}
else
{
say( "Hello " . $peopleHash{$barcode} );
}
if ($gLastCommandScanned ne "identify")
{
database::flipState($barcode, $state);
}
# Finished dealing with a person, no longer care about the lastCommand
$gLastCommandScanned = "";
}
elsif ( $commandHash{$barcode} )
{
# Clear the last command run, as we're now running another command
$gLastCommandScanned = "";
&{ $commandHash{$barcode} };
print("Found a command");
}
else
{
say("Unrecognized barcode $barcode");
}
}
# Say some text
# IN: the text to speak
# OUT: Nothing
sub say
{
my ($barcode) = @_;
print "Read barcode $barcode\n";
system("espeak -v en-scottish '$barcode'");
}
sub initSerial
{
my ($portNumber) = @_;
my $PortName = "/dev/ttyUSB" . $n;
my $PortObj = Device::SerialPort->new($PortName)
|| return;
# $PortObj->user_msg(ON);
$PortObj->databits(8);
$PortObj->baudrate(9600);
$PortObj->parity("none");
$PortObj->stopbits(1);
$PortObj->handshake("rts");
$PortObj->write_settings || undef $PortObj;
}