[Fw] Add many utils.

pull/140/head
稚晖 2 years ago
parent 77d2c2b490
commit d612c6f71d

@ -69,9 +69,11 @@ uint8_t* HWKeyboard::Remap(uint8_t _layer)
}
memset(hidBuffer, 0, KEY_REPORT_SIZE);
for (int i = 0; i < IO_NUMBER / 8; i++)
int i = 0, j = 0;
while (8 * i + j < IO_NUMBER - 6)
{
for (int j = 0; j < 8; j++)
for (j = 0; j < 8; j++)
{
index = (int16_t) (keyMap[_layer][i * 8 + j] / 8 + 1); // +1 for modifier
bitIndex = (int16_t) (keyMap[_layer][i * 8 + j] % 8);
@ -85,6 +87,8 @@ uint8_t* HWKeyboard::Remap(uint8_t _layer)
if (remapBuffer[i] & (0x80 >> j))
hidBuffer[index + 1] |= 1 << (bitIndex); // +1 for Report-ID
}
i++;
j = 0;
}
return hidBuffer;
@ -97,13 +101,19 @@ bool HWKeyboard::FnPressed()
}
void HWKeyboard::SetRgbBuffer(uint8_t _keyId, HWKeyboard::Color_t _color)
void HWKeyboard::SetRgbBufferByID(uint8_t _keyId, HWKeyboard::Color_t _color, float _brightness)
{
// To ensure there's no sequence zero bits, otherwise will case ws2812b protocol error.
if (_color.b < 1)_color.b = 1;
for (int i = 0; i < 8; i++)
{
rgbBuffer[_keyId][0][i] = (_color.g >> brightnessPrediv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
rgbBuffer[_keyId][1][i] = (_color.r >> brightnessPrediv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
rgbBuffer[_keyId][2][i] = (_color.b >> brightnessPrediv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
rgbBuffer[_keyId][0][i] =
((uint8_t) ((float) _color.g * _brightness) >> brightnessPreDiv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
rgbBuffer[_keyId][1][i] =
((uint8_t) ((float) _color.r * _brightness) >> brightnessPreDiv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
rgbBuffer[_keyId][2][i] =
((uint8_t) ((float) _color.b * _brightness) >> brightnessPreDiv) & (0x80 >> i) ? WS_HIGH : WS_LOW;
}
}
@ -133,3 +143,71 @@ uint8_t* HWKeyboard::GetHidReportBuffer(uint8_t _reportId)
return hidBuffer;
}
}
bool HWKeyboard::KeyPressed(KeyCode_t _key)
{
int index, bitIndex;
if (_key < RESERVED)
{
index = _key / 8;
bitIndex = (_key + 8) % 8;
} else
{
index = _key / 8 + 1;
bitIndex = _key % 8;
}
return hidBuffer[index + 1] & (1 << bitIndex);
}
void HWKeyboard::Press(HWKeyboard::KeyCode_t _key)
{
int index, bitIndex;
if (_key < RESERVED)
{
index = _key / 8;
bitIndex = (_key + 8) % 8;
} else
{
index = _key / 8 + 1;
bitIndex = _key % 8;
}
hidBuffer[index + 1] |= (1 << bitIndex);
}
void HWKeyboard::Release(HWKeyboard::KeyCode_t _key)
{
int index, bitIndex;
if (_key < RESERVED)
{
index = _key / 8;
bitIndex = (_key + 8) % 8;
} else
{
index = _key / 8 + 1;
bitIndex = _key % 8;
}
hidBuffer[index + 1] &= ~(1 << bitIndex);
}
uint8_t HWKeyboard::GetTouchBarState(uint8_t _id)
{
uint8_t tmp = (remapBuffer[10] & 0b00000001) << 5 |
(remapBuffer[10] & 0b00000010) << 3 |
(remapBuffer[10] & 0b00000100) << 1 |
(remapBuffer[10] & 0b00001000) >> 1 |
(remapBuffer[10] & 0b00010000) >> 3 |
(remapBuffer[10] & 0b00100000) >> 5;
return _id == 0 ? tmp : (tmp & (1 << (_id - 1)));
}

@ -12,8 +12,12 @@ public:
{
scanBuffer = &spiBuffer[1];
// Enable 74HC165 clock
// Enable clock
HAL_GPIO_WritePin(CE_GPIO_Port,CE_Pin,GPIO_PIN_RESET);
// Init RGB buffer
for (uint8_t i = 0; i < HWKeyboard::LED_NUMBER; i++)
SetRgbBufferByID(i, HWKeyboard::Color_t{0, 0, 0});
}
@ -21,7 +25,6 @@ public:
static const uint8_t KEY_NUMBER = 82;
static const uint8_t TOUCHPAD_NUMBER = 6;
static const uint8_t LED_NUMBER = 104;
static const uint8_t KEYMAP_NUM = 5;
static const uint16_t KEY_REPORT_SIZE = 1 + 16;
static const uint16_t RAW_REPORT_SIZE = 1 + 32;
static const uint16_t HID_REPORT_SIZE = KEY_REPORT_SIZE + RAW_REPORT_SIZE;
@ -47,19 +50,11 @@ public:
PAD_SLASH,PAD_ASTERISK,PAD_MINUS,PAD_PLUS,PAD_ENTER,
PAD_NUM_1,PAD_NUM_2,PAD_NUM_3,PAD_NUM_4,PAD_NUM_5,
PAD_NUM_6,PAD_NUM_7,PAD_NUM_8,PAD_NUM_9,PAD_NUM_0,
PAD_PERIOD = 99, // followed with 20 bits 0, total 16 Bytes report data
PAD_PERIOD , NONUS_BACKSLASH,APPLICATION,POWER,PAD_EQUAL,
F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24, EXECUTE,
HELP,MENU,SELECT,STOP,AGAIN,UNDO,CUT,COPY,PASTE,FIND,MUTE,VOLUME_UP,VOLUME_DOWN,
FN = 1000
/*------------------------- HID report data -------------------------*/
// /*-------------------------- Virtual keys ---------------------------*/
// EXECUTE = 116,
// HELP, MENU, SELECT, STOP, AGAIN, UNDO, CUT, COPY, PASTE, FIND, MUTE,
// VOLUME_UP, VOLUME_DOWN,
// /*-------------------------- Virtual keys ---------------------------*/
/*--------------------------- Smart keys ----------------------------*/
FN = 1000,
TOUCH_BAR_1, TOUCH_BAR_2, TOUCH_BAR_3, TOUCH_BAR_4, TOUCH_BAR_5, TOUCH_BAR_6
/*--------------------------- Smart keys ----------------------------*/
};
struct Color_t
@ -68,54 +63,51 @@ public:
uint8_t g;
uint8_t b;
};
enum SpiWs2812Byte_t : uint8_t
{
// This should be tuned considering WS2812b clock timing
WS_HIGH = 0xFE,
WS_LOW = 0xE0
};
uint8_t* ScanKeyStates();
void ApplyDebounceFilter(uint32_t _filterTimeUs = 100);
uint8_t* Remap(uint8_t _layer = 1);
void SetRgbBuffer(uint8_t _keyId, Color_t _color);
void SyncLights();
bool FnPressed();
uint8_t* GetHidReportBuffer(uint8_t _reportId = 0);
bool KeyPressed(KeyCode_t _key);
void Press(KeyCode_t _key);
void Release(KeyCode_t _key);
uint8_t* GetHidReportBuffer(uint8_t _reportId);
uint8_t GetTouchBarState(uint8_t _id = 0);
void SetRgbBufferByID(uint8_t _keyId, Color_t _color, float _brightness = 1);
int16_t keyMap[KEYMAP_NUM][IO_NUMBER] = {
// The first layer, used for aligning 74HC165 IO pins to PCB key layout
int16_t keyMap[5][IO_NUMBER] = {
{67,61,60,58,59,52,55,51,50,49,48,47,46,3,
80,81,64,57,62,63,53,54,45,44,40,31,26,18,2,
19,70,71,66,65,56,36,37,38,39,43,42,41,28,1,
15,74,73,72,68,69,29,30,35,34,33,32,24,0,
14,76,77,78,79,16,20,21,22,23,27,25,17,4,
13,12,8,75,9,10,7,11,6,5,
82,83,84,85,86,87},
86,84,82,87,85,83}, // TouchBar index
// Other layer, used for mapping default key layout to custom layout
{ESC,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,PAUSE,
GRAVE_ACCENT,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9,NUM_0,MINUS,EQUAL,BACKSPACE,INSERT,
TAB,Q,W,E,R,T,Y,U,I,O,P,LEFT_U_BRACE,RIGHT_U_BRACE,BACKSLASH,DELETE,
CAP_LOCK,A,S,D,F,G,H,J,K,L,SEMI_COLON,QUOTE,ENTER,PAGE_UP,
LEFT_SHIFT,Z,X,C,V,B,N,M,COMMA,PERIOD,SLASH,RIGHT_SHIFT,UP_ARROW,PAGE_DOWN,
LEFT_CTRL, LEFT_GUI, LEFT_ALT, SPACE, RIGHT_ALT, RIGHT_GUI, FN, LEFT_ARROW, DOWN_ARROW, RIGHT_ARROW,
PAD_NUM_1,PAD_NUM_2,PAD_NUM_3,PAD_NUM_4,PAD_NUM_5,PAD_NUM_6},
LEFT_CTRL,LEFT_GUI,LEFT_ALT,SPACE,RIGHT_ALT,FN,RIGHT_CTRL,LEFT_ARROW,DOWN_ARROW,RIGHT_ARROW },
{ESC,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,PAUSE,
GRAVE_ACCENT,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9,NUM_0,MINUS,EQUAL,BACKSPACE,INSERT,
TAB, Q,W,E,R,T,Y,U,I,O,P,LEFT_U_BRACE, RIGHT_U_BRACE, BACKSLASH, DELETE,
CAP_LOCK, A,S,D,F,G,H,J,K,L,SEMI_COLON, QUOTE, ENTER, PAGE_UP,
LEFT_SHIFT, Z,X,C,V,B,N,M,COMMA, PERIOD, SLASH, RIGHT_SHIFT, A,PAGE_DOWN,
LEFT_CTRL, LEFT_GUI, LEFT_ALT, SPACE, RIGHT_ALT, RIGHT_GUI, FN, LEFT_ARROW, C,RIGHT_ARROW,
PAD_NUM_1,PAD_NUM_2,PAD_NUM_3,PAD_NUM_4,PAD_NUM_5,PAD_NUM_6}
TAB,A,B,C,D,E,F,G,H,I,J,LEFT_U_BRACE,RIGHT_U_BRACE,BACKSLASH,DELETE,
CAP_LOCK,K,L,M,N,O,P,Q,R,S,SEMI_COLON,QUOTE,ENTER,PAGE_UP,
LEFT_SHIFT,T,U,V,W,X,Y,Z,COMMA,PERIOD,SLASH,RIGHT_SHIFT,A,PAGE_DOWN,
LEFT_CTRL,LEFT_GUI,LEFT_ALT,SPACE,RIGHT_ALT,FN,RIGHT_CTRL,LEFT_ARROW,DOWN_ARROW,RIGHT_ARROW }
};
volatile bool isRgbTxBusy{};
uint8_t hidBuffer[HID_REPORT_SIZE]{};
volatile bool isRgbTxBusy;
bool isCapsLocked = false;
private:
@ -123,11 +115,11 @@ private:
uint8_t spiBuffer[IO_NUMBER / 8 + 1]{};
uint8_t* scanBuffer;
uint8_t debounceBuffer[IO_NUMBER / 8 + 1]{};
uint8_t hidBuffer[HID_REPORT_SIZE]{};
uint8_t remapBuffer[IO_NUMBER / 8]{};
uint8_t rgbBuffer[LED_NUMBER][3][8]{};
uint8_t wsCommit[64] = {0};
uint8_t brightnessPrediv = 2; // means 1/4 brightness
uint8_t brightnessPreDiv = 2; // 1/4
};

@ -40,7 +40,7 @@ void Main()
else if (t < 1) fadeDir = true;
for (uint8_t i = 0; i < HWKeyboard::LED_NUMBER; i++)
keyboard.SetRgbBuffer(i, HWKeyboard::Color_t{(uint8_t) t, 50, 0});
keyboard.SetRgbBufferByID(i, HWKeyboard::Color_t{(uint8_t) t, 50, 0});
/*-----------------------------------*/
// Send RGB buffers to LEDs
@ -55,6 +55,15 @@ extern "C" void OnTimerCallback() // 1000Hz callback
keyboard.ApplyDebounceFilter(100);
keyboard.Remap(keyboard.FnPressed() ? 2 : 1); // When Fn pressed use layer-2
if (keyboard.KeyPressed(HWKeyboard::LEFT_CTRL) &&
keyboard.KeyPressed(HWKeyboard::A))
{
// do something...
// or trigger some keys...
keyboard.Press(HWKeyboard::DELETE);
}
// Report HID key states
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS,
keyboard.GetHidReportBuffer(1),

Loading…
Cancel
Save