-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad_apple.c
183 lines (151 loc) · 5.63 KB
/
bad_apple.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <furi.h>
#include <furi_hal.h>
#include <stm32wbxx_ll_tim.h>
#include <gui/gui.h>
#include <input/input.h>
#include <storage/storage.h>
#include <notification/notification.h>
#include <notification/notification_messages.h>
#include "bad_apple.h"
#include "video_player.h"
#define TAG "badapple"
typedef enum {
BadAppleEventTypeInput,
BadAppleEventTypeTick,
} BadAppleEventType;
typedef struct {
BadAppleEventType type;
InputEvent* input;
} BadAppleEvent;
// Screen is 128x64 px
static void app_draw_callback(Canvas* canvas, void* ctx) {
BadAppleCtx* inst = ctx;
canvas_clear(canvas);
canvas_draw_xbm(canvas, VIDEO_X, VIDEO_Y, VIDEO_WIDTH, SCREEN_HEIGHT, inst->framebuffer);
canvas_draw_box(canvas, 0, 0, VIDEO_X, SCREEN_HEIGHT);
canvas_draw_box(
canvas, VIDEO_X + VIDEO_WIDTH, 0, SCREEN_WIDTH - VIDEO_WIDTH - VIDEO_X, SCREEN_HEIGHT);
}
static void app_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
BadAppleEvent event = {.type = BadAppleEventTypeInput, .input = input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
BadAppleCtx* bad_apple_ctx_alloc(void) {
BadAppleCtx* inst = malloc(sizeof(BadAppleCtx));
memset(inst, 0, sizeof(BadAppleCtx));
if(inst) {
inst->storage = furi_record_open(RECORD_STORAGE);
inst->video_file = storage_file_alloc(inst->storage);
inst->file_buffer_offset = sizeof(inst->file_buffer);
}
return inst;
}
void bad_apple_ctx_free(BadAppleCtx* inst) {
if(inst) {
storage_file_free(inst->video_file);
furi_record_close(RECORD_STORAGE);
free(inst);
}
}
void bad_apple_load_next_video_chunk(BadAppleCtx* inst) {
size_t bytes_to_read = sizeof(inst->file_buffer);
uint8_t* buf_ptr = inst->file_buffer;
while(bytes_to_read > 0) {
uint16_t curr_bytes_to_read = bytes_to_read > (UINT16_MAX / 2 + 1) ? (UINT16_MAX / 2 + 1) :
bytes_to_read;
uint16_t read = storage_file_read(inst->video_file, buf_ptr, curr_bytes_to_read);
bytes_to_read -= read;
buf_ptr += read;
if(read == 0) break;
}
inst->file_buffer_offset = 0;
}
uint8_t bad_apple_read_byte(BadAppleCtx* inst) {
if(inst->file_buffer_offset >= sizeof(inst->file_buffer)) {
bad_apple_load_next_video_chunk(inst);
}
return inst->file_buffer[inst->file_buffer_offset++];
}
void bad_apple_timer_isr(void* ctx) {
FuriMessageQueue* event_queue = ctx;
BadAppleEvent event = {.type = BadAppleEventTypeTick};
furi_message_queue_put(event_queue, &event, 0);
LL_TIM_ClearFlag_UPDATE(TIM2);
}
void bad_apple_timer_setup(BadAppleCtx* inst, void* ctx) {
UNUSED(inst);
LL_TIM_InitTypeDef tim_init = {
.Prescaler = 63999,
.CounterMode = LL_TIM_COUNTERMODE_UP,
.Autoreload = 30,
};
LL_TIM_Init(TIM2, &tim_init);
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
LL_TIM_DisableCounter(TIM2);
LL_TIM_SetCounter(TIM2, 0);
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, bad_apple_timer_isr, ctx);
LL_TIM_EnableIT_UPDATE(TIM2);
}
void bad_apple_timer_deinit(void) {
LL_TIM_DisableCounter(TIM2);
LL_TIM_DisableIT_UPDATE(TIM2);
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
LL_TIM_DeInit(TIM2);
}
int32_t bad_apple_main(void* p) {
UNUSED(p);
BadAppleCtx* ctx = bad_apple_ctx_alloc();
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(BadAppleEvent));
// Configure view port
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, app_draw_callback, ctx);
view_port_input_callback_set(view_port, app_input_callback, event_queue);
// Register view port in GUI
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
// Frame rate: 32 FPS
bad_apple_timer_setup(ctx, event_queue);
bool is_opened = storage_file_open(ctx->video_file, VIDEO_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(is_opened) {
BadAppleEvent event;
notification_message(notification, &sequence_display_backlight_enforce_on);
bool running = true;
LL_TIM_EnableCounter(TIM2);
while(running) {
if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
if(event.type == BadAppleEventTypeInput) {
InputEvent* input_event = event.input;
if(input_event->type == InputTypeLong) {
if(input_event->key == InputKeyBack) {
running = false;
}
}
} else if(event.type == BadAppleEventTypeTick) {
// FURI_LOG_D(TAG, "Update frame");
if(!vp_play_frame(ctx)) {
running = false;
}
}
}
view_port_update(view_port);
}
LL_TIM_DisableCounter(TIM2);
notification_message(notification, &sequence_display_backlight_enforce_auto);
storage_file_close(ctx->video_file);
}
furi_record_close(RECORD_NOTIFICATION);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_record_close(RECORD_GUI);
bad_apple_timer_deinit();
bad_apple_ctx_free(ctx);
return 0;
}