📄 pcspeakerdriver.java
字号:
package org.jnode.driver.sound.speaker.pc;
import javax.naming.NameNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.Driver;
import org.jnode.driver.DriverException;
import org.jnode.driver.sound.speaker.Note;
import org.jnode.driver.sound.speaker.SpeakerAPI;
import org.jnode.naming.InitialNaming;
import org.jnode.system.IOResource;
import org.jnode.system.ResourceManager;
import org.jnode.system.ResourceNotFreeException;
/** A driver for the internal speaker of a IBM Compatable machine.
* @author Matt Paine
**/
public class PCSpeakerDriver extends Driver implements SpeakerAPI
{
//********** constants **********//
/** The port for the speaker **/
public final static int SPEAKER_PORT = 0x61;
/** The PIT Control Port **/
public final static int CONTROL_PORT = 0x43;
/** The PIT Channel 2 Port **/
public final static int CHANNEL2_PORT = 0x42;
/** The base frequency for the PIT **/
public final static int BASE_FREQUENCY = 1193100;
//********** private variables **********//
/** This holds the reference to the IOResource we need to manipulate. **/
private IOResource speakIO;
/** This holds the reference to the IOResource for the PIT **/
private IOResource pitIO;
//********** Driver implementation **********//
/** A routine that claims all the resources nessasary to run the PCSpeaker. **/
public void startDevice() throws DriverException
{
try
{
final Device dev = getDevice();
final ResourceManager rm = (ResourceManager) InitialNaming.lookup(ResourceManager.NAME);
speakIO = rm.claimIOResource(dev, SPEAKER_PORT, 1);
pitIO = rm.claimIOResource(dev, CHANNEL2_PORT, 2);
getDevice().registerAPI(SpeakerAPI.class, this);
// do a test beep during startup
//beep();
}
catch (NameNotFoundException nnfex) { throw new DriverException (nnfex); }
catch (ResourceNotFreeException rnfex) { throw new DriverException (rnfex); }
}
/** A routine that releases all the resources back to the operating system. **/
public void stopDevice() throws DriverException
{
getDevice().unregisterAPI(SpeakerAPI.class);
pitIO.release();
speakIO.release();
}
//********** Speaker implementation **********//
public void beep()
{
// backup the port, and start the beep
int oldPort = speakIO.inPortByte(SPEAKER_PORT);
int newValue = oldPort | 0x03; // 0b0000_0011;
speakIO.outPortByte(SPEAKER_PORT, newValue);
// sleep for the duration of the beep
try { Thread.sleep(125); }
catch (InterruptedException iex) { }
// restore the speaker port
speakIO.outPortByte(SPEAKER_PORT, oldPort);
}
public void playNote (Note n)
{
pitIO.outPortByte (CONTROL_PORT, 0xb6);
playNote (n.getNote(), n.getLength());
}
public void playNote (Note[] n)
{
pitIO.outPortByte (CONTROL_PORT, 0xb6);
for (int x = 0; x < n.length; x++)
playNote(n[x].getNote(), n[x].getLength());
}
public void playNote (int frequency, int length)
{
int freq = (BASE_FREQUENCY/frequency);
pitIO.outPortByte(CHANNEL2_PORT, (byte)(freq & 0xff));
pitIO.outPortByte(CHANNEL2_PORT, (byte)(freq >> 8));
// backup the port, and start the beep
int oldPort = speakIO.inPortByte(SPEAKER_PORT);
int newValue = oldPort | 0x03; // 0b0000_0011;
speakIO.outPortByte(SPEAKER_PORT, newValue);
// sleep for the duration of the beep
try { Thread.sleep(length); }
catch (InterruptedException iex) { }
// restore the speaker port
speakIO.outPortByte(SPEAKER_PORT, oldPort);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -