diff --git a/input.c b/input.c index b6c2f9a..6969b8f 100644 --- a/input.c +++ b/input.c @@ -27,6 +27,7 @@ enum keycodes { uint8_t keyjazz_enabled = 0; uint8_t keyjazz_base_octave = 2; +uint8_t keyjazz_velocity = 0x64; static uint8_t keycode = 0; // value of the pressed key static int num_joysticks = 0; @@ -91,7 +92,7 @@ void close_game_controllers() { } static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) { - input_msg_s key = {keyjazz, keyvalue}; + input_msg_s key = {keyjazz, keyvalue, keyjazz_velocity, event->type}; switch (event->key.keysym.scancode) { case SDL_SCANCODE_Z: key.value = keyjazz_base_octave * 12; @@ -184,14 +185,40 @@ static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue) { key.type = normal; if (event->type == SDL_KEYDOWN && keyjazz_base_octave > 0) { keyjazz_base_octave--; - display_keyjazz_overlay(1, keyjazz_base_octave); + display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity); } break; case SDL_SCANCODE_KP_MULTIPLY: key.type = normal; if (event->type == SDL_KEYDOWN && keyjazz_base_octave < 8) { keyjazz_base_octave++; - display_keyjazz_overlay(1, keyjazz_base_octave); + display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity); + } + break; + case SDL_SCANCODE_KP_MINUS: + key.type = normal; + if (event->type == SDL_KEYDOWN) { + if ((event->key.keysym.mod & KMOD_ALT) > 0) { + if (keyjazz_velocity > 1) + keyjazz_velocity -= 1; + } else { + if (keyjazz_velocity > 0x10) + keyjazz_velocity -= 0x10; + } + display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity); + } + break; + case SDL_SCANCODE_KP_PLUS: + key.type = normal; + if (event->type == SDL_KEYDOWN) { + if ((event->key.keysym.mod & KMOD_ALT) > 0) { + if (keyjazz_velocity < 0x7F) + keyjazz_velocity += 1; + } else { + if (keyjazz_velocity < 0x6F) + keyjazz_velocity += 0x10; + } + display_keyjazz_overlay(1, keyjazz_base_octave, keyjazz_velocity); } break; default: @@ -370,7 +397,7 @@ void handle_sdl_events(config_params_s *conf) { // ESC = toggle keyjazz if (event.key.keysym.sym == SDLK_ESCAPE) { - display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave); + display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity); } // Normal keyboard inputs diff --git a/input.h b/input.h index 8c5a6c7..25acd09 100644 --- a/input.h +++ b/input.h @@ -32,6 +32,8 @@ typedef enum special_messages_t { typedef struct input_msg_s { input_type_t type; uint8_t value; + uint8_t value2; + uint32_t eventType; } input_msg_s; void close_game_controllers(); diff --git a/main.c b/main.c index 54070aa..8c04507 100644 --- a/main.c +++ b/main.c @@ -72,6 +72,7 @@ int main(int argc, char *argv[]) { #endif uint8_t prev_input = 0; + uint8_t prev_note = 0; // main loop while (run) { @@ -87,14 +88,15 @@ int main(int argc, char *argv[]) { } break; case keyjazz: - if (input.value != prev_input) { - prev_input = input.value; - if (input.value != 0) { - send_msg_keyjazz(port, input.value, 0xFF); - } else { - send_msg_keyjazz(port, 0, 0); + if (input.value != 0) { + if (input.eventType == SDL_KEYDOWN && input.value != prev_input) { + send_msg_keyjazz(port, input.value, input.value2); + prev_note = input.value; + } else if (input.eventType == SDL_KEYUP && input.value == prev_note) { + send_msg_keyjazz(port, 0xFF, 0); } } + prev_input = input.value; break; case special: if (input.value != prev_input) { diff --git a/render.c b/render.c index e601500..16ed9d3 100644 --- a/render.c +++ b/render.c @@ -165,7 +165,7 @@ void draw_waveform(struct draw_oscilloscope_waveform_command *command) { } } -void display_keyjazz_overlay(uint8_t show, uint8_t base_octave) { +void display_keyjazz_overlay(uint8_t show, uint8_t base_octave, uint8_t velocity) { if (show) { struct draw_rectangle_command drc; @@ -181,19 +181,27 @@ void display_keyjazz_overlay(uint8_t show, uint8_t base_octave) { dcc.background = (struct color){background_color.r, background_color.g, background_color.b}; dcc.foreground = (struct color){200, 200, 200}; - dcc.c = base_octave + 48; - dcc.pos.x = 300; + dcc.pos.x = 296; dcc.pos.y = 226; draw_character(&dcc); + char buf[8]; + sprintf(buf, "%02X %u", velocity, base_octave); + + for (int i = 3; i >= 0; i--){ + dcc.c = buf[i]; + draw_character(&dcc); + dcc.pos.x -= 8; + } + } else { struct draw_rectangle_command drc; drc.color = (struct color){background_color.r, background_color.g, background_color.b}; - drc.pos.x = 300; + drc.pos.x = 272; drc.pos.y = 226; - drc.size.width = 20; + drc.size.width = 45; drc.size.height = 14; draw_rectangle(&drc); diff --git a/render.h b/render.h index a7a1265..5e3b0b2 100644 --- a/render.h +++ b/render.h @@ -16,6 +16,6 @@ int draw_character(struct draw_character_command *command); void render_screen(); void toggle_fullscreen(); -void display_keyjazz_overlay(uint8_t show, uint8_t base_octave); +void display_keyjazz_overlay(uint8_t show, uint8_t base_octave, uint8_t velocity); #endif \ No newline at end of file diff --git a/write.c b/write.c index cdd15de..82a02d2 100644 --- a/write.c +++ b/write.c @@ -71,8 +71,8 @@ int send_msg_controller(struct sp_port *port, uint8_t input) { } int send_msg_keyjazz(struct sp_port *port, uint8_t note, uint8_t velocity) { - if (velocity > 64) - velocity = 64; + if (velocity > 0x7F) + velocity = 0x7F; char buf[3] = {'K',note,velocity}; size_t nbytes = 3; int result;