Prusa MINI Firmware overview
jogwheel.c File Reference
#include "jogwheel.h"
#include <limits.h>
#include "gpio.h"

Functions

void jogwheel_init (void)
 
void jogwheel_update_1ms (void)
 
void jogwheel_encoder_set (int32_t val, int32_t min, int32_t max)
 

Variables

uint8_t jogwheel_signals = 0
 
uint8_t jogwheel_signals_old = 0
 
uint8_t jogwheel_signals_new = 0
 
int32_t jogwheel_encoder = 0
 
int32_t jogwheel_encoder_min = INT_MIN
 
int32_t jogwheel_encoder_max = INT_MAX
 
uint16_t jogwheel_button_down = 0
 
uint8_t jogwheel_changed = 0
 
jogwheel_config_t jogwheel_config
 

Function Documentation

◆ jogwheel_init()

void jogwheel_init ( void  )

◆ jogwheel_update_1ms()

void jogwheel_update_1ms ( void  )
22  {
23  uint8_t signals = 0;
25  signals |= 4; //bit 2 - button press
27  signals ^= 4;
29  signals |= 1; //bit 0 - phase0
31  signals |= 2; //bit 1 - phase1
33  signals ^= 3;
34  if (jogwheel_signals_new != signals) {
35  jogwheel_signals_new = signals;
36  return;
37  }
38  uint8_t change = signals ^ jogwheel_signals;
39  if (change & 3) //encoder phase signals changed
40  {
42  int32_t encoder = jogwheel_encoder;
43  int32_t dir = (jogwheel_config.flg & JOGWHEEL_FLG_INV_DIR) ? -1 : 1;
45  if (((signals & 3) == 0) || ((signals & 3) == 3)) {
46  if (((change & 3) == 1) && ((change2 & 3) == 2))
47  encoder += dir;
48  if (((change & 3) == 2) && ((change2 & 3) == 1))
49  encoder -= dir;
50  }
51  } else {
52  if ((change & 1) && (signals & 1) && !(signals & 2))
53  encoder += dir;
54  if ((change & 2) && (signals & 2) && !(signals & 1))
55  encoder -= dir;
56  }
57  if (encoder < jogwheel_encoder_min)
58  encoder = jogwheel_encoder_min;
59  if (encoder > jogwheel_encoder_max)
60  encoder = jogwheel_encoder_max;
61  if (jogwheel_encoder != encoder) {
62  jogwheel_encoder = encoder;
63  change |= 8; //bit3 means encoder changed
64  }
65  }
66  if (signals & 4)
68  else
70  if (change & 7) //encoder phase signals or button changed
71  {
72  jogwheel_signals_old = jogwheel_signals; //save old signal state
73  jogwheel_signals = signals; //update signal state
74  }
75  if (change & 12) //encoder changed or button changed
76  {
77  jogwheel_signals_old = jogwheel_signals; //save old signal state
78  jogwheel_signals = signals; //update signal state
79  jogwheel_changed |= (change >> 2); //synchronization is not necessary because we are inside interrupt
80  }
81 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ jogwheel_encoder_set()

void jogwheel_encoder_set ( int32_t  val,
int32_t  min,
int32_t  max 
)
83  {
84  if (min > max)
85  return;
86  if (val < min)
87  val = min;
88  if (val > max)
89  val = max;
90  jogwheel_encoder = val;
93 }
Here is the caller graph for this function:

Variable Documentation

◆ jogwheel_signals

uint8_t jogwheel_signals = 0

◆ jogwheel_signals_old

uint8_t jogwheel_signals_old = 0

◆ jogwheel_signals_new

uint8_t jogwheel_signals_new = 0

◆ jogwheel_encoder

int32_t jogwheel_encoder = 0

◆ jogwheel_encoder_min

int32_t jogwheel_encoder_min = INT_MIN

◆ jogwheel_encoder_max

int32_t jogwheel_encoder_max = INT_MAX

◆ jogwheel_button_down

uint16_t jogwheel_button_down = 0

◆ jogwheel_changed

uint8_t jogwheel_changed = 0

◆ jogwheel_config

jogwheel_config_t jogwheel_config
Initial value:
= {
0,
0,
0,
0,
}
_jogwheel_config_t::pinEN2
uint8_t pinEN2
Definition: jogwheel.h:22
JOGWHEEL_FLG_2PULSES
#define JOGWHEEL_FLG_2PULSES
Definition: jogwheel.h:11
jogwheel_button_down
uint16_t jogwheel_button_down
Definition: jogwheel.c:13
jogwheel_encoder
int32_t jogwheel_encoder
Definition: jogwheel.c:10
max
#define max(a, b)
Definition: wiring_constants.h:40
jogwheel_signals_old
uint8_t jogwheel_signals_old
Definition: jogwheel.c:8
jogwheel_signals
uint8_t jogwheel_signals
Definition: jogwheel.c:7
GPIO_PULLUP
#define GPIO_PULLUP
Definition: stm32f4xx_hal_gpio.h:171
gpio_get
static int gpio_get(uint8_t pin8)
Definition: gpio.h:94
JOGWHEEL_FLG_INV_ENC
#define JOGWHEEL_FLG_INV_ENC
Definition: jogwheel.h:9
min
#define min(a, b)
Definition: wiring_constants.h:36
gpio_init
static void gpio_init(uint8_t pin8, uint32_t mode, uint32_t pull, uint32_t speed)
Definition: gpio.h:102
jogwheel_changed
uint8_t jogwheel_changed
Definition: jogwheel.c:14
_jogwheel_config_t::flg
uint8_t flg
Definition: jogwheel.h:24
JOGWHEEL_FLG_INV_DIR
#define JOGWHEEL_FLG_INV_DIR
Definition: jogwheel.h:8
JOGWHEEL_FLG_INV_E12
#define JOGWHEEL_FLG_INV_E12
Definition: jogwheel.h:10
uint8_t
const uint8_t[]
Definition: 404_html.c:3
jogwheel_encoder_min
int32_t jogwheel_encoder_min
Definition: jogwheel.c:11
jogwheel_encoder_max
int32_t jogwheel_encoder_max
Definition: jogwheel.c:12
GPIO_MODE_INPUT
#define GPIO_MODE_INPUT
Definition: stm32f4xx_hal_gpio.h:135
_jogwheel_config_t::pinEN1
uint8_t pinEN1
Definition: jogwheel.h:21
jogwheel_signals_new
uint8_t jogwheel_signals_new
Definition: jogwheel.c:9
_jogwheel_config_t::pinENC
uint8_t pinENC
Definition: jogwheel.h:23
GPIO_SPEED_FREQ_LOW
#define GPIO_SPEED_FREQ_LOW
Definition: stm32f4xx_hal_gpio.h:158
jogwheel_config
jogwheel_config_t jogwheel_config
Definition: jogwheel.c:95