Pārlūkot izejas kodu

lcd and keypad working; begin business

develop
Robin Thoni 8 gadus atpakaļ
vecāks
revīzija
24d265b71d
4 mainītis faili ar 2579 papildinājumiem un 0 dzēšanām
  1. 16
    0
      CMakeLists.txt
  2. 89
    0
      cmake/ArduinoToolchain.cmake
  3. 2304
    0
      cmake/Platform/Arduino.cmake
  4. 170
    0
      main.ino

+ 16
- 0
CMakeLists.txt Parādīt failu

@@ -0,0 +1,16 @@
1
+cmake_minimum_required(VERSION 2.8.4)
2
+set(CMAKE_VERBOSE_MAKEFILE TRUE)
3
+set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
4
+set(PROJECT_NAME digicode)
5
+project(${PROJECT_NAME})
6
+
7
+#set(${CMAKE_PROJECT_NAME}_BOARD mega2560)
8
+#set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyACM0)
9
+
10
+set(${CMAKE_PROJECT_NAME}_BOARD nano328)
11
+set(${CMAKE_PROJECT_NAME}_PORT /dev/ttyUSB0)
12
+
13
+enable_language(ASM)
14
+
15
+set(${CMAKE_PROJECT_NAME}_SKETCH main.ino)
16
+generate_arduino_firmware(${CMAKE_PROJECT_NAME})

+ 89
- 0
cmake/ArduinoToolchain.cmake Parādīt failu

@@ -0,0 +1,89 @@
1
+#=============================================================================#
2
+# Author: Tomasz Bogdal (QueezyTheGreat)
3
+# Home:   https://github.com/queezythegreat/arduino-cmake
4
+#
5
+# This Source Code Form is subject to the terms of the Mozilla Public
6
+# License, v. 2.0. If a copy of the MPL was not distributed with this file,
7
+# You can obtain one at http://mozilla.org/MPL/2.0/.
8
+#=============================================================================#
9
+set(CMAKE_SYSTEM_NAME Arduino)
10
+
11
+set(CMAKE_C_COMPILER   avr-gcc)
12
+set(CMAKE_CXX_COMPILER avr-g++)
13
+
14
+# Add current directory to CMake Module path automatically
15
+if(EXISTS  ${CMAKE_CURRENT_LIST_DIR}/Platform/Arduino.cmake)
16
+    set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR})
17
+endif()
18
+
19
+#=============================================================================#
20
+#                         System Paths                                        #
21
+#=============================================================================#
22
+if(UNIX)
23
+    include(Platform/UnixPaths)
24
+    if(APPLE)
25
+        list(APPEND CMAKE_SYSTEM_PREFIX_PATH ~/Applications
26
+                                             /Applications
27
+                                             /Developer/Applications
28
+                                             /sw        # Fink
29
+                                             /opt/local) # MacPorts
30
+    endif()
31
+elseif(WIN32)
32
+    include(Platform/WindowsPaths)
33
+endif()
34
+
35
+
36
+#=============================================================================#
37
+#                         Detect Arduino SDK                                  #
38
+#=============================================================================#
39
+if(NOT ARDUINO_SDK_PATH)
40
+    set(ARDUINO_PATHS)
41
+
42
+    foreach(DETECT_VERSION_MAJOR 1)
43
+        foreach(DETECT_VERSION_MINOR RANGE 5 0)
44
+            list(APPEND ARDUINO_PATHS arduino-${DETECT_VERSION_MAJOR}.${DETECT_VERSION_MINOR})
45
+            foreach(DETECT_VERSION_PATCH  RANGE 3 0)
46
+                list(APPEND ARDUINO_PATHS arduino-${DETECT_VERSION_MAJOR}.${DETECT_VERSION_MINOR}.${DETECT_VERSION_PATCH})
47
+            endforeach()
48
+        endforeach()
49
+    endforeach()
50
+
51
+    foreach(VERSION RANGE 23 19)
52
+        list(APPEND ARDUINO_PATHS arduino-00${VERSION})
53
+    endforeach()
54
+
55
+    if(UNIX)
56
+        file(GLOB SDK_PATH_HINTS /usr/share/arduino*
57
+            /opt/local/arduino*
58
+            /opt/arduino*
59
+            /usr/local/share/arduino*)
60
+    elseif(WIN32)
61
+        set(SDK_PATH_HINTS "C:\\Program Files\\Arduino"
62
+            "C:\\Program Files (x86)\\Arduino"
63
+            )
64
+    endif()
65
+    list(SORT SDK_PATH_HINTS)
66
+    list(REVERSE SDK_PATH_HINTS)
67
+endif()
68
+
69
+find_path(ARDUINO_SDK_PATH
70
+          NAMES lib/version.txt
71
+          PATH_SUFFIXES share/arduino
72
+                        Arduino.app/Contents/Java/
73
+                        Arduino.app/Contents/Resources/Java/
74
+                        ${ARDUINO_PATHS}
75
+          HINTS ${SDK_PATH_HINTS}
76
+          DOC "Arduino SDK path.")
77
+
78
+if(ARDUINO_SDK_PATH)
79
+    list(APPEND CMAKE_SYSTEM_PREFIX_PATH ${ARDUINO_SDK_PATH}/hardware/tools/avr)
80
+    list(APPEND CMAKE_SYSTEM_PREFIX_PATH ${ARDUINO_SDK_PATH}/hardware/tools/avr/utils)
81
+else()
82
+    message(FATAL_ERROR "Could not find Arduino SDK (set ARDUINO_SDK_PATH)!")
83
+endif()
84
+
85
+set(ARDUINO_CPUMENU)
86
+if(ARDUINO_CPU)
87
+    set(ARDUINO_CPUMENU ".menu.cpu.${ARDUINO_CPU}")
88
+endif(ARDUINO_CPU)
89
+

+ 2304
- 0
cmake/Platform/Arduino.cmake
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 170
- 0
main.ino Parādīt failu

@@ -0,0 +1,170 @@
1
+#include <Arduino.h>
2
+#include <Keypad/Keypad.h>
3
+#include <LiquidCrystal.h>
4
+
5
+
6
+#define KP_ROWS 4
7
+#define KP_COLS 4
8
+char keys[KP_ROWS][KP_COLS] = {
9
+        {'1','2','3','A'},
10
+        {'4','5','6','B'},
11
+        {'7','8','9','C'},
12
+        {'*','0','#','.'}
13
+};
14
+byte rowPins[KP_ROWS] = {A3, A2, A0, A1};
15
+byte colPins[KP_COLS] = {8, 9, 10, 11};
16
+Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);
17
+
18
+
19
+#define LCD_ROWS 2
20
+#define LCD_COLS 16
21
+LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
22
+
23
+
24
+#define SELF_TEST_LABEL "Self testing..."
25
+#define UID_LABEL "UID:"
26
+#define PWD_LABEL "PWD:"
27
+#define UID_MAX_LEN 12
28
+#define PWD_MAX_LEN 12
29
+String uid;
30
+String password;
31
+enum Status {
32
+    Uid,
33
+    Password,
34
+    SelfTest
35
+};
36
+Status status = Uid;
37
+
38
+#define MAX_IDLE_TIME 20
39
+unsigned long lastActivity = 0;
40
+#define SELF_TEST_INTERVAL 10
41
+unsigned long lastSelfTest = 0;
42
+
43
+void setup() {
44
+    Serial.begin(9600);
45
+    Serial.println("Starting...");
46
+
47
+    lcd.begin(LCD_COLS, LCD_ROWS);
48
+    lcd.blink();
49
+    lcd.noAutoscroll();
50
+
51
+    reset();
52
+}
53
+
54
+String makeChars(char c, int count) {
55
+    char str[count + 1];
56
+    memset(str, c, count);
57
+    str[count] = 0;
58
+    return String(str);
59
+}
60
+
61
+bool handleUidPassword(String& str, int maxLength, char key) {
62
+    int len = str.length();
63
+    if (key >= '0' && key <= '9') {
64
+        if (len < maxLength) {
65
+            str += key;
66
+            return true;
67
+        }
68
+    }
69
+    else if (key == 'C' || key == '*') {
70
+        if (len > 0) {
71
+            str = str.substring(0, len - 2);
72
+            return true;
73
+        }
74
+    }
75
+    return false;
76
+}
77
+
78
+void loop() {
79
+    unsigned long secs = millis() / 1000;
80
+    if (handleKeyPad()) {
81
+        lastActivity = secs;
82
+        updateLcd();
83
+    }
84
+    if ((uid.length() != 0 || password.length() != 0) && secs - lastActivity >= MAX_IDLE_TIME) {
85
+        reset();
86
+    }
87
+    if (uid.length() == 0 && password.length() == 0 && secs - lastSelfTest >= SELF_TEST_INTERVAL) {
88
+        selfTest();
89
+    }
90
+}
91
+
92
+bool handleKeyPad()
93
+{
94
+    char key = keypad.getKey();
95
+    if (key != NO_KEY) {
96
+        if (status == Uid) {
97
+            if (key == '#') {
98
+                if (uid.length() != 0) {
99
+                    status = Password;
100
+                    return true;
101
+                }
102
+            }
103
+            else {
104
+                return handleUidPassword(uid, UID_MAX_LEN, key);
105
+            }
106
+        }
107
+        else if (status == Password) {
108
+            if (key == '#') {
109
+                if (password.length() != 0) {
110
+                    login(uid, password);
111
+                    return true;
112
+                }
113
+            }
114
+            else {
115
+                return handleUidPassword(password, PWD_MAX_LEN, key);
116
+            }
117
+        }
118
+    }
119
+    return false;
120
+}
121
+
122
+void updateLcd()
123
+{
124
+    Serial.println("Update LCD");
125
+    lcd.clear();
126
+    if (status ==  Uid || status == Password)
127
+    {
128
+        lcd.setCursor(0, 0);
129
+        lcd.print(UID_LABEL);
130
+        lcd.print(uid);
131
+        lcd.setCursor(0, 1);
132
+        lcd.print(PWD_LABEL);
133
+        lcd.print(makeChars('*', password.length()));
134
+        if (status == Uid) {
135
+            lcd.setCursor(strlen(UID_LABEL) + uid.length(), 0);
136
+        }
137
+        else {
138
+            lcd.setCursor(strlen(PWD_LABEL) + password.length(), 1);
139
+        }
140
+    }
141
+    else if (status == SelfTest) {
142
+        lcd.print(SELF_TEST_LABEL);
143
+    }
144
+}
145
+
146
+void reset()
147
+{
148
+    status = Uid;
149
+    uid = "";
150
+    password = "";
151
+    updateLcd();
152
+}
153
+
154
+void login(String uid, String password)
155
+{
156
+    Serial.println(uid);
157
+    Serial.println(password);
158
+    reset();
159
+}
160
+
161
+void selfTest()
162
+{
163
+    status = SelfTest;
164
+    updateLcd();
165
+    Serial.println("Self Testing");
166
+//    while (Serial.available() == 0);
167
+    Serial.read();
168
+    lastSelfTest = millis() / 1000;
169
+    reset();
170
+}

Notiek ielāde…
Atcelt
Saglabāt