Skip to content

Commit

Permalink
Replace no need functions and usage text, and even if using ENCODER_M…
Browse files Browse the repository at this point in the history
…AP_ENABLE, play melody.
  • Loading branch information
ykeisuke committed Jun 24, 2024
1 parent 1134c38 commit c81492d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 57 deletions.
105 changes: 55 additions & 50 deletions keyboards/planck/rev7/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,61 @@ layer_state_t layer_state_set_user(layer_state_t state) {
return update_tri_layer_state(state, _LOWER, _RAISE, _ADJUST);
}

/* clang-format off */
float melody[8][2][2] = {
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
};
/* clang-format on */

#define JUST_MINOR_THIRD 1.2
#define JUST_MAJOR_THIRD 1.25
#define JUST_PERFECT_FOURTH 1.33333333
#define JUST_TRITONE 1.42222222
#define JUST_PERFECT_FIFTH 1.33333333

#define ET12_MINOR_SECOND 1.059463
#define ET12_MAJOR_SECOND 1.122462
#define ET12_MINOR_THIRD 1.189207
#define ET12_MAJOR_THIRD 1.259921
#define ET12_PERFECT_FOURTH 1.33484
#define ET12_TRITONE 1.414214
#define ET12_PERFECT_FIFTH 1.498307

deferred_token tokens[8];

uint32_t reset_note(uint32_t trigger_time, void *note) {
*(float*)note = 440.0f;
return 0;
}

bool play_melody(uint8_t index, bool clockwise) {
cancel_deferred_exec(tokens[index]);
if (clockwise) {
melody[index][1][0] = melody[index][1][0] * ET12_MINOR_SECOND;
melody[index][0][0] = melody[index][1][0] / ET12_PERFECT_FIFTH;
audio_play_melody(&melody[index], 2, false);
} else {
melody[index][1][0] = melody[index][1][0] / ET12_MINOR_SECOND;
melody[index][0][0] = melody[index][1][0] * ET12_TRITONE;
audio_play_melody(&melody[index], 2, false);
}
tokens[index] = defer_exec(1000, reset_note, &melody[index][1][0]);
return false;
}

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
#ifdef ENCODER_MAP_ENABLE
if (IS_ENCODEREVENT(record->event) && record->event.pressed) {
play_melody(record->event.key.col, record->event.type == ENCODER_CCW_EVENT);
}
#endif
switch (keycode) {
case QWERTY:
if (record->event.pressed) {
Expand Down Expand Up @@ -282,58 +336,9 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
return true;
}

/* clang-format off */
float melody[8][2][2] = {
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
{{440.0f, 8}, {440.0f, 24}},
};
/* clang-format on */

#define JUST_MINOR_THIRD 1.2
#define JUST_MAJOR_THIRD 1.25
#define JUST_PERFECT_FOURTH 1.33333333
#define JUST_TRITONE 1.42222222
#define JUST_PERFECT_FIFTH 1.33333333

#define ET12_MINOR_SECOND 1.059463
#define ET12_MAJOR_SECOND 1.122462
#define ET12_MINOR_THIRD 1.189207
#define ET12_MAJOR_THIRD 1.259921
#define ET12_PERFECT_FOURTH 1.33484
#define ET12_TRITONE 1.414214
#define ET12_PERFECT_FIFTH 1.498307

deferred_token tokens[8];

uint32_t reset_note(uint32_t trigger_time, void *note) {
*(float*)note = 440.0f;
return 0;
}

bool encoder_update_user(uint8_t index, bool clockwise) {
cancel_deferred_exec(tokens[index]);
if (clockwise) {
melody[index][1][0] = melody[index][1][0] * ET12_MINOR_SECOND;
melody[index][0][0] = melody[index][1][0] / ET12_PERFECT_FIFTH;
audio_play_melody(&melody[index], 2, false);
} else {
melody[index][1][0] = melody[index][1][0] / ET12_MINOR_SECOND;
melody[index][0][0] = melody[index][1][0] * ET12_TRITONE;
audio_play_melody(&melody[index], 2, false);
}
tokens[index] = defer_exec(1000, reset_note, &melody[index][1][0]);
return false;
}

void encoder_keymap_task(uint8_t index, bool clockwise) {
// Write your custom logic here
encoder_update_user(index, clockwise);
return play_melody(index, clockwise);
}

bool dip_switch_update_user(uint8_t index, bool active) {
Expand Down
6 changes: 0 additions & 6 deletions keyboards/planck/rev7/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}
uint8_t encoder_state[8] = {0};
int8_t encoder_pulses[8] = {0};


__attribute__ ((weak)) void encoder_keymap_task(uint8_t index, bool clockwise) {
}

static void encoder_handle_state_change(uint8_t index, uint8_t state) {
uint8_t i = index;

Expand All @@ -141,13 +137,11 @@ static void encoder_handle_state_change(uint8_t index, uint8_t state) {

if (encoder_pulses[i] >= resolution) {
encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
encoder_keymap_task(index, ENCODER_COUNTER_CLOCKWISE);
}

// direction is arbitrary here, but this clockwise
if (encoder_pulses[i] <= -resolution) {
encoder_queue_event(index, ENCODER_CLOCKWISE);
encoder_keymap_task(index, ENCODER_CLOCKWISE);
}
encoder_pulses[i] %= resolution;

Expand Down
2 changes: 1 addition & 1 deletion keyboards/planck/rev7/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Define it as follows in `rules.mk`:
ENCODER_MAP_ENABLE = yes
```

If you enable `ENCODER_MAP_ENABLE`, defined `const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS]` and configure your keycode. And, If you enable `ENCODER_MAP_ENABLE`, does not use `encoder_update_user` directly. However by default, `encoder_update_user` has use via `encoder_keymap_task(uint8_t index, bool clockwise)` for play tone sequence.
If you enable `ENCODER_MAP_ENABLE`, defined `const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS]` and configure your keycode. And, If you enable `ENCODER_MAP_ENABLE`, does not use `encoder_update_user` directly.

Absolutely, You can use the following config.h options:

Expand Down

0 comments on commit c81492d

Please sign in to comment.