FN_ACTIONS = { ACTION_FUNCTION(0), ACTION_FUNCTION(1), ACTION_FUNCTION(2), };
Therefore, we can define our action function like this:
void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) { bool pressed = record->event.pressed; dprint("== action function called"); dprintln(); dprint("= id: "); debug_dec(id); dprintln(); dprint("= pressed: "); debug_dec(record->event.pressed); dprintln(); if (id == 0) { if (pressed) { layer_on(1); } else { layer_clear(); } } else if (id == 1) { // bluefruit pair button if (pressed) { dprintf("= setting pair button HIGH\n"); digitalWrite(PAIR_BUTTON_PIN, HIGH); } else { dprintf("= setting pair button LOW\n"); digitalWrite(PAIR_BUTTON_PIN, LOW); } } else if (id == 2) { if (pressed) { if (reset_press_time == 0) { reset_press_time = timer_read(); } } else { reset_press_time = 0; } } dprint("== end of action function\n"); }
Since we're on the subject of interacting with Bluefruit, lets take a quick look at the BluefruitHost object:
void BluefruitHost::begin() { Serial1.begin(9600); } uint8_t BluefruitHost::getLEDs() { // not implemented on Bluefruit; method is virtual so feel free to override return 0; } void BluefruitHost::sendKeyboard(KeyboardReport &report) { bluefruit_trace_header(); dprintf("(keyboard) "); _serial_send(0xFD); _serial_send(report.getModifiers()); _serial_send(report.getReserved()); for (short i = 0; i < REPORT_SIZE; i++) { _serial_send(report.getKey(i)); } bluefruit_trace_footer(); } void BluefruitHost::sendMouse(MouseReport &report) { bluefruit_trace_header(); dprintf("(mouse) "); _serial_send(0xFD); _serial_send(0x00); _serial_send(0x03); _serial_send(report.getButtons()); _serial_send(report.getX()); _serial_send(report.getY()); _serial_send(report.getV()); // TODO: determine if bluefruit _serial_send(report.getH()); // supports mouse wheel - BCG _serial_send(0x00); bluefruit_trace_footer(); };
In Bluefruit 1.2, support for "consumer keys" was added. You can also send this information over serial, allow it is a little trickier. The data that Bluefruit expects is a 16 bit map to denote which key is pressed. Some of the consumer key functions do not map to existing TMK keycodes, so I've ignored those; similarly some of the defined TMK consumer keys are not supported by Bluefruit, so I've ignored those as well. Feel free to change them to suit your purposes.
/* +-----------------+-------------------+-------+ | Consumer Key | Bit Map | Hex | +-----------------+-------------------+-------+ | Home | 00000001 00000000 | 01 00 | | KeyboardLayout | 00000010 00000000 | 02 00 | | Search | 00000100 00000000 | 04 00 | | Snapshot | 00001000 00000000 | 08 00 | | VolumeUp | 00010000 00000000 | 10 00 | | VolumeDown | 00100000 00000000 | 20 00 | | Play/Pause | 01000000 00000000 | 40 00 | | Fast Forward | 10000000 00000000 | 80 00 | | Rewind | 00000000 00000001 | 00 01 | | Scan Next Track | 00000000 00000010 | 00 02 | | Scan Prev Track | 00000000 00000100 | 00 04 | | Random Play | 00000000 00001000 | 00 08 | | Stop | 00000000 00010000 | 00 10 | +-------------------------------------+-------+ */ #define CONSUMER2BLUEFRUIT(usage) \ (usage == AUDIO_MUTE ? 0x0000 : \ (usage == AUDIO_VOL_UP ? 0x1000 : \ (usage == AUDIO_VOL_DOWN ? 0x2000 : \ (usage == TRANSPORT_NEXT_TRACK ? 0x0002 : \ (usage == TRANSPORT_PREV_TRACK ? 0x0004 : \ (usage == TRANSPORT_STOP ? 0x0010 : \ (usage == TRANSPORT_STOP_EJECT ? 0x0000 : \ (usage == TRANSPORT_PLAY_PAUSE ? 0x4000 : \ (usage == AL_CC_CONFIG ? 0x0000 : \ (usage == AL_EMAIL ? 0x0000 : \ (usage == AL_CALCULATOR ? 0x0000 : \ (usage == AL_LOCAL_BROWSER ? 0x0000 : \ (usage == AC_SEARCH ? 0x0400 : \ (usage == AC_HOME ? 0x0100 : \ (usage == AC_BACK ? 0x0000 : \ (usage == AC_FORWARD ? 0x0000 : \ (usage == AC_STOP ? 0x0000 : \ (usage == AC_REFRESH ? 0x0000 : \ (usage == AC_BOOKMARKS ? 0x0000 : 0))))))))))))))))))) void BluefruitHost::sendConsumer(uint16_t data) { if (data == _last_consumer_data) return; _last_consumer_data = data; uint16_t bitmap = CONSUMER2BLUEFRUIT(data); bluefruit_trace_header(); dprintf("(consumer) "); _serial_send(0xFD); _serial_send(0x00); _serial_send(0x02); _serial_send((bitmap>>8)&0xFF); _serial_send(bitmap&0xFF); _serial_send(0x00); _serial_send(0x00); _serial_send(0x00); _serial_send(0x00); bluefruit_trace_footer(); }; void BluefruitHost::sendSystem(uint16_t data) { // not implemented in Bluefruit }