Explorar el Código

working communication

develop
Robin Thoni hace 8 años
padre
commit
ce1dafa6f2
Se han modificado 4 ficheros con 206 adiciones y 41 borrados
  1. 80
    0
      HostCommunication.cpp
  2. 41
    0
      HostCommunication.h
  3. 50
    0
      config.h
  4. 35
    41
      main.ino

+ 80
- 0
HostCommunication.cpp Ver fichero

@@ -0,0 +1,80 @@
1
+//
2
+// Created by robin on 5/5/16.
3
+//
4
+
5
+#include "HostCommunication.h"
6
+
7
+#include <Arduino.h>
8
+
9
+HostCommunication::HostCommunication()
10
+{
11
+}
12
+
13
+void HostCommunication::init()
14
+{
15
+    Serial.begin(SERIAL_BAUDRATE);
16
+    debug("Starting...");
17
+}
18
+
19
+void HostCommunication::debug(String str)
20
+{
21
+#ifdef DEBUG
22
+    send(PACKET_DEBUG, str);
23
+#endif
24
+}
25
+
26
+void HostCommunication::selfTest()
27
+{
28
+    send(PACKET_SELF_TEST);
29
+}
30
+
31
+void HostCommunication::login(String uid, String password)
32
+{
33
+    send(PACKET_LOGIN, uid + "\n" + password);
34
+}
35
+
36
+void HostCommunication::send(SERIAL_PACKET_TYPE type)
37
+{
38
+    send(type, "");
39
+}
40
+
41
+void HostCommunication::send(SERIAL_PACKET_TYPE type, String str)
42
+{
43
+    Serial.write((SERIAL_PACKET_TYPE)type);
44
+    Serial.write((SERIAL_PACKET_LENGTH_TYPE)str.length());
45
+    Serial.write(str.c_str());
46
+}
47
+
48
+HostCommunicationResult HostCommunication::read(int timeout)
49
+{
50
+    HostCommunicationResult res;
51
+    res.length = 0;
52
+    res.data = 0;
53
+    res.isSuccess = false;
54
+    if (!waitData(timeout, sizeof(SERIAL_PACKET_LENGTH_TYPE))) {
55
+        return res;
56
+    }
57
+
58
+    SERIAL_PACKET_LENGTH_TYPE length = 0;
59
+    Serial.readBytes((char*)&length, sizeof(SERIAL_PACKET_LENGTH_TYPE));
60
+
61
+    if (!waitData(timeout, length)) {
62
+        return res;
63
+    }
64
+
65
+    res.data = new char[length];
66
+    Serial.readBytes(res.data, length);
67
+    res.isSuccess = true;
68
+    res.length = length;
69
+
70
+    return res;
71
+}
72
+
73
+bool HostCommunication::waitData(int timeout, int length)
74
+{
75
+    unsigned long secs = millis();
76
+    while (Serial.available() < length && millis() - secs < timeout) {
77
+        delay(1);
78
+    }
79
+    return Serial.available() >= length;
80
+}

+ 41
- 0
HostCommunication.h Ver fichero

@@ -0,0 +1,41 @@
1
+//
2
+// Created by robin on 5/5/16.
3
+//
4
+
5
+#ifndef DIGICODE_HOSTCOMMUNICATION_H
6
+#define DIGICODE_HOSTCOMMUNICATION_H
7
+
8
+#include <WString.h>
9
+
10
+#include "config.h"
11
+
12
+struct HostCommunicationResult
13
+{
14
+    char* data;
15
+    int length;
16
+    bool isSuccess;
17
+};
18
+
19
+class HostCommunication
20
+{
21
+public:
22
+    HostCommunication();
23
+
24
+    void init();
25
+
26
+    void debug(String str);
27
+    void selfTest();
28
+    void login(String uid, String password);
29
+
30
+    HostCommunicationResult read(int timeout);
31
+
32
+protected:
33
+
34
+    void send(SERIAL_PACKET_TYPE type);
35
+    void send(SERIAL_PACKET_TYPE type, String str);
36
+
37
+    bool waitData(int timeout, int length);
38
+};
39
+
40
+
41
+#endif //DIGICODE_HOSTCOMMUNICATION_H

+ 50
- 0
config.h Ver fichero

@@ -0,0 +1,50 @@
1
+//
2
+// Created by robin on 5/5/16.
3
+//
4
+
5
+#ifndef DIGICODE_CONFIG_H
6
+#define DIGICODE_CONFIG_H
7
+
8
+
9
+#define KP_ROWS 4
10
+#define KP_COLS 4
11
+
12
+#define LCD_ROWS 2
13
+#define LCD_COLS 16
14
+
15
+#define SELF_TEST_LABEL "Self testing..."
16
+#define SELF_TEST_ERROR_LABEL "Error: "
17
+#define UID_LABEL "UID:"
18
+#define PWD_LABEL "PWD:"
19
+#define LOGIN_IN_PROGRESS "Login..."
20
+#define LOGIN_FAILED "Login failed"
21
+#define LOGIN_SUCCESS "Access granted"
22
+
23
+#define UID_MAX_LEN 12
24
+#define PWD_MAX_LEN 12
25
+
26
+#define MAX_IDLE_TIME 20
27
+
28
+#define LOGIN_FAILED_TIME 2
29
+
30
+#define SELF_TEST_INTERVAL 60
31
+#define SELF_TEST_ERROR_INTERVAL 2
32
+#define MAX_SELF_TEST_TIME 5
33
+
34
+#define SERIAL_BAUDRATE 9600
35
+#define SERIAL_PACKET_TYPE unsigned char
36
+#define SERIAL_PACKET_LENGTH_TYPE unsigned char
37
+#define SERIAL_PACKET_TYPE_INT unsigned char
38
+
39
+#define ERROR_NONE 0
40
+#define ERROR_HOST_PROTOCOL 1
41
+#define ERROR_HOST_TIMEOUT 2
42
+
43
+#define PACKET_DEBUG 1
44
+#define PACKET_SELF_TEST 2
45
+#define PACKET_LOGIN 3
46
+
47
+#define DEBUG
48
+
49
+
50
+#endif //DIGICODE_CONFIG_H

+ 35
- 41
main.ino Ver fichero

@@ -2,9 +2,10 @@
2 2
 #include <Keypad/Keypad.h>
3 3
 #include <LiquidCrystal.h>
4 4
 
5
+#include "config.h"
6
+
7
+#include "HostCommunication.h"
5 8
 
6
-#define KP_ROWS 4
7
-#define KP_COLS 4
8 9
 char keys[KP_ROWS][KP_COLS] = {
9 10
         {'1','2','3','A'},
10 11
         {'4','5','6','B'},
@@ -16,21 +17,9 @@ byte colPins[KP_COLS] = {8, 9, 10, 11};
16 17
 Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);
17 18
 
18 19
 
19
-#define LCD_ROWS 2
20
-#define LCD_COLS 16
21 20
 LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
22 21
 
23 22
 
24
-#define SELF_TEST_LABEL "Self testing..."
25
-#define SELF_TEST_ERROR_LABEL "Error: "
26
-#define UID_LABEL "UID:"
27
-#define PWD_LABEL "PWD:"
28
-#define LOGIN_IN_PROGRESS "Login..."
29
-#define LOGIN_FAILED "Login failed"
30
-#define LOGIN_SUCCESS "Access granted"
31
-
32
-#define UID_MAX_LEN 12
33
-#define PWD_MAX_LEN 12
34 23
 String uid;
35 24
 String password;
36 25
 enum Status {
@@ -43,21 +32,16 @@ enum Status {
43 32
 };
44 33
 Status status = Uid;
45 34
 
46
-#define MAX_IDLE_TIME 20
47 35
 unsigned long lastActivity = 0;
48 36
 
49
-#define LOGIN_FAILED_TIME 2
50
-
51
-#define SELF_TEST_INTERVAL 60
52
-#define SELF_TEST_ERROR_INTERVAL 2
53
-#define MAX_SELF_TEST_TIME 5
54 37
 unsigned long lastSelfTest = 0;
55
-#define SELF_TEST_NO_ERROR '0'
56
-int lastSelfTestResult = SELF_TEST_NO_ERROR;
38
+int lastSelfTestResult = ERROR_NONE;
39
+
40
+HostCommunication hostCommunication;
57 41
 
58 42
 void setup() {
59
-    Serial.begin(9600);
60
-    Serial.println("Starting...");
43
+
44
+    hostCommunication.init();
61 45
 
62 46
     lcd.begin(LCD_COLS, LCD_ROWS);
63 47
     lcd.blink();
@@ -140,7 +124,7 @@ bool handleKeyPad()
140 124
 
141 125
 void updateLcd()
142 126
 {
143
-    Serial.println("Update LCD");
127
+    hostCommunication.debug("Update LCD");
144 128
     lcd.clear();
145 129
     if (status ==  Uid || status == Password)
146 130
     {
@@ -159,7 +143,7 @@ void updateLcd()
159 143
     }
160 144
     else if (status == SelfTest) {
161 145
         lcd.print(SELF_TEST_LABEL);
162
-        if (lastSelfTestResult != SELF_TEST_NO_ERROR) {
146
+        if (lastSelfTestResult != ERROR_NONE) {
163 147
             lcd.setCursor(0, 1);
164 148
             lcd.print(SELF_TEST_ERROR_LABEL);
165 149
             lcd.print(lastSelfTestResult);
@@ -191,8 +175,7 @@ void login(String uid, String password)
191 175
 
192 176
     delay(LOGIN_FAILED_TIME * 1000);
193 177
 
194
-    Serial.println(uid);
195
-    Serial.println(password);
178
+    hostCommunication.login(uid, password);
196 179
 
197 180
     status = LoginSuccess;
198 181
     updateLcd();
@@ -204,24 +187,35 @@ void login(String uid, String password)
204 187
 
205 188
 void selfTest()
206 189
 {
207
-    lastSelfTest = SELF_TEST_NO_ERROR;
190
+    lastSelfTest = ERROR_NONE;
208 191
     status = SelfTest;
209 192
     updateLcd();
210 193
 
211 194
     bool test = true;
212 195
     while (test) {
213
-        unsigned long secs = millis() / 1000;
214
-        Serial.println("Self Testing");
215
-        while (Serial.available() == 0 && (millis() / 1000) - secs < MAX_SELF_TEST_TIME);
216
-        if (Serial.available() > 0) {
217
-            lastSelfTestResult = Serial.read();
218
-            if (lastSelfTestResult == SELF_TEST_NO_ERROR) {
219
-                test = false;
220
-            }
221
-            else {
222
-                updateLcd();
223
-                delay(SELF_TEST_ERROR_INTERVAL * 1000);
224
-            }
196
+        hostCommunication.debug("Self Testing");
197
+        hostCommunication.selfTest();
198
+
199
+        HostCommunicationResult res = hostCommunication.read(MAX_SELF_TEST_TIME * 1000);
200
+
201
+        if (!res.isSuccess) {
202
+            lastSelfTestResult = ERROR_HOST_TIMEOUT;
203
+        }
204
+        else if (res.length != sizeof(SERIAL_PACKET_TYPE_INT)) {
205
+            lastSelfTestResult = ERROR_HOST_PROTOCOL;
206
+        }
207
+        else {
208
+            lastSelfTestResult = *(SERIAL_PACKET_TYPE_INT*)res.data;
209
+        }
210
+
211
+        delete res.data;
212
+
213
+        if (lastSelfTestResult == ERROR_NONE) {
214
+            test = false;
215
+        }
216
+        else {
217
+            updateLcd();
218
+            delay(SELF_TEST_ERROR_INTERVAL * 1000);
225 219
         }
226 220
     }
227 221
     lastSelfTest = millis() / 1000;

Loading…
Cancelar
Guardar