sdnfc.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(CanvasApi* 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. uint8_t rfal_result = rfalNfcInitialize();
  66. if(rfal_result) {
  67. set_text("rfal init fail");
  68. blink_red();
  69. }
  70. uint8_t bsp_result = BSP_SD_Init();
  71. if(bsp_result) {
  72. set_error("sd init fail");
  73. }
  74. FRESULT result;
  75. result = f_mount(&sd_fat_fs, sd_path, 1);
  76. if(result) {
  77. set_error("sd mount fail");
  78. }
  79. light_green();
  80. set_text("all good");
  81. AppSdNFCEvent event;
  82. while(1) {
  83. if(get_event(&event, 1000)) {
  84. if(event.type == AppSdNFCEvent::EventTypeKey) {
  85. // press events
  86. if(event.value.input.state && event.value.input.input == InputBack) {
  87. exit();
  88. }
  89. }
  90. }
  91. // signal to force gui update
  92. update_gui();
  93. };
  94. }
  95. // render app
  96. void AppSdNFC::render(CanvasApi* canvas) {
  97. canvas->set_color(canvas, ColorBlack);
  98. canvas->set_font(canvas, FontPrimary);
  99. canvas->draw_str(canvas, 2, 12, state.name);
  100. }
  101. void AppSdNFC::set_error(const char* text) {
  102. light_red();
  103. set_text(text);
  104. update_gui();
  105. while(1)
  106. ;
  107. }
  108. void AppSdNFC::set_text(const char* text) {
  109. acquire_state();
  110. state.name = text;
  111. release_state();
  112. }
  113. void AppSdNFC::light_red() {
  114. gpio_write(red_led_record, false);
  115. }
  116. void AppSdNFC::light_green() {
  117. gpio_write(green_led_record, false);
  118. }
  119. void AppSdNFC::blink_red() {
  120. gpio_write(red_led_record, false);
  121. delay(500);
  122. gpio_write(red_led_record, true);
  123. delay(500);
  124. }
  125. void AppSdNFC::blink_green() {
  126. gpio_write(green_led_record, false);
  127. delay(500);
  128. gpio_write(green_led_record, true);
  129. delay(500);
  130. }
  131. // app enter function
  132. extern "C" void sdnfc(void* p) {
  133. AppSdNFC* app = new AppSdNFC();
  134. app->run();
  135. }