2020-11-16 11:21:58 +00:00
|
|
|
#pragma once
|
|
|
|
#include "ibutton.h"
|
2021-01-28 12:30:31 +00:00
|
|
|
#include "one_wire_master.h"
|
2020-11-25 07:25:13 +00:00
|
|
|
#include "maxim_crc.h"
|
2020-11-16 11:21:58 +00:00
|
|
|
|
|
|
|
class AppiButtonModeDallasRead : public AppTemplateMode<AppiButtonState, AppiButtonEvent> {
|
|
|
|
public:
|
|
|
|
const char* name = "dallas read";
|
|
|
|
AppiButton* app;
|
2021-01-28 12:30:31 +00:00
|
|
|
OneWireMaster* onewire;
|
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();
|
|
|
|
|
|
|
|
AppiButtonModeDallasRead(AppiButton* parent_app) {
|
|
|
|
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 = new OneWireMaster(one_wire_pin_record);
|
2020-11-16 11:21:58 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
void AppiButtonModeDallasRead::event(AppiButtonEvent* event, AppiButtonState* state) {
|
|
|
|
if(event->type == AppiButtonEvent::EventTypeTick) {
|
|
|
|
bool result = 0;
|
|
|
|
uint8_t address[8];
|
|
|
|
|
|
|
|
osKernelLock();
|
|
|
|
result = onewire->reset();
|
|
|
|
osKernelUnlock();
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
osKernelLock();
|
2021-01-28 12:30:31 +00:00
|
|
|
__disable_irq();
|
2020-11-16 11:21:58 +00:00
|
|
|
onewire->write(0x33);
|
|
|
|
onewire->read_bytes(address, 8);
|
2021-01-28 12:30:31 +00:00
|
|
|
__enable_irq();
|
2020-11-16 11:21:58 +00:00
|
|
|
osKernelUnlock();
|
|
|
|
|
2020-11-25 07:25:13 +00:00
|
|
|
if(maxim_crc8(address, 8) == 0) {
|
|
|
|
memcpy(app->state.dallas_address[app->state.dallas_address_index], address, 8);
|
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) {
|
|
|
|
if(event->value.input.state && event->value.input.input == InputUp) {
|
|
|
|
app->decrease_dallas_address();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->value.input.state && event->value.input.input == InputDown) {
|
|
|
|
app->increase_dallas_address();
|
|
|
|
}
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:50:32 +00:00
|
|
|
void AppiButtonModeDallasRead::render(Canvas* canvas, AppiButtonState* state) {
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 2, 25, "Dallas read >");
|
2020-11-25 07:25:13 +00:00
|
|
|
app->render_dallas_list(canvas, state);
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeDallasRead::acquire() {
|
|
|
|
onewire->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButtonModeDallasRead::release() {
|
|
|
|
onewire->stop();
|
|
|
|
}
|