2020-11-25 07:25:13 +00:00
|
|
|
#pragma once
|
|
|
|
#include "ibutton.h"
|
2020-12-22 14:44:38 +00:00
|
|
|
#include "cyfral_reader_comp.h"
|
2020-11-25 07:25:13 +00:00
|
|
|
|
|
|
|
class AppiButtonModeCyfralRead : public AppTemplateMode<AppiButtonState, AppiButtonEvent> {
|
|
|
|
public:
|
|
|
|
const char* name = "cyfral read";
|
|
|
|
AppiButton* app;
|
2020-12-22 14:44:38 +00:00
|
|
|
CyfralReaderComp* reader;
|
2020-11-25 07:25:13 +00:00
|
|
|
|
|
|
|
void event(AppiButtonEvent* event, AppiButtonState* state);
|
2020-12-14 10:50:32 +00:00
|
|
|
void render(Canvas* canvas, AppiButtonState* state);
|
2020-11-25 07:25:13 +00:00
|
|
|
void acquire();
|
|
|
|
void release();
|
|
|
|
|
|
|
|
AppiButtonModeCyfralRead(AppiButton* parent_app) {
|
|
|
|
app = parent_app;
|
2020-12-22 14:44:38 +00:00
|
|
|
|
|
|
|
// TODO open record
|
|
|
|
const GpioPin* one_wire_pin_record = &ibutton_gpio;
|
|
|
|
reader = new CyfralReaderComp(one_wire_pin_record);
|
2020-11-25 07:25:13 +00:00
|
|
|
};
|
2020-12-22 14:44:38 +00:00
|
|
|
|
|
|
|
static const uint8_t key_length = 4;
|
|
|
|
static const uint8_t num_keys_to_check = 4;
|
|
|
|
uint8_t key_index = 0;
|
|
|
|
uint8_t keys[num_keys_to_check][4];
|
2020-11-25 07:25:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void AppiButtonModeCyfralRead::event(AppiButtonEvent* event, AppiButtonState* state) {
|
|
|
|
if(event->type == AppiButtonEvent::EventTypeTick) {
|
2020-12-22 14:44:38 +00:00
|
|
|
// if we read a key
|
|
|
|
if(reader->read(keys[key_index], key_length)) {
|
|
|
|
// read next key
|
|
|
|
key_index++;
|
|
|
|
|
|
|
|
// if we read sufficient amount of keys
|
|
|
|
if(key_index >= num_keys_to_check) {
|
|
|
|
bool result = true;
|
|
|
|
key_index = 0;
|
|
|
|
|
|
|
|
// compare all keys
|
|
|
|
for(uint8_t i = 1; i < num_keys_to_check; i++) {
|
|
|
|
if(memcmp(keys[i], keys[i - 1], key_length) != 0) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all keys is same
|
|
|
|
if(result) {
|
|
|
|
// copy key to mem and blink
|
|
|
|
memcpy(
|
|
|
|
app->state.cyfral_address[app->state.cyfral_address_index],
|
|
|
|
keys[key_index],
|
|
|
|
key_length);
|
|
|
|
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_cyfral_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_cyfral_address();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
void AppiButtonModeCyfralRead::render(Canvas* canvas, AppiButtonState* state) {
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 2, 25, "< Cyfral read >");
|
2020-11-25 07:25:13 +00:00
|
|
|
|
|
|
|
app->render_cyfral_list(canvas, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeCyfralRead::acquire() {
|
|
|
|
reader->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeCyfralRead::release() {
|
|
|
|
reader->stop();
|
|
|
|
}
|