metronome update

This commit is contained in:
RogueMaster
2022-09-19 19:18:20 -04:00
parent 518d94a031
commit ea6ed33351
7 changed files with 343 additions and 250 deletions

View File

@@ -8,9 +8,11 @@ A metronome for the [Flipper Zero](https://flipperzero.one/) device. Goes along
- BPM adjustable, fine and coarse (hold pressed) - BPM adjustable, fine and coarse (hold pressed)
- Selectable amount of beats per bar - Selectable amount of beats per bar
- First bar is pronounced
- Selectable note length - Selectable note length
- First beat is pronounced
- Progress indicator
- LED flashes accordingly - LED flashes accordingly
- 3 different settings: Beep, Vibrate, Silent (push Down to change)
## Compiling ## Compiling

View File

@@ -0,0 +1,55 @@
#include <gui/canvas.h>
#include <gui/icon_i.h>
//lib can only do bottom left/right
void elements_button_top_left(Canvas* canvas, const char* str) {
const uint8_t button_height = 12;
const uint8_t vertical_offset = 3;
const uint8_t horizontal_offset = 3;
const uint8_t string_width = canvas_string_width(canvas, str);
const Icon* icon = &I_ButtonUp_7x4;
const uint8_t icon_h_offset = 3;
const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
const uint8_t icon_v_offset = icon->height + vertical_offset;
const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
const uint8_t x = 0;
const uint8_t y = 0 + button_height;
canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
canvas_draw_line(canvas, x + button_width + 0, y - button_height, x + button_width + 0, y - 1);
canvas_draw_line(canvas, x + button_width + 1, y - button_height, x + button_width + 1, y - 2);
canvas_draw_line(canvas, x + button_width + 2, y - button_height, x + button_width + 2, y - 3);
canvas_invert_color(canvas);
canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonUp_7x4);
canvas_draw_str(
canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
canvas_invert_color(canvas);
}
void elements_button_top_right(Canvas* canvas, const char* str) {
const uint8_t button_height = 12;
const uint8_t vertical_offset = 3;
const uint8_t horizontal_offset = 3;
const uint8_t string_width = canvas_string_width(canvas, str);
const Icon* icon = &I_ButtonUp_7x4;
const uint8_t icon_h_offset = 3;
const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
const uint8_t icon_v_offset = icon->height + vertical_offset;
const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
const uint8_t x = canvas_width(canvas);
const uint8_t y = 0 + button_height;
canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
canvas_invert_color(canvas);
canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
canvas_draw_icon(
canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonUp_7x4);
canvas_invert_color(canvas);
}

View File

@@ -0,0 +1,3 @@
void elements_button_top_right(Canvas* canvas, const char* str);
void elements_button_top_left(Canvas* canvas, const char* str);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 B

View File

@@ -7,17 +7,32 @@
#include <gui/gui.h> #include <gui/gui.h>
#include <gui/elements.h> #include <gui/elements.h>
#include <gui/canvas.h> #include <gui/canvas.h>
#include <gui/icon_i.h>
#include <notification/notification.h> #include <notification/notification.h>
#include <notification/notification_messages.h> #include <notification/notification_messages.h>
#include "gui_extensions.h"
#define BPM_STEP_SIZE_FINE 0.5d #define BPM_STEP_SIZE_FINE 0.5d
#define BPM_STEP_SIZE_COARSE 10.0d #define BPM_STEP_SIZE_COARSE 10.0d
#define BPM_BOUNDARY_LOW 10.0d #define BPM_BOUNDARY_LOW 10.0d
#define BPM_BOUNDARY_HIGH 300.0d #define BPM_BOUNDARY_HIGH 300.0d
#define BEEP_DELAY_MS 50 #define BEEP_DELAY_MS 50
#define wave_bitmap_left_width 4
#define wave_bitmap_left_height 14
static uint8_t wave_bitmap_left_bits[] = {
0x08, 0x0C, 0x06, 0x06, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x06, 0x06,
0x0C, 0x08
};
#define wave_bitmap_right_width 4
#define wave_bitmap_right_height 14
static uint8_t wave_bitmap_right_bits[] = {
0x01, 0x03, 0x06, 0x06, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x06, 0x06,
0x03, 0x01
};
typedef enum { typedef enum {
EventTypeTick, EventTypeTick,
EventTypeKey, EventTypeKey,
@@ -28,91 +43,59 @@ typedef struct {
InputEvent input; InputEvent input;
} PluginEvent; } PluginEvent;
enum OutputMode {
Loud,
Vibro,
Silent
};
typedef struct { typedef struct {
double bpm; double bpm;
bool playing; bool playing;
int beats_per_bar; int beats_per_bar;
int note_length; int note_length;
int current_beat; int current_beat;
enum OutputMode output_mode;
FuriTimer* timer; FuriTimer* timer;
NotificationApp* notifications; NotificationApp* notifications;
} MetronomeState; } MetronomeState;
//lib can only do bottom left/right
static void elements_button_top_left(Canvas* canvas, const char* str) {
const uint8_t button_height = 12;
const uint8_t vertical_offset = 3;
const uint8_t horizontal_offset = 3;
const uint8_t string_width = canvas_string_width(canvas, str);
const Icon* icon = &I_ButtonUp_7x4;
const uint8_t icon_h_offset = 3;
const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
const uint8_t icon_v_offset = icon->height + vertical_offset;
const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
const uint8_t x = 0;
const uint8_t y = 0 + button_height;
canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
canvas_draw_line(canvas, x + button_width + 0, y - button_height, x + button_width + 0, y - 1);
canvas_draw_line(canvas, x + button_width + 1, y - button_height, x + button_width + 1, y - 2);
canvas_draw_line(canvas, x + button_width + 2, y - button_height, x + button_width + 2, y - 3);
canvas_invert_color(canvas);
canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonUp_7x4);
canvas_draw_str(
canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
canvas_invert_color(canvas);
}
void elements_button_top_right(Canvas* canvas, const char* str) {
const uint8_t button_height = 12;
const uint8_t vertical_offset = 3;
const uint8_t horizontal_offset = 3;
const uint8_t string_width = canvas_string_width(canvas, str);
const Icon* icon = &I_ButtonUp_7x4;
const uint8_t icon_h_offset = 3;
const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
const uint8_t icon_v_offset = icon->height + vertical_offset;
const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
const uint8_t x = canvas_width(canvas);
const uint8_t y = 0 + button_height;
canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
canvas_invert_color(canvas);
canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
canvas_draw_icon(
canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonUp_7x4);
canvas_invert_color(canvas);
}
static void render_callback(Canvas* const canvas, void* ctx) { static void render_callback(Canvas* const canvas, void* ctx) {
const MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); const MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25);
if(metronome_state == NULL) { if(metronome_state == NULL) {
return; return;
} }
string_t tempStr; string_t tempStr;
string_init(tempStr); string_init(tempStr);
// border around the edge of the screen
canvas_draw_frame(canvas, 0, 0, 128, 64); canvas_draw_frame(canvas, 0, 0, 128, 64);
canvas_set_font(canvas, FontPrimary); canvas_set_font(canvas, FontPrimary);
// draw bars/beat // draw bars/beat
string_printf(tempStr, "%d/%d", metronome_state->beats_per_bar, metronome_state->note_length); string_printf(tempStr, "%d/%d", metronome_state->beats_per_bar, metronome_state->note_length);
canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, string_get_cstr(tempStr)); canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, string_get_cstr(tempStr));
string_reset(tempStr); string_reset(tempStr);
// draw BPM value // draw BPM value
string_printf(tempStr, "%.2f", metronome_state->bpm); string_printf(tempStr, "%.2f", metronome_state->bpm);
canvas_set_font(canvas, FontBigNumbers); canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, string_get_cstr(tempStr)); canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, string_get_cstr(tempStr));
string_reset(tempStr); string_reset(tempStr);
// draw volume indicator
// always draw first waves
canvas_draw_xbm(canvas, 20, 17, wave_bitmap_left_width, wave_bitmap_left_height, wave_bitmap_left_bits);
canvas_draw_xbm(canvas, canvas_width(canvas)-20-wave_bitmap_right_width, 17, wave_bitmap_right_width, wave_bitmap_right_height, wave_bitmap_right_bits);
if (metronome_state->output_mode < Silent) {
canvas_draw_xbm(canvas, 16, 17, wave_bitmap_left_width, wave_bitmap_left_height, wave_bitmap_left_bits);
canvas_draw_xbm(canvas, canvas_width(canvas)-16-wave_bitmap_right_width, 17, wave_bitmap_right_width, wave_bitmap_right_height, wave_bitmap_right_bits);
}
if (metronome_state->output_mode < Vibro) {
canvas_draw_xbm(canvas, 12, 17, wave_bitmap_left_width, wave_bitmap_left_height, wave_bitmap_left_bits);
canvas_draw_xbm(canvas, canvas_width(canvas)-12-wave_bitmap_right_width, 17, wave_bitmap_right_width, wave_bitmap_right_height, wave_bitmap_right_bits);
}
// draw button prompts // draw button prompts
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
elements_button_left(canvas, "Slow"); elements_button_left(canvas, "Slow");
@@ -128,6 +111,7 @@ static void render_callback(Canvas* const canvas, void* ctx) {
// draw progress bar // draw progress bar
elements_progress_bar(canvas, 8, 36, 112, (float)metronome_state->current_beat/metronome_state->beats_per_bar); elements_progress_bar(canvas, 8, 36, 112, (float)metronome_state->current_beat/metronome_state->beats_per_bar);
// cleanup
string_clear(tempStr); string_clear(tempStr);
release_mutex((ValueMutex*)ctx, metronome_state); release_mutex((ValueMutex*)ctx, metronome_state);
} }
@@ -140,26 +124,64 @@ static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queu
} }
static void timer_callback(void* ctx) { static void timer_callback(void* ctx) {
// this is where we go BEEP!
MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25);
metronome_state->current_beat++; metronome_state->current_beat++;
if (metronome_state->current_beat > metronome_state->beats_per_bar) { if (metronome_state->current_beat > metronome_state->beats_per_bar) {
metronome_state->current_beat = 1; metronome_state->current_beat = 1;
} }
if (metronome_state->current_beat == 1) { if (metronome_state->current_beat == 1) {
// pronounced beat
notification_message(metronome_state->notifications, &sequence_set_only_red_255); notification_message(metronome_state->notifications, &sequence_set_only_red_255);
switch(metronome_state->output_mode) {
case Loud:
furi_hal_speaker_start(440.0f, 1.0f); furi_hal_speaker_start(440.0f, 1.0f);
break;
case Vibro:
notification_message(metronome_state->notifications, &sequence_set_vibro_on);
break;
case Silent:
break;
}
} else { } else {
// unpronounced beat
notification_message(metronome_state->notifications, &sequence_set_only_green_255); notification_message(metronome_state->notifications, &sequence_set_only_green_255);
switch(metronome_state->output_mode) {
case Loud:
furi_hal_speaker_start(220.0f, 1.0f); furi_hal_speaker_start(220.0f, 1.0f);
break;
case Vibro:
notification_message(metronome_state->notifications, &sequence_set_vibro_on);
break;
case Silent:
break;
}
}; };
// this is a bit of a kludge... if we are on vibro and unpronounced, stop vibro after half the usual duration
switch(metronome_state->output_mode) {
case Loud:
furi_delay_ms(BEEP_DELAY_MS); furi_delay_ms(BEEP_DELAY_MS);
notification_message(metronome_state->notifications, &sequence_reset_rgb);
furi_hal_speaker_stop(); furi_hal_speaker_stop();
break;
case Vibro:
if (metronome_state->current_beat == 1) {
furi_delay_ms(BEEP_DELAY_MS);
notification_message(metronome_state->notifications, &sequence_reset_vibro);
} else {
furi_delay_ms((int)BEEP_DELAY_MS/2);
notification_message(metronome_state->notifications, &sequence_reset_vibro);
furi_delay_ms((int)BEEP_DELAY_MS/2);
}
break;
case Silent:
break;
}
notification_message(metronome_state->notifications, &sequence_reset_rgb);
release_mutex((ValueMutex*)ctx, metronome_state); release_mutex((ValueMutex*)ctx, metronome_state);
} }
static uint32_t state_to_sleep_ticks(MetronomeState* metronome_state) { static uint32_t state_to_sleep_ticks(MetronomeState* metronome_state) {
// calculate time between beeps // calculate time between beeps
uint32_t tps = furi_kernel_get_tick_frequency(); uint32_t tps = furi_kernel_get_tick_frequency();
@@ -210,12 +232,20 @@ static void cycle_note_length(MetronomeState* metronome_state) {
update_timer(metronome_state); update_timer(metronome_state);
} }
static void cycle_output_mode(MetronomeState* metronome_state) {
metronome_state->output_mode++;
if (metronome_state->output_mode > Silent) {
metronome_state->output_mode = Loud;
}
}
static void metronome_state_init(MetronomeState* const metronome_state) { static void metronome_state_init(MetronomeState* const metronome_state) {
metronome_state->bpm = 120.0; metronome_state->bpm = 120.0;
metronome_state->playing = false; metronome_state->playing = false;
metronome_state->beats_per_bar = 4; metronome_state->beats_per_bar = 4;
metronome_state->note_length = 4; metronome_state->note_length = 4;
metronome_state->current_beat = 0; metronome_state->current_beat = 0;
metronome_state->output_mode = Loud;
metronome_state->notifications = furi_record_open(RECORD_NOTIFICATION); metronome_state->notifications = furi_record_open(RECORD_NOTIFICATION);
} }
@@ -249,14 +279,15 @@ int32_t metronome_app() {
MetronomeState* metronome_state = (MetronomeState*)acquire_mutex_block(&state_mutex); MetronomeState* metronome_state = (MetronomeState*)acquire_mutex_block(&state_mutex);
if(event_status == FuriStatusOk) { if(event_status == FuriStatusOk) {
// press events
if(event.type == EventTypeKey) { if(event.type == EventTypeKey) {
if(event.input.type == InputTypeShort) { if(event.input.type == InputTypeShort) {
// push events
switch(event.input.key) { switch(event.input.key) {
case InputKeyUp: case InputKeyUp:
cycle_beats_per_bar(metronome_state); cycle_beats_per_bar(metronome_state);
break; break;
case InputKeyDown: case InputKeyDown:
cycle_output_mode(metronome_state);
break; break;
case InputKeyRight: case InputKeyRight:
increase_bpm(metronome_state, BPM_STEP_SIZE_FINE); increase_bpm(metronome_state, BPM_STEP_SIZE_FINE);
@@ -277,6 +308,7 @@ int32_t metronome_app() {
break; break;
} }
} else if (event.input.type == InputTypeLong) { } else if (event.input.type == InputTypeLong) {
// hold events
switch(event.input.key) { switch(event.input.key) {
case InputKeyUp: case InputKeyUp:
cycle_note_length(metronome_state); cycle_note_length(metronome_state);
@@ -296,6 +328,7 @@ int32_t metronome_app() {
break; break;
} }
} else if (event.input.type == InputTypeRepeat) { } else if (event.input.type == InputTypeRepeat) {
// repeat events
switch(event.input.key) { switch(event.input.key) {
case InputKeyUp: case InputKeyUp:
break; break;
@@ -316,7 +349,7 @@ int32_t metronome_app() {
} }
} }
} else { } else {
FURI_LOG_D("Hello_world", "FuriMessageQueue: event timeout"); FURI_LOG_D("Metronome", "FuriMessageQueue: event timeout");
// event timeout // event timeout
} }