updated examples

This commit is contained in:
strawmanbobi
2020-07-26 20:52:04 +08:00
parent d0cd9c34c6
commit e2d2f8024a
27 changed files with 1179 additions and 635 deletions

View File

@@ -1,237 +1,327 @@
#include "ir_decoder\include\ir_decode.h"
/**************************************************************************************
Filename: DecodeTestWin.cpp
Revised: Date: 2016-11-05
Revision: Revision: 1.0
#include<iostream>
using namespace std;
Description: This file provides main entry for irda decoder
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Revision log:
* 2016-11-05: created by strawmanbobi
**************************************************************************************/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ir_decoder\src\include\ir_defs.h"
#include "ir_decoder\src\include\ir_decode.h"
#define INPUT_MAX 3
// global variable definition
long binary_length = 0;
UINT8 *binary_content = NULL;
t_remote_ac_status ac_status;
UINT16 user_data[USER_DATA_SIZE] = { 0 };
UINT16 user_data[USER_DATA_SIZE];
INT8 decode_as_ac(char* file_name)
void input_number(int *val)
{
// keyboard input
int in_char = 0;
int count = 0;
BOOL op_match = TRUE;
UINT8 function_code = AC_FUNCTION_MAX;
// get status
UINT8 supported_mode = 0x00;
INT8 min_temperature = 0;
INT8 max_temperature = 0;
UINT8 supported_speed = 0x00;
UINT8 supported_swing = 0x00;
BOOL need_control = TRUE;
// 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_OFF;
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(0, 0, file_name))
{
ir_close();
return IR_DECODE_FAILED;
}
do
{
in_char = getchar();
op_match = TRUE;
need_control = TRUE;
switch (in_char)
{
case 'w':
case 'W':
// temperature plus
ac_status.ac_temp = (t_ac_temperature)((ac_status.ac_temp == AC_TEMP_30) ? AC_TEMP_30 : (ac_status.ac_temp + 1));
function_code = AC_FUNCTION_TEMPERATURE_UP;
break;
case 's':
case 'S':
ac_status.ac_temp = (t_ac_temperature)((ac_status.ac_temp == AC_TEMP_16) ? AC_TEMP_16 : (ac_status.ac_temp - 1));
function_code = AC_FUNCTION_TEMPERATURE_DOWN;
// temperature minus
break;
case 'a':
case 'A':
ac_status.ac_wind_speed = (t_ac_wind_speed) (ac_status.ac_wind_speed + 1);
ac_status.ac_wind_speed = (t_ac_wind_speed) (ac_status.ac_wind_speed % AC_WS_MAX);
function_code = AC_FUNCTION_WIND_SPEED;
// wind speed loop
break;
case 'd':
case 'D':
ac_status.ac_wind_dir = (t_ac_swing)((ac_status.ac_wind_dir == 0) ? 1 : 0);
function_code = AC_FUNCTION_WIND_SWING;
// wind swing loop
break;
case 'q':
case 'Q':
ac_status.ac_mode = (t_ac_mode)(ac_status.ac_mode + 1);
ac_status.ac_mode = (t_ac_mode) (ac_status.ac_mode % AC_MODE_MAX);
function_code = AC_FUNCTION_MODE;
break;
case '1':
// turn on
ac_status.ac_power = AC_POWER_ON;
function_code = AC_FUNCTION_POWER;
break;
case '2':
// turn off
ac_status.ac_power = AC_POWER_OFF;
// FUNCTION MAX refers to power off
// function_code = AC_FUNCTION_POWER;
break;
case '3':
if (IR_DECODE_SUCCEEDED == get_supported_mode(&supported_mode))
{
ir_printf("supported mode = %02X\n", supported_mode);
}
need_control = FALSE;
break;
case '4':
if (IR_DECODE_SUCCEEDED == get_supported_swing(ac_status.ac_mode, &supported_swing))
{
ir_printf("supported swing in %d = %02X\n", ac_status.ac_mode, supported_swing);
}
need_control = FALSE;
break;
case '5':
if (IR_DECODE_SUCCEEDED == get_supported_wind_speed(ac_status.ac_mode, &supported_speed))
{
ir_printf("supported wind speed in %d = %02X\n", ac_status.ac_mode, supported_speed);
}
need_control = FALSE;
break;
case '6':
if (IR_DECODE_SUCCEEDED == get_temperature_range(ac_status.ac_mode, &min_temperature, &max_temperature))
{
ir_printf("supported temperature range in mode %d = %d, %d\n",
ac_status.ac_mode, min_temperature, max_temperature);
}
need_control = FALSE;
break;
default:
op_match = FALSE;
break;
}
if (TRUE == op_match && TRUE == need_control)
{
ir_printf("switch AC to power = %d, mode = %d, temp = %d, speed = %d, swing = %d\n",
ac_status.ac_power,
ac_status.ac_mode,
ac_status.ac_temp,
ac_status.ac_wind_speed,
ac_status.ac_wind_dir
);
ir_decode(function_code, user_data, &ac_status, TRUE);
}
} while ('0' != in_char);
ir_close();
// free binary buffer
ir_free(binary_content);
binary_length = 0;
return IR_DECODE_SUCCEEDED;
char n[50]={0};
int i = 0;
*val = 0;
scanf_s("%3s", n);
getchar();
while(1)
{
if(n[i] < '0'||n[i] > '9')
{
printf("\nInvalid number format, please re-input : ");
scanf_s("%3s", n);
i=0;
}
else
{
i++;
}
if(n[i] == '\0')
break;
}
i = 0;
while (n[i] != '\0')
{
*val = (*val * 10 + (int)n[i] - 48);
i++;
}
}
INT8 decode_as_tv(char *file_name, UINT8 irda_hex_encode)
static INT8 decode_as_ac(char *file_name)
{
// keyboard input
int in_char = 0;
int key_code = -1;
int count = 0;
BOOL op_match = TRUE;
BOOL change_wind_dir = FALSE;
UINT8 function_code = AC_FUNCTION_MAX;
int key_code = 0;
int first_time = 1;
int length = 0;
int index = 0;
if (IR_DECODE_FAILED == ir_file_open(1, 1, file_name))
{
return IR_DECODE_FAILED;
}
// 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;
do
{
in_char = getchar();
if (in_char >= '0' && in_char <= '9')
{
key_code = in_char - '0';
ir_decode(key_code, user_data, NULL, FALSE);
}
else if (in_char >= 'a' && in_char <= 'f')
{
key_code = 10 + (in_char - 'a');
ir_decode(key_code, user_data, NULL, FALSE);
}
else if (in_char == 'q')
{
ir_close();
}
else
{
// do nothing
}
} while ('Q' != in_char);
BOOL need_control = TRUE;
// free binary buffer
ir_free(binary_content);
binary_length = 0;
// 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;
return IR_DECODE_SUCCEEDED;
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;
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
{
int temp_mode = 0;
int temp_wind_speed = 0;
switch (key_code)
{
case 0:
ac_status.ac_power = ((ac_status.ac_wind_dir == AC_POWER_ON) ? AC_POWER_OFF : AC_POWER_ON);
need_control = TRUE;
break;
case 1:
temp_mode = (int) ac_status.ac_mode;
temp_mode++;
ac_status.ac_mode = (t_ac_mode) (temp_mode % AC_MODE_MAX);
need_control = TRUE;
break;
case 2:
case 7:
ac_status.ac_temp = (t_ac_temperature) ((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 = (t_ac_temperature) ((ac_status.ac_temp == AC_TEMP_16) ? AC_TEMP_16 : (ac_status.ac_temp - 1));
need_control = TRUE;
break;
case 9:
temp_wind_speed = (int)ac_status.ac_wind_speed;
temp_wind_speed++;
ac_status.ac_wind_speed = (t_ac_wind_speed) (temp_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)
{
printf("switch AC to power = %d, mode = %d, temp = %d, speed = %d, swing = %d with function 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,
function_code);
length = ir_decode(function_code, user_data, &ac_status, change_wind_dir);
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))
{
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, 0);
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 void print_usage(const char *progn) {
printf("Usage: %s [function] [file] [subcate]\n"
"[function] : 0 - decode for AC; 1 - decode for TV\n"
"[file] : the remote control binary file\n"
"[subcate] : the sub_cate value from remote_index", progn);
}
int main(int argc, char *argv[])
{
int nRetCode = 0;
char function = '0';
UINT8 ir_hex_encode = 0;
char function = '0';
UINT8 irda_hex_encode = 0;
if (4 != argc)
{
print_usage(argv[0]);
return -1;
}
if (4 != argc)
{
ir_printf("number of args error !\n");
return -1;
}
function = argv[1][0];
ir_hex_encode = (UINT8) (argv[3][0] - '0');
function = argv[1][0];
irda_hex_encode = (UINT8)(argv[3][0] - '0');
ir_printf("decode functionality = %c\n", function);
switch (function)
{
case '0':
printf("Decode %s as status-typed binary\n", argv[2]);
decode_as_ac(argv[2]);
break;
switch (function)
{
case '0':
ir_printf("decode binary file as AC\n");
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;
case '1':
ir_printf("decode binary file as TV : %d\n", irda_hex_encode);
decode_as_tv(argv[2], irda_hex_encode);
break;
default:
printf("Decode functionality not supported : %c\n", function);
break;
}
default:
ir_printf("decode functionality error !\n");
break;
}
system("pause");
return nRetCode;
}