From 32ec9336088799c42d8aa7ace6648802e1b267cf Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Thu, 7 Nov 2024 17:00:19 +0100 Subject: did alot of designing and testing out on a linux machine --- .DS_Store | Bin 6148 -> 6148 bytes .idea/workspace.xml | 51 +++++++++ bin/icon.png | Bin 0 -> 3754 bytes bin/kasper_gui.py | 10 ++ bin/kasper_source.py | 163 +++++++++++++++++++++++++++++ bin/script.py | 109 ------------------- documentation/lafvin_starterkit.pdf | Bin 9311241 -> 0 bytes documentation/rpi.pdf | Bin 19405814 -> 0 bytes recourses/.DS_Store | Bin 6148 -> 0 bytes recourses/Buzzer.py | 56 ---------- recourses/Keypad.py | 202 ------------------------------------ recourses/Membrane_Switch_Module.py | 27 ----- 12 files changed, 224 insertions(+), 394 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 bin/icon.png create mode 100644 bin/kasper_gui.py create mode 100644 bin/kasper_source.py delete mode 100644 bin/script.py delete mode 100644 documentation/lafvin_starterkit.pdf delete mode 100644 documentation/rpi.pdf delete mode 100644 recourses/.DS_Store delete mode 100644 recourses/Buzzer.py delete mode 100644 recourses/Keypad.py delete mode 100644 recourses/Membrane_Switch_Module.py diff --git a/.DS_Store b/.DS_Store index 0f8dc76..8eba22d 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..21b26aa --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + 1730971563473 + + + + \ No newline at end of file diff --git a/bin/icon.png b/bin/icon.png new file mode 100644 index 0000000..c372c49 Binary files /dev/null and b/bin/icon.png differ diff --git a/bin/kasper_gui.py b/bin/kasper_gui.py new file mode 100644 index 0000000..2826638 --- /dev/null +++ b/bin/kasper_gui.py @@ -0,0 +1,10 @@ +import tkinter +from tkinter import PhotoImage + +display = tkinter.Tk() +display.title("Kasper") +photo = PhotoImage(file = "icon.png") +display.iconphoto(False, photo) + + +display.mainloop() \ No newline at end of file diff --git a/bin/kasper_source.py b/bin/kasper_source.py new file mode 100644 index 0000000..d8b0599 --- /dev/null +++ b/bin/kasper_source.py @@ -0,0 +1,163 @@ +from smbus2 import SMBus +from time import sleep +from gpiozero import CPUTemperature +from rpi_lcd import LCD + +import speech_recognition as sr +import sounddevice +import os + +ALIGN_FUNC = { + 'left': 'ljust', + 'right': 'rjust', + 'center': 'center'} +CLEAR_DISPLAY = 0x01 +ENABLE_BIT = 0b00000100 +LINES = { + 1: 0x80, + 2: 0xC0, + 3: 0x94, + 4: 0xD4} + +LCD_BACKLIGHT = 0x08 +LCD_NOBACKLIGHT = 0x00 + +ERROR_BAD_REQUEST = '400' +ERROR_UNAUTHORIZED = '401' +ERROR_NOT_FOUND = '404' +ERROR_TIMEOUT = '408' + +class LCD(object): + + def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): + self.address = address + self.bus = SMBus(bus) + self.delay = 0.0005 + self.rows = rows + self.width = width + self.backlight_status = backlight + + self.write(0x33) + self.write(0x32) + self.write(0x06) + self.write(0x0C) + self.write(0x28) + self.write(CLEAR_DISPLAY) + sleep(self.delay) + + def _write_byte(self, byte): + self.bus.write_byte(self.address, byte) + self.bus.write_byte(self.address, (byte | ENABLE_BIT)) + sleep(self.delay) + self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) + sleep(self.delay) + + def write(self, byte, mode=0): + backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT + self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) + + def text(self, text, line, align='left'): + self.write(LINES.get(line, LINES[1])) + text, other_lines = self.get_text_line(text) + text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) + for char in text: + self.write(ord(char), mode=1) + if other_lines and line <= self.rows - 1: + self.text(other_lines, line + 1, align=align) + + def backlight(self, turn_on=True): + self.backlight_status = turn_on + self.write(0) + + def get_text_line(self, text): + line_break = self.width + if len(text) > self.width: + line_break = text[:self.width + 1].rfind(' ') + if line_break < 0: + line_break = self.width + return text[:line_break], text[line_break:].strip() + + def clear(self): + self.write(CLEAR_DISPLAY) + +LCD_DISPLAY = LCD() +VOICE_REC = sr.Recognizer() +MIC = sr.Microphone() +PROCES_LOAD = os.getloadavg() +TIME = current_time.time() +UPTIME = time.CLOCK_UPTIME() +CPU_TEMP = CPUTemperature() + +# clearing the lcd from any text that was on it before the program started to ensure smooth operations +lcd.clear() + +# Listening to the user's voice and putting it into a variable +def listen_voice(): + global audio + with mic as source: + r.adjust_for_ambient_noise(source) + audio = r.listen(source) + return audio + +# Transcribing the audio to text and printing it out +# Using the Google Speech Recognizer +def recognize_speech(audio): + try: + words = r.recognize_google(audio) + LCD_DISPLAY.text(words, 1) + print(f"Printing on screen: {words}") + except sr.UnknownValueError: + LCD_DISPLAY.text(ERROR_BAD_REQUEST, 1) + print(ERROR_BAD_REQUEST) + except sr.RequestError: + LCD_DISPLAY.text(ERROR_UNAUTHORIZED, 1) + print(ERROR_UNAUTHORIZED) + +def CPU_INFO(): + print("you chose to display the cpou") + while (True): + LCD.text(PROCES_LOAD(),1,left) + +def CURRENT_TIME(): + while True: + backlight_mode = True + LCD.text(TIME,2,center) + +def UPTIME(): + while True: + LCD.text(UPTIME,1,left) + +def CPU_TEMP(): + while True: + LCD.text(cpu.temperature) + +def CPU_LOAD(): + backlight_mode = True + LCD.text(PROCES_LOAD,1,left) + +def NOTES(): + count = 0 + user_notes = input() + for i in user_notes: + while count < 20: + lcd.text(i,1,left) + count += 1 + + +OPTIONS = ["CPU_CLOCK", "TIME", "UPTIME", "CPU_TEMP", "CPU_LOAD", "NOTES", "SPEECH_TRANSCRIBER"] + +def PROGRAM(USER_INPUT): + print("WELCOME TO THE I2C COMMAND LINE CENTER \n WHAT DO YOU WISH TO DO? ") + print(OPTIONS) + + FOUND = False + while FOUND == False: + + USER_INPUT = input().upper() + for i in OPTIONS: + if i == USER_INPUT: + FOUND = True + else: + print(ERROR_NOT_FOUND) + +PROGRAM() \ No newline at end of file diff --git a/bin/script.py b/bin/script.py deleted file mode 100644 index 5c33638..0000000 --- a/bin/script.py +++ /dev/null @@ -1,109 +0,0 @@ -from smbus2 import SMBus -from time import sleep - -import speech_recognition as sr -import sounddevice -import os - -ALIGN_FUNC = { - 'left': 'ljust', - 'right': 'rjust', - 'center': 'center'} -CLEAR_DISPLAY = 0x01 -ENABLE_BIT = 0b00000100 -LINES = { - 1: 0x80, - 2: 0xC0, - 3: 0x94, - 4: 0xD4} - -LCD_BACKLIGHT = 0x08 -LCD_NOBACKLIGHT = 0x00 - -ERROR_BAD_REQUEST = '400' -ERROR_UNAUTHORIZED = '401' -ERROR_NOT_FOUND = '404' -ERROR_TIMEOUT = '408' - -class LCD(object): - - def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): - self.address = address - self.bus = SMBus(bus) - self.delay = 0.0005 - self.rows = rows - self.width = width - self.backlight_status = backlight - - self.write(0x33) - self.write(0x32) - self.write(0x06) - self.write(0x0C) - self.write(0x28) - self.write(CLEAR_DISPLAY) - sleep(self.delay) - - def _write_byte(self, byte): - self.bus.write_byte(self.address, byte) - self.bus.write_byte(self.address, (byte | ENABLE_BIT)) - sleep(self.delay) - self.bus.write_byte(self.address,(byte & ~ENABLE_BIT)) - sleep(self.delay) - - def write(self, byte, mode=0): - backlight_mode = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT - self._write_byte(mode | ((byte << 4) & 0xF0) | backlight_mode) - - def text(self, text, line, align='left'): - self.write(LINES.get(line, LINES[1])) - text, other_lines = self.get_text_line(text) - text = getattr(text, ALIGN_FUNC.get(align, 'ljust'))(self.width) - for char in text: - self.write(ord(char), mode=1) - if other_lines and line <= self.rows - 1: - self.text(other_lines, line + 1, align=align) - - def backlight(self, turn_on=True): - self.backlight_status = turn_on - self.write(0) - - def get_text_line(self, text): - line_break = self.width - if len(text) > self.width: - line_break = text[:self.width + 1].rfind(' ') - if line_break < 0: - line_break = self.width - return text[:line_break], text[line_break:].strip() - - def clear(self): - self.write(CLEAR_DISPLAY) - - - -LCD = LCD() -VOICE_REC = sr.Recognizer() -MIC = sr.Microphone() -PROCES_LOAD = os.getloadavg() -TIME = current_time.time() -UPTIME = time.CLOCK_UPTIME() - - - -def CPU_INFO(): - while (True): - LCD.text(PROCES_LOAD(),1,left) - -def CURRENT_TIME(): - while True: - backlight_mode = true - LCD.text(UPTIME,1,left) - LCD.text(TIME,2,center) - - - - -OPTIONS = ["CPU_CLOCK", "TIME", "UPTIME", "CPU_TEMP", "CPU LOAD", "NOTES"] - -def USER_GUI(): - print("WELCOME TO THE I2C COMMAND LINE CENTER \n WHAT DO YOU WISH TO DO? ") - print(OPTIONS) diff --git a/documentation/lafvin_starterkit.pdf b/documentation/lafvin_starterkit.pdf deleted file mode 100644 index 9735753..0000000 Binary files a/documentation/lafvin_starterkit.pdf and /dev/null differ diff --git a/documentation/rpi.pdf b/documentation/rpi.pdf deleted file mode 100644 index a4a9d10..0000000 Binary files a/documentation/rpi.pdf and /dev/null differ diff --git a/recourses/.DS_Store b/recourses/.DS_Store deleted file mode 100644 index 94a5988..0000000 Binary files a/recourses/.DS_Store and /dev/null differ diff --git a/recourses/Buzzer.py b/recourses/Buzzer.py deleted file mode 100644 index 8555184..0000000 --- a/recourses/Buzzer.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python - -import RPi.GPIO as GPIO -import time - -# Set #17 as buzzer pin -BeepPin = 17 - -def print_message(): - print("========================================") - print ("| Beep |") - print ("| ------------------------------ |") - print ("| Buzzer connect to GPIO0 |") - print ("| |") - print ("| Make Buzzer beep |") - print ("| |") - print ("| |") - print ("========================================\n") - print 'Program is running...' - print 'Please press Ctrl+C to end the program...' - raw_input ("Press Enter to begin\n") - -def setup(): - # Set the GPIO modes to BCM Numbering - GPIO.setmode(GPIO.BCM) - # Set LedPin's mode to output, - # and initial level to High(3.3v) - GPIO.setup(BeepPin, GPIO.OUT, initial=GPIO.HIGH) - -def main(): - print_message() - while True: - # Buzzer on (Beep) - print 'Buzzer On' - GPIO.output(BeepPin, GPIO.LOW) - time.sleep(0.1) - # Buzzer off - print 'Buzzer Off' - GPIO.output(BeepPin, GPIO.HIGH) - time.sleep(0.1) - -def destroy(): - # Turn off buzzer - GPIO.output(BeepPin, GPIO.HIGH) - # Release resource - GPIO.cleanup() - -# If run this script directly, do: -if __name__ == '__main__': - setup() - try: - main() - # When 'Ctrl+C' is pressed, the child program - # destroy() will be executed. - except KeyboardInterrupt: - destroy() \ No newline at end of file diff --git a/recourses/Keypad.py b/recourses/Keypad.py deleted file mode 100644 index f762b4b..0000000 --- a/recourses/Keypad.py +++ /dev/null @@ -1,202 +0,0 @@ -#!/usr/bin/env python3 - -import RPi.GPIO as GPIO -import time -#class Key:Define some of the properties of Key -class Key(object): - NO_KEY = '\0' - #Defines the four states of Key - IDLE = 0 - PRESSED = 1 - HOLD = 2 - RELEASED = 3 - #define OPEN and CLOSED - OPEN = 0 - CLOSED =1 - #constructor - def __init__(self): - self.kchar = self.NO_KEY - self.kstate = self.IDLE - self.kcode = -1 - self.stateChanged = False - -class Keypad(object): - NULL = '\0' - LIST_MAX = 10 #Max number of keys on the active list. - MAPSIZE = 10 #MAPSIZE is the number of rows (times 16 columns) - bitMap = [0]*MAPSIZE - key = [Key()]*LIST_MAX - holdTime = 500 #key hold time - holdTimer = 0 - startTime = 0 - #Allows custom keymap, pin configuration, and keypad sizes. - def __init__(self,usrKeyMap,row_Pins,col_Pins,num_Rows,num_Cols): - GPIO.setmode(GPIO.BOARD) - self.rowPins = row_Pins - self.colPins = col_Pins - self.numRows = num_Rows - self.numCols = num_Cols - - self.keymap = usrKeyMap - self.setDebounceTime(10) - #Returns a single key only. Retained for backwards compatibility. - def getKey(self): - single_key = True - if(self.getKeys() and self.key[0].stateChanged and (self.key[0].kstate == self.key[0].PRESSED)): - return self.key[0].kchar - single_key = False - return self.key[0].NO_KEY - #Populate the key list. - def getKeys(self): - keyActivity = False - #Limit how often the keypad is scanned. - if((time.time() - self.startTime) > self.debounceTime*0.001): - self.scanKeys() - keyActivity = self.updateList() - self.startTime = time.time() - return keyActivity - #Hardware scan ,the result store in bitMap - def scanKeys(self): - #Re-intialize the row pins. Allows sharing these pins with other hardware. - for pin_r in self.rowPins: - GPIO.setup(pin_r,GPIO.IN,pull_up_down = GPIO.PUD_UP) - #bitMap stores ALL the keys that are being pressed. - for pin_c in self.colPins: - GPIO.setup(pin_c,GPIO.OUT) - GPIO.output(pin_c,GPIO.LOW) - for r in self.rowPins: #keypress is active low so invert to high. - self.bitMap[self.rowPins.index(r)] = self.bitWrite(self.bitMap[self.rowPins.index(r)],self.colPins.index(pin_c),not GPIO.input(r)) - #Set pin to high impedance input. Effectively ends column pulse. - GPIO.output(pin_c,GPIO.HIGH) - GPIO.setup(pin_c,GPIO.IN) - #Manage the list without rearranging the keys. Returns true if any keys on the list changed state. - def updateList(self): - anyActivity = False - kk = Key() - #Delete any IDLE keys - for i in range(self.LIST_MAX): - if(self.key[i].kstate == kk.IDLE): - self.key[i].kchar = kk.NO_KEY - self.key[i].kcode = -1 - self.key[i].stateChanged = False - # Add new keys to empty slots in the key list. - for r in range(self.numRows): - for c in range(self.numCols): - button = self.bitRead(self.bitMap[r],c) - keyChar = self.keymap[r * self.numCols +c] - keyCode = r * self.numCols +c - idx = self.findInList(keyCode) - #Key is already on the list so set its next state. - if(idx > -1): - self.nextKeyState(idx,button) - #Key is NOT on the list so add it. - if((idx == -1) and button): - for i in range(self.LIST_MAX): - if(self.key[i].kchar == kk.NO_KEY): #Find an empty slot or don't add key to list. - self.key[i].kchar = keyChar - self.key[i].kcode = keyCode - self.key[i].kstate = kk.IDLE #Keys NOT on the list have an initial state of IDLE. - self.nextKeyState(i,button) - break #Don't fill all the empty slots with the same key. - #Report if the user changed the state of any key. - for i in range(self.LIST_MAX): - if(self.key[i].stateChanged): - anyActivity = True - return anyActivity - #This function is a state machine but is also used for debouncing the keys. - def nextKeyState(self,idx, button): - self.key[idx].stateChanged = False - kk = Key() - if(self.key[idx].kstate == kk.IDLE): - if(button == kk.CLOSED): - self.transitionTo(idx,kk.PRESSED) - self.holdTimer = time.time() #Get ready for next HOLD state. - elif(self.key[idx].kstate == kk.PRESSED): - if((time.time() - self.holdTimer) > self.holdTime*0.001): #Waiting for a key HOLD... - self.transitionTo(idx,kk.HOLD) - elif(button == kk.OPEN): # or for a key to be RELEASED. - self.transitionTo(idx,kk.RELEASED) - elif(self.key[idx].kstate == kk.HOLD): - if(button == kk.OPEN): - self.transitionTo(idx,kk.RELEASED) - elif(self.key[idx].kstate == kk.RELEASED): - self.transitionTo(idx,kk.IDLE) - - def transitionTo(self,idx,nextState): - self.key[idx].kstate = nextState - self.key[idx].stateChanged = True - #Search by code for a key in the list of active keys. - #Returns -1 if not found or the index into the list of active keys. - def findInList(self,keyCode): - for i in range(self.LIST_MAX): - if(self.key[i].kcode == keyCode): - return i - return -1 - #set Debounce Time, The default is 50ms - def setDebounceTime(self,ms): - self.debounceTime = ms - #set HoldTime,The default is 500ms - def setHoldTime(self,ms): - self.holdTime = ms - # - def isPressed(keyChar): - for i in range(self.LIST_MAX): - if(self.key[i].kchar == keyChar): - if(self.key[i].kstate == self.self.key[i].PRESSED and self.key[i].stateChanged): - return True - return False - # - def waitForKey(): - kk = Key() - waitKey = kk.NO_KEY - while(waitKey == kk.NO_KEY): - waitKey = getKey() - return waitKey - - def getState(): - return self.key[0].kstate - # - def keyStateChanged(): - return self.key[0].stateChanged - - def bitWrite(self,x,n,b): - if(b): - x |= (1<>n)&1 == 1): - return True - else: - return False - -ROWS = 4 -COLS = 4 -keys = [ '1','2','3','A', - '4','5','6','B', - '7','8','9','C', - '*','0','#','D' ] -rowsPins = [12,16,18,22] -colsPins = [19,15,13,11] - -def loop(): - keypad = Keypad(keys,rowsPins,colsPins,ROWS,COLS) - keypad.setDebounceTime(50) - while(True): - key = keypad.getKey() - if(key != keypad.NULL): - print ("You Pressed Key : %c "%(key) ) - -if __name__ == '__main__': # Program start from here - print ("Program is starting ... ") - try: - loop() - except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. - pass - GPIO.cleanup() - - - - - diff --git a/recourses/Membrane_Switch_Module.py b/recourses/Membrane_Switch_Module.py deleted file mode 100644 index 168bcf9..0000000 --- a/recourses/Membrane_Switch_Module.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python3 - -import RPi.GPIO as GPIO -import Keypad #import module Keypad -ROWS = 4 # number of rows of the Keypad -COLS = 4 #number of columns of the Keypad -keys = [ '1','2','3','A', #key code - '4','5','6','B', - '7','8','9','C', - '*','0','#','D' ] -rowsPins = [12,16,18,22] #connect to the row pinouts of the keypad -colsPins = [19,15,13,11] #connect to the column pinouts of the keypad - -def loop(): - keypad = Keypad.Keypad(keys,rowsPins,colsPins,ROWS,COLS) #creat Keypad object - keypad.setDebounceTime(50) #set the debounce time - while(True): - key = keypad.getKey() #obtain the state of keys - if(key != keypad.NULL): #if there is key pressed, print its key code. - print ("You Pressed Key : %c "%(key)) - -if __name__ == '__main__': #Program start from here - print ("Program is starting ... ") - try: - loop() - except KeyboardInterrupt: #When 'Ctrl+C' is pressed, exit the program. - GPIO.cleanup() -- cgit v1.2.3-70-g09d2