ibutton.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "ibutton.h"
  2. #include "ibutton_mode_dallas_read.h"
  3. #include "ibutton_mode_dallas_emulate.h"
  4. #include "ibutton_mode_cyfral_read.h"
  5. #include "ibutton_mode_cyfral_emulate.h"
  6. // start app
  7. void AppiButton::run() {
  8. mode[0] = new AppiButtonModeDallasRead(this);
  9. mode[1] = new AppiButtonModeDallasEmulate(this);
  10. mode[2] = new AppiButtonModeCyfralRead(this);
  11. mode[3] = new AppiButtonModeCyfralEmulate(this);
  12. switch_to_mode(0);
  13. // TODO open record
  14. red_led_record = &led_gpio[0];
  15. green_led_record = &led_gpio[1];
  16. // configure pin
  17. gpio_init(red_led_record, GpioModeOutputOpenDrain);
  18. gpio_init(green_led_record, GpioModeOutputOpenDrain);
  19. app_ready();
  20. AppiButtonEvent event;
  21. while(1) {
  22. if(get_event(&event, 20)) {
  23. if(event.type == AppiButtonEvent::EventTypeKey) {
  24. // press events
  25. if(event.value.input.state && event.value.input.input == InputBack) {
  26. printf("[ibutton] bye!\n");
  27. // TODO remove all widgets create by app
  28. widget_enabled_set(widget, false);
  29. furiac_exit(NULL);
  30. }
  31. if(event.value.input.state && event.value.input.input == InputLeft) {
  32. decrease_mode();
  33. }
  34. if(event.value.input.state && event.value.input.input == InputRight) {
  35. increase_mode();
  36. }
  37. }
  38. } else {
  39. event.type = AppiButtonEvent::EventTypeTick;
  40. }
  41. acquire_state();
  42. mode[state.mode_index]->event(&event, &state);
  43. release_state();
  44. widget_update(widget);
  45. };
  46. }
  47. // render app
  48. void AppiButton::render(Canvas* canvas) {
  49. canvas_set_color(canvas, ColorBlack);
  50. canvas_set_font(canvas, FontPrimary);
  51. canvas_draw_str(canvas, 2, 12, "iButton");
  52. mode[state.mode_index]->render(canvas, &state);
  53. }
  54. void AppiButton::render_dallas_list(Canvas* canvas, AppiButtonState* state) {
  55. const uint8_t buffer_size = 50;
  56. char buf[buffer_size];
  57. for(uint8_t i = 0; i < state->dallas_address_count; i++) {
  58. snprintf(
  59. buf,
  60. buffer_size,
  61. "%s[%u] %x:%x:%x:%x:%x:%x:%x:%x",
  62. (i == state->dallas_address_index) ? "> " : "",
  63. i + 1,
  64. state->dallas_address[i][0],
  65. state->dallas_address[i][1],
  66. state->dallas_address[i][2],
  67. state->dallas_address[i][3],
  68. state->dallas_address[i][4],
  69. state->dallas_address[i][5],
  70. state->dallas_address[i][6],
  71. state->dallas_address[i][7]);
  72. canvas_draw_str(canvas, 2, 37 + i * 12, buf);
  73. }
  74. }
  75. void AppiButton::render_cyfral_list(Canvas* canvas, AppiButtonState* state) {
  76. const uint8_t buffer_size = 50;
  77. char buf[buffer_size];
  78. for(uint8_t i = 0; i < state->cyfral_address_count; i++) {
  79. snprintf(
  80. buf,
  81. buffer_size,
  82. "%s[%u] %x:%x:%x:%x",
  83. (i == state->cyfral_address_index) ? "> " : "",
  84. i + 1,
  85. state->cyfral_address[i][0],
  86. state->cyfral_address[i][1],
  87. state->cyfral_address[i][2],
  88. state->cyfral_address[i][3]);
  89. canvas_draw_str(canvas, 2, 37 + i * 12, buf);
  90. }
  91. }
  92. void AppiButton::blink_red() {
  93. gpio_write(red_led_record, 0);
  94. delay(10);
  95. gpio_write(red_led_record, 1);
  96. }
  97. void AppiButton::blink_green() {
  98. gpio_write(green_led_record, 0);
  99. delay(10);
  100. gpio_write(green_led_record, 1);
  101. }
  102. void AppiButton::increase_mode() {
  103. acquire_state();
  104. if(state.mode_index < (modes_count - 1)) {
  105. mode[state.mode_index]->release();
  106. state.mode_index++;
  107. mode[state.mode_index]->acquire();
  108. }
  109. release_state();
  110. }
  111. void AppiButton::decrease_mode() {
  112. acquire_state();
  113. if(state.mode_index > 0) {
  114. mode[state.mode_index]->release();
  115. state.mode_index--;
  116. mode[state.mode_index]->acquire();
  117. }
  118. release_state();
  119. }
  120. void AppiButton::increase_dallas_address() {
  121. if(state.dallas_address_index < (state.dallas_address_count - 1)) {
  122. state.dallas_address_index++;
  123. }
  124. }
  125. void AppiButton::decrease_dallas_address() {
  126. if(state.dallas_address_index > 0) {
  127. state.dallas_address_index--;
  128. }
  129. }
  130. void AppiButton::increase_cyfral_address() {
  131. if(state.cyfral_address_index < (state.cyfral_address_count - 1)) {
  132. state.cyfral_address_index++;
  133. }
  134. }
  135. void AppiButton::decrease_cyfral_address() {
  136. if(state.cyfral_address_index > 0) {
  137. state.cyfral_address_index--;
  138. }
  139. }
  140. void AppiButton::switch_to_mode(uint8_t mode_index) {
  141. mode[state.mode_index]->release();
  142. state.mode_index = mode_index;
  143. mode[state.mode_index]->acquire();
  144. }
  145. // app enter function
  146. extern "C" void app_ibutton(void* p) {
  147. AppiButton* app = new AppiButton();
  148. app->run();
  149. }