Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69908 > unrolled thread
| Started by | Grawburg <grawburg@myglnc.com> |
|---|---|
| First post | 2014-04-08 16:09 -0400 |
| Last post | 2014-04-09 15:43 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
"Latching" variables in function Grawburg <grawburg@myglnc.com> - 2014-04-08 16:09 -0400
Re: "Latching" variables in function Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-09 00:28 +0000
Re: "Latching" variables in function Mark H Harris <harrismh777@gmail.com> - 2014-04-09 08:53 -0500
Re: "Latching" variables in function alister <alister.nospam.ware@ntlworld.com> - 2014-04-09 15:43 +0000
| From | Grawburg <grawburg@myglnc.com> |
|---|---|
| Date | 2014-04-08 16:09 -0400 |
| Subject | "Latching" variables in function |
| Message-ID | <mailman.9045.1397000922.18130.python-list@python.org> |
I've probably used the wrong term - I'm thinking of what I do when writing PLC code - so I can't find how to do this in my reference books. This is part of a project I'm working on with a Raspberry Pi and an MCP23017 port expander. I have a N/O pushbutton that I want to "latch" a value to a variable when it's been pressed. I have this function that gets called periodically in a 'while True' statement: def button(): pushbutton = 0 button_value = 0 pushbutton=bus.read_byte_data(address,GPIOB) if pushbutton > 0: button_value = 1 return button_value I need button_value to become '1' when the button is pressed and to remain '1' until the entire program (only about 25 lines) ends with a sys.exit() What do I use to 'latch' button_value? Brian Grawburg North Carolina -- The truth will set you free . . .but first it will infuriate you.
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-04-09 00:28 +0000 |
| Message-ID | <li247k$rtj$1@dont-email.me> |
| In reply to | #69908 |
On Tue, 08 Apr 2014 16:09:28 -0400, Grawburg wrote:
> def button():
> pushbutton = 0
> button_value = 0
> pushbutton=bus.read_byte_data(address,GPIOB)
> if pushbutton > 0:
> button_value = 1
> return button_value
Every time your function is called, you start out with button_value of 0.
You may need a global variable that starts out as False (or 0), and once
flipped to True (or 1) and then stays there:
button_value = False # or: button_value = 0
def button():
global button_value
pushbutton = bus.read_byte_data( address, GPIOB )
if pushbutton > 0:
button_value = True # or: button_value = 1
return button_value
Also I think I'd probably pass the IO address as a parameter to the
button function.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-04-09 08:53 -0500 |
| Message-ID | <mailman.9076.1397051608.18130.python-list@python.org> |
| In reply to | #69908 |
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
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-04-09 15:43 +0000 |
| Message-ID | <FUd1v.90724$rf1.40465@fx08.am4> |
| In reply to | #69951 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web