Skip to content

Commit d7d3d45

Browse files
authored
Add files via upload
1 parent c1c60d6 commit d7d3d45

File tree

2 files changed

+505
-0
lines changed

2 files changed

+505
-0
lines changed

MaixduinoFactorAuth.ino

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
#include <Sipeed_ST7789.h>
2+
#include "Maix_Speech_Recognition.h"
3+
#include "voice_model.h"
4+
#include <Wire.h>
5+
#include "paj7620.h"
6+
7+
SpeechRecognizer rec;
8+
9+
SPIClass spi_(SPI0); // MUST be SPI0 for Maix series on board LCD
10+
Sipeed_ST7789 lcd(320, 240, spi_);
11+
12+
#define PIR_MOTION_SENSOR 6
13+
#define GES_REACTION_TIME 500
14+
#define GES_ENTRY_TIME 800
15+
#define GES_QUIT_TIME 1000
16+
17+
long security_time_out = 10000;
18+
long test_start_millis = 0;
19+
bool passed_auth_motion = false;
20+
bool passed_auth_voice = false;
21+
bool passed_auth_gesture = false;
22+
bool passed_auth_rfid = false;
23+
enum auth_level_states { NONE, MOTION, VOICE, GESTURE, RFID };
24+
enum auth_level_states auth_level_passed = NONE;
25+
26+
int phrase_array[3];
27+
int gesture_array[4];
28+
29+
unsigned char buffer[64]; // buffer array for data receive over serial port
30+
int count = 0;
31+
long keyfobID = 697827; // ID printed on keyfob
32+
33+
34+
void auth_motion()
35+
{
36+
lcd.fillScreen(COLOR_BLACK);
37+
if (digitalRead(PIR_MOTION_SENSOR))
38+
{
39+
Serial.println("Hi, people is coming");
40+
lcd.fillRect(10, 160, 300, 70, COLOR_RED);
41+
lcd.setTextColor(COLOR_WHITE);
42+
lcd.setCursor(10, 10);
43+
lcd.print("Welcome To Security");
44+
passed_auth_motion = true;
45+
auth_level_passed = MOTION;
46+
}
47+
}
48+
49+
bool auth_gesture()
50+
{
51+
delay(1000);
52+
lcd.setCursor(10, 40);
53+
lcd.println("Do gesture sequence");
54+
long test_start_millis = millis();
55+
int i = 0;
56+
Serial.println("gesture waiting");
57+
delay(500);
58+
while (millis() - test_start_millis < security_time_out && i <= 3) {
59+
60+
uint8_t data = 0;
61+
paj7620ReadReg(0x43, 1, &data); // Read Bank_0_Reg_0x43/0x44 for gesture result.
62+
//delay(GES_QUIT_TIME);
63+
64+
if (data == GES_LEFT_FLAG || data == GES_RIGHT_FLAG || data == GES_UP_FLAG || data == GES_DOWN_FLAG)
65+
{
66+
Serial.printf("Gesture : %d ", data);
67+
gesture_array[i] = data;
68+
Serial.println(gesture_array[i]);
69+
if (gesture_array[0] == 4 && gesture_array[1] == 8 && gesture_array[2] == 1 && gesture_array[3] == 2) {
70+
Serial.println("gesture accepted");
71+
passed_auth_gesture = true;
72+
auth_level_passed = GESTURE;
73+
memset(gesture_array, 0, sizeof(gesture_array)); // reset array
74+
return true;
75+
}
76+
i++;
77+
}
78+
}
79+
Serial.println("gesture failed");
80+
auth_fail();
81+
return false;
82+
}
83+
84+
bool auth_voice()
85+
{
86+
lcd.setTextColor(COLOR_WHITE);
87+
lcd.setCursor(10, 70);
88+
lcd.println("Say the pass phrase");
89+
rec.begin();
90+
rec.addVoiceModel(0, 0, red, fram_num_red);
91+
rec.addVoiceModel(1, 0, green, fram_num_green);
92+
rec.addVoiceModel(2, 0, blue, fram_num_blue);
93+
94+
long test_start_millis = millis();
95+
int i = 0;
96+
97+
while (millis() - test_start_millis < security_time_out) {
98+
Serial.println("voice waiting");
99+
100+
int res;
101+
res = rec.recognize(); // need to timeout this somehow
102+
Serial.printf("res : %d ", res);
103+
if (res > 0) {
104+
phrase_array[i] = res;
105+
i++;
106+
if (phrase_array[0] == 1 && phrase_array[1] == 2 && phrase_array[2] == 3) {
107+
Serial.println("voice accepted");
108+
passed_auth_voice = true;
109+
auth_level_passed = VOICE;
110+
memset(phrase_array, 0, sizeof(phrase_array)); // reset array
111+
return true;
112+
}
113+
switch (res)
114+
{
115+
case 1:
116+
Serial.println("rec : red ");
117+
break;
118+
119+
case 2:
120+
Serial.println("rec : green ");
121+
break;
122+
123+
case 3:
124+
Serial.println("rec : blue ");
125+
break;
126+
}
127+
}
128+
}
129+
Serial.println("voice failed");
130+
auth_fail();
131+
return false;
132+
}
133+
134+
135+
136+
bool auth_rfid()
137+
{
138+
lcd.setCursor(10, 100);
139+
lcd.println("Use RIFD tag");
140+
long test_start_millis = millis();
141+
String keyfobString = "";
142+
String receivedString = "";
143+
count = 0;
144+
while (millis() - test_start_millis < security_time_out) {
145+
if (Serial2.available())
146+
delay(500);
147+
{
148+
while (Serial2.available())
149+
{
150+
buffer[count++] = Serial2.read(); // writing data into array
151+
if (count == 64)break;
152+
}
153+
for (int i = 0; i < count; i++)
154+
{
155+
if (buffer[i] > 47) { // not control char
156+
receivedString += (char)buffer[i];
157+
}
158+
}
159+
memset(buffer, 0, sizeof(buffer)); // reset buffer array
160+
receivedString.remove(0, 5);
161+
receivedString.remove(5, 2);
162+
163+
keyfobString = String(keyfobID, HEX);
164+
165+
if (receivedString.equalsIgnoreCase(keyfobString)) {
166+
Serial.println("RFID accepted");
167+
passed_auth_rfid = true;
168+
auth_level_passed = RFID;
169+
return true;
170+
}
171+
}
172+
173+
}
174+
Serial.println("RFID auth failed");
175+
auth_fail();
176+
return false;
177+
}
178+
179+
180+
void auth_success()
181+
{
182+
lcd.fillRect(10, 160, 300, 70, COLOR_GREEN);
183+
lcd.setCursor(10, 130);
184+
lcd.println("Door Opening...");
185+
auth_reset();
186+
}
187+
188+
void auth_fail()
189+
{
190+
lcd.fillRect(10, 160, 300, 70, COLOR_RED);
191+
lcd.setCursor(10, 130);
192+
lcd.println("Auth error. Resetting.");
193+
auth_reset();
194+
}
195+
196+
void auth_reset()
197+
{
198+
delay(3000);
199+
passed_auth_motion = false;
200+
passed_auth_voice = false;
201+
passed_auth_gesture = false;
202+
passed_auth_rfid = false;
203+
auth_level_passed = NONE;
204+
}
205+
206+
void setup() {
207+
Serial.begin(115200);
208+
Serial2.begin(9600, 2, 3); //rfid reader
209+
210+
pinMode(PIR_MOTION_SENSOR, INPUT);
211+
212+
lcd.begin(15000000, COLOR_BLACK);
213+
lcd.setTextSize(2);
214+
uint8_t error = 0;
215+
paj7620Init();
216+
217+
}
218+
219+
void loop() {
220+
221+
switch (auth_level_passed)
222+
{
223+
case NONE:
224+
auth_motion();
225+
break;
226+
227+
case MOTION:
228+
auth_gesture();
229+
break;
230+
231+
case GESTURE:
232+
auth_voice();
233+
break;
234+
235+
case VOICE:
236+
auth_rfid();
237+
break;
238+
239+
case RFID:
240+
auth_success();
241+
break;
242+
}
243+
244+
}

0 commit comments

Comments
 (0)