ibutton.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, 20)) {
  26. if(event.type == AppiButtonEvent::EventTypeKey) {
  27. // press events
  28. if(event.value.input.state && event.value.input.input == InputBack) {
  29. view_port_enabled_set(view_port, false);
  30. gui_remove_view_port(gui, view_port);
  31. api_hal_timebase_insomnia_exit();
  32. osThreadExit();
  33. }
  34. if(event.value.input.state && event.value.input.input == InputLeft) {
  35. decrease_mode();
  36. }
  37. if(event.value.input.state && event.value.input.input == InputRight) {
  38. increase_mode();
  39. }
  40. }
  41. } else {
  42. event.type = AppiButtonEvent::EventTypeTick;
  43. }
  44. acquire_state();
  45. mode[state.mode_index]->event(&event, &state);
  46. release_state();
  47. view_port_update(view_port);
  48. };
  49. }
  50. // render app
  51. void AppiButton::render(Canvas* canvas) {
  52. canvas_set_color(canvas, ColorBlack);
  53. canvas_set_font(canvas, FontPrimary);
  54. canvas_draw_str(canvas, 2, 12, "iButton");
  55. mode[state.mode_index]->render(canvas, &state);
  56. }
  57. void AppiButton::render_dallas_list(Canvas* canvas, AppiButtonState* state) {
  58. const uint8_t buffer_size = 50;
  59. char buf[buffer_size];
  60. for(uint8_t i = 0; i < state->dallas_address_count; i++) {
  61. snprintf(
  62. buf,
  63. buffer_size,
  64. "%s[%u] %x:%x:%x:%x:%x:%x:%x:%x",
  65. (i == state->dallas_address_index) ? "> " : "",
  66. i + 1,
  67. state->dallas_address[i][0],
  68. state->dallas_address[i][1],
  69. state->dallas_address[i][2],
  70. state->dallas_address[i][3],
  71. state->dallas_address[i][4],
  72. state->dallas_address[i][5],
  73. state->dallas_address[i][6],
  74. state->dallas_address[i][7]);
  75. canvas_draw_str(canvas, 2, 37 + i * 12, buf);
  76. }
  77. }
  78. void AppiButton::render_cyfral_list(Canvas* canvas, AppiButtonState* state) {
  79. const uint8_t buffer_size = 50;
  80. char buf[buffer_size];
  81. for(uint8_t i = 0; i < state->cyfral_address_count; i++) {
  82. snprintf(
  83. buf,
  84. buffer_size,
  85. "%s[%u] %x:%x:%x:%x",
  86. (i == state->cyfral_address_index) ? "> " : "",
  87. i + 1,
  88. state->cyfral_address[i][0],
  89. state->cyfral_address[i][1],
  90. state->cyfral_address[i][2],
  91. state->cyfral_address[i][3]);
  92. canvas_draw_str(canvas, 2, 37 + i * 12, buf);
  93. }
  94. }
  95. void AppiButton::blink_red() {
  96. gpio_write(red_led_record, 0);
  97. delay(10);
  98. gpio_write(red_led_record, 1);
  99. }
  100. void AppiButton::blink_green() {
  101. gpio_write(green_led_record, 0);
  102. delay(10);
  103. gpio_write(green_led_record, 1);
  104. }
  105. void AppiButton::increase_mode() {
  106. acquire_state();
  107. if(state.mode_index < (modes_count - 1)) {
  108. mode[state.mode_index]->release();
  109. state.mode_index++;
  110. mode[state.mode_index]->acquire();
  111. }
  112. release_state();
  113. }
  114. void AppiButton::decrease_mode() {
  115. acquire_state();
  116. if(state.mode_index > 0) {
  117. mode[state.mode_index]->release();
  118. state.mode_index--;
  119. mode[state.mode_index]->acquire();
  120. }
  121. release_state();
  122. }
  123. void AppiButton::increase_dallas_address() {
  124. if(state.dallas_address_index < (state.dallas_address_count - 1)) {
  125. state.dallas_address_index++;
  126. }
  127. }
  128. void AppiButton::decrease_dallas_address() {
  129. if(state.dallas_address_index > 0) {
  130. state.dallas_address_index--;
  131. }
  132. }
  133. void AppiButton::increase_cyfral_address() {
  134. if(state.cyfral_address_index < (state.cyfral_address_count - 1)) {
  135. state.cyfral_address_index++;
  136. }
  137. }
  138. void AppiButton::decrease_cyfral_address() {
  139. if(state.cyfral_address_index > 0) {
  140. state.cyfral_address_index--;
  141. }
  142. }
  143. void AppiButton::switch_to_mode(uint8_t mode_index) {
  144. mode[state.mode_index]->release();
  145. state.mode_index = mode_index;
  146. mode[state.mode_index]->acquire();
  147. }
  148. // app enter function
  149. extern "C" void app_ibutton(void* p) {
  150. AppiButton* app = new AppiButton();
  151. app->run();
  152. }