aboutsummaryrefslogtreecommitdiff
path: root/keyboards/neson_design/n6/n6.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/neson_design/n6/n6.c')
-rw-r--r--keyboards/neson_design/n6/n6.c319
1 files changed, 319 insertions, 0 deletions
diff --git a/keyboards/neson_design/n6/n6.c b/keyboards/neson_design/n6/n6.c
new file mode 100644
index 000000000..800aa0ae9
--- /dev/null
+++ b/keyboards/neson_design/n6/n6.c
@@ -0,0 +1,319 @@
1/**
2 * @file n6.c
3 *
4 Copyright 2021 astro
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "n6.h"
21#include "i2c_master.h"
22#include "issi/is31fl3731.h"
23
24enum {
25 SELF_TESTING,
26 CAPS_ALERT,
27 NORMAL,
28};
29
30enum {
31 ST_STAGE_1,
32 ST_STAGE_2,
33 ST_STAGE_3,
34};
35
36// alert state update interval
37#define ALERT_INTERVAL 600
38// self testing state update interval
39#define ST_INTERVAL 100
40// self testing start index
41#define ST_DEFAULT_INDEX 15
42// self testing stage delay
43#define ST_STAGE_DELAY 10
44// self testing stage cycle count
45#define ST_STAGE_COUNT 4
46// self testing stage end duration
47#define ST_END_DURATION 10
48
49#ifdef RGBLIGHT_ENABLE
50extern rgblight_config_t rgblight_config;
51
52typedef struct {
53 uint8_t state;
54 uint8_t testing;
55 bool alert;
56 uint8_t index;
57 uint8_t delay;
58 uint8_t count;
59 bool dir;
60 uint8_t duration;
61 uint16_t ticks;
62} rgb_state_t;
63
64static rgb_state_t rgb_state = {
65 .state = //NORMAL,
66 SELF_TESTING,
67 .testing = ST_STAGE_1,
68 .ticks = 0,
69 .alert = false,
70 .index = ST_DEFAULT_INDEX,
71 .delay = ST_STAGE_DELAY,
72 .count = ST_STAGE_COUNT,
73 .dir = true,
74 .duration = ST_END_DURATION,
75};
76
77static void update_ticks(void)
78{
79 rgb_state.ticks = timer_read();
80}
81
82static void self_testing(void)
83{
84 if (timer_elapsed(rgb_state.ticks) < ST_INTERVAL) return;
85 HSV hsv;
86 hsv.h = rgblight_config.hue;
87 hsv.s = rgblight_config.sat;
88 hsv.v = rgblight_config.val;
89
90 RGB led = hsv_to_rgb(hsv);
91 switch(rgb_state.testing) {
92 case ST_STAGE_1:
93 if (rgb_state.index !=0 ) {
94 IS31FL3731_set_color_all(0, 0, 0);
95 }
96
97 if (rgb_state.index >= 1) {
98 for (int i = rgb_state.index-1; i < 32-rgb_state.index+1; i++) {
99 IS31FL3731_set_color(i, led.r, led.g, led.b);
100 }
101 if (rgb_state.index==1) {
102 rgb_state.index=0;
103 } else {
104 rgb_state.index -= 2;
105 }
106 } else{
107 if (rgb_state.delay > 0) {
108 rgb_state.delay--;
109 } else {
110 // move to stage 2
111 rgb_state.index = 2;
112 rgb_state.testing = ST_STAGE_2;
113 }
114 }
115 break;
116 case ST_STAGE_2: {
117 // clear all
118 IS31FL3731_set_color_all(0, 0, 0);
119 // light left and right
120 IS31FL3731_set_color(0, led.r, led.g, led.b);
121 IS31FL3731_set_color(1, led.r, led.g, led.b);
122 IS31FL3731_set_color(30, led.r, led.g, led.b);
123 IS31FL3731_set_color(31, led.r, led.g, led.b);
124 if (rgb_state.dir) {
125 // left to right
126 for (int i = rgb_state.index; i < rgb_state.index+4; i++) {
127 IS31FL3731_set_color(i, led.r, led.g, led.b);
128 }
129 rgb_state.index += 4;
130 if (rgb_state.index == 30) {
131 rgb_state.dir = !rgb_state.dir;
132 rgb_state.count--;
133 }
134 } else {
135 // right to left
136 for (int i = rgb_state.index-4; i < rgb_state.index; i++) {
137 IS31FL3731_set_color(i, led.r, led.g, led.b);
138 }
139 rgb_state.index -= 4;
140 if (rgb_state.index == 2) {
141 rgb_state.dir = !rgb_state.dir;
142 rgb_state.count--;
143 }
144 }
145
146 if (rgb_state.count == 0) {
147 // move to stage 3
148 rgb_state.testing = ST_STAGE_3;
149 rgb_state.index = 0;
150 rgb_state.delay = ST_STAGE_DELAY;
151 rgb_state.duration = ST_END_DURATION;
152 }
153 }
154 break;
155 case ST_STAGE_3:
156 if (rgb_state.index != 16) {
157 IS31FL3731_set_color_all(0, 0, 0);
158 }
159
160 // light left and right
161
162 if (rgb_state.index == 16) {
163 if (rgb_state.duration) {
164 rgb_state.duration--;
165 } else {
166 if (IS_HOST_LED_ON(USB_LED_CAPS_LOCK)) {
167 rgb_state.state = CAPS_ALERT;
168 } else {
169 rgb_state.state = NORMAL;
170 rgblight_set();
171 }
172 }
173 } else {
174 // left
175 for (int i = 0; i < rgb_state.index+1; i++) {
176 IS31FL3731_set_color(i, led.r, led.g, led.b);
177 }
178 // right
179 for (int i = 31; i > 31-rgb_state.index-1; i--) {
180 IS31FL3731_set_color(i, led.r, led.g, led.b);
181 }
182 rgb_state.index ++;
183 }
184 break;
185 }
186
187 update_ticks();
188}
189
190const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
191/* Refer to IS31 manual for these locations
192 * driver
193 * | R location
194 * | | G location
195 * | | | B location
196 * | | | | */
197// left CA
198 {0, C1_1, C3_2, C4_2},
199 {0, C1_2, C2_2, C4_3},
200 {0, C1_3, C2_3, C3_3},
201 {0, C1_4, C2_4, C3_4},
202 {0, C1_5, C2_5, C3_5},
203 {0, C1_6, C2_6, C3_6},
204 {0, C1_7, C2_7, C3_7},
205 {0, C1_8, C2_8, C3_8},
206
207 {0, C9_1, C8_1, C7_1},
208 {0, C9_2, C8_2, C7_2},
209 {0, C9_3, C8_3, C7_3},
210 {0, C9_4, C8_4, C7_4},
211 {0, C9_5, C8_5, C7_5},
212 {0, C9_6, C8_6, C7_6},
213 {0, C9_7, C8_7, C6_6},
214 {0, C9_8, C7_7, C6_7},
215// left CB
216 {0, C1_9, C3_10, C4_10},
217 {0, C1_10, C2_10, C4_11},
218 {0, C1_11, C2_11, C3_11},
219 {0, C1_12, C2_12, C3_12},
220 {0, C1_13, C2_13, C3_13},
221 {0, C1_14, C2_14, C3_14},
222 {0, C1_15, C2_15, C3_15},
223 {0, C1_16, C2_16, C3_16},
224
225 {0, C9_9, C8_9, C7_9},
226 {0, C9_10, C8_10, C7_10},
227 {0, C9_11, C8_11, C7_11},
228 {0, C9_12, C8_12, C7_12},
229 {0, C9_13, C8_13, C7_13},
230 {0, C9_14, C8_14, C7_14},
231 {0, C9_15, C8_15, C6_14},
232 {0, C9_16, C7_15, C6_15},
233};
234#endif
235__attribute__((weak))
236void matrix_init_user(void) {}
237
238void matrix_init_kb(void)
239{
240 // clear caps led
241 setPinOutput(CAPS_PIN);
242 writePinLow(CAPS_PIN);
243#ifdef RGBLIGHT_ENABLE
244 i2c_init();
245 IS31FL3731_init(DRIVER_ADDR_1);
246 for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
247 IS31FL3731_set_led_control_register(index, true, true, true);
248 }
249 IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
250 update_ticks();
251#endif
252 matrix_init_user();
253}
254
255#ifdef RGBLIGHT_ENABLE
256void housekeeping_task_kb(void)
257{
258 if (rgb_state.state == SELF_TESTING) {
259 self_testing();
260 } else if (rgb_state.state == CAPS_ALERT) {
261 //gold 0xFF, 0xD9, 0x00
262 LED_TYPE led = {
263 .r = 0xFF,
264 .g = 0xD9,
265 .b = 0x00,
266 };
267 if (rgb_state.alert) {
268 IS31FL3731_set_color_all(led.r, led.g, led.b);
269 ws2812_setleds(&led, 1);
270 } else {
271 led.r = 0;
272 led.g = 0;
273 led.b = 0;
274 IS31FL3731_set_color_all(0, 0, 0);
275 ws2812_setleds(&led, 1);
276 }
277
278 if (timer_elapsed(rgb_state.ticks) > ALERT_INTERVAL) {
279 rgb_state.alert = !rgb_state.alert;
280 update_ticks();
281 }
282 }
283
284 IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1,0);
285
286 housekeeping_task_user();
287}
288
289void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds)
290{
291 if (rgb_state.state != NORMAL) return;
292
293 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
294 IS31FL3731_set_color(i, start_led[i].r, start_led[i].g, start_led[i].b);
295 }
296 ws2812_setleds(start_led+DRIVER_LED_TOTAL, 1);
297}
298#endif
299
300bool led_update_kb(led_t led_state)
301{
302 bool res = led_update_user(led_state);
303 if (res) {
304 writePin(CAPS_PIN, led_state.caps_lock);
305
306#ifdef RGBLIGHT_ENABLE
307 if (rgb_state.state != SELF_TESTING) {
308 if (led_state.caps_lock) {
309 rgb_state.state = CAPS_ALERT;
310 update_ticks();
311 } else {
312 rgb_state.state = NORMAL;
313 rgblight_set();
314 }
315 }
316#endif
317 }
318 return res;
319} \ No newline at end of file