summaryrefslogtreecommitdiff
path: root/keyboards/nullbitsco/snap/keymaps/bongo_reactive_single_oled/bongo.h
blob: 0cf1de3045bce1ea3f1d911c0b06a28460d9cf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Copyright 2021 Chris Tanaka <https://github.com/christanaka>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "bongo_graphics.h"
#include QMK_KEYBOARD_H

#define _IDLE_FRAMES         5
#define _PREP_FRAMES         1
#define _TAP_FRAMES          2
#define _ANIM_BYTES          512   // Number of bytes in array (max is 1024)
#define _IDLE_FRAME_DURATION 175
#define _TAP_FRAME_DURATION  75
#define _PREP_TIMEOUT        750

enum anim_states
{
    Idle,
    Prep,
    Tap
};

static uint8_t anim_state = Idle;
static uint8_t anim_duration = _IDLE_FRAME_DURATION;
static uint32_t anim_timer = 0;

static uint8_t idle_frame = 0;
static uint8_t tap_frame = 0;

static uint32_t prep_timer = 0;

// Decompress and write a precompressed bitmap frame to the OLED.
// Documentation and python compression script available at:
// https://github.com/nullbitsco/squeez-o
#ifdef USE_OLED_BITMAP_COMPRESSION
static void oled_write_compressed_P(const char* input_block_map, const char* input_block_list) {
    uint16_t block_index = 0;
    for (uint16_t i = 0; i < NUM_OLED_BYTES; i++) {
        uint8_t bit          = i % 8;
        uint8_t map_index    = i / 8;
        uint8_t _block_map   = (uint8_t)pgm_read_byte_near(input_block_map + map_index);
        uint8_t nonzero_byte = (_block_map & (1 << bit));
        if (nonzero_byte) {
            const char data = (const char)pgm_read_byte_near(input_block_list + block_index++);
            oled_write_raw_byte(data, i);
        } else {
            const char data = (const char)0x00;
            oled_write_raw_byte(data, i);
        }
    }
}
#endif

static void animate(uint8_t x, uint8_t y) {
  oled_set_cursor(x, y);

  // Update frame
  switch (anim_state) {
    case Idle:
      idle_frame = (idle_frame + 1) % _IDLE_FRAMES;
      #ifdef USE_OLED_BITMAP_COMPRESSION
          oled_write_compressed_P(idle_block_map[abs(1 - idle_frame)], idle[abs(1 - idle_frame)]);
      #else
          oled_write_raw_P(idle[abs(1 - idle_frame)], sizeof(idle[abs(1 - idle_frame)]));
      #endif
      break;
    case Prep:
      #ifdef USE_OLED_BITMAP_COMPRESSION
          oled_write_compressed_P(prep_block_map[0], prep[0]);
      #else
          oled_write_raw_P(prep[0], sizeof(prep[0]));
      #endif
      break;
    case Tap:
      tap_frame = (tap_frame + 1) % _TAP_FRAMES;
      #ifdef USE_OLED_BITMAP_COMPRESSION
          oled_write_compressed_P(tap_block_map[abs(1 - tap_frame)], tap[abs(1 - tap_frame)]);
      #else
          oled_write_raw_P(tap[abs(1 - tap_frame)], sizeof(tap[abs(1 - tap_frame)]));
      #endif
      break;
    default:
      break;
  }
}

void bongo_render(uint8_t x, uint8_t y) {
  if (timer_elapsed32(anim_timer) > anim_duration) {
    anim_timer = timer_read32();
    animate(x, y);
  }

  if (anim_state == Prep && timer_elapsed32(prep_timer) > _PREP_TIMEOUT) {
    anim_state = Idle;
    anim_duration = _IDLE_FRAME_DURATION;
  }
}

void bongo_process_record(keyrecord_t *record) {
  if (record->event.pressed) {
    anim_state = Tap;
    anim_duration = _TAP_FRAME_DURATION;
  } else {
    if (anim_state == Tap) {
      anim_state = Prep;
      prep_timer = timer_read32();
    }
  }
}