From 8dd0ff6c7a1ebc70fb70fe64654353d3e5afeb68 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Fri, 1 Feb 2019 14:14:39 +1100 Subject: Remove keyboard description from Adafruit BLE device name --- tmk_core/protocol/lufa/adafruit_ble.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index bee6bb2c19..80839731fd 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -533,8 +533,7 @@ bool adafruit_ble_enable_keyboard(void) { // Disable command echo static const char kEcho[] PROGMEM = "ATE=0"; // Make the advertised name match the keyboard - static const char kGapDevName[] PROGMEM = - "AT+GAPDEVNAME=" STR(PRODUCT) " " STR(DESCRIPTION); + static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); // Turn on keyboard support static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; -- cgit v1.2.3 From b624f32f944acdc59dcb130674c09090c5c404cb Mon Sep 17 00:00:00 2001 From: skullY Date: Fri, 30 Aug 2019 11:19:03 -0700 Subject: clang-format changes --- tmk_core/protocol/lufa/adafruit_ble.cpp | 1117 +++++++++++++++---------------- tmk_core/protocol/lufa/adafruit_ble.h | 30 +- tmk_core/protocol/lufa/bluetooth.c | 20 +- tmk_core/protocol/lufa/bluetooth.h | 37 +- tmk_core/protocol/lufa/lufa.c | 692 +++++++++---------- tmk_core/protocol/lufa/lufa.h | 20 +- tmk_core/protocol/lufa/outputselect.c | 9 +- tmk_core/protocol/lufa/outputselect.h | 14 +- 8 files changed, 892 insertions(+), 1047 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 80839731fd..71d0936f8f 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -15,35 +15,34 @@ // You may define them to something else in your config.h // if yours is wired up differently. #ifndef AdafruitBleResetPin -#define AdafruitBleResetPin D4 +# define AdafruitBleResetPin D4 #endif #ifndef AdafruitBleCSPin -#define AdafruitBleCSPin B4 +# define AdafruitBleCSPin B4 #endif #ifndef AdafruitBleIRQPin -#define AdafruitBleIRQPin E6 +# define AdafruitBleIRQPin E6 #endif - #define SAMPLE_BATTERY #define ConnectionUpdateInterval 1000 /* milliseconds */ static struct { - bool is_connected; - bool initialized; - bool configured; + bool is_connected; + bool initialized; + bool configured; #define ProbedEvents 1 #define UsingEvents 2 - bool event_flags; + bool event_flags; #ifdef SAMPLE_BATTERY - uint16_t last_battery_update; - uint32_t vbat; + uint16_t last_battery_update; + uint32_t vbat; #endif - uint16_t last_connection_update; + uint16_t last_connection_update; } state; // Commands are encoded using SDEP and sent via SPI @@ -51,14 +50,14 @@ static struct { #define SdepMaxPayload 16 struct sdep_msg { - uint8_t type; - uint8_t cmd_low; - uint8_t cmd_high; - struct __attribute__((packed)) { - uint8_t len:7; - uint8_t more:1; - }; - uint8_t payload[SdepMaxPayload]; + uint8_t type; + uint8_t cmd_low; + uint8_t cmd_high; + struct __attribute__((packed)) { + uint8_t len : 7; + uint8_t more : 1; + }; + uint8_t payload[SdepMaxPayload]; } __attribute__((packed)); // The recv latency is relatively high, so when we're hammering keys quickly, @@ -68,28 +67,28 @@ struct sdep_msg { // information here. enum queue_type { - QTKeyReport, // 1-byte modifier + 6-byte key report - QTConsumer, // 16-bit key code + QTKeyReport, // 1-byte modifier + 6-byte key report + QTConsumer, // 16-bit key code #ifdef MOUSE_ENABLE - QTMouseMove, // 4-byte mouse report + QTMouseMove, // 4-byte mouse report #endif }; struct queue_item { - enum queue_type queue_type; - uint16_t added; - union __attribute__((packed)) { - struct __attribute__((packed)) { - uint8_t modifier; - uint8_t keys[6]; - } key; - - uint16_t consumer; - struct __attribute__((packed)) { - int8_t x, y, scroll, pan; - uint8_t buttons; - } mousemove; - }; + enum queue_type queue_type; + uint16_t added; + union __attribute__((packed)) { + struct __attribute__((packed)) { + uint8_t modifier; + uint8_t keys[6]; + } key; + + uint16_t consumer; + struct __attribute__((packed)) { + int8_t x, y, scroll, pan; + uint8_t buttons; + } mousemove; + }; }; // Items that we wish to send @@ -102,108 +101,104 @@ static RingBuffer resp_buf; static bool process_queue_item(struct queue_item *item, uint16_t timeout); enum sdep_type { - SdepCommand = 0x10, - SdepResponse = 0x20, - SdepAlert = 0x40, - SdepError = 0x80, - SdepSlaveNotReady = 0xfe, // Try again later - SdepSlaveOverflow = 0xff, // You read more data than is available + SdepCommand = 0x10, + SdepResponse = 0x20, + SdepAlert = 0x40, + SdepError = 0x80, + SdepSlaveNotReady = 0xfe, // Try again later + SdepSlaveOverflow = 0xff, // You read more data than is available }; enum ble_cmd { - BleInitialize = 0xbeef, - BleAtWrapper = 0x0a00, - BleUartTx = 0x0a01, - BleUartRx = 0x0a02, + BleInitialize = 0xbeef, + BleAtWrapper = 0x0a00, + BleUartTx = 0x0a01, + BleUartRx = 0x0a02, }; enum ble_system_event_bits { - BleSystemConnected = 0, - BleSystemDisconnected = 1, - BleSystemUartRx = 8, - BleSystemMidiRx = 10, + BleSystemConnected = 0, + BleSystemDisconnected = 1, + BleSystemUartRx = 8, + BleSystemMidiRx = 10, }; // The SDEP.md file says 2MHz but the web page and the sample driver // both use 4MHz #define SpiBusSpeed 4000000 -#define SdepTimeout 150 /* milliseconds */ -#define SdepShortTimeout 10 /* milliseconds */ -#define SdepBackOff 25 /* microseconds */ +#define SdepTimeout 150 /* milliseconds */ +#define SdepShortTimeout 10 /* milliseconds */ +#define SdepBackOff 25 /* microseconds */ #define BatteryUpdateInterval 10000 /* milliseconds */ -static bool at_command(const char *cmd, char *resp, uint16_t resplen, - bool verbose, uint16_t timeout = SdepTimeout); -static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, - bool verbose = false); +static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout = SdepTimeout); +static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose = false); struct SPI_Settings { - uint8_t spcr, spsr; + uint8_t spcr, spsr; }; static struct SPI_Settings spi; // Initialize 4Mhz MSBFIRST MODE0 void SPI_init(struct SPI_Settings *spi) { - spi->spcr = _BV(SPE) | _BV(MSTR); - spi->spsr = _BV(SPI2X); + spi->spcr = _BV(SPE) | _BV(MSTR); + spi->spsr = _BV(SPI2X); - static_assert(SpiBusSpeed == F_CPU / 2, "hard coded at 4Mhz"); + static_assert(SpiBusSpeed == F_CPU / 2, "hard coded at 4Mhz"); - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - // Ensure that SS is OUTPUT High - digitalWrite(B0, PinLevelHigh); - pinMode(B0, PinDirectionOutput); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + // Ensure that SS is OUTPUT High + digitalWrite(B0, PinLevelHigh); + pinMode(B0, PinDirectionOutput); - SPCR |= _BV(MSTR); - SPCR |= _BV(SPE); - pinMode(B1 /* SCK */, PinDirectionOutput); - pinMode(B2 /* MOSI */, PinDirectionOutput); - } + SPCR |= _BV(MSTR); + SPCR |= _BV(SPE); + pinMode(B1 /* SCK */, PinDirectionOutput); + pinMode(B2 /* MOSI */, PinDirectionOutput); + } } -static inline void SPI_begin(struct SPI_Settings*spi) { - SPCR = spi->spcr; - SPSR = spi->spsr; +static inline void SPI_begin(struct SPI_Settings *spi) { + SPCR = spi->spcr; + SPSR = spi->spsr; } static inline uint8_t SPI_TransferByte(uint8_t data) { - SPDR = data; - asm volatile("nop"); - while (!(SPSR & _BV(SPIF))) { - ; // wait - } - return SPDR; + SPDR = data; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + return SPDR; } static inline void spi_send_bytes(const uint8_t *buf, uint8_t len) { - if (len == 0) return; - const uint8_t *end = buf + len; - while (buf < end) { - SPDR = *buf; - while (!(SPSR & _BV(SPIF))) { - ; // wait + if (len == 0) return; + const uint8_t *end = buf + len; + while (buf < end) { + SPDR = *buf; + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + ++buf; } - ++buf; - } } -static inline uint16_t spi_read_byte(void) { - return SPI_TransferByte(0x00 /* dummy */); -} +static inline uint16_t spi_read_byte(void) { return SPI_TransferByte(0x00 /* dummy */); } static inline void spi_recv_bytes(uint8_t *buf, uint8_t len) { - const uint8_t *end = buf + len; - if (len == 0) return; - while (buf < end) { - SPDR = 0; // write a dummy to initiate read - while (!(SPSR & _BV(SPIF))) { - ; // wait + const uint8_t *end = buf + len; + if (len == 0) return; + while (buf < end) { + SPDR = 0; // write a dummy to initiate read + while (!(SPSR & _BV(SPIF))) { + ; // wait + } + *buf = SPDR; + ++buf; } - *buf = SPDR; - ++buf; - } } #if 0 @@ -223,600 +218,572 @@ static void dump_pkt(const struct sdep_msg *msg) { // Send a single SDEP packet static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { - SPI_begin(&spi); + SPI_begin(&spi); + + digitalWrite(AdafruitBleCSPin, PinLevelLow); + uint16_t timerStart = timer_read(); + bool success = false; + bool ready = false; + + do { + ready = SPI_TransferByte(msg->type) != SdepSlaveNotReady; + if (ready) { + break; + } - digitalWrite(AdafruitBleCSPin, PinLevelLow); - uint16_t timerStart = timer_read(); - bool success = false; - bool ready = false; + // Release it and let it initialize + digitalWrite(AdafruitBleCSPin, PinLevelHigh); + _delay_us(SdepBackOff); + digitalWrite(AdafruitBleCSPin, PinLevelLow); + } while (timer_elapsed(timerStart) < timeout); - do { - ready = SPI_TransferByte(msg->type) != SdepSlaveNotReady; if (ready) { - break; + // Slave is ready; send the rest of the packet + spi_send_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); + success = true; } - // Release it and let it initialize digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); - } while (timer_elapsed(timerStart) < timeout); - - if (ready) { - // Slave is ready; send the rest of the packet - spi_send_bytes(&msg->cmd_low, - sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); - success = true; - } - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - - return success; + return success; } -static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, - const uint8_t *payload, uint8_t len, - bool moredata) { - msg->type = SdepCommand; - msg->cmd_low = command & 0xff; - msg->cmd_high = command >> 8; - msg->len = len; - msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0; +static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, const uint8_t *payload, uint8_t len, bool moredata) { + msg->type = SdepCommand; + msg->cmd_low = command & 0xff; + msg->cmd_high = command >> 8; + msg->len = len; + msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0; - static_assert(sizeof(*msg) == 20, "msg is correctly packed"); + static_assert(sizeof(*msg) == 20, "msg is correctly packed"); - memcpy(msg->payload, payload, len); + memcpy(msg->payload, payload, len); } // Read a single SDEP packet static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) { - bool success = false; - uint16_t timerStart = timer_read(); - bool ready = false; + bool success = false; + uint16_t timerStart = timer_read(); + bool ready = false; + + do { + ready = digitalRead(AdafruitBleIRQPin); + if (ready) { + break; + } + _delay_us(1); + } while (timer_elapsed(timerStart) < timeout); - do { - ready = digitalRead(AdafruitBleIRQPin); if (ready) { - break; - } - _delay_us(1); - } while (timer_elapsed(timerStart) < timeout); + SPI_begin(&spi); - if (ready) { - SPI_begin(&spi); + digitalWrite(AdafruitBleCSPin, PinLevelLow); - digitalWrite(AdafruitBleCSPin, PinLevelLow); + do { + // Read the command type, waiting for the data to be ready + msg->type = spi_read_byte(); + if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { + // Release it and let it initialize + digitalWrite(AdafruitBleCSPin, PinLevelHigh); + _delay_us(SdepBackOff); + digitalWrite(AdafruitBleCSPin, PinLevelLow); + continue; + } + + // Read the rest of the header + spi_recv_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); + + // and get the payload if there is any + if (msg->len <= SdepMaxPayload) { + spi_recv_bytes(msg->payload, msg->len); + } + success = true; + break; + } while (timer_elapsed(timerStart) < timeout); - do { - // Read the command type, waiting for the data to be ready - msg->type = spi_read_byte(); - if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { - // Release it and let it initialize digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); - continue; - } - - // Read the rest of the header - spi_recv_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); - - // and get the payload if there is any - if (msg->len <= SdepMaxPayload) { - spi_recv_bytes(msg->payload, msg->len); - } - success = true; - break; - } while (timer_elapsed(timerStart) < timeout); - - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - } - return success; + } + return success; } static void resp_buf_read_one(bool greedy) { - uint16_t last_send; - if (!resp_buf.peek(last_send)) { - return; - } + uint16_t last_send; + if (!resp_buf.peek(last_send)) { + return; + } - if (digitalRead(AdafruitBleIRQPin)) { - struct sdep_msg msg; + if (digitalRead(AdafruitBleIRQPin)) { + struct sdep_msg msg; -again: - if (sdep_recv_pkt(&msg, SdepTimeout)) { - if (!msg.more) { - // We got it; consume this entry - resp_buf.get(last_send); - dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send)); - } + again: + if (sdep_recv_pkt(&msg, SdepTimeout)) { + if (!msg.more) { + // We got it; consume this entry + resp_buf.get(last_send); + dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send)); + } - if (greedy && resp_buf.peek(last_send) && digitalRead(AdafruitBleIRQPin)) { - goto again; - } - } + if (greedy && resp_buf.peek(last_send) && digitalRead(AdafruitBleIRQPin)) { + goto again; + } + } - } else if (timer_elapsed(last_send) > SdepTimeout * 2) { - dprintf("waiting_for_result: timeout, resp_buf size %d\n", - (int)resp_buf.size()); + } else if (timer_elapsed(last_send) > SdepTimeout * 2) { + dprintf("waiting_for_result: timeout, resp_buf size %d\n", (int)resp_buf.size()); - // Timed out: consume this entry - resp_buf.get(last_send); - } + // Timed out: consume this entry + resp_buf.get(last_send); + } } static void send_buf_send_one(uint16_t timeout = SdepTimeout) { - struct queue_item item; - - // Don't send anything more until we get an ACK - if (!resp_buf.empty()) { - return; - } - - if (!send_buf.peek(item)) { - return; - } - if (process_queue_item(&item, timeout)) { - // commit that peek - send_buf.get(item); - dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size()); - } else { - dprint("failed to send, will retry\n"); - _delay_ms(SdepTimeout); - resp_buf_read_one(true); - } + struct queue_item item; + + // Don't send anything more until we get an ACK + if (!resp_buf.empty()) { + return; + } + + if (!send_buf.peek(item)) { + return; + } + if (process_queue_item(&item, timeout)) { + // commit that peek + send_buf.get(item); + dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size()); + } else { + dprint("failed to send, will retry\n"); + _delay_ms(SdepTimeout); + resp_buf_read_one(true); + } } static void resp_buf_wait(const char *cmd) { - bool didPrint = false; - while (!resp_buf.empty()) { - if (!didPrint) { - dprintf("wait on buf for %s\n", cmd); - didPrint = true; + bool didPrint = false; + while (!resp_buf.empty()) { + if (!didPrint) { + dprintf("wait on buf for %s\n", cmd); + didPrint = true; + } + resp_buf_read_one(true); } - resp_buf_read_one(true); - } } static bool ble_init(void) { - state.initialized = false; - state.configured = false; - state.is_connected = false; + state.initialized = false; + state.configured = false; + state.is_connected = false; - pinMode(AdafruitBleIRQPin, PinDirectionInput); - pinMode(AdafruitBleCSPin, PinDirectionOutput); - digitalWrite(AdafruitBleCSPin, PinLevelHigh); + pinMode(AdafruitBleIRQPin, PinDirectionInput); + pinMode(AdafruitBleCSPin, PinDirectionOutput); + digitalWrite(AdafruitBleCSPin, PinLevelHigh); - SPI_init(&spi); + SPI_init(&spi); - // Perform a hardware reset - pinMode(AdafruitBleResetPin, PinDirectionOutput); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); - digitalWrite(AdafruitBleResetPin, PinLevelLow); - _delay_ms(10); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); + // Perform a hardware reset + pinMode(AdafruitBleResetPin, PinDirectionOutput); + digitalWrite(AdafruitBleResetPin, PinLevelHigh); + digitalWrite(AdafruitBleResetPin, PinLevelLow); + _delay_ms(10); + digitalWrite(AdafruitBleResetPin, PinLevelHigh); - _delay_ms(1000); // Give it a second to initialize + _delay_ms(1000); // Give it a second to initialize - state.initialized = true; - return state.initialized; + state.initialized = true; + return state.initialized; } -static inline uint8_t min(uint8_t a, uint8_t b) { - return a < b ? a : b; -} +static inline uint8_t min(uint8_t a, uint8_t b) { return a < b ? a : b; } static bool read_response(char *resp, uint16_t resplen, bool verbose) { - char *dest = resp; - char *end = dest + resplen; - - while (true) { - struct sdep_msg msg; - - if (!sdep_recv_pkt(&msg, 2 * SdepTimeout)) { - dprint("sdep_recv_pkt failed\n"); - return false; + char *dest = resp; + char *end = dest + resplen; + + while (true) { + struct sdep_msg msg; + + if (!sdep_recv_pkt(&msg, 2 * SdepTimeout)) { + dprint("sdep_recv_pkt failed\n"); + return false; + } + + if (msg.type != SdepResponse) { + *resp = 0; + return false; + } + + uint8_t len = min(msg.len, end - dest); + if (len > 0) { + memcpy(dest, msg.payload, len); + dest += len; + } + + if (!msg.more) { + // No more data is expected! + break; + } } - if (msg.type != SdepResponse) { - *resp = 0; - return false; - } + // Ensure the response is NUL terminated + *dest = 0; - uint8_t len = min(msg.len, end - dest); - if (len > 0) { - memcpy(dest, msg.payload, len); - dest += len; + // "Parse" the result text; we want to snip off the trailing OK or ERROR line + // Rewind past the possible trailing CRLF so that we can strip it + --dest; + while (dest > resp && (dest[0] == '\n' || dest[0] == '\r')) { + *dest = 0; + --dest; } - if (!msg.more) { - // No more data is expected! - break; + // Look back for start of preceeding line + char *last_line = strrchr(resp, '\n'); + if (last_line) { + ++last_line; + } else { + last_line = resp; } - } - - // Ensure the response is NUL terminated - *dest = 0; - // "Parse" the result text; we want to snip off the trailing OK or ERROR line - // Rewind past the possible trailing CRLF so that we can strip it - --dest; - while (dest > resp && (dest[0] == '\n' || dest[0] == '\r')) { - *dest = 0; - --dest; - } + bool success = false; + static const char kOK[] PROGMEM = "OK"; - // Look back for start of preceeding line - char *last_line = strrchr(resp, '\n'); - if (last_line) { - ++last_line; - } else { - last_line = resp; - } + success = !strcmp_P(last_line, kOK); - bool success = false; - static const char kOK[] PROGMEM = "OK"; + if (verbose || !success) { + dprintf("result: %s\n", resp); + } + return success; +} - success = !strcmp_P(last_line, kOK ); +static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout) { + const char * end = cmd + strlen(cmd); + struct sdep_msg msg; - if (verbose || !success) { - dprintf("result: %s\n", resp); - } - return success; -} + if (verbose) { + dprintf("ble send: %s\n", cmd); + } -static bool at_command(const char *cmd, char *resp, uint16_t resplen, - bool verbose, uint16_t timeout) { - const char *end = cmd + strlen(cmd); - struct sdep_msg msg; - - if (verbose) { - dprintf("ble send: %s\n", cmd); - } - - if (resp) { - // They want to decode the response, so we need to flush and wait - // for all pending I/O to finish before we start this one, so - // that we don't confuse the results - resp_buf_wait(cmd); - *resp = 0; - } - - // Fragment the command into a series of SDEP packets - while (end - cmd > SdepMaxPayload) { - sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, SdepMaxPayload, true); - if (!sdep_send_pkt(&msg, timeout)) { - return false; + if (resp) { + // They want to decode the response, so we need to flush and wait + // for all pending I/O to finish before we start this one, so + // that we don't confuse the results + resp_buf_wait(cmd); + *resp = 0; } - cmd += SdepMaxPayload; - } - sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, end - cmd, false); - if (!sdep_send_pkt(&msg, timeout)) { - return false; - } + // Fragment the command into a series of SDEP packets + while (end - cmd > SdepMaxPayload) { + sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, SdepMaxPayload, true); + if (!sdep_send_pkt(&msg, timeout)) { + return false; + } + cmd += SdepMaxPayload; + } - if (resp == NULL) { - auto now = timer_read(); - while (!resp_buf.enqueue(now)) { - resp_buf_read_one(false); + sdep_build_pkt(&msg, BleAtWrapper, (uint8_t *)cmd, end - cmd, false); + if (!sdep_send_pkt(&msg, timeout)) { + return false; } - auto later = timer_read(); - if (TIMER_DIFF_16(later, now) > 0) { - dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now)); + + if (resp == NULL) { + auto now = timer_read(); + while (!resp_buf.enqueue(now)) { + resp_buf_read_one(false); + } + auto later = timer_read(); + if (TIMER_DIFF_16(later, now) > 0) { + dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now)); + } + return true; } - return true; - } - return read_response(resp, resplen, verbose); + return read_response(resp, resplen, verbose); } bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose) { - auto cmdbuf = (char *)alloca(strlen_P(cmd) + 1); - strcpy_P(cmdbuf, cmd); - return at_command(cmdbuf, resp, resplen, verbose); + auto cmdbuf = (char *)alloca(strlen_P(cmd) + 1); + strcpy_P(cmdbuf, cmd); + return at_command(cmdbuf, resp, resplen, verbose); } -bool adafruit_ble_is_connected(void) { - return state.is_connected; -} +bool adafruit_ble_is_connected(void) { return state.is_connected; } bool adafruit_ble_enable_keyboard(void) { - char resbuf[128]; - - if (!state.initialized && !ble_init()) { - return false; - } - - state.configured = false; - - // Disable command echo - static const char kEcho[] PROGMEM = "ATE=0"; - // Make the advertised name match the keyboard - static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); - // Turn on keyboard support - static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; - - // Adjust intervals to improve latency. This causes the "central" - // system (computer/tablet) to poll us every 10-30 ms. We can't - // set a smaller value than 10ms, and 30ms seems to be the natural - // processing time on my macbook. Keeping it constrained to that - // feels reasonable to type to. - static const char kGapIntervals[] PROGMEM = "AT+GAPINTERVALS=10,30,,"; - - // Reset the device so that it picks up the above changes - static const char kATZ[] PROGMEM = "ATZ"; - - // Turn down the power level a bit - static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12"; - static PGM_P const configure_commands[] PROGMEM = { - kEcho, - kGapIntervals, - kGapDevName, - kHidEnOn, - kPower, - kATZ, - }; - - uint8_t i; - for (i = 0; i < sizeof(configure_commands) / sizeof(configure_commands[0]); - ++i) { - PGM_P cmd; - memcpy_P(&cmd, configure_commands + i, sizeof(cmd)); - - if (!at_command_P(cmd, resbuf, sizeof(resbuf))) { - dprintf("failed BLE command: %S: %s\n", cmd, resbuf); - goto fail; - } - } - - state.configured = true; - - // Check connection status in a little while; allow the ATZ time - // to kick in. - state.last_connection_update = timer_read(); + char resbuf[128]; + + if (!state.initialized && !ble_init()) { + return false; + } + + state.configured = false; + + // Disable command echo + static const char kEcho[] PROGMEM = "ATE=0"; + // Make the advertised name match the keyboard + static const char kGapDevName[] PROGMEM = "AT+GAPDEVNAME=" STR(PRODUCT); + // Turn on keyboard support + static const char kHidEnOn[] PROGMEM = "AT+BLEHIDEN=1"; + + // Adjust intervals to improve latency. This causes the "central" + // system (computer/tablet) to poll us every 10-30 ms. We can't + // set a smaller value than 10ms, and 30ms seems to be the natural + // processing time on my macbook. Keeping it constrained to that + // feels reasonable to type to. + static const char kGapIntervals[] PROGMEM = "AT+GAPINTERVALS=10,30,,"; + + // Reset the device so that it picks up the above changes + static const char kATZ[] PROGMEM = "ATZ"; + + // Turn down the power level a bit + static const char kPower[] PROGMEM = "AT+BLEPOWERLEVEL=-12"; + static PGM_P const configure_commands[] PROGMEM = { + kEcho, kGapIntervals, kGapDevName, kHidEnOn, kPower, kATZ, + }; + + uint8_t i; + for (i = 0; i < sizeof(configure_commands) / sizeof(configure_commands[0]); ++i) { + PGM_P cmd; + memcpy_P(&cmd, configure_commands + i, sizeof(cmd)); + + if (!at_command_P(cmd, resbuf, sizeof(resbuf))) { + dprintf("failed BLE command: %S: %s\n", cmd, resbuf); + goto fail; + } + } + + state.configured = true; + + // Check connection status in a little while; allow the ATZ time + // to kick in. + state.last_connection_update = timer_read(); fail: - return state.configured; + return state.configured; } static void set_connected(bool connected) { - if (connected != state.is_connected) { - if (connected) { - print("****** BLE CONNECT!!!!\n"); - } else { - print("****** BLE DISCONNECT!!!!\n"); - } - state.is_connected = connected; - - // TODO: if modifiers are down on the USB interface and - // we cut over to BLE or vice versa, they will remain stuck. - // This feels like a good point to do something like clearing - // the keyboard and/or generating a fake all keys up message. - // However, I've noticed that it takes a couple of seconds - // for macOS to to start recognizing key presses after BLE - // is in the connected state, so I worry that doing that - // here may not be good enough. - } + if (connected != state.is_connected) { + if (connected) { + print("****** BLE CONNECT!!!!\n"); + } else { + print("****** BLE DISCONNECT!!!!\n"); + } + state.is_connected = connected; + + // TODO: if modifiers are down on the USB interface and + // we cut over to BLE or vice versa, they will remain stuck. + // This feels like a good point to do something like clearing + // the keyboard and/or generating a fake all keys up message. + // However, I've noticed that it takes a couple of seconds + // for macOS to to start recognizing key presses after BLE + // is in the connected state, so I worry that doing that + // here may not be good enough. + } } void adafruit_ble_task(void) { - char resbuf[48]; - - if (!state.configured && !adafruit_ble_enable_keyboard()) { - return; - } - resp_buf_read_one(true); - send_buf_send_one(SdepShortTimeout); - - if (resp_buf.empty() && (state.event_flags & UsingEvents) && - digitalRead(AdafruitBleIRQPin)) { - // Must be an event update - if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) { - uint32_t mask = strtoul(resbuf, NULL, 16); - - if (mask & BleSystemConnected) { - set_connected(true); - } else if (mask & BleSystemDisconnected) { - set_connected(false); - } - } - } - - if (timer_elapsed(state.last_connection_update) > ConnectionUpdateInterval) { - bool shouldPoll = true; - if (!(state.event_flags & ProbedEvents)) { - // Request notifications about connection status changes. - // This only works in SPIFRIEND firmware > 0.6.7, which is why - // we check for this conditionally here. - // Note that at the time of writing, HID reports only work correctly - // with Apple products on firmware version 0.6.7! - // https://forums.adafruit.com/viewtopic.php?f=8&t=104052 - if (at_command_P(PSTR("AT+EVENTENABLE=0x1"), resbuf, sizeof(resbuf))) { - at_command_P(PSTR("AT+EVENTENABLE=0x2"), resbuf, sizeof(resbuf)); - state.event_flags |= UsingEvents; - } - state.event_flags |= ProbedEvents; - - // leave shouldPoll == true so that we check at least once - // before relying solely on events - } else { - shouldPoll = false; - } + char resbuf[48]; - static const char kGetConn[] PROGMEM = "AT+GAPGETCONN"; - state.last_connection_update = timer_read(); + if (!state.configured && !adafruit_ble_enable_keyboard()) { + return; + } + resp_buf_read_one(true); + send_buf_send_one(SdepShortTimeout); + + if (resp_buf.empty() && (state.event_flags & UsingEvents) && digitalRead(AdafruitBleIRQPin)) { + // Must be an event update + if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) { + uint32_t mask = strtoul(resbuf, NULL, 16); + + if (mask & BleSystemConnected) { + set_connected(true); + } else if (mask & BleSystemDisconnected) { + set_connected(false); + } + } + } - if (at_command_P(kGetConn, resbuf, sizeof(resbuf))) { - set_connected(atoi(resbuf)); + if (timer_elapsed(state.last_connection_update) > ConnectionUpdateInterval) { + bool shouldPoll = true; + if (!(state.event_flags & ProbedEvents)) { + // Request notifications about connection status changes. + // This only works in SPIFRIEND firmware > 0.6.7, which is why + // we check for this conditionally here. + // Note that at the time of writing, HID reports only work correctly + // with Apple products on firmware version 0.6.7! + // https://forums.adafruit.com/viewtopic.php?f=8&t=104052 + if (at_command_P(PSTR("AT+EVENTENABLE=0x1"), resbuf, sizeof(resbuf))) { + at_command_P(PSTR("AT+EVENTENABLE=0x2"), resbuf, sizeof(resbuf)); + state.event_flags |= UsingEvents; + } + state.event_flags |= ProbedEvents; + + // leave shouldPoll == true so that we check at least once + // before relying solely on events + } else { + shouldPoll = false; + } + + static const char kGetConn[] PROGMEM = "AT+GAPGETCONN"; + state.last_connection_update = timer_read(); + + if (at_command_P(kGetConn, resbuf, sizeof(resbuf))) { + set_connected(atoi(resbuf)); + } } - } #ifdef SAMPLE_BATTERY - // I don't know if this really does anything useful yet; the reported - // voltage level always seems to be around 3200mV. We may want to just rip - // this code out. - if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && - resp_buf.empty()) { - state.last_battery_update = timer_read(); - - if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) { - state.vbat = atoi(resbuf); + // I don't know if this really does anything useful yet; the reported + // voltage level always seems to be around 3200mV. We may want to just rip + // this code out. + if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) { + state.last_battery_update = timer_read(); + + if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) { + state.vbat = atoi(resbuf); + } } - } #endif } static bool process_queue_item(struct queue_item *item, uint16_t timeout) { - char cmdbuf[48]; - char fmtbuf[64]; + char cmdbuf[48]; + char fmtbuf[64]; - // Arrange to re-check connection after keys have settled - state.last_connection_update = timer_read(); + // Arrange to re-check connection after keys have settled + state.last_connection_update = timer_read(); #if 1 - if (TIMER_DIFF_16(state.last_connection_update, item->added) > 0) { - dprintf("send latency %dms\n", - TIMER_DIFF_16(state.last_connection_update, item->added)); - } + if (TIMER_DIFF_16(state.last_connection_update, item->added) > 0) { + dprintf("send latency %dms\n", TIMER_DIFF_16(state.last_connection_update, item->added)); + } #endif - switch (item->queue_type) { - case QTKeyReport: - strcpy_P(fmtbuf, - PSTR("AT+BLEKEYBOARDCODE=%02x-00-%02x-%02x-%02x-%02x-%02x-%02x")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, - item->key.keys[0], item->key.keys[1], item->key.keys[2], - item->key.keys[3], item->key.keys[4], item->key.keys[5]); - return at_command(cmdbuf, NULL, 0, true, timeout); + switch (item->queue_type) { + case QTKeyReport: + strcpy_P(fmtbuf, PSTR("AT+BLEKEYBOARDCODE=%02x-00-%02x-%02x-%02x-%02x-%02x-%02x")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->key.modifier, item->key.keys[0], item->key.keys[1], item->key.keys[2], item->key.keys[3], item->key.keys[4], item->key.keys[5]); + return at_command(cmdbuf, NULL, 0, true, timeout); - case QTConsumer: - strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer); - return at_command(cmdbuf, NULL, 0, true, timeout); + case QTConsumer: + strcpy_P(fmtbuf, PSTR("AT+BLEHIDCONTROLKEY=0x%04x")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->consumer); + return at_command(cmdbuf, NULL, 0, true, timeout); #ifdef MOUSE_ENABLE - case QTMouseMove: - strcpy_P(fmtbuf, PSTR("AT+BLEHIDMOUSEMOVE=%d,%d,%d,%d")); - snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->mousemove.x, - item->mousemove.y, item->mousemove.scroll, item->mousemove.pan); - if (!at_command(cmdbuf, NULL, 0, true, timeout)) { - return false; - } - strcpy_P(cmdbuf, PSTR("AT+BLEHIDMOUSEBUTTON=")); - if (item->mousemove.buttons & MOUSE_BTN1) { - strcat(cmdbuf, "L"); - } - if (item->mousemove.buttons & MOUSE_BTN2) { - strcat(cmdbuf, "R"); - } - if (item->mousemove.buttons & MOUSE_BTN3) { - strcat(cmdbuf, "M"); - } - if (item->mousemove.buttons == 0) { - strcat(cmdbuf, "0"); - } - return at_command(cmdbuf, NULL, 0, true, timeout); + case QTMouseMove: + strcpy_P(fmtbuf, PSTR("AT+BLEHIDMOUSEMOVE=%d,%d,%d,%d")); + snprintf(cmdbuf, sizeof(cmdbuf), fmtbuf, item->mousemove.x, item->mousemove.y, item->mousemove.scroll, item->mousemove.pan); + if (!at_command(cmdbuf, NULL, 0, true, timeout)) { + return false; + } + strcpy_P(cmdbuf, PSTR("AT+BLEHIDMOUSEBUTTON=")); + if (item->mousemove.buttons & MOUSE_BTN1) { + strcat(cmdbuf, "L"); + } + if (item->mousemove.buttons & MOUSE_BTN2) { + strcat(cmdbuf, "R"); + } + if (item->mousemove.buttons & MOUSE_BTN3) { + strcat(cmdbuf, "M"); + } + if (item->mousemove.buttons == 0) { + strcat(cmdbuf, "0"); + } + return at_command(cmdbuf, NULL, 0, true, timeout); #endif - default: - return true; - } -} - -bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, - uint8_t nkeys) { - struct queue_item item; - bool didWait = false; - - item.queue_type = QTKeyReport; - item.key.modifier = hid_modifier_mask; - item.added = timer_read(); - - while (nkeys >= 0) { - item.key.keys[0] = keys[0]; - item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; - item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; - item.key.keys[3] = nkeys >= 3 ? keys[3] : 0; - item.key.keys[4] = nkeys >= 4 ? keys[4] : 0; - item.key.keys[5] = nkeys >= 5 ? keys[5] : 0; - - if (!send_buf.enqueue(item)) { - if (!didWait) { - dprint("wait for buf space\n"); - didWait = true; - } - send_buf_send_one(); - continue; + default: + return true; } +} - if (nkeys <= 6) { - return true; +bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { + struct queue_item item; + bool didWait = false; + + item.queue_type = QTKeyReport; + item.key.modifier = hid_modifier_mask; + item.added = timer_read(); + + while (nkeys >= 0) { + item.key.keys[0] = keys[0]; + item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; + item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; + item.key.keys[3] = nkeys >= 3 ? keys[3] : 0; + item.key.keys[4] = nkeys >= 4 ? keys[4] : 0; + item.key.keys[5] = nkeys >= 5 ? keys[5] : 0; + + if (!send_buf.enqueue(item)) { + if (!didWait) { + dprint("wait for buf space\n"); + didWait = true; + } + send_buf_send_one(); + continue; + } + + if (nkeys <= 6) { + return true; + } + + nkeys -= 6; + keys += 6; } - nkeys -= 6; - keys += 6; - } - - return true; + return true; } bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration) { - struct queue_item item; + struct queue_item item; - item.queue_type = QTConsumer; - item.consumer = keycode; + item.queue_type = QTConsumer; + item.consumer = keycode; - while (!send_buf.enqueue(item)) { - send_buf_send_one(); - } - return true; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } + return true; } #ifdef MOUSE_ENABLE -bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, - int8_t pan, uint8_t buttons) { - struct queue_item item; - - item.queue_type = QTMouseMove; - item.mousemove.x = x; - item.mousemove.y = y; - item.mousemove.scroll = scroll; - item.mousemove.pan = pan; - item.mousemove.buttons = buttons; - - while (!send_buf.enqueue(item)) { - send_buf_send_one(); - } - return true; +bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { + struct queue_item item; + + item.queue_type = QTMouseMove; + item.mousemove.x = x; + item.mousemove.y = y; + item.mousemove.scroll = scroll; + item.mousemove.pan = pan; + item.mousemove.buttons = buttons; + + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } + return true; } #endif -uint32_t adafruit_ble_read_battery_voltage(void) { - return state.vbat; -} +uint32_t adafruit_ble_read_battery_voltage(void) { return state.vbat; } bool adafruit_ble_set_mode_leds(bool on) { - if (!state.configured) { - return false; - } - - // The "mode" led is the red blinky one - at_command_P(on ? PSTR("AT+HWMODELED=1") : PSTR("AT+HWMODELED=0"), NULL, 0); - - // Pin 19 is the blue "connected" LED; turn that off too. - // When turning LEDs back on, don't turn that LED on if we're - // not connected, as that would be confusing. - at_command_P(on && state.is_connected ? PSTR("AT+HWGPIO=19,1") - : PSTR("AT+HWGPIO=19,0"), - NULL, 0); - return true; + if (!state.configured) { + return false; + } + + // The "mode" led is the red blinky one + at_command_P(on ? PSTR("AT+HWMODELED=1") : PSTR("AT+HWMODELED=0"), NULL, 0); + + // Pin 19 is the blue "connected" LED; turn that off too. + // When turning LEDs back on, don't turn that LED on if we're + // not connected, as that would be confusing. + at_command_P(on && state.is_connected ? PSTR("AT+HWGPIO=19,1") : PSTR("AT+HWGPIO=19,0"), NULL, 0); + return true; } // https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le/ble-generic#at-plus-blepowerlevel bool adafruit_ble_set_power_level(int8_t level) { - char cmd[46]; - if (!state.configured) { - return false; - } - snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level); - return at_command(cmd, NULL, 0, false); + char cmd[46]; + if (!state.configured) { + return false; + } + snprintf(cmd, sizeof(cmd), "AT+BLEPOWERLEVEL=%d", level); + return at_command(cmd, NULL, 0, false); } diff --git a/tmk_core/protocol/lufa/adafruit_ble.h b/tmk_core/protocol/lufa/adafruit_ble.h index 5d26a9d5a3..cef46fe9f7 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.h +++ b/tmk_core/protocol/lufa/adafruit_ble.h @@ -4,16 +4,16 @@ */ #pragma once #ifdef MODULE_ADAFRUIT_BLE -#include -#include -#include +# include +# include +# include -#include "config_common.h" -#include "progmem.h" +# include "config_common.h" +# include "progmem.h" -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif /* Instruct the module to enable HID keyboard support and reset */ extern bool adafruit_ble_enable_keyboard(void); @@ -34,20 +34,18 @@ extern void adafruit_ble_task(void); * this set of keys. * Also sends a key release indicator, so that the keys do not remain * held down. */ -extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, - uint8_t nkeys); +extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); /* Send a consumer keycode, holding it down for the specified duration * (milliseconds) */ extern bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration); -#ifdef MOUSE_ENABLE +# ifdef MOUSE_ENABLE /* Send a mouse/wheel movement report. * The parameters are signed and indicate positive of negative direction * change. */ -extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, - int8_t pan, uint8_t buttons); -#endif +extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); +# endif /* Compute battery voltage by reading an analog pin. * Returns the integer number of millivolts */ @@ -56,8 +54,8 @@ extern uint32_t adafruit_ble_read_battery_voltage(void); extern bool adafruit_ble_set_mode_leds(bool on); extern bool adafruit_ble_set_power_level(int8_t level); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif -#endif // MODULE_ADAFRUIT_BLE +#endif // MODULE_ADAFRUIT_BLE diff --git a/tmk_core/protocol/lufa/bluetooth.c b/tmk_core/protocol/lufa/bluetooth.c index 549606162d..5eb52860b1 100644 --- a/tmk_core/protocol/lufa/bluetooth.c +++ b/tmk_core/protocol/lufa/bluetooth.c @@ -21,16 +21,18 @@ along with this program. If not, see . #include "debug.h" #include "bluetooth.h" -void bluefruit_keyboard_print_report(report_keyboard_t *report) -{ +void bluefruit_keyboard_print_report(report_keyboard_t *report) { if (!debug_keyboard) return; - dprintf("keys: "); for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { debug_hex8(report->keys[i]); dprintf(" "); } - dprintf(" mods: "); debug_hex8(report->mods); - dprintf(" reserved: "); debug_hex8(report->reserved); + dprintf("keys: "); + for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + debug_hex8(report->keys[i]); + dprintf(" "); + } + dprintf(" mods: "); + debug_hex8(report->mods); + dprintf(" reserved: "); + debug_hex8(report->reserved); dprintf("\n"); } -void bluefruit_serial_send(uint8_t data) -{ - serial_send(data); -} \ No newline at end of file +void bluefruit_serial_send(uint8_t data) { serial_send(data); } \ No newline at end of file diff --git a/tmk_core/protocol/lufa/bluetooth.h b/tmk_core/protocol/lufa/bluetooth.h index f4b2f6f8b1..081271a4e6 100644 --- a/tmk_core/protocol/lufa/bluetooth.h +++ b/tmk_core/protocol/lufa/bluetooth.h @@ -41,39 +41,8 @@ void bluefruit_serial_send(uint8_t data); | 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))))))))))))))))))) +#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))))))))))))))))))) -#define CONSUMER2RN42(usage) \ - (usage == AUDIO_MUTE ? 0x0040 : \ - (usage == AUDIO_VOL_UP ? 0x0010 : \ - (usage == AUDIO_VOL_DOWN ? 0x0020 : \ - (usage == TRANSPORT_NEXT_TRACK ? 0x0100 : \ - (usage == TRANSPORT_PREV_TRACK ? 0x0200 : \ - (usage == TRANSPORT_STOP ? 0x0400 : \ - (usage == TRANSPORT_STOP_EJECT ? 0x0800 : \ - (usage == TRANSPORT_PLAY_PAUSE ? 0x0080 : \ - (usage == AL_EMAIL ? 0x0200 : \ - (usage == AL_LOCAL_BROWSER ? 0x8000 : \ - (usage == AC_SEARCH ? 0x0400 : \ - (usage == AC_HOME ? 0x0100 : 0)))))))))))) +#define CONSUMER2RN42(usage) (usage == AUDIO_MUTE ? 0x0040 : (usage == AUDIO_VOL_UP ? 0x0010 : (usage == AUDIO_VOL_DOWN ? 0x0020 : (usage == TRANSPORT_NEXT_TRACK ? 0x0100 : (usage == TRANSPORT_PREV_TRACK ? 0x0200 : (usage == TRANSPORT_STOP ? 0x0400 : (usage == TRANSPORT_STOP_EJECT ? 0x0800 : (usage == TRANSPORT_PLAY_PAUSE ? 0x0080 : (usage == AL_EMAIL ? 0x0200 : (usage == AL_LOCAL_BROWSER ? 0x8000 : (usage == AC_SEARCH ? 0x0400 : (usage == AC_HOME ? 0x0100 : 0)))))))))))) - #endif +#endif diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index f2ecf24651..12a6924fd0 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -45,7 +45,7 @@ #include "sendchar.h" #include "debug.h" #ifdef SLEEP_LED_ENABLE -#include "sleep_led.h" +# include "sleep_led.h" #endif #include "suspend.h" @@ -57,86 +57,80 @@ #include "rgblight_reconfig.h" #ifdef NKRO_ENABLE - #include "keycode_config.h" +# include "keycode_config.h" - extern keymap_config_t keymap_config; +extern keymap_config_t keymap_config; #endif - #ifdef AUDIO_ENABLE - #include +# include #endif #ifdef BLUETOOTH_ENABLE - #ifdef MODULE_ADAFRUIT_BLE - #include "adafruit_ble.h" - #else - #include "bluetooth.h" - #endif +# ifdef MODULE_ADAFRUIT_BLE +# include "adafruit_ble.h" +# else +# include "bluetooth.h" +# endif #endif #ifdef VIRTSER_ENABLE - #include "virtser.h" +# include "virtser.h" #endif #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE) - #include "rgblight.h" +# include "rgblight.h" #endif #ifdef MIDI_ENABLE - #include "qmk_midi.h" +# include "qmk_midi.h" #endif #ifdef RAW_ENABLE - #include "raw_hid.h" +# include "raw_hid.h" #endif uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ -uint8_t keyboard_protocol = 1; +uint8_t keyboard_protocol = 1; static uint8_t keyboard_led_stats = 0; static report_keyboard_t keyboard_report_sent; /* Host driver */ static uint8_t keyboard_leds(void); -static void send_keyboard(report_keyboard_t *report); -static void send_mouse(report_mouse_t *report); -static void send_system(uint16_t data); -static void send_consumer(uint16_t data); -host_driver_t lufa_driver = { - keyboard_leds, - send_keyboard, - send_mouse, - send_system, - send_consumer, +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); +host_driver_t lufa_driver = { + keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer, }; #ifdef VIRTSER_ENABLE -USB_ClassInfo_CDC_Device_t cdc_device = -{ - .Config = - { - .ControlInterfaceNumber = CCI_INTERFACE, - .DataINEndpoint = - { - .Address = CDC_IN_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = CDC_OUT_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .NotificationEndpoint = - { - .Address = CDC_NOTIFICATION_EPADDR, - .Size = CDC_NOTIFICATION_EPSIZE, - .Banks = 1, - }, - }, +USB_ClassInfo_CDC_Device_t cdc_device = { + .Config = + { + .ControlInterfaceNumber = CCI_INTERFACE, + .DataINEndpoint = + { + .Address = CDC_IN_EPADDR, + .Size = CDC_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = CDC_OUT_EPADDR, + .Size = CDC_EPSIZE, + .Banks = 1, + }, + .NotificationEndpoint = + { + .Address = CDC_NOTIFICATION_EPADDR, + .Size = CDC_NOTIFICATION_EPSIZE, + .Banks = 1, + }, + }, }; #endif @@ -146,84 +140,73 @@ USB_ClassInfo_CDC_Device_t cdc_device = * * FIXME: Needs doc */ -void raw_hid_send( uint8_t *data, uint8_t length ) -{ - // TODO: implement variable size packet - if ( length != RAW_EPSIZE ) - { - return; - } - - if (USB_DeviceState != DEVICE_STATE_Configured) - { - return; - } - - // TODO: decide if we allow calls to raw_hid_send() in the middle - // of other endpoint usage. - uint8_t ep = Endpoint_GetCurrentEndpoint(); - - Endpoint_SelectEndpoint(RAW_IN_EPNUM); - - // Check to see if the host is ready to accept another packet - if (Endpoint_IsINReady()) - { - // Write data - Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL); - // Finalize the stream transfer to send the last packet - Endpoint_ClearIN(); - } - - Endpoint_SelectEndpoint(ep); +void raw_hid_send(uint8_t *data, uint8_t length) { + // TODO: implement variable size packet + if (length != RAW_EPSIZE) { + return; + } + + if (USB_DeviceState != DEVICE_STATE_Configured) { + return; + } + + // TODO: decide if we allow calls to raw_hid_send() in the middle + // of other endpoint usage. + uint8_t ep = Endpoint_GetCurrentEndpoint(); + + Endpoint_SelectEndpoint(RAW_IN_EPNUM); + + // Check to see if the host is ready to accept another packet + if (Endpoint_IsINReady()) { + // Write data + Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL); + // Finalize the stream transfer to send the last packet + Endpoint_ClearIN(); + } + + Endpoint_SelectEndpoint(ep); } /** \brief Raw HID Receive * * FIXME: Needs doc */ -__attribute__ ((weak)) -void raw_hid_receive( uint8_t *data, uint8_t length ) -{ - // Users should #include "raw_hid.h" in their own code - // and implement this function there. Leave this as weak linkage - // so users can opt to not handle data coming in. +__attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { + // Users should #include "raw_hid.h" in their own code + // and implement this function there. Leave this as weak linkage + // so users can opt to not handle data coming in. } /** \brief Raw HID Task * * FIXME: Needs doc */ -static void raw_hid_task(void) -{ - // Create a temporary buffer to hold the read in data from the host - uint8_t data[RAW_EPSIZE]; - bool data_read = false; - - // Device must be connected and configured for the task to run - if (USB_DeviceState != DEVICE_STATE_Configured) - return; - - Endpoint_SelectEndpoint(RAW_OUT_EPNUM); - - // Check to see if a packet has been sent from the host - if (Endpoint_IsOUTReceived()) - { - // Check to see if the packet contains data - if (Endpoint_IsReadWriteAllowed()) - { - /* Read data */ - Endpoint_Read_Stream_LE(data, sizeof(data), NULL); - data_read = true; - } - - // Finalize the stream transfer to receive the last packet - Endpoint_ClearOUT(); - - if ( data_read ) - { - raw_hid_receive( data, sizeof(data) ); - } - } +static void raw_hid_task(void) { + // Create a temporary buffer to hold the read in data from the host + uint8_t data[RAW_EPSIZE]; + bool data_read = false; + + // Device must be connected and configured for the task to run + if (USB_DeviceState != DEVICE_STATE_Configured) return; + + Endpoint_SelectEndpoint(RAW_OUT_EPNUM); + + // Check to see if a packet has been sent from the host + if (Endpoint_IsOUTReceived()) { + // Check to see if the packet contains data + if (Endpoint_IsReadWriteAllowed()) { + /* Read data */ + Endpoint_Read_Stream_LE(data, sizeof(data), NULL); + data_read = true; + } + + // Finalize the stream transfer to receive the last packet + Endpoint_ClearOUT(); + + if (data_read) { + raw_hid_receive(data, sizeof(data)); + } + } } #endif @@ -235,15 +218,13 @@ static void raw_hid_task(void) * * FIXME: Needs doc */ -static void Console_Task(void) -{ +static void Console_Task(void) { /* Device must be connected and configured for the task to run */ - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if (USB_DeviceState != DEVICE_STATE_Configured) return; uint8_t ep = Endpoint_GetCurrentEndpoint(); -#if 0 +# if 0 // TODO: impl receivechar()/recvchar() Endpoint_SelectEndpoint(CONSOLE_OUT_EPNUM); @@ -266,7 +247,7 @@ static void Console_Task(void) /* Finalize the stream transfer to send the last packet */ Endpoint_ClearOUT(); } -#endif +# endif /* IN packet */ Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); @@ -276,8 +257,7 @@ static void Console_Task(void) } // fill empty bank - while (Endpoint_IsReadWriteAllowed()) - Endpoint_Write_8(0); + while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0); // flash senchar packet if (Endpoint_IsINReady()) { @@ -288,7 +268,6 @@ static void Console_Task(void) } #endif - /******************************************************************************* * USB Events ******************************************************************************/ @@ -298,13 +277,12 @@ static void Console_Task(void) * 1) EVENT_USB_Device_Suspend * 2) EVENT_USB_Device_Reset * 3) EVENT_USB_Device_Wake -*/ + */ /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Connect(void) -{ +void EVENT_USB_Device_Connect(void) { print("[C]"); /* For battery powered device */ if (!USB_IsInitialized) { @@ -318,35 +296,30 @@ void EVENT_USB_Device_Connect(void) * * FIXME: Needs doc */ -void EVENT_USB_Device_Disconnect(void) -{ +void EVENT_USB_Device_Disconnect(void) { print("[D]"); /* For battery powered device */ USB_IsInitialized = false; -/* TODO: This doesn't work. After several plug in/outs can not be enumerated. - if (USB_IsInitialized) { - USB_Disable(); // Disable all interrupts - USB_Controller_Enable(); - USB_INT_Enable(USB_INT_VBUSTI); - } -*/ + /* TODO: This doesn't work. After several plug in/outs can not be enumerated. + if (USB_IsInitialized) { + USB_Disable(); // Disable all interrupts + USB_Controller_Enable(); + USB_INT_Enable(USB_INT_VBUSTI); + } + */ } /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Reset(void) -{ - print("[R]"); -} +void EVENT_USB_Device_Reset(void) { print("[R]"); } /** \brief Event USB Device Connect * * FIXME: Needs doc */ -void EVENT_USB_Device_Suspend() -{ +void EVENT_USB_Device_Suspend() { print("[S]"); #ifdef SLEEP_LED_ENABLE sleep_led_enable(); @@ -357,8 +330,7 @@ void EVENT_USB_Device_Suspend() * * FIXME: Needs doc */ -void EVENT_USB_Device_WakeUp() -{ +void EVENT_USB_Device_WakeUp() { print("[W]"); suspend_wakeup_init(); @@ -369,23 +341,19 @@ void EVENT_USB_Device_WakeUp() #endif } - - #ifdef CONSOLE_ENABLE static bool console_flush = false; -#define CONSOLE_FLUSH_SET(b) do { \ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\ - console_flush = b; \ - } \ -} while (0) +# define CONSOLE_FLUSH_SET(b) \ + do { \ + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { console_flush = b; } \ + } while (0) /** \brief Event USB Device Start Of Frame * * FIXME: Needs doc * called every 1ms */ -void EVENT_USB_Device_StartOfFrame(void) -{ +void EVENT_USB_Device_StartOfFrame(void) { static uint8_t count; if (++count % 50) return; count = 0; @@ -404,44 +372,37 @@ void EVENT_USB_Device_StartOfFrame(void) * ATMega32u2 supports dual bank(ping-pong mode) only on endpoint 3 and 4, * it is safe to use single bank for all endpoints. */ -void EVENT_USB_Device_ConfigurationChanged(void) -{ +void EVENT_USB_Device_ConfigurationChanged(void) { bool ConfigSuccess = true; /* Setup Keyboard HID Report Endpoints */ #ifndef KEYBOARD_SHARED_EP - ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) /* Setup Mouse HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef SHARED_EP_ENABLE /* Setup Shared HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - SHARED_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef RAW_ENABLE /* Setup Raw HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - RAW_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, - RAW_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); #endif #ifdef CONSOLE_ENABLE /* Setup Console HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, - CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); -#if 0 + ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); +# if 0 ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); -#endif +# endif #endif #ifdef MIDI_ENABLE @@ -473,26 +434,23 @@ Other Device Required Optional Optional Optional Optional Opti * * This is fired before passing along unhandled control requests to the library for processing internally. */ -void EVENT_USB_Device_ControlRequest(void) -{ - uint8_t* ReportData = NULL; +void EVENT_USB_Device_ControlRequest(void) { + uint8_t *ReportData = NULL; uint8_t ReportSize = 0; /* Handle HID Class specific requests */ - switch (USB_ControlRequest.bRequest) - { + switch (USB_ControlRequest.bRequest) { case HID_REQ_GetReport: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); // Interface switch (USB_ControlRequest.wIndex) { - case KEYBOARD_INTERFACE: - // TODO: test/check - ReportData = (uint8_t*)&keyboard_report_sent; - ReportSize = sizeof(keyboard_report_sent); - break; + case KEYBOARD_INTERFACE: + // TODO: test/check + ReportData = (uint8_t *)&keyboard_report_sent; + ReportSize = sizeof(keyboard_report_sent); + break; } /* Write the report data to the control endpoint */ @@ -502,47 +460,43 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetReport: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { - + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { // Interface switch (USB_ControlRequest.wIndex) { - case KEYBOARD_INTERFACE: + case KEYBOARD_INTERFACE: #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP) - case SHARED_INTERFACE: + case SHARED_INTERFACE: #endif - Endpoint_ClearSETUP(); + Endpoint_ClearSETUP(); - while (!(Endpoint_IsOUTReceived())) { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + while (!(Endpoint_IsOUTReceived())) { + if (USB_DeviceState == DEVICE_STATE_Unattached) return; + } - if (Endpoint_BytesInEndpoint() == 2) { - uint8_t report_id = Endpoint_Read_8(); + if (Endpoint_BytesInEndpoint() == 2) { + uint8_t report_id = Endpoint_Read_8(); - if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { - keyboard_led_stats = Endpoint_Read_8(); - } - } else { - keyboard_led_stats = Endpoint_Read_8(); - } + if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { + keyboard_led_stats = Endpoint_Read_8(); + } + } else { + keyboard_led_stats = Endpoint_Read_8(); + } - Endpoint_ClearOUT(); - Endpoint_ClearStatusStage(); - break; + Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); + break; } - } break; case HID_REQ_GetProtocol: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { Endpoint_ClearSETUP(); - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_Write_8(keyboard_protocol); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); @@ -551,8 +505,7 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetProtocol: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { if (USB_ControlRequest.wIndex == KEYBOARD_INTERFACE) { Endpoint_ClearSETUP(); Endpoint_ClearStatusStage(); @@ -564,8 +517,7 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_SetIdle: - if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); Endpoint_ClearStatusStage(); @@ -574,10 +526,10 @@ void EVENT_USB_Device_ControlRequest(void) break; case HID_REQ_GetIdle: - if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_Write_8(keyboard_idle); Endpoint_ClearIN(); Endpoint_ClearStatusStage(); @@ -598,54 +550,50 @@ void EVENT_USB_Device_ControlRequest(void) * * FIXME: Needs doc */ -static uint8_t keyboard_leds(void) -{ - return keyboard_led_stats; -} +static uint8_t keyboard_leds(void) { return keyboard_led_stats; } /** \brief Send Keyboard * * FIXME: Needs doc */ -static void send_keyboard(report_keyboard_t *report) -{ +static void send_keyboard(report_keyboard_t *report) { uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); #ifdef BLUETOOTH_ENABLE - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE - adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); - #elif MODULE_RN42 - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x09); - bluefruit_serial_send(0x01); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); - } - #else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); - } - #endif - } + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { +# ifdef MODULE_ADAFRUIT_BLE + adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); +# elif MODULE_RN42 + bluefruit_serial_send(0xFD); + bluefruit_serial_send(0x09); + bluefruit_serial_send(0x01); + bluefruit_serial_send(report->mods); + bluefruit_serial_send(report->reserved); + for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + bluefruit_serial_send(report->keys[i]); + } +# else + bluefruit_serial_send(0xFD); + bluefruit_serial_send(report->mods); + bluefruit_serial_send(report->reserved); + for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { + bluefruit_serial_send(report->keys[i]); + } +# endif + } #endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } /* Select the Keyboard Report Endpoint */ - uint8_t ep = KEYBOARD_IN_EPNUM; + uint8_t ep = KEYBOARD_IN_EPNUM; uint8_t size = KEYBOARD_REPORT_SIZE; #ifdef NKRO_ENABLE if (keyboard_protocol && keymap_config.nkro) { - ep = SHARED_IN_EPNUM; + ep = SHARED_IN_EPNUM; size = sizeof(struct nkro_report); } #endif @@ -666,38 +614,37 @@ static void send_keyboard(report_keyboard_t *report) keyboard_report_sent = *report; } - + /** \brief Send Mouse * * FIXME: Needs doc */ -static void send_mouse(report_mouse_t *report) -{ +static void send_mouse(report_mouse_t *report) { #ifdef MOUSE_ENABLE uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); -#ifdef BLUETOOTH_ENABLE - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE - // FIXME: mouse buttons - adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); - #else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x03); - bluefruit_serial_send(report->buttons); - bluefruit_serial_send(report->x); - bluefruit_serial_send(report->y); - bluefruit_serial_send(report->v); // should try sending the wheel v here - bluefruit_serial_send(report->h); // should try sending the wheel h here - bluefruit_serial_send(0x00); - #endif - } -#endif +# ifdef BLUETOOTH_ENABLE + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { +# ifdef MODULE_ADAFRUIT_BLE + // FIXME: mouse buttons + adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); +# else + bluefruit_serial_send(0xFD); + bluefruit_serial_send(0x00); + bluefruit_serial_send(0x03); + bluefruit_serial_send(report->buttons); + bluefruit_serial_send(report->x); + bluefruit_serial_send(report->y); + bluefruit_serial_send(report->v); // should try sending the wheel v here + bluefruit_serial_send(report->h); // should try sending the wheel h here + bluefruit_serial_send(0x00); +# endif + } +# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } /* Select the Mouse Report Endpoint */ @@ -719,18 +666,13 @@ static void send_mouse(report_mouse_t *report) * * FIXME: Needs doc */ -static void send_system(uint16_t data) -{ +static void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if (USB_DeviceState != DEVICE_STATE_Configured) return; - report_extra_t r = { - .report_id = REPORT_ID_SYSTEM, - .usage = data - SYSTEM_POWER_DOWN + 1 - }; + report_extra_t r = {.report_id = REPORT_ID_SYSTEM, .usage = data - SYSTEM_POWER_DOWN + 1}; Endpoint_SelectEndpoint(SHARED_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ @@ -746,52 +688,48 @@ static void send_system(uint16_t data) * * FIXME: Needs doc */ -static void send_consumer(uint16_t data) -{ +static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE uint8_t timeout = 255; - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); -#ifdef BLUETOOTH_ENABLE +# ifdef BLUETOOTH_ENABLE if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { - #ifdef MODULE_ADAFRUIT_BLE +# ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_consumer_key(data, 0); - #elif MODULE_RN42 +# elif MODULE_RN42 static uint16_t last_data = 0; if (data == last_data) return; - last_data = data; + last_data = data; uint16_t bitmap = CONSUMER2RN42(data); bluefruit_serial_send(0xFD); bluefruit_serial_send(0x03); bluefruit_serial_send(0x03); - bluefruit_serial_send(bitmap&0xFF); - bluefruit_serial_send((bitmap>>8)&0xFF); - #else + bluefruit_serial_send(bitmap & 0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); +# else static uint16_t last_data = 0; if (data == last_data) return; - last_data = data; + last_data = data; uint16_t bitmap = CONSUMER2BLUEFRUIT(data); bluefruit_serial_send(0xFD); bluefruit_serial_send(0x00); bluefruit_serial_send(0x02); - bluefruit_serial_send((bitmap>>8)&0xFF); - bluefruit_serial_send(bitmap&0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); + bluefruit_serial_send(bitmap & 0xFF); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); - #endif +# endif } -#endif +# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { - return; + return; } - report_extra_t r = { - .report_id = REPORT_ID_CONSUMER, - .usage = data - }; + report_extra_t r = {.report_id = REPORT_ID_CONSUMER, .usage = data}; Endpoint_SelectEndpoint(SHARED_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ @@ -803,18 +741,16 @@ static void send_consumer(uint16_t data) #endif } - /******************************************************************************* * sendchar ******************************************************************************/ #ifdef CONSOLE_ENABLE -#define SEND_TIMEOUT 5 +# define SEND_TIMEOUT 5 /** \brief Send Char * * FIXME: Needs doc */ -int8_t sendchar(uint8_t c) -{ +int8_t sendchar(uint8_t c) { // Not wait once timeouted. // Because sendchar() is called so many times, waiting each call causes big lag. static bool timeouted = false; @@ -823,8 +759,7 @@ int8_t sendchar(uint8_t c) // or char will be lost. These two function is mutually exclusive. CONSOLE_FLUSH_SET(false); - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; + if (USB_DeviceState != DEVICE_STATE_Configured) return -1; uint8_t ep = Endpoint_GetCurrentEndpoint(); Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM); @@ -857,7 +792,8 @@ int8_t sendchar(uint8_t c) // send when bank is full if (!Endpoint_IsReadWriteAllowed()) { - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + ; Endpoint_ClearIN(); } else { CONSOLE_FLUSH_SET(true); @@ -870,10 +806,7 @@ ERROR_EXIT: return -1; } #else -int8_t sendchar(uint8_t c) -{ - return 0; -} +int8_t sendchar(uint8_t c) { return 0; } #endif /******************************************************************************* @@ -881,33 +814,28 @@ int8_t sendchar(uint8_t c) ******************************************************************************/ #ifdef MIDI_ENABLE -USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = -{ - .Config = - { - .StreamingInterfaceNumber = AS_INTERFACE, - .DataINEndpoint = - { - .Address = MIDI_STREAM_IN_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = MIDI_STREAM_OUT_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, - }, +USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = { + .Config = + { + .StreamingInterfaceNumber = AS_INTERFACE, + .DataINEndpoint = + { + .Address = MIDI_STREAM_IN_EPADDR, + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1, + }, + .DataOUTEndpoint = + { + .Address = MIDI_STREAM_OUT_EPADDR, + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1, + }, + }, }; -void send_midi_packet(MIDI_EventPacket_t* event) { - MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); -} +void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); } -bool recv_midi_packet(MIDI_EventPacket_t* const event) { - return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); -} +bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); } #endif @@ -920,66 +848,60 @@ bool recv_midi_packet(MIDI_EventPacket_t* const event) { * * FIXME: Needs doc */ -void virtser_init(void) -{ - cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ; - CDC_Device_SendControlLineStateChange(&cdc_device); +void virtser_init(void) { + cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR; + CDC_Device_SendControlLineStateChange(&cdc_device); } /** \brief Virtual Serial Receive * * FIXME: Needs doc */ -void virtser_recv(uint8_t c) __attribute__ ((weak)); -void virtser_recv(uint8_t c) -{ - // Ignore by default +void virtser_recv(uint8_t c) __attribute__((weak)); +void virtser_recv(uint8_t c) { + // Ignore by default } /** \brief Virtual Serial Task * * FIXME: Needs doc */ -void virtser_task(void) -{ - uint16_t count = CDC_Device_BytesReceived(&cdc_device); - uint8_t ch; - if (count) - { - ch = CDC_Device_ReceiveByte(&cdc_device); - virtser_recv(ch); - } +void virtser_task(void) { + uint16_t count = CDC_Device_BytesReceived(&cdc_device); + uint8_t ch; + if (count) { + ch = CDC_Device_ReceiveByte(&cdc_device); + virtser_recv(ch); + } } /** \brief Virtual Serial Send * * FIXME: Needs doc */ -void virtser_send(const uint8_t byte) -{ - uint8_t timeout = 255; - uint8_t ep = Endpoint_GetCurrentEndpoint(); +void virtser_send(const uint8_t byte) { + uint8_t timeout = 255; + uint8_t ep = Endpoint_GetCurrentEndpoint(); - if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) - { - /* IN packet */ - Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address); + if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) { + /* IN packet */ + Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address); - if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { - Endpoint_SelectEndpoint(ep); - return; - } + if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) { + Endpoint_SelectEndpoint(ep); + return; + } - while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); + while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); - Endpoint_Write_8(byte); - CDC_Device_Flush(&cdc_device); + Endpoint_Write_8(byte); + CDC_Device_Flush(&cdc_device); - if (Endpoint_IsINReady()) { - Endpoint_ClearIN(); - } + if (Endpoint_IsINReady()) { + Endpoint_ClearIN(); + } - Endpoint_SelectEndpoint(ep); - } + Endpoint_SelectEndpoint(ep); + } } #endif @@ -990,8 +912,7 @@ void virtser_send(const uint8_t byte) * * FIXME: Needs doc */ -static void setup_mcu(void) -{ +static void setup_mcu(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); @@ -1007,8 +928,7 @@ static void setup_mcu(void) * * FIXME: Needs doc */ -static void setup_usb(void) -{ +static void setup_usb(void) { // Leonardo needs. Without this USB device is not recognized. USB_Disable(); @@ -1023,9 +943,8 @@ static void setup_usb(void) * * FIXME: Needs doc */ -int main(void) __attribute__ ((weak)); -int main(void) -{ +int main(void) __attribute__((weak)); +int main(void) { #ifdef MIDI_ENABLE setup_midi(); #endif @@ -1043,11 +962,11 @@ int main(void) #ifdef WAIT_FOR_USB while (USB_DeviceState != DEVICE_STATE_Configured) { - #if defined(INTERRUPT_CONTROL_ENDPOINT) - ; - #else - USB_USBTask(); - #endif +# if defined(INTERRUPT_CONTROL_ENDPOINT) + ; +# else + USB_USBTask(); +# endif } print("USB configured.\n"); #else @@ -1066,15 +985,15 @@ int main(void) print("Keyboard start.\n"); while (1) { - #if !defined(NO_USB_STARTUP_CHECK) +#if !defined(NO_USB_STARTUP_CHECK) while (USB_DeviceState == DEVICE_STATE_Suspended) { print("[s]"); suspend_power_down(); if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { - USB_Device_SendRemoteWakeup(); + USB_Device_SendRemoteWakeup(); } } - #endif +#endif keyboard_task(); @@ -1102,14 +1021,7 @@ int main(void) #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif - } } -uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, - const uint16_t wIndex, - const void** const DescriptorAddress) -{ - return get_usb_descriptor(wValue, wIndex, DescriptorAddress); -} - +uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint16_t wIndex, const void **const DescriptorAddress) { return get_usb_descriptor(wValue, wIndex, DescriptorAddress); } diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 7364cdf7ce..652e4e79b7 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -62,27 +62,27 @@ extern host_driver_t lufa_driver; typedef struct { uint8_t report_id; uint16_t usage; -} __attribute__ ((packed)) report_extra_t; +} __attribute__((packed)) report_extra_t; #ifdef API_ENABLE - #include "api.h" +# include "api.h" #endif #ifdef API_SYSEX_ENABLE - #include "api_sysex.h" - // Allocate space for encoding overhead. - //The header and terminator are not stored to save a few bytes of precious ram - #define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) +# include "api_sysex.h" +// Allocate space for encoding overhead. +// The header and terminator are not stored to save a few bytes of precious ram +# define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) #endif // #if LUFA_VERSION_INTEGER < 0x120730 // /* old API 120219 */ // #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank) // #else - /* new API >= 120730 */ - #define ENDPOINT_BANK_SINGLE 1 - #define ENDPOINT_BANK_DOUBLE 2 - #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum) , eptype, epsize, epbank) +/* new API >= 120730 */ +#define ENDPOINT_BANK_SINGLE 1 +#define ENDPOINT_BANK_DOUBLE 2 +#define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank) // #endif #endif diff --git a/tmk_core/protocol/lufa/outputselect.c b/tmk_core/protocol/lufa/outputselect.c index 42de806120..b115ea9691 100644 --- a/tmk_core/protocol/lufa/outputselect.c +++ b/tmk_core/protocol/lufa/outputselect.c @@ -15,7 +15,7 @@ along with this program. If not, see . #include "lufa.h" #include "outputselect.h" #ifdef MODULE_ADAFRUIT_BLE - #include "adafruit_ble.h" +# include "adafruit_ble.h" #endif uint8_t desired_output = OUTPUT_DEFAULT; @@ -33,9 +33,7 @@ void set_output(uint8_t output) { * * FIXME: Needs doc */ -__attribute__((weak)) -void set_output_user(uint8_t output) { -} +__attribute__((weak)) void set_output_user(uint8_t output) {} /** \brief Auto Detect Output * @@ -53,7 +51,7 @@ uint8_t auto_detect_output(void) { #endif #ifdef BLUETOOTH_ENABLE - return OUTPUT_BLUETOOTH; // should check if BT is connected here + return OUTPUT_BLUETOOTH; // should check if BT is connected here #endif return OUTPUT_NONE; @@ -69,4 +67,3 @@ uint8_t where_to_send(void) { } return desired_output; } - diff --git a/tmk_core/protocol/lufa/outputselect.h b/tmk_core/protocol/lufa/outputselect.h index 28cc3298e6..24fe4daa24 100644 --- a/tmk_core/protocol/lufa/outputselect.h +++ b/tmk_core/protocol/lufa/outputselect.h @@ -27,14 +27,14 @@ enum outputs { * backward compatibility for BLUETOOTH_ENABLE, send to BT and USB by default */ #ifndef OUTPUT_DEFAULT - #ifdef BLUETOOTH_ENABLE - #define OUTPUT_DEFAULT OUTPUT_USB_AND_BT - #else - #define OUTPUT_DEFAULT OUTPUT_AUTO - #endif +# ifdef BLUETOOTH_ENABLE +# define OUTPUT_DEFAULT OUTPUT_USB_AND_BT +# else +# define OUTPUT_DEFAULT OUTPUT_AUTO +# endif #endif -void set_output(uint8_t output); -void set_output_user(uint8_t output); +void set_output(uint8_t output); +void set_output_user(uint8_t output); uint8_t auto_detect_output(void); uint8_t where_to_send(void); \ No newline at end of file -- cgit v1.2.3 From fa71c4c91e8b822e2d78d420d7d6fe2ca32245af Mon Sep 17 00:00:00 2001 From: bwhelm Date: Tue, 3 Sep 2019 14:29:23 -0400 Subject: Fix battery level code in adafruit_ble.cpp (#6648) * Fix battery level code in adafruit_ble.cpp The code in tsk_core/protocol/lufa/adafluit_ble.cpp that polls the battery level for the Adafruit feather BLE controller reads the regulated voltage, not the raw voltage coming from the battery. To do that, the Adafruit Feather docs say you should read from pin A9: https://learn.adafruit.com/adafruit-feather-32u4-basic-proto/power-management#measuring-battery-4-9. (See also https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le/pinouts#logic-pins-2-9.) I'm not sure why, but analogRead(9); doesn't read the correct pin. Checking all available analog pins experimentally, it turns out that analogRead(7); returns the correct value. So the code above should read: state.vbat = analogRead(7); * Update tmk_core/protocol/lufa/adafruit_ble.cpp Co-Authored-By: Drashna Jaelre * Remove old comment * Fix linking error * Remove `#ifdef` around `#include analog.h`. * Really fix linking error --- tmk_core/protocol/lufa/adafruit_ble.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 71d0936f8f..1d8ae1038b 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -10,6 +10,7 @@ #include "action_util.h" #include "ringbuffer.hpp" #include +#include "analog.h" // These are the pin assignments for the 32u4 boards. // You may define them to something else in your config.h @@ -29,6 +30,12 @@ #define SAMPLE_BATTERY #define ConnectionUpdateInterval 1000 /* milliseconds */ +#ifdef SAMPLE_BATTERY +#ifndef BATTERY_LEVEL_PIN +# define BATTERY_LEVEL_PIN 7 +#endif +#endif + static struct { bool is_connected; bool initialized; @@ -631,15 +638,10 @@ void adafruit_ble_task(void) { } #ifdef SAMPLE_BATTERY - // I don't know if this really does anything useful yet; the reported - // voltage level always seems to be around 3200mV. We may want to just rip - // this code out. if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) { state.last_battery_update = timer_read(); - if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) { - state.vbat = atoi(resbuf); - } + state.vbat = analogRead(BATTERY_LEVEL_PIN); } #endif } -- cgit v1.2.3 From 0d94730da05c9ba1375357cebedd54648463b0b8 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sun, 8 Sep 2019 01:18:05 +1000 Subject: Adafruit BLE: Set SPI2X bit only when F_CPU is 8MHz (#6671) --- tmk_core/protocol/lufa/adafruit_ble.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 1d8ae1038b..505794a80c 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -151,9 +151,10 @@ static struct SPI_Settings spi; // Initialize 4Mhz MSBFIRST MODE0 void SPI_init(struct SPI_Settings *spi) { spi->spcr = _BV(SPE) | _BV(MSTR); +#if F_CPU == 8000000 + // For MCUs running at 8MHz (such as Feather 32U4, or 3.3V Pro Micros) we set the SPI doublespeed bit spi->spsr = _BV(SPI2X); - - static_assert(SpiBusSpeed == F_CPU / 2, "hard coded at 4Mhz"); +#endif ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { // Ensure that SS is OUTPUT High -- cgit v1.2.3 From 22812aee5c207c35010c4b5478af6039c4f2b1ef Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 31 Oct 2019 05:30:21 +0000 Subject: rgblight_task logic fixes (#7214) --- tmk_core/protocol/lufa/lufa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 12a6924fd0..eb166c828e 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -78,7 +78,7 @@ extern keymap_config_t keymap_config; # include "virtser.h" #endif -#if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE) +#if (defined(RGB_MIDI) || defined(RGBLIGHT_ANIMATIONS)) && defined(RGBLIGHT_ENABLE) # include "rgblight.h" #endif @@ -1001,7 +1001,7 @@ int main(void) { MIDI_Device_USBTask(&USB_MIDI_Interface); #endif -#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE) +#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE) rgblight_task(); #endif -- cgit v1.2.3 From a91c0c476507cb8c12840abb59bff34ab0de3c03 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 17 Nov 2019 14:02:26 +0000 Subject: Run clang-format manually to fix recently changed files --- tmk_core/protocol/lufa/adafruit_ble.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 505794a80c..7b3ffdef7a 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -31,9 +31,9 @@ #define ConnectionUpdateInterval 1000 /* milliseconds */ #ifdef SAMPLE_BATTERY -#ifndef BATTERY_LEVEL_PIN -# define BATTERY_LEVEL_PIN 7 -#endif +# ifndef BATTERY_LEVEL_PIN +# define BATTERY_LEVEL_PIN 7 +# endif #endif static struct { -- cgit v1.2.3 From 698d0dbda8a4b081103b4a382baa1f99e3f77ceb Mon Sep 17 00:00:00 2001 From: fauxpark Date: Wed, 11 Dec 2019 12:26:40 +1100 Subject: Use `clock_prescale_set()` from avr-libc (#7540) * Use `clock_prescale_set()` from avr-libc * Remove outdated FAQ info --- tmk_core/protocol/lufa/lufa.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index eb166c828e..8319b34e94 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -914,14 +914,11 @@ void virtser_send(const uint8_t byte) { */ static void setup_mcu(void) { /* Disable watchdog if enabled by bootloader/fuses */ - MCUSR &= ~(1 << WDRF); + MCUSR &= ~_BV(WDRF); wdt_disable(); /* Disable clock division */ - // clock_prescale_set(clock_div_1); - - CLKPR = (1 << CLKPCE); - CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); + clock_prescale_set(clock_div_1); } /** \brief Setup USB -- cgit v1.2.3 From ba628a28bcf09421739922d8eeef05e6ca9e9b6b Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 19 Jan 2020 00:59:09 +0000 Subject: Move rgblight and backlight task to common location (#7733) --- tmk_core/protocol/lufa/lufa.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 8319b34e94..7d325a9ba2 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -998,10 +998,6 @@ int main(void) { MIDI_Device_USBTask(&USB_MIDI_Interface); #endif -#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE) - rgblight_task(); -#endif - #ifdef MODULE_ADAFRUIT_BLE adafruit_ble_task(); #endif -- cgit v1.2.3 From eb309be6f009a9f3923ea49cec3653b75ec47e69 Mon Sep 17 00:00:00 2001 From: xyzz <1065521+xyzz@users.noreply.github.com> Date: Thu, 30 Jan 2020 19:30:41 -0500 Subject: Improve VIRTSER performance (#7528) --- tmk_core/protocol/lufa/lufa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 7d325a9ba2..db66a07222 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -869,7 +869,7 @@ void virtser_recv(uint8_t c) { void virtser_task(void) { uint16_t count = CDC_Device_BytesReceived(&cdc_device); uint8_t ch; - if (count) { + for (; count; --count) { ch = CDC_Device_ReceiveByte(&cdc_device); virtser_recv(ch); } -- cgit v1.2.3 From b2ce2f8a34fba72e4b4ac2fba0ec11431b0acb0c Mon Sep 17 00:00:00 2001 From: fauxpark Date: Mon, 3 Feb 2020 07:17:05 +1100 Subject: Dedupe extrakey report struct, and send functions in V-USB & LUFA (#7993) * Dedupe extrakey report struct, and send functions in V-USB & LUFA * Doc comment for consistency * Wrap it in ifdef to prevent unused function error * Do the same for ATSAM --- tmk_core/protocol/lufa/lufa.c | 27 ++++++++++++++------------- tmk_core/protocol/lufa/lufa.h | 6 ------ 2 files changed, 14 insertions(+), 19 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index db66a07222..792db43402 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -662,17 +662,17 @@ static void send_mouse(report_mouse_t *report) { #endif } -/** \brief Send System +/** \brief Send Extra * * FIXME: Needs doc */ -static void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE +static void send_extra(uint8_t report_id, uint16_t data) { uint8_t timeout = 255; if (USB_DeviceState != DEVICE_STATE_Configured) return; - report_extra_t r = {.report_id = REPORT_ID_SYSTEM, .usage = data - SYSTEM_POWER_DOWN + 1}; + report_extra_t r = {.report_id = report_id, .usage = data}; Endpoint_SelectEndpoint(SHARED_IN_EPNUM); /* Check if write ready for a polling interval around 10ms */ @@ -681,6 +681,16 @@ static void send_system(uint16_t data) { Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); Endpoint_ClearIN(); +} +#endif + +/** \brief Send System + * + * FIXME: Needs doc + */ +static void send_system(uint16_t data) { +#ifdef EXTRAKEY_ENABLE + send_extra(REPORT_ID_SYSTEM, data - SYSTEM_POWER_DOWN + 1); #endif } @@ -690,7 +700,6 @@ static void send_system(uint16_t data) { */ static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE - uint8_t timeout = 255; uint8_t where = where_to_send(); # ifdef BLUETOOTH_ENABLE @@ -729,15 +738,7 @@ static void send_consumer(uint16_t data) { return; } - report_extra_t r = {.report_id = REPORT_ID_CONSUMER, .usage = data}; - Endpoint_SelectEndpoint(SHARED_IN_EPNUM); - - /* Check if write ready for a polling interval around 10ms */ - while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); - if (!Endpoint_IsReadWriteAllowed()) return; - - Endpoint_Write_Stream_LE(&r, sizeof(report_extra_t), NULL); - Endpoint_ClearIN(); + send_extra(REPORT_ID_CONSUMER, data); #endif } diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 652e4e79b7..1b88060f14 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -58,12 +58,6 @@ extern host_driver_t lufa_driver; } #endif -/* extra report structure */ -typedef struct { - uint8_t report_id; - uint16_t usage; -} __attribute__((packed)) report_extra_t; - #ifdef API_ENABLE # include "api.h" #endif -- cgit v1.2.3 From fa9c74c6a2301c8f0f12727eb730e213b3d7453c Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Sun, 2 Feb 2020 20:47:40 +0000 Subject: format code according to conventions [skip ci] --- tmk_core/protocol/lufa/lufa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 792db43402..933b212c96 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -700,7 +700,7 @@ static void send_system(uint16_t data) { */ static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); # ifdef BLUETOOTH_ENABLE if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { -- cgit v1.2.3 From 088b64ab3d598574244db5ac67fec889bdc58119 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 25 Feb 2020 12:18:11 +1100 Subject: Improvements to extrakey HID descriptors (#8156) --- tmk_core/protocol/lufa/lufa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 933b212c96..738c2a6d31 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -690,7 +690,7 @@ static void send_extra(uint8_t report_id, uint16_t data) { */ static void send_system(uint16_t data) { #ifdef EXTRAKEY_ENABLE - send_extra(REPORT_ID_SYSTEM, data - SYSTEM_POWER_DOWN + 1); + send_extra(REPORT_ID_SYSTEM, data); #endif } -- cgit v1.2.3 From dbbab409812ea7c579760e2973399245c21ff843 Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Tue, 10 Mar 2020 17:46:03 +0900 Subject: Refactor rgblight_reconfig.h (#7773) * Moved contents of rgblight_reconfig.h to rgblight_post_config.h. In #3582, rgblight_reconfig.h had to be newly created. Now, the build system of qmk_firmware has a post_cofig feature, so that what was done in rgblight_reconfig.h can now be realized in rgblight_post_config.h. **This commit does not change the build result.** Testing script ```shell # build on master git checkout master echo master > /tmp/master_md5.txt # RGBLIGHT_ENABLE = no make HELIX=verbose helix/rev2:default:clean make HELIX=verbose helix/rev2:default md5 helix_rev2_default.hex >> /tmp/master_md5.txt # RGBLIGHT_ENABLE = yes, with animations make HELIX=verbose helix/rev2/back:default:clean make HELIX=verbose helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/master_md5.txt # RGBLIGHT_ENABLE = yes, without animations make HELIX=verbose,no_ani helix/rev2/back:default:clean make HELIX=verbose,no_ani helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/master_md5.txt # build on refactor_rgblight_reconfig.h git checkout refactor_rgblight_reconfig.h echo refactor_rgblight_reconfig.h > /tmp/branch_md5.txt # RGBLIGHT_ENABLE = no make HELIX=verbose helix/rev2:default:clean make HELIX=verbose helix/rev2:default md5 helix_rev2_default.hex >> /tmp/branch_md5.txt # RGBLIGHT_ENABLE = yes, with animations make HELIX=verbose helix/rev2/back:default:clean make HELIX=verbose helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/branch_md5.txt # RGBLIGHT_ENABLE = yes, without animations make HELIX=verbose,no_ani helix/rev2/back:default:clean make HELIX=verbose,no_ani helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/branch_md5.txt diff -u /tmp/master_md5.txt /tmp/branch_md5.txt ``` Test result: ``` --- /tmp/master_md5.txt 2020-01-03 15:42:22.000000000 +0900 +++ /tmp/branch_md5.txt 2020-01-03 15:42:42.000000000 +0900 @@ -1,4 +1,4 @@ -master +refactor_rgblight_reconfig.h MD5 (helix_rev2_default.hex) = f360032edd522448366d471d8f4f8181 MD5 (helix_rev2_back_default.hex) = 0c663acc6cccc44476b3b969ad22a48f MD5 (helix_rev2_back_default.hex) = e66b1195ff6d38e6e22c975b8ae42fd3 ``` * Expressions that are too long are difficult to read, so wrap them. * Edit the expression again * remove `defined(RGBLIGHT_ANIMATIONS)` in `tmk_core/common/*/suspend.c`, `tmk_core/protocol/*/main.c` move contents of rgblight_reconfig.h to rgblight.h. The following changes were made to rgblight.h. ```diff +#ifdef RGBLIGHT_USE_TIMER void rgblight_task(void); void rgblight_timer_init(void); void rgblight_timer_enable(void); void rgblight_timer_disable(void); void rgblight_timer_toggle(void); +#else +#define rgblight_task() +#define rgblight_timer_init() +#define rgblight_timer_enable() +#define rgblight_timer_disable() +#define rgblight_timer_toggle() +#endif ``` The following changes were made to tmk_core/common/avr/suspend.c, tmk_core/common/chibios/suspend.c, tmk_core/protocol/chibios/main.c, tmk_core/protocol/lufa/lufa.c, tmk_core/protocol/vusb/main.c. ```diff -# ifdef RGBLIGHT_ANIMATIONS rgblight_timer_enable(); -# endif ``` ```diff -#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE) +#if defined(RGBLIGHT_ENABLE) rgblight_task(); #endif ``` * remove 'defined(RGBLIGHT_ANIMATIONS)' in tmk_core/common/keyboard.c Co-authored-by: Joel Challis --- tmk_core/protocol/lufa/lufa.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 738c2a6d31..ca75a91d0e 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -54,7 +54,6 @@ #include "quantum.h" #include #include "outputselect.h" -#include "rgblight_reconfig.h" #ifdef NKRO_ENABLE # include "keycode_config.h" @@ -78,7 +77,7 @@ extern keymap_config_t keymap_config; # include "virtser.h" #endif -#if (defined(RGB_MIDI) || defined(RGBLIGHT_ANIMATIONS)) && defined(RGBLIGHT_ENABLE) +#if defined(RGBLIGHT_ENABLE) # include "rgblight.h" #endif -- cgit v1.2.3 From 9dfebb9d67a211db3ded762b60ee868299dd406e Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Mon, 16 Mar 2020 18:26:32 +0900 Subject: Remove unnecessary import of rgblight.h in tmk_core/protocol/*/*.c (#8432) * Remove unnecessary import of rgblight.h in tmk_core/protocol/*/*.c * tmk_core/protocol/chibios/main.c * tmk_core/protocol/lufa/lufa.c see #8380 for tmk_core/protocol/vusb/main.c. * Remove '#include "rgblight.h"' from tmk_core/protocol/vusb/main.c. --- tmk_core/protocol/lufa/lufa.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index ca75a91d0e..d673841fd5 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -77,10 +77,6 @@ extern keymap_config_t keymap_config; # include "virtser.h" #endif -#if defined(RGBLIGHT_ENABLE) -# include "rgblight.h" -#endif - #ifdef MIDI_ENABLE # include "qmk_midi.h" #endif -- cgit v1.2.3 From 95c5c11d0b909f14696c9c0b2dd30b3bfd406273 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 2 Apr 2020 01:49:45 +1100 Subject: Miscellaneous cleanups (#8639) * Miscellaneous cleanups * Cast NO_PIN --- tmk_core/protocol/lufa/lufa.h | 6 ------ 1 file changed, 6 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 1b88060f14..82a5f8e05f 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -69,14 +69,8 @@ extern host_driver_t lufa_driver; # define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) #endif -// #if LUFA_VERSION_INTEGER < 0x120730 -// /* old API 120219 */ -// #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint(epnum, eptype, epdir, epsize, epbank) -// #else -/* new API >= 120730 */ #define ENDPOINT_BANK_SINGLE 1 #define ENDPOINT_BANK_DOUBLE 2 #define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank) -// #endif #endif -- cgit v1.2.3 From 400ca2d035d7bdb85c077a53b2cd85cdd04882ab Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 8 Apr 2020 11:04:31 +1000 Subject: spi_master for AVR (#8299) * Change _delay_ms/us() to wait_ms/us() * Switch to platform-agnostic GPIO macros * Add AVR spi_master and migrate Adafruit BLE code * Set verbose back to false * Add clock divisor, bit order and SPI mode configuration for init * Add start and stop functions * Move configuration of mode, endianness and speed to `spi_start()` * Some breaks here would be good * Default Adafruit BLE clock divisor to 4 (2MHz on the Feather 32U4) * Remove mode and divisor enums * Add some docs * No hr at EOF * Add links in sidebar --- tmk_core/protocol/lufa/adafruit_ble.cpp | 162 ++++++++------------------------ 1 file changed, 38 insertions(+), 124 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 7b3ffdef7a..f04ab757ea 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -1,15 +1,15 @@ #include "adafruit_ble.h" + #include #include #include -#include -#include #include "debug.h" -#include "pincontrol.h" #include "timer.h" #include "action_util.h" #include "ringbuffer.hpp" #include +#include "spi_master.h" +#include "wait.h" #include "analog.h" // These are the pin assignments for the 32u4 boards. @@ -27,6 +27,12 @@ # define AdafruitBleIRQPin E6 #endif +#ifndef AdafruitBleSpiClockSpeed +# define AdafruitBleSpiClockSpeed 4000000UL // SCK frequency +#endif + +#define SCK_DIVISOR (F_CPU / AdafruitBleSpiClockSpeed) + #define SAMPLE_BATTERY #define ConnectionUpdateInterval 1000 /* milliseconds */ @@ -130,10 +136,6 @@ enum ble_system_event_bits { BleSystemMidiRx = 10, }; -// The SDEP.md file says 2MHz but the web page and the sample driver -// both use 4MHz -#define SpiBusSpeed 4000000 - #define SdepTimeout 150 /* milliseconds */ #define SdepShortTimeout 10 /* milliseconds */ #define SdepBackOff 25 /* microseconds */ @@ -142,116 +144,32 @@ enum ble_system_event_bits { static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbose, uint16_t timeout = SdepTimeout); static bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose = false); -struct SPI_Settings { - uint8_t spcr, spsr; -}; - -static struct SPI_Settings spi; - -// Initialize 4Mhz MSBFIRST MODE0 -void SPI_init(struct SPI_Settings *spi) { - spi->spcr = _BV(SPE) | _BV(MSTR); -#if F_CPU == 8000000 - // For MCUs running at 8MHz (such as Feather 32U4, or 3.3V Pro Micros) we set the SPI doublespeed bit - spi->spsr = _BV(SPI2X); -#endif - - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - // Ensure that SS is OUTPUT High - digitalWrite(B0, PinLevelHigh); - pinMode(B0, PinDirectionOutput); - - SPCR |= _BV(MSTR); - SPCR |= _BV(SPE); - pinMode(B1 /* SCK */, PinDirectionOutput); - pinMode(B2 /* MOSI */, PinDirectionOutput); - } -} - -static inline void SPI_begin(struct SPI_Settings *spi) { - SPCR = spi->spcr; - SPSR = spi->spsr; -} - -static inline uint8_t SPI_TransferByte(uint8_t data) { - SPDR = data; - asm volatile("nop"); - while (!(SPSR & _BV(SPIF))) { - ; // wait - } - return SPDR; -} - -static inline void spi_send_bytes(const uint8_t *buf, uint8_t len) { - if (len == 0) return; - const uint8_t *end = buf + len; - while (buf < end) { - SPDR = *buf; - while (!(SPSR & _BV(SPIF))) { - ; // wait - } - ++buf; - } -} - -static inline uint16_t spi_read_byte(void) { return SPI_TransferByte(0x00 /* dummy */); } - -static inline void spi_recv_bytes(uint8_t *buf, uint8_t len) { - const uint8_t *end = buf + len; - if (len == 0) return; - while (buf < end) { - SPDR = 0; // write a dummy to initiate read - while (!(SPSR & _BV(SPIF))) { - ; // wait - } - *buf = SPDR; - ++buf; - } -} - -#if 0 -static void dump_pkt(const struct sdep_msg *msg) { - print("pkt: type="); - print_hex8(msg->type); - print(" cmd="); - print_hex8(msg->cmd_high); - print_hex8(msg->cmd_low); - print(" len="); - print_hex8(msg->len); - print(" more="); - print_hex8(msg->more); - print("\n"); -} -#endif - // Send a single SDEP packet static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { - SPI_begin(&spi); - - digitalWrite(AdafruitBleCSPin, PinLevelLow); + spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR); uint16_t timerStart = timer_read(); bool success = false; bool ready = false; do { - ready = SPI_TransferByte(msg->type) != SdepSlaveNotReady; + ready = spi_write(msg->type, 100) != SdepSlaveNotReady; if (ready) { break; } // Release it and let it initialize - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); + spi_stop(); + wait_us(SdepBackOff); + spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR); } while (timer_elapsed(timerStart) < timeout); if (ready) { // Slave is ready; send the rest of the packet - spi_send_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); + spi_transmit(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len, 100); success = true; } - digitalWrite(AdafruitBleCSPin, PinLevelHigh); + spi_stop(); return success; } @@ -275,41 +193,39 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) { bool ready = false; do { - ready = digitalRead(AdafruitBleIRQPin); + ready = readPin(AdafruitBleIRQPin); if (ready) { break; } - _delay_us(1); + wait_us(1); } while (timer_elapsed(timerStart) < timeout); if (ready) { - SPI_begin(&spi); - - digitalWrite(AdafruitBleCSPin, PinLevelLow); + spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR); do { // Read the command type, waiting for the data to be ready - msg->type = spi_read_byte(); + msg->type = spi_read(100); if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { // Release it and let it initialize - digitalWrite(AdafruitBleCSPin, PinLevelHigh); - _delay_us(SdepBackOff); - digitalWrite(AdafruitBleCSPin, PinLevelLow); + spi_stop(); + wait_us(SdepBackOff); + spi_start(AdafruitBleCSPin, false, 0, SCK_DIVISOR); continue; } // Read the rest of the header - spi_recv_bytes(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); + spi_receive(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)), 100); // and get the payload if there is any if (msg->len <= SdepMaxPayload) { - spi_recv_bytes(msg->payload, msg->len); + spi_receive(msg->payload, msg->len, 100); } success = true; break; } while (timer_elapsed(timerStart) < timeout); - digitalWrite(AdafruitBleCSPin, PinLevelHigh); + spi_stop(); } return success; } @@ -320,7 +236,7 @@ static void resp_buf_read_one(bool greedy) { return; } - if (digitalRead(AdafruitBleIRQPin)) { + if (readPin(AdafruitBleIRQPin)) { struct sdep_msg msg; again: @@ -331,7 +247,7 @@ static void resp_buf_read_one(bool greedy) { dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send)); } - if (greedy && resp_buf.peek(last_send) && digitalRead(AdafruitBleIRQPin)) { + if (greedy && resp_buf.peek(last_send) && readPin(AdafruitBleIRQPin)) { goto again; } } @@ -361,7 +277,7 @@ static void send_buf_send_one(uint16_t timeout = SdepTimeout) { dprintf("send_buf_send_one: have %d remaining\n", (int)send_buf.size()); } else { dprint("failed to send, will retry\n"); - _delay_ms(SdepTimeout); + wait_ms(SdepTimeout); resp_buf_read_one(true); } } @@ -382,20 +298,18 @@ static bool ble_init(void) { state.configured = false; state.is_connected = false; - pinMode(AdafruitBleIRQPin, PinDirectionInput); - pinMode(AdafruitBleCSPin, PinDirectionOutput); - digitalWrite(AdafruitBleCSPin, PinLevelHigh); + setPinInput(AdafruitBleIRQPin); - SPI_init(&spi); + spi_init(); // Perform a hardware reset - pinMode(AdafruitBleResetPin, PinDirectionOutput); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); - digitalWrite(AdafruitBleResetPin, PinLevelLow); - _delay_ms(10); - digitalWrite(AdafruitBleResetPin, PinLevelHigh); + setPinOutput(AdafruitBleResetPin); + writePinHigh(AdafruitBleResetPin); + writePinLow(AdafruitBleResetPin); + wait_ms(10); + writePinHigh(AdafruitBleResetPin); - _delay_ms(1000); // Give it a second to initialize + wait_ms(1000); // Give it a second to initialize state.initialized = true; return state.initialized; @@ -596,7 +510,7 @@ void adafruit_ble_task(void) { resp_buf_read_one(true); send_buf_send_one(SdepShortTimeout); - if (resp_buf.empty() && (state.event_flags & UsingEvents) && digitalRead(AdafruitBleIRQPin)) { + if (resp_buf.empty() && (state.event_flags & UsingEvents) && readPin(AdafruitBleIRQPin)) { // Must be an event update if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) { uint32_t mask = strtoul(resbuf, NULL, 16); -- cgit v1.2.3 From 46e449376163779413b74f81e320a7d7bbd7e13b Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Mon, 13 Apr 2020 17:09:50 +1000 Subject: Fix AVR SPI parameter configuration, remove timeouts due to sync protocol. (#8775) --- tmk_core/protocol/lufa/adafruit_ble.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index f04ab757ea..b07407f387 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -152,7 +152,7 @@ static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { bool ready = false; do { - ready = spi_write(msg->type, 100) != SdepSlaveNotReady; + ready = spi_write(msg->type) != SdepSlaveNotReady; if (ready) { break; } @@ -165,7 +165,7 @@ static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { if (ready) { // Slave is ready; send the rest of the packet - spi_transmit(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len, 100); + spi_transmit(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)) + msg->len); success = true; } @@ -205,7 +205,7 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) { do { // Read the command type, waiting for the data to be ready - msg->type = spi_read(100); + msg->type = spi_read(); if (msg->type == SdepSlaveNotReady || msg->type == SdepSlaveOverflow) { // Release it and let it initialize spi_stop(); @@ -215,11 +215,11 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) { } // Read the rest of the header - spi_receive(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload)), 100); + spi_receive(&msg->cmd_low, sizeof(*msg) - (1 + sizeof(msg->payload))); // and get the payload if there is any if (msg->len <= SdepMaxPayload) { - spi_receive(msg->payload, msg->len, 100); + spi_receive(msg->payload, msg->len); } success = true; break; -- cgit v1.2.3 From 5f82b0782f90645eee5926691dcc0ec617d0dff4 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 3 May 2020 01:25:39 +0100 Subject: Initial vusb console support (#8559) --- tmk_core/protocol/lufa/lufa.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index d673841fd5..374add20f9 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -801,8 +801,6 @@ ERROR_EXIT: Endpoint_SelectEndpoint(ep); return -1; } -#else -int8_t sendchar(uint8_t c) { return 0; } #endif /******************************************************************************* -- cgit v1.2.3 From f209f91c7ce073aa0381e79c10219616cb7db523 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 7 Jun 2020 15:00:21 +1000 Subject: Various tidyups for USB descriptor code (#9005) --- tmk_core/protocol/lufa/lufa.c | 120 +++++++++++++++++++++--------------------- tmk_core/protocol/lufa/lufa.h | 4 -- 2 files changed, 61 insertions(+), 63 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 374add20f9..19f417770f 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -88,7 +88,7 @@ extern keymap_config_t keymap_config; uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ uint8_t keyboard_protocol = 1; -static uint8_t keyboard_led_stats = 0; +static uint8_t keyboard_led_state = 0; static report_keyboard_t keyboard_report_sent; @@ -103,30 +103,30 @@ host_driver_t lufa_driver = { }; #ifdef VIRTSER_ENABLE +// clang-format off + USB_ClassInfo_CDC_Device_t cdc_device = { - .Config = - { - .ControlInterfaceNumber = CCI_INTERFACE, - .DataINEndpoint = - { - .Address = CDC_IN_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = CDC_OUT_EPADDR, - .Size = CDC_EPSIZE, - .Banks = 1, - }, - .NotificationEndpoint = - { - .Address = CDC_NOTIFICATION_EPADDR, - .Size = CDC_NOTIFICATION_EPSIZE, - .Banks = 1, - }, + .Config = { + .ControlInterfaceNumber = CCI_INTERFACE, + .DataINEndpoint = { + .Address = (CDC_IN_EPNUM | ENDPOINT_DIR_IN), + .Size = CDC_EPSIZE, + .Banks = 1 + }, + .DataOUTEndpoint = { + .Address = (CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), + .Size = CDC_EPSIZE, + .Banks = 1 }, + .NotificationEndpoint = { + .Address = (CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN), + .Size = CDC_NOTIFICATION_EPSIZE, + .Banks = 1 + } + } }; + +// clang-format on #endif #ifdef RAW_ENABLE @@ -254,7 +254,7 @@ static void Console_Task(void) { // fill empty bank while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0); - // flash senchar packet + // flush sendchar packet if (Endpoint_IsINReady()) { Endpoint_ClearIN(); } @@ -370,45 +370,46 @@ void EVENT_USB_Device_StartOfFrame(void) { void EVENT_USB_Device_ConfigurationChanged(void) { bool ConfigSuccess = true; - /* Setup Keyboard HID Report Endpoints */ #ifndef KEYBOARD_SHARED_EP - ConfigSuccess &= ENDPOINT_CONFIG(KEYBOARD_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, KEYBOARD_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup keyboard report endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((KEYBOARD_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, KEYBOARD_EPSIZE, 1); #endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) - /* Setup Mouse HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(MOUSE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, MOUSE_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup mouse report endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((MOUSE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, MOUSE_EPSIZE, 1); #endif #ifdef SHARED_EP_ENABLE - /* Setup Shared HID Report Endpoint */ - ConfigSuccess &= ENDPOINT_CONFIG(SHARED_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, SHARED_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup shared report endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((SHARED_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, SHARED_EPSIZE, 1); #endif #ifdef RAW_ENABLE - /* Setup Raw HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(RAW_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= ENDPOINT_CONFIG(RAW_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, RAW_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup raw HID endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint((RAW_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, RAW_EPSIZE, 1); #endif #ifdef CONSOLE_ENABLE - /* Setup Console HID Report Endpoints */ - ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup console endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1); # if 0 - ConfigSuccess &= ENDPOINT_CONFIG(CONSOLE_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT, - CONSOLE_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint((CONSOLE_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_INTERRUPT, CONSOLE_EPSIZE, 1); # endif #endif #ifdef MIDI_ENABLE - ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup MIDI stream endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1); #endif #ifdef VIRTSER_ENABLE - ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE); - ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup virtual serial endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_NOTIFICATION_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1); #endif } @@ -472,10 +473,10 @@ void EVENT_USB_Device_ControlRequest(void) { uint8_t report_id = Endpoint_Read_8(); if (report_id == REPORT_ID_KEYBOARD || report_id == REPORT_ID_NKRO) { - keyboard_led_stats = Endpoint_Read_8(); + keyboard_led_state = Endpoint_Read_8(); } } else { - keyboard_led_stats = Endpoint_Read_8(); + keyboard_led_state = Endpoint_Read_8(); } Endpoint_ClearOUT(); @@ -545,7 +546,7 @@ void EVENT_USB_Device_ControlRequest(void) { * * FIXME: Needs doc */ -static uint8_t keyboard_leds(void) { return keyboard_led_stats; } +static uint8_t keyboard_leds(void) { return keyboard_led_state; } /** \brief Send Keyboard * @@ -808,25 +809,26 @@ ERROR_EXIT: ******************************************************************************/ #ifdef MIDI_ENABLE +// clang-format off + USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface = { - .Config = - { - .StreamingInterfaceNumber = AS_INTERFACE, - .DataINEndpoint = - { - .Address = MIDI_STREAM_IN_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, - .DataOUTEndpoint = - { - .Address = MIDI_STREAM_OUT_EPADDR, - .Size = MIDI_STREAM_EPSIZE, - .Banks = 1, - }, + .Config = { + .StreamingInterfaceNumber = AS_INTERFACE, + .DataINEndpoint = { + .Address = (MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1 }, + .DataOUTEndpoint = { + .Address = (MIDI_STREAM_OUT_EPNUM | ENDPOINT_DIR_OUT), + .Size = MIDI_STREAM_EPSIZE, + .Banks = 1 + } + } }; +// clang-format on + void send_midi_packet(MIDI_EventPacket_t *event) { MIDI_Device_SendEventPacket(&USB_MIDI_Interface, event); } bool recv_midi_packet(MIDI_EventPacket_t *const event) { return MIDI_Device_ReceiveEventPacket(&USB_MIDI_Interface, event); } diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 82a5f8e05f..71fd7aad85 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -69,8 +69,4 @@ extern host_driver_t lufa_driver; # define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) #endif -#define ENDPOINT_BANK_SINGLE 1 -#define ENDPOINT_BANK_DOUBLE 2 -#define ENDPOINT_CONFIG(epnum, eptype, epdir, epsize, epbank) Endpoint_ConfigureEndpoint((epdir) | (epnum), eptype, epsize, epbank) - #endif -- cgit v1.2.3 From 1193e45bf4c8c69e33c054c6eb3d545d5d52fcb5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 7 Jun 2020 15:00:59 +1000 Subject: Convert `CONSUMER2BLUEFRUIT()` and `CONSUMER2RN42()` macros to static inline functions (#9055) --- tmk_core/protocol/lufa/bluetooth.h | 98 ++++++++++++++++++++++++++++---------- tmk_core/protocol/lufa/lufa.c | 2 +- 2 files changed, 74 insertions(+), 26 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/bluetooth.h b/tmk_core/protocol/lufa/bluetooth.h index 081271a4e6..67f031439e 100644 --- a/tmk_core/protocol/lufa/bluetooth.h +++ b/tmk_core/protocol/lufa/bluetooth.h @@ -15,34 +15,82 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef BLUETOOTH_H -#define BLUETOOTH_H +#pragma once #include "../serial.h" void bluefruit_serial_send(uint8_t data); -/* -+-----------------+-------------------+-------+ -| 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))))))))))))))))))) - -#define CONSUMER2RN42(usage) (usage == AUDIO_MUTE ? 0x0040 : (usage == AUDIO_VOL_UP ? 0x0010 : (usage == AUDIO_VOL_DOWN ? 0x0020 : (usage == TRANSPORT_NEXT_TRACK ? 0x0100 : (usage == TRANSPORT_PREV_TRACK ? 0x0200 : (usage == TRANSPORT_STOP ? 0x0400 : (usage == TRANSPORT_STOP_EJECT ? 0x0800 : (usage == TRANSPORT_PLAY_PAUSE ? 0x0080 : (usage == AL_EMAIL ? 0x0200 : (usage == AL_LOCAL_BROWSER ? 0x8000 : (usage == AC_SEARCH ? 0x0400 : (usage == AC_HOME ? 0x0100 : 0)))))))))))) +// https://learn.adafruit.com/introducing-bluefruit-ez-key-diy-bluetooth-hid-keyboard/sending-keys-via-serial#raw-hid-consumer-reports-8-14 +static inline uint16_t CONSUMER2BLUEFRUIT(uint16_t usage) { + switch (usage) { + case AC_HOME: + return 0x0001; + case AL_KEYBOARD_LAYOUT: + return 0x0002; + case AC_SEARCH: + return 0x0004; + case SNAPSHOT: + return 0x0008; + case AUDIO_VOL_UP: + return 0x0010; + case AUDIO_VOL_DOWN: + return 0x0020; + case TRANSPORT_PLAY_PAUSE: + return 0x0040; + case TRANSPORT_FAST_FORWARD: + return 0x0080; + case TRANSPORT_REWIND: + return 0x0100; + case TRANSPORT_NEXT_TRACK: + return 0x0200; + case TRANSPORT_PREV_TRACK: + return 0x0400; + case TRANSPORT_RANDOM_PLAY: + return 0x0800; + case TRANSPORT_STOP: + return 0x1000; + default: + return 0; + } +} -#endif +// https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734 +static inline uint16_t CONSUMER2RN42(uint16_t usage) { + switch (usage) { + case AC_HOME: + return 0x0001; + case AL_EMAIL: + return 0x0002; + case AC_SEARCH: + return 0x0004; + case AL_KEYBOARD_LAYOUT: + return 0x0008; + case AUDIO_VOL_UP: + return 0x0010; + case AUDIO_VOL_DOWN: + return 0x0020; + case AUDIO_MUTE: + return 0x0040; + case TRANSPORT_PLAY_PAUSE: + return 0x0080; + case TRANSPORT_NEXT_TRACK: + return 0x0100; + case TRANSPORT_PREV_TRACK: + return 0x0200; + case TRANSPORT_STOP: + return 0x0400; + case TRANSPORT_EJECT: + return 0x0800; + case TRANSPORT_FAST_FORWARD: + return 0x1000; + case TRANSPORT_REWIND: + return 0x2000; + case TRANSPORT_STOP_EJECT: + return 0x4000; + case AL_LOCAL_BROWSER: + return 0x8000; + default: + return 0; + } +} diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 19f417770f..3ca1a809d1 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -720,8 +720,8 @@ static void send_consumer(uint16_t data) { bluefruit_serial_send(0xFD); bluefruit_serial_send(0x00); bluefruit_serial_send(0x02); - bluefruit_serial_send((bitmap >> 8) & 0xFF); bluefruit_serial_send(bitmap & 0xFF); + bluefruit_serial_send((bitmap >> 8) & 0xFF); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); bluefruit_serial_send(0x00); -- cgit v1.2.3 From 3b34858b772cda36fa9988e4d45039ff7ff9146a Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 17 Jun 2020 21:43:20 +1000 Subject: Don't compile outputselect.c if Bluetooth is disabled (#9356) --- tmk_core/protocol/lufa/lufa.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 3ca1a809d1..ca3384ba25 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -53,7 +53,6 @@ #include "lufa.h" #include "quantum.h" #include -#include "outputselect.h" #ifdef NKRO_ENABLE # include "keycode_config.h" @@ -66,6 +65,7 @@ extern keymap_config_t keymap_config; #endif #ifdef BLUETOOTH_ENABLE +# include "outputselect.h" # ifdef MODULE_ADAFRUIT_BLE # include "adafruit_ble.h" # else @@ -554,9 +554,10 @@ static uint8_t keyboard_leds(void) { return keyboard_led_state; } */ static void send_keyboard(report_keyboard_t *report) { uint8_t timeout = 255; - uint8_t where = where_to_send(); #ifdef BLUETOOTH_ENABLE + uint8_t where = where_to_send(); + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); @@ -578,11 +579,11 @@ static void send_keyboard(report_keyboard_t *report) { } # endif } -#endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } +#endif /* Select the Keyboard Report Endpoint */ uint8_t ep = KEYBOARD_IN_EPNUM; @@ -618,9 +619,10 @@ static void send_keyboard(report_keyboard_t *report) { static void send_mouse(report_mouse_t *report) { #ifdef MOUSE_ENABLE uint8_t timeout = 255; - uint8_t where = where_to_send(); # ifdef BLUETOOTH_ENABLE + uint8_t where = where_to_send(); + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE // FIXME: mouse buttons @@ -637,11 +639,11 @@ static void send_mouse(report_mouse_t *report) { bluefruit_serial_send(0x00); # endif } -# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } +# endif /* Select the Mouse Report Endpoint */ Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); @@ -696,9 +698,9 @@ static void send_system(uint16_t data) { */ static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE +# ifdef BLUETOOTH_ENABLE uint8_t where = where_to_send(); -# ifdef BLUETOOTH_ENABLE if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_consumer_key(data, 0); @@ -728,11 +730,11 @@ static void send_consumer(uint16_t data) { bluefruit_serial_send(0x00); # endif } -# endif if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } +# endif send_extra(REPORT_ID_CONSUMER, data); #endif -- cgit v1.2.3 From 60e5733c487cc0435e7d3b913f31ce4acc405d4a Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Mon, 22 Jun 2020 11:22:00 +1000 Subject: `qmk cformat` on `develop` (#9501) --- tmk_core/protocol/lufa/lufa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index ca3384ba25..6776a964e9 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -556,7 +556,7 @@ static void send_keyboard(report_keyboard_t *report) { uint8_t timeout = 255; #ifdef BLUETOOTH_ENABLE - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE @@ -621,7 +621,7 @@ static void send_mouse(report_mouse_t *report) { uint8_t timeout = 255; # ifdef BLUETOOTH_ENABLE - uint8_t where = where_to_send(); + uint8_t where = where_to_send(); if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE -- cgit v1.2.3 From 8a4a0c25fdb2c4d0ac3ac8fd5f6ba14b56f4cd28 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 2 Jul 2020 13:12:34 +1000 Subject: Change analogRead calls to analogReadPin (#9023) * Change analogRead calls to analogReadPin * Add ChangeLog * Update docs, remove mention of `analogRead()` * Retarget changelog for next round --- tmk_core/protocol/lufa/adafruit_ble.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index b07407f387..79b35fca31 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -38,7 +38,7 @@ #ifdef SAMPLE_BATTERY # ifndef BATTERY_LEVEL_PIN -# define BATTERY_LEVEL_PIN 7 +# define BATTERY_LEVEL_PIN B5 # endif #endif @@ -556,7 +556,7 @@ void adafruit_ble_task(void) { if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) { state.last_battery_update = timer_read(); - state.vbat = analogRead(BATTERY_LEVEL_PIN); + state.vbat = analogReadPin(BATTERY_LEVEL_PIN); } #endif } -- cgit v1.2.3 From d4be07dad368c57669c88ead6c093c9e23086855 Mon Sep 17 00:00:00 2001 From: a-chol Date: Sat, 25 Jul 2020 14:01:15 +0200 Subject: Hid joystick interface (#4226) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan * Update docs/feature_joystick.md Co-Authored-By: Ryan * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol Co-authored-by: José Júnior Co-authored-by: a-chol Co-authored-by: Nick Brassel Co-authored-by: Ryan --- tmk_core/protocol/lufa/lufa.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 6776a964e9..85603646db 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -85,6 +85,10 @@ extern keymap_config_t keymap_config; # include "raw_hid.h" #endif +#ifdef JOYSTICK_ENABLE +# include "joystick.h" +#endif + uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ uint8_t keyboard_protocol = 1; @@ -263,6 +267,66 @@ static void Console_Task(void) { } #endif +/******************************************************************************* + * Joystick + ******************************************************************************/ +#ifdef JOYSTICK_ENABLE +void send_joystick_packet(joystick_t *joystick) { + uint8_t timeout = 255; + + joystick_report_t r = { +# if JOYSTICK_AXES_COUNT > 0 + .axes = {joystick->axes[0], + +# if JOYSTICK_AXES_COUNT >= 2 + joystick->axes[1], +# endif +# if JOYSTICK_AXES_COUNT >= 3 + joystick->axes[2], +# endif +# if JOYSTICK_AXES_COUNT >= 4 + joystick->axes[3], +# endif +# if JOYSTICK_AXES_COUNT >= 5 + joystick->axes[4], +# endif +# if JOYSTICK_AXES_COUNT >= 6 + joystick->axes[5], +# endif + }, +# endif // JOYSTICK_AXES_COUNT>0 + +# if JOYSTICK_BUTTON_COUNT > 0 + .buttons = {joystick->buttons[0], + +# if JOYSTICK_BUTTON_COUNT > 8 + joystick->buttons[1], +# endif +# if JOYSTICK_BUTTON_COUNT > 16 + joystick->buttons[2], +# endif +# if JOYSTICK_BUTTON_COUNT > 24 + joystick->buttons[3], +# endif + } +# endif // JOYSTICK_BUTTON_COUNT>0 + }; + + /* Select the Joystick Report Endpoint */ + Endpoint_SelectEndpoint(JOYSTICK_IN_EPNUM); + + /* Check if write ready for a polling interval around 10ms */ + while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40); + if (!Endpoint_IsReadWriteAllowed()) return; + + /* Write Joystick Report Data */ + Endpoint_Write_Stream_LE(&r, sizeof(joystick_report_t), NULL); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); +} +#endif + /******************************************************************************* * USB Events ******************************************************************************/ @@ -411,6 +475,9 @@ void EVENT_USB_Device_ConfigurationChanged(void) { ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1); #endif +#ifdef JOYSTICK_ENABLE + ConfigSuccess &= ENDPOINT_CONFIG(JOYSTICK_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, ENDPOINT_BANK_SINGLE); +#endif } /* FIXME: Expose this table in the docs somehow -- cgit v1.2.3 From 12333b7c5e47f935c8e11276caebae464331df8a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 8 Aug 2020 10:24:29 +1000 Subject: Fix joystick compile issues (#9949) --- tmk_core/protocol/lufa/lufa.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 85603646db..8410eb483f 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -475,8 +475,10 @@ void EVENT_USB_Device_ConfigurationChanged(void) { ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_OUT_EPNUM | ENDPOINT_DIR_OUT), EP_TYPE_BULK, CDC_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint((CDC_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, CDC_EPSIZE, 1); #endif + #ifdef JOYSTICK_ENABLE - ConfigSuccess &= ENDPOINT_CONFIG(JOYSTICK_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, JOYSTICK_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Setup joystick endpoint */ + ConfigSuccess &= Endpoint_ConfigureEndpoint((JOYSTICK_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1); #endif } -- cgit v1.2.3 From 3f392c09b60d46d9e4a4d3cc150a26294dea4da7 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 19 Aug 2020 22:46:15 +1000 Subject: More Bluetooth refactoring (#9905) --- tmk_core/protocol/lufa/adafruit_ble.h | 23 +++++++++++------------ tmk_core/protocol/lufa/outputselect.c | 14 ++++++++++++-- tmk_core/protocol/lufa/outputselect.h | 6 +++++- 3 files changed, 28 insertions(+), 15 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.h b/tmk_core/protocol/lufa/adafruit_ble.h index cef46fe9f7..aebded7b31 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.h +++ b/tmk_core/protocol/lufa/adafruit_ble.h @@ -2,18 +2,19 @@ * Author: Wez Furlong, 2016 * Supports the Adafruit BLE board built around the nRF51822 chip. */ + #pragma once -#ifdef MODULE_ADAFRUIT_BLE -# include -# include -# include -# include "config_common.h" -# include "progmem.h" +#include +#include +#include + +#include "config_common.h" +#include "progmem.h" -# ifdef __cplusplus +#ifdef __cplusplus extern "C" { -# endif +#endif /* Instruct the module to enable HID keyboard support and reset */ extern bool adafruit_ble_enable_keyboard(void); @@ -54,8 +55,6 @@ extern uint32_t adafruit_ble_read_battery_voltage(void); extern bool adafruit_ble_set_mode_leds(bool on); extern bool adafruit_ble_set_power_level(int8_t level); -# ifdef __cplusplus +#ifdef __cplusplus } -# endif - -#endif // MODULE_ADAFRUIT_BLE +#endif diff --git a/tmk_core/protocol/lufa/outputselect.c b/tmk_core/protocol/lufa/outputselect.c index b115ea9691..f758c65280 100644 --- a/tmk_core/protocol/lufa/outputselect.c +++ b/tmk_core/protocol/lufa/outputselect.c @@ -12,8 +12,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "lufa.h" #include "outputselect.h" + +#if defined(PROTOCOL_LUFA) +# include "lufa.h" +#endif + #ifdef MODULE_ADAFRUIT_BLE # include "adafruit_ble.h" #endif @@ -35,12 +39,18 @@ void set_output(uint8_t output) { */ __attribute__((weak)) void set_output_user(uint8_t output) {} +static bool is_usb_configured(void) { +#if defined(PROTOCOL_LUFA) + return USB_DeviceState == DEVICE_STATE_Configured; +#endif +} + /** \brief Auto Detect Output * * FIXME: Needs doc */ uint8_t auto_detect_output(void) { - if (USB_DeviceState == DEVICE_STATE_Configured) { + if (is_usb_configured()) { return OUTPUT_USB; } diff --git a/tmk_core/protocol/lufa/outputselect.h b/tmk_core/protocol/lufa/outputselect.h index 24fe4daa24..7f7ed00b95 100644 --- a/tmk_core/protocol/lufa/outputselect.h +++ b/tmk_core/protocol/lufa/outputselect.h @@ -12,6 +12,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#pragma once + +#include + enum outputs { OUTPUT_AUTO, @@ -37,4 +41,4 @@ enum outputs { void set_output(uint8_t output); void set_output_user(uint8_t output); uint8_t auto_detect_output(void); -uint8_t where_to_send(void); \ No newline at end of file +uint8_t where_to_send(void); -- cgit v1.2.3 From 0f596881b36587a298568192078f9b467c51c109 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 25 Aug 2020 19:03:45 +1000 Subject: Remove support for Adafruit EZ-Key (#10103) * Remove support for Adafruit EZ-Key * Update docs/ja/feature_bluetooth.md Co-authored-by: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Co-authored-by: Takeshi ISHII <2170248+mtei@users.noreply.github.com> --- tmk_core/protocol/lufa/bluetooth.c | 38 -------------- tmk_core/protocol/lufa/bluetooth.h | 96 --------------------------------- tmk_core/protocol/lufa/lufa.c | 105 ++++++++++++++++++++++--------------- 3 files changed, 62 insertions(+), 177 deletions(-) delete mode 100644 tmk_core/protocol/lufa/bluetooth.c delete mode 100644 tmk_core/protocol/lufa/bluetooth.h (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/bluetooth.c b/tmk_core/protocol/lufa/bluetooth.c deleted file mode 100644 index 5eb52860b1..0000000000 --- a/tmk_core/protocol/lufa/bluetooth.c +++ /dev/null @@ -1,38 +0,0 @@ -/* -Bluefruit Protocol for TMK firmware -Author: Benjamin Gould, 2013 - Jack Humbert, 2015 -Based on code Copyright 2011 Jun Wako -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include -#include "report.h" -#include "print.h" -#include "debug.h" -#include "bluetooth.h" - -void bluefruit_keyboard_print_report(report_keyboard_t *report) { - if (!debug_keyboard) return; - dprintf("keys: "); - for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - debug_hex8(report->keys[i]); - dprintf(" "); - } - dprintf(" mods: "); - debug_hex8(report->mods); - dprintf(" reserved: "); - debug_hex8(report->reserved); - dprintf("\n"); -} - -void bluefruit_serial_send(uint8_t data) { serial_send(data); } \ No newline at end of file diff --git a/tmk_core/protocol/lufa/bluetooth.h b/tmk_core/protocol/lufa/bluetooth.h deleted file mode 100644 index 67f031439e..0000000000 --- a/tmk_core/protocol/lufa/bluetooth.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -Bluefruit Protocol for TMK firmware -Author: Benjamin Gould, 2013 - Jack Humbert, 2015 -Based on code Copyright 2011 Jun Wako -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#pragma once - -#include "../serial.h" - -void bluefruit_serial_send(uint8_t data); - -// https://learn.adafruit.com/introducing-bluefruit-ez-key-diy-bluetooth-hid-keyboard/sending-keys-via-serial#raw-hid-consumer-reports-8-14 -static inline uint16_t CONSUMER2BLUEFRUIT(uint16_t usage) { - switch (usage) { - case AC_HOME: - return 0x0001; - case AL_KEYBOARD_LAYOUT: - return 0x0002; - case AC_SEARCH: - return 0x0004; - case SNAPSHOT: - return 0x0008; - case AUDIO_VOL_UP: - return 0x0010; - case AUDIO_VOL_DOWN: - return 0x0020; - case TRANSPORT_PLAY_PAUSE: - return 0x0040; - case TRANSPORT_FAST_FORWARD: - return 0x0080; - case TRANSPORT_REWIND: - return 0x0100; - case TRANSPORT_NEXT_TRACK: - return 0x0200; - case TRANSPORT_PREV_TRACK: - return 0x0400; - case TRANSPORT_RANDOM_PLAY: - return 0x0800; - case TRANSPORT_STOP: - return 0x1000; - default: - return 0; - } -} - -// https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734 -static inline uint16_t CONSUMER2RN42(uint16_t usage) { - switch (usage) { - case AC_HOME: - return 0x0001; - case AL_EMAIL: - return 0x0002; - case AC_SEARCH: - return 0x0004; - case AL_KEYBOARD_LAYOUT: - return 0x0008; - case AUDIO_VOL_UP: - return 0x0010; - case AUDIO_VOL_DOWN: - return 0x0020; - case AUDIO_MUTE: - return 0x0040; - case TRANSPORT_PLAY_PAUSE: - return 0x0080; - case TRANSPORT_NEXT_TRACK: - return 0x0100; - case TRANSPORT_PREV_TRACK: - return 0x0200; - case TRANSPORT_STOP: - return 0x0400; - case TRANSPORT_EJECT: - return 0x0800; - case TRANSPORT_FAST_FORWARD: - return 0x1000; - case TRANSPORT_REWIND: - return 0x2000; - case TRANSPORT_STOP_EJECT: - return 0x4000; - case AL_LOCAL_BROWSER: - return 0x8000; - default: - return 0; - } -} diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 8410eb483f..09ba0bacf7 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -69,7 +69,7 @@ extern keymap_config_t keymap_config; # ifdef MODULE_ADAFRUIT_BLE # include "adafruit_ble.h" # else -# include "bluetooth.h" +# include "../serial.h" # endif #endif @@ -89,6 +89,46 @@ extern keymap_config_t keymap_config; # include "joystick.h" #endif +// https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734 +static inline uint16_t CONSUMER2RN42(uint16_t usage) { + switch (usage) { + case AC_HOME: + return 0x0001; + case AL_EMAIL: + return 0x0002; + case AC_SEARCH: + return 0x0004; + case AL_KEYBOARD_LAYOUT: + return 0x0008; + case AUDIO_VOL_UP: + return 0x0010; + case AUDIO_VOL_DOWN: + return 0x0020; + case AUDIO_MUTE: + return 0x0040; + case TRANSPORT_PLAY_PAUSE: + return 0x0080; + case TRANSPORT_NEXT_TRACK: + return 0x0100; + case TRANSPORT_PREV_TRACK: + return 0x0200; + case TRANSPORT_STOP: + return 0x0400; + case TRANSPORT_EJECT: + return 0x0800; + case TRANSPORT_FAST_FORWARD: + return 0x1000; + case TRANSPORT_REWIND: + return 0x2000; + case TRANSPORT_STOP_EJECT: + return 0x4000; + case AL_LOCAL_BROWSER: + return 0x8000; + default: + return 0; + } +} + uint8_t keyboard_idle = 0; /* 0: Boot Protocol, 1: Report Protocol(default) */ uint8_t keyboard_protocol = 1; @@ -631,20 +671,13 @@ static void send_keyboard(report_keyboard_t *report) { # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); # elif MODULE_RN42 - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x09); - bluefruit_serial_send(0x01); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); - for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); - } -# else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(report->mods); - bluefruit_serial_send(report->reserved); + serial_send(0xFD); + serial_send(0x09); + serial_send(0x01); + serial_send(report->mods); + serial_send(report->reserved); for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { - bluefruit_serial_send(report->keys[i]); + serial_send(report->keys[i]); } # endif } @@ -697,15 +730,15 @@ static void send_mouse(report_mouse_t *report) { // FIXME: mouse buttons adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); # else - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x03); - bluefruit_serial_send(report->buttons); - bluefruit_serial_send(report->x); - bluefruit_serial_send(report->y); - bluefruit_serial_send(report->v); // should try sending the wheel v here - bluefruit_serial_send(report->h); // should try sending the wheel h here - bluefruit_serial_send(0x00); + serial_send(0xFD); + serial_send(0x00); + serial_send(0x03); + serial_send(report->buttons); + serial_send(report->x); + serial_send(report->y); + serial_send(report->v); // should try sending the wheel v here + serial_send(report->h); // should try sending the wheel h here + serial_send(0x00); # endif } @@ -778,25 +811,11 @@ static void send_consumer(uint16_t data) { if (data == last_data) return; last_data = data; uint16_t bitmap = CONSUMER2RN42(data); - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x03); - bluefruit_serial_send(0x03); - bluefruit_serial_send(bitmap & 0xFF); - bluefruit_serial_send((bitmap >> 8) & 0xFF); -# else - static uint16_t last_data = 0; - if (data == last_data) return; - last_data = data; - uint16_t bitmap = CONSUMER2BLUEFRUIT(data); - bluefruit_serial_send(0xFD); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x02); - bluefruit_serial_send(bitmap & 0xFF); - bluefruit_serial_send((bitmap >> 8) & 0xFF); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x00); - bluefruit_serial_send(0x00); + serial_send(0xFD); + serial_send(0x03); + serial_send(0x03); + serial_send(bitmap & 0xFF); + serial_send((bitmap >> 8) & 0xFF); # endif } @@ -1018,7 +1037,7 @@ int main(void) { setup_usb(); sei(); -#if defined(MODULE_ADAFRUIT_EZKEY) || defined(MODULE_RN42) +#if defined(MODULE_RN42) serial_init(); #endif -- cgit v1.2.3 From a3db72df7299140e52f57d082a3742a8b480a226 Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Sat, 29 Aug 2020 22:57:48 +0000 Subject: format code according to conventions [skip ci] --- tmk_core/protocol/lufa/adafruit_ble.h | 4 ++-- tmk_core/protocol/lufa/lufa.c | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.h b/tmk_core/protocol/lufa/adafruit_ble.h index aebded7b31..9dfc9b4355 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.h +++ b/tmk_core/protocol/lufa/adafruit_ble.h @@ -41,12 +41,12 @@ extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uin * (milliseconds) */ extern bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration); -# ifdef MOUSE_ENABLE +#ifdef MOUSE_ENABLE /* Send a mouse/wheel movement report. * The parameters are signed and indicate positive of negative direction * change. */ extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); -# endif +#endif /* Compute battery voltage by reading an analog pin. * Returns the integer number of millivolts */ diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 09ba0bacf7..cec0044026 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -316,39 +316,43 @@ void send_joystick_packet(joystick_t *joystick) { joystick_report_t r = { # if JOYSTICK_AXES_COUNT > 0 - .axes = {joystick->axes[0], + .axes = + { + joystick->axes[0], # if JOYSTICK_AXES_COUNT >= 2 - joystick->axes[1], + joystick->axes[1], # endif # if JOYSTICK_AXES_COUNT >= 3 - joystick->axes[2], + joystick->axes[2], # endif # if JOYSTICK_AXES_COUNT >= 4 - joystick->axes[3], + joystick->axes[3], # endif # if JOYSTICK_AXES_COUNT >= 5 - joystick->axes[4], + joystick->axes[4], # endif # if JOYSTICK_AXES_COUNT >= 6 - joystick->axes[5], + joystick->axes[5], # endif - }, + }, # endif // JOYSTICK_AXES_COUNT>0 # if JOYSTICK_BUTTON_COUNT > 0 - .buttons = {joystick->buttons[0], + .buttons = + { + joystick->buttons[0], # if JOYSTICK_BUTTON_COUNT > 8 - joystick->buttons[1], + joystick->buttons[1], # endif # if JOYSTICK_BUTTON_COUNT > 16 - joystick->buttons[2], + joystick->buttons[2], # endif # if JOYSTICK_BUTTON_COUNT > 24 - joystick->buttons[3], + joystick->buttons[3], # endif - } + } # endif // JOYSTICK_BUTTON_COUNT>0 }; -- cgit v1.2.3 From c66df1664497546f32662409778731143e45a552 Mon Sep 17 00:00:00 2001 From: James Young <18669334+noroadsleft@users.noreply.github.com> Date: Sat, 28 Nov 2020 12:02:18 -0800 Subject: 2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change --- tmk_core/protocol/lufa/lufa.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index cec0044026..878be7d342 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -1104,6 +1104,10 @@ int main(void) { #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif + + // Run housekeeping + housekeeping_task_kb(); + housekeeping_task_user(); } } -- cgit v1.2.3 From 501f2fdef115314713e94428d409e5c3b5bfc1c2 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 11 Dec 2020 13:45:24 +1100 Subject: Normalise include statements in core code (#11153) * Normalise include statements in core code * Missed one --- tmk_core/protocol/lufa/lufa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 878be7d342..8fd4be8af5 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -61,7 +61,7 @@ extern keymap_config_t keymap_config; #endif #ifdef AUDIO_ENABLE -# include +# include "audio.h" #endif #ifdef BLUETOOTH_ENABLE -- cgit v1.2.3 From 1d1d5da43f86d9dded47c66afec94991d623f114 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Dec 2020 15:56:11 +1100 Subject: Change include guards in tmk_core/ and drivers/ to pragma once (#11240) --- tmk_core/protocol/lufa/lufa.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.h b/tmk_core/protocol/lufa/lufa.h index 71fd7aad85..348a84c031 100644 --- a/tmk_core/protocol/lufa/lufa.h +++ b/tmk_core/protocol/lufa/lufa.h @@ -36,8 +36,7 @@ this software. */ -#ifndef _LUFA_H_ -#define _LUFA_H_ +#pragma once #include #include @@ -68,5 +67,3 @@ extern host_driver_t lufa_driver; // The header and terminator are not stored to save a few bytes of precious ram # define MIDI_SYSEX_BUFFER (API_SYSEX_MAX_SIZE + API_SYSEX_MAX_SIZE / 7 + (API_SYSEX_MAX_SIZE % 7 ? 1 : 0)) #endif - -#endif -- cgit v1.2.3 From 84e2f1ec17f12234e6c795e3855914316812cd4d Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 15 Jan 2021 14:32:00 +1100 Subject: Adafruit BLE cleanups (#11556) --- tmk_core/protocol/lufa/adafruit_ble.cpp | 45 +++++++++++++++------------------ tmk_core/protocol/lufa/adafruit_ble.h | 11 ++++---- tmk_core/protocol/lufa/lufa.c | 2 +- 3 files changed, 26 insertions(+), 32 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/adafruit_ble.cpp b/tmk_core/protocol/lufa/adafruit_ble.cpp index 79b35fca31..3f2cc35734 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.cpp +++ b/tmk_core/protocol/lufa/adafruit_ble.cpp @@ -11,6 +11,7 @@ #include "spi_master.h" #include "wait.h" #include "analog.h" +#include "progmem.h" // These are the pin assignments for the 32u4 boards. // You may define them to something else in your config.h @@ -36,10 +37,8 @@ #define SAMPLE_BATTERY #define ConnectionUpdateInterval 1000 /* milliseconds */ -#ifdef SAMPLE_BATTERY -# ifndef BATTERY_LEVEL_PIN -# define BATTERY_LEVEL_PIN B5 -# endif +#ifndef BATTERY_LEVEL_PIN +# define BATTERY_LEVEL_PIN B5 #endif static struct { @@ -118,15 +117,15 @@ enum sdep_type { SdepResponse = 0x20, SdepAlert = 0x40, SdepError = 0x80, - SdepSlaveNotReady = 0xfe, // Try again later - SdepSlaveOverflow = 0xff, // You read more data than is available + SdepSlaveNotReady = 0xFE, // Try again later + SdepSlaveOverflow = 0xFF, // You read more data than is available }; enum ble_cmd { - BleInitialize = 0xbeef, - BleAtWrapper = 0x0a00, - BleUartTx = 0x0a01, - BleUartRx = 0x0a02, + BleInitialize = 0xBEEF, + BleAtWrapper = 0x0A00, + BleUartTx = 0x0A01, + BleUartRx = 0x0A02, }; enum ble_system_event_bits { @@ -176,7 +175,7 @@ static bool sdep_send_pkt(const struct sdep_msg *msg, uint16_t timeout) { static inline void sdep_build_pkt(struct sdep_msg *msg, uint16_t command, const uint8_t *payload, uint8_t len, bool moredata) { msg->type = SdepCommand; - msg->cmd_low = command & 0xff; + msg->cmd_low = command & 0xFF; msg->cmd_high = command >> 8; msg->len = len; msg->more = (moredata && len == SdepMaxPayload) ? 1 : 0; @@ -407,11 +406,11 @@ static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbo } if (resp == NULL) { - auto now = timer_read(); + uint16_t now = timer_read(); while (!resp_buf.enqueue(now)) { resp_buf_read_one(false); } - auto later = timer_read(); + uint16_t later = timer_read(); if (TIMER_DIFF_16(later, now) > 0) { dprintf("waited %dms for resp_buf\n", TIMER_DIFF_16(later, now)); } @@ -422,7 +421,7 @@ static bool at_command(const char *cmd, char *resp, uint16_t resplen, bool verbo } bool at_command_P(const char *cmd, char *resp, uint16_t resplen, bool verbose) { - auto cmdbuf = (char *)alloca(strlen_P(cmd) + 1); + char *cmdbuf = (char *)alloca(strlen_P(cmd) + 1); strcpy_P(cmdbuf, cmd); return at_command(cmdbuf, resp, resplen, verbose); } @@ -484,9 +483,9 @@ fail: static void set_connected(bool connected) { if (connected != state.is_connected) { if (connected) { - print("****** BLE CONNECT!!!!\n"); + dprint("BLE connected\n"); } else { - print("****** BLE DISCONNECT!!!!\n"); + dprint("BLE disconnected\n"); } state.is_connected = connected; @@ -612,7 +611,7 @@ static bool process_queue_item(struct queue_item *item, uint16_t timeout) { } } -bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { +void adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { struct queue_item item; bool didWait = false; @@ -638,30 +637,27 @@ bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nk } if (nkeys <= 6) { - return true; + return; } nkeys -= 6; keys += 6; } - - return true; } -bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration) { +void adafruit_ble_send_consumer_key(uint16_t usage) { struct queue_item item; item.queue_type = QTConsumer; - item.consumer = keycode; + item.consumer = usage; while (!send_buf.enqueue(item)) { send_buf_send_one(); } - return true; } #ifdef MOUSE_ENABLE -bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { +void adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { struct queue_item item; item.queue_type = QTMouseMove; @@ -674,7 +670,6 @@ bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, while (!send_buf.enqueue(item)) { send_buf_send_one(); } - return true; } #endif diff --git a/tmk_core/protocol/lufa/adafruit_ble.h b/tmk_core/protocol/lufa/adafruit_ble.h index 9dfc9b4355..b43e0771d9 100644 --- a/tmk_core/protocol/lufa/adafruit_ble.h +++ b/tmk_core/protocol/lufa/adafruit_ble.h @@ -10,7 +10,6 @@ #include #include "config_common.h" -#include "progmem.h" #ifdef __cplusplus extern "C" { @@ -35,17 +34,17 @@ extern void adafruit_ble_task(void); * this set of keys. * Also sends a key release indicator, so that the keys do not remain * held down. */ -extern bool adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); +extern void adafruit_ble_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); -/* Send a consumer keycode, holding it down for the specified duration +/* Send a consumer usage. * (milliseconds) */ -extern bool adafruit_ble_send_consumer_key(uint16_t keycode, int hold_duration); +extern void adafruit_ble_send_consumer_key(uint16_t usage); #ifdef MOUSE_ENABLE /* Send a mouse/wheel movement report. - * The parameters are signed and indicate positive of negative direction + * The parameters are signed and indicate positive or negative direction * change. */ -extern bool adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); +extern void adafruit_ble_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); #endif /* Compute battery voltage by reading an analog pin. diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 8fd4be8af5..623aa33ff5 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -809,7 +809,7 @@ static void send_consumer(uint16_t data) { if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { # ifdef MODULE_ADAFRUIT_BLE - adafruit_ble_send_consumer_key(data, 0); + adafruit_ble_send_consumer_key(data); # elif MODULE_RN42 static uint16_t last_data = 0; if (data == last_data) return; -- cgit v1.2.3 From 9a4618b05b9f1093908c2153c719c5eb5d4a79ee Mon Sep 17 00:00:00 2001 From: Joshua Diamond Date: Mon, 1 Feb 2021 19:12:41 -0500 Subject: Address wake from sleep instability (#11450) * resolve race condition between suspend and wake in LUFA * avoid multiple calls to suspend_power_down() / suspend_wakeup_init() * Remove duplicate suspend_power_down_kb() call * pause on wakeup to wait for USB state to settle * need the repeated suspend_power_down() (that's where the sleep is) * more efficient implementation * fine tune the pause after sending wakeup * speculative chibios version of pause-after-wake * make wakeup delay configurable, and adjust value * better location for wakeup delay --- tmk_core/protocol/lufa/lufa.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 623aa33ff5..74e48222d0 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -435,7 +435,9 @@ void EVENT_USB_Device_Suspend() { */ void EVENT_USB_Device_WakeUp() { print("[W]"); +#if defined(NO_USB_STARTUP_CHECK) suspend_wakeup_init(); +#endif #ifdef SLEEP_LED_ENABLE sleep_led_disable(); @@ -1073,12 +1075,26 @@ int main(void) { print("Keyboard start.\n"); while (1) { #if !defined(NO_USB_STARTUP_CHECK) - while (USB_DeviceState == DEVICE_STATE_Suspended) { + if (USB_DeviceState == DEVICE_STATE_Suspended) { print("[s]"); - suspend_power_down(); - if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { - USB_Device_SendRemoteWakeup(); + while (USB_DeviceState == DEVICE_STATE_Suspended) { + suspend_power_down(); + if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { + USB_Device_SendRemoteWakeup(); + clear_keyboard(); + +# if USB_SUSPEND_WAKEUP_DELAY > 0 + // Some hubs, kvm switches, and monitors do + // weird things, with USB device state bouncing + // around wildly on wakeup, yielding race + // conditions that can corrupt the keyboard state. + // + // Pause for a while to let things settle... + wait_ms(USB_SUSPEND_WAKEUP_DELAY); +# endif + } } + suspend_wakeup_init(); } #endif -- cgit v1.2.3 From 1f2fe2eab99548f4bde2a79b03e3303cc9b73214 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 14 Feb 2021 01:44:22 +0000 Subject: Refactor platform logic within print.h (#11863) * Remove GCC check from debug * Remove platform logic from common.mk * Refactor platform logic within print.h * restore debug.c format * headers * Rename function pointer type * review comments * Update tmk_core/common/printf.c Co-authored-by: Nick Brassel * Format Co-authored-by: Nick Brassel --- tmk_core/protocol/lufa/lufa.c | 1 - 1 file changed, 1 deletion(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 74e48222d0..bd9daaac90 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -1025,7 +1025,6 @@ static void setup_usb(void) { // for Console_Task USB_Device_EnableSOFEvents(); - print_set_sendchar(sendchar); } /** \brief Main -- cgit v1.2.3 From 7ab9f6a1010108c329f6880fe08716dd06a90daf Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 20 Feb 2021 18:11:02 +1100 Subject: Output selection: Remove "USB and BT" option (#11940) --- tmk_core/protocol/lufa/lufa.c | 21 +++------------------ tmk_core/protocol/lufa/outputselect.h | 14 ++------------ 2 files changed, 5 insertions(+), 30 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index bd9daaac90..f1908b3d07 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -671,9 +671,7 @@ static void send_keyboard(report_keyboard_t *report) { uint8_t timeout = 255; #ifdef BLUETOOTH_ENABLE - uint8_t where = where_to_send(); - - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { + if (where_to_send() == OUTPUT_BLUETOOTH) { # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_keys(report->mods, report->keys, sizeof(report->keys)); # elif MODULE_RN42 @@ -686,9 +684,6 @@ static void send_keyboard(report_keyboard_t *report) { serial_send(report->keys[i]); } # endif - } - - if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } #endif @@ -729,9 +724,7 @@ static void send_mouse(report_mouse_t *report) { uint8_t timeout = 255; # ifdef BLUETOOTH_ENABLE - uint8_t where = where_to_send(); - - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { + if (where_to_send() == OUTPUT_BLUETOOTH) { # ifdef MODULE_ADAFRUIT_BLE // FIXME: mouse buttons adafruit_ble_send_mouse_move(report->x, report->y, report->v, report->h, report->buttons); @@ -746,9 +739,6 @@ static void send_mouse(report_mouse_t *report) { serial_send(report->h); // should try sending the wheel h here serial_send(0x00); # endif - } - - if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } # endif @@ -807,9 +797,7 @@ static void send_system(uint16_t data) { static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE # ifdef BLUETOOTH_ENABLE - uint8_t where = where_to_send(); - - if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { + if (where_to_send() == OUTPUT_BLUETOOTH) { # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_consumer_key(data); # elif MODULE_RN42 @@ -823,9 +811,6 @@ static void send_consumer(uint16_t data) { serial_send(bitmap & 0xFF); serial_send((bitmap >> 8) & 0xFF); # endif - } - - if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { return; } # endif diff --git a/tmk_core/protocol/lufa/outputselect.h b/tmk_core/protocol/lufa/outputselect.h index 7f7ed00b95..c4548e1122 100644 --- a/tmk_core/protocol/lufa/outputselect.h +++ b/tmk_core/protocol/lufa/outputselect.h @@ -21,21 +21,11 @@ enum outputs { OUTPUT_NONE, OUTPUT_USB, - OUTPUT_BLUETOOTH, - - // backward compatibility - OUTPUT_USB_AND_BT + OUTPUT_BLUETOOTH }; -/** - * backward compatibility for BLUETOOTH_ENABLE, send to BT and USB by default - */ #ifndef OUTPUT_DEFAULT -# ifdef BLUETOOTH_ENABLE -# define OUTPUT_DEFAULT OUTPUT_USB_AND_BT -# else -# define OUTPUT_DEFAULT OUTPUT_AUTO -# endif +# define OUTPUT_DEFAULT OUTPUT_AUTO #endif void set_output(uint8_t output); -- cgit v1.2.3 From dbd65d01b656e0e43511da4b144dc3408f3046d1 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Sun, 25 Apr 2021 06:11:41 +0300 Subject: Fix how USB queue overflow is handled in chibios. (#12576) * Fix how USB queue overflow is handled in chibios. This commit reverts PR 12472 (commit c823fe2d3f23ed090e36ce39beed4c448298bd2f), and it implements the original intent of the commit in a better way. The original intent of the above mentioned commit was to not deadlock the keyboard when console is enabled, and hid_listen is not started. The above mentioned commit had a few drawbacks: 1) When a lot of data was printed to the console, the queue would get full, and drop data, even if hid_listen was running. (For example having matrix debug enabled just didn't work right at all) 2) I believe the function in which this was implemented is used by all other USB endpoints, so with the above change, overflow, and data loss could happen in other important functions of QMK as well. This commit implements deadlock prevention in a slightly similar way to how it's done on AVR. There is an additional static local variable, that memorizes whether the console has timeouted before. If we are in the timeouted=false state, then we send the character normally with a 5ms timeout. If it does time out, then hid_listen is likely not running, and future characters should not be sent with a timeout, but those characters should still be sent if there is space in the queue. The difference between the AVR implementation and this one is that the AVR implementation checks the queue state directly, but this implementation instead attempts to write the character with a zero timeout. If it fails, then we remain in the timeouted=true state, if it succeeds, then hid_listen started removing data from the queue, so we can go out of the timeouted=true state. * Added comment explaining the timeouted logic to console flow control. * Console flow control: refactor chibios flowcontrol code to make it more readable, and rename the timeouted variable to timed_out on both chibios and lufa. Changed comments to says timed_out is an approximation of listener_disconnected, to make it clear that it's not the same thing * fix typo --- tmk_core/protocol/lufa/lufa.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'tmk_core/protocol/lufa') diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index f1908b3d07..85d71d0835 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -829,9 +829,10 @@ static void send_consumer(uint16_t data) { * FIXME: Needs doc */ int8_t sendchar(uint8_t c) { - // Not wait once timeouted. + // Do not wait if the previous write has timed_out. // Because sendchar() is called so many times, waiting each call causes big lag. - static bool timeouted = false; + // The `timed_out` state is an approximation of the ideal `is_listener_disconnected?` state. + static bool timed_out = false; // prevents Console_Task() from running during sendchar() runs. // or char will be lost. These two function is mutually exclusive. @@ -845,11 +846,11 @@ int8_t sendchar(uint8_t c) { goto ERROR_EXIT; } - if (timeouted && !Endpoint_IsReadWriteAllowed()) { + if (timed_out && !Endpoint_IsReadWriteAllowed()) { goto ERROR_EXIT; } - timeouted = false; + timed_out = false; uint8_t timeout = SEND_TIMEOUT; while (!Endpoint_IsReadWriteAllowed()) { @@ -860,7 +861,7 @@ int8_t sendchar(uint8_t c) { goto ERROR_EXIT; } if (!(timeout--)) { - timeouted = true; + timed_out = true; goto ERROR_EXIT; } _delay_ms(1); -- cgit v1.2.3