Iám ciurrently Working on a Little Programm wich should check the current state of an explizit GPIO Pin and than toggle ist. For this iám using java and PI4J. When my Programm starts, the LED is turned of. But as soon as i get it as a variable it automaticly sets the state to HIGH. Dos anyone have an idea how to avoid this ? my code so far:
final GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT);
PinState ledStatus = led.getState();
if (ledStatus.isHigh())
{
led.setShutdownOptions(true, PinState.LOW);
System.out.println("Set shutdownOption LOW");
}
else
{
led.setShutdownOptions(true, PinState.HIGH);
System.out.println("Set shutdownOption HIGH");
}
if(ledStatus.isHigh())
{
System.out.println("LEDS aus...");
}
else
{
System.out.println("LEDS an...");
}
led.toggle();
This Works quite fine. But as i said, before i start the programm... the LED is off! as soon as i get to this line :
GpioPinDigitalOutput led =gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07,PinMode.DIGITAL_OUTPUT);
The LED is ON! i need to figure out a way to get the state before the app is running , change it and then exit my programm with the LED toggled.
Thanks for your Help :)
try using the provisionDigitalMultipurposePin method to set first mode INPUT in order to read the state, and then switch to OUTPUT mode to change the pin state.
GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalMultipurpose led = gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_07, PinMode.DIGITAL_INPUT);
// read state
led.setMode(PinMode.DIGITAL_OUTPUT);
// write state
Related
I am working on a simple music app in android and I have tried adding EnviromentalReverb and PresetReverb to mediaPlayer (wav and m4a formats) but the reverb doesn't apply. There is no change when the audio plays. I have checked whether my device supports the reverb using the below code and it does. I have looked at similar questions on stackoverflow but there isn't an answer that works.
final AudioEffect.Descriptor[] effects = AudioEffect.queryEffects();
// Determine available/supported effects
for (final AudioEffect.Descriptor effect : effects) {
Log.d("Effects", effect.name.toString() + ", type: " + effect.type.toString());
}
The code used for EnvironmentalReverb and PresetReverb is below
First try
EnvironmentalReverb eReverb = new EnvironmentalReverb(1,0);
eReverb.setReverbDelay(85);
eReverb.setEnabled(true);
mMediaPlayer.attachAuxEffect(eReverb.getId());
mMediaPlayer.setAuxEffectSendLevel(1.0f);
Second try
PresetReverb mReverb = new PresetReverb(1, 0);
mReverb.setPreset(PresetReverb.PRESET_LARGEROOM);
mReverb.setEnabled(true);
mMediaPlayer.attachAuxEffect(mReverb.getId());
mMediaPlayer.setAuxEffectSendLevel(1.0f);
Both return 0 for setEnabled(true) but neither work on the audio. Can someone please point me in the right direction? I am not sure what is wrong with the implementation.
Answering my question so it can be helpful for someone else.
I wasn't able to get the PresetReverb to work. The EnvironmentalReverb however was working but to find out whether it was working I had to add seekbars for room level and reverb level so I could alter it in real time.
EnvironmentalReverb eReverb = new EnvironmentalReverb(0,0);
mMediaPlayer.attachAuxEffect(eReverb.getId());
mMediaPlayer.setAuxEffectSendLevel(1.0f);
I enabled the reverb on click of a button and then used seek bars to change the room level and reverb level.
I use a java program for communication between a arduino board and a scratch file. The communication happen well. I use a user interface to start the communication where i have buttons called
connect
close and minimize
When the user clicks the connect button code will check the value in combo box and accordingly it opens the scratch file.
Once the connect button is clicked the control moves to the scratch application. After completing my work when i tried closing the scratch. My scratch application closes as expected but the control does not return to the user interface because of which i am not able to close the application and i close it in net beans forcefully. In the output screen i don't see build successful and instead i get build stopped. That is my process works perfectly until i give connect but once the button is pressed it is hanged up some where.
I tried making it as a jar file and running it in a different machine at that time i use end task in task manager to close the application.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if("Disconnect".equals(jButton1.getText()))
{
System.exit(0);
}
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one port");
}
else
{
Runtime r = Runtime.getRuntime();
try {
//this.hide();
//p = r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");
p = Runtime.getRuntime().exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\scratch files new.sb2");
//Runtime.getRuntime().exec("taskkill /F /IM <p>.exe");
//p.destroy();
//r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
A4S a4sObj = new A4S(new String[] {jComboBox2.getSelectedItem().toString()}); //defaultline
//A4S a4sObj = new A4S(new String[]{"COM16"}); //addedline
//r.gc();
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
//p.destroy();
//System.gc();
// }
}
Here is the code i tried. But none seems to work.
Move all Process related work into separate Thread.
Use waitFor method to recognise Process end - then you are free to
exit your app.
As I can understood you used SWING for creating UI.
You can set
yourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
for your frame. This must help.
I've been attempting to unmute the computer by using a simple java application. However, when I try to do something like:
final Line line = AudioSystem.getLine(Port.Info.SPEAKER);
line.open();
BooleanControl muteControl = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
muteControl.setValue(false);
It does not work. Can anyone lead me in the right direction/
Thanks,
-Jaccob.
Line doesn't provide a setter for this property, you just can't turn on/off the main line that way AFAIK.
IMHO it's may be better that way: I don't know what app you're writing, but as a user, I hate when an application plays with my speaker ^^
In my j2me application i have to play a small sound file each times user click on an item. But the issues is when i play sound file multiple times like after 10-14 times it gives me
out of memory exception. Although i release the player each time i play the file but still it
gives out of memory exception : Here is the code snippet,
public void playSound(String soundFile) {
try{
if (player!=null) {
try {
player.deallocate(); //deallocate the unnecessary memory.
} catch (Exception ex) {
player=null;
System.gc();
}
}
player = Manager.createPlayer(getClass().getResourceAsStream(musicFolder + soundFile), "audio/mpeg");
// player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
if (vc != null) {
vc.setLevel(100);
}
player.prefetch();
player.start();
isException=false;
} catch (Exception e) {
isException=true;
}
}
Can someone tell me what is going wrong?
3 things to keep in mind
If you are going to play the same sound several times, you might want to keep one Player prefetched and simply start it multiple times.
When you want to properly cleanup a player, you should call Player.close()
You may want to use a media event listener to close and/or restart a player independently of user input.
I think you should also call
player.close()
right after after
player.deallocate();
According to documentation "When deallocate returns, the Player is in the UNREALIZED or REALIZED state." but close goes further... "When the method returns, the Player is in the CLOSED state and can no longer be used."
I'm not sure why the de-allocation isn't working. I guess it either takes longer to de-allocated than to create a new one, or the de-allocation fails for some reason. Is there a player.stop() to match the player.start()?
Another thing to try (if nothing else, for good form :) is not to create new player unless you need to/should. I.e. move the
if(player!=null){
So it also covers
player = Manager.createPlayer(getClass().getResourceAsStream(musicFolder + soundFile), "audio/mpeg");
HTH!
I have a piece of code that outputs the player's total experience.
The issue is that the XP value it outputs doesn't change if the actual XP does.
For example, at level 50,000 the XP was 2,147,483,647. When level dropped to 4, the XP value stayed the same.
if (cmd.getName().equalsIgnoreCase("checkxp")) {
// If person is null, it's not a player!
if (person == null) {
sender.sendMessage("This command can only be used by a player, sorry!");
return false;
} else {
int curxp;
Player player = (Player) sender;
curxp = player.getTotalExperience();
sender.sendMessage("You currently have: " + curxp + " XP!");
return true;
}
}
How do I get this value to reset after it was ran so it re-checks the XP and updates the variable with the new amount?
If you don't update your Player object which looks like it's coming from sender it will hold the old values, so I check that sender is coming in with new data.
Looking at ExpSkills, it seems that playing with experience instead of levels is the way to go.
So use setTotalExperience() and getTotalExperience() for now.
Once you get everything working, then try and add support for levels.
Maybe there's a converter somewhere to convert levels to experience and experience to levels?
If not, here are the conversion charts.