This commit is contained in:
2025-09-17 12:51:01 -07:00
parent 8e0ae599a7
commit 736b7f406d
25 changed files with 16650 additions and 15995 deletions

View File

@@ -46,7 +46,141 @@ extern "C" {
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
// Modifier Keys
#define KEY_LEFT_CTRL 0xE0
#define KEY_LEFT_SHIFT 0xE1
#define KEY_LEFT_ALT 0xE2
#define KEY_LEFT_GUI 0xE3
#define KEY_RIGHT_CTRL 0xE4
#define KEY_RIGHT_SHIFT 0xE5
#define KEY_RIGHT_ALT 0xE6
#define KEY_RIGHT_GUI 0xE7
// Regular Keys (Usage ID 0x040x73)
#define KEY_A 0x04
#define KEY_B 0x05
#define KEY_C 0x06
#define KEY_D 0x07
#define KEY_E 0x08
#define KEY_F 0x09
#define KEY_G 0x0A
#define KEY_H 0x0B
#define KEY_I 0x0C
#define KEY_J 0x0D
#define KEY_K 0x0E
#define KEY_L 0x0F
#define KEY_M 0x10
#define KEY_N 0x11
#define KEY_O 0x12
#define KEY_P 0x13
#define KEY_Q 0x14
#define KEY_R 0x15
#define KEY_S 0x16
#define KEY_T 0x17
#define KEY_U 0x18
#define KEY_V 0x19
#define KEY_W 0x1A
#define KEY_X 0x1B
#define KEY_Y 0x1C
#define KEY_Z 0x1D
#define KEY_1 0x1E
#define KEY_2 0x1F
#define KEY_3 0x20
#define KEY_4 0x21
#define KEY_5 0x22
#define KEY_6 0x23
#define KEY_7 0x24
#define KEY_8 0x25
#define KEY_9 0x26
#define KEY_0 0x27
#define KEY_ENTER 0x28
#define KEY_ESC 0x29
#define KEY_BACKSPACE 0x2A
#define KEY_TAB 0x2B
#define KEY_SPACE 0x2C
#define KEY_MINUS 0x2D
#define KEY_EQUAL 0x2E
#define KEY_LEFT_BRACKET 0x2F
#define KEY_RIGHT_BRACKET 0x30
#define KEY_BACKSLASH 0x31
#define KEY_NON_US_HASH 0x32
#define KEY_SEMICOLON 0x33
#define KEY_APOSTROPHE 0x34
#define KEY_GRAVE 0x35
#define KEY_COMMA 0x36
#define KEY_PERIOD 0x37
#define KEY_SLASH 0x38
#define KEY_CAPS_LOCK 0x39
// Function Keys
#define KEY_F1 0x3A
#define KEY_F2 0x3B
#define KEY_F3 0x3C
#define KEY_F4 0x3D
#define KEY_F5 0x3E
#define KEY_F6 0x3F
#define KEY_F7 0x40
#define KEY_F8 0x41
#define KEY_F9 0x42
#define KEY_F10 0x43
#define KEY_F11 0x44
#define KEY_F12 0x45
#define KEY_PRINT_SCREEN 0x46
#define KEY_SCROLL_LOCK 0x47
#define KEY_PAUSE 0x48
// Navigation Keys
#define KEY_INSERT 0x49
#define KEY_HOME 0x4A
#define KEY_PAGE_UP 0x4B
#define KEY_DELETE 0x4C
#define KEY_END 0x4D
#define KEY_PAGE_DOWN 0x4E
#define KEY_RIGHT_ARROW 0x4F
#define KEY_LEFT_ARROW 0x50
#define KEY_DOWN_ARROW 0x51
#define KEY_UP_ARROW 0x52
// Keypad
#define KEY_NUM_LOCK 0x53
#define KEYPAD_SLASH 0x54
#define KEYPAD_ASTERISK 0x55
#define KEYPAD_MINUS 0x56
#define KEYPAD_PLUS 0x57
#define KEYPAD_ENTER 0x58
#define KEYPAD_1 0x59
#define KEYPAD_2 0x5A
#define KEYPAD_3 0x5B
#define KEYPAD_4 0x5C
#define KEYPAD_5 0x5D
#define KEYPAD_6 0x5E
#define KEYPAD_7 0x5F
#define KEYPAD_8 0x60
#define KEYPAD_9 0x61
#define KEYPAD_0 0x62
#define KEYPAD_DOT 0x63
// Misc/Non-US
#define KEY_NON_US_BACKSLASH 0x64
#define KEY_APPLICATION 0x65
#define KEY_POWER 0x66
#define KEYPAD_EQUAL 0x67
#define KEY_F13 0x68
#define KEY_F14 0x69
#define KEY_F15 0x6A
#define KEY_F16 0x6B
#define KEY_F17 0x6C
#define KEY_F18 0x6D
#define KEY_F19 0x6E
#define KEY_F20 0x6F
#define KEY_F21 0x70
#define KEY_F22 0x71
#define KEY_F23 0x72
#define KEY_F24 0x73
/* USER CODE END EM */
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

View File

@@ -19,6 +19,7 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
#include <string.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
@@ -27,18 +28,24 @@
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct {
uint8_t MODIFIER;
uint8_t RESERVED;
uint8_t KEYCODE1;
uint8_t KEYCODE2;
uint8_t KEYCODE3;
uint8_t KEYCODE4;
uint8_t KEYCODE5;
uint8_t KEYCODE6;
}USBHIDReport;
typedef struct{
uint8_t MODIFIER;
uint8_t RESERVED;
uint8_t KEYPRESS[13];
}HIDReport;
USBHIDReport USBREPORT = {0,0,0,0,0,0,0,0};
typedef struct {
GPIO_TypeDef* GPIOx;
uint16_t PIN;
}SwitchPins;
typedef struct {
uint8_t buffer[256];
uint16_t head;
uint16_t tail;
uint16_t count;
} HIDQueue;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
@@ -48,10 +55,18 @@ USBHIDReport USBREPORT = {0,0,0,0,0,0,0,0};
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define ROW 2
#define COL 2
#define MAXQUEUE 256
#define MODE_INACTIVE 0
#define MODE_MAINBOARD 1
#define MODE_ACTIVE 2
#define MODE_DEBUG 3
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
I2C_HandleTypeDef hi2c1;
TIM_HandleTypeDef htim2;
@@ -63,11 +78,36 @@ UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
// Initialize HID report properly
HIDReport REPORT = {0, 0, {0}};
// Initialize column pins array (no pointer needed)
SwitchPins ROW_PINS[] = {
{GPIOC, GPIO_PIN_4},
{GPIOC, GPIO_PIN_5}
};
// Initialize row pins array
SwitchPins COLUMN_PINS[] = {
{GPIOC, GPIO_PIN_6},
{GPIOC, GPIO_PIN_7}
};
// Initialize keycodes array
uint8_t KEYCODES[2][2] = {
{KEY_M, KEY_I}, // 'M', 'I'
{KEY_K, KEY_U} // 'K', 'U'
};
extern USBD_HandleTypeDef hUsbDeviceFS;
volatile uint8_t MODE = MODE_MAINBOARD;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
@@ -78,8 +118,17 @@ static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
//Queue Functions (move this later as a separate c/h file)
uint8_t Q_IsEmpty(HIDQueue* q);
uint8_t Q_IsMax(HIDQueue*q);
uint8_t Q_Enqueue(HIDQueue* q);
uint8_t Q_Dequeue(HIDQueue* q);
uint8_t Q_Peek(HIDQueue* q);
void addUSBReport(uint8_t usageID);
void matrixScan(void);
void resetReport(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@@ -131,41 +180,33 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// 2x2 Matrix Keyboard: Rows = PC6, PC7; Cols = PC4, PC5
// Key mapping: [Row0-Col0]='M', [Row0-Col1]='I', [Row1-Col0]='K', [Row1-Col1]='U'
const uint8_t keycodes[2][2] = {
{0x10, 0x0C}, // 'M', 'I' (HID keycodes)
{0x0E, 0x18} // 'K', 'U'
};
uint8_t prev_state[2][2] = {{1,1},{1,1}}; // Inputs are HIGH by default
while (1)
{
for (int row = 0; row < 2; row++) {
// Set current row LOW, other HIGH
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, (row == 0) ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, (row == 1) ? GPIO_PIN_RESET : GPIO_PIN_SET);
HAL_Delay(1);
uint8_t col0 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);
uint8_t col1 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
uint8_t curr_state[2] = {col0, col1};
for (int col = 0; col < 2; col++) {
if (curr_state[col] == GPIO_PIN_RESET && prev_state[row][col] == GPIO_PIN_SET) {
// Key pressed (edge, input goes LOW)
USBREPORT.KEYCODE1 = keycodes[row][col];
USBD_HID_SendReport(&hUsbDeviceFS, &USBREPORT, sizeof(USBREPORT));
}
if (curr_state[col] == GPIO_PIN_SET && prev_state[row][col] == GPIO_PIN_RESET) {
// Key released (input goes HIGH)
USBREPORT.KEYCODE1 = 0;
USBD_HID_SendReport(&hUsbDeviceFS, &USBREPORT, sizeof(USBREPORT));
}
prev_state[row][col] = curr_state[col];
}
}
HAL_Delay(10);
/* USER CODE END WHILE */
if (MODE != MODE_INACTIVE){
//Reset Report
resetReport();
/* USER CODE BEGIN 3 */
//TODO: Append Child Module Reports
matrixScan();
switch (MODE){
case MODE_ACTIVE:
//TODO: Send to parent
//TODO: Check heartbeat signal from parent.
break;
case MODE_MAINBOARD:
//Send to USB
USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)&REPORT, sizeof(REPORT));
break;
}
//TODO: Send heartbeat signal to child nodes
}else{ //INACTIVE Mode
//TODO: Request parents
}
HAL_Delay(USBD_HID_GetPollingInterval(&hUsbDeviceFS));
}
/* USER CODE END 3 */
}
@@ -510,9 +551,9 @@ static void MX_USART3_UART_Init(void)
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
@@ -541,26 +582,26 @@ static void MX_GPIO_Init(void)
/*Configure GPIO pins : PC4 PC5 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 PB10 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PC6 PC7 PC8 PC9 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA8 */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -570,6 +611,52 @@ static void MX_GPIO_Init(void)
}
/* USER CODE BEGIN 4 */
/**
* @brief Returns whether the HID Queue is Full
* @retval uint8_t; 1 for full, 0 otherwise
* @param HIDQueue
*/
uint8_t Q_IsEmpty(HIDQueue* q){
return 0;
}
uint8_t Q_IsMax(HIDQueue*q){
return 0;
}
uint8_t Q_Enqueue(HIDQueue* q){
return 0;
}
uint8_t Q_Dequeue(HIDQueue* q){
return 0;
}
uint8_t Q_Peek(HIDQueue* q){
return 0;
}
void addUSBReport(uint8_t usageID){
if(usageID < 0x04 || usageID > 0x73) return; //Usage ID is out of bounds
uint16_t bit_index = usageID - 0x04; //Offset, UsageID starts with 0x04. Gives us the actual value of the bit
uint8_t byte_index = bit_index/8; //Calculates which byte in the REPORT array
uint8_t bit_offset = bit_index%8; //Calculates which bits in the REPORT[byte_index] should be set/unset
REPORT.KEYPRESS[byte_index] |= (1 << bit_offset);
}
void matrixScan(void){
for (uint8_t col = 0; col < COL; col++){
HAL_GPIO_WritePin(COLUMN_PINS[col].GPIOx, COLUMN_PINS[col].PIN, GPIO_PIN_SET);
HAL_Delay(1);
for(uint8_t row = 0; row < ROW; row++){
if(HAL_GPIO_ReadPin(ROW_PINS[row].GPIOx, ROW_PINS[row].PIN)){
addUSBReport(KEYCODES[row][col]);
}
}
HAL_GPIO_WritePin(COLUMN_PINS[col].GPIOx, COLUMN_PINS[col].PIN, GPIO_PIN_RESET);
}
}
void resetReport(void){
REPORT.MODIFIER = 0;
memset(REPORT.KEYPRESS, 0, sizeof(REPORT.KEYPRESS));
}
/* USER CODE END 4 */