2020-11-16 11:21:58 +00:00
|
|
|
#include "ibutton.h"
|
|
|
|
#include "ibutton_mode_dallas_read.h"
|
|
|
|
#include "ibutton_mode_dallas_emulate.h"
|
|
|
|
|
|
|
|
// start app
|
|
|
|
void AppiButton::run() {
|
2020-11-16 21:10:58 +00:00
|
|
|
acquire_state();
|
2020-11-16 11:21:58 +00:00
|
|
|
mode[0] = new AppiButtonModeDallasRead(this);
|
|
|
|
mode[1] = new AppiButtonModeDallasEmulate(this);
|
2020-11-16 21:10:58 +00:00
|
|
|
release_state();
|
|
|
|
|
|
|
|
switch_to_mode(0);
|
2020-11-16 11:21:58 +00:00
|
|
|
|
|
|
|
// create pin
|
|
|
|
GpioPin red_led = led_gpio[0];
|
|
|
|
GpioPin green_led = led_gpio[1];
|
|
|
|
|
|
|
|
// TODO open record
|
|
|
|
red_led_record = &red_led;
|
|
|
|
green_led_record = &green_led;
|
|
|
|
|
|
|
|
// configure pin
|
|
|
|
gpio_init(red_led_record, GpioModeOutputOpenDrain);
|
|
|
|
gpio_init(green_led_record, GpioModeOutputOpenDrain);
|
|
|
|
|
|
|
|
AppiButtonEvent event;
|
|
|
|
while(1) {
|
2020-11-18 12:51:23 +00:00
|
|
|
if(get_event(&event, 100)) {
|
2020-11-16 11:21:58 +00:00
|
|
|
if(event.type == AppiButtonEvent::EventTypeKey) {
|
|
|
|
// press events
|
|
|
|
if(event.value.input.state && event.value.input.input == InputBack) {
|
|
|
|
printf("[ibutton] 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) {
|
|
|
|
decrease_mode();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputRight) {
|
|
|
|
increase_mode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.type = AppiButtonEvent::EventTypeTick;
|
|
|
|
}
|
|
|
|
|
|
|
|
acquire_state();
|
|
|
|
mode[state.mode_index]->event(&event, &state);
|
|
|
|
release_state();
|
|
|
|
|
|
|
|
widget_update(widget);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// render app
|
|
|
|
void AppiButton::render(CanvasApi* canvas) {
|
|
|
|
canvas->set_color(canvas, ColorBlack);
|
|
|
|
canvas->set_font(canvas, FontPrimary);
|
|
|
|
canvas->draw_str(canvas, 2, 12, "iButton");
|
|
|
|
|
2020-11-18 12:51:23 +00:00
|
|
|
if(mode[state.mode_index] != NULL) {
|
|
|
|
mode[state.mode_index]->render(canvas, &state);
|
|
|
|
}
|
2020-11-16 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButton::blink_red() {
|
|
|
|
gpio_write(red_led_record, 0);
|
|
|
|
delay(10);
|
|
|
|
gpio_write(red_led_record, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButton::blink_green() {
|
|
|
|
gpio_write(green_led_record, 0);
|
|
|
|
delay(10);
|
|
|
|
gpio_write(green_led_record, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButton::increase_mode() {
|
|
|
|
acquire_state();
|
|
|
|
if(state.mode_index < (modes_count - 1)) {
|
|
|
|
mode[state.mode_index]->release();
|
|
|
|
state.mode_index++;
|
|
|
|
mode[state.mode_index]->acquire();
|
|
|
|
}
|
|
|
|
release_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppiButton::decrease_mode() {
|
|
|
|
acquire_state();
|
|
|
|
if(state.mode_index > 0) {
|
|
|
|
mode[state.mode_index]->release();
|
|
|
|
state.mode_index--;
|
|
|
|
mode[state.mode_index]->acquire();
|
|
|
|
}
|
|
|
|
release_state();
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:10:58 +00:00
|
|
|
void AppiButton::switch_to_mode(uint8_t mode_index) {
|
|
|
|
acquire_state();
|
|
|
|
mode[state.mode_index]->release();
|
|
|
|
state.mode_index = mode_index;
|
|
|
|
mode[state.mode_index]->acquire();
|
|
|
|
release_state();
|
|
|
|
}
|
|
|
|
|
2020-11-16 11:21:58 +00:00
|
|
|
// app enter function
|
|
|
|
extern "C" void app_ibutton(void* p) {
|
|
|
|
AppiButton* app = new AppiButton();
|
|
|
|
app->run();
|
|
|
|
}
|