2020-11-16 11:21:58 +00:00
|
|
|
#pragma once
|
|
|
|
#include "ibutton.h"
|
2021-01-28 12:30:31 +00:00
|
|
|
#include "one_wire_slave.h"
|
2020-11-25 07:25:13 +00:00
|
|
|
#include "one_wire_device_ds_1990.h"
|
2021-01-28 12:30:31 +00:00
|
|
|
#include "callback-connector.h"
|
|
|
|
#include <atomic>
|
2020-11-16 11:21:58 +00:00
|
|
|
|
|
|
|
class AppiButtonModeDallasEmulate : public AppTemplateMode<AppiButtonState, AppiButtonEvent> {
|
2021-01-28 12:30:31 +00:00
|
|
|
private:
|
|
|
|
void result_callback(bool success, void* ctx);
|
|
|
|
|
2020-11-16 11:21:58 +00:00
|
|
|
public:
|
|
|
|
const char* name = "dallas emulate";
|
|
|
|
AppiButton* app;
|
2020-11-25 07:25:13 +00:00
|
|
|
DS1990 key;
|
2021-01-28 12:30:31 +00:00
|
|
|
OneWireSlave* onewire_slave;
|
2020-11-16 11:21:58 +00:00
|
|
|
|
|
|
|
void event(AppiButtonEvent* event, AppiButtonState* state);
|
2020-12-14 10:50:32 +00:00
|
|
|
void render(Canvas* canvas, AppiButtonState* state);
|
2020-11-16 11:21:58 +00:00
|
|
|
void acquire();
|
|
|
|
void release();
|
|
|
|
|
2021-01-28 12:30:31 +00:00
|
|
|
std::atomic<bool> emulated_result{false};
|
|
|
|
|
2020-11-25 07:25:13 +00:00
|
|
|
AppiButtonModeDallasEmulate(AppiButton* parent_app)
|
|
|
|
: key(1, 2, 3, 4, 5, 6, 7) {
|
2020-11-16 11:21:58 +00:00
|
|
|
app = parent_app;
|
|
|
|
|
|
|
|
// TODO open record
|
2020-11-19 12:25:32 +00:00
|
|
|
const GpioPin* one_wire_pin_record = &ibutton_gpio;
|
2021-01-28 12:30:31 +00:00
|
|
|
onewire_slave = new OneWireSlave(one_wire_pin_record);
|
|
|
|
onewire_slave->attach(&key);
|
|
|
|
|
|
|
|
auto cb = cbc::obtain_connector(this, &AppiButtonModeDallasEmulate::result_callback);
|
|
|
|
onewire_slave->set_result_callback(cb, this);
|
2020-11-16 11:21:58 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-28 12:30:31 +00:00
|
|
|
void AppiButtonModeDallasEmulate::result_callback(bool success, void* ctx) {
|
|
|
|
AppiButtonModeDallasEmulate* _this = static_cast<AppiButtonModeDallasEmulate*>(ctx);
|
|
|
|
_this->emulated_result = success;
|
|
|
|
}
|
|
|
|
|
2020-11-16 11:21:58 +00:00
|
|
|
void AppiButtonModeDallasEmulate::event(AppiButtonEvent* event, AppiButtonState* state) {
|
|
|
|
if(event->type == AppiButtonEvent::EventTypeTick) {
|
2021-01-28 12:30:31 +00:00
|
|
|
if(emulated_result) {
|
|
|
|
emulated_result = false;
|
2020-11-16 11:21:58 +00:00
|
|
|
app->blink_green();
|
2020-11-25 07:25:13 +00:00
|
|
|
}
|
|
|
|
} else if(event->type == AppiButtonEvent::EventTypeKey) {
|
2021-02-10 08:56:05 +00:00
|
|
|
if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
|
2020-11-25 07:25:13 +00:00
|
|
|
app->decrease_dallas_address();
|
|
|
|
}
|
|
|
|
|
2021-02-10 08:56:05 +00:00
|
|
|
if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
|
2020-11-25 07:25:13 +00:00
|
|
|
app->increase_dallas_address();
|
|
|
|
}
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
2021-01-28 12:30:31 +00:00
|
|
|
|
|
|
|
onewire_slave->deattach();
|
|
|
|
memcpy(key.id_storage, state->dallas_address[state->dallas_address_index], 8);
|
|
|
|
onewire_slave->attach(&key);
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
void AppiButtonModeDallasEmulate::render(Canvas* canvas, AppiButtonState* state) {
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 2, 25, "< Dallas emulate >");
|
2020-11-25 07:25:13 +00:00
|
|
|
|
|
|
|
app->render_dallas_list(canvas, state);
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeDallasEmulate::acquire() {
|
|
|
|
onewire_slave->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeDallasEmulate::release() {
|
|
|
|
onewire_slave->stop();
|
|
|
|
}
|