]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/hadron/ver3/ver3.c
adding Hadron v3 keyboard, QWIIC devices support, haptic feedback support (#4462)
[qmk_firmware.git] / keyboards / hadron / ver3 / ver3.c
1 /* Copyright 2018 Jack Humbert <jack.humb@gmail.com>
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "ver3.h"
17 #include "qwiic.h"
18 #include "action_layer.h"
19 #include "matrix.h"
20 #include "DRV2605L.h"
21
22 #ifdef QWIIC_MICRO_OLED_ENABLE
23
24 /* screen off after this many milliseconds */
25 #include "timer.h"
26 #define ScreenOffInterval 60000 /* milliseconds */
27 static uint16_t last_flush;
28
29 volatile uint8_t led_numlock = false; 
30 volatile uint8_t  led_capslock = false; 
31 volatile uint8_t  led_scrolllock = false;
32
33 static uint8_t layer;
34 static bool queue_for_send = false; 
35 static uint8_t encoder_value = 32;
36
37 __attribute__ ((weak))
38 void draw_ui(void) {
39   clear_buffer();
40   last_flush = timer_read();
41   send_command(DISPLAYON);
42
43 /* Layer indicator is 41 x 10 pixels */
44 #define LAYER_INDICATOR_X 0
45 #define LAYER_INDICATOR_Y 0 
46
47   draw_string(LAYER_INDICATOR_X + 1, LAYER_INDICATOR_Y + 2, "LAYER", PIXEL_ON, NORM, 0);
48   draw_rect_filled_soft(LAYER_INDICATOR_X + 32, LAYER_INDICATOR_Y + 1, 9, 9, PIXEL_ON, NORM);
49   draw_char(LAYER_INDICATOR_X + 34, LAYER_INDICATOR_Y + 2, layer + 0x30, PIXEL_ON, XOR, 0);
50
51 /* Matrix display is 19 x 9 pixels */
52 #define MATRIX_DISPLAY_X 0
53 #define MATRIX_DISPLAY_Y 18
54
55   for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
56     for (uint8_t y = 0; y < MATRIX_COLS; y++) {
57       draw_pixel(MATRIX_DISPLAY_X + y + 2, MATRIX_DISPLAY_Y + x + 2,(matrix_get_row(x) & (1 << y)) > 0, NORM);
58     }
59   } 
60   draw_rect_soft(MATRIX_DISPLAY_X, MATRIX_DISPLAY_Y, 19, 9, PIXEL_ON, NORM);
61   /* hadron oled location on thumbnail */
62   draw_rect_filled_soft(MATRIX_DISPLAY_X + 14, MATRIX_DISPLAY_Y + 2, 3, 1, PIXEL_ON, NORM);
63 /*
64   draw_rect_soft(0, 13, 64, 6, PIXEL_ON, NORM);
65   draw_line_vert(encoder_value, 13, 6, PIXEL_ON, NORM);
66
67 */
68
69 /* Mod display is 41 x 16 pixels */
70 #define MOD_DISPLAY_X 30
71 #define MOD_DISPLAY_Y 18
72
73   uint8_t mods = get_mods();
74   if (mods & MOD_LSFT) {
75     draw_rect_filled_soft(MOD_DISPLAY_X + 0, MOD_DISPLAY_Y, 5 + (1 * 6), 11, PIXEL_ON, NORM);
76     draw_string(MOD_DISPLAY_X + 3, MOD_DISPLAY_Y + 2, "S", PIXEL_OFF, NORM, 0);
77   } else {
78     draw_string(MOD_DISPLAY_X + 3, MOD_DISPLAY_Y + 2, "S", PIXEL_ON, NORM, 0);
79   }
80   if (mods & MOD_LCTL) {
81     draw_rect_filled_soft(MOD_DISPLAY_X + 10, MOD_DISPLAY_Y, 5 + (1 * 6), 11, PIXEL_ON, NORM);
82     draw_string(MOD_DISPLAY_X + 13, MOD_DISPLAY_Y + 2, "C", PIXEL_OFF, NORM, 0);
83   } else {
84     draw_string(MOD_DISPLAY_X + 13, MOD_DISPLAY_Y + 2, "C", PIXEL_ON, NORM, 0);
85   }
86   if (mods & MOD_LALT) {
87     draw_rect_filled_soft(MOD_DISPLAY_X + 20, MOD_DISPLAY_Y, 5 + (1 * 6), 11, PIXEL_ON, NORM);
88     draw_string(MOD_DISPLAY_X + 23, MOD_DISPLAY_Y + 2, "A", PIXEL_OFF, NORM, 0);
89   } else {
90     draw_string(MOD_DISPLAY_X + 23, MOD_DISPLAY_Y + 2, "A", PIXEL_ON, NORM, 0);
91   }
92   if (mods & MOD_LGUI) {
93     draw_rect_filled_soft(MOD_DISPLAY_X + 30, MOD_DISPLAY_Y, 5 + (1 * 6), 11, PIXEL_ON, NORM);
94     draw_string(MOD_DISPLAY_X + 33, MOD_DISPLAY_Y + 2, "G", PIXEL_OFF, NORM, 0);
95   } else {
96     draw_string(MOD_DISPLAY_X + 33, MOD_DISPLAY_Y + 2, "G", PIXEL_ON, NORM, 0);
97   }
98
99 /* Lock display is 23 x 32 */
100 #define LOCK_DISPLAY_X 100
101 #define LOCK_DISPLAY_Y 0
102
103   if (led_numlock == true) {
104     draw_rect_filled_soft(LOCK_DISPLAY_X, LOCK_DISPLAY_Y, 5 + (3 * 6), 9, PIXEL_ON, NORM);
105     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 1, "NUM", PIXEL_OFF, NORM, 0);
106   } else if (led_numlock == false) {
107     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 1, "NUM", PIXEL_ON, NORM, 0);
108   }
109   if (led_capslock == true) {
110     draw_rect_filled_soft(LOCK_DISPLAY_X + 0, LOCK_DISPLAY_Y + 11, 5 + (3 * 6), 9, PIXEL_ON, NORM);
111     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 11 +1, "CAP", PIXEL_OFF, NORM, 0);
112   } else if (led_capslock == false) {
113     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 11 +1, "CAP", PIXEL_ON, NORM, 0);
114   }
115
116   if (led_scrolllock == true) {
117     draw_rect_filled_soft(LOCK_DISPLAY_X + 0, LOCK_DISPLAY_Y + 22, 5 + (3 * 6), 9, PIXEL_ON, NORM);
118     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 22 +1, "SCR", PIXEL_OFF, NORM, 0);
119   } else if (led_scrolllock == false) {
120     draw_string(LOCK_DISPLAY_X + 3, LOCK_DISPLAY_Y + 22 +1, "SCR", PIXEL_ON, NORM, 0);
121   }
122   send_buffer();
123 }
124
125 void read_host_led_state(void) {
126   uint8_t leds = host_keyboard_leds();
127   if (leds & (1 << USB_LED_NUM_LOCK))    {
128     if (led_numlock == false){
129     led_numlock = true;}
130     } else {
131     if (led_numlock == true){
132     led_numlock = false;}
133     }
134   if (leds & (1 << USB_LED_CAPS_LOCK))   {
135     if (led_capslock == false){
136     led_capslock = true;}
137     } else {
138     if (led_capslock == true){  
139     led_capslock = false;}
140     }
141   if (leds & (1 << USB_LED_SCROLL_LOCK)) {
142     if (led_scrolllock == false){
143     led_scrolllock = true;}
144     } else {
145     if (led_scrolllock == true){
146     led_scrolllock = false;}
147     }
148 }
149
150 uint32_t layer_state_set_kb(uint32_t state) {
151   state = layer_state_set_user(state);
152   layer = biton32(state);
153   queue_for_send = true;
154   return state;
155 }
156
157 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
158   queue_for_send = true;
159   return process_record_user(keycode, record);
160 }
161
162 void encoder_update_kb(uint8_t index, bool clockwise) {
163   encoder_value = (encoder_value + (clockwise ? 1 : -1)) % 64;
164   queue_for_send = true;
165 }
166
167 #endif
168
169 void matrix_init_kb(void) {
170 #ifdef DRV2605L
171   DRV_init();
172 #endif
173   queue_for_send = true;
174         matrix_init_user();
175 }
176             
177 void matrix_scan_kb(void) {
178
179 if (queue_for_send) {
180   #ifdef DRV2605L
181     DRV_EFFECT play_eff = strong_click; 
182     DRV_pulse(play_eff);
183   #endif
184 #ifdef QWIIC_MICRO_OLED_ENABLE
185    read_host_led_state();
186    draw_ui();
187 #endif
188    queue_for_send = false;
189   }
190 #ifdef QWIIC_MICRO_OLED_ENABLE
191   if (timer_elapsed(last_flush) > ScreenOffInterval) {
192   send_command(DISPLAYOFF);      /* 0xAE */
193   }
194 #endif
195         matrix_scan_user();
196 }