Skip to content

Commit

Permalink
Fixed button reading while values not ready yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Vostrenkov committed Jan 23, 2022
1 parent cabbabe commit 441e009
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 26 deletions.
30 changes: 30 additions & 0 deletions MDK-ARM/FreeJoy.uvoptx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@
<WinNumber>1</WinNumber>
<ItemText>sensors,0x10</ItemText>
</Ww>
<Ww>
<count>4</count>
<WinNumber>1</WinNumber>
<ItemText>raw_buttons_data</ItemText>
</Ww>
<Ww>
<count>5</count>
<WinNumber>1</WinNumber>
<ItemText>physical_buttons_state</ItemText>
</Ww>
<Ww>
<count>6</count>
<WinNumber>1</WinNumber>
<ItemText>logical_buttons_state,0x0A</ItemText>
</Ww>
<Ww>
<count>7</count>
<WinNumber>1</WinNumber>
<ItemText>out_buttons_data</ItemText>
</Ww>
<Ww>
<count>8</count>
<WinNumber>1</WinNumber>
<ItemText>log_buttons_data</ItemText>
</Ww>
<Ww>
<count>9</count>
<WinNumber>1</WinNumber>
<ItemText>shifts_state</ItemText>
</Ww>
</WatchWindow1>
<MemoryWindow1>
<Mm>
Expand Down
2 changes: 1 addition & 1 deletion application/Inc/common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

//#define DEBUG

#define FIRMWARE_VERSION 0x1712 // v1.7.1b2
#define FIRMWARE_VERSION 0x1713 // v1.7.1b2
#define USED_PINS_NUM 30 // constant for BluePill and BlackPill boards
#define MAX_AXIS_NUM 8 // max 8
#define MAX_BUTTONS_NUM 128 // power of 2, max 128
Expand Down
2 changes: 1 addition & 1 deletion application/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

static const dev_config_t init_config =
{
.firmware_version = 0x1712, // do not change
.firmware_version = 0x1713, // do not change
/*
Name of device in devices dispatcher
*/
Expand Down
2 changes: 1 addition & 1 deletion application/Src/analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ void AxesProcess (dev_config_t * p_dev_config)

// Buttons section
{
int64_t millis = GetMillis();
int32_t millis = GetMillis();

uint8_t inc_button_num = 0;
uint8_t rst_button_num = 0;
Expand Down
22 changes: 15 additions & 7 deletions application/Src/buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uint8_t pov_pos[MAX_POVS_NUM];
uint8_t shifts_state = 0;
uint8_t a2b_first = 0;
uint8_t a2b_last = 0;
uint8_t button_mutex = 0;

/**
* @brief Processing debounce for raw buttons input
Expand All @@ -44,7 +45,7 @@ uint8_t a2b_last = 0;
*/
void ButtonsDebounceProcess (dev_config_t * p_dev_config)
{
int64_t millis;
int32_t millis;
uint16_t debounce;

millis = GetMillis();
Expand Down Expand Up @@ -88,7 +89,7 @@ void ButtonsDebounceProcess (dev_config_t * p_dev_config)
}
}

static void LogicalButtonProcessTimer (logical_buttons_state_t * p_button_state, int64_t millis, dev_config_t * p_dev_config, uint8_t num)
static void LogicalButtonProcessTimer (logical_buttons_state_t * p_button_state, int32_t millis, dev_config_t * p_dev_config, uint8_t num)
{
uint16_t tmp_press_time;
uint16_t tmp_delay_time;
Expand Down Expand Up @@ -180,7 +181,7 @@ static void LogicalButtonProcessTimer (logical_buttons_state_t * p_button_state,
void LogicalButtonProcessState (logical_buttons_state_t * p_button_state, uint8_t * pov_buf, dev_config_t * p_dev_config, uint8_t num)
{

int64_t millis;
int32_t millis;
uint8_t pov_group = 0;

millis = GetMillis();
Expand Down Expand Up @@ -936,9 +937,13 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
// convert data to report format
uint8_t k = 0;

// buttons read is permitted
button_mutex = 1;

memset(out_buttons_data, 0, sizeof(out_buttons_data));
memset(log_buttons_data, 0, sizeof(log_buttons_data));
memset(phy_buttons_data, 0, sizeof(phy_buttons_data));

for (int i=0;i<MAX_BUTTONS_NUM;i++)
{
uint8_t is_enabled = !p_dev_config->buttons[i].is_disabled && (p_dev_config->buttons[i].physical_num >= 0);
Expand Down Expand Up @@ -969,6 +974,9 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
phy_buttons_data[(i & 0xF8)>>3] |= (physical_buttons_state[i].current_state << (i & 0x07));
}

// buttons read is allowed
button_mutex = 0;

// convert POV data to report format
for (int i=0; i<MAX_POVS_NUM; i++)
{
Expand Down Expand Up @@ -1013,19 +1021,19 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
*/
void ButtonsGet (uint8_t * out_data, uint8_t * log_data, uint8_t * phy_data, uint8_t * shift_data)
{
if (out_data != NULL)
if (out_data != NULL && !button_mutex)
{
memcpy(out_data, out_buttons_data, sizeof(out_buttons_data));
}
if (log_data != NULL)
if (log_data != NULL && !button_mutex)
{
memcpy(log_data, log_buttons_data, sizeof(log_buttons_data));
}
if (phy_data != NULL)
if (phy_data != NULL && !button_mutex)
{
memcpy(phy_data, &phy_buttons_data, sizeof(phy_buttons_data));
}
if (shift_data != NULL)
if (shift_data != NULL && !button_mutex)
{
memcpy(shift_data, &shifts_state, sizeof(shifts_state));
}
Expand Down
2 changes: 1 addition & 1 deletion application/Src/encoders.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void EncoderProcess (logical_buttons_state_t * button_state_buf, dev_config_t *
uint8_t tmp_a = 0;
uint8_t tmp_b = 0;

int64_t millis = GetMillis();
int32_t millis = GetMillis();

for (int k = 0; k < MAX_ENCODERS_NUM; k++)
{
Expand Down
19 changes: 10 additions & 9 deletions application/Src/stm32f10x_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@

/* Private variables ---------------------------------------------------------*/

volatile int64_t millis = 0;
volatile int64_t joy_millis = 0;
volatile int64_t encoder_ticks = 0;
volatile int64_t adc_ticks = 0;
volatile int64_t sensors_ticks = 1;
volatile int64_t buttons_ticks = 0;
volatile int64_t configurator_millis = 0;
static joy_report_t joy_report;
static params_report_t params_report;

volatile int32_t millis = 0;
volatile int32_t joy_millis = 0;
volatile int32_t encoder_ticks = 0;
volatile int32_t adc_ticks = 0;
volatile int32_t sensors_ticks = 1;
volatile int32_t buttons_ticks = 0;
volatile int32_t configurator_millis = 0;
volatile int status = 0;
extern dev_config_t dev_config;

Expand Down Expand Up @@ -182,8 +185,6 @@ void SysTick_Handler(void)

void TIM2_IRQHandler(void)
{
joy_report_t joy_report;
params_report_t params_report;
uint8_t report_buf[64];
uint8_t pos = 0;
app_config_t tmp_app_config;
Expand Down
12 changes: 6 additions & 6 deletions application/Src/usb_endp.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile extern uint8_t bootloader;
volatile extern int64_t joy_millis;
volatile extern int64_t encoder_ticks;
volatile extern int64_t adc_ticks;
volatile extern int64_t sensors_ticks;
volatile extern int64_t buttons_ticks;
volatile extern int64_t configurator_millis;
volatile extern int32_t joy_millis;
volatile extern int32_t encoder_ticks;
volatile extern int32_t adc_ticks;
volatile extern int32_t sensors_ticks;
volatile extern int32_t buttons_ticks;
volatile extern int32_t configurator_millis;

__IO uint8_t EP1_PrevXferComplete = 1;
__IO uint8_t EP2_PrevXferComplete = 1;
Expand Down

0 comments on commit 441e009

Please sign in to comment.