2020-10-23 09:39:11 +00:00
|
|
|
#include "flipper.h"
|
|
|
|
#include "flipper_v2.h"
|
|
|
|
#include "irda_nec.h"
|
2020-10-26 07:27:27 +00:00
|
|
|
#include "irda_samsung.h"
|
2020-10-23 09:39:11 +00:00
|
|
|
#include "irda_protocols.h"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EventTypeTick,
|
|
|
|
EventTypeKey,
|
2020-11-11 07:36:27 +00:00
|
|
|
EventTypeRX,
|
2020-10-23 09:39:11 +00:00
|
|
|
} EventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
InputEvent input;
|
2020-11-11 07:36:27 +00:00
|
|
|
bool rx_edge;
|
2020-10-23 09:39:11 +00:00
|
|
|
} value;
|
|
|
|
EventType type;
|
2020-10-26 09:26:15 +00:00
|
|
|
} AppEvent;
|
2020-10-23 09:39:11 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t mode_id;
|
|
|
|
uint16_t carrier_freq;
|
|
|
|
uint8_t carrier_duty_cycle_id;
|
|
|
|
uint8_t nec_packet_id;
|
2020-10-26 07:27:27 +00:00
|
|
|
uint8_t samsung_packet_id;
|
2020-10-23 09:39:11 +00:00
|
|
|
} State;
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
typedef void (*ModeInput)(AppEvent*, State*);
|
2020-10-23 09:39:11 +00:00
|
|
|
typedef void (*ModeRender)(CanvasApi*, State*);
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_carrier(AppEvent* event, State* state);
|
2020-10-23 09:39:11 +00:00
|
|
|
void render_carrier(CanvasApi* canvas, State* state);
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_nec(AppEvent* event, State* state);
|
2020-10-23 09:39:11 +00:00
|
|
|
void render_nec(CanvasApi* canvas, State* state);
|
2020-10-26 07:27:27 +00:00
|
|
|
void render_carrier(CanvasApi* canvas, State* state);
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_samsung(AppEvent* event, State* state);
|
2020-10-26 07:27:27 +00:00
|
|
|
void render_samsung(CanvasApi* canvas, State* state);
|
2020-10-23 09:39:11 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ModeRender render;
|
|
|
|
ModeInput input;
|
|
|
|
} Mode;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t addr;
|
|
|
|
uint8_t data;
|
|
|
|
} NecPacket;
|
|
|
|
|
2020-10-26 07:27:27 +00:00
|
|
|
typedef struct {
|
|
|
|
uint16_t addr;
|
|
|
|
uint16_t data;
|
|
|
|
} SamsungPacket;
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
const Mode modes[] = {
|
|
|
|
{.render = render_carrier, .input = input_carrier},
|
|
|
|
{.render = render_nec, .input = input_nec},
|
2020-10-26 07:27:27 +00:00
|
|
|
{.render = render_samsung, .input = input_samsung},
|
2020-10-23 09:39:11 +00:00
|
|
|
};
|
|
|
|
|
2020-10-26 07:27:27 +00:00
|
|
|
const NecPacket nec_packets[] = {
|
|
|
|
{.addr = 0xFF, .data = 0x11},
|
2020-10-23 09:39:11 +00:00
|
|
|
{.addr = 0xF7, .data = 0x59},
|
|
|
|
{.addr = 0xFF, .data = 0x01},
|
|
|
|
{.addr = 0xFF, .data = 0x10},
|
|
|
|
{.addr = 0xFF, .data = 0x15},
|
|
|
|
{.addr = 0xFF, .data = 0x25},
|
|
|
|
{.addr = 0xFF, .data = 0xF0},
|
|
|
|
};
|
|
|
|
|
2020-10-26 07:27:27 +00:00
|
|
|
const SamsungPacket samsung_packets[] = {
|
|
|
|
{.addr = 0xE0E, .data = 0xF30C},
|
|
|
|
{.addr = 0xE0E, .data = 0xF40D},
|
|
|
|
{.addr = 0xE0E, .data = 0xF50E},
|
|
|
|
};
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
const float duty_cycles[] = {0.1, 0.25, 0.333, 0.5, 1.0};
|
|
|
|
|
|
|
|
void render_carrier(CanvasApi* canvas, State* state) {
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
canvas->draw_str(canvas, 2, 25, "carrier mode >");
|
|
|
|
canvas->draw_str(canvas, 2, 37, "? /\\ freq | \\/ duty cycle");
|
|
|
|
{
|
|
|
|
char buf[24];
|
2020-10-29 07:58:19 +00:00
|
|
|
sprintf(buf, "frequency: %u Hz", state->carrier_freq);
|
2020-10-23 09:39:11 +00:00
|
|
|
canvas->draw_str(canvas, 2, 50, buf);
|
|
|
|
sprintf(
|
2020-10-29 07:58:19 +00:00
|
|
|
buf, "duty cycle: %d/1000", (int)(duty_cycles[state->carrier_duty_cycle_id] * 1000));
|
2020-10-23 09:39:11 +00:00
|
|
|
canvas->draw_str(canvas, 2, 62, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_nec(CanvasApi* canvas, State* state) {
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
2020-10-26 07:27:27 +00:00
|
|
|
canvas->draw_str(canvas, 2, 25, "< nec mode >");
|
|
|
|
canvas->draw_str(canvas, 2, 37, "? /\\ \\/ packet");
|
|
|
|
{
|
|
|
|
char buf[24];
|
|
|
|
sprintf(
|
|
|
|
buf,
|
|
|
|
"packet: %02X %02X",
|
|
|
|
nec_packets[state->nec_packet_id].addr,
|
|
|
|
nec_packets[state->nec_packet_id].data);
|
|
|
|
canvas->draw_str(canvas, 2, 50, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_samsung(CanvasApi* canvas, State* state) {
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
canvas->draw_str(canvas, 2, 25, "< samsung32 mode");
|
2020-10-23 09:39:11 +00:00
|
|
|
canvas->draw_str(canvas, 2, 37, "? /\\ \\/ packet");
|
|
|
|
{
|
|
|
|
char buf[24];
|
|
|
|
sprintf(
|
|
|
|
buf,
|
|
|
|
"packet: %02X %02X",
|
2020-10-26 07:27:27 +00:00
|
|
|
samsung_packets[state->samsung_packet_id].addr,
|
|
|
|
samsung_packets[state->samsung_packet_id].data);
|
2020-10-23 09:39:11 +00:00
|
|
|
canvas->draw_str(canvas, 2, 50, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_carrier(AppEvent* event, State* state) {
|
2020-10-23 09:39:11 +00:00
|
|
|
if(event->value.input.input == InputOk) {
|
|
|
|
if(event->value.input.state) {
|
2020-11-06 08:31:59 +00:00
|
|
|
irda_pwm_set(duty_cycles[state->carrier_duty_cycle_id], state->carrier_freq);
|
2020-10-23 09:39:11 +00:00
|
|
|
} else {
|
2020-11-06 08:31:59 +00:00
|
|
|
irda_pwm_stop();
|
2020-10-23 09:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputUp) {
|
|
|
|
if(state->carrier_freq < 45000) {
|
|
|
|
state->carrier_freq += 1000;
|
|
|
|
} else {
|
|
|
|
state->carrier_freq = 33000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputDown) {
|
|
|
|
uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
|
|
|
|
if(state->carrier_duty_cycle_id < (duty_cycles_count - 1)) {
|
|
|
|
state->carrier_duty_cycle_id++;
|
|
|
|
} else {
|
|
|
|
state->carrier_duty_cycle_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_nec(AppEvent* event, State* state) {
|
2020-10-26 07:27:27 +00:00
|
|
|
uint8_t packets_count = sizeof(nec_packets) / sizeof(nec_packets[0]);
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
if(event->value.input.input == InputOk) {
|
|
|
|
if(event->value.input.state) {
|
|
|
|
vTaskSuspendAll();
|
2020-10-26 07:27:27 +00:00
|
|
|
ir_nec_send(
|
|
|
|
nec_packets[state->nec_packet_id].addr, nec_packets[state->nec_packet_id].data);
|
2020-10-23 09:39:11 +00:00
|
|
|
xTaskResumeAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputUp) {
|
|
|
|
if(state->nec_packet_id < (packets_count - 1)) {
|
|
|
|
state->nec_packet_id++;
|
|
|
|
} else {
|
|
|
|
state->nec_packet_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputDown) {
|
|
|
|
if(state->nec_packet_id > 0) {
|
|
|
|
state->nec_packet_id--;
|
|
|
|
} else {
|
|
|
|
state->nec_packet_id = packets_count - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
void input_samsung(AppEvent* event, State* state) {
|
2020-10-26 07:27:27 +00:00
|
|
|
uint8_t packets_count = sizeof(samsung_packets) / sizeof(samsung_packets[0]);
|
|
|
|
|
|
|
|
if(event->value.input.input == InputOk) {
|
|
|
|
if(event->value.input.state) {
|
|
|
|
vTaskSuspendAll();
|
|
|
|
ir_samsung_send(
|
|
|
|
samsung_packets[state->samsung_packet_id].addr,
|
|
|
|
samsung_packets[state->samsung_packet_id].data);
|
|
|
|
xTaskResumeAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputUp) {
|
|
|
|
if(state->samsung_packet_id < (packets_count - 1)) {
|
|
|
|
state->samsung_packet_id++;
|
|
|
|
} else {
|
|
|
|
state->samsung_packet_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputDown) {
|
|
|
|
if(state->samsung_packet_id > 0) {
|
|
|
|
state->samsung_packet_id--;
|
|
|
|
} else {
|
|
|
|
state->samsung_packet_id = packets_count - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
static void render_callback(CanvasApi* canvas, void* ctx) {
|
|
|
|
State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
|
|
|
|
|
|
|
|
canvas->clear(canvas);
|
|
|
|
canvas->set_color(canvas, ColorBlack);
|
|
|
|
canvas->set_font(canvas, FontPrimary);
|
|
|
|
canvas->draw_str(canvas, 2, 12, "irda test");
|
|
|
|
|
|
|
|
modes[state->mode_id].render(canvas, state);
|
|
|
|
|
|
|
|
release_mutex((ValueMutex*)ctx, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void input_callback(InputEvent* input_event, void* ctx) {
|
|
|
|
osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
AppEvent event;
|
2020-10-23 09:39:11 +00:00
|
|
|
event.type = EventTypeKey;
|
|
|
|
event.value.input = *input_event;
|
|
|
|
osMessageQueuePut(event_queue, &event, 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-11-11 07:36:27 +00:00
|
|
|
osMessageQueueId_t irda_event_queue;
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
void irda(void* p) {
|
2020-11-11 07:36:27 +00:00
|
|
|
osMessageQueueId_t event_queue = osMessageQueueNew(32, sizeof(AppEvent), NULL);
|
|
|
|
irda_event_queue = event_queue;
|
2020-10-23 09:39:11 +00:00
|
|
|
|
|
|
|
State _state;
|
|
|
|
uint8_t mode_count = sizeof(modes) / sizeof(modes[0]);
|
|
|
|
uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
|
|
|
|
|
|
|
|
_state.carrier_duty_cycle_id = duty_cycles_count - 2;
|
|
|
|
_state.carrier_freq = 36000;
|
|
|
|
_state.mode_id = 0;
|
|
|
|
_state.nec_packet_id = 0;
|
2020-10-26 07:27:27 +00:00
|
|
|
_state.samsung_packet_id = 0;
|
2020-10-23 09:39:11 +00:00
|
|
|
|
|
|
|
ValueMutex state_mutex;
|
|
|
|
if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
|
|
|
|
printf("cannot create mutex\n");
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* widget = widget_alloc();
|
|
|
|
|
|
|
|
widget_draw_callback_set(widget, render_callback, &state_mutex);
|
|
|
|
widget_input_callback_set(widget, input_callback, event_queue);
|
|
|
|
|
|
|
|
// Open GUI and register widget
|
|
|
|
GuiApi* gui = (GuiApi*)furi_open("gui");
|
|
|
|
if(gui == NULL) {
|
|
|
|
printf("gui is not available\n");
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
2020-10-26 17:00:17 +00:00
|
|
|
gui->add_widget(gui, widget, GuiLayerFullscreen);
|
2020-10-23 09:39:11 +00:00
|
|
|
|
2020-11-11 07:36:27 +00:00
|
|
|
// Red LED
|
|
|
|
// TODO open record
|
2020-11-19 12:25:32 +00:00
|
|
|
const GpioPin* led_record = &led_gpio[0];
|
2020-11-11 07:36:27 +00:00
|
|
|
|
|
|
|
// configure pin
|
|
|
|
gpio_init(led_record, GpioModeOutputOpenDrain);
|
|
|
|
|
|
|
|
// setup irda rx timer
|
|
|
|
tim_irda_rx_init();
|
|
|
|
|
2020-10-26 09:26:15 +00:00
|
|
|
AppEvent event;
|
2020-10-23 09:39:11 +00:00
|
|
|
while(1) {
|
|
|
|
osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
|
|
|
|
State* state = (State*)acquire_mutex_block(&state_mutex);
|
|
|
|
|
|
|
|
if(event_status == osOK) {
|
|
|
|
if(event.type == EventTypeKey) {
|
|
|
|
// press events
|
|
|
|
if(event.value.input.state && event.value.input.input == InputBack) {
|
|
|
|
printf("[irda] bye!\n");
|
|
|
|
// TODO remove all widgets create by app
|
|
|
|
widget_enabled_set(widget, false);
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputLeft) {
|
|
|
|
if(state->mode_id > 0) {
|
|
|
|
state->mode_id--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputRight) {
|
|
|
|
if(state->mode_id < (mode_count - 1)) {
|
|
|
|
state->mode_id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modes[state->mode_id].input(&event, state);
|
2020-11-11 07:36:27 +00:00
|
|
|
} else if(event.type == EventTypeRX) {
|
|
|
|
gpio_write(led_record, event.value.rx_edge);
|
2020-10-23 09:39:11 +00:00
|
|
|
}
|
2020-11-11 07:36:27 +00:00
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
} else {
|
|
|
|
// event timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
release_mutex(&state_mutex, state);
|
|
|
|
widget_update(widget);
|
|
|
|
}
|
2020-11-11 07:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {
|
|
|
|
if(htim->Instance == TIM2) {
|
|
|
|
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
|
|
|
|
// falling event
|
|
|
|
AppEvent event;
|
|
|
|
event.type = EventTypeRX;
|
|
|
|
event.value.rx_edge = false;
|
|
|
|
osMessageQueuePut(irda_event_queue, &event, 0, 0);
|
|
|
|
} else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
|
|
|
|
// rising event
|
|
|
|
//uint32_t period_in_us = HAL_TIM_ReadCapturedValue();
|
|
|
|
AppEvent event;
|
|
|
|
event.type = EventTypeRX;
|
|
|
|
event.value.rx_edge = true;
|
|
|
|
osMessageQueuePut(irda_event_queue, &event, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 09:39:11 +00:00
|
|
|
}
|