forked from drinks-wallet/DrinksRfidTerminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarcode.cpp
77 lines (70 loc) · 1.53 KB
/
Barcode.cpp
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
#include "Barcode.h"
#include <Arduino.h>
#include <SoftwareSerial.h>
Barcode::Barcode(int rx, int tx, int en) : mySerial(rx, tx)
{
enable_pin = en;
pinMode(enable_pin, OUTPUT);
digitalWrite(enable_pin, LOW);
}
void Barcode::begin()
{
mySerial.begin(9600);
mySerial.flush();
}
void Barcode::enable()
{
digitalWrite(enable_pin, HIGH);
delay(250);
mySerial.flush();
}
void Barcode::disable()
{
digitalWrite(enable_pin, LOW);
}
void Barcode::flush()
{
while(mySerial.available())
mySerial.read();
}
int Barcode::available()
{
//int temp = mySerial.available();
//Serial.println(temp);
return mySerial.available();
}
int Barcode::get(char* barcode, int size)
{
if(!mySerial.available())
return -1;
int index = 0;
char inChar=-1;
while (mySerial.available() > 0)
{
if(index < size-1)
{
inChar = mySerial.read(); // Read a character
barcode[index++] = inChar; // Store it
//barcode[index] = '\0'; // Null terminate the string
}
else {
flush();
break;
}
}
delay(500); //sometimes the text is sent over 2 lines because of a delay in serial connection
while (mySerial.available() > 0)
{
if(index < size-1)
{
inChar = mySerial.read(); // Read a character
barcode[index++] = inChar; // Store it
//barcode[index] = '\0'; // Null terminate the string
}
else {
flush();
break;
}
}
return index;
}