-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make crate compatible with no_std #86
Conversation
Thanks again for all this, incredible work. I'll take some time after classes today to review the changes. I'd like to get this in for 0.12. That said, there are a handful of breaking changes planned for 0.20 planned (see #64 and #85), with some work already done on the decouple_colors branch that I may need some help porting over to no_std. I may reach out again when that's moved further along. |
I've decided to make a separate spatial_led_examples repository. It doesn't make a whole lot of sense to have ratatui as a dev dependency for this crate, and it'd be nice to have some examples for more specific setups like raspberry pi or no_std. |
It seems that enabling the std flag on glam doesn't automatically override libm. This PR compared to master, compiled with std enabled. Explicitly disabling libm and enabling std via glam = { version = "0.29", default-features = false, features = ["std"] } I'm a little stumped on how to make glam only compile with the libm feature if std is disabled. I could introduce a no_std flag and require that the user has either std or no_std enabled, but that feels kinda gross. On a side note, we could probably expose core-simd for those willing to compile with nightly for some extra performance on some no_std systems. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably the way to go, though I'm open to other suggestions. If you're opting out of std, you should also opt in to libm if you'll need it.
Some sleepers, such as Delay from embedded-hal, might need mutable access to themselves to sleep.
I pushed some of the suggested changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks again!
This pull request makes the crate usable in
no_std
environments (alloc
is still required).Trivial changes:
I replaced all
std::collections::HashSet
andstd::collections::HashMap
withalloc::collections::BTreeSet
andalloc::collections::BTreeMap
.They should be nearly equivalent, unless there was an actual reason for using
HashMap
instead ofBTreeMap
that I missed.I put the constructor for
Config
that takes a path and reads it from a file behind a featurestd
.On a
no_std
environment the client should load the configuration on their own, and pass it as a string.I replaced all
std::time::Duration
withcore::time::Duration
.Non-trivial changes:
std::time::Instant
is not available incore
, because without the operating system there is no general way to get the current time.I defined a trait in
time::Instant
, so clients can choose an appropriate representation for their platform.Then
Driver
takes a generic argumentINSTANT: Instant
.A predefined alias
StdDriver
is available forstd
platforms, usingstd::time::Instant
, gated behind a feature.std::thread::sleep()
is also not available incore
, andspin_sleep
requiresstd.
I defined a trait in
time::Sleeper
, clients can choose an appropriate implementation for their platform.Then
Scheduler
takes two generic argumentsINSTANT: Instant
andSLEEPER: Sleeper
.A predefined alias
StdScheduler
is available forstd
platforms, usingstd::time::Instant
andstd::thread:sleep
, and another oneSpinScheduler
, usingstd::time::Instant
andspin_sleep::sleep
, both gated behind features.I added an
AsyncSleeper
that has anasync
interface (embassy is getting popular in embedded Rust).Scheduler
is the only place that handles sleeping, and it is not so big, so I simply duplicated the logic intoAsyncScheduler
.There might be a more clever approach using
maybe-async
.In some
no_std
environments, floating-point operations are not available.I added
num-traits
as a dependency, which reimplements those operations withlibm
.This is gated behind a feature
std
.Finally, I set the right features of the dependencies so that they would also be
no_std
.Mostly, I disabled default features and enabled
features = ["libm"]
.The result is a crate that can be used in
no_std
environments, though it still needs an allocator for dynamic memory.I guess it would require a non-trivial redesign to make it work without dynamic allocation.
Anyway, as long as a microcontroller has enough memory, it should not be an issue to enable
alloc
.Moreover, I also implemented a simple firmware using embassy on a ESP32-C3.
I implemented
Instant
on a wrapper aroundembassy_time::Instant
, andSleeper
on a wrapper aroundembassy_time::Timer::after()
.But I am not sure where to put this example.
It needs a custom cargo configuration, so it cannot be easily placed in the directory
example
.Note: I do not actually have a LED strip to test it :D
For now this firmware just prints the LEDs values to the serial interface.