找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 6995|回复: 10

针对声音传感器封装了一个仿电报码识别的工具类

[复制链接]
发表于 2011-7-14 23:56:53 | 显示全部楼层 |阅读模式
本帖最后由 cloudor 于 2011-7-24 10:01 编辑

为了让声音传感器识别多种指令。我把声音按照长短音的序列识别出来,这样就能够用嘴巴发出“滴滴~滴"的电报码来对NXT发出多种语音指令控制。

其中main方法就是使用例子。
比如,
发的是三个短音,收到的cmd就是“...”;
发的是两个长音,收到的cmd就是"--"。

电报码调试

电报码调试



import java.util.ArrayList;
import java.util.List;

import lejos.nxt.Button;
import lejos.nxt.SensorPort;
import lejos.nxt.SensorPortListener;
import lejos.nxt.Sound;
import lejos.nxt.SoundSensor;
import lejos.nxt.comm.RConsole;

/**
* @author PuYun <cloudor@gmail.com>
* @version $Id$
*
*/
public class BeepInput implements SensorPortListener, Runnable
{
    public static interface Listener
    {
        void beeps(String cmd);
    }

    public static void main(String[] args)
    {
        Sound.beep();
        RConsole.open();
        BeepInput input = new BeepInput(SensorPort.S1);
        input.addListener(new Listener()
        {
            public void beeps(String cmd)
            {
                System.out.println(cmd);
            }
        });
        Button.waitForPress();
        Sound.beepSequence();
        RConsole.close();
    }

    private int beepCommandInterval;

    private String beepsCache = "";

    private long lastBeep;

    private List<Listener> listenerList = new ArrayList<Listener>();

    private Object lock = new Object();

    private int longBeepTime;

    private Thread monitor;

    private volatile long noiseEnd;

    private long noiseStart;

    private int shortBeepTime;

    private SoundSensor sound;

    public BeepInput(SensorPort port)
    {
        this(port, 50, 400, 1000);
    }

    public BeepInput(SensorPort port, int shortBeepTime, int longBeepTime,
            int beepCommandInterval)
    {
        this.sound = new SoundSensor(port, true);
        this.shortBeepTime = shortBeepTime;
        this.longBeepTime = longBeepTime;
        this.beepCommandInterval = beepCommandInterval;
        port.addSensorPortListener(this);
        monitor = new Thread(this, "BeepMonitor");
        monitor.setDaemon(true);
        monitor.start();
    }

    public void addListener(Listener l)
    {
        listenerList.add(l);
    }

    public void removeListener(Listener l)
    {
        listenerList.remove(l);
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        while (true)
        {
            Thread.yield();
            if (sound.readValue() > 3)
            {
                continue;
            }
            long current = System.currentTimeMillis();
            if (noiseEnd > 0)
            {
                boolean quietForAWhile = false;
                synchronized (lock)
                {
                    quietForAWhile = noiseEnd > 0 && current - noiseEnd > 20;
                }
                if (quietForAWhile)
                {
                    long time = noiseEnd - noiseStart;
                    if (time > longBeepTime)
                    {
                        beepsCache += "-";
                        lastBeep = current;
                    } else if (time > shortBeepTime)
                    {
                        beepsCache += ".";
                        lastBeep = current;
                    }
                    synchronized (lock)
                    {
                        noiseStart = -1;
                        noiseEnd = -1;
                    }
                }
            } else if (lastBeep > 0 && current - lastBeep > beepCommandInterval)
            {
                for (Listener l : listenerList)
                {
                    l.beeps(beepsCache);
                }
                lastBeep = 0;
                beepsCache = "";
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see lejos.nxt.SensorPortListener#stateChanged(lejos.nxt.SensorPort, int, int)
     */
    public void stateChanged(SensorPort aSource, int aOldValue, int aNewValue)
    {
        int value = sound.readValue();
        if (value > 3)
        {
            synchronized (lock)
            {
                if (noiseStart <= 0)
                {
                    noiseStart = System.currentTimeMillis();
                } else
                {
                    noiseEnd = System.currentTimeMillis();
                }
            }
        }
    }
}

如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2011-7-15 10:59:26 | 显示全部楼层
这个就= =||看不懂了……
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-7-15 11:50:19 | 显示全部楼层
有没有.net的?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-7-15 12:40:02 | 显示全部楼层
有.rbt的吗?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-7-15 17:12:27 | 显示全部楼层
这是个java的类吧
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2011-7-24 10:00:29 | 显示全部楼层
main方法就是使用例子。
比如,
发的是三个短音,收到的cmd就是“...”;
发的是两个长音,收到的cmd就是"--"。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2011-7-24 10:02:23 | 显示全部楼层
lcy2000 发表于 2011-7-15 17:12
这是个java的类吧

是的,这是lejos版嘛,内容当然是java的了。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-7-28 12:24:59 | 显示全部楼层
俺是搞C++的,不懂。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-11-28 19:48:55 | 显示全部楼层
怎么联系啊
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-11-28 19:51:53 | 显示全部楼层
其中main方法就是使用例子。
比如,
发的是三个短音,收到的cmd就是“...”;
发的是两个长音,收到的cmd就是"--"。
这个东西要那里找啊
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2016-12-11 16:43:36 | 显示全部楼层
赞一个,不知道ev3上是否可以实现?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-4-16 19:50 , Processed in 0.090879 second(s), 22 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表