ibutton.cpp 5.1 KB

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