How can I get words instead of letters in my Braille to Text conversion project?

139 views Asked by At

I am working on a braille to text project and I created a braille keyboard using 7 push buttons. The output is display in a 128x64 lcd display. But it gives only letters. How can I get the words instead of letters?

And I need to remove the unnecessary parts from this code too.

Here is the Python code -

`

import time
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

def speek(data):
   mytext = data
   language = 'en'
'''
define pin for lcd
'''
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
delay = 1



# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 11
LCD_D4 = 12
LCD_D5 = 13
LCD_D6 = 15
LCD_D7 = 16
switch_1 = 18
switch_2 = 22
switch_3 = 29
switch_4 = 31
switch_5 = 32
switch_6 = 33
switch_7 = 36

GPIO.setup(LCD_E, GPIO.OUT)  # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6

GPIO.setup(LCD_D7, GPIO.OUT) # DB7
GPIO.setup(switch_1, GPIO.IN) # DB7
GPIO.setup(switch_2, GPIO.IN) # DB7
GPIO.setup(switch_3, GPIO.IN) # DB7
GPIO.setup(switch_4, GPIO.IN) # DB7
GPIO.setup(switch_5, GPIO.IN) # DB7
GPIO.setup(switch_6, GPIO.IN) # DB7
GPIO.setup(switch_7, GPIO.IN) # DB7

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

'''
Function Name :lcd_init()
Function Description : this function is used to initialized lcd by sending the different commands
'''
def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)
'''
Function Name :lcd_byte(bits ,mode)
Fuction Name :the main purpose of this function to convert the byte data into bit and send to lcd port
'''
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command
 
  GPIO.output(LCD_RS, mode) # RS
 
  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
 
  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)
 
  # Toggle 'Enable' pin
  lcd_toggle_enable()
'''
Function Name : lcd_toggle_enable()
Function Description:basically this is used to toggle Enable pin
'''
def lcd_toggle_enable():
  # Toggle enable
  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)
  time.sleep(E_DELAY)
'''
Function Name :lcd_string(message,line)
Function  Description :print the data on lcd
'''
def lcd_string(message,line):
  # Send string to display
 
  message = message.ljust(LCD_WIDTH," ")
 
  lcd_byte(line, LCD_CMD)
 
  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

lcd_init()
lcd_string("Welcome ",LCD_LINE_1)
time.sleep(2)
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
# Define delay between readings
delay = 5
lcd_string("Give Input ",LCD_LINE_1)
time.sleep(1)
speak_data =""
while 1:
  # Print out results
  switch1_data =GPIO.input(switch_1)
  time.sleep(0.2)
  switch2_data =GPIO.input(switch_2)
  time.sleep(0.2)
  switch3_data =GPIO.input(switch_3)
  time.sleep(0.2)
  switch4_data =GPIO.input(switch_4)
  time.sleep(0.2)
  switch5_data =GPIO.input(switch_5)
  time.sleep(0.2)
  switch6_data =GPIO.input(switch_6)
  time.sleep(0.2)

  if(switch1_data ==1):
   speak_data = "a"
 
  if((switch1_data ==1) and (switch2_data == 1)):
   speak_data = "b"

  if((switch1_data ==1) and (switch4_data == 1)):
   speak_data = "c"

  if((switch1_data ==1) and (switch4_data == 1)and (switch5_data == 1)):
   speak_data = "d"

  if((switch1_data ==1) and (switch5_data == 1)):
   speak_data = "e"
 
  if((switch1_data ==1) and (switch2_data == 1) and (switch4_data == 1)):
   speak_data = "f"  

  if((switch1_data ==1) and (switch2_data == 1) and (switch4_data == 1) and (switch5_data == 1)):
   speak_data = "g"    
   
  if((switch1_data ==1) and (switch2_data == 1) and (switch5_data == 1)):
   speak_data = "H"    

  if((switch2_data == 1) and (switch4_data == 1)):
   speak_data = "i"    
   
  if((switch2_data == 1) and (switch4_data == 1)and (switch5_data == 1)):
   speak_data = "j"    

  if((switch2_data == 1)and (switch3_data == 1)):
   speak_data = "k"  
   
  if((switch1_data ==1) and (switch2_data == 1) and (switch3_data == 1)):
   speak_data = "l"    
   
  if((switch1_data ==1) and (switch3_data == 1)and (switch4_data == 1)):
   speak_data = "m"  

  if((switch1_data ==1) and (switch3_data == 1)and (switch4_data == 1)and (switch5_data == 1)):
   speak_data = "n"    

  if((switch1_data ==1) and (switch3_data == 1)and (switch5_data == 1)):
   speak_data = "o"    
   
  if((switch1_data ==1)  and (switch2_data == 1) and (switch3_data == 1)and (switch4_data == 1)):
   speak_data = "p"  
   
  if((switch1_data ==1)  and (switch2_data == 1)and (switch3_data == 1)and (switch4_data == 1)and (switch5_data == 1)):
   speak_data = "q"
   
  if((switch1_data ==1)  and (switch2_data == 1) and (switch3_data == 1)and (switch5_data == 1)):
   speak_data = "r"    
   
  if((switch2_data ==1)  and (switch3_data == 1)and (switch4_data == 1)):
   speak_data = "s"  
   
  if((switch2_data ==1)  and (switch3_data == 1) and (switch4_data == 1)and (switch5_data == 1)):
   speak_data = "t"
   
  if((switch1_data ==1)  and (switch3_data == 1)and (switch6_data == 1)):
   speak_data = "u"    
   
  if((switch1_data ==1) and (switch2_data == 1) and (switch3_data == 1) and (switch6_data == 1)):
   speak_data = "v"  

  if((switch2_data ==1) and (switch4_data == 1) and (switch5_data == 1) and (switch6_data == 1)):
   speak_data = "w"  
   
  if((switch1_data ==1) and (switch4_data == 1)and (switch3_data == 1)and (switch6_data == 1)):
   speak_data = "x"  
   
  if((switch1_data ==1)and (switch3_data == 1) and (switch4_data == 1) and (switch5_data == 1) and (switch6_data == 1)):
   speak_data = "y"  
   
  if((switch1_data ==1)and (switch3_data == 1)  and (switch5_data == 1) and (switch6_data == 1)):
   speak_data = "z"
   
  if GPIO.input(switch_7):
   lcd_string(str(speak_data),LCD_LINE_2)  
   speek(speak_data)

`

Display output

2

There are 2 answers

2
d-dutchview On

assuming you're code works as it is impossible for me to test it. try and do something like this:

returnstring = "" #this is what will contain all letters
if(switch1_data ==1):
   returnstring += "a" ##append it to the string
 
  if((switch1_data ==1) and (switch2_data == 1)):
   returnstring += "b" ##append it to the string

then once you have the entire word:

  if GPIO.input(switch_7):
   lcd_string(str(returnstring),LCD_LINE_2)  
   speek(returnstring) ## return the actual final string

again I have not tested it this theory but this is just my quick thoughts on it try and give it a go.

0
user19077881 On

Some ideas for simplifying your code shown below as a reduced form, specifically using a translation dictionary to lose all the if statements. Extension to cover all the letters and switches is I think obvious.

d = { '100000': 'a',
      '110000': 'b'
      }

switch_data = ['0' for _ in range(6)]

switch_data[0] = str(1) # replace with str(GPIO.input(switch_1))
switch_data[1] = str(0) # replace with str(GPIO.input(switch_2))

switch_code = ''.join(switch_data)  # join characters into string

print('The letter is ', d[switch_code])