generated from cepdnaclk/eYY-XXX-project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/lahirumenik/e19-co227-Keybo…
- Loading branch information
Showing
12 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import asyncio | ||
|
||
from src.SerialReader import * | ||
from src.BluetoothReader import * | ||
|
||
if __name__ == "__main__": | ||
#agent = ESPSerialReader() | ||
#async def bluetooth(): | ||
#agent = ESPBluetoothReader() | ||
#await agent.connect() | ||
#while True: | ||
#data = await agent.read() | ||
#if data: | ||
#print(data) | ||
|
||
#asyncio.run(bluetooth()) | ||
agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Author : Dasun Theekshana | ||
# Date : 19/09/2023 | ||
# File : SerialReader.py | ||
|
||
import asyncio | ||
from bleak import BleakScanner, BleakClient | ||
|
||
service_uuid = '4fafc201-1fb5-459e-8fcc-c5c9c331914b' # Service UUID | ||
characteristic_uuid = 'beb5483e-36e1-4688-b7f5-ea07361b26a8' # Characteristic UUID | ||
|
||
class ESPBluetoothReader: | ||
def __init__(self): | ||
self.macaddrs = None | ||
self.name = None | ||
self.devices = None | ||
self.client = None | ||
|
||
async def scan(self): | ||
self.devices = await BleakScanner.discover(return_adv=True) | ||
for device,data in self.devices.items(): | ||
print(f"Name: {data[1].local_name} MAC: {device} RSSI: {data[1].rssi}") | ||
|
||
async def isavalable(self): | ||
await self.scan() | ||
if self.devices: | ||
devicename = input("Enter the Devices Name: ") | ||
for device,data in self.devices.items(): | ||
name = data[1].local_name | ||
if devicename == name: | ||
self.macaddrs = device | ||
self.name = name | ||
return True | ||
print("Device not Found") | ||
return False | ||
|
||
async def connect(self): | ||
if await self.isavalable(): | ||
self.client = BleakClient(self.macaddrs) | ||
try: | ||
await self.client.connect() | ||
print(f"Connected to {self.name}") | ||
return True | ||
|
||
except Exception as e: | ||
print(f"Error Connecting: {str(e)}") | ||
return False | ||
|
||
async def disconnect(self): | ||
try: | ||
await self.client.disconnect() | ||
print(f"{self.name} disonnected") | ||
self.name = None | ||
self.macaddrs = None | ||
self.client = None | ||
return True | ||
except Exception as e: | ||
print(f"Error Disonnecting: {str(e)}") | ||
return False | ||
|
||
async def read(self): | ||
try: | ||
data = await self.client.read_gatt_char(characteristic_uuid) | ||
if data: | ||
return data.decode('utf-8') | ||
return False | ||
except Exception as e: | ||
print(f"Error Reading: {str(e)}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Author : Dasun Theekshana | ||
# Date : 19/09/2023 | ||
# File : SerialReader.py | ||
|
||
import serial | ||
import serial.tools.list_ports | ||
|
||
class ESPSerialReader: | ||
def __init__(self): | ||
self.port = None # Stores the name of the ESP COM port (e.g., COMx) | ||
self.ser = None # Stores the serial connection object | ||
|
||
def scan(self): | ||
""" | ||
Scan the device ports to find ESP COM port | ||
return -> COMx | ||
""" | ||
esp_port =None | ||
|
||
# List available serial ports | ||
available_ports = list(serial.tools.list_ports.comports()) | ||
|
||
# Iterate through Port to Find the ESP Board | ||
for port in available_ports: | ||
port_name = port.device | ||
port_discription = port.description | ||
try: | ||
print(f"Try to connect to {port_name}") | ||
|
||
# Open a connection with the port at 9600 baud rate with a timeout of 1 second | ||
ser = serial.Serial(port_name, 9600, timeout=1) | ||
|
||
# If the port description matches the ESP board's description, identify it | ||
if "CP210x USB to UART Bridge" in port_discription: | ||
print(f"ESP Board: {port_name}") | ||
esp_port = port_name | ||
break | ||
|
||
# Close the connection | ||
ser.close() | ||
|
||
except Exception as e: | ||
print(f"Failed to connect to {port_name}: {str(e)}") | ||
|
||
return esp_port | ||
|
||
def isavalable(self): | ||
# Check if an ESP board is available by calling the scan method | ||
self.port = self.scan() | ||
if self.port: | ||
return True | ||
return False | ||
|
||
def connect(self): | ||
try: | ||
if self.isavalable(): | ||
# If an ESP board is available, establish a serial connection to it | ||
self.ser = serial.Serial(self.port, 9600) | ||
print(f"Key Board Connected via : {self.port}") | ||
return True | ||
return False | ||
except Exception as e: | ||
print(f"Error Connecting: {str(e)}") | ||
return False | ||
|
||
def read(self): | ||
try: | ||
data = self.ser.read().decode('utf-8') # Read data from the serial connection | ||
if data: | ||
return data | ||
return False | ||
except Exception as e: | ||
print(f"Error Disonnecting: {str(e)}") | ||
return False |
Binary file not shown.
Binary file not shown.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const int buttonPins[] = {2, 3, 4, 5, 6, 10, 11, 12, 13}; | ||
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]); // the total size of the array divided by the size of a single element gives you the number of elements in the array. | ||
int pressedKeys[3]; // declare an integer array pressedKeys with a size of 3 to store the currently pressed button values. | ||
int numPressedKeys = 0; | ||
|
||
int sequences[37][9] = { | ||
{2, 3, 4}, {2, 3, 5}, {2, 3, 10}, {2, 3, 11}, {2, 3, 12}, {2, 3, 13}, | ||
{2, 4, 5}, {2, 4, 10}, {2, 4, 11}, {2, 4, 12}, {2, 4, 13}, | ||
{2, 5, 10}, {2, 5, 11}, {2, 5, 12}, {2, 5, 13}, | ||
{2, 10, 11}, {2, 10, 12}, {2, 10, 13}, | ||
{3, 4, 5}, {3, 4, 10}, {3, 4, 11}, {3, 4, 12}, {3, 4, 13}, | ||
{3, 5, 10}, {3, 5, 11}, {3, 5, 12}, {3, 5, 13}, | ||
{3, 10, 11}, {3, 10, 12}, {3, 10, 13}, | ||
{4, 5, 10}, {4, 5, 11}, {4, 5, 12}, {4, 5, 13}, | ||
{4, 10, 11}, {4, 10, 12}, {6} | ||
}; | ||
|
||
const char* characters[37] = { | ||
"A", "B", "C", "D", "E", "F", "G", "H", "I", | ||
"J", "K", "L", "M", "N", "O", "P", "Q", "R", | ||
"S", "T", "U", "V", "W", "X", "Y", "Z", | ||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " " | ||
}; | ||
|
||
//used to compare the currently pressed keys to predefined sequences. | ||
void checkPressedKeys() { | ||
|
||
for (int i = 0; i < 37; i++) { | ||
bool match = true; | ||
for (int j = 0; j < 3; j++) { | ||
if (pressedKeys[j] != sequences[i][j]) { //currently pressed button at position j) is not equal to the value in the sequences array at position [i][j]. If there is a mismatch between the pressed key and the predefined sequence, match is set to false, and the loop breaks. | ||
match = false; //If there is a mismatch between the pressed key and the predefined sequence, match is set to false, and the loop breaks. | ||
break; | ||
} | ||
} | ||
if (match) { | ||
Serial.print("Matched Sequence "); | ||
Serial.print(i + 1); // displays the sequence number (i + 1, because array indices are zero-based) | ||
Serial.print(" => Character: "); | ||
Serial.println(characters[i]); | ||
|
||
for (int j = 0; j < 3; j++) { //resets the pressedKeys array by setting all its elements to 0. This ensures that the code is ready to capture a new button sequence. | ||
pressedKeys[j] = 0; | ||
} | ||
numPressedKeys = 0; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
void setup() { | ||
Serial.begin(9600); | ||
for (int i = 0; i < 36; i++) { | ||
Serial.print("Sequence "); | ||
Serial.print(i + 1); | ||
Serial.print(": "); | ||
|
||
for (int j = 0; j < 3; j++) { | ||
Serial.print(sequences[i][j]); | ||
Serial.print(" "); | ||
} | ||
|
||
Serial.print(" => Character: "); | ||
Serial.println(characters[i]); | ||
} | ||
|
||
for (int i = 0; i < numButtons; i++) { | ||
pinMode(buttonPins[i], INPUT_PULLUP); | ||
} | ||
} | ||
|
||
void loop() { | ||
|
||
for (int i = 0; i < numButtons; i++) { | ||
if (digitalRead(buttonPins[i]) == LOW) { | ||
Serial.println(buttonPins[i]); | ||
pressedKeys[numPressedKeys] = buttonPins[i]; | ||
numPressedKeys++; | ||
delay(500); | ||
} | ||
} | ||
if (numPressedKeys == 3) { | ||
Serial.print(pressedKeys[0]); | ||
Serial.print(pressedKeys[1]); | ||
Serial.println(pressedKeys[2]); | ||
checkPressedKeys(); | ||
|
||
} | ||
|
||
} |