"Latching" variables in function

alister alister.nospam.ware at ntlworld.com
Wed Apr 9 11:43:33 EDT 2014


On Wed, 09 Apr 2014 08:53:19 -0500, Mark H Harris wrote:

> On 4/8/14 3:09 PM, Grawburg wrote:
> 
>> I have a N/O pushbutton that I want to "latch" a value to a variable
>> when it's been pressed.
>> I need button_value to become '1' when the button is pressed and to
>> remain '1' until ...
> 
>> What do I use to 'latch' button_value?
> 
> Philosophically speaking buttons don't latch. You push the button, an
> event is generated, and the call-back handles the event to do something
> in your project.
> 
> You might try setting a global variable on the button-push event.
> 
> Or, if I understand you, you might want to use a configurable, like a
> radio button or a check box, either of which are designed to be
> "latched".
> 
> 
> marcus

How familiar are you with oop?
a class would sort this quite nicely

Class Button(object)
	def __init__(self,bit):
		self.pressed=False:
		self.bit=bit

	def check_button(self):
		if bus.read_byte_data(bit):
			self.pressed=True
		

button=Button(address.GPIOB)
while not button.pressed:
	button.check button()

once button.pressed has been set True it will remain True until you reset 
it with button.pressed=False

+ with a bit of tweaking you should be able to create multiple button 
objects for each switch you connect

	
*note this code has been typed direct & not tested so may contain minor 
errors.




-- 
Imbalance of power corrupts and monopoly of power corrupts absolutely.
		-- Genji



More information about the Python-list mailing list