1+ // Copyright 2018-2024 QMK (@qmk)
2+ // SPDX-License-Identifier: GPL-2.0-or-later
3+
4+ #include QMK_KEYBOARD_H
5+ #include "quantum.h"
6+ #include "eeprom.h"
7+
8+ const uint16_t PROGMEM keymaps [][MATRIX_ROWS ][MATRIX_COLS ] = {
9+ [0 ] = LAYOUT_all (
10+ KC_ESC , KC_1 , KC_2 , KC_3 , KC_4 , KC_5 , KC_6 , KC_7 , KC_8 , KC_9 , KC_0 , KC_MINS , KC_EQL , KC_BSLS , KC_BSPC , KC_INS ,
11+ KC_TAB , KC_Q , KC_W , KC_E , KC_R , KC_T , KC_Y , KC_U , KC_I , KC_O , KC_P , KC_LBRC , KC_RBRC , KC_BSLS , KC_DEL ,
12+ KC_CAPS , KC_A , KC_S , KC_D , KC_F , KC_G , KC_H , KC_J , KC_K , KC_L , KC_SCLN , KC_QUOT , KC_ENT ,
13+ KC_LSFT , KC_Z , KC_X , KC_C , KC_V , KC_B , KC_N , KC_M , KC_COMM , KC_DOT , KC_SLSH , KC_RSFT , KC_UP ,
14+ KC_LCTL , KC_LWIN , KC_LALT , KC_SPC , KC_RALT , MO (1 ), KC_LEFT , KC_DOWN , KC_RGHT
15+ ),
16+ [1 ] = LAYOUT_all (
17+ KC_TILD , KC_F1 , KC_F2 , KC_F3 , KC_F4 , KC_F5 , KC_F6 , KC_F7 , KC_F8 , KC_F9 , KC_F10 , KC_F11 , KC_F12 , KC_TRNS , KC_TRNS , KC_PGUP ,
18+ KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_PSCR , KC_SCRL , KC_PAUS , KC_TRNS , KC_PGDN ,
19+ KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS ,
20+ KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_MUTE , KC_VOLU , KC_VOLD , KC_TRNS , KC_TRNS , KC_TRNS ,
21+ KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS , KC_TRNS
22+ )
23+ };
24+
25+ typedef union {
26+ uint32_t raw ;
27+ struct {
28+ bool indicator_toggle :1 ; // 1 byte
29+ HSV indicator_hsv ; // 3 bytes
30+ } __attribute__((packed )); // total 4 bytes
31+ } brick65s_config_t ;
32+
33+ brick65s_config_t g_brick65s_config ;
34+
35+ #ifdef VIA_ENABLE
36+ // via value id declaration
37+ enum tomak_custom_value_id {
38+ id_custom_indicator_toggle = 0 ,
39+ id_custom_indicator_brightness ,
40+ id_custom_indicator_color
41+ };
42+
43+ // function declaration
44+ void indicator_config_set_value ( uint8_t * data );
45+ void indicator_config_get_value ( uint8_t * data );
46+ void indicator_config_save ( void );
47+ void _set_color (HSV * color , uint8_t * data );
48+ void _get_color (HSV * color , uint8_t * data );
49+
50+ static void read_brick65s_config_from_eeprom (brick65s_config_t * config ) {
51+ config -> raw = eeconfig_read_kb () & 0xffffffff ;
52+ }
53+
54+ static void write_brick65s_config_to_eeprom (brick65s_config_t * config ) {
55+ eeconfig_update_kb (config -> raw );
56+ }
57+
58+ void eeconfig_init_kb (void ) {
59+ g_brick65s_config .raw = 0 ;
60+ g_brick65s_config .indicator_toggle = true;
61+ g_brick65s_config .indicator_hsv .h = 255 ;
62+ g_brick65s_config .indicator_hsv .s = 255 ;
63+ g_brick65s_config .indicator_hsv .v = 255 ;
64+ write_brick65s_config_to_eeprom (& g_brick65s_config );
65+ eeconfig_init_user ();
66+ }
67+
68+ void matrix_init_kb (void ) {
69+ read_brick65s_config_from_eeprom (& g_brick65s_config );
70+ matrix_init_user ();
71+ }
72+
73+ bool rgb_matrix_indicators_advanced_user (uint8_t led_min , uint8_t led_max )
74+ {
75+ if (g_brick65s_config .indicator_toggle ) {
76+ RGB rgb_caps = hsv_to_rgb ( (HSV ){ .h = g_brick65s_config .indicator_hsv .h ,
77+ .s = g_brick65s_config .indicator_hsv .s ,
78+ .v = g_brick65s_config .indicator_hsv .v } );
79+ if (host_keyboard_led_state ().caps_lock ) {
80+ rgb_matrix_set_color (0 , rgb_caps .r , rgb_caps .g , rgb_caps .b );
81+ } else {
82+ rgb_matrix_set_color (0 , 0 , 0 , 0 );
83+ }
84+
85+ if (host_keyboard_led_state ().scroll_lock ) {
86+ rgb_matrix_set_color (1 , rgb_caps .r , rgb_caps .g , rgb_caps .b );
87+ } else {
88+ rgb_matrix_set_color (1 , 0 , 0 , 0 );
89+ }
90+ } else {
91+ rgb_matrix_set_color (0 , 0 , 0 , 0 );
92+ rgb_matrix_set_color (1 , 0 , 0 , 0 );
93+ }
94+
95+ return false;
96+ }
97+
98+ void via_init_kb (void )
99+ {
100+ // If the EEPROM has the magic, the data is good.
101+ // OK to load from EEPROM
102+ if (via_eeprom_is_valid ()) {
103+ read_brick65s_config_from_eeprom (& g_brick65s_config );
104+ } else {
105+ write_brick65s_config_to_eeprom (& g_brick65s_config );
106+ // DO NOT set EEPROM valid here, let caller do this
107+ }
108+ }
109+
110+ // Some helpers for setting/getting HSV
111+ void _set_color ( HSV * color , uint8_t * data )
112+ {
113+ color -> h = data [0 ];
114+ color -> s = data [1 ];
115+ }
116+
117+ void _get_color ( HSV * color , uint8_t * data )
118+ {
119+ data [0 ] = color -> h ;
120+ data [1 ] = color -> s ;
121+ }
122+
123+ void indicator_config_get_value ( uint8_t * data )
124+ {
125+ // data = [ value_id, value_data ]
126+ uint8_t * value_id = & (data [0 ]);
127+ uint8_t * value_data = & (data [1 ]);
128+
129+ switch ( * value_id )
130+ {
131+ case id_custom_indicator_toggle :
132+ {
133+ * value_data = g_brick65s_config .indicator_toggle ;
134+ break ;
135+ }
136+ case id_custom_indicator_brightness :
137+ {
138+ * value_data = g_brick65s_config .indicator_hsv .v ;
139+ break ;
140+ }
141+ case id_custom_indicator_color :
142+ {
143+ _get_color ( & (g_brick65s_config .indicator_hsv ), value_data );
144+ break ;
145+ }
146+ }
147+ }
148+
149+ void indicator_config_set_value ( uint8_t * data )
150+ {
151+ // data = [ value_id, value_data ]
152+ uint8_t * value_id = & (data [0 ]);
153+ uint8_t * value_data = & (data [1 ]);
154+
155+ switch ( * value_id )
156+ {
157+ case id_custom_indicator_toggle :
158+ {
159+ g_brick65s_config .indicator_toggle = (bool ) * value_data ;
160+ break ;
161+ }
162+ case id_custom_indicator_brightness :
163+ {
164+ g_brick65s_config .indicator_hsv .v = * value_data ;
165+ break ;
166+ }
167+ case id_custom_indicator_color :
168+ {
169+ _set_color ( & (g_brick65s_config .indicator_hsv ), value_data );
170+ break ;
171+ }
172+ }
173+ }
174+
175+ void via_custom_value_command_kb (uint8_t * data , uint8_t length )
176+ {
177+ // data = [ command_id, channel_id, value_id, value_data ]
178+ uint8_t * command_id = & (data [0 ]);
179+ uint8_t * channel_id = & (data [1 ]);
180+ uint8_t * value_id_and_data = & (data [2 ]);
181+
182+ if ( * channel_id == id_custom_channel ) {
183+ switch ( * command_id )
184+ {
185+ case id_custom_set_value :
186+ {
187+ indicator_config_set_value (value_id_and_data );
188+ break ;
189+ }
190+ case id_custom_get_value :
191+ {
192+ indicator_config_get_value (value_id_and_data );
193+ break ;
194+ }
195+ case id_custom_save :
196+ {
197+ write_brick65s_config_to_eeprom (& g_brick65s_config );
198+ break ;
199+ }
200+ default :
201+ {
202+ // Unhandled message.
203+ * command_id = id_unhandled ;
204+ break ;
205+ }
206+ }
207+ return ;
208+ }
209+
210+ // Return the unhandled state
211+ * command_id = id_unhandled ;
212+
213+ }
214+ #endif
0 commit comments