From f2683a4b707cd714b7f540ebf6482563df83d51e Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 8 Apr 2017 14:21:36 +0200 Subject: Near complete rewrite. --- main-sdl.c | 245 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 215 insertions(+), 30 deletions(-) (limited to 'main-sdl.c') diff --git a/main-sdl.c b/main-sdl.c index 687e5c7..6bc3970 100644 --- a/main-sdl.c +++ b/main-sdl.c @@ -10,22 +10,31 @@ #include #include #include +#include #include #include "display.h" #include "config.h" #include "engine.h" +#include "calib.h" /*** Global config and status values ***/ /* Window width and height */ -// const int g_width = 1024, g_height = 768; -const int g_width = 800, g_height = 600; +enum { + HARFE_WIDTH = 1024, + HARFE_HEIGHT = 768, + SCREEN_WIDTH = 800, + SCREEN_HEIGHT = 600 +}; int g_harfe_connected = 0; int g_harfe_fd = -1; +int g_importing_config = 0; +ConfigSource g_config_source = source_none; +int g_calibration_running = 0; static char * find_harfe() @@ -150,6 +159,13 @@ harfe_worker(void) free(portname); g_harfe_connected = 1; + /* Get remote config (if any) */ + config_reset(); + write(g_harfe_fd, "S2\n", 3); + usleep(50); + g_importing_config = 1; + write(g_harfe_fd, "I\n", 2); + while (running) { while (text_fill < sizeof(text) && !memchr(text, '\n', text_fill)) { ssize_t b = read(g_harfe_fd, text + text_fill, sizeof(text) - text_fill); @@ -178,15 +194,29 @@ harfe_worker(void) if (text_fill && lineend[-1] == '\r') lineend[-1] = 0; printf( "%s\n", text ); - int num_points = sscanf(text, "%04d:%04d %04d:%04d %04d:%04d %04d:%04d", &p[0].x, &p[0].y, &p[1].x, &p[1].y, &p[2].x, &p[2].y, &p[3].x, &p[3].y); - ptime = now(); - for (i = 0; i < num_points / 2; ++i) { + if (g_importing_config) { + if (!strcmp(text, "-- DONE")) { + g_importing_config = 0; + g_config_source = source_harfe; + } else + config_handle_line(text); + } else { + + int num_points = sscanf(text, "%04d:%04d %04d:%04d %04d:%04d %04d:%04d", &p[0].x, &p[0].y, &p[1].x, &p[1].y, &p[2].x, &p[2].y, &p[3].x, &p[3].y); + + ptime = now(); + for (i = 0; i < num_points / 2; ++i) { // printf("%04d:%04d\n", p[i].x, p[i].y); - engine_handle_point(p + i, ptime); - } - if (num_points > 1 || *text == '-') + + if (!g_calibration_running) + engine_handle_point(p + i, ptime); + else + calib_handle_point(p + i, ptime); + } + if (num_points > 1 || *text == '-') ++g_events; + } consumed = lineend - text + 1; memmove(text, lineend + 1, text_fill - consumed); @@ -226,6 +256,36 @@ config_parse(char *config_file) } fclose(fh); + g_config_source = source_file; +} + +static void +calib_fetch() { + int default_notes[] = { 60, 62, 64, 65, 67, 69, 71, 72, 74, 76, 77, 79, 81, 83 }; + int i, result_count; + LLine *result = calib_get_results(&result_count); + + config_reset(); + g_string_count = result_count; + + g_min_y = 0; g_max_y = 1024; + + for (i=0; ip0.y > g_min_y) + g_min_y = l->p0.y; + if (l->p1.y < g_max_y) + g_max_y = l->p1.y; + + g_string_conf[i].line = *l; + g_string_conf[i].mode = midi_three_octaves; + g_string_conf[i].channel = i; + g_string_conf[i].note = default_notes[i]; + g_string_conf[i].modifier = pitch_bend_up; + } + + g_config_source = source_edit; } int @@ -236,48 +296,153 @@ main(int argc, char **argv) pthread_t thread_id; uint32_t runtime; static int last_click_x, last_click_y, last_mouse_event; + static int g_up_pressed = 0, g_down_pressed = 0; - display_init(g_width, g_height); + display_init(SCREEN_WIDTH, SCREEN_HEIGHT, HARFE_WIDTH, HARFE_HEIGHT); engine_init(); - config_parse("config_midi"); +// config_parse("config_midi"); pthread_create(&thread_id, NULL, worker, NULL); +/* + E - Export config to Harfe (with implicite write to SD) + I - Import config from Harfe + W - Write config on Harfe to SD (no implicite Transfer) + + S - Save config to local file + L - Load config from local default (config_midi) + + Y - Invert String order + + Q - Reset local Config + C - Start calibration + M - Play MIDI C through Harfe +*/ + /* Spin and let call back do all the work */ while (1) { SDL_WaitEventTimeout(&ev, 10); switch (ev.type) { case SDL_QUIT: exit(0); - case SDL_KEYDOWN: + case SDL_KEYUP: + if (ev.key.keysym.scancode == SDL_SCANCODE_UP) g_up_pressed = 0; + if (ev.key.keysym.scancode == SDL_SCANCODE_DOWN) g_down_pressed = 0; + break; + case SDL_KEYDOWN: /* - if( ev.key.keysym.sym >= SDLK_1 && ev.key.keysym.sym <= SDLK_9 ) + if ( ev.key.keysym.sym >= SDLK_1 && ev.key.keysym.sym <= SDLK_9 ) engine_select_string( ev.key.keysym.sym - SDLK_1 ); - if( ev.key.keysym.sym == SDLK_BACKSPACE || ev.key.keysym.sym == SDLK_DELETE ) + if ( ev.key.keysym.sym == SDLK_BACKSPACE || ev.key.keysym.sym == SDLK_DELETE ) engine_delete_selected_string( ); - if( ev.key.keysym.sym == SDLK_d ) { + if ( ev.key.keysym.sym == SDLK_d ) { g_stringsdescending = 1 - g_stringsdescending; printf( "String order (left to right) is now %sscending.\n", g_stringsdescending ? "de" : "a" ); } */ - if( ev.key.keysym.scancode == SDL_SCANCODE_R) { - fprintf( stderr, "re-read config\n" ); - write(g_harfe_fd, "R\n", 2); + if ( ev.key.keysym.scancode == SDL_SCANCODE_0) + engine_select_config(sel_none); + if ( ev.key.keysym.scancode == SDL_SCANCODE_1) + engine_select_config(sel_min_y); + if ( ev.key.keysym.scancode == SDL_SCANCODE_2) + engine_select_config(sel_max_y); + if ( ev.key.keysym.scancode == SDL_SCANCODE_3) + engine_select_config(sel_2_oct); + if ( ev.key.keysym.scancode == SDL_SCANCODE_4) + engine_select_config(sel_3_oct_top); + if ( ev.key.keysym.scancode == SDL_SCANCODE_5) + engine_select_config(sel_3_oct_bottom); + + if ( ev.key.keysym.scancode == SDL_SCANCODE_UP && ! g_up_pressed++) + engine_change_selected(1); + if ( ev.key.keysym.scancode == SDL_SCANCODE_DOWN && ! g_down_pressed++) + engine_change_selected(-1); + + if ( ev.key.keysym.scancode == SDL_SCANCODE_S) { /* export locally */ + const char *homeDir = getenv("HOME"); + char savefile[512], date[32], confdump[512]; + time_t t = time(NULL); + struct tm *tmp = localtime(&t); + int fd; + size_t len; + + if (!homeDir) { + struct passwd* pwd = getpwuid(getuid()); + if (pwd) + homeDir = (const char*)pwd->pw_dir; + } + strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", tmp); + snprintf( savefile, sizeof(savefile), "%s/Laserharfe-%s.cfg", homeDir, date); + fd = open(savefile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + + len = config_dumpglobals( confdump, sizeof(confdump)); + write(fd, confdump, len ); + for (i=0; i 30) { + if (runtime - g_lastredraw > 30 && !g_importing_config) { g_lastredraw = runtime; - engine_redraw(); + + if (!g_calibration_running) + engine_redraw(); + else + calib_redraw(); + } if (runtime / 1000 - g_last_avg > 10) { printf("avg: %i\n", g_events / 10); -- cgit v1.2.3