Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Core/Inc/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#define DP_FONT_MEDIUM u8g2_font_NokiaLargeBold_tr

void drawSplashScreen(void);
void drawStartupScreen(uint8_t mode);
void drawStartupScreen();
void drawSensorBarInline();
void drawMeasurement(uint32_t baseline, uint32_t new, uint32_t latency);
void drawAverage(uint32_t latencies_us[], float mean_ms, float sd_ms);
void drawMainMenuInline(uint8_t index);
void drawMainMenuInline();
void drawParamsMenu(uint8_t index);
void drawError(char *error);
void drawGraphInline(uint32_t latencies_us[]);
Expand Down
2 changes: 2 additions & 0 deletions Core/Inc/display_assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ extern const unsigned char click_bitmap[];
extern const unsigned char move_bitmap[];

extern const unsigned char menu_selection_bitmap[];
extern const unsigned char menu_selection_2x_bitmap[];
extern const unsigned char cogwheel_bitmap[];
extern const unsigned char external_bitmap[];

extern const unsigned char left_bitmap[];
extern const unsigned char right_bitmap[];
Expand Down
5 changes: 3 additions & 2 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ extern volatile uint8_t wakeup_requested;
extern volatile uint8_t display_sleeping;

enum ParamMenuSelector { CYCLES = 0, THRESHOLD = 1, SENSOR = 2, EXIT = 3 };

enum MainMenuSelector { CLICK = 0, MOVE = 1, JIGGLER = 2, PARAMS = 3 };
enum MainMenuSelector { LATENCY = 0, JIGGLER = 1, PARAMS = 2 };
enum MainModeSelector { CLICK = 0, MOVE = 1, EXTERNAL = 2 };

extern enum MainMenuSelector mainMenuIndex;
extern enum MainModeSelector mainModeIndex;
extern enum ParamMenuSelector paramMenuIndex;

/* USER CODE END Private defines */
Expand Down
4 changes: 2 additions & 2 deletions Core/Inc/usb.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>

uint32_t startMouseAction();
void stopMouseAction();
int8_t startMouseAction();
int8_t stopMouseAction();
void randomMouseMove();
20 changes: 20 additions & 0 deletions Core/Src/buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ void pollMainMenuButtons() {
mainMenuIndex++;
btn_down_pressed = 0;
}

if (btn_left_pressed && mainMenuIndex == LATENCY) {
if (mainModeIndex == CLICK) {
mainModeIndex = EXTERNAL;
btn_left_pressed = 0;
return;
}
mainModeIndex--;
btn_left_pressed = 0;
}

if (btn_right_pressed && mainMenuIndex == LATENCY) {
if (mainModeIndex == EXTERNAL) {
mainModeIndex = CLICK;
btn_right_pressed = 0;
return;
}
mainModeIndex++;
btn_right_pressed = 0;
}
}

/**
Expand Down
42 changes: 29 additions & 13 deletions Core/Src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void drawSplashScreen(void) {
u8g2_SendBuffer(&u8g2);
}

void drawStartupScreen(uint8_t mode) {
void drawStartupScreen() {
u8g2_ClearBuffer(&u8g2);

u8g2_SetFont(&u8g2, DP_FONT_LARGE);
Expand All @@ -27,7 +27,7 @@ void drawStartupScreen(uint8_t mode) {
u8g2_SetFont(&u8g2, DP_FONT_SMALL);
u8g2_DrawStr(&u8g2, 60, 26, "davidramiro");

drawMainMenuInline(mode);
drawMainMenuInline();

u8g2_SendBuffer(&u8g2);
}
Expand Down Expand Up @@ -64,21 +64,37 @@ void drawSensorBarInline(void) {
u8g2_DrawRBox(&u8g2, 2, 119, (uint8_t)bar_w_f, 6, 2);
}

void drawMainMenuInline(uint8_t index) {
const uint8_t selectorY = (uint8_t)(31 + index * 24);
void drawMainMenuInline() {
if (mainMenuIndex == LATENCY) {
u8g2_DrawXBMP(&u8g2, 2, 31, 126, 42, menu_selection_2x_bitmap);
u8g2_DrawXBM(&u8g2, 29, 57, 3, 5, left_bitmap);
u8g2_DrawXBM(&u8g2, 115, 57, 3, 5, right_bitmap);
} else {
const uint8_t selectorY = (uint8_t)(55 + mainMenuIndex * 24);
u8g2_DrawXBMP(&u8g2, 2, selectorY, 126, 21, menu_selection_bitmap);
}

u8g2_SetFont(&u8g2, DP_FONT_MEDIUM);
u8g2_DrawStr(&u8g2, 31, 49, "LATENCY TEST");

u8g2_DrawXBMP(&u8g2, 2, selectorY, 128, 21, menu_selection_bitmap);
u8g2_SetFont(&u8g2, DP_FONT_SMALL);

if (mainModeIndex == CLICK) {
u8g2_DrawStr(&u8g2, 38, 63, " CLICK MODE ");
u8g2_DrawXBMP(&u8g2, 8, 44, 15, 16, click_bitmap);
} else if (mainModeIndex == MOVE) {
u8g2_DrawStr(&u8g2, 35, 63, " MOVE MODE ");
u8g2_DrawXBMP(&u8g2, 10, 44, 11, 16, move_bitmap);
} else if (mainModeIndex == EXTERNAL) {
u8g2_DrawStr(&u8g2, 35, 63, "EXTERNAL MODE");
u8g2_DrawXBMP(&u8g2, 8, 44, 16, 16, external_bitmap);
}

u8g2_DrawXBMP(&u8g2, 9, 33, 15, 16, click_bitmap);
u8g2_DrawXBMP(&u8g2, 12, 57, 11, 16, move_bitmap);
u8g2_DrawXBMP(&u8g2, 8, 81, 17, 16, jiggler_bitmap);
u8g2_DrawXBMP(&u8g2, 8, 105, 16, 16, cogwheel_bitmap);

u8g2_SetFont(&u8g2, DP_FONT_SMALL);
u8g2_DrawStr(&u8g2, 40, 45, "CLICK MODE");
u8g2_DrawStr(&u8g2, 40, 69, "MOVE MODE");
u8g2_DrawStr(&u8g2, 43, 93, "JIGGLER");
u8g2_DrawStr(&u8g2, 40, 117, "PARAMETERS");
u8g2_DrawStr(&u8g2, 53, 93, "JIGGLER");
u8g2_DrawStr(&u8g2, 46, 117, "PARAMETERS");
}

void drawParamsMenu(uint8_t index) {
Expand All @@ -91,7 +107,7 @@ void drawParamsMenu(uint8_t index) {
u8g2_SetFont(&u8g2, DP_FONT_SMALL);
snprintf(value_buf, sizeof(value_buf), "%d", (int)num_cycles);
int str_width = u8g2_GetStrWidth(&u8g2, value_buf);
u8g2_DrawStr(&u8g2, 110 - str_width / 2, 14, value_buf);
u8g2_DrawStr(&u8g2, 110 - str_width / 2, 14, value_buf);
snprintf(value_buf, sizeof(value_buf), "%d", (int)sensor_threshold);
str_width = u8g2_GetStrWidth(&u8g2, value_buf);
u8g2_DrawStr(&u8g2, 110 - str_width / 2, 14 + 24, value_buf);
Expand Down
61 changes: 61 additions & 0 deletions Core/Src/display_assets.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,71 @@ const unsigned char menu_selection_bitmap[] = {
0x00, 0x00, 0x00, 0x30, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f};
const unsigned char menu_selection_2x_bitmap[] = {
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xfc, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f};
const unsigned char cogwheel_bitmap[] = {
0xc0, 0x03, 0x48, 0x12, 0x34, 0x2c, 0x02, 0x40, 0xc4, 0x23, 0x24,
0x24, 0x13, 0xc8, 0x11, 0x88, 0x11, 0x88, 0x13, 0xc8, 0x24, 0x24,
0xc4, 0x23, 0x02, 0x40, 0x34, 0x2c, 0x48, 0x12, 0xc0, 0x03};
const unsigned char external_bitmap[] = {
0x04, 0x20, 0xc8, 0x13, 0x20, 0x04, 0x10, 0x08, 0x95, 0xa8, 0x90,
0x09, 0x90, 0x08, 0x24, 0x24, 0x42, 0x42, 0x80, 0x00, 0xc0, 0x03,
0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00};
const unsigned char left_bitmap[] = {0x04, 0x06, 0x07, 0x06, 0x04};
const unsigned char right_bitmap[] = {0x01, 0x03, 0x07, 0x03, 0x01};
const unsigned char sensor_bitmap[] = {
Expand Down
14 changes: 10 additions & 4 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ static volatile uint8_t debounce_running = 0;
static volatile uint8_t jiggle_interrupt_counter = 0;
static volatile uint16_t standby_interrupt_counter = 0;

enum MainMenuSelector mainMenuIndex = CLICK;
enum MainMenuSelector mainMenuIndex = LATENCY;
enum ParamMenuSelector paramMenuIndex = CYCLES;
enum MainModeSelector mainModeIndex = CLICK;
u8g2_t u8g2;

/* USER CODE END PV */
Expand Down Expand Up @@ -359,11 +360,16 @@ int main(void) {

// wait for button press
if (btn_center_pressed) {
if (mainMenuIndex == CLICK || mainMenuIndex == MOVE) {
if (mainMenuIndex == LATENCY) {
uint32_t latencies_us[num_cycles] = {};

while (cycle_index < num_cycles) {
measure(latencies_us);
int8_t error = measure(latencies_us);

if (error) {
cycle_index = 0;
break;
}
cycle_index++;
}

Expand Down Expand Up @@ -394,7 +400,7 @@ int main(void) {
}
}

drawStartupScreen(mainMenuIndex);
drawStartupScreen();

HAL_Delay(10);

Expand Down
28 changes: 25 additions & 3 deletions Core/Src/measure.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,27 @@ uint32_t readAveragedADC() {
* @brief Sends a HID event, measures latency in microseconds until ADC value
* changes beyond threshold.
* @param latencies_us Array to store the measured latencies in microseconds.
* @return int8_t Error code
*/
void measure(uint32_t latencies_us[]) {
int8_t measure(uint32_t latencies_us[]) {
const uint32_t baseline = readADC();

drawMeasurement(baseline, -1, -1);

const uint32_t start = startMouseAction();
HAL_TIM_Base_Stop_IT(&htim2);
__HAL_TIM_SET_COUNTER(&htim2, 0);
HAL_TIM_Base_Start_IT(&htim2);

if (mainModeIndex == EXTERNAL) {
HAL_GPIO_WritePin(EXT_TRIGGER_GPIO_Port, EXT_TRIGGER_Pin, GPIO_PIN_SET);
} else {
int8_t error = startMouseAction();
if (error) {
return;
}
}

const uint32_t start = TIM2->CNT;

while (1) {
tud_task();
Expand All @@ -49,7 +63,15 @@ void measure(uint32_t latencies_us[]) {

if (abs(delta) > sensor_threshold) {
uint32_t latency = (uint32_t)__HAL_TIM_GET_COUNTER(&htim2) - start;
stopMouseAction();
if (mainModeIndex == EXTERNAL) {
HAL_GPIO_WritePin(EXT_TRIGGER_GPIO_Port, EXT_TRIGGER_Pin,
GPIO_PIN_RESET);
} else {
int8_t error = stopMouseAction();
if (error) {
return;
}
}

if (cycle_index < num_cycles) {
latencies_us[cycle_index] = latency;
Expand Down
34 changes: 14 additions & 20 deletions Core/Src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@
#include "usbd.h"

/**
* @brief Triggers a mouse action based on the current menu index and measures the execution time.
*
* @details TIM2 is used for latency measurement with microsecond precision. Timer gets reset at first.
* HID mouse report depending on the `mainMenuIndex` (click or move) gets prepared.
* It waits for the USB HID interface to be ready. If a timeout occurs (6 seconds),
* it triggers an error state. Finally, it sends the report and returns the timer counter value.
*
* @return uint32_t The timer counter value at the moment of return.
* @brief Triggers a mouse action based on the current menu index.
* @details To be used as the first action within a latency measurement.
* Depending on `mainMenuIndex`, either clicks mouse1 or moves by 127px x, 127px y. It waits for the USB HID interface to be ready.
* If a timeout occurs (6 seconds), it triggers an error state by flashing the error LED and displaying a message.
* @return int8_t Error code
*/
uint32_t startMouseAction() {
HAL_TIM_Base_Stop_IT(&htim2);
__HAL_TIM_SET_COUNTER(&htim2, 0);
HAL_TIM_Base_Start_IT(&htim2);

int8_t startMouseAction() {
hid_mouse_report_t report = {.wheel = 0, .pan = 0};

if (mainMenuIndex == CLICK) {
if (mainModeIndex == CLICK) {
report.buttons = 1;
} else if (mainMenuIndex == MOVE) {
} else if (mainModeIndex == MOVE) {
report.x = 127, report.y = 127;
}

Expand All @@ -40,7 +33,7 @@ uint32_t startMouseAction() {
drawError("USB timeout. Connected?");
HAL_Delay(2000);
HAL_GPIO_WritePin(ERR_LED_GPIO_Port, ERR_LED_Pin, GPIO_PIN_RESET);
return 0;
return 1;
}
}

Expand Down Expand Up @@ -92,13 +85,14 @@ void randomMouseMove() {
* the original position. It waits for the USB HID interface to be ready.
* If a timeout occurs (6 seconds), it triggers an error state by flashing the error LED and displaying a message.
* Finally, it sends the report.
* @return int8_t Error code
*/
void stopMouseAction() {
int8_t stopMouseAction() {
hid_mouse_report_t report = {.wheel = 0, .pan = 0};

if (mainMenuIndex == CLICK) {
if (mainModeIndex == CLICK) {
report.buttons = 0;
} else if (mainMenuIndex == MOVE) {
} else if (mainModeIndex == MOVE) {
report.x = -127, report.y = -127;
}

Expand All @@ -109,7 +103,7 @@ void stopMouseAction() {
drawError("USB timeout. Connected?");
HAL_Delay(2000);
HAL_GPIO_WritePin(ERR_LED_GPIO_Port, ERR_LED_Pin, GPIO_PIN_RESET);
return;
return 1;
}
}

Expand Down
Loading