mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-05-04 15:20:16 +00:00
200 lines
5.6 KiB
C
200 lines
5.6 KiB
C
#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 IR_CommandNumber[3];
|
||
unsigned char IRStatus;
|
||
unsigned char NumberMacroProgramShot;
|
||
unsigned char NumberShot;
|
||
unsigned char NumberProgram;
|
||
unsigned char NumberMacro;
|
||
unsigned char CommandMacroProgramShot;
|
||
|
||
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)
|
||
{
|
||
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;
|
||
}
|
||
|
||
void ClearIRCommand()
|
||
{
|
||
IR_CommandNumber[0] = 0xFF;
|
||
IR_CommandNumber[1] = 0xFF;
|
||
IR_CommandNumber[2] = 0xFF;
|
||
}
|
||
|
||
void ClearIR()
|
||
{
|
||
IR_Command = 0x7F;
|
||
IR_Address = 0x1F;
|
||
IR_OptionsProgram = 0x00;
|
||
|
||
NumberMacroProgramShot = 0xFF;
|
||
NumberShot = 0xFF;
|
||
NumberProgram = 0xFF;
|
||
NumberMacro = 0xFF;
|
||
|
||
CommandMacroProgramShot = 0x00;
|
||
|
||
ClearIRStatus();
|
||
}
|
||
|
||
void SetShiftReg(unsigned char shiftreg[3])
|
||
{
|
||
for (unsigned char i = 0; i < 3; i++)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
for (unsigned char j = 0; j < 8; j++)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
|
||
if (((shiftreg[i] >> j) & 0x01) == 0x01)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
|
||
}
|
||
else
|
||
{
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
||
}
|
||
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
|
||
}
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
|
||
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
|
||
}
|
||
}
|