-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCodeScanner.cpp
More file actions
73 lines (62 loc) · 1.22 KB
/
BCodeScanner.cpp
File metadata and controls
73 lines (62 loc) · 1.22 KB
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
/* ---------
Description: This is an Arduino library for USB HID Barcode Scanner by Wallace Lee.
This library only work with x86 based machine such as Intel(r) Galileo.
MIT License
-----------*/
#include "BCodeScanner.h"
#include <linux/input.h>
#include <string>
#include <fcntl.h>
bool
BCodeScanner::isValid()
{
return (fd != -1);
}
const char*
BCodeScanner::readBC()
{
struct input_event ev[64];
int rd, value, size = sizeof (struct input_event);
std::string barcode="";
while (1)
{
if ((rd = read (fd, ev, size * 64)) < size)
exit(1);
for(int i=0; i < rd / sizeof(struct input_event); i++)
{
if(ev[i].type == 1 && ev[i].value == 1 && ev[i].code != 42) { // scancode 42 is LSHIFT key
if(ev[i].code != 28) {
barcode += keycode[ev[i].code];
}
else {
return barcode.c_str();
}
}
}
}
}
void
BCodeScanner::begin()
{
if ((fd = open (dev_path, O_RDONLY)) != -1)
{
ioctl (fd, EVIOCGNAME (sizeof (dev_name)), dev_name);
}
}
BCodeScanner::~BCodeScanner()
{
if(fd != -1) {
close(fd);
}
}
BCodeScanner::BCodeScanner(char *path)
{
unsigned int len = strlen(path);
memset(dev_path, 0, 1024);
memcpy(dev_path, path, len);
}
char*
BCodeScanner::GetDeviceName()
{
return dev_name;
}