Virtual Peripherals
edgellmh
Posts: 85
I discovered that I can solve my problem in an earlier post of needing too many VPs by removing and installing them as needed. This means I need to create a custom VirtualPeripheral. Can I just write the code in Javelin as a class extending VirtualPeripheral or do I need to do something special to get my extended class to function? i do not have my Javelin Stamp ahrdware yet so I cannot test my code which follows.
import stamp.core.*;
public class ManualButtonChecker01 extends VirtualPeripheral{
/*
* This class reads the state of manual button presses and latches presses
* until the microcontroller gets around to checking the state and reacting.
* That is, the microcontroller may be doing some time consuming task when
* a button is pressed and hence this checking needs to be done in the background
*
*
*/
//Class variables
static boolean openButtonPinState = false; //Active HIGH button
static boolean closeButtonPinState = false; //Active HIGH button
//Instance variables
int openButtonPin;
int closeButtonPin;
// Constructor
public ManualButtonChecker01(int _openButtonPin, int _closeButtonPin){
openButtonPin = _openButtonPin;
closeButtonPin = _closeButtonPin;
}
public void getState(){
if(CPU.readPin(openButtonPin) == true){
openButtonPinState = true; //This latches the state
}
if(CPU.readPin(closeButtonPin) == true){
closeButtonPinState = true; //This latches the state
}
}
public void resetState(){
//Only do this after reading the latched state
openButtonPinState = false;
closeButtonPinState = false;
}
}
marshall edgell
import stamp.core.*;
public class ManualButtonChecker01 extends VirtualPeripheral{
/*
* This class reads the state of manual button presses and latches presses
* until the microcontroller gets around to checking the state and reacting.
* That is, the microcontroller may be doing some time consuming task when
* a button is pressed and hence this checking needs to be done in the background
*
*
*/
//Class variables
static boolean openButtonPinState = false; //Active HIGH button
static boolean closeButtonPinState = false; //Active HIGH button
//Instance variables
int openButtonPin;
int closeButtonPin;
// Constructor
public ManualButtonChecker01(int _openButtonPin, int _closeButtonPin){
openButtonPin = _openButtonPin;
closeButtonPin = _closeButtonPin;
}
public void getState(){
if(CPU.readPin(openButtonPin) == true){
openButtonPinState = true; //This latches the state
}
if(CPU.readPin(closeButtonPin) == true){
closeButtonPinState = true; //This latches the state
}
}
public void resetState(){
//Only do this after reading the latched state
openButtonPinState = false;
closeButtonPinState = false;
}
}
marshall edgell
Comments
You can poll for the buttonpress during your main loop.
static void main() {
· //initialisation code here
· while (true) {
··· //do something
··· //poll button
··· //do something
· }
}
As long as you return within 0.1 sec to your main loop
from other code you run, you will not miss a button press
which usually lasts a few hundred milliseconds.
regards peter
That is my problem. Some of the things in the main loop take way more than 0.1 second.
Can I catch the button presses with hardware?
marshall
Can he use a capacitor which will discharge to the pin for longeur than a button? If the capacitor take 0,5s to discharge, it will be like if the button is press for 0,5s, no?
But don't ask me how to do that, I absolutly don't know.
JM
http://forums.parallax.com/showthread.php?p=730357
Another option is to change the routine that takes alot of time
into a statemachine, allowing you to return to the main loop
faster, or keep polling the button from within·the long routine
and setting a flag so you can serve the button
after the long routine finishes.
regards peter
·