mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Merge branch '420' of https://github.com/RogueMaster/flipperzero-firmware-wPlugins into Dev
This commit is contained in:
@@ -408,15 +408,16 @@ void subghz_scene_receiver_config_on_enter(void* context) {
|
||||
variable_item_set_current_value_text(item, rssi_threshold_text[value_index]);
|
||||
|
||||
// Lock keyboard
|
||||
item = variable_item_list_add(
|
||||
subghz->variable_item_list,
|
||||
"Sound:",
|
||||
SPEAKER_COUNT,
|
||||
subghz_scene_receiver_config_set_speaker,
|
||||
subghz);
|
||||
value_index = value_index_uint32(subghz->txrx->speaker_state, speaker_value, SPEAKER_COUNT);
|
||||
variable_item_set_current_value_index(item, value_index);
|
||||
variable_item_set_current_value_text(item, speaker_text[value_index]);
|
||||
item = variable_item_list_add(
|
||||
subghz->variable_item_list,
|
||||
"Sound:",
|
||||
SPEAKER_COUNT,
|
||||
subghz_scene_receiver_config_set_speaker,
|
||||
subghz);
|
||||
value_index =
|
||||
value_index_uint32(subghz->txrx->speaker_state, speaker_value, SPEAKER_COUNT);
|
||||
variable_item_set_current_value_index(item, value_index);
|
||||
variable_item_set_current_value_text(item, speaker_text[value_index]);
|
||||
|
||||
variable_item_list_add(subghz->variable_item_list, "Lock Keyboard", 1, NULL, NULL);
|
||||
variable_item_list_set_enter_callback(
|
||||
|
||||
10
applications/plugins/clock/application.fam
Normal file
10
applications/plugins/clock/application.fam
Normal file
@@ -0,0 +1,10 @@
|
||||
App(
|
||||
appid="ClockV1",
|
||||
name="Clock",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="clock_app",
|
||||
requires=["gui"],
|
||||
stack_size=2 * 1024,
|
||||
fap_icon="clock.png",
|
||||
fap_category="Tools",
|
||||
)
|
||||
@@ -1,5 +1,5 @@
|
||||
App(
|
||||
appid="GPIOReader",
|
||||
appid="GPIO_Reader_B",
|
||||
name="GPIO Reader (biotinker)",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="gpio_app",
|
||||
@@ -9,4 +9,5 @@ App(
|
||||
order=50,
|
||||
fap_libs=["assets"],
|
||||
fap_category="GPIO",
|
||||
fap_icon="icon.png",
|
||||
)
|
||||
|
||||
BIN
applications/plugins/gpioreader/icon.png
Normal file
BIN
applications/plugins/gpioreader/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 135 B |
@@ -1,5 +1,5 @@
|
||||
App(
|
||||
appid="GPIOReader2",
|
||||
appid="GPIO_Reader_A",
|
||||
name="GPIO Reader (Aurelilc)",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="GPIO_reader_app",
|
||||
|
||||
367
applications/plugins/music_beeper/music_beeper.c
Normal file
367
applications/plugins/music_beeper/music_beeper.c
Normal file
@@ -0,0 +1,367 @@
|
||||
#include "music_beeper_worker.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include <Music_Beeper_icons.h>
|
||||
#include <gui/gui.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#define TAG "MusicBeeper"
|
||||
|
||||
#define MUSIC_BEEPER_APP_PATH_FOLDER ANY_PATH("music_player")
|
||||
#define MUSIC_BEEPER_APP_EXTENSION "*"
|
||||
|
||||
#define MUSIC_BEEPER_SEMITONE_HISTORY_SIZE 4
|
||||
|
||||
typedef struct {
|
||||
uint8_t semitone_history[MUSIC_BEEPER_SEMITONE_HISTORY_SIZE];
|
||||
uint8_t duration_history[MUSIC_BEEPER_SEMITONE_HISTORY_SIZE];
|
||||
|
||||
uint8_t volume;
|
||||
uint8_t semitone;
|
||||
uint8_t dots;
|
||||
uint8_t duration;
|
||||
float position;
|
||||
} MusicBeeperModel;
|
||||
|
||||
typedef struct {
|
||||
MusicBeeperModel* model;
|
||||
FuriMutex** model_mutex;
|
||||
|
||||
FuriMessageQueue* input_queue;
|
||||
|
||||
ViewPort* view_port;
|
||||
Gui* gui;
|
||||
|
||||
MusicBeeperWorker* worker;
|
||||
} MusicBeeper;
|
||||
|
||||
static const float MUSIC_BEEPER_VOLUMES[] = {0, .25, .5, .75, 1};
|
||||
|
||||
static const char* semitone_to_note(int8_t semitone) {
|
||||
switch(semitone) {
|
||||
case 0:
|
||||
return "C";
|
||||
case 1:
|
||||
return "C#";
|
||||
case 2:
|
||||
return "D";
|
||||
case 3:
|
||||
return "D#";
|
||||
case 4:
|
||||
return "E";
|
||||
case 5:
|
||||
return "F";
|
||||
case 6:
|
||||
return "F#";
|
||||
case 7:
|
||||
return "G";
|
||||
case 8:
|
||||
return "G#";
|
||||
case 9:
|
||||
return "A";
|
||||
case 10:
|
||||
return "A#";
|
||||
case 11:
|
||||
return "B";
|
||||
default:
|
||||
return "--";
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_white_note(uint8_t semitone, uint8_t id) {
|
||||
switch(semitone) {
|
||||
case 0:
|
||||
if(id == 0) return true;
|
||||
break;
|
||||
case 2:
|
||||
if(id == 1) return true;
|
||||
break;
|
||||
case 4:
|
||||
if(id == 2) return true;
|
||||
break;
|
||||
case 5:
|
||||
if(id == 3) return true;
|
||||
break;
|
||||
case 7:
|
||||
if(id == 4) return true;
|
||||
break;
|
||||
case 9:
|
||||
if(id == 5) return true;
|
||||
break;
|
||||
case 11:
|
||||
if(id == 6) return true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_black_note(uint8_t semitone, uint8_t id) {
|
||||
switch(semitone) {
|
||||
case 1:
|
||||
if(id == 0) return true;
|
||||
break;
|
||||
case 3:
|
||||
if(id == 1) return true;
|
||||
break;
|
||||
case 6:
|
||||
if(id == 3) return true;
|
||||
break;
|
||||
case 8:
|
||||
if(id == 4) return true;
|
||||
break;
|
||||
case 10:
|
||||
if(id == 5) return true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void render_callback(Canvas* canvas, void* ctx) {
|
||||
MusicBeeper* music_beeper = ctx;
|
||||
furi_check(furi_mutex_acquire(music_beeper->model_mutex, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 12, "MusicBeeper");
|
||||
|
||||
uint8_t x_pos = 0;
|
||||
uint8_t y_pos = 24;
|
||||
const uint8_t white_w = 10;
|
||||
const uint8_t white_h = 40;
|
||||
|
||||
const int8_t black_x = 6;
|
||||
const int8_t black_y = -5;
|
||||
const uint8_t black_w = 8;
|
||||
const uint8_t black_h = 32;
|
||||
|
||||
// white keys
|
||||
for(size_t i = 0; i < 7; i++) {
|
||||
if(is_white_note(music_beeper->model->semitone, i)) {
|
||||
canvas_draw_box(canvas, x_pos + white_w * i, y_pos, white_w + 1, white_h);
|
||||
} else {
|
||||
canvas_draw_frame(canvas, x_pos + white_w * i, y_pos, white_w + 1, white_h);
|
||||
}
|
||||
}
|
||||
|
||||
// black keys
|
||||
for(size_t i = 0; i < 7; i++) {
|
||||
if(i != 2 && i != 6) {
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_box(
|
||||
canvas, x_pos + white_w * i + black_x, y_pos + black_y, black_w + 1, black_h);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
if(is_black_note(music_beeper->model->semitone, i)) {
|
||||
canvas_draw_box(
|
||||
canvas, x_pos + white_w * i + black_x, y_pos + black_y, black_w + 1, black_h);
|
||||
} else {
|
||||
canvas_draw_frame(
|
||||
canvas, x_pos + white_w * i + black_x, y_pos + black_y, black_w + 1, black_h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// volume view_port
|
||||
x_pos = 124;
|
||||
y_pos = 0;
|
||||
const uint8_t volume_h =
|
||||
(64 / (COUNT_OF(MUSIC_BEEPER_VOLUMES) - 1)) * music_beeper->model->volume;
|
||||
canvas_draw_frame(canvas, x_pos, y_pos, 4, 64);
|
||||
canvas_draw_box(canvas, x_pos, y_pos + (64 - volume_h), 4, volume_h);
|
||||
|
||||
// note stack view_port
|
||||
x_pos = 73;
|
||||
y_pos = 0;
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_frame(canvas, x_pos, y_pos, 49, 64);
|
||||
canvas_draw_line(canvas, x_pos + 28, 0, x_pos + 28, 64);
|
||||
|
||||
char duration_text[16];
|
||||
for(uint8_t i = 0; i < MUSIC_BEEPER_SEMITONE_HISTORY_SIZE; i++) {
|
||||
if(music_beeper->model->duration_history[i] == 0xFF) {
|
||||
snprintf(duration_text, 15, "--");
|
||||
} else {
|
||||
snprintf(duration_text, 15, "%d", music_beeper->model->duration_history[i]);
|
||||
}
|
||||
|
||||
if(i == 0) {
|
||||
canvas_draw_box(canvas, x_pos, y_pos + 48, 49, 16);
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
} else {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
}
|
||||
canvas_draw_str(
|
||||
canvas,
|
||||
x_pos + 4,
|
||||
64 - 16 * i - 3,
|
||||
semitone_to_note(music_beeper->model->semitone_history[i]));
|
||||
canvas_draw_str(canvas, x_pos + 31, 64 - 16 * i - 3, duration_text);
|
||||
canvas_draw_line(canvas, x_pos, 64 - 16 * i, x_pos + 48, 64 - 16 * i);
|
||||
}
|
||||
|
||||
furi_mutex_release(music_beeper->model_mutex);
|
||||
}
|
||||
|
||||
static void input_callback(InputEvent* input_event, void* ctx) {
|
||||
MusicBeeper* music_beeper = ctx;
|
||||
if(input_event->type == InputTypeShort) {
|
||||
furi_message_queue_put(music_beeper->input_queue, input_event, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void music_beeper_worker_callback(
|
||||
uint8_t semitone,
|
||||
uint8_t dots,
|
||||
uint8_t duration,
|
||||
float position,
|
||||
void* context) {
|
||||
MusicBeeper* music_beeper = context;
|
||||
furi_check(furi_mutex_acquire(music_beeper->model_mutex, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
for(size_t i = 0; i < MUSIC_BEEPER_SEMITONE_HISTORY_SIZE - 1; i++) {
|
||||
size_t r = MUSIC_BEEPER_SEMITONE_HISTORY_SIZE - 1 - i;
|
||||
music_beeper->model->duration_history[r] = music_beeper->model->duration_history[r - 1];
|
||||
music_beeper->model->semitone_history[r] = music_beeper->model->semitone_history[r - 1];
|
||||
}
|
||||
|
||||
semitone = (semitone == 0xFF) ? 0xFF : semitone % 12;
|
||||
|
||||
music_beeper->model->semitone = semitone;
|
||||
music_beeper->model->dots = dots;
|
||||
music_beeper->model->duration = duration;
|
||||
music_beeper->model->position = position;
|
||||
|
||||
music_beeper->model->semitone_history[0] = semitone;
|
||||
music_beeper->model->duration_history[0] = duration;
|
||||
|
||||
furi_mutex_release(music_beeper->model_mutex);
|
||||
view_port_update(music_beeper->view_port);
|
||||
}
|
||||
|
||||
void music_beeper_clear(MusicBeeper* instance) {
|
||||
memset(instance->model->duration_history, 0xff, MUSIC_BEEPER_SEMITONE_HISTORY_SIZE);
|
||||
memset(instance->model->semitone_history, 0xff, MUSIC_BEEPER_SEMITONE_HISTORY_SIZE);
|
||||
music_beeper_worker_clear(instance->worker);
|
||||
}
|
||||
|
||||
MusicBeeper* music_beeper_alloc() {
|
||||
MusicBeeper* instance = malloc(sizeof(MusicBeeper));
|
||||
|
||||
instance->model = malloc(sizeof(MusicBeeperModel));
|
||||
instance->model->volume = 4;
|
||||
|
||||
instance->model_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
|
||||
instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
|
||||
|
||||
instance->worker = music_beeper_worker_alloc();
|
||||
music_beeper_worker_set_volume(
|
||||
instance->worker, MUSIC_BEEPER_VOLUMES[instance->model->volume]);
|
||||
music_beeper_worker_set_callback(instance->worker, music_beeper_worker_callback, instance);
|
||||
|
||||
music_beeper_clear(instance);
|
||||
|
||||
instance->view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(instance->view_port, render_callback, instance);
|
||||
view_port_input_callback_set(instance->view_port, input_callback, instance);
|
||||
|
||||
// Open GUI and register view_port
|
||||
instance->gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void music_beeper_free(MusicBeeper* instance) {
|
||||
gui_remove_view_port(instance->gui, instance->view_port);
|
||||
furi_record_close(RECORD_GUI);
|
||||
view_port_free(instance->view_port);
|
||||
|
||||
music_beeper_worker_free(instance->worker);
|
||||
|
||||
furi_message_queue_free(instance->input_queue);
|
||||
|
||||
furi_mutex_free(instance->model_mutex);
|
||||
|
||||
free(instance->model);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
int32_t music_beeper_app(void* p) {
|
||||
MusicBeeper* music_beeper = music_beeper_alloc();
|
||||
|
||||
FuriString* file_path;
|
||||
file_path = furi_string_alloc();
|
||||
|
||||
do {
|
||||
if(p && strlen(p)) {
|
||||
furi_string_set(file_path, (const char*)p);
|
||||
} else {
|
||||
furi_string_set(file_path, MUSIC_BEEPER_APP_PATH_FOLDER);
|
||||
|
||||
DialogsFileBrowserOptions browser_options;
|
||||
dialog_file_browser_set_basic_options(
|
||||
&browser_options, MUSIC_BEEPER_APP_EXTENSION, &I_music_10px);
|
||||
browser_options.hide_ext = false;
|
||||
|
||||
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
|
||||
bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
|
||||
|
||||
furi_record_close(RECORD_DIALOGS);
|
||||
if(!res) {
|
||||
FURI_LOG_E(TAG, "No file selected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!music_beeper_worker_load(music_beeper->worker, furi_string_get_cstr(file_path))) {
|
||||
FURI_LOG_E(TAG, "Unable to load file");
|
||||
break;
|
||||
}
|
||||
|
||||
music_beeper_worker_start(music_beeper->worker);
|
||||
|
||||
InputEvent input;
|
||||
while(furi_message_queue_get(music_beeper->input_queue, &input, FuriWaitForever) ==
|
||||
FuriStatusOk) {
|
||||
furi_check(
|
||||
furi_mutex_acquire(music_beeper->model_mutex, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
if(input.key == InputKeyBack) {
|
||||
furi_mutex_release(music_beeper->model_mutex);
|
||||
break;
|
||||
} else if(input.key == InputKeyUp) {
|
||||
if(music_beeper->model->volume < COUNT_OF(MUSIC_BEEPER_VOLUMES) - 1)
|
||||
music_beeper->model->volume++;
|
||||
music_beeper_worker_set_volume(
|
||||
music_beeper->worker, MUSIC_BEEPER_VOLUMES[music_beeper->model->volume]);
|
||||
} else if(input.key == InputKeyDown) {
|
||||
if(music_beeper->model->volume > 0) music_beeper->model->volume--;
|
||||
music_beeper_worker_set_volume(
|
||||
music_beeper->worker, MUSIC_BEEPER_VOLUMES[music_beeper->model->volume]);
|
||||
}
|
||||
|
||||
furi_mutex_release(music_beeper->model_mutex);
|
||||
view_port_update(music_beeper->view_port);
|
||||
}
|
||||
|
||||
music_beeper_worker_stop(music_beeper->worker);
|
||||
if(p && strlen(p)) break; // Exit instead of going to browser if launched with arg
|
||||
music_beeper_clear(music_beeper);
|
||||
} while(1);
|
||||
|
||||
furi_string_free(file_path);
|
||||
music_beeper_free(music_beeper);
|
||||
|
||||
return 0;
|
||||
}
|
||||
507
applications/plugins/music_beeper/music_beeper_worker.c
Normal file
507
applications/plugins/music_beeper/music_beeper_worker.c
Normal file
@@ -0,0 +1,507 @@
|
||||
#include "music_beeper_worker.h"
|
||||
|
||||
#include <furi_hal.h>
|
||||
#include <furi.h>
|
||||
|
||||
#include <storage/storage.h>
|
||||
#include <lib/flipper_format/flipper_format.h>
|
||||
|
||||
#include <m-array.h>
|
||||
|
||||
#define TAG "MusicBeeperWorker"
|
||||
|
||||
#define MUSIC_BEEPER_FILETYPE "Flipper Music Format"
|
||||
#define MUSIC_BEEPER_VERSION 0
|
||||
|
||||
#define SEMITONE_PAUSE 0xFF
|
||||
|
||||
#define NOTE_C4 261.63f
|
||||
#define NOTE_C4_SEMITONE (4.0f * 12.0f)
|
||||
#define TWO_POW_TWELTH_ROOT 1.059463094359f
|
||||
|
||||
typedef struct {
|
||||
uint8_t semitone;
|
||||
uint8_t duration;
|
||||
uint8_t dots;
|
||||
} NoteBlock;
|
||||
|
||||
ARRAY_DEF(NoteBlockArray, NoteBlock, M_POD_OPLIST);
|
||||
|
||||
struct MusicBeeperWorker {
|
||||
FuriThread* thread;
|
||||
bool should_work;
|
||||
|
||||
MusicBeeperWorkerCallback callback;
|
||||
void* callback_context;
|
||||
|
||||
float volume;
|
||||
uint32_t bpm;
|
||||
uint32_t duration;
|
||||
uint32_t octave;
|
||||
NoteBlockArray_t notes;
|
||||
};
|
||||
|
||||
static int32_t music_beeper_worker_thread_callback(void* context) {
|
||||
furi_assert(context);
|
||||
MusicBeeperWorker* instance = context;
|
||||
|
||||
NoteBlockArray_it_t it;
|
||||
NoteBlockArray_it(it, instance->notes);
|
||||
if(furi_hal_speaker_acquire(1000)) {
|
||||
while(instance->should_work) {
|
||||
if(NoteBlockArray_end_p(it)) {
|
||||
NoteBlockArray_it(it, instance->notes);
|
||||
furi_delay_ms(10);
|
||||
} else {
|
||||
NoteBlock* note_block = NoteBlockArray_ref(it);
|
||||
|
||||
float note_from_a4 = (float)note_block->semitone - NOTE_C4_SEMITONE;
|
||||
float frequency = NOTE_C4 * powf(TWO_POW_TWELTH_ROOT, note_from_a4);
|
||||
float duration = 60.0 * furi_kernel_get_tick_frequency() * 4 / instance->bpm /
|
||||
note_block->duration;
|
||||
uint32_t dots = note_block->dots;
|
||||
while(dots > 0) {
|
||||
duration += duration / 2;
|
||||
dots--;
|
||||
}
|
||||
uint32_t next_tick = furi_get_tick() + duration;
|
||||
float volume = instance->volume;
|
||||
|
||||
if(instance->callback) {
|
||||
instance->callback(
|
||||
note_block->semitone,
|
||||
note_block->dots,
|
||||
note_block->duration,
|
||||
0.0,
|
||||
instance->callback_context);
|
||||
}
|
||||
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_start(frequency, volume);
|
||||
while(instance->should_work && furi_get_tick() < next_tick) {
|
||||
volume *= 0.9945679;
|
||||
furi_hal_speaker_set_volume(volume);
|
||||
furi_delay_ms(2);
|
||||
}
|
||||
NoteBlockArray_next(it);
|
||||
}
|
||||
}
|
||||
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Speaker system is busy with another process.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MusicBeeperWorker* music_beeper_worker_alloc() {
|
||||
MusicBeeperWorker* instance = malloc(sizeof(MusicBeeperWorker));
|
||||
|
||||
NoteBlockArray_init(instance->notes);
|
||||
|
||||
instance->thread = furi_thread_alloc_ex(
|
||||
"MusicBeeperWorker", 1024, music_beeper_worker_thread_callback, instance);
|
||||
|
||||
instance->volume = 1.0f;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void music_beeper_worker_clear(MusicBeeperWorker* instance) {
|
||||
NoteBlockArray_reset(instance->notes);
|
||||
}
|
||||
|
||||
void music_beeper_worker_free(MusicBeeperWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_thread_free(instance->thread);
|
||||
NoteBlockArray_clear(instance->notes);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static bool is_digit(const char c) {
|
||||
return isdigit(c) != 0;
|
||||
}
|
||||
|
||||
static bool is_letter(const char c) {
|
||||
return islower(c) != 0 || isupper(c) != 0;
|
||||
}
|
||||
|
||||
static bool is_space(const char c) {
|
||||
return c == ' ' || c == '\t';
|
||||
}
|
||||
|
||||
static size_t extract_number(const char* string, uint32_t* number) {
|
||||
size_t ret = 0;
|
||||
*number = 0;
|
||||
while(is_digit(*string)) {
|
||||
*number *= 10;
|
||||
*number += (*string - '0');
|
||||
string++;
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t extract_dots(const char* string, uint32_t* number) {
|
||||
size_t ret = 0;
|
||||
*number = 0;
|
||||
while(*string == '.') {
|
||||
*number += 1;
|
||||
string++;
|
||||
ret++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t extract_char(const char* string, char* symbol) {
|
||||
if(is_letter(*string)) {
|
||||
*symbol = *string;
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static size_t extract_sharp(const char* string, char* symbol) {
|
||||
if(*string == '#' || *string == '_') {
|
||||
*symbol = '#';
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static size_t skip_till(const char* string, const char symbol) {
|
||||
size_t ret = 0;
|
||||
while(*string != '\0' && *string != symbol) {
|
||||
string++;
|
||||
ret++;
|
||||
}
|
||||
if(*string != symbol) {
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool music_beeper_worker_add_note(
|
||||
MusicBeeperWorker* instance,
|
||||
uint8_t semitone,
|
||||
uint8_t duration,
|
||||
uint8_t dots) {
|
||||
NoteBlock note_block;
|
||||
|
||||
note_block.semitone = semitone;
|
||||
note_block.duration = duration;
|
||||
note_block.dots = dots;
|
||||
|
||||
NoteBlockArray_push_back(instance->notes, note_block);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int8_t note_to_semitone(const char note) {
|
||||
switch(note) {
|
||||
case 'C':
|
||||
return 0;
|
||||
// C#
|
||||
case 'D':
|
||||
return 2;
|
||||
// D#
|
||||
case 'E':
|
||||
return 4;
|
||||
case 'F':
|
||||
return 5;
|
||||
// F#
|
||||
case 'G':
|
||||
return 7;
|
||||
// G#
|
||||
case 'A':
|
||||
return 9;
|
||||
// A#
|
||||
case 'B':
|
||||
return 11;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static bool music_beeper_worker_parse_notes(MusicBeeperWorker* instance, const char* string) {
|
||||
const char* cursor = string;
|
||||
bool result = true;
|
||||
|
||||
while(*cursor != '\0') {
|
||||
if(!is_space(*cursor)) {
|
||||
uint32_t duration = 0;
|
||||
char note_char = '\0';
|
||||
char sharp_char = '\0';
|
||||
uint32_t octave = 0;
|
||||
uint32_t dots = 0;
|
||||
|
||||
// Parsing
|
||||
cursor += extract_number(cursor, &duration);
|
||||
cursor += extract_char(cursor, ¬e_char);
|
||||
cursor += extract_sharp(cursor, &sharp_char);
|
||||
cursor += extract_number(cursor, &octave);
|
||||
cursor += extract_dots(cursor, &dots);
|
||||
|
||||
// Post processing
|
||||
note_char = toupper(note_char);
|
||||
if(!duration) {
|
||||
duration = instance->duration;
|
||||
}
|
||||
if(!octave) {
|
||||
octave = instance->octave;
|
||||
}
|
||||
|
||||
// Validation
|
||||
bool is_valid = true;
|
||||
is_valid &= (duration >= 1 && duration <= 128);
|
||||
is_valid &= ((note_char >= 'A' && note_char <= 'G') || note_char == 'P');
|
||||
is_valid &= (sharp_char == '#' || sharp_char == '\0');
|
||||
is_valid &= (octave <= 16);
|
||||
is_valid &= (dots <= 16);
|
||||
if(!is_valid) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Invalid note: %lu%c%c%lu.%lu",
|
||||
duration,
|
||||
note_char == '\0' ? '_' : note_char,
|
||||
sharp_char == '\0' ? '_' : sharp_char,
|
||||
octave,
|
||||
dots);
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// Note to semitones
|
||||
uint8_t semitone = 0;
|
||||
if(note_char == 'P') {
|
||||
semitone = SEMITONE_PAUSE;
|
||||
} else {
|
||||
semitone += octave * 12;
|
||||
semitone += note_to_semitone(note_char);
|
||||
semitone += sharp_char == '#' ? 1 : 0;
|
||||
}
|
||||
|
||||
if(music_beeper_worker_add_note(instance, semitone, duration, dots)) {
|
||||
FURI_LOG_D(
|
||||
TAG,
|
||||
"Added note: %c%c%lu.%lu = %u %lu",
|
||||
note_char == '\0' ? '_' : note_char,
|
||||
sharp_char == '\0' ? '_' : sharp_char,
|
||||
octave,
|
||||
dots,
|
||||
semitone,
|
||||
duration);
|
||||
} else {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"Invalid note: %c%c%lu.%lu = %u %lu",
|
||||
note_char == '\0' ? '_' : note_char,
|
||||
sharp_char == '\0' ? '_' : sharp_char,
|
||||
octave,
|
||||
dots,
|
||||
semitone,
|
||||
duration);
|
||||
}
|
||||
cursor += skip_till(cursor, ',');
|
||||
}
|
||||
|
||||
if(*cursor != '\0') cursor++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool music_beeper_worker_load(MusicBeeperWorker* instance, const char* file_path) {
|
||||
furi_assert(instance);
|
||||
furi_assert(file_path);
|
||||
|
||||
bool ret = false;
|
||||
if(strcasestr(file_path, ".fmf")) {
|
||||
ret = music_beeper_worker_load_fmf_from_file(instance, file_path);
|
||||
} else {
|
||||
ret = music_beeper_worker_load_rtttl_from_file(instance, file_path);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool music_beeper_worker_load_fmf_from_file(MusicBeeperWorker* instance, const char* file_path) {
|
||||
furi_assert(instance);
|
||||
furi_assert(file_path);
|
||||
|
||||
bool result = false;
|
||||
FuriString* temp_str;
|
||||
temp_str = furi_string_alloc();
|
||||
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!flipper_format_file_open_existing(file, file_path)) break;
|
||||
|
||||
uint32_t version = 0;
|
||||
if(!flipper_format_read_header(file, temp_str, &version)) break;
|
||||
if(furi_string_cmp_str(temp_str, MUSIC_BEEPER_FILETYPE) ||
|
||||
(version != MUSIC_BEEPER_VERSION)) {
|
||||
FURI_LOG_E(TAG, "Incorrect file format or version");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_uint32(file, "BPM", &instance->bpm, 1)) {
|
||||
FURI_LOG_E(TAG, "BPM is missing");
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_read_uint32(file, "Duration", &instance->duration, 1)) {
|
||||
FURI_LOG_E(TAG, "Duration is missing");
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_read_uint32(file, "Octave", &instance->octave, 1)) {
|
||||
FURI_LOG_E(TAG, "Octave is missing");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_format_read_string(file, "Notes", temp_str)) {
|
||||
FURI_LOG_E(TAG, "Notes is missing");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!music_beeper_worker_parse_notes(instance, furi_string_get_cstr(temp_str))) {
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
flipper_format_free(file);
|
||||
furi_string_free(temp_str);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool music_beeper_worker_load_rtttl_from_file(MusicBeeperWorker* instance, const char* file_path) {
|
||||
furi_assert(instance);
|
||||
furi_assert(file_path);
|
||||
|
||||
bool result = false;
|
||||
FuriString* content;
|
||||
content = furi_string_alloc();
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file");
|
||||
break;
|
||||
};
|
||||
|
||||
uint16_t ret = 0;
|
||||
do {
|
||||
uint8_t buffer[65] = {0};
|
||||
ret = storage_file_read(file, buffer, sizeof(buffer) - 1);
|
||||
for(size_t i = 0; i < ret; i++) {
|
||||
furi_string_push_back(content, buffer[i]);
|
||||
}
|
||||
} while(ret > 0);
|
||||
|
||||
furi_string_trim(content);
|
||||
if(!furi_string_size(content)) {
|
||||
FURI_LOG_E(TAG, "Empty file");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!music_beeper_worker_load_rtttl_from_string(instance, furi_string_get_cstr(content))) {
|
||||
FURI_LOG_E(TAG, "Invalid file content");
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
} while(0);
|
||||
|
||||
storage_file_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
furi_string_free(content);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool music_beeper_worker_load_rtttl_from_string(MusicBeeperWorker* instance, const char* string) {
|
||||
furi_assert(instance);
|
||||
|
||||
const char* cursor = string;
|
||||
|
||||
// Skip name
|
||||
cursor += skip_till(cursor, ':');
|
||||
if(*cursor != ':') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Duration
|
||||
cursor += skip_till(cursor, '=');
|
||||
if(*cursor != '=') {
|
||||
return false;
|
||||
}
|
||||
cursor++;
|
||||
cursor += extract_number(cursor, &instance->duration);
|
||||
|
||||
// Octave
|
||||
cursor += skip_till(cursor, '=');
|
||||
if(*cursor != '=') {
|
||||
return false;
|
||||
}
|
||||
cursor++;
|
||||
cursor += extract_number(cursor, &instance->octave);
|
||||
|
||||
// BPM
|
||||
cursor += skip_till(cursor, '=');
|
||||
if(*cursor != '=') {
|
||||
return false;
|
||||
}
|
||||
cursor++;
|
||||
cursor += extract_number(cursor, &instance->bpm);
|
||||
|
||||
// Notes
|
||||
cursor += skip_till(cursor, ':');
|
||||
if(*cursor != ':') {
|
||||
return false;
|
||||
}
|
||||
cursor++;
|
||||
if(!music_beeper_worker_parse_notes(instance, cursor)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void music_beeper_worker_set_callback(
|
||||
MusicBeeperWorker* instance,
|
||||
MusicBeeperWorkerCallback callback,
|
||||
void* context) {
|
||||
furi_assert(instance);
|
||||
instance->callback = callback;
|
||||
instance->callback_context = context;
|
||||
}
|
||||
|
||||
void music_beeper_worker_set_volume(MusicBeeperWorker* instance, float volume) {
|
||||
furi_assert(instance);
|
||||
instance->volume = volume;
|
||||
}
|
||||
|
||||
void music_beeper_worker_start(MusicBeeperWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->should_work == false);
|
||||
|
||||
instance->should_work = true;
|
||||
furi_thread_start(instance->thread);
|
||||
}
|
||||
|
||||
void music_beeper_worker_stop(MusicBeeperWorker* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->should_work == true);
|
||||
|
||||
instance->should_work = false;
|
||||
furi_thread_join(instance->thread);
|
||||
}
|
||||
107
applications/plugins/musictracker/tracker_engine/speaker_hal.c
Normal file
107
applications/plugins/musictracker/tracker_engine/speaker_hal.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "speaker_hal.h"
|
||||
|
||||
#define FURI_HAL_SPEAKER_TIMER TIM16
|
||||
#define FURI_HAL_SPEAKER_CHANNEL LL_TIM_CHANNEL_CH1
|
||||
#define FURI_HAL_SPEAKER_PRESCALER 500
|
||||
|
||||
void tracker_speaker_play(float frequency, float pwm) {
|
||||
uint32_t autoreload = (SystemCoreClock / FURI_HAL_SPEAKER_PRESCALER / frequency) - 1;
|
||||
if(autoreload < 2) {
|
||||
autoreload = 2;
|
||||
} else if(autoreload > UINT16_MAX) {
|
||||
autoreload = UINT16_MAX;
|
||||
}
|
||||
|
||||
if(pwm < 0) pwm = 0;
|
||||
if(pwm > 1) pwm = 1;
|
||||
|
||||
uint32_t compare_value = pwm * autoreload;
|
||||
|
||||
if(compare_value == 0) {
|
||||
compare_value = 1;
|
||||
}
|
||||
|
||||
if(LL_TIM_OC_GetCompareCH1(FURI_HAL_SPEAKER_TIMER) != compare_value) {
|
||||
LL_TIM_OC_SetCompareCH1(FURI_HAL_SPEAKER_TIMER, compare_value);
|
||||
}
|
||||
|
||||
if(LL_TIM_GetAutoReload(FURI_HAL_SPEAKER_TIMER) != autoreload) {
|
||||
LL_TIM_SetAutoReload(FURI_HAL_SPEAKER_TIMER, autoreload);
|
||||
if(LL_TIM_GetCounter(FURI_HAL_SPEAKER_TIMER) > autoreload) {
|
||||
LL_TIM_SetCounter(FURI_HAL_SPEAKER_TIMER, 0);
|
||||
}
|
||||
}
|
||||
|
||||
LL_TIM_EnableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
||||
}
|
||||
|
||||
void tracker_speaker_stop() {
|
||||
LL_TIM_DisableAllOutputs(FURI_HAL_SPEAKER_TIMER);
|
||||
}
|
||||
|
||||
void tracker_speaker_init() {
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(200.0f, 0.01f);
|
||||
}
|
||||
tracker_speaker_stop();
|
||||
}
|
||||
|
||||
void tracker_speaker_deinit() {
|
||||
if(furi_hal_speaker_is_mine()) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
}
|
||||
|
||||
static FuriHalInterruptISR tracker_isr;
|
||||
static void* tracker_isr_context;
|
||||
static void tracker_interrupt_cb(void* context) {
|
||||
UNUSED(context);
|
||||
|
||||
if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) {
|
||||
LL_TIM_ClearFlag_UPDATE(TIM2);
|
||||
|
||||
if(tracker_isr) {
|
||||
tracker_isr(tracker_isr_context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tracker_interrupt_init(float freq, FuriHalInterruptISR isr, void* context) {
|
||||
tracker_isr = isr;
|
||||
tracker_isr_context = context;
|
||||
|
||||
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, tracker_interrupt_cb, NULL);
|
||||
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
// Prescaler to get 1kHz clock
|
||||
TIM_InitStruct.Prescaler = SystemCoreClock / 1000000 - 1;
|
||||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||||
// Auto reload to get freq Hz interrupt
|
||||
TIM_InitStruct.Autoreload = (1000000 / freq) - 1;
|
||||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||||
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
||||
LL_TIM_EnableIT_UPDATE(TIM2);
|
||||
LL_TIM_EnableAllOutputs(TIM2);
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
}
|
||||
|
||||
void tracker_interrupt_deinit() {
|
||||
FURI_CRITICAL_ENTER();
|
||||
LL_TIM_DeInit(TIM2);
|
||||
FURI_CRITICAL_EXIT();
|
||||
|
||||
furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, NULL, NULL);
|
||||
}
|
||||
|
||||
void tracker_debug_init() {
|
||||
furi_hal_gpio_init(&gpio_ext_pc3, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
|
||||
}
|
||||
|
||||
void tracker_debug_set(bool value) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, value);
|
||||
}
|
||||
|
||||
void tracker_debug_deinit() {
|
||||
furi_hal_gpio_init(&gpio_ext_pc3, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
||||
}
|
||||
134
applications/plugins/ocarina/ocarina.c
Normal file
134
applications/plugins/ocarina/ocarina.c
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NOTE_UP 587.33f
|
||||
#define NOTE_LEFT 493.88f
|
||||
#define NOTE_RIGHT 440.00f
|
||||
#define NOTE_DOWN 349.23
|
||||
#define NOTE_OK 293.66f
|
||||
|
||||
typedef struct {
|
||||
FuriMutex* model_mutex;
|
||||
|
||||
FuriMessageQueue* event_queue;
|
||||
|
||||
ViewPort* view_port;
|
||||
Gui* gui;
|
||||
} Ocarina;
|
||||
|
||||
void draw_callback(Canvas* canvas, void* ctx) {
|
||||
Ocarina* ocarina = ctx;
|
||||
furi_check(furi_mutex_acquire(ocarina->model_mutex, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
//canvas_draw_box(canvas, ocarina->model->x, ocarina->model->y, 4, 4);
|
||||
canvas_draw_frame(canvas, 0, 0, 128, 64);
|
||||
canvas_draw_str(canvas, 50, 10, "Ocarina");
|
||||
canvas_draw_str(canvas, 30, 20, "OK button for A");
|
||||
|
||||
furi_mutex_release(ocarina->model_mutex);
|
||||
}
|
||||
|
||||
void input_callback(InputEvent* input, void* ctx) {
|
||||
Ocarina* ocarina = ctx;
|
||||
// Puts input onto event queue with priority 0, and waits until completion.
|
||||
furi_message_queue_put(ocarina->event_queue, input, FuriWaitForever);
|
||||
}
|
||||
|
||||
Ocarina* ocarina_alloc() {
|
||||
Ocarina* instance = malloc(sizeof(Ocarina));
|
||||
|
||||
instance->model_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
|
||||
instance->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
|
||||
|
||||
instance->view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(instance->view_port, draw_callback, instance);
|
||||
view_port_input_callback_set(instance->view_port, input_callback, instance);
|
||||
|
||||
instance->gui = furi_record_open("gui");
|
||||
gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ocarina_free(Ocarina* instance) {
|
||||
view_port_enabled_set(instance->view_port, false); // Disabsles our ViewPort
|
||||
gui_remove_view_port(instance->gui, instance->view_port); // Removes our ViewPort from the Gui
|
||||
furi_record_close("gui"); // Closes the gui record
|
||||
view_port_free(instance->view_port); // Frees memory allocated by view_port_alloc
|
||||
furi_message_queue_free(instance->event_queue);
|
||||
|
||||
furi_mutex_free(instance->model_mutex);
|
||||
|
||||
if(furi_hal_speaker_is_mine()) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
|
||||
free(instance);
|
||||
}
|
||||
|
||||
int32_t ocarina_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
Ocarina* ocarina = ocarina_alloc();
|
||||
|
||||
InputEvent event;
|
||||
for(bool processing = true; processing;) {
|
||||
// Pops a message off the queue and stores it in `event`.
|
||||
// No message priority denoted by NULL, and 100 ticks of timeout.
|
||||
FuriStatus status = furi_message_queue_get(ocarina->event_queue, &event, 100);
|
||||
furi_check(furi_mutex_acquire(ocarina->model_mutex, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
float volume = 1.0f;
|
||||
if(status == FuriStatusOk) {
|
||||
if(event.type == InputTypePress) {
|
||||
switch(event.key) {
|
||||
case InputKeyUp:
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(NOTE_UP, volume);
|
||||
}
|
||||
break;
|
||||
case InputKeyDown:
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(NOTE_DOWN, volume);
|
||||
}
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(NOTE_LEFT, volume);
|
||||
}
|
||||
break;
|
||||
case InputKeyRight:
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(NOTE_RIGHT, volume);
|
||||
}
|
||||
break;
|
||||
case InputKeyOk:
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(NOTE_OK, volume);
|
||||
}
|
||||
break;
|
||||
case InputKeyBack:
|
||||
processing = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if(event.type == InputTypeRelease) {
|
||||
if(furi_hal_speaker_is_mine()) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
furi_mutex_release(ocarina->model_mutex);
|
||||
view_port_update(ocarina->view_port); // signals our draw callback
|
||||
}
|
||||
ocarina_free(ocarina);
|
||||
return 0;
|
||||
}
|
||||
142
applications/plugins/tama_p1/hal.c
Normal file
142
applications/plugins/tama_p1/hal.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stm32wbxx_ll_tim.h>
|
||||
#include "tama.h"
|
||||
|
||||
#define TAG_HAL "TamaLIB"
|
||||
|
||||
static void* tama_p1_hal_malloc(u32_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_free(void* ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_halt(void) {
|
||||
g_ctx->halted = true;
|
||||
}
|
||||
|
||||
static bool_t tama_p1_hal_is_log_enabled(log_level_t level) {
|
||||
switch(level) {
|
||||
case LOG_ERROR:
|
||||
return true;
|
||||
case LOG_INFO:
|
||||
return true;
|
||||
case LOG_MEMORY:
|
||||
return false;
|
||||
case LOG_CPU:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void tama_p1_hal_log(log_level_t level, char* buff, ...) {
|
||||
if(!tama_p1_hal_is_log_enabled(level)) return;
|
||||
|
||||
FuriString* string;
|
||||
va_list args;
|
||||
va_start(args, buff);
|
||||
furi_string_cat_vprintf(string, buff, args);
|
||||
va_end(args);
|
||||
|
||||
switch(level) {
|
||||
case LOG_ERROR:
|
||||
FURI_LOG_E(TAG_HAL, "%s", furi_string_get_cstr(string));
|
||||
break;
|
||||
case LOG_INFO:
|
||||
FURI_LOG_I(TAG_HAL, "%s", furi_string_get_cstr(string));
|
||||
break;
|
||||
case LOG_MEMORY:
|
||||
case LOG_CPU:
|
||||
default:
|
||||
FURI_LOG_D(TAG_HAL, "%s", furi_string_get_cstr(string));
|
||||
break;
|
||||
}
|
||||
|
||||
furi_string_free(string);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_sleep_until(timestamp_t ts) {
|
||||
while(true) {
|
||||
uint32_t count = LL_TIM_GetCounter(TIM2);
|
||||
uint32_t delay = ts - count;
|
||||
// FURI_LOG_D(TAG, "delay: %x", delay);
|
||||
// Stolen from furi_delay_until_tick
|
||||
if(delay != 0 && 0 == (delay >> (8 * sizeof(uint32_t) - 1))) {
|
||||
// Not the best place to release mutex, but this is the only place we know whether
|
||||
// we're ahead or behind, otherwise around the step call we'll always have to
|
||||
// delay a tick and run more and more behind.
|
||||
furi_mutex_release(g_state_mutex);
|
||||
furi_delay_tick(1);
|
||||
while(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk)
|
||||
furi_delay_tick(1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static timestamp_t tama_p1_hal_get_timestamp(void) {
|
||||
return LL_TIM_GetCounter(TIM2);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_update_screen(void) {
|
||||
// Do nothing, covered by main loop
|
||||
}
|
||||
|
||||
static void tama_p1_hal_set_lcd_matrix(u8_t x, u8_t y, bool_t val) {
|
||||
if(val)
|
||||
g_ctx->framebuffer[y] |= 1 << x;
|
||||
else
|
||||
g_ctx->framebuffer[y] &= ~(1 << x);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_set_lcd_icon(u8_t icon, bool_t val) {
|
||||
if(val)
|
||||
g_ctx->icons |= 1 << icon;
|
||||
else
|
||||
g_ctx->icons &= ~(1 << icon);
|
||||
}
|
||||
|
||||
static void tama_p1_hal_play_frequency(bool_t en) {
|
||||
if(en) {
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(g_ctx->frequency, 0.5f);
|
||||
}
|
||||
} else {
|
||||
if(furi_hal_speaker_is_mine()) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
}
|
||||
g_ctx->buzzer_on = en;
|
||||
}
|
||||
|
||||
static void tama_p1_hal_set_frequency(u32_t freq) {
|
||||
g_ctx->frequency = freq / 10.0F;
|
||||
if(g_ctx->buzzer_on) tama_p1_hal_play_frequency(true);
|
||||
}
|
||||
|
||||
static int tama_p1_hal_handler(void) {
|
||||
// Do nothing
|
||||
return 0;
|
||||
}
|
||||
|
||||
void tama_p1_hal_init(hal_t* hal) {
|
||||
hal->malloc = tama_p1_hal_malloc;
|
||||
hal->free = tama_p1_hal_free;
|
||||
hal->halt = tama_p1_hal_halt;
|
||||
hal->is_log_enabled = tama_p1_hal_is_log_enabled;
|
||||
hal->log = tama_p1_hal_log;
|
||||
hal->sleep_until = tama_p1_hal_sleep_until;
|
||||
hal->get_timestamp = tama_p1_hal_get_timestamp;
|
||||
hal->update_screen = tama_p1_hal_update_screen;
|
||||
hal->set_lcd_matrix = tama_p1_hal_set_lcd_matrix;
|
||||
hal->set_lcd_icon = tama_p1_hal_set_lcd_icon;
|
||||
hal->set_frequency = tama_p1_hal_set_frequency;
|
||||
hal->play_frequency = tama_p1_hal_play_frequency;
|
||||
hal->handler = tama_p1_hal_handler;
|
||||
}
|
||||
@@ -114,11 +114,17 @@ static void decrease_volume(TuningForkState* tuning_fork_state) {
|
||||
}
|
||||
|
||||
static void play(TuningForkState* tuning_fork_state) {
|
||||
furi_hal_speaker_start(current_tuning_note_freq(tuning_fork_state), tuning_fork_state->volume);
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(
|
||||
current_tuning_note_freq(tuning_fork_state), tuning_fork_state->volume);
|
||||
}
|
||||
}
|
||||
|
||||
static void stop() {
|
||||
furi_hal_speaker_stop();
|
||||
if(furi_hal_speaker_is_mine()) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
}
|
||||
|
||||
static void replay(TuningForkState* tuning_fork_state) {
|
||||
|
||||
84
applications/plugins/usb_midi/usb_midi.c
Normal file
84
applications/plugins/usb_midi/usb_midi.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include "usb/usb_midi_driver.h"
|
||||
#include "midi/parser.h"
|
||||
#include "midi/usb_message.h"
|
||||
#include <math.h>
|
||||
|
||||
float note_to_frequency(int note) {
|
||||
float a = 440;
|
||||
return (a / 32) * powf(2, ((note - 9) / 12.0));
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
MidiThreadEventStop = (1 << 0),
|
||||
MidiThreadEventRx = (1 << 1),
|
||||
MidiThreadEventAll = MidiThreadEventStop | MidiThreadEventRx,
|
||||
} MidiThreadEvent;
|
||||
|
||||
static void midi_rx_callback(void* context) {
|
||||
furi_assert(context);
|
||||
FuriThreadId thread_id = (FuriThreadId)context;
|
||||
furi_thread_flags_set(thread_id, MidiThreadEventRx);
|
||||
}
|
||||
|
||||
int32_t usb_midi_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
FuriHalUsbInterface* usb_config_prev;
|
||||
usb_config_prev = furi_hal_usb_get_config();
|
||||
midi_usb_set_context(furi_thread_get_id(furi_thread_get_current()));
|
||||
midi_usb_set_rx_callback(midi_rx_callback);
|
||||
furi_hal_usb_set_config(&midi_usb_interface, NULL);
|
||||
|
||||
MidiParser* parser = midi_parser_alloc();
|
||||
uint32_t events;
|
||||
uint8_t current_note = 255;
|
||||
|
||||
while(1) {
|
||||
events = furi_thread_flags_wait(MidiThreadEventAll, FuriFlagWaitAny, FuriWaitForever);
|
||||
|
||||
if(!(events & FuriFlagError)) {
|
||||
if(events & MidiThreadEventRx) {
|
||||
uint8_t buffer[64];
|
||||
size_t size = midi_usb_rx(buffer, sizeof(buffer));
|
||||
// loopback
|
||||
// midi_usb_tx(buffer, size);
|
||||
size_t start = 0;
|
||||
while(start < size) {
|
||||
CodeIndex code_index = code_index_from_data(buffer[start]);
|
||||
uint8_t data_size = usb_message_data_size(code_index);
|
||||
if(data_size == 0) break;
|
||||
|
||||
start += 1;
|
||||
for(size_t j = 0; j < data_size; j++) {
|
||||
if(midi_parser_parse(parser, buffer[start + j])) {
|
||||
MidiEvent* event = midi_parser_get_message(parser);
|
||||
if(event->type == NoteOn) {
|
||||
NoteOnEvent note_on = AsNoteOn(event);
|
||||
current_note = note_on.note;
|
||||
if(furi_hal_speaker_acquire(30)) {
|
||||
furi_hal_speaker_start(
|
||||
note_to_frequency(note_on.note),
|
||||
note_on.velocity / 127.0f);
|
||||
}
|
||||
} else if(event->type == NoteOff) {
|
||||
NoteOffEvent note_off = AsNoteOff(event);
|
||||
if(furi_hal_speaker_is_mine() && note_off.note == current_note) {
|
||||
furi_hal_speaker_stop();
|
||||
furi_hal_speaker_release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
start += data_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
midi_parser_free(parser);
|
||||
furi_hal_usb_set_config(usb_config_prev, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user