On Apr 9, 2014, at 12:35 AM, Terry Reedy <tjre...@udel.edu> wrote: > On 4/8/2014 4:09 PM, Grawburg wrote: >> >> 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? > > It depends on whether you can set up your system so that pushing the button > generates an interrupt. But I know little about R.Pi and less about the 'port > expander'. If there were an interrupt, you would just have to write an > interrupt handler. When possible, this is much better than polling. > > -- > Terry Jan Reedy >
I think what the OP was asking for was a way in standard Python to “lock" the value of an instance variable, which of course, you really can’t do. However, what he (I assume it’s a he) could do is arrange his calling program so that after the button method returns a “1”, the method isn’t called again. That is, test for truth of button = 1 in the calling program, and if true, skip the call. The next time the program runs, the button value will be re-initialized to zero and everything is back to square one. -Bill -- https://mail.python.org/mailman/listinfo/python-list