-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightning_distance.c
More file actions
160 lines (136 loc) · 5.77 KB
/
lightning_distance.c
File metadata and controls
160 lines (136 loc) · 5.77 KB
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
#include <furi.h>
#include <gui/gui.h>
#include <gui/elements.h>
#include <furi_hal.h>
#include <input/input.h>
#include <stdlib.h>
#include <stdio.h>
// App state machine
typedef enum {
LightningAppState_WaitFlash,
LightningAppState_Counting,
LightningAppState_ShowResult
} LightningAppState;
// App data structure
typedef struct {
LightningAppState state;
uint32_t start_time;
uint32_t end_time;
float distance_km;
float temperature_C; // User adjustable temperature in Celsius
bool running;
} LightningApp;
// Input handler
static void lightning_input_callback(InputEvent* event, void* context) {
LightningApp* app = context;
if(event->type == InputTypePress || event->type == InputTypeRepeat) {
switch(app->state) {
case LightningAppState_WaitFlash:
if(event->key == InputKeyLeft) {
app->temperature_C -= 0.5f;
if(app->temperature_C < -50.0f) app->temperature_C = -50.0f;
} else if(event->key == InputKeyRight) {
app->temperature_C += 0.5f;
if(app->temperature_C > 60.0f) app->temperature_C = 60.0f;
} else if(event->key == InputKeyOk && event->type == InputTypePress) {
app->start_time = furi_get_tick();
app->state = LightningAppState_Counting;
} else if(event->key == InputKeyBack && event->type == InputTypePress) {
app->running = false;
}
break;
case LightningAppState_Counting:
if(event->key == InputKeyOk && event->type == InputTypePress) {
app->end_time = furi_get_tick();
float time_diff = (app->end_time - app->start_time) / 1000.0f;
float speed_of_sound = 331.3f + 0.606f * app->temperature_C;
app->distance_km = (time_diff * speed_of_sound) / 1000.0f;
app->state = LightningAppState_ShowResult;
} else if(event->key == InputKeyBack && event->type == InputTypePress) {
app->state = LightningAppState_WaitFlash;
}
break;
case LightningAppState_ShowResult:
if(event->key == InputKeyOk && event->type == InputTypePress) {
app->state = LightningAppState_WaitFlash;
} else if(event->key == InputKeyBack && event->type == InputTypePress) {
app->running = false;
}
break;
}
}
}
// Drawing UI
static void lightning_draw(Canvas* canvas, LightningApp* app) {
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
switch(app->state) {
case LightningAppState_WaitFlash: {
// Draw each line manually to avoid \n
char buf[32];
snprintf(buf, sizeof(buf), "Set Temp: %.1f C", (double)app->temperature_C);
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignCenter, buf);
canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignCenter, "< / > to adjust");
canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "OK on Lightning");
break;
}
// Hi, Muffin here. I'm worse at math than I am at programming; So if you want to validate these calculations, feel fucking free!
case LightningAppState_Counting: {
// Get current tick
uint32_t now = furi_get_tick();
// Time difference in seconds
float time_diff = (now - app->start_time) / 1000.0f;
// Speeds
const float speed_sound = 331.3f + 0.606f * app->temperature_C; // in m/s
const float speed_light = 299792458.0f; // in m/s
// Calculate corrected distance (in meters) using:
// d = t / (1/s - 1/c)
float inverse_sound = 1.0f / speed_sound;
float inverse_light = 1.0f / speed_light;
float distance_m = time_diff / (inverse_sound - inverse_light);
// Convert to kilometers
float distance_km = distance_m / 1000.0f;
// Draw the "Counting..." label
char buf[32];
snprintf(buf, sizeof(buf), "Counting...");
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignCenter, buf);
// Display the live-updated distance in km
snprintf(buf, sizeof(buf), "%.2f km", (double)distance_km);
canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, buf);
// Show instructions to press OK when thunder is heard
canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "OK on Thunder");
break;
}
case LightningAppState_ShowResult: {
char buf[32];
snprintf(buf, sizeof(buf), "Final Distance:");
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignCenter, buf);
snprintf(buf, sizeof(buf), "%.2f km", (double)app->distance_km);
canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, buf);
canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "OK to restart");
break;
}
}
}
// Main app entry point
int32_t lightning_distance_app(void* p) {
UNUSED(p);
LightningApp app = {
.state = LightningAppState_WaitFlash,
.running = true,
.temperature_C = 20.0f, // Default temperature
};
Gui* gui = furi_record_open("gui");
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, (ViewPortDrawCallback)lightning_draw, &app);
view_port_input_callback_set(view_port, lightning_input_callback, &app);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
while(app.running) {
view_port_update(view_port);
furi_delay_ms(50);
}
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_record_close("gui");
return 0;
}