2024-08-25 20:27:17 +03:00

143 lines
3.5 KiB
C
Raw Blame History

#include "IR.h"
#include "usbd_cdc_if.h"
#include "Print.h"
unsigned char IR_Command;
unsigned char IR_Address;
unsigned char IR_OptionsProgram;
unsigned char IR_IndexBitCommand;
unsigned char IR_IndexBitAddress;
unsigned int IR_Pulse;
unsigned int currentIR_Pulse;
unsigned char IRStatus;
void IR_handler() // SONY (SIRC) protocol 12 bit (7+5)
{
if ((GPIOB->IDR & 0x0002) == 0)
{
IR_Pulse = SysTick->VAL;
// GPIOC->ODR ^= GPIO_PIN_13;
}
else
{
{
currentIR_Pulse = SysTick->VAL;
if (currentIR_Pulse > IR_Pulse)
{
currentIR_Pulse = IR_Pulse + 479999 - currentIR_Pulse;
}
else
{
currentIR_Pulse = IR_Pulse - currentIR_Pulse;
}
// if (currentIR_Pulse > 150000 ) currentIR_Pulse = 0xFFFFFFFF - currentIR_Pulse;
switch (IRStatus)
{
case STATUS_START:
{
if ((currentIR_Pulse < 130000) && (currentIR_Pulse > 110000)) // 2,7 ms - 2,3 ms
{
IRStatus = STATUS_COMMAND; // 129600 - 110400
// print("STATUS_COMMAND\n");
}
else
{
IRStatus = STATUS_ERROR;
}
}
break;
case STATUS_COMMAND:
{
if (currentIR_Pulse < 37440) // 0.78 ms 37440
{
IR_Command &= ~((1 << IR_IndexBitCommand) & 0xFF);
// print("0");
}
else
{
IR_Command |= ((1 << IR_IndexBitCommand) & 0xFF);
// print("1");
}
IR_IndexBitCommand++;
if (IR_IndexBitCommand == 7)
{
IRStatus = STATUS_ADDRESS;
// print(" STATUS_ADDRESS ");
}
else
{
IRStatus = STATUS_COMMAND;
}
}
break;
case STATUS_ADDRESS:
{
{
if (currentIR_Pulse < 37440) // 0.78 <20><>
{
IR_Address &= ~((1 << IR_IndexBitAddress) & 0xFF);
// print("0");
}
else
{
IR_Address |= ((1 << IR_IndexBitAddress) & 0xFF);
// print("1");
}
IR_IndexBitAddress++;
if (IR_IndexBitAddress == 5)
{
// printNumber(IR_Command);
IRStatus = STATUS_REPEAT;
// print("\n\n\n");
}
else
{
IRStatus = STATUS_ADDRESS;
}
}
}
break;
case STATUS_REPEAT:
break;
case STATUS_ERROR:
// print("STATUS_ERROR\n\n");
default:
ClearIRStatus();
break;
}
}
}
}
void ClearIRStatus()
{
IR_IndexBitCommand = 0x00;
IR_IndexBitAddress = 0x00;
IRStatus = STATUS_START;
}
IRData getIRData(){
NVIC_DisableIRQ(EXTI1_IRQn);
IRData ret = {IR_Address, IR_Command};
NVIC_EnableIRQ(EXTI1_IRQn);
return ret;
}
unsigned char IR_Available(){
return IRStatus == STATUS_REPEAT;
}