Skip to content

Commit 01afdb3

Browse files
committed
Switch to Frequency + Rework Settings for Variable Item List Use
[Problem] Currently using period and frequency to generate the tone to play. This isn't easy to understand, and isn't nicely compatible with understanding what tone will play. [Solution] Updated to use frequency for the tone to play, plus updated the Settings Scene to instead use a variable item list for changing the settings. This makes the settings manageable now. [Testing] Tested on device and confirmed working.
1 parent 296928b commit 01afdb3

File tree

4 files changed

+67
-83
lines changed

4 files changed

+67
-83
lines changed

src/scenes/playback_scene.c

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
#include "../app_context.h"
33
#include "../tone_gen.h"
44

5-
#define SINE_WAVE(x, toneModelData) \
6-
(toneDataModel->amplitude * \
7-
sin((x + toneDataModel->animationOffset) * 50 * toneDataModel->period) * 20 + \
8-
(64 / 2))
5+
#define SINE_WAVE(x, toneModelData) \
6+
(sin((x + toneDataModel->animationOffset) * 50) * 20 + (64 / 2))
97

10-
#define SQUARE_WAVE(x, toneModelData) \
11-
(toneDataModel->amplitude * \
12-
(sin((x + toneDataModel->animationOffset) * 50 * toneDataModel->period) > 0 ? 1 : -1) * \
13-
20 + \
14-
(64 / 2))
8+
#define SQUARE_WAVE(x, toneModelData) \
9+
((sin((x + toneDataModel->animationOffset) * 50) > 0 ? 1 : -1) * 20 + (64 / 2))
1510

1611
// Renders the waveform
1712
static void playback_view_draw_callback(Canvas* canvas, void* model) {
@@ -59,9 +54,8 @@ void scene_on_enter_playback_scene(void* context) {
5954

6055
FURI_LOG_I(TAG, "setting view model");
6156
struct ToneData_t* toneDataModel = (struct ToneData_t*)view_get_model(playbackView->viewData);
62-
toneDataModel->amplitude = ((struct ToneData_t*)app->additionalData)->amplitude;
63-
toneDataModel->period = ((struct ToneData_t*)app->additionalData)->period;
6457
toneDataModel->waveType = ((struct ToneData_t*)app->additionalData)->waveType;
58+
toneDataModel->frequency = ((struct ToneData_t*)app->additionalData)->frequency;
6559

6660
// Set the currently active view
6761
FURI_LOG_I(TAG, "setting active view");

src/scenes/settings_scene.c

+59-67
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
#include <gui/modules/submenu.h>
2-
#include <gui/modules/popup.h>
1+
#include <gui/modules/variable_item_list.h>
32

43
#include "settings_scene.h"
54
#include "../app_context.h"
65
#include "../tone_gen.h"
76
#include "../utils/linked_list.h"
87

9-
/** indices for menu items */
10-
typedef enum {
11-
SettingsMenuOptions_WaveType,
12-
SettingsMenuOptions_Amplitude,
13-
SettingsMenuOptions_Period,
14-
} SettingsMenuOptions;
15-
16-
/** main menu callback - sends a custom event to the scene manager based on the menu selection */
8+
// Not actively used in this instance.
179
void menu_callback_settings_scene(void* context, uint32_t index) {
1810
UNUSED(context);
19-
// struct AppContext_t* app = context;
20-
switch(index) {
21-
case SettingsMenuOptions_WaveType:
22-
FURI_LOG_I(TAG, "selection one");
23-
// scene_manager_handle_custom_event(app->scene_manager, ToneGenAppEvent_StartPlayback);
24-
break;
25-
case SettingsMenuOptions_Amplitude:
26-
FURI_LOG_I(TAG, "selection two");
27-
// scene_manager_handle_custom_event(app->scene_manager, ToneGenAppEvent_AdjustTone);
28-
break;
29-
case SettingsMenuOptions_Period:
30-
FURI_LOG_I(TAG, "selection three");
31-
// scene_manager_handle_custom_event(app->scene_manager, ToneGenAppEvent_AdjustTone);
32-
break;
33-
}
11+
UNUSED(index);
12+
}
13+
14+
static uint8_t wave_option_values[] = {SINE, SQUARE};
15+
static char* wave_option_names[] = {"Sine", "Square"};
16+
static void wave_type_option_change(VariableItem* item) {
17+
struct AppContext_t* app = variable_item_get_context(item);
18+
uint8_t index = variable_item_get_current_value_index(item);
19+
variable_item_set_current_value_text(item, wave_option_names[index]);
20+
((struct ToneData_t*)app->additionalData)->waveType = index;
21+
}
22+
23+
// Since the max number of options for variable item lists is
24+
// the size of an 8-bit integer, we need to limit the max
25+
// number of steps. In this case, we limit it to 241 total
26+
// steps available, incrementing in steps of 10.
27+
#define MIN_FREQ 100
28+
#define MAX_FREQ 2500
29+
#define FREQ_STEPS 10
30+
#define INDEX_TO_FREQ(index) (uint16_t)((index * FREQ_STEPS) + MIN_FREQ)
31+
#define FREQ_TO_INDEX(freq) (uint8_t)((freq - MIN_FREQ) / FREQ_STEPS)
32+
char* frequencyStr;
33+
static void frequency_option_change(VariableItem* item) {
34+
struct AppContext_t* app = variable_item_get_context(item);
35+
uint8_t index = variable_item_get_current_value_index(item);
36+
((struct ToneData_t*)app->additionalData)->frequency = INDEX_TO_FREQ(index);
37+
snprintf(frequencyStr, 8, "%dhz", ((struct ToneData_t*)app->additionalData)->frequency);
38+
variable_item_set_current_value_text(item, frequencyStr);
3439
}
3540

3641
/** resets the menu, gives it content, callbacks and selection enums */
@@ -40,62 +45,49 @@ void scene_on_enter_settings_scene(void* context) {
4045

4146
// Setup our menu
4247
FURI_LOG_D(TAG, "Adding view menu");
43-
struct View_t* menuView = app->activeViews[ToneGenAppView_Submenu];
48+
struct View_t* variableItemListView = app->activeViews[ToneGenAppView_VariableItemList];
4449

4550
// Set the currently active view
46-
submenu_reset(menuView->viewData);
51+
variable_item_list_reset(variableItemListView->viewData);
4752

48-
submenu_set_header(menuView->viewData, "Tone Settings");
49-
50-
FURI_LOG_D(TAG, "Adding menu options for settings");
51-
submenu_add_item(
52-
menuView->viewData,
53+
FURI_LOG_D(TAG, "Adding options for settings");
54+
VariableItem* item = variable_item_list_add(
55+
variableItemListView->viewData,
5356
"Wave Type",
54-
SettingsMenuOptions_WaveType,
55-
menu_callback_settings_scene,
56-
app);
57-
submenu_add_item(
58-
menuView->viewData,
59-
"Amplitude",
60-
SettingsMenuOptions_Amplitude,
61-
menu_callback_settings_scene,
57+
COUNT_OF(wave_option_values),
58+
wave_type_option_change,
6259
app);
63-
submenu_add_item(
64-
menuView->viewData,
65-
"Period",
66-
SettingsMenuOptions_Period,
67-
menu_callback_settings_scene,
60+
variable_item_set_current_value_index(
61+
item, ((struct ToneData_t*)app->additionalData)->waveType);
62+
variable_item_set_current_value_text(
63+
item, wave_option_names[((struct ToneData_t*)app->additionalData)->waveType]);
64+
65+
item = variable_item_list_add(
66+
variableItemListView->viewData,
67+
"Frequency",
68+
FREQ_TO_INDEX(MAX_FREQ) + 1,
69+
frequency_option_change,
6870
app);
69-
view_dispatcher_switch_to_view(app->view_dispatcher, ToneGenAppView_Submenu);
71+
variable_item_set_current_value_index(
72+
item, FREQ_TO_INDEX(((struct ToneData_t*)app->additionalData)->frequency));
73+
74+
frequencyStr = calloc(8, sizeof(char));
75+
snprintf(frequencyStr, 8, "%dhz", ((struct ToneData_t*)app->additionalData)->frequency);
76+
variable_item_set_current_value_text(item, frequencyStr);
77+
78+
view_dispatcher_switch_to_view(app->view_dispatcher, ToneGenAppView_VariableItemList);
7079
}
7180

72-
/** main menu event handler - switches scene based on the event */
81+
// Not actively used in this instance.
7382
bool scene_on_event_settings_scene(void* context, SceneManagerEvent event) {
7483
FURI_LOG_I(TAG, "scene_on_event_settings_scene");
7584
UNUSED(context);
76-
// struct AppContext_t* app = context;
77-
bool consumed = false;
78-
switch(event.type) {
79-
case SceneManagerEventTypeCustom:
80-
// switch(event.event) {
81-
// case ToneGenAppEvent_StartPlayback:
82-
// scene_manager_next_scene(app->scene_manager, ToneGenAppScene_Playback);
83-
// consumed = true;
84-
// break;
85-
// case ToneGenAppEvent_AdjustTone:
86-
// scene_manager_next_scene(app->scene_manager, ToneGenAppScene_AdjustTone);
87-
// consumed = true;
88-
// break;
89-
// }
90-
break;
91-
default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
92-
consumed = false;
93-
break;
94-
}
95-
return consumed;
85+
UNUSED(event);
86+
return false;
9687
}
9788

9889
void scene_on_exit_settings_scene(void* context) {
9990
FURI_LOG_I(TAG, "scene_on_exit_settings_scene");
10091
UNUSED(context);
92+
free(frequencyStr);
10193
}

src/tone_gen.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ int32_t tone_gen_app(void* p) {
114114
if(result == APP_CONTEXT_OK) {
115115
appContext->additionalData = malloc(sizeof(struct ToneData_t));
116116
((struct ToneData_t*)appContext->additionalData)->animationOffset = 0;
117-
((struct ToneData_t*)appContext->additionalData)->amplitude = 1;
118-
((struct ToneData_t*)appContext->additionalData)->period = 1;
117+
((struct ToneData_t*)appContext->additionalData)->frequency = 440;
119118
((struct ToneData_t*)appContext->additionalData)->waveType = SINE;
120119

121120
result = setupViews(&appContext);

src/tone_gen.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ typedef enum {
2525
ToneGenAppView_count
2626
} ToneGenAppView;
2727

28-
typedef enum { SQUARE, SINE } ToneWaveType;
28+
typedef enum { SINE, SQUARE } ToneWaveType;
2929

3030
struct ToneData_t {
3131
int animationOffset;
32-
int amplitude;
33-
int period;
3432
ToneWaveType waveType;
33+
uint16_t frequency;
3534
};
3635

3736
#endif

0 commit comments

Comments
 (0)