Encoder/src/Encoder.c

93 lines
2.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by villuton on 06.02.25.
//
#include "Encoder.h"
#define EncoderPinGet(PIN) env->pinGet(PIN)
void Encoder_Init(tEncoder *env,
void *pinA,
void *pinB,
void *pinKey,
pinGetProvider pinGet,
uint16_t flags){
env->pinA = pinA;
env->pinB = pinB;
env->pinKey = pinKey;
env->pinGet = pinGet;
env->flags = flags;
}
/*
| y2 y1 x2 x1 Вперёд Назад Состояние
------------------------------------------------------------------
0 |0 0 0 0 0 0 Стоп
1 |0 0 0 1 0 1 Назад
2 |0 0 1 0 1 0 Вперёд
3 |0 0 1 1 0 0 Не определено
4 |0 1 0 0 1 0 Вперед
5 |0 1 0 1 0 0 Стоп
6 |0 1 1 0 0 1/0 Назад*
7 |0 1 1 1 0 1 Назад
8 |1 0 0 0 0 1 Назад
9 |1 0 0 1 1/0 0 Вперёд*
A |1 0 1 0 0 0 Стоп
B |1 0 1 1 1 0 Вперёд
C |1 1 0 0 0 0 Не определено
D |1 1 0 1 1 0 Вперёд
E |1 1 1 0 0 1 Назад
F |1 1 1 1 0 0 Стоп
------------------------------------------------------------------
*/
eEncoderEvent Encoder_Check(tEncoder *env){
uint8_t result = ENC_NON;
static uint8_t oldResult = ENC_NON;
env->newValue = ((EncoderPinGet(env->pinA)&0b1)<<0b1)|(EncoderPinGet(env->pinB)&0b1);
env->fullState = env->newValue | env->encoderState << 2;
switch(env->fullState)
{
case 0x2: case 0x4: case 0xB: case 0xD: //case 0x9:
if(env->undefState){
env->undefState = false;
} else{
env->countValue ++;
result = ENC_FWD;
}
break;
case 0x1: case 0x7: case 0x8: case 0xE://case 0x6:
if(env->undefState){
env->undefState = false;
} else{
env->countValue--;
result = ENC_BACK;
}
break;
case 0x3:case 0xC:case 0xF:case 0x5:case 0xA:
env->undefState = true;
default:
break;
}
env->encoderState = env->newValue;
if((oldResult|result)==(ENC_BACK|ENC_FWD)){
oldResult = result;
result = ENC_NON;
}
if(result != ENC_NON) {
if (env->flags & ENCODER_WITHOUT_KEY) {
return result;
} else {
return EncoderPinGet(env->pinKey) ? (result | ENC_KEY) : result;
}
}
return result;
}
int Encoder_GetCounter(tEncoder *env){
return env->countValue;
}