2021-08-08 18:03:25 +00:00
|
|
|
#include <furi-hal-delay.h>
|
2021-07-16 16:43:54 +00:00
|
|
|
#include <irda.h>
|
|
|
|
#include <app-template.h>
|
|
|
|
#include <cli/cli.h>
|
|
|
|
#include <cmsis_os2.h>
|
|
|
|
#include <irda_worker.h>
|
2021-06-09 13:04:49 +00:00
|
|
|
#include <furi.h>
|
2021-08-08 18:03:25 +00:00
|
|
|
#include <furi-hal-irda.h>
|
2021-06-09 13:04:49 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <m-string.h>
|
2021-07-16 16:43:54 +00:00
|
|
|
#include <irda_transmit.h>
|
2021-08-05 21:11:35 +00:00
|
|
|
#include <sys/types.h>
|
2021-06-09 13:04:49 +00:00
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
static void signal_received_callback(void* context, IrdaWorkerSignal* received_signal) {
|
|
|
|
furi_assert(received_signal);
|
|
|
|
char buf[100];
|
|
|
|
size_t buf_cnt;
|
|
|
|
Cli* cli = (Cli*)context;
|
|
|
|
|
|
|
|
if(irda_worker_signal_is_decoded(received_signal)) {
|
2021-08-11 17:51:06 +00:00
|
|
|
const IrdaMessage* message = irda_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",
|
|
|
|
irda_get_protocol_name(message->protocol),
|
2021-09-09 21:37:32 +00:00
|
|
|
ROUND_UP_TO(irda_get_protocol_address_length(message->protocol), 4),
|
2021-07-16 16:43:54 +00:00
|
|
|
message->address,
|
2021-09-09 21:37:32 +00:00
|
|
|
ROUND_UP_TO(irda_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;
|
|
|
|
irda_worker_get_raw_signal(received_signal, &timings, &timings_cnt);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:47:48 +00:00
|
|
|
void irda_cli_start_ir_rx(Cli* cli, string_t args, void* context) {
|
2021-08-08 18:03:25 +00:00
|
|
|
if(furi_hal_irda_is_busy()) {
|
2021-06-09 13:04:49 +00:00
|
|
|
printf("IRDA is busy. Exit.");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-16 16:43:54 +00:00
|
|
|
|
|
|
|
IrdaWorker* worker = irda_worker_alloc();
|
2021-08-11 17:51:06 +00:00
|
|
|
irda_worker_rx_start(worker);
|
|
|
|
irda_worker_rx_set_received_signal_callback(worker, signal_received_callback, cli);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
printf("Receiving IRDA...\r\nPress Ctrl+C to abort\r\n");
|
|
|
|
while(!cli_cmd_interrupt_received(cli)) {
|
2021-07-16 16:43:54 +00:00
|
|
|
delay(50);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:51:06 +00:00
|
|
|
irda_worker_rx_stop(worker);
|
2021-07-16 16:43:54 +00:00
|
|
|
irda_worker_free(worker);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void irda_cli_print_usage(void) {
|
2021-06-25 12:25:03 +00:00
|
|
|
printf("Usage:\r\n\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:");
|
|
|
|
for(int i = 0; irda_is_protocol_valid((IrdaProtocol)i); ++i) {
|
|
|
|
printf(" %s", irda_get_protocol_name((IrdaProtocol)i));
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
IRDA_MIN_FREQUENCY,
|
|
|
|
IRDA_MAX_FREQUENCY);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
static bool parse_message(const char* str, IrdaMessage* message) {
|
2021-06-09 13:04:49 +00:00
|
|
|
uint32_t command = 0;
|
|
|
|
uint32_t address = 0;
|
2021-06-25 12:25:03 +00:00
|
|
|
char protocol_name[32];
|
2021-07-16 16:43:54 +00:00
|
|
|
int parsed = sscanf(str, "%31s %lX %lX", protocol_name, &address, &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
|
|
|
}
|
|
|
|
|
2021-06-25 12:25:03 +00:00
|
|
|
IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
|
2021-06-09 13:04:49 +00:00
|
|
|
|
|
|
|
if(!irda_is_protocol_valid(protocol)) {
|
2021-07-16 16:43:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:37:32 +00:00
|
|
|
uint32_t address_length = irda_get_protocol_address_length(protocol);
|
|
|
|
uint32_t address_mask = (1LU << address_length) - 1;
|
|
|
|
if(address != (address & address_mask)) {
|
|
|
|
printf("Address out of range (mask 0x%08lX): 0x%lX\r\n", address_mask, address);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t command_length = irda_get_protocol_command_length(protocol);
|
|
|
|
uint32_t command_mask = (1LU << command_length) - 1;
|
|
|
|
if(command != (command & command_mask)) {
|
|
|
|
printf("Command out of range (mask 0x%08lX): 0x%lX\r\n", command_mask, command);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
message->protocol = protocol;
|
|
|
|
message->address = address;
|
|
|
|
message->command = command;
|
|
|
|
message->repeat = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-08-19 00:18:42 +00:00
|
|
|
if((*frequency > IRDA_MAX_FREQUENCY) || (*frequency < IRDA_MIN_FREQUENCY)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((*duty_cycle <= 0) || (*duty_cycle > 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-19 00:18:42 +00:00
|
|
|
if(*timings_cnt > 0) {
|
|
|
|
printf("\r\nTransmit:");
|
|
|
|
for(size_t i = 0; i < *timings_cnt; ++i) {
|
|
|
|
printf(" %ld", timings[i]);
|
|
|
|
}
|
|
|
|
printf("\r\n");
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 00:18:42 +00:00
|
|
|
return (parsed == 2) && (*timings_cnt > 0);
|
2021-07-16 16:43:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 18:47:48 +00:00
|
|
|
void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) {
|
2021-08-08 18:03:25 +00:00
|
|
|
if(furi_hal_irda_is_busy()) {
|
2021-07-16 16:43:54 +00:00
|
|
|
printf("IRDA is busy. Exit.");
|
2021-06-09 13:04:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:43:54 +00:00
|
|
|
IrdaMessage message;
|
|
|
|
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;
|
2021-08-19 00:18:42 +00:00
|
|
|
uint32_t* timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * 512);
|
|
|
|
uint32_t timings_cnt = 512;
|
2021-07-16 16:43:54 +00:00
|
|
|
|
|
|
|
if(parse_message(str, &message)) {
|
|
|
|
irda_send(&message, 1);
|
|
|
|
} else if(parse_signal_raw(str, timings, &timings_cnt, &duty_cycle, &frequency)) {
|
2021-08-05 21:11:35 +00:00
|
|
|
irda_send_raw_ext(timings, timings_cnt, true, frequency, duty_cycle);
|
2021-07-16 16:43:54 +00:00
|
|
|
} else {
|
|
|
|
printf("Wrong arguments.\r\n");
|
|
|
|
irda_cli_print_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
free(timings);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 18:47:48 +00:00
|
|
|
extern "C" void irda_on_system_start() {
|
|
|
|
#ifdef SRV_CLI
|
2021-06-09 13:04:49 +00:00
|
|
|
Cli* cli = (Cli*)furi_record_open("cli");
|
2021-07-18 18:09:00 +00:00
|
|
|
cli_add_command(cli, "ir_rx", CliCommandFlagDefault, irda_cli_start_ir_rx, NULL);
|
|
|
|
cli_add_command(cli, "ir_tx", CliCommandFlagDefault, irda_cli_start_ir_tx, 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
|
|
|
}
|