Skip to content

Commit 7edac21

Browse files
committed
separated into api files/folder
1 parent f25596b commit 7edac21

File tree

15 files changed

+303
-283
lines changed

15 files changed

+303
-283
lines changed

build_keyboard.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ ifndef CUSTOM_MATRIX
131131
SRC += $(QUANTUM_DIR)/matrix.c
132132
endif
133133

134+
ifeq ($(strip $(API_SYSEX_ENABLE)), yes)
135+
OPT_DEFS += -DAPI_SYSEX_ENABLE
136+
SRC += $(QUANTUM_DIR)/api/api_sysex.c
137+
OPT_DEFS += -DAPI_ENABLE
138+
SRC += $(QUANTUM_DIR)/api.c
139+
MIDI_ENABLE=yes
140+
endif
141+
134142
ifeq ($(strip $(MIDI_ENABLE)), yes)
135143
OPT_DEFS += -DMIDI_ENABLE
136144
SRC += $(QUANTUM_DIR)/process_keycode/process_midi.c

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ COMMON_VPATH += $(QUANTUM_PATH)
2323
COMMON_VPATH += $(QUANTUM_PATH)/keymap_extras
2424
COMMON_VPATH += $(QUANTUM_PATH)/audio
2525
COMMON_VPATH += $(QUANTUM_PATH)/process_keycode
26+
COMMON_VPATH += $(QUANTUM_PATH)/api
2627
COMMON_VPATH += $(SERIAL_PATH)

keyboards/ergodox/keymaps/jack/keymap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
9191
break;
9292
case 2:
9393
if (record->event.pressed) { // For resetting EEPROM
94-
send_unicode_midi(0x0CA0);
94+
api_send_unicode(0x0CA0);
9595
}
9696
break;
9797
}

keyboards/ergodox/rules.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ COMMAND_ENABLE ?= yes # Commands for debug and configuration
2424
CUSTOM_MATRIX ?= yes # Custom matrix file for the ErgoDox EZ
2525
SLEEP_LED_ENABLE ?= yes # Breathing sleep LED during USB suspend
2626
NKRO_ENABLE ?= yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
27-
MIDI_ENABLE ?= no # MIDI controls
2827
UNICODE_ENABLE ?= yes # Unicode
2928
ONEHAND_ENABLE ?= yes # Allow swapping hands of keyboard

keyboards/planck/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ AUDIO_ENABLE ?= no # Audio output on port C6
6262
UNICODE_ENABLE ?= no # Unicode
6363
BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
6464
RGBLIGHT_ENABLE ?= no # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
65+
API_SYSEX_ENABLE = yes
6566

6667
# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
6768
SLEEP_LED_ENABLE ?= no # Breathing sleep LED during USB suspend
Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
2-
3-
# Build Options
4-
# change to "no" to disable the options, or define them in the Makefile in
5-
# the appropriate keymap folder that will get included automatically
6-
#
7-
BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000)
8-
MOUSEKEY_ENABLE = yes # Mouse keys(+4700)
9-
EXTRAKEY_ENABLE = yes # Audio control and System control(+450)
10-
CONSOLE_ENABLE = no # Console for debug(+400)
11-
COMMAND_ENABLE = yes # Commands for debug and configuration
12-
NKRO_ENABLE = yes # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
13-
BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality
14-
MIDI_ENABLE = no # MIDI controls
15-
AUDIO_ENABLE = yes # Audio output on port C6
16-
UNICODE_ENABLE = no # Unicode
17-
BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
18-
RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
19-
20-
# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
21-
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
22-
231
ifndef QUANTUM_DIR
242
include ../../../../Makefile
253
endif

keyboards/preonic/rules.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ OPT_DEFS += -DBOOTLOADER_SIZE=4096
5353
# the appropriate keymap folder that will get included automatically
5454
#
5555
BOOTMAGIC_ENABLE ?= no # Virtual DIP switch configuration(+1000)
56-
MOUSEKEY_ENABLE ?= yes # Mouse keys(+4700)
56+
MOUSEKEY_ENABLE ?= no # Mouse keys(+4700)
5757
EXTRAKEY_ENABLE ?= yes # Audio control and System control(+450)
5858
CONSOLE_ENABLE ?= no # Console for debug(+400)
5959
COMMAND_ENABLE ?= yes # Commands for debug and configuration
@@ -64,6 +64,7 @@ AUDIO_ENABLE ?= no # Audio output on port C6
6464
UNICODE_ENABLE ?= no # Unicode
6565
BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
6666
RGBLIGHT_ENABLE ?= no # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
67+
API_SYSEX_ENABLE ?= yes
6768

6869
# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
6970
SLEEP_LED_ENABLE ?= no # Breathing sleep LED during USB suspend

quantum/api.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "api.h"
2+
#include "quantum.h"
3+
4+
void dword_to_bytes(uint32_t dword, uint8_t * bytes) {
5+
bytes[0] = (dword >> 24) & 0xFF;
6+
bytes[1] = (dword >> 16) & 0xFF;
7+
bytes[2] = (dword >> 8) & 0xFF;
8+
bytes[3] = (dword >> 0) & 0xFF;
9+
}
10+
11+
uint32_t bytes_to_dword(uint8_t * bytes, uint8_t index) {
12+
return ((uint32_t)bytes[index + 0] << 24) | ((uint32_t)bytes[index + 1] << 16) | ((uint32_t)bytes[index + 2] << 8) | (uint32_t)bytes[index + 3];
13+
}
14+
15+
__attribute__ ((weak))
16+
bool process_api_quantum(uint8_t length, uint8_t * data) {
17+
return process_api_keyboard(length, data);
18+
}
19+
20+
__attribute__ ((weak))
21+
bool process_api_keyboard(uint8_t length, uint8_t * data) {
22+
return process_api_user(length, data);
23+
}
24+
25+
__attribute__ ((weak))
26+
bool process_api_user(uint8_t length, uint8_t * data) {
27+
return true;
28+
}
29+
30+
void process_api(uint16_t length, uint8_t * data) {
31+
// SEND_STRING("\nRX: ");
32+
// for (uint8_t i = 0; i < length; i++) {
33+
// send_byte(data[i]);
34+
// SEND_STRING(" ");
35+
// }
36+
if (!process_api_quantum(length, data))
37+
return;
38+
39+
switch (data[0]) {
40+
case MT_SET_DATA:
41+
switch (data[1]) {
42+
case DT_DEFAULT_LAYER: {
43+
eeconfig_update_default_layer(data[2]);
44+
default_layer_set((uint32_t)(data[2]));
45+
break;
46+
}
47+
case DT_KEYMAP_OPTIONS: {
48+
eeconfig_update_keymap(data[2]);
49+
break;
50+
}
51+
case DT_RGBLIGHT: {
52+
#ifdef RGBLIGHT_ENABLE
53+
uint32_t rgblight = bytes_to_dword(data, 2);
54+
rgblight_update_dword(rgblight);
55+
#endif
56+
break;
57+
}
58+
}
59+
case MT_GET_DATA:
60+
switch (data[1]) {
61+
case DT_HANDSHAKE: {
62+
MT_GET_DATA_ACK(DT_HANDSHAKE, NULL, 0);
63+
break;
64+
}
65+
case DT_DEBUG: {
66+
uint8_t debug_bytes[1] = { eeprom_read_byte(EECONFIG_DEBUG) };
67+
MT_GET_DATA_ACK(DT_DEBUG, debug_bytes, 1);
68+
break;
69+
}
70+
case DT_DEFAULT_LAYER: {
71+
uint8_t default_bytes[1] = { eeprom_read_byte(EECONFIG_DEFAULT_LAYER) };
72+
MT_GET_DATA_ACK(DT_DEFAULT_LAYER, default_bytes, 1);
73+
break;
74+
}
75+
case DT_CURRENT_LAYER: {
76+
uint8_t layer_state_bytes[4];
77+
dword_to_bytes(layer_state, layer_state_bytes);
78+
MT_GET_DATA_ACK(DT_CURRENT_LAYER, layer_state_bytes, 4);
79+
break;
80+
}
81+
case DT_AUDIO: {
82+
#ifdef AUDIO_ENABLE
83+
uint8_t audio_bytes[1] = { eeprom_read_byte(EECONFIG_AUDIO) };
84+
MT_GET_DATA_ACK(DT_AUDIO, audio_bytes, 1);
85+
#else
86+
MT_GET_DATA_ACK(DT_AUDIO, NULL, 0);
87+
#endif
88+
break;
89+
}
90+
case DT_BACKLIGHT: {
91+
#ifdef BACKLIGHT_ENABLE
92+
uint8_t backlight_bytes[1] = { eeprom_read_byte(EECONFIG_BACKLIGHT) };
93+
MT_GET_DATA_ACK(DT_BACKLIGHT, backlight_bytes, 1);
94+
#else
95+
MT_GET_DATA_ACK(DT_BACKLIGHT, NULL, 0);
96+
#endif
97+
break;
98+
}
99+
case DT_RGBLIGHT: {
100+
#ifdef RGBLIGHT_ENABLE
101+
uint8_t rgblight_bytes[4];
102+
dword_to_bytes(eeconfig_read_rgblight(), rgblight_bytes);
103+
MT_GET_DATA_ACK(DT_RGBLIGHT, rgblight_bytes, 4);
104+
#else
105+
MT_GET_DATA_ACK(DT_RGBLIGHT, NULL, 0);
106+
#endif
107+
break;
108+
}
109+
case DT_KEYMAP_OPTIONS: {
110+
uint8_t keymap_bytes[1] = { eeconfig_read_keymap() };
111+
MT_GET_DATA_ACK(DT_KEYMAP_OPTIONS, keymap_bytes, 1);
112+
break;
113+
}
114+
case DT_KEYMAP_SIZE: {
115+
uint8_t keymap_size[2] = {MATRIX_ROWS, MATRIX_COLS};
116+
MT_GET_DATA_ACK(DT_KEYMAP_SIZE, keymap_size, 2);
117+
break;
118+
}
119+
case DT_KEYMAP: {
120+
uint8_t keymap_data[MATRIX_ROWS * MATRIX_COLS * 4 + 3];
121+
keymap_data[0] = data[2];
122+
keymap_data[1] = MATRIX_ROWS;
123+
keymap_data[2] = MATRIX_COLS;
124+
for (int i = 0; i < MATRIX_ROWS; i++) {
125+
for (int j = 0; j < MATRIX_COLS; j++) {
126+
keymap_data[3 + (i*MATRIX_COLS*2) + (j*2)] = pgm_read_word(&keymaps[data[2]][i][j]) >> 8;
127+
keymap_data[3 + (i*MATRIX_COLS*2) + (j*2) + 1] = pgm_read_word(&keymaps[data[2]][i][j]) & 0xFF;
128+
}
129+
}
130+
MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, MATRIX_ROWS * MATRIX_COLS * 4 + 3);
131+
// uint8_t keymap_data[5];
132+
// keymap_data[0] = data[2];
133+
// keymap_data[1] = data[3];
134+
// keymap_data[2] = data[4];
135+
// keymap_data[3] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) >> 8;
136+
// keymap_data[4] = pgm_read_word(&keymaps[data[2]][data[3]][data[4]]) & 0xFF;
137+
138+
// MT_GET_DATA_ACK(DT_KEYMAP, keymap_data, 5);
139+
break;
140+
}
141+
default:
142+
break;
143+
}
144+
break;
145+
case MT_SET_DATA_ACK:
146+
case MT_GET_DATA_ACK:
147+
break;
148+
case MT_SEND_DATA:
149+
break;
150+
case MT_SEND_DATA_ACK:
151+
break;
152+
case MT_EXE_ACTION:
153+
break;
154+
case MT_EXE_ACTION_ACK:
155+
break;
156+
case MT_TYPE_ERROR:
157+
break;
158+
default: ; // command not recognised
159+
SEND_BYTES(MT_TYPE_ERROR, DT_NONE, data, length);
160+
break;
161+
162+
// #ifdef RGBLIGHT_ENABLE
163+
// case 0x27: ; // RGB LED functions
164+
// switch (*data++) {
165+
// case 0x00: ; // Update HSV
166+
// rgblight_sethsv((data[0] << 8 | data[1]) % 360, data[2], data[3]);
167+
// break;
168+
// case 0x01: ; // Update RGB
169+
// break;
170+
// case 0x02: ; // Update mode
171+
// rgblight_mode(data[0]);
172+
// break;
173+
// }
174+
// break;
175+
// #endif
176+
}
177+
178+
}

quantum/api.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef _API_H_
2+
#define _API_H_
3+
4+
#include "lufa.h"
5+
6+
enum MESSAGE_TYPE {
7+
MT_GET_DATA = 0x10, // Get data from keyboard
8+
MT_GET_DATA_ACK = 0x11, // returned data to process (ACK)
9+
MT_SET_DATA = 0x20, // Set data on keyboard
10+
MT_SET_DATA_ACK = 0x21, // returned data to confirm (ACK)
11+
MT_SEND_DATA = 0x30, // Sending data/action from keyboard
12+
MT_SEND_DATA_ACK = 0x31, // returned data/action confirmation (ACK)
13+
MT_EXE_ACTION = 0x40, // executing actions on keyboard
14+
MT_EXE_ACTION_ACK =0x41, // return confirmation/value (ACK)
15+
MT_TYPE_ERROR = 0x80 // type not recofgnised (ACK)
16+
};
17+
18+
enum DATA_TYPE {
19+
DT_NONE = 0x00,
20+
DT_HANDSHAKE,
21+
DT_DEFAULT_LAYER,
22+
DT_CURRENT_LAYER,
23+
DT_KEYMAP_OPTIONS,
24+
DT_BACKLIGHT,
25+
DT_RGBLIGHT,
26+
DT_UNICODE,
27+
DT_DEBUG,
28+
DT_AUDIO,
29+
DT_QUANTUM_ACTION,
30+
DT_KEYBOARD_ACTION,
31+
DT_USER_ACTION,
32+
DT_KEYMAP_SIZE,
33+
DT_KEYMAP
34+
};
35+
36+
void dword_to_bytes(uint32_t dword, uint8_t * bytes);
37+
uint32_t bytes_to_dword(uint8_t * bytes, uint8_t index);
38+
39+
#define MT_GET_DATA(data_type, data, length) SEND_BYTES(MT_GET_DATA, data_type, data, length)
40+
#define MT_GET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_GET_DATA_ACK, data_type, data, length)
41+
#define MT_SET_DATA(data_type, data, length) SEND_BYTES(MT_SET_DATA, data_type, data, length)
42+
#define MT_SET_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SET_DATA_ACK, data_type, data, length)
43+
#define MT_SEND_DATA(data_type, data, length) SEND_BYTES(MT_SEND_DATA, data_type, data, length)
44+
#define MT_SEND_DATA_ACK(data_type, data, length) SEND_BYTES(MT_SEND_DATA_ACK, data_type, data, length)
45+
#define MT_EXE_ACTION(data_type, data, length) SEND_BYTES(MT_EXE_ACTION, data_type, data, length)
46+
#define MT_EXE_ACTION_ACK(data_type, data, length) SEND_BYTES(MT_EXE_ACTION_ACK, data_type, data, length)
47+
48+
void process_api(uint16_t length, uint8_t * data);
49+
50+
__attribute__ ((weak))
51+
bool process_api_quantum(uint8_t length, uint8_t * data);
52+
53+
__attribute__ ((weak))
54+
bool process_api_keyboard(uint8_t length, uint8_t * data);
55+
56+
__attribute__ ((weak))
57+
bool process_api_user(uint8_t length, uint8_t * data);
58+
59+
#endif

quantum/api/api_sysex.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "api_sysex.h"
2+
3+
void send_bytes_sysex(uint8_t message_type, uint8_t data_type, uint8_t * bytes, uint16_t length) {
4+
// SEND_STRING("\nTX: ");
5+
// for (uint8_t i = 0; i < length; i++) {
6+
// send_byte(bytes[i]);
7+
// SEND_STRING(" ");
8+
// }
9+
uint8_t * precode = malloc(sizeof(uint8_t) * (length + 2));
10+
precode[0] = message_type;
11+
precode[1] = data_type;
12+
memcpy(precode + 2, bytes, length);
13+
uint8_t * encoded = malloc(sizeof(uint8_t) * (sysex_encoded_length(length + 2)));
14+
uint16_t encoded_length = sysex_encode(encoded, precode, length + 2);
15+
uint8_t * array = malloc(sizeof(uint8_t) * (encoded_length + 5));
16+
array[0] = 0xF0;
17+
array[1] = 0x00;
18+
array[2] = 0x00;
19+
array[3] = 0x00;
20+
array[encoded_length + 4] = 0xF7;
21+
memcpy(array + 4, encoded, encoded_length);
22+
midi_send_array(&midi_device, encoded_length + 5, array);
23+
24+
// SEND_STRING("\nTD: ");
25+
// for (uint8_t i = 0; i < encoded_length + 5; i++) {
26+
// send_byte(array[i]);
27+
// SEND_STRING(" ");
28+
// }
29+
}

0 commit comments

Comments
 (0)