LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2011, 10:55 AM   #1
Fjerr Fjerrson
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Rep: Reputation: Disabled
Bash script problem - using "read" and Festival TTS


Hey there,

I keep running into a problem whereby my script does not act as I would like it to.

What my goal is:
To read an input (USB RFID reader - emulates PS/2 keyboard via USB) and then have the script perform an action based on the input from the RFID reader. In this case, I want my script to use festival to read out a line of text when a certain card is swiped. The RFID reader reads the swiped tag and outputs a 10-digit number, followed by a line break. This is factory default operation and cannot be customised as this is a sealed unit with no configurable options.

Problem:
Unfortunately the RFID reader is quite sensitive and occasionally double-scans the tag, resulting in e.g. 1234567890\n1234567890\n rather than just 1234567890\n

When swiping the tag and it reads correctly, the script works fine. When a double(or more) swipe occurs, the script acts fine, but then once it has read and spoken, it then does this again and again until all of the duplicate reads are spoken.

I have tried various solutions (e.g. "sleep 3" after the "echo text to say | festival --tts" line, clearing and unsetting the myTag variable, adding an extra "rubbish" variable to the read command but these do not help).

Please find below my latest version of the script so far:

Code:
#!/bin/bash
while(true); do
      clear
      read -p "swipe tag: " -n 10 myTag rubbish
      if [ $myTag == "1234567890" ]; then
            echo "you swiped my tag" | festival --tts
      else
            echo "you did not swipe my tag" | festival --tts
      fi
done
I've been puzzling over this for a week now and I just can't seem to see what I've done wrong here. I look forward to seeing some fresh perspectives on this.
 
Old 09-04-2011, 11:22 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You could try skipping input happening within e.g. 3 seconds of the last-used input:
Code:
#!/bin/bash

time=0

while(true); do
      clear
      read -p "swipe tag: " -n 10 myTag #rubbish
      new_time="$(date +%s)"
      ((new_time-3>time)) || continue
      time=$new_time

      if [ $myTag == "1234567890" ]; then
            echo "you swiped my tag" | festival --tts
      else
            echo "you did not swipe my tag" | festival --tts
      fi
done
Kevin Barry
 
Old 09-04-2011, 11:32 AM   #3
Fjerr Fjerrson
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
You could try skipping input happening within e.g. 3 seconds of the last-used input:
Code:
#!/bin/bash

time=0

while(true); do
      clear
      read -p "swipe tag: " -n 10 myTag #rubbish
      new_time="$(date +%s)"
      ((new_time-3>time)) || continue
      time=$new_time

      if [ $myTag == "1234567890" ]; then
            echo "you swiped my tag" | festival --tts
      else
            echo "you did not swipe my tag" | festival --tts
      fi
done
Kevin Barry
Hey, thanks for the speedy reply!

I tried adding your suggestion to my code but unfortunately it still acts the same:

swipe tag: *swipes* <--- double-read
festival: "you swiped my tag"
*while loop restarts*
festival: "you swiped my tag"
swipe tag: *waiting for user*

Last edited by Fjerr Fjerrson; 09-04-2011 at 11:46 AM. Reason: error
 
Old 09-04-2011, 12:01 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Fjerr Fjerrson View Post
Hey, thanks for the speedy reply!

I tried adding your suggestion to my code but unfortunately it still acts the same:
Maybe this then. It will clear all waiting data just before the prompt.
Code:
#!/bin/bash

while(true); do
      clear
      while read -t 0.0001 rubbish; do :; done
      read -p "swipe tag: " -n 10 myTag #rubbish

      if [ $myTag == "1234567890" ]; then
            echo "you swiped my tag" | festival --tts
      else
            echo "you did not swipe my tag" | festival --tts
      fi
done
Note that the timeout doesn't work correctly when set to 0 for some reason.
Kevin Barry
 
1 members found this post helpful.
Old 09-04-2011, 12:05 PM   #5
Fjerr Fjerrson
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by ta0kira View Post
Maybe this then. It will clear all waiting data just before the prompt.
Code:
#!/bin/bash

while(true); do
      clear
      while read -t 0.0001 rubbish; do :; done
      read -p "swipe tag: " -n 10 myTag #rubbish

      if [ $myTag == "1234567890" ]; then
            echo "you swiped my tag" | festival --tts
      else
            echo "you did not swipe my tag" | festival --tts
      fi
done
Note that the timeout doesn't work correctly when set to 0 for some reason.
Kevin Barry
Fantastic! That has completely solved the problem! Thank you very much for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] Bash script problem "No such file or directory" cnmoore Programming 23 03-31-2011 03:55 PM
Login Script for KDE, I want to use Festival --tts chickenlinux Linux - Software 8 08-16-2008 08:39 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration