Files
examples/win32-example/ir_decoder/src/ir_decode_test.c

327 lines
9.0 KiB
C
Raw Normal View History

2020-07-26 20:52:04 +08:00
/**************************************************************************************
2022-03-03 19:57:21 +08:00
Filename: ir_decode_test.c
2020-07-26 20:52:04 +08:00
Revised: Date: 2016-11-05
Revision: Revision: 1.0
2022-03-03 19:57:21 +08:00
Description: This file provides main entry for irda decoder test
2020-07-26 20:52:04 +08:00
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 "include/ir_defs.h"
#include "include/ir_decode.h"
#define INPUT_MAX 3
// global variable definition
t_remote_ac_status ac_status;
UINT16 user_data[USER_DATA_SIZE];
void input_number(int *val)
{
char n[50]={0};
int i = 0;
*val = 0;
2025-10-20 16:23:33 +08:00
if (0 != scanf("%s", n))
{
return;
}
2020-07-26 20:52:04 +08:00
getchar();
while(1)
{
if(n[i] < '0'||n[i] > '9')
{
2025-10-20 16:23:33 +08:00
printf("invalid number format, please re-input : ");
if (0 != scanf("%s", n))
{
break;
}
i = 0;
2020-07-26 20:52:04 +08:00
}
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;
2022-03-03 19:57:21 +08:00
BOOL need_control = TRUE;
2020-07-26 20:52:04 +08:00
// init air conditioner status
ac_status.ac_display = 0;
ac_status.ac_sleep = 0;
ac_status.ac_timer = 0;
2022-03-03 19:57:21 +08:00
ac_status.ac_power = AC_POWER_ON;
2020-07-26 20:52:04 +08:00
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;
if (IR_DECODE_FAILED == ir_file_open(REMOTE_CATEGORY_AC, 0, file_name))
{
ir_close();
return IR_DECODE_FAILED;
}
do
{
if (1 == first_time)
{
2025-10-20 16:23:33 +08:00
printf("please input valid key code "
"(key code could be referenced from https://irext.net/doc#keymap) : \n");
2020-07-26 20:52:04 +08:00
first_time = 0;
}
else
{
2025-10-20 16:23:33 +08:00
printf("please input valid key code : \n");
2020-07-26 20:52:04 +08:00
}
input_number(&key_code);
op_match = TRUE;
need_control = FALSE;
change_wind_dir = FALSE;
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)
{
2022-03-03 19:57:21 +08:00
// notice: only if ac_power is turned on will user_data change when input a different key_code
2020-07-26 20:52:04 +08:00
case 0:
2022-03-03 19:57:21 +08:00
ac_status.ac_power = ((ac_status.ac_power == AC_POWER_ON) ? AC_POWER_OFF : AC_POWER_ON);
2020-07-26 20:52:04 +08:00
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) {
change_wind_dir = TRUE;
}
need_control = TRUE;
break;
default:
op_match = FALSE;
break;
}
if (TRUE == op_match && TRUE == need_control)
{
2022-03-03 19:57:21 +08:00
printf("switch AC to power = %d, mode = %d, temp = %d, speed = %d, swing = %d with key_code = %d\n",
2020-07-26 20:52:04 +08:00
ac_status.ac_power,
ac_status.ac_mode,
ac_status.ac_temp,
ac_status.ac_wind_speed,
ac_status.ac_wind_dir,
2022-03-03 19:57:21 +08:00
key_code);
length = ir_decode(key_code, user_data, &ac_status, change_wind_dir);
2025-10-20 16:23:33 +08:00
printf("\n === binary decoded : %d\n", length);
2020-07-26 20:52:04 +08:00
for (index = 0; index < length; index++)
{
printf("%d, ", user_data[index]);
}
2022-03-03 19:57:21 +08:00
printf("\n");
2020-07-26 20:52:04 +08:00
}
}
} 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))
{
2025-10-20 16:23:33 +08:00
printf("failed to open file : %s\n", file_name);
2020-07-26 20:52:04 +08:00
ir_close();
return IR_DECODE_FAILED;
}
do
{
if (1 == first_time)
{
2025-10-20 16:23:33 +08:00
printf("please input valid key code "
2020-07-26 20:52:04 +08:00
"(Key code could be referenced from https://irext.net/doc#keymap) : \n");
first_time = 0;
}
else
{
2025-10-20 16:23:33 +08:00
printf("please input valid key code : \n");
2020-07-26 20:52:04 +08:00
}
input_number(&key_code);
if (99 == key_code)
{
break;
}
length = ir_decode(key_code, user_data, NULL, 0);
2025-10-20 16:23:33 +08:00
printf("\n === binary decoded : %d\n", length);
2020-07-26 20:52:04 +08:00
for (index = 0; index < length; index++)
{
printf("%d, ", user_data[index]);
}
printf("===\n");
} while (TRUE);
ir_close();
return IR_DECODE_SUCCEEDED;
}
static void print_usage(const char *progn) {
2025-10-20 16:23:33 +08:00
printf("usage: %s [function] [file] [subcate]\n"
"[function] : 0 - decode for AC; 1 - decode for others\n"
2020-07-26 20:52:04 +08:00
"[file] : the remote control binary file\n"
2025-10-20 16:23:33 +08:00
"[sub_cate] : the sub_cate value from remote_index", progn);
2020-07-26 20:52:04 +08:00
}
int main(int argc, char *argv[])
{
char function = '0';
UINT8 ir_hex_encode = 0;
if (4 != argc)
{
2022-03-03 19:57:21 +08:00
print_usage((argv[0]));
2020-07-26 20:52:04 +08:00
return -1;
}
function = argv[1][0];
ir_hex_encode = (UINT8) (argv[3][0] - '0');
switch (function)
{
case '0':
2025-10-20 16:23:33 +08:00
printf("decode %s as status-typed binary\n", argv[2]);
2020-07-26 20:52:04 +08:00
decode_as_ac(argv[2]);
break;
case '1':
2025-10-20 16:23:33 +08:00
printf("decode %s as command-typed binary in sub_cate %d\n", argv[2], ir_hex_encode);
2020-07-26 20:52:04 +08:00
decode_as_tv(argv[2], ir_hex_encode);
break;
default:
2025-10-20 16:23:33 +08:00
printf("decode functionality not supported : %c\n", function);
2020-07-26 20:52:04 +08:00
break;
}
}