sdnfc.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "app-template.h"
  2. extern "C" {
  3. #include <rfal_analogConfig.h>
  4. #include <rfal_rf.h>
  5. #include <rfal_nfc.h>
  6. #include <rfal_nfca.h>
  7. #include <st25r3916.h>
  8. #include <st25r3916_irq.h>
  9. }
  10. #include "fatfs/ff.h"
  11. #include "stm32_adafruit_sd.h"
  12. // event enumeration type
  13. typedef uint8_t event_t;
  14. // app state class
  15. class AppSdNFCState {
  16. public:
  17. // state data
  18. const char* name;
  19. // state initializer
  20. AppSdNFCState() {
  21. name = "sd nfc test";
  22. }
  23. };
  24. // app events class
  25. class AppSdNFCEvent {
  26. public:
  27. // events enum
  28. static const event_t EventTypeTick = 0;
  29. static const event_t EventTypeKey = 1;
  30. // payload
  31. union {
  32. InputEvent input;
  33. } value;
  34. // event type
  35. event_t type;
  36. };
  37. // our app derived from base AppTemplate class
  38. // with template variables <state, events>
  39. class AppSdNFC : public AppTemplate<AppSdNFCState, AppSdNFCEvent> {
  40. public:
  41. GpioPin* red_led_record;
  42. GpioPin* green_led_record;
  43. void run();
  44. void render(Canvas* canvas);
  45. void set_error(const char* text);
  46. void set_text(const char* text);
  47. void light_red();
  48. void light_green();
  49. void blink_red();
  50. void blink_green();
  51. };
  52. FATFS sd_fat_fs;
  53. char sd_path[6] = "";
  54. // start app
  55. void AppSdNFC::run() {
  56. // create pin
  57. GpioPin red_led = led_gpio[0];
  58. GpioPin green_led = led_gpio[1];
  59. // TODO open record
  60. red_led_record = &red_led;
  61. green_led_record = &green_led;
  62. // configure pin
  63. gpio_init(red_led_record, GpioModeOutputOpenDrain);
  64. gpio_init(green_led_record, GpioModeOutputOpenDrain);
  65. app_ready();
  66. uint8_t rfal_result = rfalNfcInitialize();
  67. if(rfal_result) {
  68. set_text("rfal init fail");
  69. blink_red();
  70. }
  71. uint8_t bsp_result = BSP_SD_Init();
  72. if(bsp_result) {
  73. set_error("sd init fail");
  74. }
  75. FRESULT result;
  76. result = f_mount(&sd_fat_fs, sd_path, 1);
  77. if(result) {
  78. set_error("sd mount fail");
  79. }
  80. light_green();
  81. set_text("all good");
  82. AppSdNFCEvent event;
  83. while(1) {
  84. if(get_event(&event, 1000)) {
  85. if(event.type == AppSdNFCEvent::EventTypeKey) {
  86. // press events
  87. if(event.value.input.state && event.value.input.input == InputBack) {
  88. exit();
  89. }
  90. }
  91. }
  92. // signal to force gui update
  93. update_gui();
  94. };
  95. }
  96. // render app
  97. void AppSdNFC::render(Canvas* canvas) {
  98. canvas_set_color(canvas, ColorBlack);
  99. canvas_set_font(canvas, FontPrimary);
  100. canvas_draw_str(canvas, 2, 12, state.name);
  101. }
  102. void AppSdNFC::set_error(const char* text) {
  103. light_red();
  104. set_text(text);
  105. update_gui();
  106. while(1)
  107. ;
  108. }
  109. void AppSdNFC::set_text(const char* text) {
  110. acquire_state();
  111. state.name = text;
  112. release_state();
  113. }
  114. void AppSdNFC::light_red() {
  115. gpio_write(red_led_record, false);
  116. }
  117. void AppSdNFC::light_green() {
  118. gpio_write(green_led_record, false);
  119. }
  120. void AppSdNFC::blink_red() {
  121. gpio_write(red_led_record, false);
  122. delay(500);
  123. gpio_write(red_led_record, true);
  124. delay(500);
  125. }
  126. void AppSdNFC::blink_green() {
  127. gpio_write(green_led_record, false);
  128. delay(500);
  129. gpio_write(green_led_record, true);
  130. delay(500);
  131. }
  132. // app enter function
  133. extern "C" void sdnfc(void* p) {
  134. AppSdNFC* app = new AppSdNFC();
  135. app->run();
  136. }