Isokheir/oled.c

246 lines
9.9 KiB
C

#pragma once
#include <stdio.h>
#include <transactions.h>
char wpm_str[10];
uint32_t anim_timer = 0;
uint8_t current_idle_frame = 0;
uint8_t current_tap_frame = 0;
char is_slave_connected = 0;
oled_rotation_t oled_init_user(oled_rotation_t rotation) {
return OLED_ROTATION_270;
}
#define L_BASE 0
#define L_LOWER 2
#define L_RAISE 4
#define L_ADJUST 8
#define IDLE_FRAMES 5
#define IDLE_SPEED 20
#define TAP_SPEED_SLOW 40
#define TAP_SPEED_HALF 60
#define TAP_SPEED_FAST 80
#define TAP_FRAMES 2
#define ANIM_SIZE 256
#define ANIM_FRAME_DURATION 250
#define ANIM_TAP_SLOW 400
#define ANIM_TAP_HALF 300
#define ANIM_TAP_FAST 200
typedef struct _master_to_slave_t {
int m2s_data;
} master_to_slave_t;
typedef struct _slave_to_master_t {
int s2m_data;
} slave_to_master_t;
void oled_render_layer_state(void) {
oled_write_P(PSTR("Layer: "), false);
switch (layer_state) {
case L_BASE:
oled_write_ln_P(PSTR("Default"), false);
break;
case L_LOWER:
oled_write_ln_P(PSTR("Lower"), false);
break;
case L_RAISE:
oled_write_ln_P(PSTR("Raise"), false);
break;
case L_ADJUST:
case L_ADJUST|L_LOWER:
case L_ADJUST|L_RAISE:
case L_ADJUST|L_LOWER|L_RAISE:
oled_write_ln_P(PSTR("Adjust"), false);
break;
}
}
char keylog_str[24] = {};
const char code_to_name[60] = {
' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'R', 'E', 'B', 'T', '_', '-', '=', '[', ']', '\\',
'#', ';', '\'', '`', ',', '.', '/', ' ', ' ', ' '};
void set_keylog(uint16_t keycode, keyrecord_t *record) {
char name = ' ';
if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) ||
(keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) { keycode = keycode & 0xFF; }
if (keycode < 60) {
name = code_to_name[keycode];
}
// update keylog
snprintf(keylog_str, sizeof(keylog_str), "%dx%d, k%2d : %c",
record->event.key.row, record->event.key.col,
keycode, name);
}
void oled_render_keylog(void) {
oled_write(keylog_str, false);
}
void oled_render_wpm(void) {
sprintf(wpm_str, "%3d", get_current_wpm());
oled_write(wpm_str, false);
}
void oled_render_logo(void) {
static const char PROGMEM crkbd_logo[] = {
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4,
0};
oled_write_P(crkbd_logo, false);
}
static void oled_render_bongo(void) {
static const char PROGMEM bongo_idle[IDLE_FRAMES][ANIM_SIZE] = {
{0, 0,128,128,192,192,192,192,224,240,248,248,248,248,252,252,252,252,254,254,254,252,240,224,192,128, 0, 0, 0, 0, 0, 0, 0,128,225,247,255,255,255,255,223,223,223,243,243,255,127,119,111,111, 55, 51, 60, 60, 63, 27, 59, 59,127,126,124,120, 56, 0},
{0, 0,128,128,128,128,192,192,192,224,224,240,240,240,240,248,248,252,254,254,254,240,224,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0,129,231,255,255,255,255,255,191,191,167,231,255,127,111, 95, 95, 47, 39, 57, 57, 63, 23, 55, 55,126,124,120,120, 48, 0},
{0, 0, 0, 0, 0, 0,128,128,128,192,192,192,224,224,240,240,240,248,252,252,254,240,224,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0,129,231,255,255,255,255,255,191,191,167,231,255,255,239,223,223,239,103,121,121, 63, 23, 55, 55,126,124,120,120, 48, 0},
{0, 0, 0,128,128,128,192,192,192,192,224,224,224,240,240,240,248,252,252,254,254,240,240,224,192,128, 0, 0, 0, 0, 0, 0, 0, 0,129,231,255,255,255,255,255,191,191,179,243,255,239,223,223,239,239,119,124,124, 63, 23, 55, 55,127,126,120,120, 48, 0},
{0, 0, 0,128,128,192,192,192,224,224,224,240,240,248,248,248,248,252,254,254,254,240,240,224,192,128, 0, 0, 0, 0, 0, 0, 0,128,193,231,255,255,255,255,255,223,223,211,243,255,255,247,239,111,119,115,124, 60, 63, 27, 59, 59,127,126,124,120, 48, 0}
};
static const char PROGMEM bongo_prep[ANIM_SIZE] = {
0, 0,128,128,192,192,192,192,224,240,248,248,248,248,252,252,252,252,254,254,254,252,240,224,192, 0,128,192, 64,192,128, 0, 0,128,225,247,223,239,183,215,183,239,159,243,243,255,127,119,111,111, 55, 51, 60, 60, 31, 27, 27, 27, 15, 14, 15, 6, 7, 0
};
static const char PROGMEM bongo_tap[TAP_FRAMES][ANIM_SIZE] = {
{0, 0,128,128,192,192,192,192,224,240,248,248,248,248,252,252,252,252,254,254,254,252,240,224,192,128, 0, 0, 0, 0, 0, 0, 0,128,225,247,223,239,183,215,183,239,159,243,243,255,127,119,111,111, 55, 51, 60, 60, 63, 27, 59, 59,127,254,252,248,112, 0},
{0, 0,128,128,192,192,192,192,224,240,248,248,248,248,252,252,252,252,254,254,254,252,240,224,192, 0,128,192, 64,192,128, 0, 0,128,225,247,255,255,255,255,255,255,255,243,243,255,127,119,111,111, 55, 51, 60, 60, 31, 27, 27, 27, 15, 14, 15, 6, 7, 0}
};
void animation_idle_phase(void) {
if(get_current_wpm() <= IDLE_SPEED) {
current_idle_frame = (current_idle_frame + 1) % IDLE_FRAMES;
oled_write_raw_P(bongo_idle[abs((IDLE_FRAMES - 1) - current_idle_frame)], ANIM_SIZE);
return;
}
if(get_current_wpm() > IDLE_SPEED && get_current_wpm() < TAP_SPEED_SLOW) {
oled_write_raw_P(bongo_prep, ANIM_SIZE);
return;
}
}
void animation_tap_phase(void) {
if(get_current_wpm() > TAP_SPEED_FAST && timer_elapsed32(anim_timer) > ANIM_TAP_FAST) {
anim_timer = timer_read32();
current_tap_frame = (current_tap_frame + 1) % TAP_FRAMES;
oled_write_raw_P(bongo_tap[abs((TAP_FRAMES - 1) - current_tap_frame)], ANIM_SIZE);
return;
}
if(get_current_wpm() > TAP_SPEED_HALF && timer_elapsed32(anim_timer) > ANIM_TAP_HALF) {
anim_timer = timer_read32();
current_tap_frame = (current_tap_frame + 1) % TAP_FRAMES;
oled_write_raw_P(bongo_tap[abs((TAP_FRAMES - 1) - current_tap_frame)], ANIM_SIZE);
return;
}
if(get_current_wpm() > TAP_SPEED_SLOW && timer_elapsed32(anim_timer) > ANIM_TAP_SLOW) {
anim_timer = timer_read32();
current_tap_frame = (current_tap_frame + 1) % TAP_FRAMES;
oled_write_raw_P(bongo_tap[abs((TAP_FRAMES - 1) - current_tap_frame)], ANIM_SIZE);
return;
}
}
if(get_current_wpm() > TAP_SPEED_SLOW) {
animation_tap_phase();
} else {
if(timer_elapsed32(anim_timer) > ANIM_FRAME_DURATION) {
anim_timer = timer_read32();
animation_idle_phase();
}
}
}
static void oled_render_split_status(void) {
static const char PROGMEM kb_status[2][64] = {
{0,192, 64, 64,192, 64, 64,192, 96,112,200,136,132,132, 4, 4, 4,196, 68, 68,232, 48, 48,224, 32, 32,224, 32, 32,224, 0, 0, 0, 31, 18, 18, 31, 18, 18, 31, 18, 18, 63, 36, 36, 63, 0, 0, 0, 31, 18, 18, 31, 9, 9, 15, 9, 9, 15, 9, 9, 15, 0, 0},
{0,192, 64, 64,192, 64, 64,192, 96,112,200,136,132,132, 0, 4, 4, 0, 4, 64,136, 0, 0, 0,128, 64, 0, 0, 0, 0, 0, 0, 0, 31, 18, 18, 31, 18, 18, 31, 18, 18, 63, 36, 36, 63, 0, 0, 0, 0, 0, 16, 8, 5, 2, 5, 8, 16, 0, 0, 0, 0, 0, 0}
};
if(is_slave_connected) {
oled_write_raw_P(kb_status[0], sizeof(kb_status[0]));
} else {
oled_write_raw_P(kb_status[1], sizeof(kb_status[1]));
}
}
void oled_render_layer_status(void) {
oled_write_ln_P(PSTR("A"), true);
//switch (layer_state) {
// case L_BASE:
// oled_write_ln_P(PSTR("Default"), false);
// break;
// case L_LOWER:
// oled_write_ln_P(PSTR("Lower"), false);
// break;
// case L_RAISE:
// oled_write_ln_P(PSTR("Raise"), false);
// break;
// case L_ADJUST:
// case L_ADJUST|L_LOWER:
// case L_ADJUST|L_RAISE:
// case L_ADJUST|L_LOWER|L_RAISE:
// oled_write_ln_P(PSTR("Adjust"), false);
// break;
//}
}
static void oled_render_dashboard(void) {
oled_render_split_status();
//oled_render_layer_status();
}
bool oled_task_user(void) {
if (is_keyboard_master()) {
//oled_render_layer_state();
//oled_render_keylog();
oled_render_dashboard();
} else {
oled_render_bongo();
oled_set_cursor(0, 4);
oled_render_wpm();
}
return false;
}
void user_sync_a_slave_handler(uint8_t in_buflen, const void* in_data, uint8_t out_buflen, void* out_data) {
const master_to_slave_t *m2s = (const master_to_slave_t*)in_data;
slave_to_master_t *s2m = (slave_to_master_t*)out_data;
s2m->s2m_data = m2s->m2s_data + 5; // whatever comes in, add 5 so it can be sent back
}
void keyboard_post_init_user(void) {
transaction_register_rpc(USER_SYNC_A, user_sync_a_slave_handler);
}
void housekeeping_task_user(void) {
if (is_keyboard_master()) {
// Interact with slave every 500ms
static uint32_t last_sync = 0;
if (timer_elapsed32(last_sync) > 500) {
master_to_slave_t m2s = {6};
slave_to_master_t s2m = {0};
if(transaction_rpc_exec(USER_SYNC_A, sizeof(m2s), &m2s, sizeof(s2m), &s2m)) {
last_sync = timer_read32();
is_slave_connected = 1;
} else {
is_slave_connected = 0;
}
}
}
}