feat: adds pairing key

This commit is contained in:
Florian Didron
2019-11-05 19:02:07 +09:00
committed by Florian Didron
parent 40e9813ba2
commit f3edef8c69
10 changed files with 88 additions and 9 deletions

View File

@@ -288,6 +288,10 @@ ifeq ($(strip $(USB_HID_ENABLE)), yes)
include $(TMK_DIR)/protocol/usb_hid.mk
endif
ifeq ($(strip $(WEBUSB_ENABLE)), yes)
SRC += $(TMK_DIR)/common/webusb.c
endif
ifeq ($(strip $(ENCODER_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/encoder.c
OPT_DEFS += -DENCODER_ENABLE

View File

@@ -57,6 +57,10 @@
# include "encoder.h"
#endif
#ifdef WEBUSB_ENABLE
# include "webusb.h"
#endif
#ifdef AUDIO_ENABLE
# ifndef GOODBYE_SONG
# define GOODBYE_SONG SONG(GOODBYE_SOUND)
@@ -712,6 +716,13 @@ bool process_record_quantum(keyrecord_t *record) {
}
return false;
}
#endif
#ifdef WEBUSB_ENABLE
case WEBUSB_PAIR:
if (record->event.pressed) {
webusb_state.paired = true;
}
return false;
#endif
}

View File

@@ -503,6 +503,9 @@ enum quantum_keycodes {
MAGIC_UNSWAP_CTL_GUI,
MAGIC_TOGGLE_CTL_GUI,
#ifdef WEBUSB_ENABLE
WEBUSB_PAIR,
#endif
// always leave at the end
SAFE_RANGE
};

24
tmk_core/common/webusb.c Normal file
View File

@@ -0,0 +1,24 @@
#include "webusb.h"
#include "wait.h"
webusb_state_t webusb_state = {
.paired = false,
.pairing = false,
};
void webusb_set_pairing_state() {
webusb_state.pairing = true;
uint8_t tick = 0;
do {
tick++;
wait_ms(1000);
//TODO Blink some leds
} while(webusb_state.paired == false && tick <= 30);
webusb_state.pairing = false;
}
void webusb_error(uint8_t code) {
uint8_t buffer[1];
buffer[0] = code;
webusb_send(buffer, 1);
}

24
tmk_core/common/webusb.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
void webusb_receive(uint8_t *data, uint8_t length);
void webusb_send(uint8_t *data, uint8_t length);
void webusb_error(uint8_t);
void webusb_set_pairing_state(void);
typedef struct{
bool paired;
bool pairing;
} webusb_state_t;
extern webusb_state_t webusb_state;
enum Webusb_Status_Code {
WEBUSB_STATUS_NOT_PAIRED = -1,
WEBUSB_STATUS_OK,
WEBUSB_STATUS_UNKNOWN_COMMAND,
};

View File

@@ -37,6 +37,9 @@
extern keymap_config_t keymap_config;
#endif
#ifdef WEBUSB_ENABLE
#include "webusb.h"
#endif
/* ---------------------------------------------------------
* Global interface variables and declarations
* ---------------------------------------------------------
@@ -880,8 +883,13 @@ void webusb_task(void) {
do {
size_t size = chnReadTimeout(&drivers.webusb_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
if (size > 0) {
if(webusb_state.paired == true) {
webusb_receive(buffer, size);
}
else {
webusb_error(WEBUSB_STATUS_NOT_PAIRED);
}
}
} while (size > 0);
}

View File

@@ -90,6 +90,10 @@ extern keymap_config_t keymap_config;
# include "raw_hid.h"
#endif
#ifdef WEBUSB_ENABLE
#include "webusb.h"
#endif
uint8_t keyboard_idle = 0;
/* 0: Boot Protocol, 1: Report Protocol(default) */
uint8_t keyboard_protocol = 1;
@@ -307,8 +311,13 @@ static void webusb_task(void) {
Endpoint_ClearOUT();
if (data_read) {
if(webusb_state.paired == true) {
webusb_receive(data, sizeof(data));
}
else {
webusb_error(WEBUSB_STATUS_NOT_PAIRED);
}
}
}
}

View File

@@ -40,7 +40,7 @@
#include "report.h"
#include "usb_descriptor.h"
#ifdef WEBUSB_ENABLE
#include "webusb.h"
#include "webusb_descriptor.h"
#endif
/*
* HID report descriptors

View File

@@ -49,7 +49,7 @@
# include "hal.h"
#endif
#ifdef WEBUSB_ENABLE
#include "webusb.h"
#include "webusb_descriptor.h"
#endif
/*

View File

@@ -1,9 +1,5 @@
#pragma once
void webusb_receive(uint8_t *data, uint8_t length);
void webusb_send(uint8_t *data, uint8_t length);
#ifndef WORD_TO_BYTES_LE
# define WORD_TO_BYTES_LE(n) n % 256, (n / 256) % 256
#endif