-
Notifications
You must be signed in to change notification settings - Fork 115
orientation-screen: Migrate to rust #1631
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
Open
NickeZ
wants to merge
3
commits into
BitBoxSwiss:master
Choose a base branch
from
NickeZ:maint/migrate-orientation-screen-to-rust
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Copyright 2025 Shift Crypto AG | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <delay.h> | ||
| #include <hal_timer.h> | ||
| #include <platform/driver_init.h> | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <utils_assert.h> | ||
|
|
||
| struct task { | ||
| struct timer_task timer; | ||
| volatile bool done; | ||
| }; | ||
|
|
||
| static struct task _tasks[10] = {0}; | ||
| static struct task _empty = {0}; | ||
|
|
||
| static void _hal_timer_cb(const struct timer_task* const timer) | ||
| { | ||
| for (int i = 0; i < (int)(sizeof(_tasks) / sizeof(struct task)); i++) { | ||
| if (&_tasks[i].timer == timer) { | ||
| _tasks[i].done = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool delay_init_ms(delay_t* self, uint32_t ms) | ||
| { | ||
| // find an unused slot in tasks | ||
| int i; | ||
| CRITICAL_SECTION_ENTER(); | ||
| for (i = 0; i < (int)(sizeof(_tasks) / sizeof(struct task)); i++) { | ||
| if (memcmp(&_tasks[i], &_empty, sizeof(struct task)) == 0) { | ||
| break; | ||
| } | ||
| } | ||
| CRITICAL_SECTION_LEAVE(); | ||
| if (i == sizeof(_tasks)) { | ||
| return false; | ||
| } | ||
| _tasks[i].done = false; | ||
| memset(&_tasks[i], 0, sizeof(struct task)); | ||
| _tasks[i].timer.interval = ms; | ||
| _tasks[i].timer.cb = _hal_timer_cb; | ||
| _tasks[i].timer.mode = TIMER_TASK_ONE_SHOT; | ||
| self->id = i; | ||
| return true; | ||
| } | ||
|
|
||
| void delay_start(const delay_t* self) | ||
| { | ||
| ASSERT(self->id < (sizeof(_tasks) / sizeof(struct task))); | ||
| ASSERT(!_tasks[self->id].done); | ||
| timer_add_task(&TIMER_0, &_tasks[self->id].timer); | ||
| } | ||
|
|
||
| bool delay_poll(const delay_t* self) | ||
| { | ||
| ASSERT(self->id < (sizeof(_tasks) / sizeof(struct task))); | ||
| if (_tasks[self->id].done) { | ||
| memset(&_tasks[self->id], 0, sizeof(struct task)); | ||
| return true; | ||
| } | ||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2025 Shift Crypto AG | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use util::bb02_async::option; | ||
|
|
||
| pub async fn create<CB>(orientation_selected_cb: CB) | ||
| where | ||
| CB: FnOnce(), | ||
| { | ||
| let result = core::cell::RefCell::new(None as Option<()>); | ||
| let mut orientation_arrows = bitbox02::ui::orientation_arrows(|upside_down| { | ||
| if upside_down { | ||
| bitbox02::screen_rotate() | ||
| } | ||
| *result.borrow_mut() = Some(()); | ||
| }); | ||
| orientation_arrows.screen_stack_push(); | ||
|
|
||
| // Wait until orientation has been chosen | ||
| option(&result).await; | ||
| drop(orientation_arrows); | ||
|
|
||
| // During this delay the bb02 logotype is shown | ||
| if let Ok(delay) = bitbox02::delay::Delay::from_ms(1300) { | ||
| delay.await; | ||
| } | ||
|
|
||
| // Switch to lockscreen that shows "See the bitbox app" and device name | ||
| bitbox02::ui::screen_process_waiting_switch_to_lockscreen(); | ||
|
|
||
| orientation_selected_cb(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did a double take seeing a callback called at the end of an async function. Would it be possible to simply return whatever value you need and use it when polling is done (in
if let Poll::Ready(result) = result { ...)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.
I don't think so because this future isn't run in a traditional async runtime, it is run with our "glue layer". I could return the callback and then call it in the
rust_workflow_spin()function instead. But not sure if that is great.I was hoping it would be helpful if the orientation workflow was in rust for the simulator. But in the end it didn't play out so well, I still have to call the glue-layer because I don't have a proper async runtime in the simulator either.
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.
(the rust version of the orientation selection is still more readable IMO than the c version with interrupts, so I still think it is a net win.)