|
| 1 | +// Copyright 2025 Shift Crypto AG |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "unlock_animation.h" |
| 16 | + |
| 17 | +#include <hardfault.h> |
| 18 | +#include <screen.h> |
| 19 | +#include <ui/component.h> |
| 20 | +#include <ui/ui_util.h> |
| 21 | + |
| 22 | +#include <stdint.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +// This many iterations times the slowdown factor to render the whole animation. |
| 26 | +#define LOCK_ANIMATION_N_FRAMES (38) |
| 27 | + |
| 28 | +#define SLOWDOWN_FACTOR (10) |
| 29 | + |
| 30 | +#define LOCK_ANIMATION_FRAME_WIDTH (28) |
| 31 | +#define LOCK_ANIMATION_FRAME_HEIGHT (25) |
| 32 | +#define LOCK_ANIMATION_FRAME_SIZE \ |
| 33 | + ((LOCK_ANIMATION_FRAME_WIDTH * LOCK_ANIMATION_FRAME_HEIGHT + 7) / 8) |
| 34 | + |
| 35 | +typedef struct { |
| 36 | + int frame; |
| 37 | + void (*on_done)(void*); |
| 38 | + void* on_done_param; |
| 39 | +} data_t; |
| 40 | + |
| 41 | +#define LOCK_ANIMATION_ACTUAL_N_FRAMES (10) |
| 42 | +/** |
| 43 | + * Use less frames than ticks for the animation. |
| 44 | + * |
| 45 | + * Split the remaining animation time into long frame times |
| 46 | + * for the closed lock, open lock, and turned lock. |
| 47 | + */ |
| 48 | +#define LOCK_ANIMATION_FRAMES_STOP_TIME \ |
| 49 | + ((LOCK_ANIMATION_N_FRAMES - LOCK_ANIMATION_ACTUAL_N_FRAMES) / 3) |
| 50 | + |
| 51 | +/** |
| 52 | + * At frame 4, the animation will pause for a second. |
| 53 | + */ |
| 54 | +#define LOCK_ANIMATION_PAUSE_FRAME (2) |
| 55 | + |
| 56 | +static const uint8_t LOCK_ANIMATION[LOCK_ANIMATION_ACTUAL_N_FRAMES][LOCK_ANIMATION_FRAME_SIZE] = { |
| 57 | + // Frame 0 - Closed lock |
| 58 | + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 59 | + 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0xff, 0x80, 0x00, 0x1c, 0x1c, 0x00, 0x03, |
| 60 | + 0x80, 0xe0, 0x00, 0x30, 0x06, 0x00, 0x06, 0x00, 0x30, 0x00, 0x60, 0x03, 0x00, 0x06, 0x00, |
| 61 | + 0x30, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 62 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 63 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 64 | + // Frame 1 |
| 65 | + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, |
| 66 | + 0x00, 0xff, 0x80, 0x00, 0x1c, 0x1c, 0x00, 0x03, 0x80, 0xe0, 0x00, 0x30, 0x06, 0x00, 0x06, |
| 67 | + 0x00, 0x30, 0x00, 0x60, 0x03, 0x00, 0x06, 0x00, 0x30, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 68 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 69 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 70 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 71 | + // Frame 2 - Open lock |
| 72 | + {0x00, 0x00, 0x3e, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x38, 0x0e, 0x00, |
| 73 | + 0x03, 0x00, 0x60, 0x00, 0x60, 0x03, 0x00, 0x06, 0x00, 0x30, 0x00, 0x60, 0x03, 0x00, 0x06, |
| 74 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 75 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 76 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 77 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 78 | + // Frame 3 |
| 79 | + {0x00, 0x00, 0x38, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x01, 0xc7, 0x00, 0x00, 0x38, 0x38, 0x00, |
| 80 | + 0x03, 0x01, 0x80, 0x00, 0x60, 0x0c, 0x00, 0x06, 0x00, 0xc0, 0x00, 0x60, 0x0c, 0x00, 0x06, |
| 81 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 82 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 83 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 84 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 85 | + // Frame 4 |
| 86 | + {0x00, 0x00, 0xe0, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x01, 0xb0, 0x00, 0x00, 0x31, 0x80, 0x00, |
| 87 | + 0x03, 0x18, 0x00, 0x00, 0x61, 0x80, 0x00, 0x06, 0x18, 0x00, 0x00, 0x61, 0x80, 0x00, 0x06, |
| 88 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 89 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 90 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 91 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 92 | + // Frame 5 |
| 93 | + {0x00, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x38, 0x00, 0x00, |
| 94 | + 0x03, 0x80, 0x00, 0x00, 0x68, 0x00, 0x00, 0x06, 0x80, 0x00, 0x00, 0x68, 0x00, 0x00, 0x06, |
| 95 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 96 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 97 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 98 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 99 | + // Frame 6 |
| 100 | + {0x00, 0x08, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, |
| 101 | + 0x1c, 0x00, 0x00, 0x01, 0x60, 0x00, 0x00, 0x16, 0x00, 0x00, 0x01, 0x60, 0x00, 0x00, 0x06, |
| 102 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 103 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 104 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 105 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 106 | + // Frame 7 |
| 107 | + {0x00, 0x70, 0x00, 0x00, 0x0d, 0x80, 0x00, 0x00, 0xd8, 0x00, 0x00, 0x18, 0xc0, 0x00, 0x01, |
| 108 | + 0x8c, 0x00, 0x00, 0x18, 0x60, 0x00, 0x01, 0x86, 0x00, 0x00, 0x18, 0x60, 0x00, 0x00, 0x06, |
| 109 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 110 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 111 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 112 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 113 | + // Frame 8 |
| 114 | + {0x01, 0xc0, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x0e, 0x38, 0x00, 0x01, 0xc1, 0xc0, 0x00, 0x18, |
| 115 | + 0x0c, 0x00, 0x03, 0x00, 0x60, 0x00, 0x30, 0x06, 0x00, 0x03, 0x00, 0x60, 0x00, 0x00, 0x06, |
| 116 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 117 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 118 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 119 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}, |
| 120 | + // Frame 9 - Turned lock |
| 121 | + {0x07, 0xc0, 0x00, 0x01, 0xff, 0x00, 0x00, 0x38, 0x38, 0x00, 0x07, 0x01, 0xc0, 0x00, 0x60, |
| 122 | + 0x0c, 0x00, 0x0c, 0x00, 0x60, 0x00, 0xc0, 0x06, 0x00, 0x0c, 0x00, 0x60, 0x00, 0x00, 0x06, |
| 123 | + 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, |
| 124 | + 0x00, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, |
| 125 | + 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, |
| 126 | + 0x7f, 0xff, 0x00, 0x07, 0xff, 0xf0, 0x00, 0x7f, 0xff, 0x00, 0x03, 0xff, 0xe0}}; |
| 127 | + |
| 128 | +/** |
| 129 | + * Gets a frame of the lock animation. |
| 130 | + */ |
| 131 | +static const uint8_t* _get_frame(int frame_idx) |
| 132 | +{ |
| 133 | + if (frame_idx >= LOCK_ANIMATION_N_FRAMES) { |
| 134 | + Abort("Invalid lock animation frame requested."); |
| 135 | + } |
| 136 | + /* First part of the animation: Closed lock for LOCK_ANIMATION_FRAMES_STOP_TIME frames. */ |
| 137 | + if (frame_idx < LOCK_ANIMATION_FRAMES_STOP_TIME) { |
| 138 | + return LOCK_ANIMATION[0]; |
| 139 | + } |
| 140 | + /* Second part: show the lock opening */ |
| 141 | + int actual_frame_idx = frame_idx - LOCK_ANIMATION_FRAMES_STOP_TIME; |
| 142 | + if (actual_frame_idx < LOCK_ANIMATION_PAUSE_FRAME) { |
| 143 | + return LOCK_ANIMATION[actual_frame_idx]; |
| 144 | + } |
| 145 | + /* Third part: keep the lock open for a while. */ |
| 146 | + if (actual_frame_idx < LOCK_ANIMATION_FRAMES_STOP_TIME + LOCK_ANIMATION_PAUSE_FRAME) { |
| 147 | + return LOCK_ANIMATION[LOCK_ANIMATION_PAUSE_FRAME]; |
| 148 | + } |
| 149 | + /* Fourth/fifth part: Spin the lock, then keep it open for a while. */ |
| 150 | + actual_frame_idx -= LOCK_ANIMATION_FRAMES_STOP_TIME; |
| 151 | + if (actual_frame_idx >= LOCK_ANIMATION_ACTUAL_N_FRAMES) { |
| 152 | + return LOCK_ANIMATION[LOCK_ANIMATION_ACTUAL_N_FRAMES - 1]; |
| 153 | + } |
| 154 | + return LOCK_ANIMATION[actual_frame_idx]; |
| 155 | +} |
| 156 | + |
| 157 | +static void _render(component_t* component) |
| 158 | +{ |
| 159 | + data_t* data = (data_t*)component->data; |
| 160 | + int frame = data->frame / SLOWDOWN_FACTOR; |
| 161 | + |
| 162 | + if (frame >= LOCK_ANIMATION_N_FRAMES) { |
| 163 | + /* End of the animation */ |
| 164 | + if (data->on_done) { |
| 165 | + data->on_done(data->on_done_param); |
| 166 | + data->on_done = NULL; |
| 167 | + } |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + /* Draw the frame. */ |
| 172 | + position_t pos = { |
| 173 | + .left = (SCREEN_WIDTH - LOCK_ANIMATION_FRAME_WIDTH) / 2, |
| 174 | + .top = (SCREEN_HEIGHT - LOCK_ANIMATION_FRAME_HEIGHT) / 2}; |
| 175 | + dimension_t dim = {.width = LOCK_ANIMATION_FRAME_WIDTH, .height = LOCK_ANIMATION_FRAME_HEIGHT}; |
| 176 | + in_buffer_t image = {.data = _get_frame(frame), .len = LOCK_ANIMATION_FRAME_SIZE}; |
| 177 | + graphics_draw_image(&pos, &dim, &image); |
| 178 | + data->frame++; |
| 179 | +} |
| 180 | + |
| 181 | +static const component_functions_t _component_functions = { |
| 182 | + .cleanup = ui_util_component_cleanup, |
| 183 | + .render = _render, |
| 184 | + .on_event = ui_util_on_event_noop, |
| 185 | +}; |
| 186 | + |
| 187 | +component_t* unlock_animation_create(void (*on_done)(void*), void* on_done_param) |
| 188 | +{ |
| 189 | + data_t* data = malloc(sizeof(data_t)); |
| 190 | + if (!data) { |
| 191 | + Abort("Error: malloc unlock_animation data"); |
| 192 | + } |
| 193 | + memset(data, 0, sizeof(data_t)); |
| 194 | + data->on_done = on_done; |
| 195 | + data->on_done_param = on_done_param; |
| 196 | + |
| 197 | + component_t* component = malloc(sizeof(component_t)); |
| 198 | + if (!component) { |
| 199 | + Abort("Error: malloc unlock_animation_data"); |
| 200 | + } |
| 201 | + memset(component, 0, sizeof(component_t)); |
| 202 | + |
| 203 | + component->f = &_component_functions; |
| 204 | + component->data = data; |
| 205 | + component->dimension.width = SCREEN_WIDTH; |
| 206 | + component->dimension.height = SCREEN_HEIGHT; |
| 207 | + return component; |
| 208 | +} |
0 commit comments