From 1d5ce610d31f2e918a47cdee73907c73bef7b57e Mon Sep 17 00:00:00 2001 From: unlockable Date: Fri, 7 Jul 2023 02:24:37 +0800 Subject: [PATCH] Real Homework4. --- Homework/Homework4/Bluetooth/Bluetooth.ino | 20 +++--- Homework/Homework4/Homework4.ino | 38 ------------ Homework/Homework4/Homework4/Homework4.ino | 71 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 48 deletions(-) delete mode 100644 Homework/Homework4/Homework4.ino create mode 100644 Homework/Homework4/Homework4/Homework4.ino diff --git a/Homework/Homework4/Bluetooth/Bluetooth.ino b/Homework/Homework4/Bluetooth/Bluetooth.ino index 7673be4..77c9ecc 100644 --- a/Homework/Homework4/Bluetooth/Bluetooth.ino +++ b/Homework/Homework4/Bluetooth/Bluetooth.ino @@ -13,17 +13,17 @@ void setup() { } void loop() { - if (Serial.available()) { - char ch = Serial.read(); - Serial.print(ch); - soft_serial.print(ch); + while (Serial.available()) { + String input = Serial.readString(); + Serial.print(input); + soft_serial.print(input); } - if (soft_serial.available()) { - char ch = soft_serial.read(); - Serial.print(ch); - soft_serial.print(ch); + while (soft_serial.available()) { + String input = soft_serial.readString(); + Serial.print(input); + soft_serial.print(input); } - Serial.println("A loop"); + // Serial.println("A loop"); // Serial.println("A loop"); // if (soft_serial.availableForWrite()) { // Serial.println("Available"); @@ -34,5 +34,5 @@ void loop() { // if (Serial.availableForWrite()) { // Serial.println("Can Write"); // } - delay(1000); + // delay(1000); } \ No newline at end of file diff --git a/Homework/Homework4/Homework4.ino b/Homework/Homework4/Homework4.ino deleted file mode 100644 index 43d874a..0000000 --- a/Homework/Homework4/Homework4.ino +++ /dev/null @@ -1,38 +0,0 @@ -#include "SoftwareSerial.h" -#include "U8glib.h" - -const byte bt_tx_connected_to = 11; -const byte bt_rx_connected_to = 12; - -String pass = "0721"; -String recovery_pass - -SoftwareSerial bt_serial(bt_tx_connected_to, bt_rx_connected_to); -U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); - -void setup() { - Serial.begin(9600); - bt_serial.begin(9600); - u8g.setFont(u8g_font_courR08); -} - -void loop() { - draw_locked(); - delay(1000); - draw_unlocked(); - delay(1000); -} - -void draw_unlocked() { - u8g.firstPage(); - do { - u8g.drawStr(0, 16, "Unlocked"); - } while (u8g.nextPage()); -} - -void draw_locked() { - u8g.firstPage(); - do { - u8g.drawStr(0, 8, "Locked"); - } while (u8g.nextPage()); -} \ No newline at end of file diff --git a/Homework/Homework4/Homework4/Homework4.ino b/Homework/Homework4/Homework4/Homework4.ino new file mode 100644 index 0000000..d781b87 --- /dev/null +++ b/Homework/Homework4/Homework4/Homework4.ino @@ -0,0 +1,71 @@ +#include "SoftwareSerial.h" +#include "U8glib.h" +#define UNLOCKED 0 +#define LOCKED 1 + +const byte rx_connected = 11; +const byte tx_connected = 10; + +const String password = "0721"; +const String reset_pass = "1145"; + +byte status = LOCKED; + +String input = ""; + +U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); // I2C / TWI + +SoftwareSerial soft_serial(tx_connected, rx_connected); + +void setup() { + pinMode(tx_connected, INPUT); + pinMode(rx_connected, OUTPUT); + Serial.begin(9600); + soft_serial.begin(9600); + u8g.setFont(u8g_font_courR08); + draw_locked(); +} + +void loop() { + while (soft_serial.available() && input.length() <= 4) { + input += soft_serial.readString(); + } + + if (input.length() < 4) { + return; + } + switch (status) { + case LOCKED: + if (input.substring(0, 4) == password) { + Serial.println("PASS"); + status = UNLOCKED; + draw_unlocked(); + } + else { + Serial.println("FAIL"); + } + input = ""; + break; + case UNLOCKED: + if (input.substring(0, 4) == reset_pass) { + status = LOCKED; + draw_locked(); + } + input = ""; + break; + } +} + +void draw_unlocked() { + u8g.firstPage(); + do { + u8g.drawStr(0, 16, "Unlocked"); + } while (u8g.nextPage()); +} + +void draw_locked() { + u8g.firstPage(); + do { + u8g.drawStr(0, 8, "Locked"); + } while (u8g.nextPage()); +} \ No newline at end of file