rotary encoder part void Rotary_Enc_Chk(void) { static uint32_t lastTick = 0; uint32_t currentTick = HAL_GetTick();
switch (encoderState) {
case WAIT_FOR_A_LOW:
if (HAL_GPIO_ReadPin(GPIOB, ROT_A_Pin) == GPIO_PIN_RESET) {
if (HAL_GPIO_ReadPin(GPIOB, ROT_B_Pin) == GPIO_PIN_RESET) {
encoderState = WAIT_FOR_B_LOW;
lastTick = currentTick;
} else {
encoderState = WAIT_FOR_B_HIGH;
lastTick = currentTick;
}
}
break;
case WAIT_FOR_B_LOW:
if (HAL_GPIO_ReadPin(GPIOB, ROT_B_Pin) == GPIO_PIN_SET) {
speed--;
encoderState = WAIT_FOR_A_HIGH;
rotChk = TRUE;
lastTick = currentTick;
} else if (currentTick - lastTick > 10) {
encoderState = WAIT_FOR_A_LOW;
}
break;
case WAIT_FOR_A_HIGH:
if (HAL_GPIO_ReadPin(GPIOB, ROT_A_Pin) == GPIO_PIN_SET) {
encoderState = WAIT_FOR_B_HIGH;
lastTick = currentTick;
} else if (currentTick - lastTick > 10) {
encoderState = WAIT_FOR_A_LOW;
}
break;
case WAIT_FOR_B_HIGH:
if (HAL_GPIO_ReadPin(GPIOB, ROT_B_Pin) == GPIO_PIN_RESET) {
speed++;
encoderState = WAIT_FOR_A_LOW;
lastTick = currentTick;
// Set rotChk flag
rotChk = TRUE;
} else if (currentTick - lastTick > 10) {
encoderState = WAIT_FOR_A_LOW;
}
break;
}
// Bound speed
if (speed < 0) speed = 0;
if (speed > 225) speed = 225;
}
void TM1637_DisplayDecimal(int v, int displaySeparator) { unsigned char digitArr[N_DISPLAYS]; for (int i = 0; i < N_DISPLAYS; ++i) { digitArr[i] = segmentMap[v % 10];
if (i == 2 && displaySeparator) //Code snippet for dot or display separator
{
digitArr[i] |= 1 << 7;
}
v /= 10;
}
if(digitArr[N_DISPLAYS - 1] == 0x3F) //Code Snippet for MSB zeros to be off
{
digitArr[N_DISPLAYS - 1] = 0x00;
if(digitArr[N_DISPLAYS - 2] == 0x3F)
{
digitArr[N_DISPLAYS - 2] = 0x00;
}
}