talon/User/memecoder.c
2024-01-13 12:57:51 -05:00

63 lines
1.5 KiB
C

#include "memecoder.h"
#include "ch32v20x.h"
int memecoder_prev_state = 0;
int memecoder_count = 0;
void memecoder_init() {
// Enable GPIOB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Set up GPIO on port B
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// PHASE_A - PB12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// PHASE_A - PB13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// PHASE_A - PB14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void memecoder_update() {
u8 cur_state =
GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) << 0 |
GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) << 1 |
GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) << 2;
if(cur_state != memecoder_prev_state) {
if( (memecoder_prev_state == 1 && cur_state == 3) ||
(memecoder_prev_state == 3 && cur_state == 2) ||
(memecoder_prev_state == 2 && cur_state == 6) ||
(memecoder_prev_state == 6 && cur_state == 4) ||
(memecoder_prev_state == 4 && cur_state == 5) ||
(memecoder_prev_state == 5 && cur_state == 1)) {
memecoder_count++;
} else {
memecoder_count--;
}
memecoder_prev_state = cur_state;
}
}
void memecoder_set(int value) {
memecoder_count = value;
}
int memecoder_get() {
return memecoder_count;
}
void memecoder_zero() {
memecoder_count = 0;
}