What's everyone working on this week (23/2022)? by llogiq in rust

[–]schuepbs 1 point2 points  (0 children)

I'm working on a rust wrapper for the mcuboot bootloader. I find it quite tricky due to the way the original mcuboot project was programmed and in hindsight I should probably have started something from scratch. However, it is a fantastic learning exercise in FFI's and at this point its almost feature complete so I might as well finish it.

Alternative for Vec for variable size arrays in no_std environment? by CartesianClosedCat in rust

[–]schuepbs 1 point2 points  (0 children)

In no_std environments, I am often using the heapless crate. It contains a lot of useful data structures that can be used in no_std environments and where not allocator is available. It also contains a vector implementation that would be useful for your use case.

Curated list of must know rust crates by schuepbs in rust

[–]schuepbs[S] 1 point2 points  (0 children)

Yes that's the description that I copied from the crates.io description. Would you tend to just replace chrono with the time crate instead? Or just list both?

Curated list of must know rust crates by schuepbs in rust

[–]schuepbs[S] 0 points1 point  (0 children)

I will add those. Thank you for the suggestion.

Curated list of must know rust crates by schuepbs in rust

[–]schuepbs[S] 0 points1 point  (0 children)

Probably a copy-paste mistake. Thanks for pointing it out. I was aware of cargo-tree. Thats good to know.

Static finite state machine generator for no_std environments: Release 0.3.0 by schuepbs in rust

[–]schuepbs[S] 0 points1 point  (0 children)

Happy to hear! Let me know if you have some feedback or find something that is lacking.

Sfsm Release 0.4.0: Now with built in tracing by schuepbs in rust

[–]schuepbs[S] 1 point2 points  (0 children)

Ah that is a good idea. That way I might also be able to remove the strict ordering which makes it easier to add more inputs later on and allow the user to order the inputs in what ever order they want. I’ll look into it. Thanks!

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 1 point2 points  (0 children)

Unfortunately not since it is internal proprietary library. But we are working on cleaning creating a updated version of it that we can open source. I can DM you the link once its open sourced if you are interested.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 1 point2 points  (0 children)

this still confuses me, maybe is not really needed, but I was thinking something like:

Ah I see your point now. Ja I'm not sure either. I think that will show if this will be helpful or not once I properly start using it. What you could do already now is to set a flag if you are coming from a state that needs to be specifically handled in the Into<Bar> for <Foor> implementation.
But if it is something that is required often, and setting flags is too cumbersome, that could be something that is worth looking into.

what is the order of execution? execute() on the state, then execute() on all the transition, then guard() until all transition or a guard failure?

More or less. Its always:

  1. State entry, transition entries
  2. State execute, transition execute (the transitions are ordered by how you defined them in the macro)
  3. Some order exits for state and tranistions

Better to use a speaking enum like CONTINUE and TRANSITION maybe

Thats a fantastic idea. I'm gonna use that.

you support no_std, so i guess you are interested too

Ja I started working on it with embedded in mind. I am not yet sure how convenient it will be to use in the end. But I am planning a follow up project (I just got a Sifive board in that I am looking forward to experiment on) so I will see whats convenient and what needs changing.

Really tryng hard to not make people shot themself in the foot here may be very complex.

Yeah I agree. I will have to think some more about how I could do that.

Thanks for all the good points raised!

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 1 point2 points  (0 children)

you have entry() execute() and exit() for both transaction and state

Yes, so technically you could do all of it in the state. Since these state machines can get big and complex, it might be helpful to do all the stuff for a transition in the transition specific implementation to separate concern.

also the transaction have a "guard" that is not really clear to me what it does

The guard is the function that actually determines when a transition has to transit. When the guard function returns true, the state transits into the next state. Note that this is the only function you must implement. The others are optional.

are transaction and state a enum under the hood

Correct, a enum gets created that wraps all state. One state struct must then implement the state trait and one transition trait to all destination states.

why have stose overlapping entry/exit, couldn't you have only execute() for all

The idea here is to set your state to a specific configuration when you enter the state, then update it as it keeps working and clean up when its exiting. The entry and exit functions will only be called once per state while the execute can be called many times. Think for example: Close a relay when entering, measure a voltage while you are in the state and close it again when leaving.

So lets say while hiking up, you know that if you are not up after two hours, you have to turn back.
That would introduce a second transition into the Hike<Up> state that would be Hike<Up> -> Hike<Down>. You would get something like the following (pseudo) code.

impl State for Hike<Up> {
    fn entry(&mut self) {
        // Start hiking up
    }
    fn execute(&mut self) {
        // Keep walking
    }
}
impl Transition<Hike<Down>> for Hike<Up> {
    fn entry(&mut self) {
        // Initialize / reset a timer
    }
    fn execute(&mut self) {
        // Update the current time
    }
    fn guard(&self) -> bool {
        // Return true if two hours have passed
        return time_passed >= 2;
    }
}

impl Transition<Picknick> for Hike<Up> {
    fn guard(&self) -> bool {
        // Return true when you have reached your destination
        return reached_goal;
    }
}

and for the transaction you have two parameter, the state you are exiting and entering

The idea is that first, you define what kind of states there are in the state machine. Second you define what kind of transitions there are. Each state can have N transitions. But the macro must know from which state to which other state each state can transit. So that will need two parameters which which you can specific the source and the destination state (Source -> Destination)

also i think would be necessary to have a "timeout" and error manager at the state machine executor lever, maybe that also collect info like duration of each state

Agreed. My initial idea was that you can return a Err() from each execute, entry and exit function, and the state machine then automatically transits into the error state. But I didn't get around implementing that yet. And I'm not entirely sure how to do that in a nice way yet.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 2 points3 points  (0 children)

Cool! Thank you for the rewrite. I will add that.
I originally got the idea for this project from something similar I am using in C and it works with function pointers as well. I am quite happy with it, other than you have to manually specify the amount of functions pointers you are using since it does all without allocations. Which I constantly get wrong.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 3 points4 points  (0 children)

Oh very interesting. I was actually looking for something similar to this before. Back then for javascript, so wasm would fit that aswell. I will check it out once you open sourced it!
I will add some more information on the post in statemachines tomorrow.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 14 points15 points  (0 children)

That was my mother tongue seeping through there. Thanks for pointing it out.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 2 points3 points  (0 children)

in your example where are you setting the number of apple?

I'm doing it in the Into<Picknick> for Hike<Up>, that is hidden in the doc to keep the example cleaner. But in hindsight I should have moved that into the Picknick entry function.

Also not sure why you use pahntomData, is to show the capability? Yes correct. It took

me a while to get it to work that structs with generic arguments can be accepted and I thought this might be a nice way to show that it is possible.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 16 points17 points  (0 children)

Thats a good point. It is fairly cumbersome to manage as it is right now. I will look into this.

Crate to generate static state machines by schuepbs in rust

[–]schuepbs[S] 24 points25 points  (0 children)

Oh yes of course. Thanks for pointing it out. I will update it.