Title: Adds an LED_pulse feature using ISR to `devices/grid_maze.py` by mathias-sm · Pull Request #150 · pyControl/code · GitHub
Open Graph Title: Adds an LED_pulse feature using ISR to `devices/grid_maze.py` by mathias-sm · Pull Request #150 · pyControl/code
X Title: Adds an LED_pulse feature using ISR to `devices/grid_maze.py` by mathias-sm · Pull Request #150 · pyControl/code
Description: Hi everyone, Thanks for maintaining this great ecosystem! For an experiment we're running these days, I needed a feature to dim LEDs in a grid_maze setup. I'm doing this with PWM using a MicroPython timer and a callback that turns the LED on and off at the required duty_cycle (fraction of the time on), at the required freqency. The code is heavily inspired from code/source/pyControl/hardware.py Lines 420 to 450 in a54bdca def pulse(self, freq, duty_cycle=50, n_pulses=False, load_warning=True): # Turn on pulsed output with specified frequency and duty cycle. assert duty_cycle % 5 == 0, "duty cycle must be a multiple of 5 between 5 and 95%" if not self.timer: self.timer = pyb.Timer(available_timers.pop()) self.fm = int(100 / next(x for x in (50, 25, 20, 10, 5) if duty_cycle % x == 0)) self.off_ind = int(duty_cycle / 100 * self.fm) self.i = 0 self.n_pulses = n_pulses if self.n_pulses: self.pulse_n = 0 int_freq = freq * self.fm if load_warning and int_freq > 2000: warning("This pulse freq and duty_cycle will use > 10% of pyboard processor resources.") self.timer.init(freq=int_freq) self.timer.callback(self._ISR) self.on() @micropython.native def _ISR(self, t): self.i += 1 if self.i == self.off_ind: if self.n_pulses: self.pulse_n += 1 if self.pulse_n == self.n_pulses: self.off() return self.toggle() elif self.i == self.fm: self.i = 0 self.toggle() but as they are currently written, this maze's LED_on and LED_off are allocating new memory with to_byte code/devices/grid_maze.py Line 162 in a54bdca self.I2C[i2c].mem_write(values.to_bytes(n_bytes, "little"), self.addr[exp_n], self.reg_addr[register], timeout=5) which is incompatible with ISRs (see https://docs.micropython.org/en/latest/reference/isr_rules.html). Instead, I pre-allocate buffers for on and off, and write those in a tight loop: this works, is as computationally light as I could think of, but makes pulsing incompatible with turning on/off other LEDs or delivering rewards. Feel free to merge, or otherwise leave here should anyone else need this feature! Happy to take the maintainers' input of course. This is currently being tested in an experiment, so far we have not had any trouble but I will keep upstreaming fixes if need be. Thanks to @ThomasAkam for his input; some of this code was developed together with Arya Bhomick. Cheers, Mathias.
Open Graph Description: Hi everyone, Thanks for maintaining this great ecosystem! For an experiment we're running these days, I needed a feature to dim LEDs in a grid_maze setup. I'm doing this with PWM using a Mi...
X Description: Hi everyone, Thanks for maintaining this great ecosystem! For an experiment we're running these days, I needed a feature to dim LEDs in a grid_maze setup. I'm doing this with PWM us...
Opengraph URL: https://github.com/pyControl/code/pull/150
X: @github
Domain: patch-diff.githubusercontent.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:ba30d2f5-7490-115b-65fa-47dfecfabc17 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | 95E4:37A505:92373:BC4A4:698EB641 |
| html-safe-nonce | 26e9f46c51a8790cb53ea127ab3ddc74401554cafbda9872ead03b538d706438 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUU0OjM3QTUwNTo5MjM3MzpCQzRBNDo2OThFQjY0MSIsInZpc2l0b3JfaWQiOiI1OTk1Nzc5MjczMzE0NDQ4OTYxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | da9cc983f3bfa392118b3328345e3096b41c4bd9a47f1ddf67c36ed23ad32297 |
| hovercard-subject-tag | pull_request:2524557611 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/pyControl/code/pull/150/checks |
| twitter:image | https://avatars.githubusercontent.com/u/7687623?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/7687623?s=400&v=4 |
| og:image:alt | Hi everyone, Thanks for maintaining this great ecosystem! For an experiment we're running these days, I needed a feature to dim LEDs in a grid_maze setup. I'm doing this with PWM using a Mi... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb2828a801ee6b7be618f3ac76fbf55def35bbc30f053a9c41bf90210b8b72ba |
| turbo-cache-control | no-preview |
| go-import | github.com/pyControl/code git https://github.com/pyControl/code.git |
| octolytics-dimension-user_id | 58780920 |
| octolytics-dimension-user_login | pyControl |
| octolytics-dimension-repository_id | 219033872 |
| octolytics-dimension-repository_nwo | pyControl/code |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 219033872 |
| octolytics-dimension-repository_network_root_nwo | pyControl/code |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e6b91a7e6e46287d26887e3fb7a4161657bab8f7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width