Files
examples/win32-example/ir_decoder/src/ir_decode_test.c
2026-01-10 19:47:47 +08:00

347 lines
9.4 KiB
C

/**************************************************************************************
Filename: ir_decode_test.c
Revised: Date: 2016-11-05
Revision: Revision: 1.0
Description: This file provides main entry for irda decoder test
Revision log:
* 2016-11-05: created by strawmanbobi
**************************************************************************************/
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ir_defs.h"
#include "ir_decode.h"
#define INPUT_MAX 3
// global variable definition
t_remote_ac_status *ac_status = NULL;
UINT16 user_data[USER_DATA_SIZE];
void input_number(int *val)
{
char n[50]={0};
int i = 0;
*val = 0;
if (0 != scanf("%s", n))
{
return;
}
getchar();
while(1)
{
if(n[i] < '0'||n[i] > '9')
{
printf("invalid number format, please re-input : ");
if (0 != scanf("%s", n))
{
break;
}
i = 0;
}
else
{
i++;
}
if(n[i] == '\0')
break;
}
i = 0;
while (n[i] != '\0')
{
*val = (*val * 10 + (int)n[i] - 48);
i++;
}
}
static INT8 decode_as_ac(char *file_name)
{
BOOL op_match = TRUE;
BOOL change_wind_dir = FALSE;
int key_code = 0;
int first_time = 1;
int length = 0;
int index = 0;
// get status
UINT8 supported_mode = 0x00;
INT8 min_temperature = 0;
INT8 max_temperature = 0;
UINT8 supported_speed = 0x00;
UINT8 supported_swing = 0x00;
UINT8 supported_wind_direction = 0x00;
BOOL need_control = TRUE;
if (ac_status == NULL) {
ac_status = (t_remote_ac_status *)malloc(sizeof(t_remote_ac_status));
if (ac_status == NULL) {
printf("failed to allocate memory for ac_status\n");
return IR_DECODE_FAILED;
}
}
// init air conditioner status
ac_status->ac_display = 0;
ac_status->ac_sleep = 0;
ac_status->ac_timer = 0;
ac_status->ac_power = AC_POWER_ON;
ac_status->ac_mode = AC_MODE_COOL;
ac_status->ac_temp = AC_TEMP_20;
ac_status->ac_wind_dir = AC_SWING_ON;
ac_status->ac_wind_speed = AC_WS_AUTO;
ac_status->change_wind_direction = 0;
if (IR_DECODE_FAILED == ir_file_open(REMOTE_CATEGORY_AC, 0, file_name))
{
ir_close();
return IR_DECODE_FAILED;
}
do
{
if (1 == first_time)
{
printf("please input valid key code "
"(key code could be referenced from https://irext.net/doc#keymap) : \n");
first_time = 0;
}
else
{
printf("please input valid key code : \n");
}
input_number(&key_code);
op_match = TRUE;
need_control = FALSE;
ac_status->change_wind_direction = 0;
if (99 == key_code)
{
break;
}
if (14 == key_code)
{
if (IR_DECODE_SUCCEEDED == get_supported_mode(&supported_mode))
{
printf("supported mode = %02X\n", supported_mode);
}
else
{
printf("get supported mode failed\n");
}
}
else if (15 == key_code)
{
if (IR_DECODE_SUCCEEDED == get_supported_wind_speed(ac_status->ac_mode, &supported_speed))
{
printf("supported wind speed in %d = %02X\n", ac_status->ac_mode, supported_speed);
}
else
{
printf("get supported wind speed failed\n");
}
}
else if (16 == key_code)
{
if (IR_DECODE_SUCCEEDED == get_temperature_range(ac_status->ac_mode, &min_temperature, &max_temperature))
{
printf("supported temperature range in mode %d = %d, %d\n",
ac_status->ac_mode, min_temperature, max_temperature);
}
else
{
printf("get supported temperature range failed\n");
}
}
else if (17 == key_code)
{
if (IR_DECODE_SUCCEEDED == get_supported_wind_direction(&supported_wind_direction))
{
printf("supported swing type = %02X\n", supported_wind_direction);
}
else
{
printf("get swing type failed\n");
}
}
else
{
switch (key_code)
{
// notice: only if ac_power is turned on will user_data change when input a different key_code
case 0:
ac_status->ac_power = ((ac_status->ac_power == AC_POWER_ON) ? AC_POWER_OFF : AC_POWER_ON);
need_control = TRUE;
break;
case 1:
++ac_status->ac_mode;
ac_status->ac_mode = ac_status->ac_mode % AC_MODE_MAX;
need_control = TRUE;
break;
case 2:
case 7:
ac_status->ac_temp = ((ac_status->ac_temp == AC_TEMP_30) ? AC_TEMP_30 : (ac_status->ac_temp + 1));
need_control = TRUE;
break;
case 3:
case 8:
ac_status->ac_temp = ((ac_status->ac_temp == AC_TEMP_16) ? AC_TEMP_16 : (ac_status->ac_temp - 1));
need_control = TRUE;
break;
case 9:
++ac_status->ac_wind_speed;
ac_status->ac_wind_speed = ac_status->ac_wind_speed % AC_WS_MAX;
need_control = TRUE;
break;
case 10:
ac_status->ac_wind_dir = ((ac_status->ac_wind_dir == AC_SWING_ON) ? AC_SWING_OFF : AC_SWING_ON);
need_control = TRUE;
break;
case 11:
if (ac_status->ac_wind_dir == AC_SWING_OFF) {
ac_status->change_wind_direction = 1;
}
need_control = TRUE;
break;
default:
op_match = FALSE;
break;
}
if (TRUE == op_match && TRUE == need_control)
{
printf("switch AC to power = %d, mode = %d, temp = %d, speed = %d, swing = %d with key_code = %d\n",
ac_status->ac_power,
ac_status->ac_mode,
ac_status->ac_temp,
ac_status->ac_wind_speed,
ac_status->ac_wind_dir,
key_code);
length = ir_decode(key_code, user_data, ac_status);
printf("\n === binary decoded : %d\n", length);
for (index = 0; index < length; index++)
{
printf("%d, ", user_data[index]);
}
printf("\n");
}
}
} while (TRUE);
ir_close();
return IR_DECODE_SUCCEEDED;
}
static INT8 decode_as_tv(char *file_name, UINT8 ir_hex_encode)
{
// keyboard input
int key_code = 0;
int first_time = 1;
int length = 0;
int index = 0;
// here remote category TV represents for command typed IR code
if (IR_DECODE_FAILED == ir_file_open(REMOTE_CATEGORY_TV, ir_hex_encode, file_name))
{
printf("failed to open file : %s\n", file_name);
ir_close();
return IR_DECODE_FAILED;
}
do
{
if (1 == first_time)
{
printf("please input valid key code "
"(Key code could be referenced from https://irext.net/doc#keymap) : \n");
first_time = 0;
}
else
{
printf("please input valid key code : \n");
}
input_number(&key_code);
if (99 == key_code)
{
break;
}
length = ir_decode(key_code, user_data, NULL);
printf("\n === binary decoded : %d\n", length);
for (index = 0; index < length; index++)
{
printf("%d, ", user_data[index]);
}
printf("===\n");
} while (TRUE);
ir_close();
return IR_DECODE_SUCCEEDED;
}
void cleanup() {
if (ac_status != NULL) {
free(ac_status);
ac_status = NULL;
}
}
static void print_usage(const char *progn) {
printf("usage: %s [function] [file] [subcate]\n"
"[function] : 0 - decode for AC; 1 - decode for others\n"
"[file] : the remote control binary file\n"
"[sub_cate] : the sub_cate value from remote_index", progn);
}
int main(int argc, char *argv[])
{
char function = '0';
UINT8 ir_hex_encode = 0;
if (4 != argc)
{
print_usage((argv[0]));
return -1;
}
function = argv[1][0];
ir_hex_encode = (UINT8) (argv[3][0] - '0');
switch (function)
{
case '0':
printf("decode %s as status-typed binary\n", argv[2]);
decode_as_ac(argv[2]);
break;
case '1':
printf("decode %s as command-typed binary in sub_cate %d\n", argv[2], ir_hex_encode);
decode_as_tv(argv[2], ir_hex_encode);
break;
default:
printf("decode functionality not supported : %c\n", function);
break;
}
cleanup();
return 0;
}