From ea6ed333517acc2585e884fd129c037599e2c3ec Mon Sep 17 00:00:00 2001 From: RogueMaster Date: Mon, 19 Sep 2022 19:18:20 -0400 Subject: [PATCH] metronome update --- applications/plugins/metronome/README.md | 4 +- .../plugins/metronome/gui_extensions.c | 55 ++ .../plugins/metronome/gui_extensions.h | 3 + .../plugins/metronome/img/screenshot.png | Bin 1899 -> 1978 bytes .../plugins/metronome/img/wave_left_4x14.png | Bin 0 -> 144 bytes .../plugins/metronome/img/wave_right_4x14.png | Bin 0 -> 132 bytes applications/plugins/metronome/metronome.c | 531 ++++++++++-------- 7 files changed, 343 insertions(+), 250 deletions(-) create mode 100644 applications/plugins/metronome/gui_extensions.c create mode 100644 applications/plugins/metronome/gui_extensions.h create mode 100644 applications/plugins/metronome/img/wave_left_4x14.png create mode 100644 applications/plugins/metronome/img/wave_right_4x14.png diff --git a/applications/plugins/metronome/README.md b/applications/plugins/metronome/README.md index 8376c59c4..a8af47722 100644 --- a/applications/plugins/metronome/README.md +++ b/applications/plugins/metronome/README.md @@ -8,9 +8,11 @@ A metronome for the [Flipper Zero](https://flipperzero.one/) device. Goes along - BPM adjustable, fine and coarse (hold pressed) - Selectable amount of beats per bar -- First bar is pronounced - Selectable note length +- First beat is pronounced +- Progress indicator - LED flashes accordingly +- 3 different settings: Beep, Vibrate, Silent (push Down to change) ## Compiling diff --git a/applications/plugins/metronome/gui_extensions.c b/applications/plugins/metronome/gui_extensions.c new file mode 100644 index 000000000..7b2aa62fd --- /dev/null +++ b/applications/plugins/metronome/gui_extensions.c @@ -0,0 +1,55 @@ +#include +#include + +//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); +} diff --git a/applications/plugins/metronome/gui_extensions.h b/applications/plugins/metronome/gui_extensions.h new file mode 100644 index 000000000..97df1952c --- /dev/null +++ b/applications/plugins/metronome/gui_extensions.h @@ -0,0 +1,3 @@ +void elements_button_top_right(Canvas* canvas, const char* str); + +void elements_button_top_left(Canvas* canvas, const char* str); diff --git a/applications/plugins/metronome/img/screenshot.png b/applications/plugins/metronome/img/screenshot.png index a680e1216a9c7e57f45bf9e0ca94f0f2ef7af5a8..7b6916e81ac8e1984370d33397ac6664f57b5f6a 100644 GIT binary patch literal 1978 zcmbVNeNfV89Dfj<<<4w%mCJ-~)oST!>0D|8uCo_4JJST;>CPr6x~!O@5@4OHZKbU% z(tOEW(<~G%MDYdjT9bJSxP_UR=*`UWB1{o}28xqO+ppU{tv`PE{GR9g{(QdQ&+~2M zk%P`o8=U|E&Y_1wq5!a4`bB^h)+KBvZr9SX^3mGKT`MRo!`@6Z~^7qY?jg_o`{I%fhMoE?uum{0A5MZ^6Vg#Mh@q@`#mYhv`r}1;{EQ`BguAMzJ z={1VP15jmCBr8!}zt#nA?n;V7fPyv4g4!?hfFEb}y|9)5G7mGzG8T}HL`p=U`j2Ud zZ%#~rX{KWG6@z2Mdpra7q|%aZa_;b02$HJi+BX#!mr5IB78_D4>u2W_jdozgHURdm z1o@FMn0#&^p*Viv$M{^aVeIlAwVY`R?Sy`SsCDa>^A7`{V5b&w{i$4gEIejQadZozvGR4?%v8jj$ zi!2>N-YzVNr|6)778N2fK%1m2och@bY`zYb)AOTk-!)!6P@HhOf`PkrmAAoTDPOx~ z`2;p!TpK5pb!>00ar}OsA>U|1q(6jtC*}ciF@TH{BkR_R5M3cQyVqsE%hnJ;1Gv*? z?@q8FS4hFQG47v!-BMIvlt7+n^_NfRZ)RC8zi9uR21m3E6`Uq`2pPGvu9MoV!<=v| za~7$9!#g>RW)IX$R4mfCMzG*XP&H#Xh)P3%#>bn4cVo(5QM#d?-qYo8cr}9@LUclq zMdJkp_dg}`(z>~@+ItNkk=>g#Wtm?Xq3tKp8@QyoSp~xZ?xGb1ba}G%1ohXNWFji< zM8r_X^D-FVzagLXu^7Q(>_PeT%^AY1tA2w=o`VUyL6(F5MWOV#o@HK|h=&iF`LC*I8kmjY|aTOy_>U2s1= z2Yuz>qCEY|D3vG8evyMOYZ&N$Ql=}U61B`y|5f1Qf5uIV+(Bp1byFneZV5}x!d#xG z6Rg_4QA&SWrqB*n15Zo`Kt@4ZIc77<*0$EEdbjpccZr}VyRBsgY&I;2Uz6i)txHPZ z{ZI71B3<`Y3JF$T8w6wvh)!yq`HIFd_Fpcr^3Ln2w6V)uZ)>$Wdn|2m8}QyTikzR| z|5Rk(74@3Sa_2?;@@ovxH9lPejlhS$ZPf8p>Iw1t`BWH;Zyof1C#+hU#(5!=$q=I+ zZLrZUelJtv-|+Iy_H-+Wk;YUCE)JLw5_T5sj%t_qGNs}{Q#ZBxKyI_NbX;3Y#k35N zVtwy&i+D4wGIB;cbdB6Cd{o|Sx|LyQhuca^(VTlC)>AH?Bc1U%bEE>dRhq)q55f3r z9R#vFismx<{Gjzs@>D8Gn4;vp65!Y#l&_47)i_%AbNul3a>5o~n~O5TtkB1D(JKVj6sxDd;hFZ`10iAvCa02&=^aJR{jJL+3* z%y@1f`!jvSr6Ly1SBnH=Yi#nJjP;RhMi-gq%c!YAwaLv#t7J|KK!*gkOKj~MBUsC? zLHC>Ef(b^V>1+w!EMzMvf$CU4(H9YZdj6wP8du-ilMJ2}*(@+5mwX?AT9QV;^y%Kt oCuNK}+uQP=Dfs)r1Y-v*$QL~Uk1Kkr_|}uq{YOGL`;MRg3sjKxR{#J2 literal 1899 zcmb_de^AnA9DWfy%&utb+-|yXt+iUrbZcrQh?R@bOFd@@ZK<0zl4*8%pnxE2U2AKh zZT`wYDr+hzX2eXL&`){(guaMueZP2?T_!h-}m14^Ld}o=Xsv@ z{UI&}ztr2;8vwABuro3q01xLc40ySQsBv!)m~FXAVmBMg~!-xWi%iZayZR$y=@E@BhPk3=-pO zhVgqEi`Al#p4)f{VuUG|%u)moSb@anZF^wgs0)@Sh^QJvMP$}mim27po58i$-jJJ^ z&wU(g?iDHOi7$7!;EJjdmzs*3`rD;s%ss`mQ0;|QPDpd#*Yl8f~BTS$;I)2yDu%ru*c{6(dLN7<)40l|IPzqO-^ zj62!9mVNgIAO)gEB8Q;lEpAtiRhxo4`0E9yc}{gA@eVYw%+s0ru+blkU&#ls?c z(r&-lk0E`%h-fXiZ5wznax39X3Dr1s%ll=&UBDi~x-v9bLy2nBFTy}WSfF@xyr=C3 z1wvsIlKVbdkC1QMv#YVcIl&K^EL{McAKzJ9{bIpV18F4+=+s$471bc;#mX4fA*k?$ zVdXpB!*zl zoJup5u5h07q-$(bHU#@^HYhw)Gp!N@D!Q|BL@R~P>RC{f4gii^MY`^T0+%n{+MYMs zD@~aOnbFAl0QR$m=`V+n)^r7Nb}TPhPAx1LoYLl)vO6H{53s_j9PnHV$*#EU4>i8i zVgCt*3v#hy#nhv{B>=N`8a0S?w4}#^!gt-&_sJvr^Z_3)= zv4qs#m#n@l>R zm?6pjyxV86&uOKzs+rvD4BADqeU*UCv0-@WN=SUL4G`6{cxpv}`mbbd&C|*TWc~Tj zuJN}<%oMS3Rfw3LXTs3)GiJQN)kW^Rooa?nXg+5#ikGbB<4)5%Qce1woFY9i&!4W- zqpSy6MGP~tc9_rg=<~f>)=$8jk$`*9O;7Md%-|_btC<3mr7P^yLDJuzi-?^MCG3-r zO3HQ&*I&1@`=>Gc(O`h*=GOIGg^Nd3Y3vi9Wan0fjzW68P=qhuH_*Cd8c87k8W(=_ Ru~TjU5I&8Glx#~m@;5C)$5#LV diff --git a/applications/plugins/metronome/img/wave_left_4x14.png b/applications/plugins/metronome/img/wave_left_4x14.png new file mode 100644 index 0000000000000000000000000000000000000000..beb2a611d240fb68c960f0453b11d3dec3a50794 GIT binary patch literal 144 zcmeAS@N?(olHy`uVBq!ia0vp^EI`c1!2~4RIkfzN6k~CayA#8@b22YMvgwY#jtmSN z`?>!lvI6;>1s;*bKn;gMnDKc2iWH!rs;7%%h(vhu4}NAIhlT`43l26e_H&E4*!3EE jnV&mbIvhA~fPrE2w#elHp1mi48W=oX{an^LB{Ts5YlI{H literal 0 HcmV?d00001 diff --git a/applications/plugins/metronome/img/wave_right_4x14.png b/applications/plugins/metronome/img/wave_right_4x14.png new file mode 100644 index 0000000000000000000000000000000000000000..af249ee5b94055ba631122c6cd4588ca69e00b62 GIT binary patch literal 132 zcmeAS@N?(olHy`uVBq!ia0vp^EI`c1!2~4RIkfyi0_l#vjtmSN`?>!lvI6;>1s;*b zKsAR!nDKc2iWH!rnx~6nh(vfYL(_i)hDQf}DsY`U{@}pRkcSEq8Zs8#ry4i>`~RPx Yfj2vHxnZ!40#FBor>mdKI;Vst0FNsqT>t<8 literal 0 HcmV?d00001 diff --git a/applications/plugins/metronome/metronome.c b/applications/plugins/metronome/metronome.c index 9cec7244a..9b14d4b2c 100644 --- a/applications/plugins/metronome/metronome.c +++ b/applications/plugins/metronome/metronome.c @@ -7,165 +7,187 @@ #include #include #include -#include #include #include +#include "gui_extensions.h" + #define BPM_STEP_SIZE_FINE 0.5d #define BPM_STEP_SIZE_COARSE 10.0d #define BPM_BOUNDARY_LOW 10.0d #define BPM_BOUNDARY_HIGH 300.0d #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 { - EventTypeTick, - EventTypeKey, + EventTypeTick, + EventTypeKey, } EventType; typedef struct { - EventType type; - InputEvent input; + EventType type; + InputEvent input; } PluginEvent; +enum OutputMode { + Loud, + Vibro, + Silent +}; + typedef struct { - double bpm; - bool playing; - int beats_per_bar; - int note_length; - int current_beat; - FuriTimer* timer; - NotificationApp* notifications; + double bpm; + bool playing; + int beats_per_bar; + int note_length; + int current_beat; + enum OutputMode output_mode; + FuriTimer* timer; + NotificationApp* notifications; } 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) { - const MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); - if(metronome_state == NULL) { - return; - } - string_t tempStr; - string_init(tempStr); + const MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); + if(metronome_state == NULL) { + return; + } - // border around the edge of the screen - canvas_draw_frame(canvas, 0, 0, 128, 64); - canvas_set_font(canvas, FontPrimary); - - // draw bars/beat - 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)); - string_reset(tempStr); - // draw BPM value - string_printf(tempStr, "%.2f", metronome_state->bpm); - canvas_set_font(canvas, FontBigNumbers); - canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, string_get_cstr(tempStr)); - string_reset(tempStr); + string_t tempStr; + string_init(tempStr); - // draw button prompts - canvas_set_font(canvas, FontSecondary); - elements_button_left(canvas, "Slow"); - elements_button_right(canvas, "Fast"); - if (metronome_state->playing) { - elements_button_center(canvas, "Stop "); - } else { - elements_button_center(canvas, "Start"); - } - elements_button_top_left(canvas, "Push"); - elements_button_top_right(canvas, "Hold"); + canvas_draw_frame(canvas, 0, 0, 128, 64); - // draw progress bar - elements_progress_bar(canvas, 8, 36, 112, (float)metronome_state->current_beat/metronome_state->beats_per_bar); + canvas_set_font(canvas, FontPrimary); + + // draw bars/beat + 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)); + string_reset(tempStr); - string_clear(tempStr); - release_mutex((ValueMutex*)ctx, metronome_state); + // draw BPM value + string_printf(tempStr, "%.2f", metronome_state->bpm); + canvas_set_font(canvas, FontBigNumbers); + canvas_draw_str_aligned(canvas, 64, 24, AlignCenter, AlignCenter, string_get_cstr(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 + canvas_set_font(canvas, FontSecondary); + elements_button_left(canvas, "Slow"); + elements_button_right(canvas, "Fast"); + if (metronome_state->playing) { + elements_button_center(canvas, "Stop "); + } else { + elements_button_center(canvas, "Start"); + } + elements_button_top_left(canvas, "Push"); + elements_button_top_right(canvas, "Hold"); + + // draw progress bar + elements_progress_bar(canvas, 8, 36, 112, (float)metronome_state->current_beat/metronome_state->beats_per_bar); + + // cleanup + string_clear(tempStr); + release_mutex((ValueMutex*)ctx, metronome_state); } static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) { - furi_assert(event_queue); + furi_assert(event_queue); - PluginEvent event = {.type = EventTypeKey, .input = *input_event}; - furi_message_queue_put(event_queue, &event, FuriWaitForever); + PluginEvent event = {.type = EventTypeKey, .input = *input_event}; + furi_message_queue_put(event_queue, &event, FuriWaitForever); } static void timer_callback(void* ctx) { - MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); - metronome_state->current_beat++; - if (metronome_state->current_beat > metronome_state->beats_per_bar) { - metronome_state->current_beat = 1; + // this is where we go BEEP! + MetronomeState* metronome_state = acquire_mutex((ValueMutex*)ctx, 25); + metronome_state->current_beat++; + if (metronome_state->current_beat > metronome_state->beats_per_bar) { + metronome_state->current_beat = 1; + } + if (metronome_state->current_beat == 1) { + // pronounced beat + 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); + break; + case Vibro: + notification_message(metronome_state->notifications, &sequence_set_vibro_on); + break; + case Silent: + break; } - if (metronome_state->current_beat == 1) { - notification_message(metronome_state->notifications, &sequence_set_only_red_255); - furi_hal_speaker_start(440.0f, 1.0f); - } else { - notification_message(metronome_state->notifications, &sequence_set_only_green_255); - furi_hal_speaker_start(220.0f, 1.0f); - }; - furi_delay_ms(BEEP_DELAY_MS); - notification_message(metronome_state->notifications, &sequence_reset_rgb); - furi_hal_speaker_stop(); + } else { + // unpronounced beat + 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); + break; + case Vibro: + notification_message(metronome_state->notifications, &sequence_set_vibro_on); + break; + case Silent: + break; + } + }; - release_mutex((ValueMutex*)ctx, metronome_state); + // 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_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); } - static uint32_t state_to_sleep_ticks(MetronomeState* metronome_state) { - // calculate time between beeps - uint32_t tps = furi_kernel_get_tick_frequency(); - double multiplier = 4.0d/metronome_state->note_length; - double bps = (double)metronome_state->bpm / 60; - return (uint32_t)(round(tps / bps) - ((BEEP_DELAY_MS/1000)*tps)) * multiplier; + // calculate time between beeps + uint32_t tps = furi_kernel_get_tick_frequency(); + double multiplier = 4.0d/metronome_state->note_length; + double bps = (double)metronome_state->bpm / 60; + return (uint32_t)(round(tps / bps) - ((BEEP_DELAY_MS/1000)*tps)) * multiplier; } static void update_timer(MetronomeState* metronome_state) { @@ -195,144 +217,155 @@ static void decrease_bpm(MetronomeState* metronome_state, double amount) { } static void cycle_beats_per_bar(MetronomeState* metronome_state) { - metronome_state->beats_per_bar++; - if (metronome_state->beats_per_bar > metronome_state->note_length) { - metronome_state->beats_per_bar = 1; - } + metronome_state->beats_per_bar++; + if (metronome_state->beats_per_bar > metronome_state->note_length) { + metronome_state->beats_per_bar = 1; + } } static void cycle_note_length(MetronomeState* metronome_state) { - metronome_state->note_length *= 2; - if (metronome_state->note_length > 16) { - metronome_state->note_length = 2; - metronome_state->beats_per_bar = 1; + metronome_state->note_length *= 2; + if (metronome_state->note_length > 16) { + metronome_state->note_length = 2; + metronome_state->beats_per_bar = 1; + } + 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; } - update_timer(metronome_state); } static void metronome_state_init(MetronomeState* const metronome_state) { - metronome_state->bpm = 120.0; - metronome_state->playing = false; - metronome_state->beats_per_bar = 4; - metronome_state->note_length = 4; - metronome_state->current_beat = 0; - metronome_state->notifications = furi_record_open(RECORD_NOTIFICATION); + metronome_state->bpm = 120.0; + metronome_state->playing = false; + metronome_state->beats_per_bar = 4; + metronome_state->note_length = 4; + metronome_state->current_beat = 0; + metronome_state->output_mode = Loud; + metronome_state->notifications = furi_record_open(RECORD_NOTIFICATION); } int32_t metronome_app() { - FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); + FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent)); - MetronomeState* metronome_state = malloc(sizeof(MetronomeState)); - metronome_state_init(metronome_state); + MetronomeState* metronome_state = malloc(sizeof(MetronomeState)); + metronome_state_init(metronome_state); - ValueMutex state_mutex; - if(!init_mutex(&state_mutex, metronome_state, sizeof(MetronomeState))) { - FURI_LOG_E("Metronome", "cannot create mutex\r\n"); - free(metronome_state); - return 255; - } - - // Set system callbacks - ViewPort* view_port = view_port_alloc(); - view_port_draw_callback_set(view_port, render_callback, &state_mutex); - view_port_input_callback_set(view_port, input_callback, event_queue); - metronome_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, &state_mutex); - - // Open GUI and register view_port - Gui* gui = furi_record_open("gui"); - gui_add_view_port(gui, view_port, GuiLayerFullscreen); - - PluginEvent event; - for(bool processing = true; processing;) { - FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); - - MetronomeState* metronome_state = (MetronomeState*)acquire_mutex_block(&state_mutex); - - if(event_status == FuriStatusOk) { - // press events - if(event.type == EventTypeKey) { - if(event.input.type == InputTypeShort) { - switch(event.input.key) { - case InputKeyUp: - cycle_beats_per_bar(metronome_state); - break; - case InputKeyDown: - break; - case InputKeyRight: - increase_bpm(metronome_state, BPM_STEP_SIZE_FINE); - break; - case InputKeyLeft: - decrease_bpm(metronome_state, BPM_STEP_SIZE_FINE); - break; - case InputKeyOk: - metronome_state->playing = !metronome_state->playing; - if (metronome_state->playing) { - furi_timer_start(metronome_state->timer, state_to_sleep_ticks(metronome_state)); - } else { - furi_timer_stop(metronome_state->timer); - } - break; - case InputKeyBack: - processing = false; - break; - } - } else if (event.input.type == InputTypeLong) { - switch(event.input.key) { - case InputKeyUp: - cycle_note_length(metronome_state); - break; - case InputKeyDown: - break; - case InputKeyRight: - increase_bpm(metronome_state, BPM_STEP_SIZE_COARSE); - break; - case InputKeyLeft: - decrease_bpm(metronome_state, BPM_STEP_SIZE_COARSE); - break; - case InputKeyOk: - break; - case InputKeyBack: - processing = false; - break; - } - } else if (event.input.type == InputTypeRepeat) { - switch(event.input.key) { - case InputKeyUp: - break; - case InputKeyDown: - break; - case InputKeyRight: - increase_bpm(metronome_state, BPM_STEP_SIZE_COARSE); - break; - case InputKeyLeft: - decrease_bpm(metronome_state, BPM_STEP_SIZE_COARSE); - break; - case InputKeyOk: - break; - case InputKeyBack: - processing = false; - break; - } - } - } - } else { - FURI_LOG_D("Hello_world", "FuriMessageQueue: event timeout"); - // event timeout - } - - view_port_update(view_port); - release_mutex(&state_mutex, metronome_state); - } - - view_port_enabled_set(view_port, false); - gui_remove_view_port(gui, view_port); - furi_record_close("gui"); - view_port_free(view_port); - furi_message_queue_free(event_queue); - delete_mutex(&state_mutex); - furi_timer_free(metronome_state->timer); - furi_record_close(RECORD_NOTIFICATION); + ValueMutex state_mutex; + if(!init_mutex(&state_mutex, metronome_state, sizeof(MetronomeState))) { + FURI_LOG_E("Metronome", "cannot create mutex\r\n"); free(metronome_state); + return 255; + } - return 0; + // Set system callbacks + ViewPort* view_port = view_port_alloc(); + view_port_draw_callback_set(view_port, render_callback, &state_mutex); + view_port_input_callback_set(view_port, input_callback, event_queue); + metronome_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, &state_mutex); + + // Open GUI and register view_port + Gui* gui = furi_record_open("gui"); + gui_add_view_port(gui, view_port, GuiLayerFullscreen); + + PluginEvent event; + for(bool processing = true; processing;) { + FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100); + + MetronomeState* metronome_state = (MetronomeState*)acquire_mutex_block(&state_mutex); + + if(event_status == FuriStatusOk) { + if(event.type == EventTypeKey) { + if(event.input.type == InputTypeShort) { + // push events + switch(event.input.key) { + case InputKeyUp: + cycle_beats_per_bar(metronome_state); + break; + case InputKeyDown: + cycle_output_mode(metronome_state); + break; + case InputKeyRight: + increase_bpm(metronome_state, BPM_STEP_SIZE_FINE); + break; + case InputKeyLeft: + decrease_bpm(metronome_state, BPM_STEP_SIZE_FINE); + break; + case InputKeyOk: + metronome_state->playing = !metronome_state->playing; + if (metronome_state->playing) { + furi_timer_start(metronome_state->timer, state_to_sleep_ticks(metronome_state)); + } else { + furi_timer_stop(metronome_state->timer); + } + break; + case InputKeyBack: + processing = false; + break; + } + } else if (event.input.type == InputTypeLong) { + // hold events + switch(event.input.key) { + case InputKeyUp: + cycle_note_length(metronome_state); + break; + case InputKeyDown: + break; + case InputKeyRight: + increase_bpm(metronome_state, BPM_STEP_SIZE_COARSE); + break; + case InputKeyLeft: + decrease_bpm(metronome_state, BPM_STEP_SIZE_COARSE); + break; + case InputKeyOk: + break; + case InputKeyBack: + processing = false; + break; + } + } else if (event.input.type == InputTypeRepeat) { + // repeat events + switch(event.input.key) { + case InputKeyUp: + break; + case InputKeyDown: + break; + case InputKeyRight: + increase_bpm(metronome_state, BPM_STEP_SIZE_COARSE); + break; + case InputKeyLeft: + decrease_bpm(metronome_state, BPM_STEP_SIZE_COARSE); + break; + case InputKeyOk: + break; + case InputKeyBack: + processing = false; + break; + } + } + } + } else { + FURI_LOG_D("Metronome", "FuriMessageQueue: event timeout"); + // event timeout + } + + view_port_update(view_port); + release_mutex(&state_mutex, metronome_state); + } + + view_port_enabled_set(view_port, false); + gui_remove_view_port(gui, view_port); + furi_record_close("gui"); + view_port_free(view_port); + furi_message_queue_free(event_queue); + delete_mutex(&state_mutex); + furi_timer_free(metronome_state->timer); + furi_record_close(RECORD_NOTIFICATION); + free(metronome_state); + + return 0; }