forked from igorizyumin/laser-rfid
/
laser_worker.ino
287 lines (257 loc) · 6.28 KB
/
laser_worker.ino
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// include the library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>
// #include "EEPROMAnything.h"
// Pins for LCD
#define PIN_LCD_D7 16
#define PIN_LCD_D6 17
#define PIN_LCD_D5 18
#define PIN_LCD_D4 19
#define PIN_LCD_RS 21
#define PIN_LCD_EN 20
#define MAX_DISPLAY_WIDTH 16
#define MAX_DISPLAY_HEIGHT 2
// Input pin from laser cutter mobo
#define LASER_PIN 0
// 5v from laser power supply
#define LASER_POWER_PIN 1
// laser pin is active low
#define LASER_PIN_FIRING 0
// power pin is active high
#define LASER_PIN_PWR_ON 1
// Relay to enable or disable the laser
#define ENABLE_PIN 10
// Transistor controlling the LCD backlight
#define BACKLIGHT_PIN 12
// Minimum interval between successive EEPROM writes (seconds)
#define MIN_SAVE_INTERVAL 30
// Interval the spinner should appear after last laser event_callback (seconds)
#define SPINNER_TIME 2
#define RFID_CODE_LENGTH 10
#define RFID_CODE_START 0x02
#define RFID_CODE_END 0x03
#define EEPROM_LOC1 0x30
#define EEPROM_LOC2 0x40
// ID-12LA RFID reader connected via the UART
HardwareSerial Uart = HardwareSerial();
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7);
// last write address to the EEPROM
int lastPos;
// total accumulated odometer time (seconds)
unsigned long time_total=0;
void read_odo()
{
lastPos = EEPROM.read(1);
if(lastPos == 0xFE)
{
EEPROM.get(EEPROM_LOC1, time_total);
}
else if(lastPos == 0xFF)
{
EEPROM.get(EEPROM_LOC2, time_total);
}
else
{
lastPos = 0xFF;
time_total = 0;
}
}
void write_odo()
{
if(lastPos == 0xFE)
{
EEPROM.put(EEPROM_LOC2, time_total);
EEPROM.write(1, 0xFF);
lastPos = 0xFF;
}
else
{
EEPROM.put(EEPROM_LOC1, time_total);
EEPROM.write(1, 0xFE);
lastPos = 0xFE;
}
}
void setup()
{
Serial.begin(9600);
Uart.begin(9600);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW);
pinMode(BACKLIGHT_PIN, OUTPUT);
digitalWrite(BACKLIGHT_PIN, HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(MAX_DISPLAY_WIDTH, MAX_DISPLAY_HEIGHT);
// Print a message to the LCD.
lcd.print(" Please Wait ");
pinMode(LASER_PIN, INPUT);
digitalWrite(LASER_PIN, HIGH); // enable internal pullup
pinMode(LASER_POWER_PIN, INPUT);
digitalWrite(LASER_POWER_PIN, HIGH);
read_odo();
}
void loop()
{
static unsigned char rfidByteIndex = 0;
static unsigned char rfid[RFID_CODE_LENGTH];
unsigned char rfidByte;
static unsigned char rfidFlag = 0;
static unsigned char serialCommand = 0;
static unsigned char serialDisplayBuffer[MAX_DISPLAY_WIDTH + 1];
static unsigned char serialDisplayBufferIndex = 0;
unsigned char serialByte;
static unsigned long lastIndicatorTime = 0;
static unsigned long lastWriteTime = time_total;
// accumulated fractional time in ms
static unsigned long time_ms = 0;
static unsigned long time_last = millis();
if (Serial.available() > 0)
{
serialByte = Serial.read();
switch (serialCommand)
{
case 'x':
if (serialByte == '\n')
{
time_total = 1000;
write_odo();
serialCommand = 0;
}
break;
case 'y':
if (serialByte == '\n')
{
time_total++;
write_odo();
serialCommand = 0;
}
break;
case 'z':
if (serialByte == '\n')
{
read_odo();
serialCommand = 0;
}
break;
case 'e':
// enable laser
if (serialByte == '\n')
{
digitalWrite(ENABLE_PIN, HIGH);
serialCommand = 0;
}
break;
case 'd':
// disable laser
if (serialByte == '\n')
{
digitalWrite(ENABLE_PIN, LOW);
serialCommand = 0;
}
break;
case 'p':
case 'q':
// display a message
// p = first line, q = second line
if (serialByte == '\n')
{
// buffer is full, terminate it and display the message
serialDisplayBuffer[serialDisplayBufferIndex] = 0;
lcd.setCursor(0, (serialCommand == 'p') ? 0 : 1);
lcd.print((char*)serialDisplayBuffer);
// clear rest of line
while (serialDisplayBufferIndex++ < MAX_DISPLAY_WIDTH)
lcd.print(" ");
serialDisplayBufferIndex = 0;
serialCommand = 0;
}
else
{
// append data to buffer
if (serialDisplayBufferIndex < MAX_DISPLAY_WIDTH)
serialDisplayBuffer[serialDisplayBufferIndex++] = serialByte;
}
break;
case 'o':
// report status: odometer and rfid scanned flag
if (serialByte == '\n')
{
Serial.print("o");
Serial.print(time_total + (time_ms+500)/1000);
Serial.print("x");
Serial.print(rfidFlag);
Serial.print("\n");
serialCommand = 0;
}
break;
case 'r':
// report RFID access
if (serialByte == '\n')
{
if (rfidFlag)
{
Serial.print("r");
Serial.write(rfid, RFID_CODE_LENGTH);
rfidFlag = 0;
}
Serial.print("\n");
}
serialCommand = 0;
break;
default:
// ignore unknown commands
serialCommand = serialByte;
break;
}
}
if (Uart.available() > 0)
{
rfidByte = Uart.read();
if (rfidByte == RFID_CODE_START)
{
rfidByteIndex = 0;
}
else if (rfidByteIndex < RFID_CODE_LENGTH)
{
rfid[rfidByteIndex++] = rfidByte;
}
else if (rfidByte == RFID_CODE_END)
{
// done reading RFID
rfidFlag = 1;
// flash the backlight to indicate success
digitalWrite(BACKLIGHT_PIN, LOW);
delay(250);
digitalWrite(BACKLIGHT_PIN, HIGH);
}
}
if((digitalRead(LASER_PIN) == LASER_PIN_FIRING) &&
(digitalRead(LASER_POWER_PIN) == LASER_PIN_PWR_ON))
{
time_ms += millis() - time_last;
time_total += time_ms / 1000;
time_ms = time_ms % 1000;
lastIndicatorTime = millis();
}
// save the odometer if either the accumulated laser time is >30s or
// the laser hasn't fired in 30s and the last value isn't accurate
if(time_total - lastWriteTime > MIN_SAVE_INTERVAL
|| (time_total != lastWriteTime &&
(millis()-lastIndicatorTime)/1000 > MIN_SAVE_INTERVAL))
{
write_odo();
lastWriteTime = time_total;
}
time_last = millis();
lcd.setCursor(7,1);
if((millis() - lastIndicatorTime) < SPINNER_TIME*1000)
{
switch(time_ms / 250)
{
case 0: lcd.print('/'); break;
case 1: lcd.print('-'); break;
case 2: lcd.print('/'); break;
case 3: lcd.print('|'); break;
}
}
}