sdnfc.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.type == InputTypeShort &&
  88. event.value.input.key == InputKeyBack) {
  89. exit();
  90. }
  91. }
  92. }
  93. // signal to force gui update
  94. update_gui();
  95. };
  96. }
  97. // render app
  98. void AppSdNFC::render(Canvas* canvas) {
  99. canvas_set_color(canvas, ColorBlack);
  100. canvas_set_font(canvas, FontPrimary);
  101. canvas_draw_str(canvas, 2, 12, state.name);
  102. }
  103. void AppSdNFC::set_error(const char* text) {
  104. light_red();
  105. set_text(text);
  106. update_gui();
  107. while(1)
  108. ;
  109. }
  110. void AppSdNFC::set_text(const char* text) {
  111. acquire_state();
  112. state.name = text;
  113. release_state();
  114. }
  115. void AppSdNFC::light_red() {
  116. gpio_write(red_led_record, false);
  117. }
  118. void AppSdNFC::light_green() {
  119. gpio_write(green_led_record, false);
  120. }
  121. void AppSdNFC::blink_red() {
  122. gpio_write(red_led_record, false);
  123. delay(500);
  124. gpio_write(red_led_record, true);
  125. delay(500);
  126. }
  127. void AppSdNFC::blink_green() {
  128. gpio_write(green_led_record, false);
  129. delay(500);
  130. gpio_write(green_led_record, true);
  131. delay(500);
  132. }
  133. // app enter function
  134. extern "C" int32_t sdnfc(void* p) {
  135. AppSdNFC* app = new AppSdNFC();
  136. app->run();
  137. return 0;
  138. }