~ruther/qmk_firmware

86169833c597fa5f2b5226e1587c48a6692ec017 — Jack Humbert 9 years ago 142ce47
updates to preonic default, music mode integrated
2 files changed, 165 insertions(+), 95 deletions(-)

M keyboard/preonic/keymaps/default/keymap.c
M quantum/audio.c
M keyboard/preonic/keymaps/default/keymap.c => keyboard/preonic/keymaps/default/keymap.c +110 -50
@@ 1,31 1,40 @@
#include "preonic.h"
#include "action_layer.h"
#include "eeconfig.h"
#ifdef AUDIO_ENABLE
  #include "audio.h"
  #define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
              0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
              0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
              0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
              0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
#endif

// Each layer gets a name for readability, which is then used in the keymap matrix below.
// The underscores don't mean anything - you can have a layer called STUFF or any other name.
// Layer names don't all need to be of the same length, obviously, and you can also skip them
// entirely and just use numbers.
#define _QW 0
#define _CM 1
#define _DV 2
#define _LW 3
#define _RS 4
#define _AD 5
#define _QWERTY 0
#define _COLEMAK 1
#define _DVORAK 2
#define _LOWER 3
#define _RAISE 4
#define _ADJUST 5
#define _MUSIC 6

// Macro name shortcuts
#define QWERTY M(_QW)
#define COLEMAK M(_CM)
#define DVORAK M(_DV)
#define LOWER M(_LW)
#define RAISE M(_RS)
#define QWERTY M(_QWERTY)
#define COLEMAK M(_COLEMAK)
#define DVORAK M(_DVORAK)
#define LOWER M(_LOWER)
#define RAISE M(_RAISE)
#define M_BL 5
#ifdef AUDIO_ENABLE
  #define AUD_OFF M(6)
  #define AUD_ON M(7)
#endif
#define MUS_OFF M(8)
#define MUS_ON M(9)

// Fillers to make layering more clear
#define _______ KC_TRNS


@@ 46,7 55,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * | Brite| Ctrl | Alt  | GUI  |Lower |    Space    |Raise | Left | Down |  Up  |Right |
 * `-----------------------------------------------------------------------------------'
 */
[_QW] = {
[_QWERTY] = {
  {KC_GRV,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_BSPC},
  {KC_TAB,  KC_Q,    KC_W,    KC_E,    KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,    KC_O,    KC_P,    KC_DEL},
  {KC_ESC,  KC_A,    KC_S,    KC_D,    KC_F,    KC_G,    KC_H,    KC_J,    KC_K,    KC_L,    KC_SCLN, KC_QUOT},


@@ 67,7 76,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * | Brite| Ctrl | Alt  | GUI  |Lower |    Space    |Raise | Left | Down |  Up  |Right |
 * `-----------------------------------------------------------------------------------'
 */
[_CM] = {
[_COLEMAK] = {
  {KC_GRV,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_BSPC},
  {KC_TAB,  KC_Q,    KC_W,    KC_F,    KC_P,    KC_G,    KC_J,    KC_L,    KC_U,    KC_Y,    KC_SCLN, KC_DEL},
  {KC_ESC,  KC_A,    KC_R,    KC_S,    KC_T,    KC_D,    KC_H,    KC_N,    KC_E,    KC_I,    KC_O,    KC_QUOT},


@@ 88,7 97,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * | Brite| Ctrl | Alt  | GUI  |Lower |    Space    |Raise | Left | Down |  Up  |Right |
 * `-----------------------------------------------------------------------------------'
 */
[_DV] = {
[_DVORAK] = {
  {KC_GRV,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_BSPC},
  {KC_TAB,  KC_QUOT, KC_COMM, KC_DOT,  KC_P,    KC_Y,    KC_F,    KC_G,    KC_C,    KC_R,    KC_L,    KC_DEL},
  {KC_ESC,  KC_A,    KC_O,    KC_E,    KC_U,    KC_I,    KC_D,    KC_H,    KC_T,    KC_N,    KC_S,    KC_SLSH},


@@ 109,7 118,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * |      |      |      |      |      |             |      | Next | Vol- | Vol+ | Play |
 * `-----------------------------------------------------------------------------------'
 */
[_LW] = {
[_LOWER] = {
  {KC_TILD, KC_EXLM, KC_AT,   KC_HASH, KC_DLR,  KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_BSPC},
  {KC_TILD, KC_EXLM, KC_AT,   KC_HASH, KC_DLR,  KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, KC_DEL},
  {KC_DEL,  KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5,   KC_F6,   KC_UNDS, KC_PLUS, KC_LCBR, KC_RCBR, KC_PIPE},


@@ 130,7 139,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * |      |      |      |      |      |             |      | Next | Vol- | Vol+ | Play |
 * `-----------------------------------------------------------------------------------'
 */
[_RS] = {
[_RAISE] = {
  {KC_GRV,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_BSPC},
  {KC_GRV,  KC_1,    KC_2,    KC_3,    KC_4,    KC_5,    KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    KC_DEL},
  {KC_DEL,  KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5,   KC_F6,   KC_MINS, KC_EQL,  KC_LBRC, KC_RBRC, KC_BSLS},


@@ 151,10 160,21 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
 * |      |      |      |      |      |             |      |      |      |      |      |
 * `-----------------------------------------------------------------------------------'
 */
[_AD] = {
[_ADJUST] = {
  {KC_F1,   KC_F2,   KC_F3,   KC_F4,   KC_F5,   KC_F6,   KC_F7,   KC_F8,   KC_F9,   KC_F10,  KC_F11,  KC_F12},
  {_______, RESET,   _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_DEL},
  {_______, _______, _______, AUD_ON,  AUD_OFF, _______, _______, QWERTY,  COLEMAK, DVORAK,  _______, _______},
  {_______, _______, _______, MUS_ON,  MUS_OFF, _______, _______, _______, _______, _______, _______, _______},
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}
},

/* Music (reserved for process_action_user)
 *
 */
[_MUSIC] = {
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______},
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______},
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______},
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______},
  {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}
}


@@ 166,24 186,20 @@ const uint16_t PROGMEM fn_actions[] = {

#ifdef AUDIO_ENABLE
float start_up[][2] = {
  {440.0*pow(2.0,(55)/12.0), 12},
  {0, 2},
  {440.0*pow(2.0,(67)/12.0), 8},
  {440.0*pow(2.0,(64)/12.0), 8},
  {440.0*pow(2.0,(55)/12.0), 8},
  {440.0*pow(2.0,(67)/12.0), 12},
  {0, 2},
  {440.0*pow(2.0,(67)/12.0), 12}
  {440.0*pow(2.0,(50)/12.0), 20},
  {440.0*pow(2.0,(62)/12.0), 8},
  {440.0*pow(2.0,(54)/12.0), 20},
  {440.0*pow(2.0,(62)/12.0), 8}
};

float tone_qw[][2] = {
float tone_qwerty[][2] = {
  {440.0*pow(2.0,(59)/12.0), 8},
  {440.0*pow(2.0,(60)/12.0), 8},
  {0, 4},
  {440.0*pow(2.0,(67)/12.0), 16}
};

float tone_cm[][2] = {
float tone_colemak[][2] = {
  {440.0*pow(2.0,(59)/12.0), 8},
  {440.0*pow(2.0,(60)/12.0), 8},
  {0, 4},


@@ 192,7 208,7 @@ float tone_cm[][2] = {
  {440.0*pow(2.0,(71)/12.0), 12}
};

float tone_dv[][2] = {
float tone_dvorak[][2] = {
  {440.0*pow(2.0,(59)/12.0), 8},
  {440.0*pow(2.0,(60)/12.0), 8},
  {0, 4},


@@ 202,53 218,66 @@ float tone_dv[][2] = {
  {0, 4},
  {440.0*pow(2.0,(67)/12.0), 8}
};

float tone_music[][2] = {
  {440.0*pow(2.0,(48)/12.0), 8},
  {440.0*pow(2.0,(50)/12.0), 8},
  {440.0*pow(2.0,(52)/12.0), 8},
  {440.0*pow(2.0,(53)/12.0), 8},
  {440.0*pow(2.0,(55)/12.0), 8},
  {440.0*pow(2.0,(57)/12.0), 8},
  {440.0*pow(2.0,(59)/12.0), 8},
  {440.0*pow(2.0,(60)/12.0), 8}
};
#endif


const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
{
  // MACRODOWN only works in this function
      switch(id) {
        case _QW:
        case _QWERTY:
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              play_notes(&tone_qw, 4, false);
              play_notes(&tone_qwerty, 4, false);
            #endif
            default_layer_set(1UL<<_QW);
            eeconfig_write_default_layer(1UL<<_QWERTY);
            default_layer_set(1UL<<_QWERTY);
          }
          break;
        case _CM:
        case _COLEMAK:
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              play_notes(&tone_cm, 6, false);
              play_notes(&tone_colemak, 6, false);
            #endif
            default_layer_set(1UL<<_CM);
            eeconfig_write_default_layer(1UL<<_COLEMAK);
            default_layer_set(1UL<<_COLEMAK);
          }
          break;
        case _DV:
        case _DVORAK:
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              play_notes(&tone_dv, 8, false);
              play_notes(&tone_dvorak, 8, false);
            #endif
            default_layer_set(1UL<<_DV);
            eeconfig_write_default_layer(1UL<<_DVORAK);
            default_layer_set(1UL<<_DVORAK);
          }
          break;
        case _LW:
        case _LOWER:
          if (record->event.pressed) {
            layer_on(_LW);
            update_tri_layer(_LW, _RS, _AD);
            layer_on(_LOWER);
            update_tri_layer(_LOWER, _RAISE, _ADJUST);
          } else {
            layer_off(_LW);
            update_tri_layer(_LW, _RS, _AD);
            layer_off(_LOWER);
            update_tri_layer(_LOWER, _RAISE, _ADJUST);
          }
          break;
        case _RS:
        case _RAISE:
          if (record->event.pressed) {
            layer_on(_RS);
            update_tri_layer(_LW, _RS, _AD);
            layer_on(_RAISE);
            update_tri_layer(_LOWER, _RAISE, _ADJUST);
          } else {
            layer_off(_RS);
            update_tri_layer(_LW, _RS, _AD);
            layer_off(_RAISE);
            update_tri_layer(_LOWER, _RAISE, _ADJUST);
          }
          break;
        case M_BL:


@@ 272,7 301,23 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              audio_on();
              play_notes(&start_up, 7, false);
              play_notes(&start_up, 4, false);
            #endif
          }
        break;
        case 8:
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              layer_off(_MUSIC);
              stop_all_notes();
            #endif
          }
        break;
        case 9:
          if (record->event.pressed) {
            #ifdef AUDIO_ENABLE
              play_notes(&tone_music, 8, false);
              layer_on(_MUSIC);
            #endif
          }
        break;


@@ 280,9 325,24 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
    return MACRO_NONE;
};

uint8_t starting_note = 0x0C;
int offset = 7;

void process_action_user(keyrecord_t *record) {

  if (IS_LAYER_ON(_MUSIC)) {
    if (record->event.pressed) {
        play_note(((double)261.626)*pow(2.0, -2.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)), 0xF);
    } else {
        stop_note(((double)261.626)*pow(2.0, -2.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row)));
    }
  }

}

void matrix_init_user(void) {
  #ifdef AUDIO_ENABLE
    init_notes();
    play_notes(&start_up, 7, false);
    play_notes(&start_up, 4, false);
  #endif
}

M quantum/audio.c => quantum/audio.c +55 -45
@@ 32,6 32,8 @@ int voice_place = 0;
double frequency = 0;
int volume = 0;
long position = 0;
int duty_place = 1;
int duty_counter = 0;

double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};


@@ 98,53 100,55 @@ void stop_all_notes() {
}

void stop_note(double freq) {
    #ifdef PWM_AUDIO
        freq = freq / SAMPLE_RATE;
    #endif
    for (int i = 7; i >= 0; i--) {
        if (frequencies[i] == freq) {
            frequencies[i] = 0;
            volumes[i] = 0;
            for (int j = i; (j < 7); j++) {
                frequencies[j] = frequencies[j+1];
                frequencies[j+1] = 0;
                volumes[j] = volumes[j+1];
                volumes[j+1] = 0;
            }
        }
    }
    voices--;
    if (voices < 0)
        voices = 0;
    if (voices == 0) {
    if (note) {
        #ifdef PWM_AUDIO
            TIMSK3 &= ~_BV(OCIE3A);
        #else
            TIMSK3 &= ~_BV(OCIE3A);
            TCCR3A &= ~_BV(COM3A1);
            freq = freq / SAMPLE_RATE;
        #endif
        frequency = 0;
        volume = 0;
        note = false;
    } else {
        double freq = frequencies[voices - 1];
        int vol = volumes[voices - 1];
        double starting_f = frequency;
        if (frequency < freq) {
            sliding = true;
            for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
                frequency = f;
        for (int i = 7; i >= 0; i--) {
            if (frequencies[i] == freq) {
                frequencies[i] = 0;
                volumes[i] = 0;
                for (int j = i; (j < 7); j++) {
                    frequencies[j] = frequencies[j+1];
                    frequencies[j+1] = 0;
                    volumes[j] = volumes[j+1];
                    volumes[j+1] = 0;
                }
            }
            sliding = false;
        } else if (frequency > freq) {
            sliding = true;
            for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
                frequency = f;
        }
        voices--;
        if (voices < 0)
            voices = 0;
        if (voices == 0) {
            #ifdef PWM_AUDIO
                TIMSK3 &= ~_BV(OCIE3A);
            #else
                TIMSK3 &= ~_BV(OCIE3A);
                TCCR3A &= ~_BV(COM3A1);
            #endif
            frequency = 0;
            volume = 0;
            note = false;
        } else {
            double freq = frequencies[voices - 1];
            int vol = volumes[voices - 1];
            double starting_f = frequency;
            if (frequency < freq) {
                sliding = true;
                for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
                    frequency = f;
                }
                sliding = false;
            } else if (frequency > freq) {
                sliding = true;
                for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
                    frequency = f;
                }
                sliding = false;
            }
            sliding = false;
            frequency = freq;
            volume = vol;
        }
        frequency = freq;
        volume = vol;
    }
}



@@ 239,13 243,19 @@ ISR(TIMER3_COMPA_vect) {
            if (frequency > 0) {
                // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
                // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
                if (place > 10) {
                voice_place %= voices;
                if (place > (frequencies[voice_place] / 500)) {
                    voice_place = (voice_place + 1) % voices;
                    place = 0.0;
                }
                ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period
                OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1; // Set compare to half the period
                OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1 * duty_place; // Set compare to half the period
                place++;
                // if (duty_counter > (frequencies[voice_place] / 500)) {
                //     duty_place = (duty_place % 3) + 1;
                //     duty_counter = 0;
                // }
                // duty_counter++;
            }
        #endif
    }


@@ 375,7 385,7 @@ if (audio_config.enable) {

void play_note(double freq, int vol) {

if (audio_config.enable) {
if (audio_config.enable && voices < 8) {

    if (notes)
        stop_all_notes();