2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_delay.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include <infrared.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <app_template.h>
|
2021-07-16 16:43:54 +00:00
|
|
|
#include <cli/cli.h>
|
|
|
|
#include <cmsis_os2.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include <infrared_worker.h>
|
2021-06-09 13:04:49 +00:00
|
|
|
#include <furi.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include <furi_hal_infrared.h>
|
2021-06-09 13:04:49 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <m-string.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include <infrared_transmit.h>
|
2021-08-05 21:11:35 +00:00
|
|
|
#include <sys/types.h>
|
2022-02-25 15:22:58 +00:00
|
|
|
#include "../helpers/infrared_parser.h"
|
2021-06-09 13:04:49 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_cli_start_ir_rx(Cli* cli, string_t args);
|
|
|
|
static void infrared_cli_start_ir_tx(Cli* cli, string_t args);
|
2022-02-16 11:25:23 +00:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* cmd;
|
|
|
|
void (*process_function)(Cli* cli, string_t args);
|
2022-02-25 15:22:58 +00:00
|
|
|
} infrared_cli_commands[] = {
|
|
|
|
{.cmd = "rx", .process_function = infrared_cli_start_ir_rx},
|
|
|
|
{.cmd = "tx", .process_function = infrared_cli_start_ir_tx},
|
2022-02-16 11:25:23 +00:00
|
|
|
};
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
|
2021-07-16 16:43:54 +00:00
|
|
|
furi_assert(received_signal);
|
|
|
|
char buf[100];
|
|
|
|
size_t buf_cnt;
|
|
|
|
Cli* cli = (Cli*)context;
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if(infrared_worker_signal_is_decoded(received_signal)) {
|
|
|
|
const InfraredMessage* message = infrared_worker_get_decoded_signal(received_signal);
|
2021-07-16 16:43:54 +00:00
|
|
|
buf_cnt = sniprintf(
|
|
|
|
buf,
|
|
|
|
sizeof(buf),
|
|
|
|
"%s, A:0x%0*lX, C:0x%0*lX%s\r\n",
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_get_protocol_name(message->protocol),
|
|
|
|
ROUND_UP_TO(infrared_get_protocol_address_length(message->protocol), 4),
|
2021-07-16 16:43:54 +00:00
|
|
|
message->address,
|
2022-02-25 15:22:58 +00:00
|
|
|
ROUND_UP_TO(infrared_get_protocol_command_length(message->protocol), 4),
|
2021-07-16 16:43:54 +00:00
|
|
|
message->command,
|
|
|
|
message->repeat ? " R" : "");
|
|
|
|
cli_write(cli, (uint8_t*)buf, buf_cnt);
|
|
|
|
} else {
|
|
|
|
const uint32_t* timings;
|
|
|
|
size_t timings_cnt;
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_worker_get_raw_signal(received_signal, &timings, &timings_cnt);
|
2021-07-16 16:43:54 +00:00
|
|
|
|
|
|
|
buf_cnt = sniprintf(buf, sizeof(buf), "RAW, %d samples:\r\n", timings_cnt);
|
|
|
|
cli_write(cli, (uint8_t*)buf, buf_cnt);
|
|
|
|
for(size_t i = 0; i < timings_cnt; ++i) {
|
|
|
|
buf_cnt = sniprintf(buf, sizeof(buf), "%lu ", timings[i]);
|
|
|
|
cli_write(cli, (uint8_t*)buf, buf_cnt);
|
|
|
|
}
|
|
|
|
buf_cnt = sniprintf(buf, sizeof(buf), "\r\n");
|
|
|
|
cli_write(cli, (uint8_t*)buf, buf_cnt);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_cli_start_ir_rx(Cli* cli, string_t args) {
|
|
|
|
InfraredWorker* worker = infrared_worker_alloc();
|
|
|
|
infrared_worker_rx_start(worker);
|
|
|
|
infrared_worker_rx_set_received_signal_callback(worker, signal_received_callback, cli);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
printf("Receiving INFRARED...\r\nPress Ctrl+C to abort\r\n");
|
2021-06-09 13:04:49 +00:00
|
|
|
while(!cli_cmd_interrupt_received(cli)) {
|
2022-03-30 15:23:40 +00:00
|
|
|
furi_hal_delay_ms(50);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_worker_rx_stop(worker);
|
|
|
|
infrared_worker_free(worker);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_cli_print_usage(void) {
|
2022-02-16 11:25:23 +00:00
|
|
|
printf("Usage:\r\n");
|
|
|
|
printf("\tir rx\r\n");
|
|
|
|
printf("\tir tx <protocol> <address> <command>\r\n");
|
2021-06-09 13:04:49 +00:00
|
|
|
printf("\t<command> and <address> are hex-formatted\r\n");
|
|
|
|
printf("\tAvailable protocols:");
|
2022-02-25 15:22:58 +00:00
|
|
|
for(int i = 0; infrared_is_protocol_valid((InfraredProtocol)i); ++i) {
|
|
|
|
printf(" %s", infrared_get_protocol_name((InfraredProtocol)i));
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
printf("\r\n");
|
2021-08-19 00:18:42 +00:00
|
|
|
printf("\tRaw format:\r\n");
|
|
|
|
printf("\tir_tx RAW F:<frequency> DC:<duty_cycle> <sample0> <sample1>...\r\n");
|
|
|
|
printf(
|
|
|
|
"\tFrequency (%d - %d), Duty cycle (0 - 100), max 512 samples\r\n",
|
2022-02-25 15:22:58 +00:00
|
|
|
INFRARED_MIN_FREQUENCY,
|
|
|
|
INFRARED_MAX_FREQUENCY);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static bool parse_message(const char* str, InfraredMessage* message) {
|
2021-06-25 12:25:03 +00:00
|
|
|
char protocol_name[32];
|
2022-01-10 17:13:41 +00:00
|
|
|
int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &message->address, &message->command);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
2021-06-25 12:25:03 +00:00
|
|
|
if(parsed != 3) {
|
2021-07-16 16:43:54 +00:00
|
|
|
return false;
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
message->protocol = infrared_get_protocol_by_name(protocol_name);
|
2021-07-16 16:43:54 +00:00
|
|
|
message->repeat = false;
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
return infrared_parser_is_parsed_signal_valid(message);
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_signal_raw(
|
|
|
|
const char* str,
|
|
|
|
uint32_t* timings,
|
|
|
|
uint32_t* timings_cnt,
|
|
|
|
float* duty_cycle,
|
2021-08-05 21:11:35 +00:00
|
|
|
uint32_t* frequency) {
|
2021-07-16 16:43:54 +00:00
|
|
|
char frequency_str[10];
|
|
|
|
char duty_cycle_str[10];
|
|
|
|
int parsed = sscanf(str, "RAW F:%9s DC:%9s", frequency_str, duty_cycle_str);
|
|
|
|
if(parsed != 2) return false;
|
|
|
|
|
|
|
|
*frequency = atoi(frequency_str);
|
|
|
|
*duty_cycle = (float)atoi(duty_cycle_str) / 100;
|
|
|
|
str += strlen(frequency_str) + strlen(duty_cycle_str) + 10;
|
|
|
|
|
|
|
|
uint32_t timings_cnt_max = *timings_cnt;
|
|
|
|
*timings_cnt = 0;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
char timing_str[10];
|
|
|
|
for(; *str == ' '; ++str)
|
|
|
|
;
|
|
|
|
if(1 != sscanf(str, "%9s", timing_str)) break;
|
|
|
|
str += strlen(timing_str);
|
|
|
|
uint32_t timing = atoi(timing_str);
|
|
|
|
if(timing <= 0) break;
|
|
|
|
if(*timings_cnt >= timings_cnt_max) break;
|
|
|
|
timings[*timings_cnt] = timing;
|
|
|
|
++*timings_cnt;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
return infrared_parser_is_raw_signal_valid(*frequency, *duty_cycle, *timings_cnt);
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_cli_start_ir_tx(Cli* cli, string_t args) {
|
|
|
|
InfraredMessage message;
|
2021-07-16 16:43:54 +00:00
|
|
|
const char* str = string_get_cstr(args);
|
2021-08-05 21:11:35 +00:00
|
|
|
uint32_t frequency;
|
2021-07-16 16:43:54 +00:00
|
|
|
float duty_cycle;
|
2022-01-10 17:13:41 +00:00
|
|
|
uint32_t timings_cnt = MAX_TIMINGS_AMOUNT;
|
2022-02-18 19:53:46 +00:00
|
|
|
uint32_t* timings = (uint32_t*)malloc(sizeof(uint32_t) * timings_cnt);
|
2021-07-16 16:43:54 +00:00
|
|
|
|
|
|
|
if(parse_message(str, &message)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_send(&message, 1);
|
2021-07-16 16:43:54 +00:00
|
|
|
} else if(parse_signal_raw(str, timings, &timings_cnt, &duty_cycle, &frequency)) {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_send_raw_ext(timings, timings_cnt, true, frequency, duty_cycle);
|
2021-07-16 16:43:54 +00:00
|
|
|
} else {
|
|
|
|
printf("Wrong arguments.\r\n");
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_cli_print_usage();
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(timings);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
static void infrared_cli_start_ir(Cli* cli, string_t args, void* context) {
|
|
|
|
if(furi_hal_infrared_is_busy()) {
|
|
|
|
printf("INFRARED is busy. Exit.");
|
2022-02-16 11:25:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t i = 0;
|
2022-02-25 15:22:58 +00:00
|
|
|
for(; i < COUNT_OF(infrared_cli_commands); ++i) {
|
|
|
|
size_t size = strlen(infrared_cli_commands[i].cmd);
|
|
|
|
bool cmd_found = !strncmp(string_get_cstr(args), infrared_cli_commands[i].cmd, size);
|
2022-02-16 11:25:23 +00:00
|
|
|
if(cmd_found) {
|
|
|
|
if(string_size(args) == size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(string_get_cstr(args)[size] == ' ') {
|
|
|
|
string_right(args, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if(i < COUNT_OF(infrared_cli_commands)) {
|
|
|
|
infrared_cli_commands[i].process_function(cli, args);
|
2022-02-16 11:25:23 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
infrared_cli_print_usage();
|
2022-02-16 11:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
extern "C" void infrared_on_system_start() {
|
2021-12-24 18:47:48 +00:00
|
|
|
#ifdef SRV_CLI
|
2021-06-09 13:04:49 +00:00
|
|
|
Cli* cli = (Cli*)furi_record_open("cli");
|
2022-02-25 15:22:58 +00:00
|
|
|
cli_add_command(cli, "ir", CliCommandFlagDefault, infrared_cli_start_ir, NULL);
|
2021-06-09 13:04:49 +00:00
|
|
|
furi_record_close("cli");
|
2021-12-24 18:47:48 +00:00
|
|
|
#endif
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|