Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Scheduling events at exact times
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Scheduling events at exact times

by snej on Sat Sep 12, 2020 6:56 pm

I’m looking at writing a MIDI sequencer in CircuitPython. This involves sending MIDI data (notes) at specific times, while also receiving MIDI input.

If I were writing this in a higher level system I’d use some kind of event-loop and async task API, e.g. DispatchQueues on iOS/macOS. But CircuitPython doesn’t seem to have any of this.

Are there any libraries that would help? The guts of it seem to be maintaining a time-ordered set (probably a priority queue) of abstract events, waiting until exactly the time of the next event, and then calling it. (While monitoring the input channel during the waits.) This seems like something pretty abstractable from my specific task, and it’d be nice not to have to reinvent the wheel.

snej
 
Posts: 2
Joined: Sat Sep 12, 2020 6:47 pm

Re: Scheduling events at exact times

by kevinjwalters on Mon Sep 14, 2020 6:55 am

I've not noticed one but that doesn't mean there isn't one out there. cpx-midi-send-example.py was my primitive attempt at playing back a particular "recorded" MIDI stream. I should have used three decimal places rather than two, btw. For boards that support it, time.monotonic_ns is a better choice too.

Might be worth a look at the Winterbloom Sol EuroRack module as that's based on CircuitPython.

kevinjwalters
 
Posts: 764
Joined: Sun Oct 01, 2017 3:15 pm

Re: Scheduling events at exact times

by tannewt on Mon Sep 14, 2020 4:21 pm

We don't have an async system currently. There is a lot of discussion about it here: https://github.com/adafruit/circuitpython/issues/1380

There is also discussion of a better time/tick api here: https://github.com/adafruit/circuitpython/issues/3410

tannewt
 
Posts: 1837
Joined: Thu Oct 06, 2016 8:48 pm

Re: Scheduling events at exact times

by snej on Mon Sep 14, 2020 4:37 pm

I looked at that 'better time/tick api' issue...

time.monotonic(), ... returns a floating point number of seconds ... loses precision so that after around 18 hours, intervals of 1/64 second can no longer be distinguished


That was pretty surprising, and it's definitely going to mess up my sequencing code. I did a bit of math, and that statement implies that monotonic() returns a 32-bit float. Couldn't it simply return a 64-bit double? I know we're talking about microcontrollers, but anything with the horsepower to run Python should be able to handle IEEE 64-bit, at least in software.

snej
 
Posts: 2
Joined: Sat Sep 12, 2020 6:47 pm

Re: Scheduling events at exact times

by tannewt on Mon Sep 14, 2020 4:59 pm

snej wrote:That was pretty surprising, and it's definitely going to mess up my sequencing code. I did a bit of math, and that statement implies that monotonic() returns a 32-bit float. Couldn't it simply return a 64-bit double? I know we're talking about microcontrollers, but anything with the horsepower to run Python should be able to handle IEEE 64-bit, at least in software.


It's not just a question of CPU power, we also run up against the limit of what code we can store in a microcontroller's flash. So, it's not simple to support 64-bit doubles.

tannewt
 
Posts: 1837
Joined: Thu Oct 06, 2016 8:48 pm

Re: Scheduling events at exact times

by kevinjwalters on Fri Oct 09, 2020 5:37 pm

Again, not quite what you're looking for but this may be useful for others that find this thread as it has examples of one technique for time-based transitions per device: Adafruit Learn: Multi-tasking with CircuitPython.

kevinjwalters
 
Posts: 764
Joined: Sun Oct 01, 2017 3:15 pm

Re: Scheduling events at exact times

by kevinjwalters on Sun Oct 11, 2020 12:47 pm

I stumbled across someone else with a similar problem: StackOverflow: Automatically create n calls of a function within a while loop?. This one could theoretically be neatened up with a thread approach or a very basic simulation library. I see Big Python has a library called SimPy for the latter which has a feature to run in real time, maybe that could be stripped back to run in CircuitPython?

kevinjwalters
 
Posts: 764
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.