Using an IO pin interrupt + Alarm
kbman99 last edited by kbman99
So I'm building a device using the LoPy + Pytrack where I need to have an Alarm set where every n seconds some readings are taken from sensors and sent over LoRa to my gateway, but I also need to have a physical interrupt which will send the same data as well, but it needs to be able to interrupt the Alarm if it's running (this is the main issue). Is there a way where I can set an Alarm, but allow an IO pin interrupt to override the alarm and do its thing, then once it's finished continue the alarm. Would the best method be to set a function which will return the alarm object, and when an interrupt comes up I cancel the alarm, run what I need to, then setup the alarm again?
This is what I have currently, I still have to actually test if this will work, but this does NOT implement cancelling or reinitializing the alarm. Would it be necessary to do so?
I will basically connect IO 7 (Pin 10) to IO 2 (PWR_EN) and set the pull up resistor according to this pin out
Something like this...
def get_coords(gps): print(gps.coordinates(debug=True)) print("Alarm func") def get_coords_pin(gps): print(gps.coordinates(debug=True)) print("Interrupt func") def setup_timer(func_to_run, arg, time_interval, periodic=False): return Timer.Alarm(func_to_run, s=time_interval, arg=arg, periodic=periodic) node = setup_node() py = Pytrack() gps = gps_setup.setup_gps() pin = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) pin.callback(Pin.IRQ_FALLING, get_coords_pin, arg=gps) try: timer = setup_timer(get_coords, gps, 5, True) except Exception as e: print(e) print("Setup complete!")
I basically have it so that a pin is set as input and a callback to function to read the coordinates is run whenever the pin reads a falling edge. Then a Timer object is set based on the input to the setup and is set to run every 5 seconds periodically. Would I run into any hangups in having it this way? I feel like if the timer function is running (it can take a couple seconds to finish) and the button is pressed while the timer is callback runs, it will block the pin callback. As I said I have not tested this yes, so I will be doing so tonight. Just figured I'd ask
kbman99 last edited by kbman99
@gregcope Wondering if it might be useful to simply start a thread for everything. If I spawn an alarm in one thread, an interrupt in another and so on, I may be able to avoid the issue of blocking.
The major issue is that since some of the functions I need to run can take 20 seconds or so, I don't want one to block another from running and have to wait a whole other interval for the respective functions to be run. Especially since there are multiple interrupts and one or two alarms.
So would simply running a function that spawns an alarm within a separate thread make it so that the alarm itself is contained in that thread indefinitely?
gregcope last edited by
@kbman99 i would suggest running your code in a loop, and within the interrupt handler just set a flag, that you main loop checks. Otherwise if i understand you right repeated hits on the switch could have it just going in circles. Also look into debouncing.