|
13 | 13 | #include <stdint.h> |
14 | 14 |
|
15 | 15 | #include <pbdrv/bluetooth.h> |
| 16 | +#include <pbdrv/led.h> |
16 | 17 |
|
17 | 18 | #include <pbio/button.h> |
| 19 | +#include <pbio/busy_count.h> |
| 20 | +#include <pbio/light_matrix.h> |
18 | 21 | #include <pbio/os.h> |
19 | 22 | #include <pbsys/host.h> |
20 | 23 | #include <pbsys/light.h> |
|
23 | 26 | #include <pbsys/storage_settings.h> |
24 | 27 |
|
25 | 28 | #include "hmi.h" |
26 | | -#include "light_matrix.h" |
27 | 29 |
|
28 | 30 | #define DEBUG 0 |
29 | 31 |
|
|
34 | 36 | #define DEBUG_PRINT(...) |
35 | 37 | #endif |
36 | 38 |
|
| 39 | +#if PBIO_CONFIG_LIGHT_MATRIX |
| 40 | + |
| 41 | +pbio_light_matrix_t *pbsys_hub_light_matrix; |
| 42 | + |
| 43 | +/** |
| 44 | + * Displays the idle UI. Has a square stop sign and selected slot on bottom row. |
| 45 | + * |
| 46 | + * @param brightness Brightness (0--100%). |
| 47 | + */ |
| 48 | +static void light_matrix_show_idle_ui(uint8_t brightness) { |
| 49 | + for (uint8_t r = 0; r < PBSYS_CONFIG_HMI_NUM_SLOTS; r++) { |
| 50 | + for (uint8_t c = 0; c < PBSYS_CONFIG_HMI_NUM_SLOTS; c++) { |
| 51 | + bool is_on = r < 3 && c > 0 && c < 4; |
| 52 | + is_on |= (r == 4 && c == pbsys_status_get_selected_slot()); |
| 53 | + pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, r, c, is_on ? brightness : 0, true); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Bootup and shutdown animation. This is a process rather than an animation |
| 60 | + * process so we can await completion, and later add sound. |
| 61 | + */ |
| 62 | +static pbio_error_t boot_animation_process_boot_thread(pbio_os_state_t *state, void *context) { |
| 63 | + |
| 64 | + static pbio_os_timer_t timer; |
| 65 | + static uint8_t step; |
| 66 | + |
| 67 | + const int num_steps = 10; |
| 68 | + |
| 69 | + // Makes the brightness increment or decrement. |
| 70 | + bool booting = context; |
| 71 | + |
| 72 | + PBIO_OS_ASYNC_BEGIN(state); |
| 73 | + |
| 74 | + for (step = 1; step <= num_steps; step++) { |
| 75 | + uint8_t brightness = booting ? step * (100 / num_steps) : 100 - (100 / num_steps) * step; |
| 76 | + light_matrix_show_idle_ui(brightness); |
| 77 | + PBIO_OS_AWAIT_MS(state, &timer, 200 / num_steps); |
| 78 | + } |
| 79 | + pbio_busy_count_down(); |
| 80 | + |
| 81 | + PBIO_OS_ASYNC_END(PBIO_SUCCESS); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Animation frame for program running animation. |
| 86 | + */ |
| 87 | +static uint32_t pbio_light_matrix_5x5_spinner_animation_next(pbio_light_animation_t *animation) { |
| 88 | + |
| 89 | + // The indexes of pixels to light up |
| 90 | + static const uint8_t indexes[] = { 1, 2, 3, 8, 13, 12, 11, 6 }; |
| 91 | + |
| 92 | + // Each pixel has a repeating brightness pattern of the form /\_ through |
| 93 | + // which we can cycle in 256 steps. |
| 94 | + static uint8_t cycle = 0; |
| 95 | + |
| 96 | + for (size_t i = 0; i < PBIO_ARRAY_SIZE(indexes); i++) { |
| 97 | + // The pixels are spread equally across the pattern. |
| 98 | + uint8_t offset = cycle + i * (UINT8_MAX / PBIO_ARRAY_SIZE(indexes)); |
| 99 | + uint8_t brightness = offset > 200 ? 0 : (offset < 100 ? offset : 200 - offset); |
| 100 | + |
| 101 | + // Set the brightness for this pixel |
| 102 | + pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, indexes[i] / 5, indexes[i] % 5, brightness, false); |
| 103 | + } |
| 104 | + // This increment controls the speed of the pattern |
| 105 | + cycle += 9; |
| 106 | + |
| 107 | + return 40; |
| 108 | +} |
| 109 | + |
| 110 | +static void light_matrix_start_run_animation(void) { |
| 111 | + // Central pixel in spinner is off and will not be updated. |
| 112 | + pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, 1, 2, 0, true); |
| 113 | + pbio_light_animation_init(&pbsys_hub_light_matrix->animation, pbio_light_matrix_5x5_spinner_animation_next); |
| 114 | + pbio_light_animation_start(&pbsys_hub_light_matrix->animation); |
| 115 | +} |
| 116 | + |
| 117 | +static pbio_os_process_t boot_animation_process; |
| 118 | + |
| 119 | +#endif |
| 120 | + |
| 121 | +void pbsys_hmi_init(void) { |
| 122 | + #if PBIO_CONFIG_LIGHT_MATRIX |
| 123 | + pbio_busy_count_up(); |
| 124 | + pbio_error_t err = pbio_light_matrix_get_dev(0, 5, &pbsys_hub_light_matrix); |
| 125 | + if (err != PBIO_SUCCESS) { |
| 126 | + // Effectively stopping boot if we can't get hardware. |
| 127 | + return; |
| 128 | + } |
| 129 | + pbio_os_process_start(&boot_animation_process, boot_animation_process_boot_thread, (void *)true); |
| 130 | + #endif |
| 131 | +} |
| 132 | + |
| 133 | +void pbsys_hmi_deinit(void) { |
| 134 | + pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
| 135 | + pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED); |
| 136 | + |
| 137 | + #if PBIO_CONFIG_LIGHT_MATRIX |
| 138 | + pbio_busy_count_up(); |
| 139 | + pbio_os_process_start(&boot_animation_process, boot_animation_process_boot_thread, (void *)false); |
| 140 | + #endif |
| 141 | +} |
| 142 | + |
37 | 143 | /** |
38 | 144 | * The HMI is a loop running the following steps: |
39 | 145 | * |
@@ -67,7 +173,9 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) { |
67 | 173 | DEBUG_PRINT("Start HMI loop\n"); |
68 | 174 |
|
69 | 175 | // Visually indicate current state on supported hubs. |
70 | | - pbsys_hub_light_matrix_update_program_slot(); |
| 176 | + #if PBIO_CONFIG_LIGHT_MATRIX |
| 177 | + light_matrix_show_idle_ui(100); |
| 178 | + #endif |
71 | 179 |
|
72 | 180 | // Initialize Bluetooth depending on current state. |
73 | 181 | if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) { |
@@ -215,7 +323,10 @@ static pbio_error_t run_ui(pbio_os_state_t *state, pbio_os_timer_t *timer) { |
215 | 323 | pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_ADVERTISING); |
216 | 324 |
|
217 | 325 | // Start run animations |
218 | | - pbsys_hub_light_matrix_handle_user_program_start(); |
| 326 | + #if PBIO_CONFIG_LIGHT_MATRIX |
| 327 | + light_matrix_start_run_animation(); |
| 328 | + #endif |
| 329 | + |
219 | 330 | #if PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS |
220 | 331 | pbio_color_light_start_breathe_animation(pbsys_status_light_main, PBSYS_CONFIG_STATUS_LIGHT_STATE_ANIMATIONS_HUE); |
221 | 332 | #else |
|
0 commit comments